Skip to content

Instantly share code, notes, and snippets.

@wzulfikar
Created August 20, 2022 21:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wzulfikar/9fbe4ef13429e0f29581e611bdc30251 to your computer and use it in GitHub Desktop.
Save wzulfikar/9fbe4ef13429e0f29581e611bdc30251 to your computer and use it in GitHub Desktop.
DEV.to Post: Typing for Next.js API - pages/api
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