From 844164f7c048e2a128cfc5cfd1b7a921cab5ff72 Mon Sep 17 00:00:00 2001 From: James Nail Date: Wed, 4 Jan 2017 16:13:52 -0500 Subject: [PATCH] enhanced cors package, up to v2.8 (#13593) * enhanced cors package, up to v2.8 * added missing test cases --- cors/cors-tests.ts | 39 ++++++++++++++++++++++++++++++++++++++- cors/index.d.ts | 16 +++++++++++----- 2 files changed, 49 insertions(+), 6 deletions(-) diff --git a/cors/cors-tests.ts b/cors/cors-tests.ts index 186e006726..f43b9ee153 100644 --- a/cors/cors-tests.ts +++ b/cors/cors-tests.ts @@ -6,5 +6,42 @@ var app = express(); app.use(cors()); app.use(cors({ maxAge: 100, - credentials: true + credentials: true, + optionsSuccessStatus: 200 })); +app.use(cors({ + methods: 'GET,POST,PUT', + exposedHeaders: 'Content-Range,X-Content-Range', + allowedHeaders: 'Content-Type,Authorization' +})); +app.use(cors({ + methods: ['GET', 'POST', 'PUT', 'DELETE'], + exposedHeaders: ['Content-Range', 'X-Content-Range'], + allowedHeaders: ['Content-Type', 'Authorization'] +})); +app.use(cors({ + origin: true +})); +app.use(cors({ + origin: 'http://example.com' +})); +app.use(cors({ + origin: /example\.com$/ +})); +app.use(cors({ + origin: ['http://example.com', 'http://fakeurl.com'] +})); +app.use(cors({ + origin: [/example\.com$/, /fakeurl\.com$/] +})); +app.use(cors({ + origin: (requestOrigin, cb) => { + try { + var allow = requestOrigin.indexOf('.edu') !== -1; + cb(null, allow); + } catch (err) { + cb(err); + } + } +})); + diff --git a/cors/index.d.ts b/cors/index.d.ts index 74761eb7d6..ecad80d187 100644 --- a/cors/index.d.ts +++ b/cors/index.d.ts @@ -1,4 +1,4 @@ -// Type definitions for cors +// Type definitions for cors 2.8 // Project: https://github.com/troygoode/node-cors/ // Definitions by: Mihhail Lapushkin // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped @@ -8,15 +8,21 @@ import express = require('express'); +type CustomOrigin = ( + requestOrigin: string, + callback: (err: Error, allow?: boolean) => void +) => void; + declare namespace e { interface CorsOptions { - origin?: any; - methods?: any; - allowedHeaders?: any; - exposedHeaders?: any; + origin?: boolean | string | RegExp | string[] | RegExp[] | CustomOrigin; + methods?: string | string[]; + allowedHeaders?: string | string[]; + exposedHeaders?: string | string[]; credentials?: boolean; maxAge?: number; preflightContinue?: boolean; + optionsSuccessStatus?: number; } }