cal.pub0.org/lib/helpers/httpMethods.ts

18 lines
607 B
TypeScript
Raw Normal View History

import { NextMiddleware } from "next-api-middleware";
2022-03-30 12:17:55 +00:00
export const httpMethod = (allowedHttpMethod: "GET" | "POST" | "PATCH" | "DELETE"): NextMiddleware => {
return async function (req, res, next) {
if (req.method === allowedHttpMethod || req.method == "OPTIONS") {
await next();
} else {
2022-03-30 12:17:55 +00:00
res.status(405).json({ message: `Only ${allowedHttpMethod} Method allowed` });
res.end();
}
};
};
2022-03-30 12:17:55 +00:00
export const HTTP_POST = httpMethod("POST");
export const HTTP_GET = httpMethod("GET");
export const HTTP_PATCH = httpMethod("PATCH");
export const HTTP_DELETE = httpMethod("DELETE");