@tinyhttp/cors
Advanced tools
Comparing version 1.1.1 to 1.1.2
@@ -11,2 +11,3 @@ /// <reference types="node" /> | ||
optionsSuccessStatus?: number; | ||
preflightContinue?: boolean; | ||
} | ||
@@ -16,2 +17,2 @@ /** | ||
*/ | ||
export declare const cors: ({ origin, methods, allowedHeaders, exposedHeaders, credentials, maxAge, optionsSuccessStatus }: AccessControlOptions) => (req: Request, res: Response, next?: () => void) => void; | ||
export declare const cors: (opts?: AccessControlOptions) => (req: Request, res: Response, next?: () => void) => void; |
@@ -6,3 +6,4 @@ import { vary } from 'es-vary'; | ||
*/ | ||
const cors = ({ origin = '*', methods = ['GET', 'HEAD', 'PUT', 'PATCH', 'POST', 'DELETE'], allowedHeaders, exposedHeaders, credentials, maxAge, optionsSuccessStatus = 204 }) => { | ||
const cors = (opts) => { | ||
const { origin = '*', methods = ['GET', 'HEAD', 'PUT', 'PATCH', 'POST', 'DELETE'], allowedHeaders, exposedHeaders, credentials, maxAge, optionsSuccessStatus = 204, preflightContinue = false } = opts; | ||
return (req, res, next) => { | ||
@@ -32,7 +33,9 @@ // Checking the type of the origin property | ||
res.setHeader('Access-Control-Max-Age', maxAge); | ||
if (next === undefined) { | ||
if (preflightContinue) { | ||
next === null || next === void 0 ? void 0 : next(); | ||
} | ||
else { | ||
res.statusCode = optionsSuccessStatus; | ||
res.end(); | ||
} | ||
next === null || next === void 0 ? void 0 : next(); | ||
}; | ||
@@ -39,0 +42,0 @@ }; |
{ | ||
"name": "@tinyhttp/cors", | ||
"version": "1.1.1", | ||
"version": "1.1.2", | ||
"description": "tinyhttp CORS module", | ||
@@ -5,0 +5,0 @@ "type": "module", |
@@ -34,2 +34,3 @@ # @tinyhttp/cors | ||
- `optionsSuccessStatus`: Provides a status code to use for successful OPTIONS requests, since some legacy browsers (IE11, various SmartTVs) choke on 204. | ||
- `preflightContinue`: Set 204 and finish response if `true`, call `next` if false. | ||
@@ -42,3 +43,4 @@ The default configuration is: | ||
"methods": ["GET", "HEAD", "PUT", "PATCH", "POST", "DELETE"], | ||
"optionsSuccessStatus": 204 | ||
"optionsSuccessStatus": 204, | ||
"preflightContinue": false | ||
} | ||
@@ -55,10 +57,9 @@ ``` | ||
app.use(cors({ origin: 'https://myfantastic.site/' })) | ||
app.options('*', cors()) | ||
app.get('/', (req, res) => { | ||
res.send('The headers contained in my response are defined in the cors middleware') | ||
}) | ||
app.listen(3000) | ||
app | ||
.use(cors({ origin: 'https://myfantastic.site/' })) | ||
.options('*', cors()) | ||
.get('/', (req, res) => { | ||
res.send('The headers contained in my response are defined in the cors middleware') | ||
}) | ||
.listen(3000) | ||
``` | ||
@@ -65,0 +66,0 @@ |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
8783
98
71