cal.pub0.org/apps/api/lib/helpers/withMiddleware.ts

52 lines
1.3 KiB
TypeScript
Raw Normal View History

import { label } from "next-api-middleware";
2022-03-30 12:17:55 +00:00
import { addRequestId } from "./addRequestid";
import { captureErrors } from "./captureErrors";
2022-06-08 16:52:25 +00:00
import { customPrismaClient } from "./customPrisma";
2022-06-06 16:17:10 +00:00
import { extendRequest } from "./extendRequest";
import {
HTTP_POST,
HTTP_DELETE,
HTTP_PATCH,
HTTP_GET,
HTTP_GET_OR_POST,
HTTP_GET_DELETE_PATCH,
} from "./httpMethods";
2023-10-11 22:44:00 +00:00
import { rateLimitApiKey } from "./rateLimitApiKey";
import { verifyApiKey } from "./verifyApiKey";
2022-07-05 18:12:14 +00:00
import { withPagination } from "./withPagination";
2022-03-30 12:17:55 +00:00
const middleware = {
HTTP_GET_OR_POST,
HTTP_GET_DELETE_PATCH,
HTTP_GET,
HTTP_PATCH,
HTTP_POST,
HTTP_DELETE,
addRequestId,
verifyApiKey,
rateLimitApiKey,
customPrismaClient,
extendRequest,
pagination: withPagination,
captureErrors,
};
type Middleware = keyof typeof middleware;
const middlewareOrder =
2023-08-03 15:22:38 +00:00
// The order here, determines the order of execution
[
"extendRequest",
"captureErrors",
// - Put customPrismaClient before verifyApiKey always.
"customPrismaClient",
"verifyApiKey",
2023-10-11 22:44:00 +00:00
"rateLimitApiKey",
2023-08-03 15:22:38 +00:00
"addRequestId",
] as Middleware[]; // <-- Provide a list of middleware to call automatically
const withMiddleware = label(middleware, middlewareOrder);
export { withMiddleware, middleware, middlewareOrder };