Skip to content

Commit 55a6339

Browse files
committed
:chore: broom: bug fix
1 parent c224ae6 commit 55a6339

File tree

6 files changed

+90
-57
lines changed

6 files changed

+90
-57
lines changed

bun.lockb

13 Bytes
Binary file not shown.

example/index.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@ const app = new Elysia()
1212

1313
console.log(`Elysia is running at ${app.server?.hostname}:${app.server?.port}`)
1414

15-
// app.fetch(
16-
// new Request('http://localhost/awd', {
17-
// headers: {
18-
// origin: 'https://saltyaom.com'
19-
// }
20-
// })
21-
// )
15+
app.handle(
16+
new Request('http://localhost/awd', {
17+
headers: {
18+
origin: 'https://saltyaom.com'
19+
}
20+
})
21+
).then(x => x.headers.toJSON()).then(console.log)
2222

2323
export type App = typeof app

package.json

Lines changed: 44 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,46 @@
11
{
2-
"name": "@elysiajs/cors",
3-
"version": "0.8.0-rc.1",
4-
"description": "Plugin for Elysia that for Cross Origin Requests (CORs)",
5-
"author": {
6-
"name": "saltyAom",
7-
"url": "https://github.com/SaltyAom",
8-
"email": "saltyaom@gmail.com"
9-
},
10-
"repository": {
11-
"type": "git",
12-
"url": "https://github.com/elysiajs/elysia-cors"
13-
},
14-
"exports": {
15-
"bun": "./dist/index.js",
16-
"node": "./dist/cjs/index.js",
17-
"require": "./dist/cjs/index.js",
18-
"import": "./dist/index.js",
19-
"default": "./dist/cjs/index.js"
20-
},
21-
"types": "./src/index.ts",
22-
"homepage": "https://github.com/elysiajs/elysia-cors",
23-
"keywords": [
24-
"elysia",
25-
"cors"
26-
],
27-
"license": "MIT",
28-
"scripts": {
29-
"dev": "bun run --watch example/index.ts",
30-
"test": "bun test && npm run test:node",
31-
"test:node": "npm install --prefix ./test/node/cjs/ && npm install --prefix ./test/node/esm/ && node ./test/node/cjs/index.js && node ./test/node/esm/index.js",
32-
"build": "rimraf dist && tsc --project tsconfig.esm.json && tsc --project tsconfig.cjs.json",
33-
"release": "npm run build && npm run test && npm publish --access public"
34-
},
35-
"devDependencies": {
36-
"@types/node": "^18.11.7",
37-
"bun-types": "^1.0.2",
38-
"elysia": "0.8.0-rc.0",
39-
"eslint": "^8.26.0",
40-
"rimraf": "^3.0.2",
41-
"typescript": "^5.2.2"
42-
},
43-
"peerDependencies": {
44-
"elysia": ">= 0.8.0-rc.0"
45-
}
2+
"name": "@elysiajs/cors",
3+
"version": "0.8.0-rc.1",
4+
"description": "Plugin for Elysia that for Cross Origin Requests (CORs)",
5+
"author": {
6+
"name": "saltyAom",
7+
"url": "https://github.com/SaltyAom",
8+
"email": "saltyaom@gmail.com"
9+
},
10+
"repository": {
11+
"type": "git",
12+
"url": "https://github.com/elysiajs/elysia-cors"
13+
},
14+
"exports": {
15+
"bun": "./dist/index.js",
16+
"node": "./dist/cjs/index.js",
17+
"require": "./dist/cjs/index.js",
18+
"import": "./dist/index.js",
19+
"default": "./dist/cjs/index.js"
20+
},
21+
"types": "./src/index.ts",
22+
"homepage": "https://github.com/elysiajs/elysia-cors",
23+
"keywords": [
24+
"elysia",
25+
"cors"
26+
],
27+
"license": "MIT",
28+
"scripts": {
29+
"dev": "bun run --watch example/index.ts",
30+
"test": "bun test && npm run test:node",
31+
"test:node": "npm install --prefix ./test/node/cjs/ && npm install --prefix ./test/node/esm/ && node ./test/node/cjs/index.js && node ./test/node/esm/index.js",
32+
"build": "rimraf dist && tsc --project tsconfig.esm.json && tsc --project tsconfig.cjs.json",
33+
"release": "npm run build && npm run test && npm publish --access public"
34+
},
35+
"devDependencies": {
36+
"@types/node": "^18.11.7",
37+
"bun-types": "^1.0.2",
38+
"elysia": "0.8.0-rc.7",
39+
"eslint": "^8.26.0",
40+
"rimraf": "^3.0.2",
41+
"typescript": "^5.2.2"
42+
},
43+
"peerDependencies": {
44+
"elysia": ">= 0.8.0-rc.7"
45+
}
4646
}

src/index.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -149,17 +149,17 @@ interface CORSConfig {
149149
export const cors = (
150150
config: CORSConfig = {
151151
origin: true,
152-
methods: '*',
152+
methods: true,
153153
allowedHeaders: '*',
154154
exposedHeaders: '*',
155-
credentials: false,
155+
credentials: true,
156156
maxAge: 5,
157157
preflight: true
158158
}
159159
) => {
160160
const {
161161
origin = true,
162-
methods = '*',
162+
methods = true,
163163
allowedHeaders = '*',
164164
exposedHeaders = '*',
165165
credentials = true,
@@ -294,7 +294,8 @@ export const cors = (
294294
: exposedHeaders.join(', ')
295295
}
296296

297-
if (credentials) defaultHeaders['Access-Control-Allow-Credentials'] = 'true'
297+
if (credentials === true)
298+
defaultHeaders['Access-Control-Allow-Credentials'] = 'true'
298299

299300
return app.headers(defaultHeaders).onRequest(({ set, request }) => {
300301
handleOrigin(set, request)

test/index.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ describe('CORS', () => {
1010

1111
const res = await app.handle(req('/'))
1212
expect(res.headers.get('Access-Control-Allow-Origin')).toBe('*')
13-
expect(res.headers.get('Access-Control-Allow-Methods')).toBe('*')
13+
expect(res.headers.get('Access-Control-Allow-Methods')).toBe('GET')
1414
expect(res.headers.get('Access-Control-Allow-Headers')).toBe('*')
1515
expect(res.headers.get('Access-Control-Exposed-Headers')).toBe('*')
16-
expect(res.headers.get('Access-Control-Allow-Credentials')).toBe(null)
16+
expect(res.headers.get('Access-Control-Allow-Credentials')).toBe('true')
1717
})
1818
})

test/methods.test.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,36 @@ describe('Methods', () => {
3232
'GET, POST'
3333
)
3434
})
35+
36+
it('Accept *', async () => {
37+
const app = new Elysia()
38+
.use(
39+
cors({
40+
methods: '*'
41+
})
42+
)
43+
.get('/', () => 'HI')
44+
45+
const res = await app.handle(req('/'))
46+
expect(res.headers.get('Access-Control-Allow-Methods')).toBe('*')
47+
})
48+
49+
it('Accept true', async () => {
50+
const app = new Elysia()
51+
.use(
52+
cors({
53+
methods: true
54+
})
55+
)
56+
.get('/', () => 'HI')
57+
.post('/', () => 'HI')
58+
59+
const get = await app.handle(req('/'))
60+
expect(get.headers.get('Access-Control-Allow-Methods')).toBe('GET')
61+
62+
const post = await app.handle(
63+
new Request('http://localhost/', { method: 'POST' })
64+
)
65+
expect(post.headers.get('Access-Control-Allow-Methods')).toBe('POST')
66+
})
3567
})

0 commit comments

Comments
 (0)