taxonomy/lib/api-middlewares/with-methods.ts

12 lines
339 B
TypeScript
Raw Permalink Normal View History

2022-10-26 13:18:06 +00:00
import type { NextApiHandler, NextApiRequest, NextApiResponse } from "next"
export function withMethods(methods: string[], handler: NextApiHandler) {
return async function (req: NextApiRequest, res: NextApiResponse) {
if (!methods.includes(req.method)) {
return res.status(405).end()
}
return handler(req, res)
}
}