Skip to content

Scheduling infrastructure for absolutely everyone.

adds cors support to docs endpoint

Agusti Fernandez Pardobabfc6d7cfcb
diff --git a/lib/helpers/withCors.ts b/lib/helpers/withCors.ts
new file mode 100644
index 00000000000..e0034ae1ddf
--- /dev/null
+++ b/lib/helpers/withCors.ts
@@ -0,0 +1,17 @@
+import type { NextMiddleware } from "next-api-middleware";
+import NextCors from "nextjs-cors";
+
+export const withCors: NextMiddleware = async (req, res, next) => {
+ // Run the cors middleware
+ // nextjs-cors uses the cors package, so we invite you to check the documentation https://github.com/expressjs/cors
+ await NextCors(req, res, {
+ // Options
+ methods: ["GET", "HEAD", "PUT", "PATCH", "POST", "DELETE"],
+ origin: "*",
+ optionsSuccessStatus: 200, // some legacy browsers (IE11, various SmartTVs) choke on 204
+ });
+
+ // Rest of the API logic
+ // Execute the remaining middleware
+ await next();
+};
diff --git a/package.json b/package.json
index 16dc0f9b9fd..971c197c42b 100644
--- a/package.json
+++ b/package.json
@@ -38,6 +38,7 @@
"next-swagger-doc": "^0.2.1",
"next-transpile-modules": "^9.0.0",
"next-validations": "^0.1.11",
+ "nextjs-cors": "^2.1.1",
"typescript": "^4.6.3"
}
}
diff --git a/pages/api/docs.ts b/pages/api/docs.ts
index d9aa75d18cf..39b36362f52 100644
--- a/pages/api/docs.ts
+++ b/pages/api/docs.ts
@@ -2,6 +2,9 @@ import jsonSchema from "@/json-schema/json-schema.json";
import pjson from "@/package.json";
import { withSwagger } from "next-swagger-doc";
+import { withCors } from "@lib/helpers/withCors";
+import { withMiddleware } from "@lib/helpers/withMiddleware";
+
const swaggerHandler = withSwagger({
definition: {
openapi: "3.0.0",
@@ -16,4 +19,4 @@ const swaggerHandler = withSwagger({
tags: ["users", "teams", "memeberships"],
sort: true,
});
-export default swaggerHandler();
+export default withMiddleware(withCors)(swaggerHandler());