- 🚀 Dead simple
npm install @nextvalid/zod-request --save # via npm
yarn add @nextvalid/zod-request # via yarn
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);
getServerSideProps
, getStaticProps
Open an issue.
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! 🙏❤️
MIT © belgattitude and contributors.