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

tRPC Integration

Learn how to integrate tRPC with oRPC by converting tRPC routers into oRPC routers and bridging metadata between the two.

Installation

npm install @orpc/trpc@beta
pnpm add @orpc/trpc@beta
yarn add @orpc/trpc@beta
bun add @orpc/trpc@beta
nub add @orpc/trpc@beta
aube add @orpc/trpc@beta

Router Conversion

toORPCRouter converts a tRPC router into an oRPC router:

import { toORPCRouter } from '@orpc/trpc'

const orpcRouter = toORPCRouter(trpcRouter)

The result is a regular oRPC router that works with any oRPC feature. For example, you can expose it through an RPC Handler or OpenAPI Handler, or call it directly with Server-Side Clients.

Input and output schemas that implement Standard Schema are carried over, including every chained .input(). Validation still runs inside tRPC.

Error Formatting

toORPCRouter does not support tRPC Error Formatting. Instead, errors thrown by tRPC are wrapped in ORPCError.

const handler = new OpenAPIHandler(orpcRouter, {
  interceptors: [
    async ({ next }) => {
      try {
        return await next()
      } catch (error) {
        if (
          error instanceof ORPCError &&
          error.cause instanceof TRPCError &&
          error.cause.cause instanceof z.ZodError
        ) {
          throw new ORPCError('UNPROCESSABLE_CONTENT', {
            message: z.prettifyError(error.cause.cause),
            data: z.flattenError(error.cause.cause),
            cause: error.cause.cause,
          })
        }

        throw error
      }
    },
  ],
})

Metadata

toTRPCMeta bridges oRPC metadata with tRPC meta. It returns a plain object that you can pass to tRPC .meta calls.

import { openapi } from '@orpc/openapi'
import { toTRPCMeta } from '@orpc/trpc'

export const t = initTRPC.context<Context>().create()

const example = t.procedure
  .meta(toTRPCMeta(openapi({ path: '/hello', summary: 'Hello procedure' }))) 
  .input(z.object({ name: z.string() }))
  .query(({ input }) => {
    return `Hello, ${input.name}!`
  })

const merged = t.procedure
  .meta({
    ...toTRPCMeta(
      openapi({ path: '/hello' }), 
      openapi({ method: 'POST' }), 
    ), 
    other: 'value',
  })
  .input(z.object({ name: z.string() }))
  .mutation(({ input }) => {
    return `Hello, ${input.name}!`
  })

Last updated on

Was this page helpful?