axios-retry vs axios-cache-adapter vs axios-extensions
Axios Enhancements for HTTP Requests Comparison
1 Year
axios-retryaxios-cache-adapteraxios-extensionsSimilar Packages:
What's Axios Enhancements for HTTP Requests?

These npm packages enhance the functionality of Axios, a popular promise-based HTTP client for the browser and Node.js. Each package serves a specific purpose: 'axios-cache-adapter' adds caching capabilities to reduce redundant network requests, 'axios-extensions' provides additional features like request/response transformations and caching, while 'axios-retry' introduces automatic request retries for failed requests. Together, they improve performance, reliability, and efficiency when making HTTP requests in web applications.

Package Weekly Downloads Trend
Github Stars Ranking
Stat Detail
Package
Downloads
Stars
Size
Issues
Publish
License
axios-retry4,057,7961,95833.6 kB549 months agoApache-2.0
axios-cache-adapter71,264725-594 years agoMIT
axios-extensions19,659837395 kB19-MIT
Feature Comparison: axios-retry vs axios-cache-adapter vs axios-extensions

Caching Mechanism

  • axios-retry:

    axios-retry does not provide caching capabilities; its focus is on retrying failed requests. It ensures that if a request fails due to network issues, it can automatically retry the request based on predefined conditions, but it does not cache responses.

  • axios-cache-adapter:

    axios-cache-adapter provides a flexible caching mechanism that allows you to store responses in memory or local storage, reducing the number of network requests. It supports various caching strategies, such as time-based expiration and cache invalidation, making it suitable for applications that frequently request the same resources.

  • axios-extensions:

    axios-extensions offers basic caching capabilities, but it is more focused on enhancing request and response handling rather than providing a comprehensive caching solution. It allows for simple caching of responses but lacks the advanced features of axios-cache-adapter.

Error Handling

  • axios-retry:

    axios-retry excels in error handling by automatically retrying failed requests based on customizable conditions. It allows developers to specify how many times to retry, the delay between retries, and which error types should trigger a retry, making it a robust solution for unreliable networks.

  • axios-cache-adapter:

    While axios-cache-adapter does not directly handle errors, it can improve user experience by serving cached responses when the network is down, thus preventing errors from affecting the application. However, it does not provide built-in retry logic for failed requests.

  • axios-extensions:

    axios-extensions does not include specific error handling features, but it allows for custom response transformations that can be used to handle errors more gracefully. Developers can define how to handle specific error responses before they reach the application logic.

Configuration Flexibility

  • axios-retry:

    axios-retry allows for straightforward configuration of retry logic, including the number of retries and delay intervals. It is easy to integrate into existing Axios setups and provides sensible defaults, making it user-friendly for developers looking to add retry capabilities.

  • axios-cache-adapter:

    axios-cache-adapter offers extensive configuration options, allowing developers to customize cache behavior, expiration times, and storage methods. This flexibility makes it easy to adapt the caching strategy to fit specific application needs and performance goals.

  • axios-extensions:

    axios-extensions provides a simpler configuration process, focusing on enhancing existing Axios functionality without overwhelming the developer with options. It is designed for ease of use, making it a good choice for those who want to quickly add features without deep customization.

Performance Impact

  • axios-retry:

    axios-retry can improve user experience by reducing the number of failed requests, but it may introduce delays due to retry attempts. However, the overall performance impact is positive, as it helps ensure that requests succeed even in unreliable network conditions.

  • axios-cache-adapter:

    By caching responses, axios-cache-adapter significantly reduces the number of network requests, leading to improved performance and faster load times. This is particularly beneficial for applications with high traffic or frequent data requests, as it minimizes latency and server load.

  • axios-extensions:

    axios-extensions can enhance performance by allowing for efficient request/response transformations, but its impact on performance is less pronounced compared to dedicated caching solutions. It is best used in conjunction with other packages for optimal performance.

Use Case Scenarios

  • axios-retry:

    Perfect for applications that rely on network requests in unstable environments, such as mobile apps or IoT devices. It ensures that critical requests are retried automatically, improving reliability and user satisfaction.

  • axios-cache-adapter:

    Ideal for applications that frequently fetch the same data, such as dashboards or news feeds, where caching can significantly reduce load times and improve user experience. It is also useful for mobile applications with intermittent connectivity.

  • axios-extensions:

    Best suited for applications that require additional request/response handling features without the need for a full caching solution. It works well in scenarios where developers want to enhance Axios with minimal overhead.

How to Choose: axios-retry vs axios-cache-adapter vs axios-extensions
  • axios-retry:

    Opt for axios-retry if your application needs robust error handling for network requests. This package is essential for scenarios where network reliability is a concern, as it automatically retries failed requests based on customizable conditions.

  • axios-cache-adapter:

    Choose axios-cache-adapter if you need to implement caching for your HTTP requests to optimize performance and reduce server load. It is particularly useful for applications that make frequent requests for the same data, allowing you to serve cached responses instead of hitting the server every time.

  • axios-extensions:

    Select axios-extensions if you require additional features such as request/response transformation or caching without the complexity of managing a separate cache. It is ideal for developers looking for a lightweight solution that enhances Axios with minimal configuration.

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