Skip to content

@ts-pf/client-http

FetchLink and FetchInterceptors. Implements Link from @ts-pf/client. Analog of @ts-pf/message-client.

Agent skill: skills/ts-pf-client-http/. Sync with npx skills experimental_sync -y.

Fetch interceptors wrap Request / Response inside FetchLink (headers, raw HTTP). They do not see structured RPC input or mapped PFError. Retry, cache, and in-flight dedupe belong on createClient as call interceptors / CallPlugins (RetryPlugin, DedupePlugin, CachePlugin).

import { createClient, RetryPlugin } from '@ts-pf/client'
import { FetchLink } from '@ts-pf/client-http'
const client = createClient<typeof contract>(
new FetchLink({
url: '/rpc',
interceptors: [
async ({ request, next }) => {
request.headers.set('x-trace', '1')
return next(request)
},
],
}),
{ plugins: [new RetryPlugin()] },
)

To replace the request, pass next() a new Request(request, { … }) built from the one you were given, which keeps its signal. headers in that init replaces every header, so start from new Headers(request.headers): without the original content-type, stream input and file uploads fail. new Request(request, …) also uses up the body of request unless you pass a new body. To call next() more than once, build each attempt with new Request(request.clone(), { signal: request.signal }), and don’t pass a bare request.clone(): in Node it can lose the caller’s abort after a garbage collection.

To send headers with every call, use the headers option: an object, or a function (sync or async) that runs once each time FetchLink builds a request: again for each RetryPlugin attempt, but not for each next() in an interceptor. If it throws, the call rejects with that error as is: a PFError such as UNAUTHORIZED reaches asResult, and any other error is not wrapped in one. FetchLink sets content-type and x-ts-pf-protocol itself, so its values win over yours:

new FetchLink({
url: '/rpc',
headers: async () => ({ authorization: `Bearer ${await getToken()}` }),
})

A custom fetch is called with one argument, the Request. Its type is typeof fetch, but FetchLink never passes init. Pass that request straight to fetch(request), or fetch(request, { … }) for options such as credentials, and don’t wrap it in another Request. Don’t set headers there: they replace every header on the request, including content-type and x-ts-pf-protocol. Plain JSON calls can still work without them, but stream input and file uploads fail with 400 Invalid JSON. Don’t set signal there either: it replaces the caller’s, so aborting the call no longer reaches the request.

FetchLink sends x-ts-pf-protocol: 1. A response whose x-ts-pf-protocol is present and not 1 is not decoded; the call rejects with INTERNAL, the HTTP status, and message Unsupported protocol version (x-ts-pf-protocol: <value>) — not a local failure, so RetryPlugin does not retry it by default. A response without the header that fails to decode is INTERNAL with the HTTP status (Non-RPC response (HTTP …)).

Do not import this package from @ts-pf/server-http (prod).