Skip to content

Response Headers Plugin

The Response Headers Plugin allows you to set response headers in oRPC. It injects a resHeaders instance into the context, enabling you to modify response headers easily.

Context Setup

ts
import { 
setCookie
} from '@orpc/server/helpers'
import { ResponseHeadersPluginContext } from '@orpc/server/plugins' interface ORPCContext extends ResponseHeadersPluginContext {} const
base
=
os
.
$context
<ORPCContext>()
const
example
=
base
.
use
(({
context
,
next
}) => {
context
.
resHeaders
?.
set
('x-custom-header', 'value')
return
next
()
}) .
handler
(({
context
}) => {
setCookie
(
context
.
resHeaders
, 'session_id', 'abc123', {
secure
: true,
maxAge
: 3600
}) })

INFO

Why can resHeaders be undefined? This allows procedures to run safely even when ResponseHeadersPlugin is not used, such as in direct calls.

TIP

Combine with Cookie Helpers for streamlined cookie management.

Handler Setup

ts
import { ResponseHeadersPlugin } from '@orpc/server/plugins'

const handler = new RPCHandler(router, {
  plugins: [
    new ResponseHeadersPlugin()
  ],
})

INFO

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

Released under the MIT License.