@tinyhttp/cors
Advanced tools
Comparing version 0.2.19 to 0.2.20
# @tinyhttp/cors | ||
## 0.2.20 | ||
### Patch Changes | ||
- Split app into req, res, send and router | ||
## 0.2.19 | ||
@@ -4,0 +10,0 @@ |
/// <reference types="node" /> | ||
import { IncomingMessage, ServerResponse } from 'http'; | ||
interface AccessControlOptions { | ||
origin?: string | boolean | ((req: IncomingMessage, res: ServerResponse) => string); | ||
import { IncomingMessage as Request, ServerResponse as Response } from 'http'; | ||
export interface AccessControlOptions { | ||
origin?: string | boolean | ((req: Request, res: Response) => string); | ||
methods?: string[]; | ||
@@ -16,4 +15,2 @@ allowedHeaders?: string[]; | ||
*/ | ||
declare const cors: ({ origin, methods, allowedHeaders, exposedHeaders, credentials, maxAge, optionsSuccessStatus, }: AccessControlOptions) => (req: IncomingMessage, res: ServerResponse, next?: () => void) => void; | ||
export { AccessControlOptions, cors }; | ||
export declare const cors: ({ origin, methods, allowedHeaders, exposedHeaders, credentials, maxAge, optionsSuccessStatus, }: AccessControlOptions) => (req: Request, res: Response, next?: () => void) => void; |
@@ -1,39 +0,39 @@ | ||
// src/index.ts | ||
import {vary} from "es-vary"; | ||
const cors = ({ | ||
origin = "*", | ||
methods = ["GET", "HEAD", "PUT", "PATCH", "POST", "DELETE"], | ||
allowedHeaders, | ||
exposedHeaders, | ||
credentials, | ||
maxAge, | ||
optionsSuccessStatus = 204 | ||
}) => { | ||
return (req, res, next) => { | ||
if (typeof origin === "boolean" && origin === true) | ||
res.setHeader("Access-Control-Allow-Origin", "*"); | ||
else if (typeof origin === "string") | ||
res.setHeader("Access-Control-Allow-Origin", origin); | ||
else if (typeof origin === "function") | ||
res.setHeader("Access-Control-Allow-Origin", origin(req, res)); | ||
if (typeof origin === "string" && origin !== "*" || typeof origin === "function") | ||
vary(res, "Origin"); | ||
res.setHeader("Access-Control-Allow-Methods", methods.join(", ").toUpperCase()); | ||
if (allowedHeaders) | ||
res.setHeader("Access-Control-Allow-Headers", allowedHeaders); | ||
if (exposedHeaders) | ||
res.setHeader("Access-Control-Expose-Headers", exposedHeaders); | ||
if (credentials) | ||
res.setHeader("Access-Control-Allow-Credentials", "true"); | ||
if (maxAge) | ||
res.setHeader("Access-Control-Max-Age", maxAge); | ||
if (next === void 0) { | ||
res.statusCode = optionsSuccessStatus; | ||
res.end(); | ||
} | ||
next == null ? void 0 : next(); | ||
}; | ||
import { vary } from 'es-vary'; | ||
/** | ||
* CORS Middleware | ||
*/ | ||
const cors = ({ origin = '*', methods = ['GET', 'HEAD', 'PUT', 'PATCH', 'POST', 'DELETE'], allowedHeaders, exposedHeaders, credentials, maxAge, optionsSuccessStatus = 204, }) => { | ||
return (req, res, next) => { | ||
// Checking the type of the origin property | ||
if (typeof origin === 'boolean' && origin === true) | ||
res.setHeader('Access-Control-Allow-Origin', '*'); | ||
else if (typeof origin === 'string') | ||
res.setHeader('Access-Control-Allow-Origin', origin); | ||
else if (typeof origin === 'function') | ||
res.setHeader('Access-Control-Allow-Origin', origin(req, res)); | ||
if ((typeof origin === 'string' && origin !== '*') || typeof origin === 'function') | ||
vary(res, 'Origin'); | ||
// Setting the Access-Control-Allow-Methods header from the methods array | ||
res.setHeader('Access-Control-Allow-Methods', methods.join(', ').toUpperCase()); | ||
// Setting the Access-Control-Allow-Headers header | ||
if (allowedHeaders) | ||
res.setHeader('Access-Control-Allow-Headers', allowedHeaders); | ||
// Setting the Access-Control-Expose-Headers header | ||
if (exposedHeaders) | ||
res.setHeader('Access-Control-Expose-Headers', exposedHeaders); | ||
// Setting the Access-Control-Allow-Credentials header | ||
if (credentials) | ||
res.setHeader('Access-Control-Allow-Credentials', 'true'); | ||
// Setting the Access-Control-Max-Age header | ||
if (maxAge) | ||
res.setHeader('Access-Control-Max-Age', maxAge); | ||
if (next === undefined) { | ||
res.statusCode = optionsSuccessStatus; | ||
res.end(); | ||
} | ||
next === null || next === void 0 ? void 0 : next(); | ||
}; | ||
}; | ||
export { | ||
cors | ||
}; | ||
export { cors }; |
{ | ||
"name": "@tinyhttp/cors", | ||
"version": "0.2.19", | ||
"version": "0.2.20", | ||
"description": "tinyhttp CORS module", | ||
@@ -39,9 +39,5 @@ "type": "module", | ||
"license": "MIT", | ||
"devDependencies": { | ||
"tsup": "^3.6.1", | ||
"typescript": "^4.0.2" | ||
}, | ||
"scripts": { | ||
"build": "tsup src/index.ts --format cjs,esm --dts" | ||
"build": "rollup -c ../../build/defaultConfig.js" | ||
} | ||
} |
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
11823
0
8
137