UNPKG

query-harbor

Version:

A React Query utility package for efficient data fetching and caching

61 lines (60 loc) 2.3 kB
import { QueryKey } from '@tanstack/react-query'; interface UseGlobalQueryParams { url: string; baseUrl?: string; queryKey: QueryKey; method: "GET" | "POST" | "PUT" | "DELETE"; data?: any; enabled?: boolean; staleTime?: number; refetchOnWindowFocus?: boolean; select?: (data: any) => any; } interface UseGlobalQueryReturn { refetchQuery: () => void; queryData: any; isLoading: boolean; isError: boolean; error: unknown; } /** * Custom hook for handling global queries with React Query. * @param {string} url - The endpoint URL for the query. * @param {QueryKey} queryKey - The unique key for identifying and caching the query. * @param {"GET" | "POST" | "PUT" | "DELETE"} methodType - The HTTP method type. * @param {any} [data] - The data to be sent with the request (for POST, PUT methods). * @param {boolean} [enabled=true] - Whether the query is enabled or disabled. * @param {boolean} [refetchOnWindowFocus] - Refetch on window focus if the data is stale (default: false). * @param {number} [staleTime] - Duration in milliseconds until the data is considered stale (default: 5 minutes). * * * Return type for the `useGlobalQuery` hook. * @return {Function} refetchQuery - Function to invalidate and refetch the query. * @return {any} queryData - The data returned from the query. * @return {boolean} isLoading - Whether the query is currently loading. * @return {boolean} isError - Whether the query encountered an error. * @return {unknown} error - The error object if the query failed. * * @example * // Example usage: * const { queryData, isLoading, isError, error, refetchQuery } = useGlobalQuery({ * url: "/api/data", * queryKey: ["data"], * methodType: "GET", * }); * * if (isLoading) return <div>Loading...</div>; * if (isError) return <div>Error: {error.message}</div>; * * return ( * <div> * <h1>Data:</h1> * <pre>{queryData.map((data) => <p>{data}</p>)}</pre> * <button onClick={refetchQuery}>Refresh Data</button> * </div> * ); * */ export declare const useGlobalQuery: ({ url, baseUrl, queryKey, method, data, enabled, staleTime, refetchOnWindowFocus, select }: UseGlobalQueryParams) => UseGlobalQueryReturn; export {}; //# sourceMappingURL=useGlobalQuery.d.ts.map