Skip to content

Commit

Permalink
:chore: broom: bug fix
Browse files Browse the repository at this point in the history
  • Loading branch information
SaltyAom committed Dec 21, 2023
1 parent c224ae6 commit 55a6339
Showing 6 changed files with 90 additions and 57 deletions.
Binary file modified bun.lockb
Binary file not shown.
14 changes: 7 additions & 7 deletions example/index.ts
Original file line number Diff line number Diff line change
@@ -12,12 +12,12 @@ const app = new Elysia()

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

// app.fetch(
// new Request('http://localhost/awd', {
// headers: {
// origin: 'https://saltyaom.com'
// }
// })
// )
app.handle(
new Request('http://localhost/awd', {
headers: {
origin: 'https://saltyaom.com'
}
})
).then(x => x.headers.toJSON()).then(console.log)

export type App = typeof app
88 changes: 44 additions & 44 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,46 +1,46 @@
{
"name": "@elysiajs/cors",
"version": "0.8.0-rc.1",
"description": "Plugin for Elysia that for Cross Origin Requests (CORs)",
"author": {
"name": "saltyAom",
"url": "https://github.com/SaltyAom",
"email": "saltyaom@gmail.com"
},
"repository": {
"type": "git",
"url": "https://github.com/elysiajs/elysia-cors"
},
"exports": {
"bun": "./dist/index.js",
"node": "./dist/cjs/index.js",
"require": "./dist/cjs/index.js",
"import": "./dist/index.js",
"default": "./dist/cjs/index.js"
},
"types": "./src/index.ts",
"homepage": "https://github.com/elysiajs/elysia-cors",
"keywords": [
"elysia",
"cors"
],
"license": "MIT",
"scripts": {
"dev": "bun run --watch example/index.ts",
"test": "bun test && npm run test:node",
"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",
"build": "rimraf dist && tsc --project tsconfig.esm.json && tsc --project tsconfig.cjs.json",
"release": "npm run build && npm run test && npm publish --access public"
},
"devDependencies": {
"@types/node": "^18.11.7",
"bun-types": "^1.0.2",
"elysia": "0.8.0-rc.0",
"eslint": "^8.26.0",
"rimraf": "^3.0.2",
"typescript": "^5.2.2"
},
"peerDependencies": {
"elysia": ">= 0.8.0-rc.0"
}
"name": "@elysiajs/cors",
"version": "0.8.0-rc.1",
"description": "Plugin for Elysia that for Cross Origin Requests (CORs)",
"author": {
"name": "saltyAom",
"url": "https://github.com/SaltyAom",
"email": "saltyaom@gmail.com"
},
"repository": {
"type": "git",
"url": "https://github.com/elysiajs/elysia-cors"
},
"exports": {
"bun": "./dist/index.js",
"node": "./dist/cjs/index.js",
"require": "./dist/cjs/index.js",
"import": "./dist/index.js",
"default": "./dist/cjs/index.js"
},
"types": "./src/index.ts",
"homepage": "https://github.com/elysiajs/elysia-cors",
"keywords": [
"elysia",
"cors"
],
"license": "MIT",
"scripts": {
"dev": "bun run --watch example/index.ts",
"test": "bun test && npm run test:node",
"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",
"build": "rimraf dist && tsc --project tsconfig.esm.json && tsc --project tsconfig.cjs.json",
"release": "npm run build && npm run test && npm publish --access public"
},
"devDependencies": {
"@types/node": "^18.11.7",
"bun-types": "^1.0.2",
"elysia": "0.8.0-rc.7",
"eslint": "^8.26.0",
"rimraf": "^3.0.2",
"typescript": "^5.2.2"
},
"peerDependencies": {
"elysia": ">= 0.8.0-rc.7"
}
}
9 changes: 5 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -149,17 +149,17 @@ interface CORSConfig {
export const cors = (
config: CORSConfig = {
origin: true,
methods: '*',
methods: true,
allowedHeaders: '*',
exposedHeaders: '*',
credentials: false,
credentials: true,
maxAge: 5,
preflight: true
}
) => {
const {
origin = true,
methods = '*',
methods = true,
allowedHeaders = '*',
exposedHeaders = '*',
credentials = true,
@@ -294,7 +294,8 @@ export const cors = (
: exposedHeaders.join(', ')
}

if (credentials) defaultHeaders['Access-Control-Allow-Credentials'] = 'true'
if (credentials === true)
defaultHeaders['Access-Control-Allow-Credentials'] = 'true'

return app.headers(defaultHeaders).onRequest(({ set, request }) => {
handleOrigin(set, request)
4 changes: 2 additions & 2 deletions test/index.test.ts
Original file line number Diff line number Diff line change
@@ -10,9 +10,9 @@ describe('CORS', () => {

const res = await app.handle(req('/'))
expect(res.headers.get('Access-Control-Allow-Origin')).toBe('*')
expect(res.headers.get('Access-Control-Allow-Methods')).toBe('*')
expect(res.headers.get('Access-Control-Allow-Methods')).toBe('GET')
expect(res.headers.get('Access-Control-Allow-Headers')).toBe('*')
expect(res.headers.get('Access-Control-Exposed-Headers')).toBe('*')
expect(res.headers.get('Access-Control-Allow-Credentials')).toBe(null)
expect(res.headers.get('Access-Control-Allow-Credentials')).toBe('true')
})
})
32 changes: 32 additions & 0 deletions test/methods.test.ts
Original file line number Diff line number Diff line change
@@ -32,4 +32,36 @@ describe('Methods', () => {
'GET, POST'
)
})

it('Accept *', async () => {
const app = new Elysia()
.use(
cors({
methods: '*'
})
)
.get('/', () => 'HI')

const res = await app.handle(req('/'))
expect(res.headers.get('Access-Control-Allow-Methods')).toBe('*')
})

it('Accept true', async () => {
const app = new Elysia()
.use(
cors({
methods: true
})
)
.get('/', () => 'HI')
.post('/', () => 'HI')

const get = await app.handle(req('/'))
expect(get.headers.get('Access-Control-Allow-Methods')).toBe('GET')

const post = await app.handle(
new Request('http://localhost/', { method: 'POST' })
)
expect(post.headers.get('Access-Control-Allow-Methods')).toBe('POST')
})
})

0 comments on commit 55a6339

Please sign in to comment.