import type { Dispatcher, ExtractErrorType, CacheValueType, ExtractResponseType, ExtractMutationContextType, LoggerMethods, OptimisticCallbackResult, RequestEventType, RequestInstance, RequestProgressEventType, RequestResponseEventType, ResponseErrorType, ExtractAdapterType, ResponseType, ResponseSuccessType, } from "@hyper-fetch/core"; import type { UseTrackedStateActions } from "helpers"; // Misc export type UseRequestEventsDataMap = { unmount: VoidFunction }; export type UseRequestEventsLifecycleMap = Map; // Props export type UseRequestEventsPropsType = { request: T; dispatcher: Dispatcher>; logger: LoggerMethods; actions: UseTrackedStateActions; setCacheData: (cacheData: CacheValueType, ExtractErrorType>) => void; getIsDataProcessing: (cacheKey: string) => boolean; }; export type UseRequestEventsActionsType = { /** * Callback that allows canceling ongoing requests from the given queryKey. */ abort: () => void; /** * Helper hook listening on success response. Includes `mutationContext` when `setOptimistic` is configured. */ onSuccess: (callback: OnSuccessCallbackType) => void; /** * Helper hook listening on error response. Includes `mutationContext` when `setOptimistic` is configured. */ onError: (callback: OnErrorCallbackType) => void; /** * Helper hook listening on aborting of requests. Includes `mutationContext` when `setOptimistic` is configured. */ onAbort: (callback: OnErrorCallbackType) => void; /** * Helper hook listening on request going into offline awaiting for network connection to be restored. */ onOfflineError: (callback: OnErrorCallbackType) => void; /** * Helper hook listening on any response. Includes `mutationContext` when `setOptimistic` is configured. */ onFinished: (callback: OnFinishedCallbackType) => void; /** * Helper hook listening on request start. */ onRequestStart: (callback: OnStartCallbackType) => void; /** * Helper hook listening on response start(before we receive all data from server). */ onResponseStart: (callback: OnStartCallbackType) => void; /** * Helper hook listening on download progress ETA. */ onDownloadProgress: (callback: OnProgressCallbackType) => void; /** * Helper hook listening on upload progress ETA. */ onUploadProgress: (callback: OnProgressCallbackType) => void; }; // Return export type UseRequestEventsReturnType = [ UseRequestEventsActionsType, { addCacheDataListener: (request: T) => VoidFunction; clearCacheDataListener: VoidFunction; addLifecycleListeners: ( request: T, requestId?: string, optimisticResult?: OptimisticCallbackResult, ) => VoidFunction; removeLifecycleListener: (requestId: string) => void; clearLifecycleListeners: () => void; }, ]; // Lifecycle export type CallbackParameters = { response: Resp; mutationContext: ExtractMutationContextType | undefined; } & Omit, "response">; export type OnSuccessCallbackType = ( params: CallbackParameters, ExtractAdapterType>>, ) => void | Promise; export type OnErrorCallbackType = ( params: CallbackParameters, ExtractAdapterType>>, ) => void | Promise; export type OnFinishedCallbackType = ( params: CallbackParameters< Request, ResponseType, ExtractErrorType, ExtractAdapterType> >, ) => void | Promise; export type OnStartCallbackType = ( data: RequestEventType, ) => void | Promise; export type OnProgressCallbackType = ( data: RequestProgressEventType, ) => void | Promise;