Skip to main content

Config

API, endpoint, and individual request configuration are merged in that order. Prefer defaultRequestConfig; config is retained as a deprecated alias.

API defaults may also be a function when values must be computed per request:

const API = new Api({
name: "My Backend",
baseUrl: "https://api.example.com",
defaultRequestConfig: () => ({
headers: { Authorization: `Bearer ${getToken()}` },
}),
});
import { Api } from "api-def";

const API = new Api({
name: "My Backend",
baseUrl: "http://localhost:5000/v1",
defaultRequestConfig: {
retry: 2,
headers: {
"Cool-Header": "hello!",
},
},
});

export const fetchData = API.endpoint().build({
id: "fetch_data",
defaultRequestConfig: {
retry: false,
clientCache: 1000 * 60 * 15, // 15 mins
browserCache: "force-cache", // Use browser's cache
},
});

Properties

Request config properties:

NameTypeDefaultUsage
retrynumber, false, or RetryOptionsfalseRetry failed requests; a number is the retry count.
clientCachenumber or booleanfalseCache successful GET responses for a duration in milliseconds.
browserCacheRequestCacheundefinedFetch cache mode; Axios maps it to cache headers.
headersReadonly<RawHeaders>undefinedHeaders sent with the request.
acceptableStatusAcceptableStatus[]200299Status codes or inclusive ranges treated as successful.
credentials"omit", "same-origin", or "include"undefinedCredential policy for the request backend.
lockstring or falseundefinedCancel an earlier request using the same lock key.