Skip to Content
ExecutorscreateAxiosExecutor

createAxiosExecutor(instanceOrFactory, options?)@routar/axios

Accepts an AxiosInstance (CSR) or a factory function that returns one (SSR).

import axios from 'axios' import { createAxiosExecutor } from '@routar/axios' // CSR — shared instance const executor = createAxiosExecutor(axios.create({ baseURL: 'https://api.example.com' }))

Instance vs. factory

// CSR — pass the instance directly const executor = createAxiosExecutor(axios.create({ baseURL: BASE_URL })) // SSR — factory, called per-request for fresh headers const executor = createAxiosExecutor(async () => { const token = await getServerToken() return axios.create({ baseURL: BASE_URL, headers: { Authorization: `Bearer ${token}` }, }) })

The factory form is called on every request, making it safe for per-request auth headers in SSR environments.

Options

OptionTypeDescription
middlewaresExecutorMiddleware[]Middleware chain for this executor

Errors

HTTP failures are normalized to HttpError (from @routar/core), the same type the fetch executor throws — so callers and onError plugins stay transport-agnostic. The original AxiosError is preserved on err.cause:

import { HttpError } from '@routar/core' import { isAxiosError } from 'axios' try { await todoApi.getDetail({ path: { id: 999 } }) } catch (err) { if (err instanceof HttpError) { console.log(err.status) // 404 console.log(err.statusText) // 'Not Found' console.log(err.body) // server error body // Axios-specific fields remain available via `cause` if (isAxiosError(err.cause)) { console.log(err.cause.config) // request config console.log(err.cause.code) // e.g. 'ERR_BAD_REQUEST' } } }

Errors without a response (network failures, request cancellations) are re-thrown unchanged as the original AxiosError, since there is no HTTP status to normalize.

Query parameter serialization

Axios serializes array params using its own default (ids[]=1&ids[]=2), while createFetchExecutor uses repeated keys (ids=1&ids=2). If your server is sensitive to array format and you switch executors, update your server-side parsing accordingly.

Instance detection

createAxiosExecutor distinguishes an AxiosInstance from a factory function via duck-typing: it checks for both the interceptors property and a request method. AxiosInstance objects always have both; plain factory functions do not.

Last updated on