Skip to content

Implement Contract

After defining your contract, the next step is to implement it in your server code. oRPC enforces your contract at runtime, ensuring that your API consistently adheres to its specifications.

Installation

sh
npm install @orpc/server@latest
sh
yarn add @orpc/server@latest
sh
pnpm add @orpc/server@latest
sh
bun add @orpc/server@latest
sh
deno add npm:@orpc/server@latest

The Implementer

The implement function converts your contract into an implementer instance. This instance compatible with the original os from @orpc/server provides a type-safe interface to define your procedures and supports features like Middleware and Context.

ts
import {  } from '@orpc/server'

const  = () // fully replaces the os from @orpc/server

Implementing Procedures

Define a procedure by attaching a .handler to its corresponding contract, ensuring it adheres to the contract's specifications.

ts
export const  = ..
  .(({  }) => {
    // Your logic for listing planets
    return []
  })

Building the Router

To assemble your API, create a router at the root level using .router. This ensures that the entire router is type-checked and enforces the contract at runtime.

ts
const router = os.router({ // <-- Essential for full contract enforcement
  planet: {
    list: listPlanet,
    find: findPlanet,
    create: createPlanet,
  },
})

Full Implementation Example

Below is a complete implementation of the contract defined in the previous section.

ts
const  = ()

export const  = ..
  .(({  }) => {
    return []
  })

export const  = ..
  .(({  }) => {
    return { : 123, : 'Planet X' }
  })

export const  = ..
  .(({  }) => {
    return { : 123, : 'Planet X' }
  })

export const  = .({
  : {
    : ,
    : ,
    : ,
  },
})

Released under the MIT License.