@tinyhttp/cors
Advanced tools
Comparing version 0.1.10 to 0.1.21
/// <reference types="node" /> | ||
import { IncomingMessage as Request } from 'http'; | ||
import { ServerResponse as Response } from "http"; | ||
declare const METHODS: string[]; | ||
declare const defaultMethods: string[]; | ||
declare const defaultHeaders: string[]; | ||
/** | ||
* CORS Middleware | ||
*/ | ||
declare const cors: ({ host, methods, headers }: { | ||
@@ -10,2 +14,2 @@ host?: string; | ||
}) => (_req: Request, res: Response) => void; | ||
export { cors as default, METHODS }; | ||
export { defaultMethods, defaultHeaders, cors }; |
@@ -1,12 +0,1 @@ | ||
const METHODS = ['GET', 'POST', 'PUT', 'PATCH', 'HEAD']; | ||
const cors = ({ host = '*', methods = METHODS, headers = ['Origin', 'X-Requested-With', 'Content-Type'] }) => { | ||
const prefix = 'Access-Control-Allow'; | ||
return (_req, res) => { | ||
res.setHeader(`${prefix}-Origin`, host); | ||
res.setHeader(`${prefix}-Headers`, headers.join(', ')); | ||
res.setHeader(`${prefix}-Methods`, methods.join(', ')); | ||
}; | ||
}; | ||
export default cors; | ||
export { METHODS }; | ||
const e=["GET","POST","PUT","PATCH","HEAD"],t=["Origin","X-Requested-With","Content-Type"],s=({host:s="*",methods:o=e,headers:r=t})=>{const n="Access-Control-Allow";return(e,t)=>{t.setHeader(n+"-Origin",s),t.setHeader(n+"-Headers",r.join(", ")),t.setHeader(n+"-Methods",o.join(", "))}};export{s as cors,t as defaultHeaders,e as defaultMethods}; |
@@ -1,16 +0,1 @@ | ||
'use strict'; | ||
Object.defineProperty(exports, '__esModule', { value: true }); | ||
const METHODS = ['GET', 'POST', 'PUT', 'PATCH', 'HEAD']; | ||
const cors = ({ host = '*', methods = METHODS, headers = ['Origin', 'X-Requested-With', 'Content-Type'] }) => { | ||
const prefix = 'Access-Control-Allow'; | ||
return (_req, res) => { | ||
res.setHeader(`${prefix}-Origin`, host); | ||
res.setHeader(`${prefix}-Headers`, headers.join(', ')); | ||
res.setHeader(`${prefix}-Methods`, methods.join(', ')); | ||
}; | ||
}; | ||
exports.METHODS = METHODS; | ||
exports.default = cors; | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});const e=["GET","POST","PUT","PATCH","HEAD"],t=["Origin","X-Requested-With","Content-Type"];exports.cors=({host:s="*",methods:o=e,headers:r=t})=>{const d="Access-Control-Allow";return(e,t)=>{t.setHeader(d+"-Origin",s),t.setHeader(d+"-Headers",r.join(", ")),t.setHeader(d+"-Methods",o.join(", "))}},exports.defaultHeaders=t,exports.defaultMethods=e; |
{ | ||
"name": "@tinyhttp/cors", | ||
"version": "0.1.10", | ||
"description": "tinyhttp CORS module", | ||
"main": "dist/index.js", | ||
"types": "dist/index.d.ts", | ||
"homepage": "https://github.com/talentlessguy/tinyhttp", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/talentlessguy/tinyhttp.git", | ||
"directory": "packages/cors" | ||
}, | ||
"keywords": [ | ||
"tinyhttp", | ||
"node.js", | ||
"web framework", | ||
"web", | ||
"backend", | ||
"cors" | ||
], | ||
"author": "v1rtl", | ||
"license": "MIT", | ||
"gitHead": "584a7d9a98e23543ef5285af59532a583c578d11" | ||
"name": "@tinyhttp/cors", | ||
"version": "0.1.21", | ||
"description": "tinyhttp CORS module", | ||
"main": "dist/index.js", | ||
"types": "dist/index.d.ts", | ||
"homepage": "https://github.com/talentlessguy/tinyhttp", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/talentlessguy/tinyhttp.git", | ||
"directory": "packages/cors" | ||
}, | ||
"keywords": [ | ||
"tinyhttp", | ||
"node.js", | ||
"web framework", | ||
"web", | ||
"backend", | ||
"cors" | ||
], | ||
"author": "v1rtl", | ||
"license": "MIT" | ||
} |
@@ -7,23 +7,73 @@ # @tinyhttp/cors | ||
This is a [Node.js](https://nodejs.org/) module available through the | ||
[npm registry](https://www.npmjs.com/). It can be installed using the | ||
[`npm`](https://docs.npmjs.com/getting-started/installing-npm-packages-locally) | ||
or | ||
[`yarn`](https://yarnpkg.com/en/) | ||
command line tools. | ||
```sh | ||
npm install @tinyhttp/cors --save | ||
npm install @tinyhttp/cors | ||
``` | ||
## Dependencies | ||
## API | ||
- [@tinyhttp/app](https://ghub.io/@tinyhttp/app): tinyhttp core | ||
```js | ||
import { cors } from '@tinyhttp/cors' | ||
``` | ||
## Dev Dependencies | ||
### Options | ||
None | ||
#### `host` | ||
Host that is allowed to send cross-origin requests. Defaults to '\*'. | ||
#### `methods` | ||
Allowed methods for performing a cross-origin request. Default ones are `['GET', 'POST', 'PUT', 'PATCH', 'HEAD']` and can be accessed with `defaultMethods`: | ||
```ts | ||
import { App } from '@tinyhttp/app' | ||
import { defaultMethods, cors } from '@tinyhttp/cors' | ||
const app = new App() | ||
app.use( | ||
cors({ | ||
methods: defaultMethods.filter(m => m !== 'DELETE'), | ||
host: 'https://example.com' | ||
}) | ||
) | ||
``` | ||
#### `headers` | ||
Allowed HTTP headers that can be sent in a cross-origin request. Default ones are `['Origin', 'X-Requested-With', 'Content-Type']` and can be accessed with `defaultHeaders`: | ||
```ts | ||
import { App } from '@tinyhttp/app' | ||
import { defaultHeaders, cors } from '@tinyhttp/cors' | ||
const app = new App() | ||
app.use( | ||
cors({ | ||
methods: [...defaultHeaders, 'X-Custom-Header'], | ||
host: 'https://example.com' | ||
}) | ||
) | ||
``` | ||
## Example | ||
```ts | ||
import { App } from '@tinyhttp/app' | ||
import { cors } from '@tinyhttp/cors' | ||
const app = new App() | ||
app | ||
.use( | ||
cors({ | ||
host: 'https://example.com' | ||
}) | ||
) | ||
.get('/', (_, res) => void res.end('Hello World')) | ||
``` | ||
## License | ||
MIT |
import { IncomingMessage as Request, ServerResponse as Response } from 'http' | ||
export const METHODS = ['GET', 'POST', 'PUT', 'PATCH', 'HEAD'] | ||
export const defaultMethods = ['GET', 'POST', 'PUT', 'PATCH', 'HEAD'] | ||
const cors = ({ host = '*', methods = METHODS, headers = ['Origin', 'X-Requested-With', 'Content-Type'] }) => { | ||
export const defaultHeaders = ['Origin', 'X-Requested-With', 'Content-Type'] | ||
/** | ||
* CORS Middleware | ||
*/ | ||
export const cors = ({ host = '*', methods = defaultMethods, headers = defaultHeaders }) => { | ||
const prefix = 'Access-Control-Allow' | ||
@@ -14,3 +19,1 @@ | ||
} | ||
export default cors |
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
79
3660
6
32