next-handler
v0.2.2
Published
Type save handler generator for Next.js API Routes.
Downloads
7
Maintainers
Readme
Features
- 🔒 Full type safety on body, query & response
- ✅ Zod type validation out of the box
- ✉️ Send json response via return
- 👮 Error and notFound handlers
- 🍃 Zero dependencies
Installation
npm install next-handler
# or
yarn add next-handler
Basic Usage
import { nh } from 'next-handler';
const handler = nh().post(
{
query: z.object({
id: z.string().transform((id) => parseInt(id)),
}),
body: z.object({
data: z.string(),
}),
},
({ req }) => {
// full type safety provided on query and body
data.set(req.query.id, req.body.data);
// send json response via return
return { ok: true };
}
);
export default handler.build();