Router to Contract
A normal router works as a contract router as long as it does not include a lazy router. This guide not only shows you how to unlazy a router to make it compatible with contracts, but also how to minify it and prevent internal business logic from being exposed to the client.
Unlazy the Router
If your router includes a lazy router, you need to fully resolve it to make it compatible with contract.
import { unlazyRouter } from '@orpc/server'
const resolvedRouter = await unlazyRouter(router)
Minify & Export the Contract Router for the Client
Sometimes, you'll need to import the contract on the client - for example, to use OpenAPILink or define request methods in RPCLink.
If you're using Contract First, this is safe: your contract is already lightweight and free of business logic.
However, if you're deriving the contract from a router, importing it directly can be heavy and may leak internal logic. To prevent this, follow the steps below to safely minify and export your contract.
Minify the Contract Router and Export to JSON
tsimport fs from 'node:fs' import { minifyContractRouter } from '@orpc/contract' const minifiedRouter = minifyContractRouter(router) fs.writeFileSync('./contract.json', JSON.stringify(minifiedRouter))
WARNING
minifyContractRouter
preserves only the metadata and routing information necessary for the client, all other data will be stripped out.Import the Contract JSON on the Client Side
tsimport contract from './contract.json' const link = new OpenAPILink(contract as any, { url: 'http://localhost:3000/api', })