import axios , { AxiosStatic , AxiosInstance , AxiosError } from 'axios' ; import LRU from 'lru-cache' ; export const defaultSettings = { cache : new LRU < string , ReactUseApi . CacheData | any > ( ) , axios : axios as AxiosStatic | AxiosInstance , maxRequests : 100 , useCacheData : true , alwaysUseCache : false , clearLastCacheWhenConfigChanges : true , debug : false , clientCacheVar : '__USE_API_CACHE__' , isSSR : ( ... args ) : boolean | void => typeof window === 'undefined' , renderSSR : ( ... args ) : string => '' , shouldUseApiCache : ( config ? , cacheKey ? ) : boolean | void => true , } ; export const ACTIONS = { REQUEST_START : 'REQUEST_START' , REQUEST_END : 'REQUEST_END' , } ; export const initState = { loading : false , fromCache : false , $cacheKey : '' , } ; export const configure = ( context , isSSR = false ) => { if ( context . isConfigured ) { return context as ReactUseApi . Context ; } const { settings : custom } = context ; const settings = { ... defaultSettings } ; if ( isObject ( custom ) ) { Object . keys ( custom ) . forEach ( ( key ) => { const value = custom [ key ] ; if ( defaultSettings . hasOwnProperty ( key ) && ! isNil ( value ) ) { settings [ key ] = value ; } } ) ; } isSSR = isSSR !== true && isFunction ( settings . isSSR ) ? ! ! settings . isSSR ( ) : isSSR ; Object . assign ( context , { settings , isSSR , isConfigured : true , collection : { ssrConfigs : [ ] , cacheKeys : new Set < string > ( ) , } as ReactUseApi . SSRCollection , clearCache ( ) { settings ?. cache ?. reset ( ) ; } , } ) ; return context as ReactUseApi . Context ; } ; export async function axiosAll ( context , config ) ; export async function axiosAll ( context , config ) ; export async function axiosAll ( context , config ) { const { settings : { axios : client } , } = context ; const isMulti = Array . isArray ( config ) ; const requests = ( ( isMulti ? config : [ config ] ) as ReactUseApi . MultiConfigs ) . map ( ( cfg ) => client ( cfg ) ) ; try { const responses = await Promise . all < ReactUseApi . ApiResponse > ( requests ) ; responses . forEach ( tidyResponse ) ; const response = responses . length === 1 ? responses [ 0 ] : responses ; return response ; } catch ( error ) { const { response } = error as ReactUseApi . CacheData [ 'error' ] ; if ( response ) { tidyResponse ( response ) ; } throw error as AxiosError ; } } export const tidyResponse = ( response ) => { if ( response ) { delete response . config ; delete response . request ; } return response ; } ; export const getResponseData = ( options , state ) => { const { response } = state ; const isMultiApis = Array . isArray ( response ) ; let data = isMultiApis ? ( response as ReactUseApi . ApiResponse [ ] ) . map ( ( each ) => each . data ) : ( response as ReactUseApi . ApiResponse ) . data ; const { handleData } = options ; if ( isFunction ( handleData ) ) { data = handleData ( data , state ) ; } return data ; } ; export const isObject = ( target ) => ! ! target && Object . prototype . toString . call ( target ) === '[object Object]' ; export const isFunction = ( target ) => ! ! target && typeof target === 'function' ; export const isNil = ( value ) => value === undefined || value === null || value === '' ;