Start work on API endpoints and API access used by frontend adapters#79
Open
Start work on API endpoints and API access used by frontend adapters#79
Conversation
0fc6207 to
bb1cdfa
Compare
bb1cdfa to
6a45b80
Compare
sjaghori
requested changes
Jun 2, 2024
Collaborator
sjaghori
left a comment
There was a problem hiding this comment.
Looks good! 🚀
I got some comments...
| "kysely": "^0.27.3", | ||
| "pino": "^9.0.0", | ||
| "pino-pretty": "^11.0.0", | ||
| "uuid": "^9.0.1", |
Collaborator
There was a problem hiding this comment.
since node v20 the std. crypto library has builtin uuid v4 generator. Let's not add the dependency here.
| import { createTableMigration } from '../migration.util' | ||
|
|
||
| export async function up(db: Kysely<unknown>): Promise<void> { | ||
| await createTableMigration(db, 'apiaccess') |
Collaborator
There was a problem hiding this comment.
Suggested change
| await createTableMigration(db, 'apiaccess') | |
| await createTableMigration(db, 'api_access') |
|
|
||
| export async function up(db: Kysely<unknown>): Promise<void> { | ||
| await createTableMigration(db, 'apiaccess') | ||
| .addColumn('apikey', 'text', (col) => col.unique().notNull()) |
Collaborator
There was a problem hiding this comment.
Suggested change
| .addColumn('apikey', 'text', (col) => col.unique().notNull()) | |
| .addColumn('api_key', 'text', (col) => col.unique().notNull()) |
what do you think?
| import { z } from 'zod' | ||
|
|
||
| const translationKeySchema = z.string().brand('translation-key') | ||
| export type TranslationKey = z.infer<typeof translationKeySchema> |
Collaborator
There was a problem hiding this comment.
Do we want to keep this in this folder?
| @@ -1 +1 @@ | |||
| pnpm exec lint-staged No newline at end of file | |||
| npm test | |||
Collaborator
There was a problem hiding this comment.
we also want to run lint-staged no?
| } | ||
|
|
||
| return validationResult.data | ||
| } |
Collaborator
There was a problem hiding this comment.
Great, we got something similar in production, here is the snippet.
Theoratically the request payload clould be a formdata as well, let's support both json and formdata.
import type { Logger } from 'pino'
import { ZodError, type z } from 'zod'
type ParsedRequestData<T extends z.ZodType> =
| {
status: 'success'
data: z.output<T>
}
| {
status: 'error'
data: {
validationErrorMessage: string
code: number
}
}
export const parseRequestData = async <T extends z.ZodType>(
schema: T,
request: Request,
logger: Logger,
kind: 'json' | 'from-data' = 'json'
): Promise<ParsedRequestData<T>> => {
let parsedPayload: z.output<typeof schema>
try {
let payload: unknown
if (kind === 'json') {
payload = await request.json()
} else {
payload = Object.fromEntries(await request.formData())
}
parsedPayload = schema.parse(payload)
} catch (e: unknown) {
let validationErrorMessage: string = 'Error parsing request payload.'
let code = 500
if (e instanceof ZodError) {
validationErrorMessage = `Error validating request payload against zod schema. ${e.message}`
code = 400
logger.error({
message: validationErrorMessage,
name: e.name,
errors: e.issues
})
} else {
logger.error(validationErrorMessage)
}
return { status: 'error', data: { validationErrorMessage, code } }
}
return { status: 'success', data: parsedPayload }
}
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR starts adding some groundwork necessary for the endpoints that allow frontend adapters to sync.
Among other minor things, it: