Skip to content

Commit

Permalink
feat(router): add cors options to the router
Browse files Browse the repository at this point in the history
  • Loading branch information
aleksandryackovlev committed Aug 10, 2020
1 parent 4dd1288 commit d452812
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
11 changes: 10 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import fs from 'fs';

import express from 'express';
import { CorsOptions } from 'cors';

import createRouter from './router';
import { createOperations } from './operations';
Expand All @@ -17,18 +18,26 @@ export interface MiddlewareOptions {
file: string;
locale?: string;
options?: Partial<JSFOptions>;
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) => {
Expand Down
6 changes: 3 additions & 3 deletions src/router.ts
Original file line number Diff line number Diff line change
@@ -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());
Expand Down

0 comments on commit d452812

Please sign in to comment.