import { QueryCache , QueryClient , MutationCache } from "react-query" ; interface InterceptorManagerUseParams < V > { onFulfilled ? : ( value : V ) => V | Promise < V > ; onRejected ? : ( error : any ) => any ; } export interface IInterceptors < C , R > { request ? : InterceptorManagerUseParams < C > [ ] ; response ? : InterceptorManagerUseParams < R > [ ] ; } export interface IClients < I = ClientInstance > { [ id ] : I ; } export type ClientFactory < C extends ClientConfig = any , R = any , I extends ClientInstance = any > = ( config ? : C , interceptors ? : IInterceptors < C , R > ) => I ; export interface ILink < I extends ClientInstance = any , C extends ClientConfig = any , R = any > { id : string ; config ? : C ; interceptors ? : IInterceptors < C , R > ; clientFactory ? : ClientFactory < C , R , I > ; } export type DataTransformer = ( data : any ) => any ; export interface ClientConfig { baseURL ? : string ; params ? : any ; } export interface ClientInterceptorManager < V > { use ( onFulfilled ? , onRejected ? ) : number ; eject ( id ) : void ; } export interface ClientInstance { interceptors ? : { request ? : ClientInterceptorManager < ClientConfig > ; response ? : ClientInterceptorManager < any > ; } ; get < R = any > ( url , config ? ) : Promise < R > ; delete < R = any > ( url , config ? ) : Promise < R > ; post < R = any > ( url , data ? , config ? ) : Promise < R > ; put < R = any > ( url , data ? , config ? ) : Promise < R > ; patch < R = any > ( url , data ? , config ? ) : Promise < R > ; } let queryClient ; export function getClients ( links , clientFactory ? ) { return links . reduce ( ( clients , link ) => { const linkClientFactory = link . clientFactory || clientFactory ; if ( ! linkClientFactory ) { throw new Error ( "A link client factory must be defined!" ) ; } clients [ link . id ] = linkClientFactory ( link . config , link . interceptors ) ; return clients ; } , { } ) ; } export function getMoonStore ( store ? ) { if ( store ) { queryClient = store ; return store ; } if ( queryClient ) { return queryClient ; } const queryCache = new QueryCache ( ) ; const mutationCache = new MutationCache ( ) ; queryClient = new QueryClient ( { queryCache , mutationCache , defaultOptions : { queries : { refetchOnWindowFocus : false } } } ) ; return queryClient ; }