Skip to content

CORS Handler Plugin

Use CORSHandlerPlugin to configure CORS Policy for your API.

Basic

ts
import { 
CORSHandlerPlugin
} from '@orpc/server/plugins'
const
handler
= new
RPCHandler
(
router
, {
plugins
: [
new
CORSHandlerPlugin
({
origin
: (
origin
,
options
) =>
origin
,
allowMethods
: ['GET', 'HEAD', 'PUT', 'POST', 'DELETE', 'PATCH'],
// ... }), ], })

INFO

The handler can be any supported oRPC handler, such as RPCHandler, OpenAPIHandler, or a custom one.

WARNING

To better support Blob, File, and ReadableStream<Uint8Array> at the root level in cross-origin scenarios, extend your CORS allowlist to allow clients to send and receive the Content-Disposition and Standard-Server headers. Learn more in the Standard Server documentation. If you use the CORS Plugin, include them in allowHeaders and exposeHeaders:

ts
const cors = new CORSHandlerPlugin({
  allowHeaders: ['Content-Disposition', 'Standard-Server'],
  exposeHeaders: ['Content-Disposition', 'Standard-Server'],
})

Learn More

For implementation details, see the source code.

Released under the MIT License.