@vafast/cors 0.0.1 → 0.0.2

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.
Files changed (2) hide show
  1. package/README.md +59 -29
  2. package/package.json +4 -14
package/README.md CHANGED
@@ -1,23 +1,39 @@
1
- # @huyooo/elysia-cors
2
- Plugin for [elysia](https://github.com/elysiajs/elysia) that for Cross Origin Requests (CORs)
1
+ # @vafast/cors
2
+
3
+ CORS middleware plugin for [Vafast](https://github.com/vafastjs/vafast) framework.
3
4
 
4
5
  ## Installation
6
+
5
7
  ```bash
6
- bun add @huyooo/elysia-cors
8
+ bun add @vafast/cors
9
+ # or
10
+ npm install @vafast/cors
7
11
  ```
8
12
 
9
13
  ## Example
10
- ```typescript
11
- import { Elysia } from '@huyooo/elysia'
12
- import { cors } from '@huyooo/elysia-cors'
13
14
 
14
- const app = new Elysia()
15
- .use(cors())
16
- .listen(8080)
15
+ ```typescript
16
+ import { Server, createHandler } from 'vafast'
17
+ import { cors } from '@vafast/cors'
18
+
19
+ const server = new Server([
20
+ {
21
+ method: 'GET',
22
+ path: '/',
23
+ handler: createHandler(() => 'Hello World'),
24
+ middleware: [cors()]
25
+ }
26
+ ])
27
+
28
+ export default {
29
+ fetch: (req: Request) => server.fetch(req)
30
+ }
17
31
  ```
18
32
 
19
- ## Config
33
+ ## Configuration
34
+
20
35
  ### origin
36
+
21
37
  @default `true`
22
38
 
23
39
  Assign the **Access-Control-Allow-Origin** header.
@@ -25,33 +41,34 @@ Assign the **Access-Control-Allow-Origin** header.
25
41
  Value can be one of the following:
26
42
  - `string` - String of origin which will directly assign to `Access-Control-Allow-Origin`
27
43
 
28
- - `boolean` - If set to true, `Access-Control-Allow-Origin` will be set to `*` (accept all origin)
44
+ - `boolean` - If set to true, `Access-Control-Allow-Origin` will be set to the request origin (accept all origin)
29
45
 
30
46
  - `RegExp` - Pattern to use to test with request's url, will accept origin if matched.
31
47
 
32
48
  - `Function` - Custom logic to validate origin acceptance or not. will accept origin if `true` is returned.
33
- - Function will accepts `Context` just like `Handler`
34
49
  ```typescript
35
50
  // Example usage
36
- app.use(cors, {
37
- origin: ({ request, headers }) => true
51
+ cors({
52
+ origin: (request: Request) => {
53
+ const origin = request.headers.get('Origin')
54
+ return origin === 'https://example.com'
55
+ }
38
56
  })
39
-
40
- // Type Definition
41
- type CORSOriginFn = (context: Context) => boolean | void
42
57
  ```
43
58
 
44
59
  - `Array<string | RegExp | Function>` - Will try to find truthy value of all options above. Will accept Request if one is `true`.
45
60
 
46
61
  ### methods
47
- @default `*`
62
+
63
+ @default `true`
48
64
 
49
65
  Assign **Access-Control-Allow-Methods** header.
50
66
 
51
67
  Value can be one of the following:
52
- Accept:
53
68
  - `undefined | null | ''` - Ignore all methods.
54
69
 
70
+ - `true` - Mirror the request method.
71
+
55
72
  - `*` - Accept all methods.
56
73
 
57
74
  - `HTTPMethod` - Will be directly set to **Access-Control-Allow-Methods**.
@@ -61,34 +78,41 @@ Accept:
61
78
  - eg: ['GET', 'PUT', 'POST']
62
79
 
63
80
  ### allowedHeaders
64
- @default `*`
81
+
82
+ @default `true`
65
83
 
66
84
  Assign **Access-Control-Allow-Headers** header.
67
85
 
68
86
  Allow incoming request with the specified headers.
69
87
 
70
88
  Value can be one of the following:
89
+ - `true` - Mirror the request headers.
90
+
71
91
  - `string`
72
- - Expects either a single method or a comma-delimited string (eg: 'Content-Type, Authorization').
92
+ - Expects either a single header or a comma-delimited string (eg: 'Content-Type, Authorization').
73
93
 
74
- - `string[]` - Allow multiple HTTP methods.
94
+ - `string[]` - Allow multiple headers.
75
95
  - eg: ['Content-Type', 'Authorization']
76
96
 
77
- ### exposedHeaders
78
- @default `*`
97
+ ### exposeHeaders
98
+
99
+ @default `true`
79
100
 
80
101
  Assign **Access-Control-Exposed-Headers** header.
81
102
 
82
103
  Return the specified headers to request in CORS mode.
83
104
 
84
105
  Value can be one of the following:
106
+ - `true` - Mirror the request headers.
107
+
85
108
  - `string`
86
- - Expects either a single method or a comma-delimited string (eg: 'Content-Type, 'X-Powered-By').
109
+ - Expects either a single header or a comma-delimited string (eg: 'Content-Type, X-Powered-By').
87
110
 
88
- - `string[]` - Allow multiple HTTP methods.
111
+ - `string[]` - Allow multiple headers.
89
112
  - eg: ['Content-Type', 'X-Powered-By']
90
113
 
91
114
  ### credentials
115
+
92
116
  @default `true`
93
117
 
94
118
  Assign **Access-Control-Allow-Credentials** header.
@@ -98,17 +122,23 @@ Allow incoming requests to send `credentials` header.
98
122
  - `boolean` - Available if set to `true`.
99
123
 
100
124
  ### maxAge
125
+
101
126
  @default `5`
102
127
 
103
128
  Assign **Access-Control-Max-Age** header.
104
129
 
105
- Allow incoming requests to send `credentials` header.
130
+ Duration in seconds to indicates how long the results of a preflight request can be cached.
106
131
 
107
- - `number` - Duration in seconds to indicates how long the results of a preflight request can be cached.
132
+ - `number` - Duration in seconds.
108
133
 
109
134
  ### preflight
135
+
110
136
  @default `true`
111
137
 
112
- Add `[OPTIONS] /*` handler to handle preflight request which response with `HTTP 204` and CORS hints.
138
+ Automatically handle OPTIONS preflight requests which response with `HTTP 204` and CORS headers.
113
139
 
114
140
  - `boolean` - Available if set to `true`.
141
+
142
+ ## License
143
+
144
+ MIT
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vafast/cors",
3
- "version": "0.0.1",
3
+ "version": "0.0.2",
4
4
  "description": "CORS middleware plugin for Vafast framework",
5
5
  "type": "module",
6
6
  "author": {
@@ -19,7 +19,6 @@
19
19
  ".": {
20
20
  "types": "./dist/index.d.ts",
21
21
  "import": "./dist/index.js",
22
- "require": "./dist/index.js",
23
22
  "default": "./dist/index.js"
24
23
  }
25
24
  },
@@ -31,19 +30,10 @@
31
30
  "cors"
32
31
  ],
33
32
  "scripts": {
34
- "clean": "rimraf dist",
35
33
  "build": "tsup",
36
- "dev": "NODE_DEBUG=* bun run --hot example/index.ts",
37
- "test": "bun test",
38
- "test:types": "tsc --noEmit",
39
- "lint": "eslint . --ext .ts",
40
- "lint:fix": "eslint . --ext .ts --fix",
41
- "type-check": "tsc --noEmit",
42
- "prepublishOnly": "npm run build && npm run test",
43
- "release": "npm run build && npm run test && npm publish --access=public",
44
- "release:patch": "npm version patch && npm run release && git push && git push --tags",
45
- "release:minor": "npm version minor && npm run release && git push && git push --tags",
46
- "release:major": "npm version major && npm run release && git push && git push --tags"
34
+ "dev": "npx tsx watch example/index.ts",
35
+ "test": "npx vitest run",
36
+ "release": "npm run build && npm run test && npx bumpp && npm publish --access=public"
47
37
  },
48
38
  "license": "MIT",
49
39
  "files": [