Skip to content

Response Headers Plugin

Use ResponseHeadersHandlerPlugin to accumulate response headers in context.resHeaders and merge them into the final response.

Context Access

ts
import type { ResponseHeadersHandlerPluginContext } from '@orpc/server/plugins'

interface ServerContext extends ResponseHeadersHandlerPluginContext {}

const 
base
=
os
.
$context
<ServerContext>()
const
procedure
=
base
.
use
(({
context
,
next
}) => {
context
.
resHeaders
?.
set
('x-request-id', 'req_123')
return
next
()
}) .
handler
(({
context
}) => {
setCookie
(
context
.
resHeaders
, 'session_id', 'abc123', {
secure
: true,
maxAge
: 3600
}) })

Why can resHeaders be undefined?

This allows procedures to run safely even without ResponseHeadersHandlerPlugin, such as in direct calls.

TIP

Combine with Cookie Helpers for streamlined cookie management.

Handler Setup

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

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

INFO

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

Learn More

For implementation details, see the source code.

Released under the MIT License.