This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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!' }); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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), |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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), |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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.' }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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.' }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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({ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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", | |
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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.' }); | |
| } | |
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) { |
NewerOlder