Getting started
TypeScript Procedure Factory — a contract-first, end-to-end type-safe RPC library.
Source: github.com/crobinson42/ts-pf
This procedure model, any pipe. oRPC-like DX without the dual-protocol platform. You write a contract, implement it on the server, and call it from a client that never imports server code. Fetch is the default adapter, not the runtime.
Requires Node.js 22+.
Install
Section titled “Install”npm install @ts-pf/contract @ts-pf/server @ts-pf/server-http @ts-pf/client @ts-pf/client-httpEvery @ts-pf/* package releases on one version and follows semver from 1.0. Keep them all on the same major; within it, packages depend on each other with ^ ranges, so npm update (or grouped or ungrouped Renovate / Dependabot updates) keeps one copy of each. Cross a major in one install command for every @ts-pf/* package.
@ts-pf/protocol (PFError) comes along as a dependency of server and client; import it directly, and add it to your own dependencies if your package manager blocks undeclared transitive imports.
TypeScript setup
Section titled “TypeScript setup”The packages are ESM and ship exports only, so tsconfig.json needs "moduleResolution": "node16", "nodenext", or "bundler" (the classic "node" resolver cannot see them). The types reference AbortSignal (CallOptions.signal), and FetchHandler and FetchLink are typed against the Fetch Request / Response globals: on a Node project that is "lib": ["ES2022", "DOM", "DOM.AsyncIterable"], or Node 22+ globals through @types/node listed in "types": ["node"] (TypeScript 6 no longer loads @types/* automatically). Bundler and Workers projects usually have both already.
Packages
Section titled “Packages”| Package | Role |
|---|---|
@ts-pf/contract |
procedure / router, schema adapters, nested routers, typed errors, infer types |
@ts-pf/protocol |
Portable JSON envelope, PFError, PROTOCOL_VERSION |
@ts-pf/server |
createImplementer(), middleware, runProcedure, lookupProcedure, createLocalClient(), call interceptors / DedupePlugin |
@ts-pf/client |
createClient(), Link, intercept() / CallPlugin, RetryPlugin / DedupePlugin / CachePlugin, asResult(), isLocalFailure() |
@ts-pf/http |
HTTP wire helpers: JsonCodec, RpcCodec, PROTOCOL_HEADER, path helpers, httpStatus |
@ts-pf/server-http |
FetchHandler, HandlerPlugin (CORS / limits / headers) |
@ts-pf/client-http |
FetchLink, Fetch interceptors |
@ts-pf/file |
Opt-in MultipartCodec for File/Blob attachments |
@ts-pf/stream |
Opt-in StreamCodec for root AsyncIterable (JSONL) |
@ts-pf/docs |
Opt-in procedure catalog from a contract (docs(), catalog()) |
@ts-pf/openapi |
Opt-in OpenAPI 3.1 from catalog() (POST JSON RPC) |
@ts-pf/codegen |
Opt-in .d.ts from catalog() for split repos; catalog breaking-change check |
@ts-pf/message |
Opt-in JSON text frames + MessageSession (not an HTTP codec) |
@ts-pf/message-server |
Opt-in PortHandler / WsHandler / StdioHandler |
@ts-pf/message-client |
Opt-in PortLink / WsLink / StdioLink |
@ts-pf/swr |
Opt-in SWR keys, fetchers, mutators, and matchers |
@ts-pf/mvc-kit |
Opt-in bindClient + issuesToFieldErrors + taskIssues for mvc-kit Resources |
@ts-pf/astro |
Opt-in Astro endpoint (createAstroHandler) and page caller (createAstroLocalClient / createAstroClient) |
@ts-pf/mcp |
Opt-in MCP tools from a contract: toolkit, toolsets, tool hosts, $coverage, and an MCP SDK v2 server factory |
Wire spec: packages/protocol/PROTOCOL.md.
Guides
Section titled “Guides”Prose for humans, between this README and the wire spec. Index: docs/README.md. The same pages (plus every package README) are published at crobinson42.github.io/ts-pf.
- Tutorial: install, contract, implementer,
FetchHandler,createClient. - Concepts: contract vs implementer vs app, context, middleware, links, codecs, the three interceptor kinds, validation defaults.
- Errors:
PFError, declared errors, the protocol code set and its JSON,asResult,statusper transport,onError. - Evolving a contract: safe vs breaking contract changes, old clients and new codes, deprecation, error maps, catalog diffs in CI.
- Clients in other languages: calling a ts-pf server from Dart, Swift, Kotlin, Python, Go, …: models from the codegen-profile OpenAPI, a thin transport, response classification, streams, conformance fixtures.
- Testing: in-process tests through a
fetchstub orcreateLocalClient; declared errors, middleware, streams. - Coming from tRPC / oRPC: name mapping and what is different.
Agent skills
Section titled “Agent skills”Each @ts-pf/* package ships skills/ts-pf-<name>/. @ts-pf/contract also ships the hub catalog ts-pf-app (Fetch happy path plus opt-in capabilities the app has not installed yet). Sync copies skills from installed packages. After install or npm update:
npx skills experimental_sync -yThat links the skills into the project’s agent skill dirs. Put the command on the app postinstall if you want it automatic — never on @ts-pf/*. Index of every consumer skill: skills/README.md (not published, not an authoring home). Do not add a published @ts-pf/skills package.
Contract
Section titled “Contract”import { procedure, router } from '@ts-pf/contract'import { z } from 'zod'import Type from 'typebox'
export const contract = router({ planet: { list: procedure.output(z.array(z.object({ id: z.number(), name: z.string() }))), find: procedure .input(z.object({ id: z.number() })) .output(z.object({ id: z.number(), name: z.string() })) .errors({ NOT_FOUND: { status: 404, data: z.object({ id: z.number() }) } }), create: procedure .input(Type.Object({ name: Type.String() })) .output(Type.Object({ id: Type.Number(), name: Type.String() })), },})Schemas: any Standard Schema library (Zod, Valibot, ArkType) or TypeBox. Register more with registerSchemaAdapter.
Server
Section titled “Server”import { createImplementer } from '@ts-pf/server'import { FetchHandler } from '@ts-pf/server-http'import { PFError } from '@ts-pf/protocol'import { contract } from './contract.js'
// What the adapter supplies. Middleware adds the rest (`user`), typed.export type AppCtx = { db: Db; req: Request }
const impl = createImplementer(contract).$context<AppCtx>()
const requireUser = impl.middleware(async ({ context, next }) => { const user = await auth(context.req) if (!user) throw new PFError({ code: 'UNAUTHORIZED', status: 401 }) return next({ context: { user } })})
export const app = impl.use(requireUser).router({ planet: { list: impl.planet.list.handler(async ({ context }) => context.db.planets.all()), find: impl.planet.find.handler(async ({ input, context, errors }) => { const row = await context.db.planets.get(input.id) if (!row) throw errors.NOT_FOUND({ id: input.id }) return row }), create: impl.planet.create.handler(async ({ input, context }) => context.db.planets.create(input), ), },})
// `FetchHandler` infers `AppCtx` from `app`, so the `context` passed to// handle() (next section) is checked against it.export const handler = new FetchHandler(app)Db, User, db, and auth() are your app’s types and values. $context<AppCtx>() is the context the adapter supplies. A middleware’s next({ context: { user } }) adds user to the type of the middleware and handlers built after it (const authed = impl.use(requireUser), then authed.planet.create.handler(...); see the @ts-pf/server README). impl.router() carries AppCtx on app: FetchHandler, the message handlers, createLocalClient, and @ts-pf/astro read it from there and reject a context that does not match (a plain object of handlers that did not go through impl.router() infers unknown instead), and impl.router() itself rejects a missing or extra key, a handler placed under another procedure’s key, or a slice built with a different $context, naming the key in the error.
.use() runs before input validation. .useAfter() runs after, with typed input when the input check ran. Input schemas are checked unless validation.input is false. Output schemas, including stream output items, are checked only when validation.output is true (FetchHandler, message handlers, createLocalClient, and runProcedure).
Serving FetchHandler
Section titled “Serving FetchHandler”FetchHandler speaks Fetch Request / Response only. Any runtime or framework that hands you a Request can host it: call handler.handle(request, { prefix, context }) and fall through when result.matched is false so your other routes still answer. ts-pf ships no Node IncomingMessage adapter and no generic framework adapters (the one opt-in integration is @ts-pf/astro); @hono/node-server, srvx, Hono, and Next.js below are userland packages, not @ts-pf/*.
Write the entry point once:
import { handler } from './server.js'
export async function rpc(request: Request): Promise<Response> { const result = await handler.handle(request, { prefix: '/rpc', context: (req) => ({ db, req }), }) if (!result.matched) return new Response('Not Found', { status: 404 }) return result.response}Cloudflare Workers — the module fetch export:
import { rpc } from './rpc.js'
export default { fetch: rpc }Bun:
import { rpc } from './rpc.js'
Bun.serve({ fetch: rpc, port: 3000 })Deno:
import { rpc } from './rpc.js'
Deno.serve(rpc)Node.js has no built-in Fetch server. Use a small userland bridge — @hono/node-server:
import { serve } from '@hono/node-server'import { rpc } from './rpc.js'
serve({ fetch: rpc, port: 3000 })or srvx:
import { serve } from 'srvx'import { rpc } from './rpc.js'
serve({ fetch: rpc, port: 3000 })Host extras such as srvx’s request.ip live only on the request srvx passes in. RequestLimitPlugin rewraps any request with a body, so the context factory’s req is a plain Request without them. Type the entry point with srvx’s ServerRequest and close over the outer request:
import { type ServerRequest, serve } from 'srvx'import { handler } from './server.js'
serve({ port: 3000, async fetch(request: ServerRequest) { const result = await handler.handle(request, { prefix: '/rpc', context: (req) => ({ db, req, ip: request.ip }), }) if (!result.matched) return new Response('Not Found', { status: 404 }) return result.response },})Inside a Hono app, mount it as a route and let c.notFound() handle the miss:
import { Hono } from 'hono'import { handler } from './server.js'
const web = new Hono()
web.all('/rpc/*', async (c) => { const result = await handler.handle(c.req.raw, { prefix: '/rpc', context: (req) => ({ db, req }), }) if (!result.matched) return c.notFound() return result.response})
export default webNext.js App Router — a catch-all route handler (POST only; add OPTIONS if CORSPlugin answers preflight):
import { handler } from '../../../server.js'
export const POST = async (req: Request) => { const result = await handler.handle(req, { prefix: '/rpc', context: (r) => ({ db, req: r }), }) if (!result.matched) return new Response('Not Found', { status: 404 }) return result.response}Astro has a first-party endpoint helper in @ts-pf/astro (createAstroHandler).
Compose
Section titled “Compose”router() nests slice contracts. impl.router() nests finished slice implementations. One server, one contract; the client never sees the slices.
import { router } from '@ts-pf/contract'import { createImplementer } from '@ts-pf/server'
export const contract = router({ planet: planetContract, star: starContract,})
const impl = createImplementer(contract).$context<AppCtx>()export const app = impl.router({ planet: planetApp, star: starApp,})planetApp comes from createImplementer(planetContract).$context<AppCtx>().router({ ... }). Use the same AppCtx on every slice.
A second server (web vs mobile) is a second composed contract — pick procedures when the trees differ (router({ list: users.list }) vs router({ list: users.listPopulated }), then { list: usersApp.list } vs { list: usersApp.listPopulated }). Shared domain logic stays plain functions. No mergeRouters.
HTTP plugins
Section titled “HTTP plugins”FetchHandler accepts opt-in HandlerPlugins for origin concerns. Procedure middleware cannot see Request / Response.
import { CORSPlugin, FetchHandler, RequestHeadersPlugin, RequestLimitPlugin, ResponseHeadersPlugin,} from '@ts-pf/server-http'import { app, type AppCtx } from './server.js'
const handler = new FetchHandler(app, { plugins: [ new CORSPlugin({ origin: ['https://app.example.com'] }), new RequestLimitPlugin({ maxBodySize: 1024 * 1024 }), new RequestHeadersPlugin(), new ResponseHeadersPlugin(), ],})CORSPlugin answers OPTIONS preflight (browsers will preflight: JSON + x-ts-pf-protocol are not CORS-safelisted). RequestLimitPlugin caps the HTTP body; multipart file caps stay on MultipartCodec. Header plugins inject optional context.reqHeaders / context.resHeaders; intersect RequestHeadersPluginContext / ResponseHeadersPluginContext (exported from @ts-pf/server-http) into the type you pass to $context<…>() so handlers see them.
FetchHandler.plugins are HTTP HandlerPlugins. Server call interceptors (CallInterceptor / DedupePlugin / onStart from @ts-pf/server) go on { interceptors } — a different list. Client retry/cache live on createClient.
import { applyPlugins, DedupePlugin, onStart } from '@ts-pf/server'import { CORSPlugin, FetchHandler } from '@ts-pf/server-http'import { app, type AppCtx } from './server.js'
// Dedupe reads only. The default key covers every unary call, writes included.const readKey = ({ path, input }: { path: string[]; input: unknown }) => path.join('.') === 'planet.find' ? JSON.stringify([path, input]) : undefined
const interceptors = applyPlugins( [new DedupePlugin({ key: readKey })], [onStart(({ path }) => console.log(path.join('.')))],)
new FetchHandler(app, { plugins: [new CORSPlugin({ origin: ['https://app.example.com'] })], interceptors,})createLocalClient(app, { context, plugins, interceptors }) accepts CallPlugins directly. FetchHandler / PortHandler take { interceptors } only — pass applyPlugins([new DedupePlugin({ key })], …) for plugins. They do not take HandlerPlugin as call interceptors.
Client
Section titled “Client”import { asResult, createClient, onStart, RetryPlugin } from '@ts-pf/client'import { FetchLink } from '@ts-pf/client-http'import type { ContractClient } from '@ts-pf/contract'import type { contract } from './contract.js'
export const client: ContractClient<typeof contract> = createClient( new FetchLink({ url: '/rpc' }), { plugins: [new RetryPlugin()], interceptors: [onStart(({ path }) => console.log(path.join('.')))], },)
const planet = await client.planet.find({ id: 1 })const listed = await client.planet.list()// optional second arg: { signal?: AbortSignal } (also on handler opts)
const result = await asResult(client.planet.find({ id: 1 }))if (!result.ok && result.error.code === 'NOT_FOUND') { result.error.data.id}import { asResult, isLocalFailure } from '@ts-pf/client'import { client } from './client.js'
const result = await asResult(client.planet.find({ id: 1 }))if (!result.ok) { if (isLocalFailure(result.error)) { // local: true — never reached the server } else if (result.error.code === 'NOT_FOUND') { result.error.data.id }}Any HTTP client parses the same { ok: false, error: { code, message, data? } } envelope and switches on error.code. asResult is optional TypeScript DX.
Retry, dedupe, cache
Section titled “Retry, dedupe, cache”RetryPlugin, DedupePlugin, and CachePlugin are CallPlugins on createClient(link, { plugins }). RetryPlugin retries local failures (not aborts) up to 3 times by default. A connection dropped after the request was sent is a local failure too, so the default can re-send a write that already landed; pass retry to exclude non-idempotent procedures.
DedupePlugin and CachePlugin are not read-only by default. Their default key is JSON.stringify([path, input]) for every unary call, mutations included — two concurrent planet.create({ name: 'x' }) calls would share one request and resolve to the same id. Pass key and return undefined for anything that is not a read. The same applies to the server DedupePlugin in @ts-pf/server. Runnable form: examples/plugins/src/read-key.ts.
import { CachePlugin, createClient, DedupePlugin, RetryPlugin } from '@ts-pf/client'import { FetchLink } from '@ts-pf/client-http'import type { contract } from './contract.js'
// Only planet.find shares in-flight work or a cache entry. Writes get undefined.const readKey = ({ path, input }: { path: string[]; input: unknown }) => path.join('.') === 'planet.find' ? JSON.stringify([path, input]) : undefined
const client = createClient<typeof contract>(new FetchLink({ url: '/rpc' }), { plugins: [ new RetryPlugin(), new DedupePlugin({ key: readKey }), new CachePlugin({ ttl: 5_000, key: readKey }), ],})Procedure catalogs are opt-in. Do not put this in the default happy path:
import { procedure } from '@ts-pf/contract'import { catalog, docs } from '@ts-pf/docs'import { contract } from './contract.js'
procedure.meta(docs({ description: 'Find a planet by id' }))const spec = catalog(contract, { prefix: '/rpc' })OpenAPI 3.1 is an opt-in projection of that catalog. Do not put this in the default happy path:
import { procedure } from '@ts-pf/contract'import { catalog, docs } from '@ts-pf/docs'import { openapi } from '@ts-pf/openapi'import { contract } from './contract.js'
procedure.meta(docs({ description: 'Find a planet by id' }))const spec = openapi(catalog(contract, { prefix: '/rpc' }), { info: { title: 'Planet API', version: '1.0.0' },})Split-repo typed clients are an opt-in projection of that catalog too. The server repo prints a .d.ts once per release; the client repo checks in only that file. Do not put this in the default happy path:
import { writeFileSync } from 'node:fs'import { catalog } from '@ts-pf/docs'import { emit } from '@ts-pf/codegen'import { contract } from './contract.js'
writeFileSync('contract.d.ts', emit(catalog(contract, { prefix: '/rpc' })))// client/planets.ts — next to the emitted client/contract.d.tsimport { createClient } from '@ts-pf/client'import { FetchLink } from '@ts-pf/client-http'import type { Contract } from './contract.js'
const client = createClient<Contract>(new FetchLink({ url: '/rpc' }))Message transports are opt-in too. Do not put this in the default happy path:
import { router } from '@ts-pf/contract'import { createImplementer } from '@ts-pf/server'import { PortHandler } from '@ts-pf/message-server'import { PortLink } from '@ts-pf/message-client'import { createClient } from '@ts-pf/client'import { contract } from './contract.js'
// `bind` wants the context type of the app it serves. A port has no HTTP// request, so the HTTP `app` above (its AppCtx carries `req: Request`) is the// wrong app for it: pick the procedures the port needs into a second// implementer with a transport-neutral `$context`.const portContract = router({ planet: { list: contract.planet.list } })const portImpl = createImplementer(portContract).$context<{ db: Db }>()const portApp = portImpl.router({ planet: { list: portImpl.planet.list.handler(({ context }) => context.db.planets.all()) },})
const { port1, port2 } = new MessageChannel()new PortHandler(portApp).bind(port1, { context: () => ({ db }) })const client = createClient<typeof portContract>(new PortLink({ port: port2 }))A context that does not need the request ({ db: Db; user?: User }, with the user resolved in each adapter’s context factory) serves one app over HTTP and message transports alike; see Concepts.
SWR is opt-in too. Do not put this in the default happy path:
import { createSwr } from '@ts-pf/swr'import useSWR from 'swr'import { client } from './client.js'
const swr = createSwr(client)const { data } = useSWR( swr.planet.find.key({ input: { id: 1 } }), swr.planet.find.fetcher(),)mvc-kit is opt-in too. Do not put this in the default happy path:
import { bindClient } from '@ts-pf/mvc-kit'import { Resource } from 'mvc-kit'import { client } from './client.js'
class PlanetsResource extends Resource<Planet> { private rpc = bindClient(client, this) async loadById(id: number) { this.upsert(await this.rpc.planet.find({ id })) }}Why not oRPC?
Section titled “Why not oRPC?”oRPC is a dual RPC + OpenAPI platform with many adapters, serializers, and integrations. ts-pf keeps the contract-first DX and typed middleware. The catalog is the portable contract. @ts-pf/openapi is a document projection of catalog() (POST JSON RPC), not an OpenAPI runtime or REST handler. @ts-pf/codegen prints a .d.ts from that catalog for split-repo createClient<Contract> — not an OpenAPI runtime. TanStack Query and extra adapters stay later packages. SWR lives in @ts-pf/swr. mvc-kit Resource helpers live in @ts-pf/mvc-kit.
Examples
Section titled “Examples”Smoke-tested examples in examples/: hello (Fetch; also runs as a server with npm run dev), codegen (split-repo emit / createClient<Contract>), message (MessagePort), stream (StreamCodec), plugins (CallPlugin / CallInterceptor), mcp (@ts-pf/mcp over the MCP SDK v2). Each exercises its client against its handler in-process; build first (npm run build), then see examples/README.md.
Development
Section titled “Development”See CONTRIBUTING.md for setup, the verify chain, changesets, and releasing; DECISIONS.md records why each lock exists.
npm installnpm run lint && npm run lint:root && npm run check:skills && npm run check:deps && npm run type-check && npm test && npm run build && npm run check:exports && npm run check:snippetsA public API / name / happy-path change updates packages/<pkg>/skills/ts-pf-<pkg>/SKILL.md and that package’s README.md in the same PR (npm run check:skills asserts every skill API symbol is named in the README; npm run check:snippets type-checks the fenced ts blocks). Opt-in happy-path changes also update the matching snippet on ts-pf-app.
Releases use Changesets (latest on npm). See .changeset/README.md. A PR that changes a published packages/* package must include a changeset (npx changeset).