Skip to content

Commit

Permalink
✨ add retry adapter (#67)
Browse files Browse the repository at this point in the history
  • Loading branch information
kuitos authored Feb 18, 2020
1 parent ff97bae commit 450606a
Show file tree
Hide file tree
Showing 7 changed files with 170 additions and 7,827 deletions.
40 changes: 37 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ A non-invasive, simple, reliable collection of axios extension

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

## Installing
```bash
Expand Down Expand Up @@ -68,7 +69,7 @@ new webpack.DefinePlugin({

> Makes axios cacheable
```
```typescript
cacheAdapterEnhancer(adapter: AxiosAdapter, options: Options): AxiosAdapter
```

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

### throttleAdapterEnhancer

> throttle requests most once per threshold milliseconds
> Throttle requests most once per threshold milliseconds
```
```ts
throttleAdapterEnhancer(adapter: AxiosAdapter, options: Options): AxiosAdapter
```

Expand Down Expand Up @@ -190,3 +191,36 @@ setTimeout(() => {
http.get('/users'); // after 2s, the real request makes again
}, 2 * 1000);
```

### retryAdatperEnhancer

> Retry the failed request with special times
```ts
retryAdapterEnhancer(adapter: AxiosAdapter, options: Options): AxiosAdapter
```

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:
| Param | Type | Default value | Description |
| ---------------- | ---------------------------------------- | ------------------------------------------------------------ | ---- |
| times | number | 2 | Enables cache for all requests without explicit definition in request config (e.g. `cache: true`) |

#### basic usage

```ts
import axios from 'axios';
import { retryAdapterEnhancer } from 'axios-extensions';

const http = axios.create({
baseURL: '/',
headers: { 'Cache-Control': 'no-cache' },
adapter: retryAdapterEnhancer(axios.defaults.adapter)
});

// this request will retry two times if it failed
http.get('/users');

// you could also set the retry times for a special request
http.get('/special', { retryTimes: 3 });
```

Loading

0 comments on commit 450606a

Please # to comment.