@sisu-ai/mw-cors 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +18 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.js +24 -0
- package/package.json +29 -0
package/README.md
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
# @sisu-ai/mw-cors
|
|
2
|
+
|
|
3
|
+
Tiny CORS middleware for Sisu HTTP servers. Intended for dev/local use.
|
|
4
|
+
|
|
5
|
+
## Usage
|
|
6
|
+
|
|
7
|
+
```ts
|
|
8
|
+
import { cors } from '@sisu-ai/mw-cors';
|
|
9
|
+
|
|
10
|
+
agent.use(cors());
|
|
11
|
+
// Or with options
|
|
12
|
+
agent.use(cors({ origin: '*', credentials: true }));
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
- Adds `Access-Control-Allow-*` headers
|
|
16
|
+
- Responds to `OPTIONS` with 204
|
|
17
|
+
- Defaults: `origin: '*'`, `methods: GET,POST,PUT,PATCH,DELETE,OPTIONS`, `headers: Content-Type,Authorization`, `credentials: false`, `maxAgeSec: 600`.
|
|
18
|
+
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
export function cors(opts = {}) {
|
|
2
|
+
const origin = opts.origin ?? '*';
|
|
3
|
+
const methods = opts.methods ?? 'GET,POST,PUT,PATCH,DELETE,OPTIONS';
|
|
4
|
+
const headers = opts.headers ?? 'Content-Type,Authorization';
|
|
5
|
+
const credentials = opts.credentials ?? false;
|
|
6
|
+
const maxAgeSec = opts.maxAgeSec ?? 600;
|
|
7
|
+
return async (ctx, next) => {
|
|
8
|
+
const { req, res } = ctx;
|
|
9
|
+
if (!req || !res)
|
|
10
|
+
return next();
|
|
11
|
+
res.setHeader?.('Access-Control-Allow-Origin', origin);
|
|
12
|
+
res.setHeader?.('Access-Control-Allow-Methods', methods);
|
|
13
|
+
res.setHeader?.('Access-Control-Allow-Headers', headers);
|
|
14
|
+
res.setHeader?.('Access-Control-Max-Age', String(maxAgeSec));
|
|
15
|
+
if (credentials)
|
|
16
|
+
res.setHeader?.('Access-Control-Allow-Credentials', 'true');
|
|
17
|
+
if (req.method === 'OPTIONS') {
|
|
18
|
+
res.statusCode = 204;
|
|
19
|
+
res.end();
|
|
20
|
+
return;
|
|
21
|
+
}
|
|
22
|
+
await next();
|
|
23
|
+
};
|
|
24
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@sisu-ai/mw-cors",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"license": "Apache-2.0",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "dist/index.js",
|
|
7
|
+
"types": "dist/index.d.ts",
|
|
8
|
+
"files": [
|
|
9
|
+
"dist"
|
|
10
|
+
],
|
|
11
|
+
"publishConfig": {
|
|
12
|
+
"access": "public"
|
|
13
|
+
},
|
|
14
|
+
"scripts": {
|
|
15
|
+
"build": "tsc -b"
|
|
16
|
+
},
|
|
17
|
+
"peerDependencies": {
|
|
18
|
+
"@sisu-ai/core": "1.0.2"
|
|
19
|
+
},
|
|
20
|
+
"repository": {
|
|
21
|
+
"type": "git",
|
|
22
|
+
"url": "https://github.com/finger-gun/sisu",
|
|
23
|
+
"directory": "packages/middleware/cors"
|
|
24
|
+
},
|
|
25
|
+
"homepage": "https://github.com/finger-gun/sisu#readme",
|
|
26
|
+
"bugs": {
|
|
27
|
+
"url": "https://github.com/finger-gun/sisu/issues"
|
|
28
|
+
}
|
|
29
|
+
}
|