Registry / devops / axios-interceptor

axios-interceptor

JSON →
library3.0.24jsnpmunverified

A utility library for Axios that adds configurable interceptors for caching, request deduplication, timestamp injection, retry on failure, and pending request queue monitoring. Version 3.0.24 is the latest stable release, with active development on GitHub. Key differentiators: built-in getKey for request identity, flexible cache with per-request control, and a simple API for attaching interceptors to any Axios instance.

npm install axios-interceptor
INSTALL
IMPORT
SIG · AXIOS-INTERCEPTOR
A
axios-interceptor
devopsjavascriptv3.0.24
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

useInterceptor
import { useInterceptor } from 'axios-interceptor'
import useInterceptor from 'axios-interceptor'
Default export is not provided; only named export available since v3.
CacheMap
import { CacheMap } from 'axios-interceptor'
import { CacheMap } from 'axios-interceptor/src/types'
Type is exported directly; do not import from internal paths as they may change.
useInterceptor (via require)
const { useInterceptor } = require('axios-interceptor')
const useInterceptor = require('axios-interceptor')
CommonJS requires destructuring for the named export.

Shows how to set up axios-interceptor with caching, deduplication, timestamp, retry, and queue change listener on an Axios instance.

import axios from 'axios'; import { useInterceptor } from 'axios-interceptor'; const instance = axios.create(); const { doRetry } = useInterceptor({ axios: instance, getKey: (config) => JSON.stringify(config), useCache: { isSuccess: (value) => value.status >= 200 && value.status < 300 }, useDebounce: true, useTimestamp: { timestampKey: 'ts' }, useRetry: { isRetry: (err) => !!err.response?.status && err.response.status >= 500 && err.response.status < 600 }, useChange: { requestListChange: (configs) => console.log(configs) }, }); async function main() { const res = await instance.get('https://jsonplaceholder.typicode.com/todos/1'); console.log(res.data); // To retry: doRetry(config) } main();
Debug
Known issues
breakingv3 changed exports from default to named (useInterceptor).
fix
Change import { useInterceptor } from 'axios-interceptor' (remove default import).
affects: <3.0.0
deprecateduseRetry is replaced by doRetry method returned from useInterceptor.
fix
Use const { doRetry } = useInterceptor(...) and call doRetry(config) to retry.
affects: >=3.0.0
gotchagetKey must return a unique string per request; using JSON.stringify(config) may cause issues with non-serializable fields (e.g., circular references).
fix
Implement a custom getKey that extracts relevant fields (method, url, params, data).
affects: >=3.0.0
gotchauseCache relies on getKey; if getKey is not deterministic, caching may produce wrong results.
fix
Ensure getKey produces consistent keys for requests that should be cached.
affects: >=3.0.0
Errors
Common errors & fixes
TypeError: Cannot destructure property 'useInterceptor' of ... as it is undefined.
Using default import instead of named import on v3.
fix
Use import { useInterceptor } from 'axios-interceptor' (curly braces).
Error: getKey is required when using useCache or useDebounce.
Missing getKey option in useInterceptor.
fix
Provide a getKey function, e.g., getKey: (config) => JSON.stringify(config).
AxiosError: Request failed with status code 404 - but useRetry does not retry.
useRetry.isRetry only triggers on 5xx errors; 404 is 4xx.
fix
Adjust isRetry to include 4xx if desired, e.g., isRetry: (err) => err.response?.status >= 400.
Upgrade
Version history
3.0.24latest on npm
Audit
Dependencies
axiosrequiredPeer dependency: provides the HTTP client that the interceptors modify
Agent activity
9 hits · last 30 days
node
8
OpenAI (training)
1
Resources
axios-interceptor — npm install axios-interceptor · libregistry