axios client
The axios client is a plain object with one typed function per endpoint. It runs anywhere axios does: a browser, another Node service, or your tests.
Generate it with setupCodegen, with type: 'axios'.
Create a client
createApiClient takes axios defaults, or an axios instance you already built:
const client = createApiClient({
baseURL: 'https://api.example.com',
headers: { 'x-tenant': 'acme' },
});
// or send through an instance of your own
const instance = axios.create({ baseURL: 'https://api.example.com' });
const sameClient = createApiClient(instance);Every call goes through that instance, which is always at client.axios:
client.axios.defaults.headers.common.Authorization = `Bearer ${token}`;Call an operation
Each operation takes one object that groups its request into path, query and body, from the endpoint's params, query and input:
const created = await client.orders.create({
body: { productId: 'sku_42', quantity: 2 },
});
const page = await client.orders.list({
query: { page: 2 },
body: { status: 'pending' },
});
const order = await client.orders.view({ path: { orderId: 1 } });A call resolves to the response: status, data, headers and the rest. data is typed from the endpoint's output.
const result = await client.orders.view({ path: { orderId: 1 } });
result;headers is a web Headers object.
When the body is all you want, .unwrap() resolves to it alone:
const order = await client.orders.view({ path: { orderId: 1 } }).unwrap();Errors
A response outside the 2xx range throws a ResponseError, with the status in error.status and the response body in error.data. isResponseError() narrows a caught error to it:
try {
await client.orders.view({ path: { orderId: 404 } });
} catch (error) {
if (isResponseError(error) && error.status === 404) {
// error.data is the response body
console.log(error.data);
} else {
throw error;
}
}To handle every status without try and catch, pass throwOnError: false. The call then resolves on any status, with the error body in error and data undefined:
const result = await client.orders.view({
path: { orderId: 404 },
throwOnError: false,
});
if (result.error) {
console.log(result.status, result.error);
}A request that gets no response at all, such as when the server is unreachable, throws axios's own error.
Per-call options
Beside path, query and body, a call takes:
options: axios request config for this call.signal: anAbortSignal, to cancel it.throwOnError: see Errors.
const controller = new AbortController();
await client.orders.view({
path: { orderId: 1 },
options: { timeout: 5000 },
signal: controller.signal,
});Headers and interceptors
- Set request headers on the axios instance,
client.axios, as defaults or in a request interceptor. Endpoints declare no header parameters, so a call takes none of its own. - Request interceptors apply to every call, along with the instance's defaults (
baseURL,headers,withCredentials). - Read results and errors from the call, not from response interceptors. The client decodes each response itself, so a response interceptor sees every status as a success, with the body not yet decoded.
client.axios.interceptors.request.use((config) => {
config.headers.Authorization = `Bearer ${currentToken()}`;
return config;
});