|
3 | 3 | [](https://david-dm.org/flexdinesh/axios-retry-interceptor) |
4 | 4 | [](https://opensource.org/licenses/MIT) |
5 | 5 |
|
6 | | -**To be updated...** |
| 6 | +Configurable Axios Interceptor to retry failed http calls. |
7 | 7 |
|
| 8 | +## Install |
8 | 9 |
|
9 | | -## Why? [](http://www.ted.com/talks/simon_sinek_how_great_leaders_inspire_action) |
| 10 | +``` |
| 11 | +npm install --save axios-retry-interceptor |
| 12 | +``` |
10 | 13 |
|
11 | | -**To be updated...** |
| 14 | +## Usage |
| 15 | + |
| 16 | +Import |
| 17 | +```js |
| 18 | +import retryInterceptor from 'axios-retry-interceptor'; |
| 19 | +// or |
| 20 | +const retryInterceptor = require('axios-retry-interceptor'); |
| 21 | +``` |
| 22 | + |
| 23 | +Set the interceptor for your axios instance. Voila! ✨ |
| 24 | +```js |
| 25 | +retryInterceptor(axios, { |
| 26 | + maxAttempts: 3, |
| 27 | + waitTime: 1000 |
| 28 | +}); |
| 29 | + |
| 30 | +``` |
| 31 | + |
| 32 | +## API |
| 33 | + |
| 34 | +### retryInterceptor(axiosInstance, options) |
| 35 | + |
| 36 | +- axiosInstance - your axios instance |
| 37 | +- options - config for retry interceptor |
| 38 | + |
| 39 | +### Options |
| 40 | + |
| 41 | +#### maxAttempts |
| 42 | + |
| 43 | +Max number of times the interceptor should retry the failed http call. |
| 44 | + |
| 45 | +_Type_: Number |
| 46 | + |
| 47 | +_Default_: 3 |
| 48 | + |
| 49 | +_Example_: `maxAttempts: 5` |
| 50 | + |
| 51 | +#### waitTime |
| 52 | + |
| 53 | +Duration between each retry attempt in milliseconds(1s=1000ms). |
| 54 | + |
| 55 | +_Type_: Number |
| 56 | + |
| 57 | +_Default_: 0 |
| 58 | + |
| 59 | +_Example_: `waitTime: 3000` |
| 60 | + |
| 61 | +#### statuses |
| 62 | + |
| 63 | +Response statuses for which the interceptor should retry. |
| 64 | + |
| 65 | +Ideally any implementation should retry only 5xx status(server errors) and should not retry 4xx status(client errors). The reason is, if a http call fails with a client error, then the retry call will have the same headers/params and will obviously fail. So **by default all 5xx errors will be retried**. If you want to customize the status for which the retries should be made, use this config. |
| 66 | + |
| 67 | +_Type_: Array |
| 68 | + |
| 69 | +_Default_: [] |
| 70 | + |
| 71 | +_Example_: `[500, 501, 401]` |
| 72 | + |
| 73 | +## Author's note |
| 74 | + |
| 75 | +Ideally only idempotent http methods (GET, PUT, DELETE, HEAD, OPTIONS) should be retried on failed http calls. Non-idempotent methods like POST should **NOT** be retried and that's why this library will not permit retry of non-idempotent methods. |
12 | 76 |
|
13 | 77 | ## License |
14 | 78 |
|
|
0 commit comments