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(...)
        .(() => {})     // Apply middleware to 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 (, , ) => {
    // ...
})
 
const  = 
    .()
    .({
       // ...
    })
// All routes in this router will require authentication

Nested Routers

Create hierarchical route structures:

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

Fundamentals

In oRPC, a router is essentially a nested object of procedures. The router syntax provides a convenient way to apply common configurations to multiple procedures:

import {  } from '@orpc/server'
 
const  = .(() => {})
const  = .(() => {})
 
const  = .('/users').({
    : ,
    : ,
})
 
// This is equivalent to:
 
const  = {
    : .('/users'),
    : .('/users'),
}
 
// The same principle applies to middlewares

On this page