Skip to content
You are reading the v2 docs, currently in beta.V1 docs
oRPC
Esc
↑↓navigate↵open⌘Jpreview
On this page

Plugins

Add CORS, batching, retries, compression, timeouts, and validation to oRPC with built-in plugins for handlers on the server and links on the client.

Plugins add reusable behavior through the plugins option. Handler plugins (...HandlerPlugin) run on the server, in a handler such as the RPC Handler. Link plugins (...LinkPlugin) run on the client, in a link such as the RPC Link. Some features need both.

Security

Performance

Reliability

  • Retry (client): retry failed calls.
  • Retry After (client): retry when the server sends Retry-After.
  • Timeout (both): abort calls that take too long.
  • Signal Sync (server): keep the request signal and streaming responses in sync.

Headers and HTTP

  • Request Headers (server): read headers through context.reqHeaders.
  • Response Headers (server): set headers and cookies through context.resHeaders.
  • Cookie Jar (client): store and send cookies like a browser, for server-side clients.
  • HEAD Method (server): answer HEAD requests with the headers of GET procedures.
  • Method Override (server): let HTML forms call PUT, PATCH, and DELETE.
  • Rethrow (server): pass errors on to your framework’s error handling.

Validation and Types

Files and Docs

  • Static File (server): serve static files next to your procedures.
  • Tmp File Upload (server): stream large uploads to temporary files.
  • OpenAPI Reference (server): serve Scalar or Swagger UI and the OpenAPI document.

Custom Plugins

A plugin has a name and an init method that receives the handler or link options and returns new ones:

import type { Context } from '@orpc/server'
import type { StandardHandlerOptions, StandardHandlerPlugin } from '@orpc/server/standard'

export class LoggingHandlerPlugin<T extends Context> implements StandardHandlerPlugin<T> {
  readonly name = 'logging'
  readonly after = ['~batch']

  init(options: StandardHandlerOptions<T>): StandardHandlerOptions<T> {
    return {
      ...options,
      routingInterceptors: [
        async ({ next, request }) => {
          const start = performance.now()
          const result = await next()
          console.log(`${request.method} took ${performance.now() - start}ms`)
          return result
        },
        ...(options.routingInterceptors ?? []),
      ],
    }
  }
}

Link plugins use StandardLinkPlugin and StandardLinkOptions from @orpc/client/standard.

Plugin Order

Plugins can position themselves relative to others by name:

  • before: ['~x']: initialize before ~x.
  • after: ['~x']: initialize after ~x.

Unknown names are ignored. A cycle throws.

What init order means for interceptors depends on where a plugin adds its own:

  • Prepends: initialized later means it wraps the earlier plugins.
  • Appends: initialized later means it runs inside the earlier plugins.
  • A prepended interceptor always wraps an appended one, whatever the init order.

Most built-in handler plugins prepend a routing interceptor, so after: ['~batch'] runs once per batch request and before: ['~batch'] runs once per call in the batch. Regular interceptors always run once per call.

Built-in Plugin Names

Built-in plugin names start with ~, such as ~batch and ~cors. Do not use the ~ prefix for your own plugins.

~tracing is always present on handlers. It creates the request span.

Last updated on

Was this page helpful?