Skip to content

@ts-pf/client

createClient(), Link, intercept() / CallPlugin, RetryPlugin / DedupePlugin / CachePlugin, asResult(), and isLocalFailure() for ts-pf. A typed proxy over any Link; the Fetch link is @ts-pf/client-http. Never imports server code.

Terminal window
npm i @ts-pf/contract @ts-pf/server @ts-pf/server-http @ts-pf/client @ts-pf/client-http zod
// client.ts — `contract` is the router from @ts-pf/contract (planet.list / find / create)
import { asResult, createClient, isLocalFailure, RetryPlugin } from '@ts-pf/client'
import { FetchLink } from '@ts-pf/client-http'
import type { contract } from './contract.js' // type-only import: no server code ships
export const client = createClient<typeof contract>(new FetchLink({ url: '/rpc' }), {
plugins: [new RetryPlugin()], // 3 retries on local failures (network), not aborts
})
const planet = await client.planet.find({ id: 1 }) // throws PFError on failure
const planets = await client.planet.list()
// optional last arg on every call: { signal?: AbortSignal }
const result = await asResult(client.planet.find({ id: 1 }))
if (!result.ok) {
if (isLocalFailure(result.error)) {
// network error or abort — the request never reached the server
} else if (result.error.code === 'NOT_FOUND') {
result.error.data.id // typed from .errors() on the contract
}
}

asResult is optional DX; any HTTP client can read the same { ok: false, error: { code, message, data? } } envelope and switch on error.code. Client-side input validation is off by default. A connection dropped after the request was sent is a local failure too, so RetryPlugin can re-send a write that already landed; pass retry to exclude non-idempotent procedures.

Dedupe and cache are not read-only by default

Section titled “Dedupe and cache are not read-only by default”

DedupePlugin and CachePlugin key on 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.

import { CachePlugin, createClient, DedupePlugin, RetryPlugin } from '@ts-pf/client'
import { FetchLink } from '@ts-pf/client-http'
import type { contract } from './contract.js'
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 }), // success only; ttl required
],
})

Happy path with the matching server: root README. Message links (PortLink / WsLink / StdioLink) are @ts-pf/message-client.

  • createClient, Link, intercept
  • asResult, CallResult, isLocalFailure
  • RetryPlugin, DedupePlugin, CachePlugin, applyPlugins, CallPlugin, CallInterceptor
  • onStart / onSuccess / onError / onFinish

This package ships ts-pf-client. Sync with npx skills experimental_sync -y.