Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

axios-retry

softonic12mApache-2.04.5.0TypeScript support: included

Axios plugin that intercepts failed requests and retries them whenever posible.

readme

axios-retry

Node.js CI

Axios plugin that intercepts failed requests and retries them whenever possible.

Installation

npm install axios-retry

Usage

// CommonJS
// const axiosRetry = require('axios-retry').default;

// ES6
import axiosRetry from 'axios-retry';

axiosRetry(axios, { retries: 3 });

axios.get('http://example.com/test') // The first request fails and the second returns 'ok'
  .then(result => {
    result.data; // 'ok'
  });

// Exponential back-off retry delay between requests
axiosRetry(axios, { retryDelay: axiosRetry.exponentialDelay });

// Liner retry delay between requests
axiosRetry(axios, { retryDelay: axiosRetry.linearDelay() });

// Custom retry delay
axiosRetry(axios, { retryDelay: (retryCount) => {
  return retryCount * 1000;
}});

// Works with custom axios instances
const client = axios.create({ baseURL: 'http://example.com' });
axiosRetry(client, { retries: 3 });

client.get('/test') // The first request fails and the second returns 'ok'
  .then(result => {
    result.data; // 'ok'
  });

// Allows request-specific configuration
client
  .get('/test', {
    'axios-retry': {
      retries: 0
    }
  })
  .catch(error => { // The first request fails
    error !== undefined
  });

Note: Unless shouldResetTimeout is set, the plugin interprets the request timeout as a global value, so it is not used for each retry but for the whole request lifecycle.

Options

Name Type Default Description
retries Number 3 The number of times to retry before failing. 1 = One retry after first failure
retryCondition Function isNetworkOrIdempotentRequestError A callback to further control if a request should be retried. By default, it retries if it is a network error or a 5xx error on an idempotent request (GET, HEAD, OPTIONS, PUT or DELETE).
shouldResetTimeout Boolean false Defines if the timeout should be reset between retries
retryDelay Function function noDelay() { return 0; } A callback to further control the delay in milliseconds between retried requests. By default there is no delay between retries. Another option is exponentialDelay (Exponential Backoff) or linearDelay. The function is passed retryCount and error.
onRetry Function function onRetry(retryCount, error, requestConfig) { return; } A callback to notify when a retry is about to occur. Useful for tracing and you can any async process for example refresh a token on 401. By default nothing will occur. The function is passed retryCount, error, and requestConfig.
onMaxRetryTimesExceeded Function function onMaxRetryTimesExceeded(error, retryCount) { return; } After all the retries are failed, this callback will be called with the last error before throwing the error.
validateResponse Function | null null A callback to define whether a response should be resolved or rejected. If null is passed, it will fallback to the axios default (only 2xx status codes are resolved).

Testing

Clone the repository and execute:

npm test

Contribute

  1. Fork it: git clone https://github.com/softonic/axios-retry.git
  2. Create your feature branch: git checkout -b feature/my-new-feature
  3. Commit your changes: git commit -am 'Added some feature'
  4. Check the build: npm run build
  5. Push to the branch: git push origin my-new-feature
  6. Submit a pull request :D

changelog

Change Log

All notable changes to this project will be documented in this file.

The format is based on Keep a Changelog and this project adheres to Semantic Versioning.

[4.5.0] - 2024-08-02

  • added linearDelay function

[4.4.2] - 2024-07-22

  • fix: last request time not updated correctly

[4.4.1] - 2024-06-21

  • fix: clear abort listener

[4.4.0] - 2024-06-04

  • honor client abort signal

[4.3.0] - 2024-05-20

  • added validateResponse callback to retry on custom conditions

[4.2.0] - 2024-05-15

  • retry on 429 and handle retry-after header

[4.1.0] - 2024-03-19

  • added onMaxRetryTimesExceeded

[4.0.0] - 2023-11-25

  • Implementation migrated to Typescript
  • Updated all deps
  • Import from CommonJS procedure updated to const axiosRetry = require('axios-retry').default

[3.9.1] - 2023-11-16

  • Revert: Export of type definitions, depending on whether it is ES Module or CommonJS

[3.9.0] - 2023-11-14

  • Export of type definitions, depending on whether it is ES Module or CommonJS

[3.8.2] - 2023-11-14

  • Expose axios-retry options in error object

