API reference

createClient

Create a configured client instance.

Signature

ts
function createClient(options?: ClientOptions): Client;

Options

  • baseUrl required
    string
    Prefix for every relative path, e.g. https://api.example.com.
  • headersDefault {}
    HeadersInit
    Sent with every request. Per-request headers are merged on top.
  • timeoutDefault 10000
    number
    Milliseconds before a request is aborted. 0 waits forever.
  • retryDefault { attempts: 3 }
    RetryOptions | false
    How failed requests are retried. false turns retries off.
  • cacheDefault false
    CacheOptions | false
    In-memory cache for GET requests.
  • dedupeDefault true
    boolean
    Share one in-flight GET between callers of the same URL.
  • onRequest
    Interceptor | Interceptor[]
    Runs before each try; returns the request to send.
  • onResponse
    ResponseInterceptor | ResponseInterceptor[]
    Runs on the raw response before it is parsed.
  • fetchDefault globalThis.fetch
    typeof fetch
    Custom fetch implementation, e.g. for tests.
  • onError
    (error: TempoError) => void
    Called once per request after the last retry fails.
OptionTypeDefault
baseUrl required
Prefix for every relative path, e.g. https://api.example.com.
string—
headers
Sent with every request. Per-request headers are merged on top.
HeadersInit{}
timeout
Milliseconds before a request is aborted. 0 waits forever.
number10000
retry
How failed requests are retried. false turns retries off.
RetryOptions | false{ attempts: 3 }
cache
In-memory cache for GET requests.
CacheOptions | falsefalse
dedupe
Share one in-flight GET between callers of the same URL.
booleantrue
onRequest
Runs before each try; returns the request to send.
Interceptor | Interceptor[]—
onResponse
Runs on the raw response before it is parsed.
ResponseInterceptor | ResponseInterceptor[]—
fetch
Custom fetch implementation, e.g. for tests.
typeof fetchglobalThis.fetch
onError
Called 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),});

Was this page helpful?

Updated