Reference
createApi options
This page lists every option createApi(config) takes. Look here when you set up a REST client and need an option’s exact type or default.
The config is an ApiConfig.
| Option | Type | Default | What it does |
|---|---|---|---|
baseUrl | string | required | Goes in front of every endpoint’s path. It may carry a query string (baseUrl query merging). A # in it is refused (URL fragments). |
requests | an object of endpoints | required | Each key becomes a method on the client, such as api.getUser. Each value is an endpoint from defineRequest or new Request (Endpoint options). |
middleware | Middleware[] | — | Runs on every call, before endpoint and call middleware (Writing middleware). |
headers | HeadersInit | — | Sent with every call. An endpoint or a call can replace a header (Three levels of settings). |
fetchOptions | FetchOptions | — | Options passed through to fetch on every call, such as credentials: 'include'. An endpoint or a call replaces a field (Cookies and other fetch options). |
fetch | (url: string, init: RequestInit) => Promise<Response> | the global fetch | The fetch this client sends with, such as undici’s or mockFetch().fetch. Without it, liaise looks up the global fetch on every call, so a stub or polyfill installed later is used. It is called unbound, so window.fetch works as is. A method that needs its object, such as a Cloudflare service binding’s, goes in a wrapper: fetch: (url, init) => env.SERVICE.fetch(url, init). cacheMiddleware keys on which fetch sends a call, so clients with different fetch functions never share entries, even through one shared endpoint. Clients given the same function share entries. A wrapper created for each request, such as (url, init) => env.SERVICE.fetch(url, init) inside a client built per request, gets entries of its own, so a cache shared across requests never hits for it: create the wrapper once, outside the request, if those requests may share answers. Clients on the global fetch share as before. |
timeout | number (ms) | no deadline | One deadline for every call, retries included. An endpoint or a call can set its own, and 0 there turns it off (Set a deadline with timeout). |
log | boolean | LogOptions | off | Logs every call to the console (Log every call, Log options). |
onError | (error: ApiError) => void | — | Called once per failed call, after all middleware. Calls that share one failed request count as one. Never for 'abort'. If it throws, the caller still gets its Result (Reporting errors with onError). |
createApi returns a client with one method for each key of requests. A method takes the params and an optional CallOptions, and returns a promise of a Result. The params are optional when the endpoint takes none. Each method also has getHeaders().
createApi throws if an endpoint sets both share and dedupe, because the two are opposites (When not to share). That is a configuration mistake, so it surfaces when you create the client. A call never throws. Every failure comes back in its Result.
Next: createGraphQL options lists the options of the GraphQL client.