Created
August 20, 2022 21:44
-
-
Save wzulfikar/9fbe4ef13429e0f29581e611bdc30251 to your computer and use it in GitHub Desktop.
DEV.to Post: Typing for Next.js API - pages/api
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
import { ApiHandler, handle, z } from "@backend"; | |
const schema = z.object({ | |
query: z.object({ | |
name: z.string(), | |
}), | |
}); | |
export const response = z.object({ | |
greeting: z.string(), | |
}); | |
const handler: ApiHandler<typeof schema, typeof response> = (req, res) => { | |
const { name } = req.query; | |
res.status(200).json({ success: true, data: { greeting: `Hello, ${name}!` } }); | |
}; | |
export default handle(handler, { schema }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment