2022-03-31 20:14:37 +00:00
|
|
|
import pjson from "@/package.json";
|
2022-04-30 17:46:04 +00:00
|
|
|
import modifyRes from "modify-response-middleware";
|
|
|
|
import { use } from "next-api-middleware";
|
2022-03-30 14:56:24 +00:00
|
|
|
import { withSwagger } from "next-swagger-doc";
|
2022-04-30 17:46:04 +00:00
|
|
|
import { NextApiRequest, NextApiResponse } from "next/types";
|
2022-03-30 14:56:24 +00:00
|
|
|
|
|
|
|
const swaggerHandler = withSwagger({
|
|
|
|
definition: {
|
2022-05-05 16:18:00 +00:00
|
|
|
openapi: "3.0.3",
|
2022-04-26 19:56:59 +00:00
|
|
|
servers: [
|
|
|
|
{ url: "http://localhost:3002/v1" },
|
2022-05-05 16:18:00 +00:00
|
|
|
{ url: "https://api.cal.dev/v1" },
|
|
|
|
{ url: "https://api.cal.com/v1" },
|
2022-04-26 19:56:59 +00:00
|
|
|
],
|
|
|
|
externalDocs: {
|
|
|
|
url: "https://docs.cal.com",
|
|
|
|
description: "Find more info at our main docs: https://docs.cal.com/",
|
|
|
|
},
|
2022-03-30 14:56:24 +00:00
|
|
|
info: {
|
2022-03-31 20:14:37 +00:00
|
|
|
title: `${pjson.name}: ${pjson.description}`,
|
|
|
|
version: pjson.version,
|
2022-03-30 14:56:24 +00:00
|
|
|
},
|
2022-04-17 14:18:42 +00:00
|
|
|
components: {
|
|
|
|
securitySchemes: { ApiKeyAuth: { type: "apiKey", in: "query", name: "apiKey" } },
|
|
|
|
},
|
2022-04-29 17:45:33 +00:00
|
|
|
security: [{ ApiKeyAuth: [] }],
|
2022-05-05 16:18:00 +00:00
|
|
|
tags: [
|
|
|
|
{ name: "users" },
|
|
|
|
{ name: "event-types" },
|
|
|
|
{ name: "bookings" },
|
|
|
|
{ name: "attendees" },
|
|
|
|
{ name: "payments" },
|
|
|
|
{ name: "schedules" },
|
|
|
|
{ name: "teams" },
|
|
|
|
{ name: "memberships" },
|
|
|
|
{ name: "availabilities" },
|
|
|
|
{ name: "custom-inputs" },
|
|
|
|
{ name: "event-references" },
|
|
|
|
{ name: "booking-references" },
|
|
|
|
{ name: "destination-calendars" },
|
|
|
|
{ name: "selected-calendars" },
|
|
|
|
],
|
2022-03-30 14:56:24 +00:00
|
|
|
},
|
|
|
|
apiFolder: "pages/api",
|
|
|
|
});
|
2022-04-30 17:46:04 +00:00
|
|
|
|
|
|
|
export default use(
|
2022-05-02 06:00:08 +00:00
|
|
|
modifyRes((content: string, _req: NextApiRequest, res: NextApiResponse) => {
|
|
|
|
// Add all headers here instead of next.config.js as it is throwing error( Cannot set headers after they are sent to the client) for OPTIONS method
|
|
|
|
// It is known to happen only in Dev Mode.
|
|
|
|
res.setHeader("Access-Control-Allow-Credentials", "true");
|
|
|
|
res.setHeader("Access-Control-Allow-Origin", "*");
|
|
|
|
res.setHeader("Access-Control-Allow-Methods", "GET, OPTIONS, PATCH, DELETE, POST, PUT");
|
|
|
|
res.setHeader(
|
|
|
|
"Access-Control-Allow-Headers",
|
|
|
|
"X-CSRF-Token, X-Requested-With, Accept, Accept-Version, Content-Length, Content-MD5, Content-Type, Date, X-Api-Version, Content-Type, api_key, Authorization"
|
|
|
|
);
|
2022-04-30 17:46:04 +00:00
|
|
|
if (content) {
|
|
|
|
const parsed = JSON.parse(content);
|
2022-05-05 16:18:00 +00:00
|
|
|
// HACK: This is a hack to fix the swagger-ui issue with the extra channels property.
|
2022-04-30 17:46:04 +00:00
|
|
|
delete parsed.channels;
|
|
|
|
return Buffer.from(JSON.stringify(parsed));
|
|
|
|
}
|
|
|
|
})
|
|
|
|
)(swaggerHandler());
|