重试轴
使用 Axios 截取器自动重试失败的请求 。 超级灵活。 以指数回转方式建造.
安装
npm install retry-axios
使用量
要使用此库,请同时导入 axios数字 :
// Just import rax and your favorite version of axios
const rax = require('retry-axios');
const axios = require('axios');
或者,如果您使用 TypeScript / es 模块:
import * as rax from 'retry-axios';
import axios from 'axios';
您可以附加到全球 axios 对象,并默认重试三次:
const interceptorId = rax.attach();
const res = await axios('https://test.local');
或者您可以创建自己的 arxios 实例来提出范围化请求 :
const myAxiosInstance = axios.create();
myAxiosInstance.defaults.raxConfig = {
instance: myAxiosInstance
};
const interceptorId = rax.attach(myAxiosInstance);
const res = await myAxiosInstance.get('https://test.local');
你有很多选择...
const interceptorId = rax.attach();
const res = await axios({
url: 'https://test.local',
raxConfig: {
// Retry 3 times on requests that return a response (500, etc) before giving up. Defaults to 3.
retry: 3,
// Retry twice on errors that don't return a response (ENOTFOUND, ETIMEDOUT, etc).
noResponseRetries: 2,
// Milliseconds to delay at first. Defaults to 100. Only considered when backoffType is 'static'
retryDelay: 100,
// HTTP methods to automatically retry. Defaults to:
// ['GET', 'HEAD', 'OPTIONS', 'DELETE', 'PUT']
httpMethodsToRetry: ['GET', 'HEAD', 'OPTIONS', 'DELETE', 'PUT'],
// The response status codes to retry. Supports a double
// array with a list of ranges. Defaults to:
// [[100, 199], [429, 429], [500, 599]]
statusCodesToRetry: [[100, 199], [429, 429], [500, 599]],
// If you are using a non static instance of Axios you need
// to pass that instance here (const ax = axios.create())
instance: ax,
// You can set the backoff type.
// options are 'exponential' (default), 'static' or 'linear'
backoffType: 'exponential',
// You can detect when a retry is happening, and figure out how many
// retry attempts have been made
onRetryAttempt: err => {
const cfg = rax.getConfig(err);
console.log(`Retry attempt #${cfg.currentRetryAttempt}`);
}
}
});
如果Retry Attrempt上的逻辑需要同步,您可以返回一个承诺,那么重试只有在承诺解决后才会被执行:
const res = await axios({
url: 'https://test.local',
raxConfig: {
onRetryAttempt: err => {
return new Promise((resolve, reject) => {
// call a custom asynchronous function
refreshToken(err, function(token, error) {
if (!error) {
window.localStorage.setItem('token', token);
resolve();
} else {
reject();
}
});
});
}
}
});
或者,如果你想,你可以只是决定 它是否应该重试:
const res = await axios({
url: 'https://test.local',
raxConfig: {
// Override the decision making process on if you should retry
shouldRetry: err => {
const cfg = rax.getConfig(err);
return true;
}
}
});
如果您想要添加自定义重试逻辑而不重复太多内置逻辑, rax.shouldRetryRequest 会告诉你,如果一个请求通常会重审:
const res = await axios({
url: 'https://test.local',
raxConfig: {
// Override the decision making process on if you should retry
shouldRetry: err => {
const cfg = rax.getConfig(err);
if (cfg.currentRetryAttempt >= cfg.retry) return false // ensure max retries is always respected
// Always retry this status text, regardless of code or request type
if (err.response.statusText.includes('Try again')) return true
// Handle the request based on your other config options, e.g. `statusCodesToRetry`
return rax.shouldRetryRequest(err)
}
}
});
如何运作
此库附带一个 interceptor 给一个Axios实例 你通过API。 这样你就可以选择哪个版本 axios 你可以在同一条请求的管道上 制造许多拦截器.