axios-retry vs fetch-retry vs node-fetch-retry
HTTP Request Retry Libraries Comparison
1 Year
axios-retryfetch-retrynode-fetch-retrySimilar Packages:
What's HTTP Request Retry Libraries?

These libraries provide functionality to automatically retry failed HTTP requests, which is essential for improving the resilience of applications that rely on external APIs. They help manage transient errors, such as network issues or server timeouts, by implementing retry logic based on configurable parameters. This ensures that applications can handle temporary failures gracefully, enhancing user experience and reliability.

Package Weekly Downloads Trend
Github Stars Ranking
Stat Detail
Package
Downloads
Stars
Size
Issues
Publish
License
axios-retry4,085,8241,95833.6 kB549 months agoApache-2.0
fetch-retry3,878,16131355.2 kB4a year agoMIT
node-fetch-retry93,17997.94 kB2-MIT
Feature Comparison: axios-retry vs fetch-retry vs node-fetch-retry

Integration

  • axios-retry:

    axios-retry is designed specifically for Axios, making it easy to implement retry logic without altering your existing Axios configuration. It supports all Axios features, including interceptors, which allows for a smooth integration into your application.

  • fetch-retry:

    fetch-retry works directly with the Fetch API, providing a straightforward way to add retry logic. It does not require any additional libraries, making it a lightweight choice for projects that use the Fetch API natively.

  • node-fetch-retry:

    node-fetch-retry integrates with node-fetch, ensuring that retry logic is applied seamlessly in server-side applications. It is built to work with the fetch API in Node.js, maintaining consistency across client and server requests.

Configuration Options

  • axios-retry:

    axios-retry offers extensive configuration options, allowing you to customize the retry behavior, such as the number of retries, retry delay, and conditions for retrying requests. This flexibility enables developers to fine-tune the retry logic to suit specific application needs.

  • fetch-retry:

    fetch-retry provides basic configuration options for setting the number of retries and delay between attempts. However, it may not offer as many customization options as axios-retry, making it simpler but less flexible for complex scenarios.

  • node-fetch-retry:

    node-fetch-retry allows for similar configuration options as fetch-retry, enabling customization of retry attempts and delays. It is designed to be straightforward, focusing on essential retry logic without overwhelming the developer with options.

Error Handling

  • axios-retry:

    axios-retry provides built-in support for handling specific HTTP status codes, allowing you to define which errors should trigger a retry. This feature is crucial for managing different types of failures effectively, such as network errors versus server errors.

  • fetch-retry:

    fetch-retry allows you to specify which errors should be retried, but it may require additional handling for specific HTTP status codes. Developers need to implement custom logic to manage different error scenarios, which can add complexity.

  • node-fetch-retry:

    node-fetch-retry offers similar error handling capabilities as fetch-retry, enabling you to define retry conditions based on error types. This ensures that your application can intelligently decide when to retry requests based on the nature of the failure.

Use Cases

  • axios-retry:

    axios-retry is ideal for applications that make frequent API calls and need robust error handling, such as web applications that rely on third-party services. Its deep integration with Axios makes it a go-to choice for many developers already using this library.

  • fetch-retry:

    fetch-retry is suitable for smaller projects or applications that prioritize simplicity and minimal dependencies. It works well for straightforward use cases where basic retry logic is sufficient without the need for extensive customization.

  • node-fetch-retry:

    node-fetch-retry is specifically tailored for server-side applications using node-fetch, making it a perfect fit for backend services that need to interact with external APIs reliably. It ensures that server-side requests can handle transient errors effectively.

Community and Support

  • axios-retry:

    axios-retry benefits from a large community due to its association with Axios, which is widely used. This means better support, more examples, and a wealth of resources available for developers.

  • fetch-retry:

    fetch-retry has a smaller community compared to axios-retry, but it is still actively maintained. Developers may find fewer resources and examples, but the simplicity of the library makes it easy to implement without extensive documentation.

  • node-fetch-retry:

    node-fetch-retry is also part of the node-fetch ecosystem, which has a decent community. However, being a more niche library, the support may not be as extensive as axios-retry, but it is still reliable for server-side applications.

How to Choose: axios-retry vs fetch-retry vs node-fetch-retry
  • axios-retry:

    Choose axios-retry if you are already using Axios for making HTTP requests in your application. It integrates seamlessly with Axios, allowing you to leverage its features while adding retry capabilities without significant changes to your existing codebase.

  • fetch-retry:

    Select fetch-retry if you are using the native Fetch API and want a lightweight solution to add retry functionality. This package is ideal for projects that prioritize minimal dependencies and want to maintain a clean, native approach to HTTP requests.

  • node-fetch-retry:

    Opt for node-fetch-retry if you are working in a Node.js environment and need a retry mechanism for the node-fetch library. This package is tailored for server-side applications, ensuring that you can handle retries effectively in a Node.js context.

README for axios-retry

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