Skip to content

Instantly share code, notes, and snippets.

import { Middleware } from 'next-api-route-middleware';
export const captureErrors: Middleware = async (req, res, next) => {
try {
await next();
} catch (error) {
console.error(error);
Sentry.captureException(error);
res.status(500).send({ message: 'Server error!' });
}
import type { NextApiResponse } from 'next';
import * as z from 'zod';
import { addUser, allowMethods, captureErrors, NextApiRequestWithUser, validateBody } from '../../middleware';
import { use } from 'next-api-route-middleware';
const formSchema = z
.object({
email: z.string().email(),
password: z.string().min(4),
passwordConfirm: z.string().min(4),
import type { NextApiRequest, NextApiResponse } from 'next';
type User = { userId: string };
const allowedMethods = ['GET'];
const formSchema = z
.object({
email: z.string().email(),
password: z.string().min(4),
import type { NextApiRequest, NextApiResponse } from 'next';
type User = { userId: string };
const allowedMethods = ['GET'];
const handler = (req: NextApiRequest, res: NextApiResponse<User>) => {
try {
if (!allowedMethods.includes(req.method!) || req.method == 'OPTIONS') {
return res.status(405).send({ message: 'Method not allowed.' });
import type { NextApiRequest, NextApiResponse } from 'next';
type User = { userId: string };
const allowedMethods = ['GET'];
const handler = (req: NextApiRequest, res: NextApiResponse<User>) => {
// If the req.method isn't included in the list of allowed methods we return a 405
if (!allowedMethods.includes(req.method!) || req.method == 'OPTIONS') {
return res.status(405).send({ message: 'Method not allowed.' });
import { use } from 'next-api-route-middleware';
const handler = async (req: NextApiRequest, res: NextApiResponse<{ message: string }>) => {
return res.status(200).json({ message: 'request was good!' });
};
export default use(validateBody(formSchema), handler);
import * as z from 'zod';
import { Middleware } from 'next-api-route-middleware';
export const validateBody = (zodSchema: z.ZodSchema): Middleware => {
return async function (req, res, next) {
const body = typeof req.body === 'object' ? req.body : JSON.parse(req.body);
const validation = zodSchema.safeParse(body);
if (!validation.success) {
return res.status(400).send({
const formSchema = z
.object({
email: z.string().email(),
password: z.string().min(4),
passwordConfirm: z.string().min(4),
})
.refine((data) => data.password === data.passwordConfirm, {
message: "Passwords don't match",
});
import { use, Middleware } from 'next-api-route-middleware';
export const allowMethods = (allowedMethods: string[]): Middleware => {
return async function (req, res, next) {
if (allowedMethods.includes(req.method!) || req.method == 'OPTIONS') {
next();
} else {
res.status(405).send({ message: 'Method not allowed.' });
}
};
import { Middleware } from 'next-api-route-middleware';
import { getUserByCookie } from '../utils';
export type User = { userId: string };
export type NextApiRequestWithUser = NextApiRequest & User;
export const addUser: Middleware<NextApiRequestWithUser> = async (req, res, next) => {
const authCookie = await getUserByCookie();
if (authCookie) {