Two kinds of context
| Kind | Description |
|---|---|
| Initial context | The object you pass when calling handler.handle(). Typically set per-request by the HTTP adapter. |
| Current context | The initial context enriched by middleware. Each middleware can extend it with new properties. |
Declaring the initial context
Use.$context<T>() on the builder to declare what the initial context looks like. This ensures TypeScript enforces that handlers pass the right context:
Passing initial context to the handler
Context is provided when you callhandler.handle() in your HTTP adapter:
Enriching context in middleware
Middleware can add new properties by passing a partial context object tonext():
Sharing a base builder
The recommended pattern is to export a single baseos instance configured for your application, then derive all procedures from it:
Type inference
oRPC tracks bothTInitialContext and TCurrentContext as separate generic parameters on the builder. This ensures:
- TypeScript knows exactly which properties the HTTP adapter must supply.
- TypeScript knows exactly which properties are available inside handlers.
- Middleware can narrow or widen context in a type-safe way.
