Gathering detailed insights and metrics for @audc/retry-axios
Gathering detailed insights and metrics for @audc/retry-axios
Gathering detailed insights and metrics for @audc/retry-axios
Gathering detailed insights and metrics for @audc/retry-axios
🦖 A super flexible interceptor for Axios that makes it easy to retry requests.
npm install @audc/retry-axios
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
182 Commits
1 Branches
Updated on 13 Oct 2024
TypeScript (100%)
Cumulative downloads
Total Downloads
Last day
475%
69
Compared to previous day
Last week
167.2%
366
Compared to previous week
Last month
39.7%
1,266
Compared to previous month
Last year
0%
4,466
Compared to previous year
1
Use Axios interceptors to automatically retry failed requests. Super flexible. Built in exponential backoff.
1npm install retry-axios
To use this library, import it alongside of axios
:
1// Just import rax and your favorite version of axios 2const rax = require('retry-axios'); 3const axios = require('axios');
Or, if you're using TypeScript / es modules:
1import * as rax from 'retry-axios'; 2import axios from 'axios';
You can attach to the global axios
object, and retry 3 times by default:
1const interceptorId = rax.attach(); 2const res = await axios('https://test.local');
Or you can create your own axios instance to make scoped requests:
1const myAxiosInstance = axios.create(); 2myAxiosInstance.defaults.raxConfig = { 3 instance: myAxiosInstance 4}; 5const interceptorId = rax.attach(myAxiosInstance); 6const res = await myAxiosInstance.get('https://test.local');
You have a lot of options...
1const interceptorId = rax.attach(); 2const res = await axios({ 3 url: 'https://test.local', 4 raxConfig: { 5 // Retry 3 times on requests that return a response (500, etc) before giving up. Defaults to 3. 6 retry: 3, 7 8 // Retry twice on errors that don't return a response (ENOTFOUND, ETIMEDOUT, etc). 9 // 'noResponseRetries' is limited by the 'retry' value. 10 noResponseRetries: 2, 11 12 // Milliseconds to delay at first. Defaults to 100. Only considered when backoffType is 'static' 13 retryDelay: 100, 14 15 // HTTP methods to automatically retry. Defaults to: 16 // ['GET', 'HEAD', 'OPTIONS', 'DELETE', 'PUT'] 17 httpMethodsToRetry: ['GET', 'HEAD', 'OPTIONS', 'DELETE', 'PUT'], 18 19 // The response status codes to retry. Supports a double 20 // array with a list of ranges. Defaults to: 21 // [[100, 199], [429, 429], [500, 599]] 22 statusCodesToRetry: [[100, 199], [429, 429], [500, 599]], 23 24 // If you are using a non static instance of Axios you need 25 // to pass that instance here (const ax = axios.create()) 26 instance: ax, 27 28 // You can set the backoff type. 29 // options are 'exponential' (default), 'static' or 'linear' 30 backoffType: 'exponential', 31 32 // You can detect when a retry is happening, and figure out how many 33 // retry attempts have been made 34 onRetryAttempt: err => { 35 const cfg = rax.getConfig(err); 36 console.log(`Retry attempt #${cfg.currentRetryAttempt}`); 37 } 38 } 39});
If the logic in onRetryAttempt requires to be asynchronous, you can return a promise, then retry will be executed only after the promise is resolved:
1const res = await axios({ 2 url: 'https://test.local', 3 raxConfig: { 4 onRetryAttempt: err => { 5 return new Promise((resolve, reject) => { 6 // call a custom asynchronous function 7 refreshToken(err, function(token, error) { 8 if (!error) { 9 window.localStorage.setItem('token', token); 10 resolve(); 11 } else { 12 reject(); 13 } 14 }); 15 }); 16 } 17 } 18});
Or if you want, you can just decide if it should retry or not:
1const res = await axios({ 2 url: 'https://test.local', 3 raxConfig: { 4 // Override the decision making process on if you should retry 5 shouldRetry: err => { 6 const cfg = rax.getConfig(err); 7 return true; 8 } 9 } 10});
If you want to add custom retry logic without duplicating too much of the built-in logic, rax.shouldRetryRequest
will tell you if a request would normally be retried:
1const res = await axios({ 2 url: 'https://test.local', 3 raxConfig: { 4 // Override the decision making process on if you should retry 5 shouldRetry: err => { 6 const cfg = rax.getConfig(err); 7 if (cfg.currentRetryAttempt >= cfg.retry) return false // ensure max retries is always respected 8 9 // Always retry this status text, regardless of code or request type 10 if (err.response.statusText.includes('Try again')) return true 11 12 // Handle the request based on your other config options, e.g. `statusCodesToRetry` 13 return rax.shouldRetryRequest(err) 14 } 15 } 16});
This library attaches an interceptor
to an axios instance you pass to the API. This way you get to choose which version of axios
you want to run, and you can compose many interceptors on the same request pipeline.
No vulnerabilities found.
No security vulnerabilities found.