Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

@elysiajs/cors

elysiajs144.8kMIT1.3.3TypeScript support: included

Plugin for Elysia that for Cross Origin Requests (CORs)

elysia, cors

readme

@elysiajs/cors

Plugin for elysia that for Cross Origin Requests (CORs)

Installation

bun add @elysiajs/cors

Example

import { Elysia } from 'elysia'
import { cors } from '@elysiajs/cors'

const app = new Elysia()
    .use(cors())
    .listen(8080)

Config

origin

@default true

Assign the Access-Control-Allow-Origin header.

Value can be one of the following:

  • string - String of origin which will directly assign to Access-Control-Allow-Origin

  • boolean - If set to true, Access-Control-Allow-Origin will be set to * (accept all origin)

  • RegExp - Pattern to use to test with request's url, will accept origin if matched.

  • Function - Custom logic to validate origin acceptance or not. will accept origin if true is returned.

    • Function will accepts Context just like Handler

      // Example usage
      app.use(cors, {
        origin: ({ request, headers }) => true
      })
      
      // Type Definition
      type CORSOriginFn = (context: Context) => boolean | void
  • Array<string | RegExp | Function> - Will try to find truthy value of all options above. Will accept Request if one is true.

methods

@default *

Assign Access-Control-Allow-Methods header.

Value can be one of the following: Accept:

  • undefined | null | '' - Ignore all methods.

  • * - Accept all methods.

  • HTTPMethod - Will be directly set to Access-Control-Allow-Methods.

    • Expects either a single method or a comma-delimited string (eg: 'GET, PUT, POST')
  • HTTPMethod[] - Allow multiple HTTP methods.

    • eg: ['GET', 'PUT', 'POST']

allowedHeaders

@default *

Assign Access-Control-Allow-Headers header.

Allow incoming request with the specified headers.

Value can be one of the following:

  • string

    • Expects either a single method or a comma-delimited string (eg: 'Content-Type, Authorization').
  • string[] - Allow multiple HTTP methods.

    • eg: ['Content-Type', 'Authorization']

exposedHeaders

@default *

Assign Access-Control-Exposed-Headers header.

Return the specified headers to request in CORS mode.

Value can be one of the following:

  • string

    • Expects either a single method or a comma-delimited string (eg: 'Content-Type, 'X-Powered-By').
  • string[] - Allow multiple HTTP methods.

    • eg: ['Content-Type', 'X-Powered-By']

credentials

@default true

Assign Access-Control-Allow-Credentials header.

Allow incoming requests to send credentials header.

  • boolean - Available if set to true.

maxAge

@default 5

Assign Access-Control-Max-Age header.

Allow incoming requests to send credentials header.

  • number - Duration in seconds to indicates how long the results of a preflight request can be cached.

preflight

@default true

Add [OPTIONS] /* handler to handle preflight request which response with HTTP 204 and CORS hints.

  • boolean - Available if set to true.

changelog

1.3.3 - 15 May 2025

Bug fix:

  • Using Bun shortcut to mirror headers

1.3.2 - 15 May 2025

Bug fix:

  • #65 strictly check origin protocol

1.3.1 - 8 May 2025

Bug fix:

  • strictly check origin not using sub includes

1.3.0-exp.0 - 23 Apr 2025

Change:

  • Add support for Elysia 1.3

1.2.0-rc.0 - 23 Dec 2024

Change:

  • Add support for Elysia 1.2

1.1.1 - 5 Sep 2024

Feature:

  • add provenance publish

1.1.0 - 16 Jul 2024

Change:

  • Add support for Elysia 1.1

1.0.5 - 14 Jul 2024

Bug fix:

  • fix content-type on preflight

1.0.4 - 9 Jul 2024

Bug fix:

  • Mirror allow method on preflight options

1.0.3 - 18 Mar 2024

Feature:

  • add true to allowHeaders, and exposeHeaders

Breaking Change:

  • rename exposedHeaders to exposeHeaders

Change:

  • using true on methods, allowHeaders, and exposeHeaders by default
  • using strict origin processing as per spec
  • add support for predefined origin to handle any protocol

1.0.2 - 18 Mar 2024

Change:

  • Add support for Elysia 1.0

1.0.0 - 16 Mar 2024

Change:

  • Add support for Elysia 1.0

1.0.0-rc.0 - 1 Mar 2024

Change:

  • Add support for Elysia 1.0

1.0.0-beta.1 - 17 Feb 2024

Change:

  • Add support for Elysia 1.0

1.0.0-beta.0 - 6 Feb 2024

Change:

  • Add support for Elysia 1.0

0.8.0 - 23 Dec 2023

Change:

  • Add support for Elysia 0.8

0.8.0-rc.1 - 21 Dec 2023

Improvement:

  • Using Elysia.headers to set default headers
  • Get value of static field ahead of time

Change:

  • set credential to true by default

Bug fix:

  • origin with out protocol is now handle

0.8.0-rc.0 - 15 Dec 2023

Change:

  • Add support for Elysia 0.8

0.7.2 - 14 Nov 2023

Bug fix:

  • Response using origin instead of '*' when presented

0.7.1 - 26 Sep 2023

Bug fix:

  • strictly handle string[]

0.7.0 - 20 Sep 2023

  • Add support for Elysia 0.7

0.7.0-beta.0 - 18 Sep 2023

  • Add support for Elysia 0.7

0.6.0 - 6 Aug 2023

  • Add support for Elysia 0.6

0.6.0-rc.0 - 6 Aug 2023

  • Add support for Elysia 0.6

    0.5.0 - 15 May 2023

  • Add support for Elysia 0.5
  • Add CommonJS support

0.3.0 - 17 Mar 2023

Improvement:

  • Add support for Elysia 0.3.0

0.3.0-rc.0 - 7 Mar 2023

Improvement:

  • Add support for Elysia 0.3.0-rc.0

0.1.0-rc.3 - 13 Dec 2022

Change:

  • Minimum requirement for Elysia is set to 0.1.0-rc.5
  • Using PreContext instead of Context

Fix:

  • Headers not present on Error

0.1.0-rc.2 - 9 Dec 2022

Fix:

  • Add main fields Bundlephobia

0.1.0-rc.1 - 6 Dec 2022

Improvement:

  • Support for Elysia 0.1.0-rc.1 onward

0.0.0-experimental.4 - 22 Nov 2022

Change:

  • Support for KingWorld 0.0.0-experimental.51

0.0.0-experimental.2 - 30 Oct 2022

Change:

  • Support for KingWorld 0.0.0-experimental.40

0.0.0-experimental.2 - 30 Oct 2022

Change:

  • Support for KingWorld 0.0.0-experimental.28 onward
  • chore: update dependencies