adds cors support to docs endpoint

pull/9078/head
Agusti Fernandez Pardo 2022-04-15 16:10:57 +02:00
parent 187b97afa1
commit babfc6d7cf
3 changed files with 22 additions and 1 deletions

17
lib/helpers/withCors.ts Normal file
View File

@ -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();
};

View File

@ -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"
}
}

View File

@ -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());