The screenshot API for developers -
Try ScreenshotOne
Skip to content

tRPC Integration

This guide shows how to integrate tRPC with oRPC, so you can use oRPC features in your existing tRPC applications.

Installation

sh
npm install @orpc/trpc@beta
sh
yarn add @orpc/trpc@beta
sh
pnpm add @orpc/trpc@beta
sh
bun add @orpc/trpc@beta
sh
deno add npm:@orpc/trpc@beta

Router Conversion

toORPCRouter converts a tRPC router into an oRPC router:

ts
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.

Error Formatting

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

ts
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.

ts
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}!`
  })

WARNING

Chained tRPC .meta() calls merge shallowly, so oRPC metadata merge logic (e.g. accumulating openapi.tags) only works within a single toTRPCMeta call.

Released under the MIT License.