Skip to content

Commit

Permalink
refactor(cors): remove cors handling
Browse files Browse the repository at this point in the history
BREAKING CHANGE: `cors` option is no longer available
  • Loading branch information
aleksandryackovlev committed Dec 26, 2020
1 parent 92637f6 commit 2bc16d0
Show file tree
Hide file tree
Showing 5 changed files with 4 additions and 50 deletions.
11 changes: 0 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,17 +60,6 @@ app.use(
useExamplesValue: true,
// ...
},
cors: { // cors options
enabled: true, // boolean, default to true
// default values are:
origin: '*',
enabled: true,
origin: '*',
maxAge: 31536000,
credentials: true,
methods: ['GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'HEAD', 'OPTIONS'],
allowedHeaders: ['Content-Type', 'Authorization', 'Origin', 'Accept'],
},
jsfCallback: (jsf, faker) => {
// function where you can extend json-schema-faker
...
Expand Down
18 changes: 2 additions & 16 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@
"ajv": "^7.0.2",
"chokidar": "^3.4.3",
"cookie-parser": "^1.4.5",
"cors": "^2.8.5",
"express": "^4.17.1",
"faker": "^5.1.0",
"json-schema-faker": "^0.5.0-rcv.32",
Expand All @@ -66,7 +65,6 @@
},
"devDependencies": {
"@types/cookie-parser": "^1.4.2",
"@types/cors": "^2.8.9",
"@types/express": "^4.17.9",
"@types/faker": "^5.1.5",
"@types/jest": "^26.0.19",
Expand Down
12 changes: 1 addition & 11 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import fs from 'fs';

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

import createRouter from './router';
import { createOperations } from './operations';
Expand All @@ -18,29 +17,20 @@ export interface MiddlewareOptions {
file: string;
locale?: string;
options?: Partial<JSFOptions>;
cors?: CorsOptions & { enabled?: boolean };
jsfCallback?: JSFCallback;
}

export const createMockMiddleware = ({
file,
locale = 'en',
options = {},
cors = {
enabled: true,
origin: '*',
maxAge: 31536000,
credentials: true,
methods: ['GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'HEAD', 'OPTIONS'],
allowedHeaders: ['Content-Type', 'Authorization', 'Origin', 'Accept'],
},
jsfCallback,
}: MiddlewareOptions): express.Router => {
if (!fs.existsSync(file)) {
throw new Error('File with the openapi docs does not exist');
}

const router = createRouter(cors);
const router = createRouter();
const operations = createOperations({ file, locale, options, callback: jsfCallback });

router.use('/{0,}', async (req, res, next) => {
Expand Down
11 changes: 1 addition & 10 deletions src/router.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,10 @@
import express from 'express';
import methodOverride from 'method-override';
import cookieParser from 'cookie-parser';
import cors, { CorsOptions } from 'cors';

const createRouter = ({
enabled,
...corsOptions
}: CorsOptions & { enabled?: boolean }): express.Router => {
const createRouter = (): express.Router => {
const router = express.Router();

if (enabled) {
router.options('*', cors(corsOptions));
router.use(cors(corsOptions));
}

router.use(express.urlencoded({ extended: true }));
router.use(express.json());
router.use(methodOverride());
Expand Down

0 comments on commit 2bc16d0

Please sign in to comment.