axios-retry
Axios 插件 重试失败的请求

安装
使用
import axiosRetry from 'axios-retry';
axiosRetry(axios, { retries: 3 });
axios.get('http://example.com/test') .then(result => { result.data; });
axiosRetry(axios, { retryDelay: axiosRetry.exponentialDelay});
axiosRetry(axios, { retryDelay: (retryCount) => { return retryCount * 1000; }});
const client = axios.create({ baseURL: 'http://example.com' }); axiosRetry(client, { retries: 3 });
client.get('/test') .then(result => { result.data; });
client .get('/test', { 'axios-retry': { retries: 0 } }) .catch(error => { error !== undefined });
|
备注: 除非 shouldResetTimeout
被设置, 这个插件
将请求超时解释为全局值, 不是针对每一个请求,二是全局的设置
配置
Name |
Type |
Default |
Description |
retries |
Number |
3 |
The number of times to retry before failing. |
retryCondition |
Function |
isNetworkOrIdempotentRequestError |
如果应该重试请求,则进一步控制的回调。默认情况下,如果是幂等请求的网络错误或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 . |
测试
克隆这个仓库 然后 执行:
贡献
- Fork it:
git clone https://github.com/softonic/axios-retry.git
- Create your feature branch:
git checkout -b feature/my-new-feature
- Commit your changes:
git commit -am 'Added some feature'
- Check the build:
npm run build
- Push to the branch:
git push origin my-new-feature
- Submit a pull request :D