11import * as uuid from 'uuid' ;
2- import { URL } from 'url' ;
32import axios , {
4- AxiosAdapter ,
53 AxiosError ,
4+ AxiosHeaderValue ,
65 AxiosInstance ,
76 AxiosRequestConfig ,
87 AxiosResponse ,
8+ RawAxiosRequestHeaders ,
99} from 'axios' ;
10- import { IAxiosCacheAdapterOptions , setupCache } from 'axios-cache-adapter' ;
1110import * as rax from 'retry-axios' ;
1211import { RetryConfig } from 'retry-axios' ;
1312import md5 from 'md5' ;
1413import { safeJwtCanonicalIdParse , safeJsonParse , serializeAxiosError } from '../util' ;
1514import { InternalException } from '../exceptions/internalException' ;
1615import { ClientException } from '../exceptions/clientException' ;
1716import { orionCorrelationIdRoot } from '../shared' ;
18- import { createDebounceRequestAdapter } from './deduplicateRequestAdapter ' ;
17+ import { CacheOptions , setupCache } from 'axios-cache-interceptor ' ;
1918
2019const invalidToken : string = 'Invalid token' ;
2120
@@ -60,29 +59,22 @@ export default class HttpClient {
6059 this . enableRetry = options ?. enableRetry ?? false ;
6160 this . timeout = options ?. timeout ;
6261 this . clientExceptionStatusCodeMapOverride = options ?. clientExceptionStatusCodeMapOverride ;
63- this . client =
64- options ?. client ??
65- axios . create ( {
66- adapter : ( ( ) => {
67- let adapters = axios . defaults . adapter as AxiosAdapter ;
68- if ( this . enableCache ) {
69- const cache = setupCache ( {
70- maxAge : 5 * 60 * 1000 , // all items are cached for 5 minutes
71- readHeaders : false , // ignore cache control headers in favor of the static 5 minutes
72- readOnError : true ,
73- exclude : {
74- query : false , // also cache requests with query parameters
75- } ,
76- ...options ?. cacheOptions , // allow to overwrite the defaults except of cache-key
77- key : ( req ) => HttpClient . generateCacheKey ( req ) ,
78- } ) ;
79-
80- // debounce concurrent calls with the same cacheKey so that only one HTTP request is made
81- adapters = createDebounceRequestAdapter ( cache . adapter , HttpClient . generateCacheKey ) ;
82- }
83- return adapters ;
84- } ) ( ) ,
62+ this . client = options ?. client ?? axios . create ( ) ;
63+
64+ if ( this . enableCache ) {
65+ setupCache ( this . client , {
66+ // 5 minutes TTL
67+ ttl : 5 * 60 * 1000 ,
68+ // Respect cache-control headers and have ttl as a fallback https://axios-cache-interceptor.js.org/config/request-specifics#cache-interpretheader
69+ interpretHeader : true ,
70+ // Serve stale cache on error
71+ staleIfError : true ,
72+ // Use custom cache key to include auth, url, params and body hash
73+ generateKey : ( req ) => HttpClient . generateCacheKey ( req ) ,
74+ // Allow overriding defaults via provided options
75+ ...options ?. cacheOptions ,
8576 } ) ;
77+ }
8678
8779 if ( this . enableRetry ) {
8880 this . client . defaults . raxConfig = {
@@ -224,15 +216,15 @@ export default class HttpClient {
224216 * Resolves the token with the token provider and adds it to the headers
225217 */
226218 async createHeadersWithResolvedToken (
227- headers : Record < string , string > = { } ,
228- ) : Promise < Record < string , string > > {
229- const newHeaders : Record < string , string > = { } ;
219+ headers ?: RawAxiosRequestHeaders | { [ key : string ] : AxiosHeaderValue } | Record < string , string > ,
220+ ) : Promise < { [ p : string ] : AxiosHeaderValue } > {
221+ const newHeaders : { [ key : string ] : AxiosHeaderValue } = { } ;
230222 if ( this . correlationIdResolverFunction ) {
231223 newHeaders [ orionCorrelationIdRoot ] = this . correlationIdResolverFunction ( ) ;
232224 }
233225
234226 if ( this . tokenResolverFunction ) {
235- if ( headers . Authorization ) {
227+ if ( headers && headers . Authorization ) {
236228 throw new InternalException (
237229 'Authorization header already specified, please create a new HttpClient with a different (or without a) tokenResolver' ,
238230 ) ;
@@ -243,7 +235,7 @@ export default class HttpClient {
243235 }
244236
245237 return {
246- ...headers ,
238+ ...( headers as { [ key : string ] : AxiosHeaderValue } ) ,
247239 ...newHeaders ,
248240 } ;
249241 }
@@ -349,10 +341,10 @@ export interface HttpClientOptions {
349341 */
350342 enableCache ?: boolean ;
351343 /**
352- * Cache options
353- * @link https://github.com/RasCarlito/ axios-cache-adapter/blob/master/axios-cache-adapter.d.ts#L26
344+ * Cache options (global defaults for axios-cache-interceptor)
345+ * @link https://axios-cache-interceptor.js.org/config
354346 */
355- cacheOptions ?: IAxiosCacheAdapterOptions ;
347+ cacheOptions ?: CacheOptions ;
356348 /**
357349 * Enable automatic retries
358350 */
0 commit comments