oRPC Router

Understanding routers in oRPC — nestable objects composed of procedures.

SKILL.md

GitHub

Router in oRPC

Routers are simple, nestable objects composed of procedures.

Overview

import { os } from '@orpc/server'

const ping = os.handler(async () => 'ping')
const pong = os.handler(async () => 'pong')

const router = {
  ping,
  pong,
  nested: { ping, pong }
}

Extending Router

const router = os.use(requiredAuth).router({
  ping,
  pong,
  nested: { ping, pong }
})

Avoid duplicate middleware execution. See Dedupe Middleware.

Lazy Router

// router.ts
const router = {
  ping,
  pong,
  planet: os.lazy(() => import('./planet'))
}
// planet.ts
export const listPlanet = os
  .input(z.object({ limit: z.number().optional() }))
  .handler(async ({ input }) => [{ id: 1, name: 'name' }])

export default {
  list: listPlanet,
}

Alternative using the standalone lazy helper (faster type inference):

import { lazy } from '@orpc/server'

const router = {
  ping,
  pong,
  planet: lazy(() => import('./planet'))
}

Utilities

import type {
  InferRouterInputs, InferRouterOutputs,
  InferRouterInitialContexts, InferRouterCurrentContexts
} from '@orpc/server'

type Inputs = InferRouterInputs<typeof router>
type Outputs = InferRouterOutputs<typeof router>
type InitialContexts = InferRouterInitialContexts<typeof router>
type CurrentContexts = InferRouterCurrentContexts<typeof router>

type FindPlanetInput = Inputs['planet']['find']
type FindPlanetOutput = Outputs['planet']['find']