axios-retry插件-axios请求失败自动重试

axios-retry是一个用于Axios的插件,能自动对失败的网络请求进行重试。它可以添加拦截器到Axios实例,支持配置重试次数、重试延迟策略(包括指数级退避)以及特定请求的配置。该模块可通过npm安装,并适用于CommonJS和ES6模块系统。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

介绍

axios-retry 对外导出 axiosRetry() 方法: 通过对 axios 单例添加“拦截器”,来扩展实现自动重试网络请求功能。

安装

npm install axios-retry

使用

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

// 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});

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

// 自定义 axios 实例
const client = axios.create({ baseURL: 'http://example.com' });
axiosRetry(client, { retries: 3 });

client.get('/test') // 第一次请求失败,第二次成功
  .then(result => {
    result.data; // 'ok'
  });

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

备注: 除非 shouldResetTimeout被设置, 这个插件
将请求超时解释为全局值, 不是针对每一个请求,二是全局的设置

配置

NameTypeDefaultDescription
retriesNumber3The number of times to retry before failing.
retryConditionFunctionisNetworkOrIdempotentRequestError如果应该重试请求,则进一步控制的回调。默认情况下,如果是幂等请求的网络错误或5xx错误,它会重试(GET, HEAD, OPTIONS, PUT or DELETE).
shouldResetTimeoutBooleanfalseDefines if the timeout should be reset between retries
retryDelayFunctionfunction noDelay() { return 0; }控制重试请求之间的延迟。默认情况下,重试之间没有延迟。另一个选项是exponentialDelay (Exponential Backoff). The function is passed retryCount and error.

测试

克隆这个仓库 然后 执行:

npm test

axios-retry

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

Installation

npm install axios-retry

Usage

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

// 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 });

// 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

NameTypeDefaultDescription
retriesNumber3The number of times to retry before failing. 1 = One retry after first failure
retryConditionFunctionisNetworkOrIdempotentRequestErrorA 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).
shouldResetTimeoutBooleanfalseDefines if the timeout should be reset between retries
retryDelayFunctionfunction 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). The function is passed retryCount and error.
onRetryFunctionfunction onRetry(retryCount, error, requestConfig) { return; }A callback to notify when a retry is about to occur. Useful for tracing. By default nothing will occur. The function is passed retryCounterror, and requestConfig.

Testing

Clone the repository and execute:

npm test

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值