/** * Query Hook Factory * Factory function for creating typed React Query hooks for GET requests */ import type { ApiQueryOptions, QueryKey, ServiceOptions, UseQueryResult } from '@plyaz/types/api'; import type { EndpointsList } from '../../endpoints'; import type { FetchResponse } from 'fetchff'; import type { ClientEventManager } from '@/api/client'; /** * Factory for creating typed query hooks (GET requests) * Wraps service functions with React Query's useQuery hook * * The hook accepts the same parameters as the service function, plus React Query options. * This ensures hooks can be called just like fetchers with full override capabilities. * * @template TQueryFnData - The type of data returned by queryFn (defaults to TData) * @template TError - The type of error (defaults to Error) * @template TData - The type of data after selection/transformation (defaults to TQueryFnData) * @template TQueryKey - The type of query key (defaults to QueryKey) * @template TParams - The type of parameters passed to the service function * @template TEndpoints - The type of endpoints configuration * * @param serviceFn - The service function to wrap (e.g., fetchCampaigns) * @param defaultOptions - Default React Query options (staleTime, gcTime, etc.) * @returns A hook function that can be used in React components * * @example * ```typescript * // Create a hook from a service function * export function useCampaigns( * queryKey: QueryKey, * filters?: CampaignFilters, * serviceOptions?: ServiceOptions, * queryOptions?: UseQueryOptions * ) { * return createApiQuery( * fetchCampaigns, * { staleTime: cacheStrategies.standard.ttl * 1000 } * )(queryKey, filters, serviceOptions, queryOptions); * } * * // Use in a component - just like calling the fetcher! * function CampaignList() { * const { data, isLoading } = useCampaigns( * ['campaigns', 'list'], * { status: 'active' }, // query params * { apiConfig: { timeout: 5000 } }, // service options override * { enabled: true } // react query options override * ); * return