axios-retryer
Version:
axios-retryer is an advanced Axios request manager offering intelligent retry logic with token refresh, concurrency control, priority queuing, and a flexible plugin architecture, all built with TypeScript for robust HTTP client integrations.
79 lines (78 loc) • 2.92 kB
TypeScript
import type { AxiosInstance } from 'axios';
import type { RetryManager } from '../../core/RetryManager.ts';
import type { RetryPlugin } from '../../types';
import type { TokenRefreshPluginOptions } from './types';
/**
* A RetryPlugin that manages token refresh on certain status codes (e.g., 401).
* It intercepts failed requests, attempts to refresh the token,
* and re-dispatches any queued requests if refresh succeeds.
*/
export declare class TokenRefreshPlugin implements RetryPlugin {
name: string;
version: string;
private manager;
private interceptorId;
private refreshAxios;
private isRefreshing;
private refreshQueue;
private readonly refreshToken;
private readonly options;
private logger;
constructor(refreshToken: (axiosInst: AxiosInstance) => Promise<{
token: string;
}>, options?: TokenRefreshPluginOptions);
/**
* Called by RetryManager when we register this plugin via manager.use(plugin).
* Attaches a response interceptor to the manager’s axios instance and
* creates a dedicated axios instance for refresh calls.
*/
initialize(manager: RetryManager): void;
/**
* Called when the plugin is removed.
*/
onBeforeDestroyed(manager: RetryManager): void;
/**
* Intercepts a failed response. If the error status is refreshable and the request
* hasn't already been retried, then either queues the request (if refresh is in progress)
* or starts a new refresh cycle.
*/
private handleResponseError;
/**
* Checks if the error status code is in the list of refreshable status codes.
*/
private isRefreshableError;
/**
* Main token refresh flow:
* 1) Set isRefreshing = true.
* 2) Attempt to refresh the token.
* 3) On success, update the auth header and retry both queued and original requests.
* 4) On failure, clear the queue and reject.
*/
private handleTokenRefresh;
/**
* Attempts token refresh up to (maxRefreshAttempts + 1) times if retryOnRefreshFail is true.
* Each attempt is subject to a timeout defined in refreshTimeout.
*/
private executeTokenRefresh;
/**
* Updates the manager's default auth header so subsequent requests automatically carry the new token.
*/
private updateAuthHeader;
/**
* Retries the given request using the refreshAxios instance,
* marking it with __isRetryRefreshRequest to avoid loops.
*/
private retryRequest;
/**
* If a 401 is encountered while a refresh is already in progress, queue the request.
*/
private queueRefreshRequest;
/**
* Once the token is refreshed, re-dispatch all queued requests.
*/
private retryQueuedRequests;
/**
* If the token refresh fails completely, reject all queued requests and emit an event.
*/
private handleRefreshFailure;
}