Registry / security / axios-token-refresh

axios-token-refresh

JSON →
library1.0.10jsnpmunverified

axios-token-refresh (v1.0.10) is a lightweight Axios interceptor utility that automatically retries failed HTTP requests after a token refresh. It requires a custom refresh function and optionally configurable status codes (default 401). Ships TypeScript definitions, supports Bun >=1.2.12, and peer depends on axios >=0.21.1. Differentiates from similar libraries by minimal API surface and clear error handling.

npm install axios-token-refresh
INSTALL
IMPORT
SIG · AXIOS-TOKEN-REFRES
A
axios-token-refresh
securityjavascriptv1.0.10
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.

registerAxiosTokenRefresh
import registerAxiosTokenRefresh from 'axios-token-refresh'
const { registerAxiosTokenRefresh } = require('axios-token-refresh')
Default export only, not named. CJS require returns an object with default property.
registerAxiosTokenRefresh (type)
import type { RefreshRequestConfig } from 'axios-token-refresh'
Type exports available for TypeScript users; check package types.
AxiosInstance
import { AxiosInstance } from 'axios'
The library expects an Axios instance, not the global axios.

Shows how to create an Axios instance, define a refresh token function, and register the interceptor.

import axios from 'axios'; import registerAxiosTokenRefresh from 'axios-token-refresh'; const api = axios.create({ baseURL: 'https://api.example.com' }); const refreshAuth = (failedRequest) => axios.post('/auth/refresh', { refreshToken: localStorage.getItem('refreshToken') }) .then(resp => { const newToken = resp.data.accessToken; localStorage.setItem('accessToken', newToken); failedRequest.response.config.headers['Authorization'] = 'Bearer ' + newToken; return Promise.resolve(); }); registerAxiosTokenRefresh(api, { refreshRequest: refreshAuth }); api.get('/protected', { headers: { Authorization: 'Bearer ' + localStorage.getItem('accessToken') } }).then(res => console.log(res.data)).catch(err => console.error(err));
Debug
Known issues
gotchaThe default status code is 401; requests failing with other statuses won't trigger refresh unless specified.
fix
Pass `statusCodes: [401, 403]` in config if needed.
affects: all
gotchaThe refresh function must return a Promise that resolves; otherwise the interceptor may hang.
fix
Ensure `refreshRequest` returns `Promise.resolve()` or a promise from axios.post.
affects: all
breakingBun engine requirement >=1.2.12; using older Bun may fail.
fix
Upgrade Bun to >=1.2.12 or use Node.js with appropriate polyfills.
affects: all
gotchaLibrary modifies the original request config headers; concurrent requests may interfere.
fix
Clone request config if shared across requests.
affects: all
Errors
Common errors & fixes
TypeError: Cannot read properties of undefined (reading 'config')
The failedRequest object may not have a response property (e.g., network error).
fix
Wrap refresh logic: if (!failedRequest.response) return Promise.reject(); then access config.
Error: registerAxiosTokenRefresh is not a function
Using named import instead of default import.
fix
Use `import registerAxiosTokenRefresh from 'axios-token-refresh'` (no braces).
Error: Module not found: Can't resolve 'axios'
Axios not installed or wrong version.
fix
Run `npm install axios@>=0.21.1`.
Upgrade
Version history
1.0.10latest on npm
Audit
Dependencies
axiosrequiredpeer dependency: interceptor works on Axios instances
Agent activity
20 hits · last 30 days
node
18
Amazon
1
Resources
axios-token-refresh — npm install axios-token-refresh · libregistry