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 { 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 }) => {
context.resHeaders?.set('x-custom-header', 'value')
})
INFO
Why can resHeaders
be undefined
? This allows procedures to run safely even when ResponseHeadersPlugin
is not used, such as in direct calls.
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.