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

news/2024/12/29 10:26:35/

介绍

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 failserror !== 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 failserror !== 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

 


http://www.ppmy.cn/news/525773.html

相关文章

什么是CMOS与BIOS?又有什么区别?

很多学习半导体技术的人弄不清CMOS与BIOS之间的联系与区别,容易误认为两个表达的是一个东西,其实不然,两者有关联却不相同。 什么是CMOS与BIOS? CMOS又被称作互补金属氧化物半导体,电压控制的一种放大器件&#xff0…

CMOS图像传感器基本原理

概念 CMOS称为互补式金属氧化物半导体(Complementary Metal-Oxide-Semiconductor,缩写作 CMOS),是一种集成电路的设计工艺,可以在硅质晶圆模板上制出NMOS(n-type MOSFET)和PMOS(p-t…

关于CMOS

(以下内容大多来自老师的课件结合了自己的理解) MOS管又称金属-氧化物-半导体-场效应管(Metal-Oxide-Semicondaction),又称绝缘栅型场效应管,从它的结构就能理解这两个名称的由来。 关于N沟道(NPN型)增强型MO…

【硬件】MOS管及CMOS器件介绍

文章目录 1. 什么是MOS管2. NMOS简单应用3. NMOS和PMOS区别4. NMOS和三极管区别5. CMOS芯片和TTL芯片的区别 >>返回总目录<< 1. 什么是MOS管 MOS管是指场效应晶体管&#xff0c;有G(gate 栅极)/D(drain 漏极)/S(source 源极)三个端口&#xff0c;分为PMOS管(P沟道…

CMOS图像传感器基础

一、概述 图像传感器是把光学图像信息转换成电信号的器件。图像传感器是随着电视技术在20世纪30年代发展起来的&#xff0c;早期图像传感器技术的最重要贡献在于建立了扫描&#xff08;Scan&#xff09;的概念&#xff0c;用扫描的方法把二维空间平面上的光电信息离散成行&…

CMOS与CCD的区别是什么?

CMOS与CCD对比 CMOSCCD成像原理光电转换工艺难度工艺简单工艺较为复杂成本高低成本低廉成本较高像素高低相比较低相对较高 ISO感光度差异&#xff1a;由于CMOS每个画素包含了放大器与A/D转换电路(模拟/数字转换电路)&#xff0c;过多的额外设备压缩单一画素的感光区域的表面积&…

CMOS逻辑

CMOS逻辑 一、反相器二、与非门&#xff08;NAND&#xff09;三、逻辑门四、或非门五、复合门六、传输管和传输门七、三态门八、多路开关九、锁存器和触发器 一、反相器 CMOS反相器即为非(NOT)门&#xff0c;是由一个pMOS管和nMOS管串联构成。下图为反相器的电路图(a)和符号图(…

CMOS基本工艺流程

&#xff08;1&#xff09;选择衬底&#xff1a; 晶圆的选择需要考虑三个参数&#xff1a;掺杂类型&#xff08;N或P&#xff09;、电阻率&#xff08;掺杂浓度&#xff09;、晶向。这里选择P型高掺杂的Si晶圆&#xff08;Silicon Substrate P&#xff09;、低掺杂的Si外延层&…