Response Headers Plugin
Use ResponseHeadersHandlerPlugin to set response headers and cookies via context.resHeaders and merge them into the final response.
Context Access
import type { ResponseHeadersHandlerPluginContext } from '@orpc/server/plugins'
interface ServerContext extends ResponseHeadersHandlerPluginContext {}
const const base: Builder<ServerContext & object, Record<never, never>>base = const os: Builder<DefaultInitialContext & object, Record<never, never>>The oRPC procedure builder. Chain methods like `.input`, `.use`, and `.handler`
to define procedures, then compose them into routers.os.Builder<DefaultInitialContext & object, Record<never, never>>.$context<ServerContext>(): Builder<ServerContext & object, Record<never, never>>Declares the initial context type that must be provided when executing
procedures built from this builder.$context<ServerContext>()
const const procedure: DecoratedProcedure<ServerContext & object, object, InitialInputSchema, Schema<void>, Record<never, never>, never>procedure = const base: Builder<ServerContext & object, Record<never, never>>base
.Builder<ServerContext & object, Record<never, never>>.use<object, ServerContext & object, Record<never, never>>(middleware: Middleware<ServerContext & object, object, unknown, unknown, Record<never, never>>): BuilderWithMiddlewares<ServerContext & object, object, Record<never, never>>Applies a middleware that runs before the handler of every procedure
built from this builder.use(({ context: ServerContext & objectcontext, next: MiddlewareNext<unknown>Invoke to continue the middleware chain.next }) => {
context: ServerContext & objectcontext.ResponseHeadersHandlerPluginContext.resHeaders?: Headers | undefinedResponse headers as a Headers instance. This is injected by the Response Headers Plugin.
When set before the response is sent, these headers will be included in the response. If not set, no additional headers will be added.resHeaders?.Headers.set(name: string, value: string): voidThe **`set()`** method of the Headers interface sets a new value for an existing header inside a Headers object, or adds the header if it does not already exist.
[MDN Reference](https://developer.mozilla.org/docs/Web/API/Headers/set)set('x-request-id', 'req_123')
return next: MiddlewareNext
<object>(options?: {
context?: object | undefined;
} | undefined) => MiddlewareResult<object, unknown>
Invoke to continue the middleware chain.next()
})
.BuilderWithMiddlewares<ServerContext & object, object, Record<never, never>>['handler']<void>(handler: ProcedureHandler<ServerContext & object, unknown, void, ORPCErrorConstructorMap<Record<never, never>>>): DecoratedProcedure<ServerContext & object, object, InitialInputSchema, Schema<void>, Record<never, never>, never>Defines the function that implements the procedure and completes the
chain, returning a callable procedure.handler(({ context: ServerContext & objectcontext }) => {
function setCookie(headers: Headers | undefined, name: string, value: string, options?: SetCookieOptions): voidSets a cookie in the response headers.
Does nothing if `headers` is `undefined`.setCookie(context: ServerContext & objectcontext.ResponseHeadersHandlerPluginContext.resHeaders?: Headers | undefinedResponse headers as a Headers instance. This is injected by the Response Headers Plugin.
When set before the response is sent, these headers will be included in the response. If not set, no additional headers will be added.resHeaders, 'session_id', 'abc123', {
secure?: boolean | undefinedEnables the [`Secure` `Set-Cookie` attribute](https://tools.ietf.org/html/rfc6265#section-5.2.5).
When enabled, clients will only send the cookie back if the browser has an HTTPS connection.secure: true,
maxAge?: number | undefinedSpecifies the `number` (in seconds) to be the value for the [`Max-Age` `Set-Cookie` attribute](https://tools.ietf.org/html/rfc6265#section-5.2.2).
The [cookie storage model specification](https://tools.ietf.org/html/rfc6265#section-5.3) states that if both `expires` and
`maxAge` are set, then `maxAge` takes precedence, but it is possible not all clients by obey this,
so if both are set, they should point to the same date and time.maxAge: 3600
})
})
Handler Setup
import { ResponseHeadersHandlerPlugin } from '@orpc/server/plugins'
const handler = new RPCHandler(router, {
plugins: [
new ResponseHeadersHandlerPlugin(),
],
})
Learn More
For implementation details, see the source code.