@tinyhttp/cors
Advanced tools
Comparing version 0.1.22 to 0.1.23
@@ -13,3 +13,3 @@ /// <reference types="node" /> | ||
headers?: string[]; | ||
}) => (_req: Request, res: Response) => void; | ||
}) => (_req: Request, res: Response, next?: () => void) => void; | ||
export { defaultMethods, defaultHeaders, cors }; |
@@ -1,1 +0,1 @@ | ||
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}; | ||
const e=["GET","POST","PUT","PATCH","HEAD"],t=["Origin","X-Requested-With","Content-Type"],s=({host:s="*",methods:o=e,headers:n=t})=>{const r="Access-Control-Allow";return(e,t,d)=>{t.setHeader(r+"-Origin",s),t.setHeader(r+"-Headers",n.join(", ")),t.setHeader(r+"-Methods",o.join(", ")),null==d||d()}};export{s as cors,t as defaultHeaders,e as defaultMethods}; |
@@ -1,1 +0,1 @@ | ||
"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; | ||
"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,n)=>{t.setHeader(d+"-Origin",s),t.setHeader(d+"-Headers",r.join(", ")),t.setHeader(d+"-Methods",o.join(", ")),null==n||n()}},exports.defaultHeaders=t,exports.defaultMethods=e; |
{ | ||
"name": "@tinyhttp/cors", | ||
"version": "0.1.22", | ||
"description": "tinyhttp CORS module", | ||
"main": "dist/index.js", | ||
"types": "dist/index.d.ts", | ||
"module": "dist/index.esm.js", | ||
"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" | ||
], | ||
"engines": { | ||
"node": ">=12.4.0" | ||
}, | ||
"author": "v1rtl", | ||
"license": "MIT" | ||
"name": "@tinyhttp/cors", | ||
"version": "0.1.23", | ||
"description": "tinyhttp CORS module", | ||
"main": "dist/index.js", | ||
"types": "dist/index.d.ts", | ||
"module": "dist/index.esm.js", | ||
"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" | ||
], | ||
"engines": { | ||
"node": ">=12.4.0" | ||
}, | ||
"author": "v1rtl", | ||
"license": "MIT" | ||
} |
# @tinyhttp/cors | ||
tinyhttp CORS module | ||
[![npm (scoped)](https://img.shields.io/npm/v/@tinyhttp/cors?style=flat-square)](npmjs.com/package/@tinyhttp/cors) [![npm](https://img.shields.io/npm/dt/@tinyhttp/cors?style=flat-square)](npmjs.com/package/@tinyhttp/cors) [![](https://img.shields.io/badge/website-visit-hotpink?style=flat-square)](https://tinyhttp.v1rtl.site/mw/cors) | ||
## Installation | ||
> A rewrite of [cors](https://github.com/expressjs/cors) module. | ||
CORS middleware for HTTP servers. | ||
## Install | ||
```sh | ||
@@ -13,3 +17,3 @@ pnpm i @tinyhttp/cors | ||
```js | ||
```ts | ||
import { cors } from '@tinyhttp/cors' | ||
@@ -22,40 +26,12 @@ ``` | ||
Host that is allowed to send cross-origin requests. Defaults to '\*'. | ||
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`: | ||
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`: | ||
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 | ||
@@ -80,2 +56,2 @@ | ||
MIT | ||
MIT © [v1rtl](https://v1rtl.site) |
@@ -13,7 +13,9 @@ import { IncomingMessage as Request, ServerResponse as Response } from 'http' | ||
return (_req: Request, res: Response) => { | ||
return (_req: Request, res: Response, next?: () => void) => { | ||
res.setHeader(`${prefix}-Origin`, host) | ||
res.setHeader(`${prefix}-Headers`, headers.join(', ')) | ||
res.setHeader(`${prefix}-Methods`, methods.join(', ')) | ||
next?.() | ||
} | ||
} |
4812
7
33
55