Skip to content
Myr

fix(cors): make origin optional in CORSOptions (#4905)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

* fix(cors): make origin optional in CORSOptions The CORS middleware applies a default of `origin: '*'` when no value is provided, and the JSDoc already documents `[options.origin='*']` (square brackets indicating optional). The type required `origin`, so calling `cors({ allowMethods: ['GET', 'POST'] })` errored at compile time even though the runtime worked correctly. Make `origin` optional on `CORSOptions` so the type matches the runtime default. No runtime change. Test verifies omitting `origin` produces `Access-Control-Allow-Origin: *` and an explicit `allowMethods` value still applies. Closes #4904 * remove the type error ---------

authored by Truffle · committed by GitHub · parent 8f027e5 · Browse files · 2 files+21 added−6 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Trailers

  • Co-authored-by: Yusuke Wada <yusuke@kamawada.com>

Diff stats

2 files+21 added−6 removed

  • tests:+18 added−0 removed
  • other:+3 added−6 removed

2 files+21 added−6 removed

modified:src/middleware/cors/index.test.ts+18 added
416 unmodified lines
417418 expect(res.headers.get('Access-Control-Allow-Origin')).toBe('null')419 expect(res.headers.get('Access-Control-Allow-Credentials')).toBe('true')420 })421422 it('Options without origin fall back to wildcard default', async () => {423 const subApp = new Hono()424 subApp.use('/api/*', cors({ allowMethods: ['GET', 'POST'] }))425 subApp.get('/api/abc', (c) => c.json({ ok: true }))426427 const res = await subApp.request('http://localhost/api/abc')428 expect(res.status).toBe(200)429 expect(res.headers.get('Access-Control-Allow-Origin')).toBe('*')430431 const preflight = await subApp.request(432 new Request('http://localhost/api/abc', { method: 'OPTIONS' })433 )434 expect(preflight.headers.get('Access-Control-Allow-Methods')?.split(/\s*,\s*/)).toEqual([435 'GET',436 'POST',437 ])438 })439})
modified:src/middleware/cors/index.ts+3 added-6 removed