TsRestApi and route registration
View as MarkdownIdentity
Section titled “Identity”initContract and TsRestApi exported from @sapporta/server.
Contract
Section titled “Contract”initContract()creates ts-rest query and mutation contracts.new TsRestApi<Env>()creates a Hono-compatible contract registry.register(operationId, route, handler)installs the runtime method/path and adds one documentation emitter to thatTsRestApi. The handler receives parsedrequest, Hono contextc, and thefileschannel for multipart uploads.- Handlers return a response declared by the route as
{ status, body }or a supported raw response. parent.route("/", child)mounts the child’s Hono runtime handlers. It does not copy documentation emitters.parent.extend(child)copies the child’s current documentation emitters. It does not mount runtime handlers.- Call both operations after the child has registered all routes and before
OpenAPI is mounted.
extend()snapshots the emitters present at that call; routes registered on the child later are absent from the parent snapshot. - Project route modules are imported and composed explicitly from
loadApp(). Mounting the parent under/apiadds the public runtime and OpenAPI prefix; project contract paths omit/api.
Minimal lookup
Section titled “Minimal lookup”import { initContract, TsRestApi, type SapportaEnv } from "@sapporta/server";
function mountApi( parent: TsRestApi<SapportaEnv>, child: TsRestApi<SapportaEnv>,) { parent.route("/", child); parent.extend(child);}The @sapporta/server initContract re-export is appropriate in server-only
code. A project shared contract imports it from @sapporta/rest-core so the
shared package remains browser-safe.