Skip to content

Commit b3437f4

Browse files
authored
🏷️ move typings (#69)
1 parent 38e65b6 commit b3437f4

File tree

5 files changed

+32
-17
lines changed

5 files changed

+32
-17
lines changed

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,20 @@ http.get('/users', { useCache: true }); // make the request cacheable(real http
116116
http.get('/users', { useCache: true }); // use the response cache from previous request
117117
```
118118

119+
##### custom cache typing
120+
121+
Note that if you are using custom cache flag and typescript, you may need to add the typing declaration like below:
122+
123+
```ts
124+
import { ICacheLike } from 'axios-extensions';
125+
declare module 'axios' {
126+
interface AxiosRequestConfig {
127+
// if your cacheFlag was setting to 'useCache'
128+
useCache?: boolean | ICacheLike<any>;
129+
}
130+
}
131+
```
132+
119133
#### more advanced
120134

121135
Besides configuring the request through the `cacheAdapterEnhancer`, we can enjoy more advanced features via configuring every individual request.

src/cacheAdapterEnhancer.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,13 @@ import LRUCache from 'lru-cache';
99
import buildSortedURL from './utils/buildSortedURL';
1010
import isCacheLike from './utils/isCacheLike';
1111

12+
declare module 'axios' {
13+
interface AxiosRequestConfig {
14+
forceUpdate?: boolean;
15+
cache?: boolean | ICacheLike<any>;
16+
}
17+
}
18+
1219
const FIVE_MINUTES = 1000 * 60 * 5;
1320
const CAPACITY = 100;
1421

@@ -37,7 +44,9 @@ export default function cacheAdapterEnhancer(adapter: AxiosAdapter, options: Opt
3744
return config => {
3845

3946
const { url, method, params, paramsSerializer, forceUpdate } = config;
40-
const useCache = (config[cacheFlag] !== void 0 && config[cacheFlag] !== null) ? config[cacheFlag] : enabledByDefault;
47+
const useCache = ((config as any)[cacheFlag] !== void 0 && (config as any)[cacheFlag] !== null)
48+
? (config as any)[cacheFlag]
49+
: enabledByDefault;
4150

4251
if (method === 'get' && useCache) {
4352

src/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@
55
*/
66

77
import Cache from 'lru-cache';
8-
import cacheAdapterEnhancer from './cacheAdapterEnhancer';
8+
import cacheAdapterEnhancer, { ICacheLike } from './cacheAdapterEnhancer';
99
import retryAdapterEnhancer from './retryAdapterEnhancer';
1010
import throttleAdapterEnhancer from './throttleAdapterEnhancer';
1111

1212
export {
1313
Cache,
14+
ICacheLike,
1415
cacheAdapterEnhancer,
1516
throttleAdapterEnhancer,
1617
retryAdapterEnhancer,

src/retryAdapterEnhancer.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@
55

66
import { AxiosAdapter, AxiosResponse } from 'axios';
77

8+
declare module 'axios' {
9+
interface AxiosRequestConfig {
10+
retryTimes?: number;
11+
}
12+
}
13+
814
export type Options = {
915
times?: number;
1016
};

src/typings.d.ts

Lines changed: 0 additions & 15 deletions
This file was deleted.

0 commit comments

Comments
 (0)