@mrsamdev/axios-token-refresh
axios token refresh plugin with queued retries and clean interceptor lifecycle
why
When access tokens expire, requests fail at once and apps often re-run refresh logic multiple times. This package centralizes token refresh, queues failed requests, and replays them after refresh succeeds.
overview
Lightweight TypeScript utility for Axios. Includes configurable refresh strategy, request deduping, automatic auth header injection, timeout handling, and a small
tryCatch helper.
what it does
- Refreshes token on auth failures — customizable refresh condition
- Queues and replays pending requests — prevents duplicate refresh storms
- Supports request deduping while refresh is in progress
- Auto-injects Authorization header — can be disabled
install
pnpm add @mrsamdev/axios-token-refresh
usage
import axios from "axios";
import { createRefreshTokenPlugin } from "@mrsamdev/axios-token-refresh";
const client = axios.create({ baseURL: "/api" });
const cleanup = createRefreshTokenPlugin({
refreshTokenFn: async () => "new-token",
getAuthToken: () => localStorage.getItem("token"),
})(client);
// call cleanup() when you want to eject interceptors
links