44 * @since 2017-10-12
55 */
66
7- import { AxiosAdapter , AxiosPromise } from 'axios' ;
7+ import { AxiosAdapter , AxiosPromise , AxiosRequestConfig } from 'axios' ;
88import LRUCache from 'lru-cache' ;
99import buildSortedURL from './utils/buildSortedURL' ;
1010import isCacheLike , { ICacheLike } from './utils/isCacheLike' ;
@@ -23,6 +23,7 @@ export type Options = {
2323 enabledByDefault ?: boolean ,
2424 cacheFlag ?: string ,
2525 defaultCache ?: ICacheLike < AxiosPromise > ,
26+ cacheKeyGenerator ?: ( config : AxiosRequestConfig , defaultCacheKey : string ) => string ,
2627} ;
2728
2829export default function cacheAdapterEnhancer ( adapter : AxiosAdapter , options : Options = { } ) : AxiosAdapter {
@@ -46,7 +47,11 @@ export default function cacheAdapterEnhancer(adapter: AxiosAdapter, options: Opt
4647 const cache : ICacheLike < AxiosPromise > = isCacheLike ( useCache ) ? useCache : defaultCache ;
4748
4849 // build the index according to the url and params
49- const index = buildSortedURL ( url , params , paramsSerializer ) ;
50+ const defaultCacheKey = buildSortedURL ( url , params , paramsSerializer ) ;
51+ // if had provided key generator, then use it to produce custom key
52+ const customCacheKey = options . cacheKeyGenerator && options . cacheKeyGenerator ( config , defaultCacheKey ) ;
53+
54+ const index = customCacheKey || defaultCacheKey ;
5055
5156 let responsePromise = cache . get ( index ) ;
5257
0 commit comments