Skip to content

Latest commit

 

History

History

zod-request

@nextvalid/zod-request

npm size node dist ci codecov techdebt maintainability license

Highlights

  • 🚀  Dead simple

Documentation

Install

npm install @nextvalid/zod-request --save  # via npm
yarn add @nextvalid/zod-request            # via yarn

Quick start

Api routes

Define a schema

import type { NextApiHandler } from "next";
import { zodReq } from "@nextvalid/zod-request";
import { z } from "zod";

const schema = zodReq({
  method: "GET",
  query: {
    email: z.string().email("Invalid email").optional(),
  },
  headers: {
    // authorization: z.string().regex(/^bearer /i),
  },
});

const getHandler: NextApiHandler = async (req, res) => {
  const { query, headers } = schema.parse(req);

  const { email } = query; // email is typed to `string | undefined`
  res.json({
    email: email ? `Your email is ${email}` : `No email provided`,
  });
};

export default withApiErrorHandler({
  logger: new ConsoleLogger(),
})(getHandler);

SSR pages

getServerSideProps, getStaticProps

Support

Open an issue.

Sponsors

If my OSS work brightens your day, let's take it to new heights together! Sponsor, coffee, or star – any gesture of support fuels my passion to improve. Thanks for being awesome! 🙏❤️

License

MIT © belgattitude and contributors.