Skip to content

Commit 450606a

Browse files
authored
✨ add retry adapter (#67)
1 parent ff97bae commit 450606a

File tree

7 files changed

+170
-7827
lines changed

7 files changed

+170
-7827
lines changed

README.md

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ A non-invasive, simple, reliable collection of axios extension
1414

1515
* [cacheAdapterEnhancer](#cacheadapterenhancer) makes request cacheable
1616
* [throttleAdapterEnhancer](#throttleadapterenhancer) makes request throttled automatically
17+
* [retryAdapterEnhancer](#retryadapterenhancer) makes request retry with special times while it failed
1718

1819
## Installing
1920
```bash
@@ -68,7 +69,7 @@ new webpack.DefinePlugin({
6869

6970
> Makes axios cacheable
7071
71-
```
72+
```typescript
7273
cacheAdapterEnhancer(adapter: AxiosAdapter, options: Options): AxiosAdapter
7374
```
7475

@@ -149,9 +150,9 @@ http.get('/users', { cache: cacheA, forceUpdate: true });
149150

150151
### throttleAdapterEnhancer
151152

152-
> throttle requests most once per threshold milliseconds
153+
> Throttle requests most once per threshold milliseconds
153154
154-
```
155+
```ts
155156
throttleAdapterEnhancer(adapter: AxiosAdapter, options: Options): AxiosAdapter
156157
```
157158

@@ -190,3 +191,36 @@ setTimeout(() => {
190191
http.get('/users'); // after 2s, the real request makes again
191192
}, 2 * 1000);
192193
```
194+
195+
### retryAdatperEnhancer
196+
197+
> Retry the failed request with special times
198+
199+
```ts
200+
retryAdapterEnhancer(adapter: AxiosAdapter, options: Options): AxiosAdapter
201+
```
202+
203+
Where `adapter` is an axios adapter which following the [axios adapter standard](https://github.com/axios/axios/blob/master/lib/adapters/README.md), `options` is an optional that configuring caching:
204+
| Param | Type | Default value | Description |
205+
| ---------------- | ---------------------------------------- | ------------------------------------------------------------ | ---- |
206+
| times | number | 2 | Enables cache for all requests without explicit definition in request config (e.g. `cache: true`) |
207+
208+
#### basic usage
209+
210+
```ts
211+
import axios from 'axios';
212+
import { retryAdapterEnhancer } from 'axios-extensions';
213+
214+
const http = axios.create({
215+
baseURL: '/',
216+
headers: { 'Cache-Control': 'no-cache' },
217+
adapter: retryAdapterEnhancer(axios.defaults.adapter)
218+
});
219+
220+
// this request will retry two times if it failed
221+
http.get('/users');
222+
223+
// you could also set the retry times for a special request
224+
http.get('/special', { retryTimes: 3 });
225+
```
226+

0 commit comments

Comments
 (0)