Signature
ts
function createClient(options?: ClientOptions): Client;Options
baseUrl requiredstringPrefix for every relative path, e.g.https://api.example.com.headersDefault{}HeadersInitSent with every request. Per-requestheadersare merged on top.timeoutDefault10000numberMilliseconds before a request is aborted.0waits forever.retryDefault{ attempts: 3 }RetryOptions | falseHow failed requests are retried.falseturns retries off.cacheDefaultfalseCacheOptions | falseIn-memory cache forGETrequests.dedupeDefaulttruebooleanShare one in-flightGETbetween callers of the same URL.onRequestInterceptor | Interceptor[]Runs before each try; returns the request to send.onResponseResponseInterceptor | ResponseInterceptor[]Runs on the raw response before it is parsed.fetchDefaultglobalThis.fetchtypeof fetchCustom fetch implementation, e.g. for tests.onError(error: TempoError) => voidCalled once per request after the last retry fails.
| Option | Type | Default |
|---|---|---|
baseUrl requiredPrefix for every relative path, e.g. https://api.example.com. | string | — |
headersSent with every request. Per-request headers are merged on top. | HeadersInit | {} |
timeoutMilliseconds before a request is aborted. 0 waits forever. | number | 10000 |
retryHow failed requests are retried. false turns retries off. | RetryOptions | false | { attempts: 3 } |
cacheIn-memory cache for GET requests. | CacheOptions | false | false |
dedupeShare one in-flight GET between callers of the same URL. | boolean | true |
onRequestRuns before each try; returns the request to send. | Interceptor | Interceptor[] | — |
onResponseRuns on the raw response before it is parsed. | ResponseInterceptor | ResponseInterceptor[] | — |
fetchCustom fetch implementation, e.g. for tests. | typeof fetch | globalThis.fetch |
onErrorCalled once per request after the last retry fails. | (error: TempoError) => void | — |
required
Returns
A Client with get, post, put, patch and delete methods. Each returns a promise of the parsed JSON body.
Example
ts
const api = createClient({ baseUrl: "https://api.example.com", timeout: 5000, retry: { attempts: 5, backoff: "exponential" }, onError: (error) => console.error(error.status, error.url),});