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
| Option | Type | Description |
|---|---|---|
middlewares | ExecutorMiddleware[] | Middleware chain for this executor |
Errors
Axios errors propagate as AxiosError. All Axios-specific fields are preserved:
import { isAxiosError } from 'axios'
try {
await todoApi.getDetail({ path: { id: 999 } })
} catch (err) {
if (isAxiosError(err)) {
console.log(err.response?.status) // 404
console.log(err.response?.data) // server error body
console.log(err.config) // request config
}
}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