[3.8.1] - 2023-10-26

  • onRetry support for async function

[3.8.0] - 2023-09-21

  • Don't retry cancelled requests by default

[3.7.0] - 2023-08-28

  • Changed type definitions to support both CommonJS and ESM

[3.6.1] - 2023-08-25

  • introduced IAxiosRetryReturn in type definition

[3.6.0] - 2023-07-28

  • allow to eject interceptors

[3.5.1] - 2023-06-27

  • fix exponentialDelay type definition

[3.5.0] - 2023-05-18

  • exponentialDelay now supports delayFactor as a param

[3.4.0] - 2023-01-20

  • Updated axios dep to 1.2.3
  • Update package-lock to version 2
  • Prevent the whole request lifecycle from being longer than timeout

[3.3.1] - 2022-06-29

  • Security updates

[3.3.0] - 2022-06-28

  • Added onRetry callback option

[3.2.6] - 2022-06-28

  • Add types export to package.json

[3.2.5] - 2022-04-29

  • handle retryCondition false return value

[3.2.4] - 2021-10-27

  • fix: add package.json to exports

[3.2.3] - 2021-10-19

  • fix: removed breaking requirements introduced in 3.2.1
  • fix: allow Typescript CommonJS default import

[3.2.2] - 2021-10-14

  • fix: added missing @babel/runtime runtime dep

[3.2.1] - 2021-10-14

  • updated all dependencies
  • made the package hybrid (ES modules and CommonJS)
  • BREAKING: axios >=0.21.2 is now required to work as a peer dep.
  • BREAKING: NodeJS "^12.20.0 || ^14.13.1 || >=16.0.0" to work

[3.2.0] - 2021-09-28

Added

  • Retry condition accepts a function that returns a Promise resolving to a boolean

[3.1.9] - 2020-09-18

Fixed

  • TS: extended AxiosRequestConfig with optional "axios-retry" property

[3.1.8] - 2019-04-24

Fixed

  • TS: export types for all functions

[3.1.7] - 2019-04-23

Fixed

  • Fixed default export to resolve TS2309
  • Updated dependencies to fix build error
  • Added missing CHANGELOG entries (versions 3.0.1 until today)

[3.1.6] - 2019-04-21

Fixed

  • Fixed export of IAxiosRetryConfig

[3.1.5] - 2019-04-20

Fixed

  • Fixed TS definitions

[3.1.4] - 2019-04-18

Fixed

  • Fixed TS definitions

Added

  • Added note in README related to compatibility issue with axios 0.19.0
  • Updated LICENSE

[3.1.2] - 2019-01-24

Added

  • TravisCI build status badge in README
  • Update index.d.ts with missing functions

[3.1.1] - 2018-06-13

Fixed

  • Do not run requestTransform again after retry

Added

  • Explicit return type on axiosRetry
  • Prettier

[3.1.0] - 2018-04-26

Fixed

  • Export isRetryableError for CommonJS

Added

  • Added additional param shouldResetTimeout

[3.0.2] - 2018-02-09

Added

  • Now isRetryableError method is accessible.
  • Added delayStrategy option to be able to have exponential backoff for successive retries.

[3.0.1] - 2017-08-16

Fixed

  • Fixed first request time not being taken into account in timeout across retries.
  • Fixed negative timeouts being passed to XHR (browsers), causing that no timeout was applied.
  • Fixed safe methods and idempotent errors not being retried on unknown network errors.

[3.0.0] - 2017-08-13

Changed

  • Retried errors on idempotent requests (5xx with get, head, options, put and delete) by default, along with safe network errors.
  • Moved some hard-coded conditions to the default retryCondition function so users can define a custom function that overwrites them. The conditions that verify that the error is not a timeout or an unsafe network error have been moved to isNetworkError.

Added

  • Added additional pre-defined retry conditions: isSafeRequestError, isIdempotentRequestError.

[2.0.1] - 2017-06-19

Fixed

  • Removed dependency from the package.json file.

[2.0.0] - 2017-06-15

Changed

  • Now the configured timeout in Axios is not for each retry request but for the whole request lifecycle.

[1.3.1] - 2017-06-19

Fixed

  • Removed dependency from the package.json file.

[1.3.0] - 2017-06-15

Added

  • Allowed per-request configuration using the axios-retry namespace.