Skip to content

Commit

Permalink
feat(cors): add preflight cors
Browse files Browse the repository at this point in the history
  • Loading branch information
aleksandryackovlev committed Aug 31, 2020
1 parent 6ce0c55 commit 4fec346
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
3 changes: 2 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export interface MiddlewareOptions {
file: string;
locale?: string;
options?: Partial<JSFOptions>;
cors?: CorsOptions;
cors?: CorsOptions & { enabled?: boolean };
jsfCallback?: JSFCallback;
}

Expand All @@ -27,6 +27,7 @@ export const createMockMiddleware = ({
locale = 'en',
options = {},
cors = {
enabled: true,
origin: '*',
maxAge: 31536000,
credentials: true,
Expand Down
11 changes: 9 additions & 2 deletions src/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,17 @@ import methodOverride from 'method-override';
import cookieParser from 'cookie-parser';
import cors, { CorsOptions } from 'cors';

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

router.use(cors(corsOptions));
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 4fec346

Please sign in to comment.