import axios , { CancelTokenSource , AxiosRequestConfig , CancelToken as CancelTokenAxios } from 'axios' ; import { CacheConfig , removeCacheableRequestsInProgress } from './cacheHandlers' ; import { getDefaultRequestsConfig } from './defaultReqsConfig' ; export type RequestConfiguration = { authToken ? : string | null ; retries ? : number ; timeout ? : number | null ; cancelToken ? : CancelToken ; cache ? : CacheConfig ; rewriteUrlFunc ? : ( url : string ) => string ; } ; export class CancelToken { protected token : CancelTokenAxios | null = null ; protected source : CancelTokenSource | null = null ; protected cacheKeys : Set < string > = new Set ( ) ; public constructor ( ) { this . source = axios . CancelToken . source ( ) ; this . token = this . source . token ; } public setCancelTokenCacheKey ( cacheKey ) { this . cacheKeys . add ( cacheKey ) ; } public cancel ( ) { if ( this . cacheKeys . size > 0 ) { for ( let cacheKey of this . cacheKeys ) { removeCacheableRequestsInProgress ( cacheKey ) ; } this . cacheKeys . clear ( ) ; } this . source . cancel ( ) ; } public getToken ( ) { return this . token ; } } export const isCancelled = ( err ) : boolean => { return axios . isCancel ( err ) ; } ; export const getAxiosReqParams = ( reqConfig , defaultCache ) : AxiosRequestConfig => { let axiosReqConfig = { cache : defaultCache , } ; const reqConfigWithDefault = { ... getDefaultRequestsConfig ( ) , ... reqConfig , } ; if ( reqConfigWithDefault . cancelToken ) { axiosReqConfig . setCancelTokenCacheKey = ( cacheKey ) : void => reqConfigWithDefault . cancelToken . setCancelTokenCacheKey ( cacheKey ) ; axiosReqConfig . cancelToken = reqConfigWithDefault . cancelToken . getToken ( ) ; } if ( reqConfigWithDefault . retries !== null && reqConfigWithDefault . retries !== undefined ) { axiosReqConfig . retries = reqConfigWithDefault . retries ; } if ( reqConfigWithDefault . cache ) { axiosReqConfig . cache = reqConfigWithDefault . cache ; } if ( reqConfigWithDefault . rewriteUrlFunc ) { axiosReqConfig . rewriteUrlFunc = reqConfigWithDefault . rewriteUrlFunc ; } return axiosReqConfig ; } ;