next-handler
TypeScript icon, indicating that this package has built-in type declarations

0.2.2 • Public • Published

🧑‍🔧
next-handler
npm package

Type save handler generator for Next.js API Routes.



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();

Package Sidebar

Install

npm i next-handler

Weekly Downloads

2

Version

0.2.2

License

MIT

Unpacked Size

68 kB

Total Files

6

Last publish

Collaborators

  • mkreuzmayr