I am trying to create a util like https://nuxt.com/docs/4.x/guide/directory-structure/server#server-utilities so I can reuse validation code like:
const user = await serverSupabaseUser(event)
if (!user) {
return sendError(event, createError({
statusCode: 401,
statusMessage: 'Unauthorized'
}))
}
or
const paramsValidation = await getValidatedRouterParams(event, ValidIdUUID.safeParse)
if (!paramsValidation.success) {
return sendError(event, createError({
statusCode: 400,
statusMessage: `${paramsValidation.error}`
}))
}
The util handler would take:
{ user?: boolean // Validate the user or not, paramsValidation?: zod.safeParse, etc... }
as first argument, and then inject into event.context.
Then on the endpoint handler itself, event.context.user would be type User if we passed { user: true } to validate it. Same for params, we would get the parsedData from zod.
Is this possible? Some examples out there?