export * from 'react-query' ; import type { AbortSignal } from 'abort-controller' ; import { QueryObserverResult , useMutation , useQuery } from 'react-query' ; import type { FunctionMutationOptions , FunctionQueryOptions , FunctionReturnType , NonContextFunctionArgs , ServerFunction , } from './types' ; export function useQueryFunctionBrowser < T extends ServerFunction > ( fn , args , options ? ) { return useQuery ( [ fn . name , args ] , ( ctx ) => { const abortController = new AbortController ( ) as import ( 'abort-controller' ) . AbortController ; const resultPromise = invokeViaHttp ( { functionName : ctx . queryKey [ 0 ] , args : ctx . queryKey [ 1 ] } , abortController . signal ) ; return Object . assign ( resultPromise , { cancel ( ) { abortController . abort ( ) ; } , } ) ; } , options ) as QueryObserverResult < FunctionReturnType < T > > ; } export function createFunctionMutationBrowser < T extends ServerFunction > ( fn , factoryOptions = { } ) { return function useBoundFunctionMutation ( options ) { return useMutationFunctionBrowser ( fn , Object . assign ( Object . create ( null ) , factoryOptions , options ) ) ; } ; } export function useMutationFunctionBrowser < T extends ServerFunction > ( fn , options = { } ) { return useMutation < FunctionReturnType < T > , unknown , NonContextFunctionArgs < T > > ( ( args ) => { return invokeViaHttp < FunctionReturnType < T > > ( { functionName : fn . name , args } ) ; } , options ) ; } async function invokeViaHttp < T = unknown > ( batch , signal ? ) { const res = await fetch ( process . env . NOSTALGIE_RPC_PATH ! , { method : 'POST' , headers : { 'content-type' : 'application/json' , } , body : JSON . stringify ( batch ) , signal , } ) ; if ( ! res . ok ) { throw new Error ( `Request failed` ) ; } return res . json ( ) ; }