Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- interface WrapperOptions<
- Res,
- Input extends AnyZodObject | undefined = undefined,
- > {
- input?: Input
- resolve(_args: {
- input: Input extends AnyZodObject ? Input['_output'] : undefined
- req: NextApiRequest
- res: NextApiResponse<Res>
- }): Promise<void>
- }
- export function wrapper<
- Res = unknown,
- Input extends AnyZodObject | undefined = undefined,
- >({ input, resolve }: WrapperOptions<Res, Input>): NextApiHandler<unknown> {
- return async function (req, res) {
- try {
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
- let parsedInput: any = undefined
- if (input) {
- const data = JSON.parse(req.body)
- parsedInput = await input.parseAsync(data)
- }
- return resolve({ input: parsedInput, req, res })
- } catch (e) {
- res.status(500).send(fallbackErrorMessage)
- return
- }
- }
- }
- wrapper<{ data: number }>({
- input: z.object({ hello: z.string(), bye: z.string() }),
- async resolve({ input, req, res }) {
- res.status(300).send({ data: 500 })
- throw new APIError(405, 'Method not allowed')
- },
- })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement