retry-axios
Use Axios interceptors to automatically retry failed requests. Super flexible. Built in exponential backoff.
Installation
npm install retry-axios
Usage
To use this library, import it alongside of axios
:
// Just import rax and your favorite version of axiosconst rax = ;const axios = ;
You can attach to the global axios
object, and retry 3 times by default:
const interceptorId = rax;const res = await ;
Or you can create your own axios instance to make scoped requests:
const myAxiosInstance = axios;myAxiosInstancedefaultsraxConfig = instance: myAxiosInstance;const interceptorId = rax;const res = await myAxiosInstance;
You have a lot of options...
const interceptorId = rax;const res = await ;
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:
const res = await ;
Or if you want, you can just decide if it should retry or not:
const res = await ;
How it works
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.