@nextvalid/zod-request
TypeScript icon, indicating that this package has built-in type declarations

0.4.0 • Public • Published

@nextvalid/zod-request

npm size node dist ci codecov techdebt maintainability license ko-fi

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.

Contributors

Contributions are warmly appreciated. Have a look to the CONTRIBUTING document.

Sponsors

If you are enjoying some of my OSS guides or libs for your company, I'd really appreciate a sponsorship, a coffee or a dropped star. That gives me a tasty morning boost and help me to make some of my ideas come true 🙏

License

MIT

Package Sidebar

Install

npm i @nextvalid/zod-request

Weekly Downloads

110

Version

0.4.0

License

MIT

Unpacked Size

44.5 kB

Total Files

7

Last publish

Collaborators

  • s.vanvelthem