adds cors support to docs endpoint
parent
187b97afa1
commit
babfc6d7cf
|
@ -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();
|
||||||
|
};
|
|
@ -38,6 +38,7 @@
|
||||||
"next-swagger-doc": "^0.2.1",
|
"next-swagger-doc": "^0.2.1",
|
||||||
"next-transpile-modules": "^9.0.0",
|
"next-transpile-modules": "^9.0.0",
|
||||||
"next-validations": "^0.1.11",
|
"next-validations": "^0.1.11",
|
||||||
|
"nextjs-cors": "^2.1.1",
|
||||||
"typescript": "^4.6.3"
|
"typescript": "^4.6.3"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,6 +2,9 @@ import jsonSchema from "@/json-schema/json-schema.json";
|
||||||
import pjson from "@/package.json";
|
import pjson from "@/package.json";
|
||||||
import { withSwagger } from "next-swagger-doc";
|
import { withSwagger } from "next-swagger-doc";
|
||||||
|
|
||||||
|
import { withCors } from "@lib/helpers/withCors";
|
||||||
|
import { withMiddleware } from "@lib/helpers/withMiddleware";
|
||||||
|
|
||||||
const swaggerHandler = withSwagger({
|
const swaggerHandler = withSwagger({
|
||||||
definition: {
|
definition: {
|
||||||
openapi: "3.0.0",
|
openapi: "3.0.0",
|
||||||
|
@ -16,4 +19,4 @@ const swaggerHandler = withSwagger({
|
||||||
tags: ["users", "teams", "memeberships"],
|
tags: ["users", "teams", "memeberships"],
|
||||||
sort: true,
|
sort: true,
|
||||||
});
|
});
|
||||||
export default swaggerHandler();
|
export default withMiddleware(withCors)(swaggerHandler());
|
||||||
|
|
Loading…
Reference in New Issue