oRPC Procedure

Understanding procedures in oRPC — standard functions with validation, middleware, and DI.

SKILL.md

GitHub

Procedure in oRPC

A procedure is like a standard function with built-in support for:

  • Input/output validation
  • Middleware
  • Dependency injection
  • Other extensibility features

Overview

import { os } from '@orpc/server'

const example = os
  .use(aMiddleware)
  .input(z.object({ name: z.string() }))
  .use(aMiddlewareWithInput, input => input.name)
  .output(z.object({ id: z.number() }))
  .handler(async ({ input, context }) => {
    return { id: 1 }
  })
  .callable() // Make callable
  .actionable() // Server Action compatibility

Only .handler is required.

Input/Output Validation

Supports Zod, Valibot, Arktype, and any Standard Schema library.

Explicitly specifying .output or handler return type dramatically improves type-checking speed.

type Utility

import { os, type } from '@orpc/server'

const example = os
  .input(type<{ value: number }>())
  .output(type<{ value: number }, number>(({ value }) => value))
  .handler(async ({ input }) => input)

Using Middleware

const aMiddleware = os.middleware(async ({ context, next }) => next())

const example = os
  .use(aMiddleware)
  .use(async ({ context, next }) => next())
  .handler(async ({ context }) => {})

Initial Configuration

const base = os.$input(z.void())
const base = os.$input<Schema<void, unknown>>()

.$input lets you redefine the input schema after initial configuration.

Reusability

Each modification creates a new instance:

const pub = os.use(logMiddleware)
const authed = pub.use(authMiddleware)

const pubExample = pub.handler(async ({ context }) => {})
const authedExample = pubExample.use(authMiddleware)