介紹
axios-retry 對外導出 axiosRetry() 方法: 通過對 axios 單例添加“攔截器”,來擴展實現(xiàn)自動重試網(wǎng)絡請求功能。
安裝
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
被設置, 這個插件
將請求超時解釋為全局值, 不是針對每一個請求,二是全局的設置
配置
Name | Type | Default | Description |
---|---|---|---|
retries | Number |
3 |
The number of times to retry before failing. |
retryCondition | Function |
isNetworkOrIdempotentRequestError |
如果應該重試請求,則進一步控制的回調(diào)。默認情況下,如果是冪等請求的網(wǎng)絡錯誤或5xx錯誤,它會重試(GET, HEAD, OPTIONS, PUT or DELETE). |
shouldResetTimeout | Boolean |
false | Defines if the timeout should be reset between retries |
retryDelay | Function |
function noDelay() { return 0; } |
控制重試請求之間的延遲。默認情況下,重試之間沒有延遲。另一個選項是exponentialDelay (Exponential Backoff). The function is passed?retryCount ?and?error . |
測試
克隆這個倉庫 然后 執(zhí)行:
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
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). 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. By default nothing will occur. The function is passed?retryCount ,?error , and?requestConfig . |
Testing
Clone the repository and execute:文章來源:http://www.zghlxwxcb.cn/news/detail-496158.html
npm test
?文章來源地址http://www.zghlxwxcb.cn/news/detail-496158.html
到了這里,關于axios-retry插件-axios請求失敗自動重試的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!