diff --git a/src/index.ts b/src/index.ts index c9260b1..2e43f85 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,6 +1,7 @@ import fs from 'fs'; import express from 'express'; +import { CorsOptions } from 'cors'; import createRouter from './router'; import { createOperations } from './operations'; @@ -17,18 +18,26 @@ export interface MiddlewareOptions { file: string; locale?: string; options?: Partial; + cors?: CorsOptions; } export const createMockMiddleware = ({ file, locale = 'en', options = {}, + cors = { + origin: '*', + maxAge: 31536000, + credentials: true, + methods: ['GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'HEAD', 'OPTIONS'], + allowedHeaders: ['Content-Type', 'Authorization', 'Origin', 'Accept'], + }, }: MiddlewareOptions): express.Router => { if (!fs.existsSync(file)) { throw new Error('File with the openapi docs does not exist'); } - const router = createRouter(); + const router = createRouter(cors); const operations = createOperations({ file, locale, options }); router.use('/{0,}', async (req, res, next) => { diff --git a/src/router.ts b/src/router.ts index 716f926..74a2c50 100644 --- a/src/router.ts +++ b/src/router.ts @@ -1,12 +1,12 @@ import express from 'express'; import methodOverride from 'method-override'; import cookieParser from 'cookie-parser'; -import cors from 'cors'; +import cors, { CorsOptions } from 'cors'; -const createRouter = (): express.Router => { +const createRouter = (corsOptions: CorsOptions): express.Router => { const router = express.Router(); - router.use(cors()); + router.use(cors(corsOptions)); router.use(express.urlencoded({ extended: true })); router.use(express.json()); router.use(methodOverride());