oRPC
background

Router

A powerful way to organize procedures with enhanced developer experience.

Introduction

A router is a collection of procedures with utilities that help reduce code duplication and improve organization.

import {  } from '@orpc/server'
 
const  = .({: '/'}).(() => {})
 
export const  = .({
    : {
        : ,
    },
 
    : 
        .('/users')  // Prefix will be concatenated with paths defined in route(...)
        .((, , ) => /* Logic applies for all routes in this router */ .({}))
        .({
            : ,
        }),
})

All router functionality is fully typed and begins with the orpc instance, providing a consistent and intuitive API.

Key Features

Prefixing Routes

You can add a prefix to all routes in a router:

import {  } from '@orpc/server'
 
const  = 
    .('/users')
    .({
        // ...
    })

Middleware Application

Apply middleware to all procedures in a router:

import {  } from '@orpc/server'
 
const  = .(async (, , ) => {
    // ...
    return .({})
})
 
const  = 
    .()
    .({
       // ...
    })
// All routes in this router will require authentication

Nested Routers

Create hierarchical route structures:

import {  } from '@orpc/server'
 
const  = .({
    // ...
})
 
const  = .({
    // ...
})
 
const  = .({
    // ...
})
 
const  = .({
    : ,
    : ,
    : .('/comments').(),
})

Infer Router Inputs and Outputs

The InferRouterInputs and InferRouterOutputs type utilities can be used to infer the input and output types of a router, respectively.

import { type , type  } from '@orpc/server'
import {  } from 'examples/server'
 
type  = <typeof >
type  = <typeof >
 
type  = ['getting']
type  = ['post']['find']

On this page