UNPKG

@duongtrungnguyen/next-helper

Version:
154 lines 9.49 kB
import type { QueryOptions, QueryResult, MutationOptions, MutationResult, InfiniteQueryOptions, InfiniteQueryResult, QueryClient, PaginatedQueryOptions, PaginatedQueryResult } from "./types"; /** * Custom hook to manage data fetching with caching, retry, and refetching capabilities. * * @template TData - The type of the data returned by the query function. * @template TParams - The type of the parameters passed to the query function. * * @param {QueryOptions<TData, TParams>} options - Configuration options for the query. * @param {string} options.queryKey - Unique key to identify the query. * @param {Function} options.queryFn - Function to fetch the data. * @param {boolean} [options.enabled=true] - Whether the query is enabled. * @param {number} [options.staleTime=300000] - Time in milliseconds before the data is considered stale. * @param {number} [options.cacheTime=1800000] - Time in milliseconds before the cached data is garbage collected. * @param {number} [options.retry=3] - Number of retry attempts in case of failure. * @param {Function} [options.retryDelay] - Function to calculate the delay between retry attempts. * @param {Function} [options.onSuccess] - Callback function to be called on successful data fetching. * @param {Function} [options.onError] - Callback function to be called on data fetching error. * @param {Function} [options.onSettled] - Callback function to be called when the query is settled (either success or error). * @param {TData | (() => TData)} [options.initialData] - Initial data to be used before the first fetch. * @param {boolean | number} [options.refetchInterval=false] - Interval in milliseconds to refetch the data. * @param {boolean} [options.refetchOnWindowFocus=true] - Whether to refetch the data when the window gains focus. * @param {boolean} [options.refetchOnMount=true] - Whether to refetch the data when the component mounts. * @param {boolean} [options.refetchOnReconnect=true] - Whether to refetch the data when the browser reconnects. * @param {Function} [options.select] - Function to transform or select a subset of the data. * * @returns {QueryResult<TData>} - The result of the query, including the data, status, and refetch function. */ export declare function useQuery<TData = unknown, TParams = unknown>(options: QueryOptions<TData, TParams>): QueryResult<TData>; /** * Custom hook to handle mutations with retry logic and state management. * * @template TData - The type of the data returned by the mutation. * @template TVariables - The type of the variables passed to the mutation function. * * @param {MutationOptions<TData, TVariables>} options - The options for the mutation. * @param {function} options.mutationFn - The mutation function to be executed. * @param {function} [options.onMutate] - Optional callback function to be called before the mutation function is executed. * @param {function} [options.onSuccess] - Optional callback function to be called if the mutation is successful. * @param {function} [options.onError] - Optional callback function to be called if the mutation fails. * @param {function} [options.onSettled] - Optional callback function to be called when the mutation is either successful or fails. * @param {number} [options.retry=0] - The number of retry attempts if the mutation fails. * @param {function} [options.retryDelay] - Function to calculate the delay between retry attempts. * * @returns {MutationResult<TData, TVariables>} The result of the mutation, including state and helper functions. * * @typedef {Object} MutationOptions * @property {function} mutationFn - The mutation function to be executed. * @property {function} [onMutate] - Optional callback function to be called before the mutation function is executed. * @property {function} [onSuccess] - Optional callback function to be called if the mutation is successful. * @property {function} [onError] - Optional callback function to be called if the mutation fails. * @property {function} [onSettled] - Optional callback function to be called when the mutation is either successful or fails. * @property {number} [retry=0] - The number of retry attempts if the mutation fails. * @property {function} [retryDelay] - Function to calculate the delay between retry attempts. * * @typedef {Object} MutationResult * @property {TData | undefined} data - The data returned by the mutation. * @property {Error | null} error - The error returned by the mutation, if any. * @property {boolean} isLoading - Whether the mutation is currently loading. * @property {boolean} isError - Whether the mutation has failed. * @property {boolean} isSuccess - Whether the mutation was successful. * @property {boolean} isIdle - Whether the mutation is idle. * @property {function} reset - Function to reset the mutation state. * @property {function} mutate - Function to execute the mutation. * @property {function} mutateAsync - Async function to execute the mutation. * @property {"idle" | "loading" | "error" | "success"} status - The current status of the mutation. */ export declare function useMutation<TData = unknown, TVariables = unknown>(options: MutationOptions<TData, TVariables>): MutationResult<TData, TVariables>; /** * Custom hook to manage infinite queries with pagination support. * * @template TData - The type of the data returned by the query function. * @template TParams - The type of the parameters passed to the query function. * * @param {InfiniteQueryOptions<TData, TParams>} options - The options for the infinite query. * @param {QueryKey} options.queryKey - The key for the query. * @param {Function} options.queryFn - The function to fetch the data. * @param {Function} options.getNextPageParam - Function to get the next page parameter. * @param {Function} [options.getPreviousPageParam] - Function to get the previous page parameter. * @param {number} [options.initialPageParam=0] - The initial page parameter. * @param {Object} [options.queryOptions] - Additional options for the query. * * @returns {InfiniteQueryResult<TData>} The result of the infinite query. * * @typedef {Object} InfiniteQueryOptions * @property {QueryKey} queryKey - The key for the query. * @property {Function} queryFn - The function to fetch the data. * @property {Function} getNextPageParam - Function to get the next page parameter. * @property {Function} [getPreviousPageParam] - Function to get the previous page parameter. * @property {number} [initialPageParam=0] - The initial page parameter. * @property {Object} [queryOptions] - Additional options for the query. * * @typedef {Object} InfiniteQueryResult * @property {TData[][] | undefined} data - The data returned by the query. * @property {Function} fetchNextPage - Function to fetch the next page. * @property {Function} fetchPreviousPage - Function to fetch the previous page. * @property {boolean} hasNextPage - Whether there is a next page. * @property {boolean} hasPreviousPage - Whether there is a previous page. * @property {boolean} isFetchingNextPage - Whether the next page is being fetched. * @property {boolean} isFetchingPreviousPage - Whether the previous page is being fetched. * @property {Function} refetch - Function to refetch all pages. */ export declare function useInfiniteQuery<TData = unknown, TParams = unknown>(options: InfiniteQueryOptions<TData, TParams>): InfiniteQueryResult<TData>; /** * A custom hook that provides paginated query functionality using React Query. * * @template TData - The type of the data returned by the query. * @template TParams - The type of the parameters passed to the query function. * * @param {PaginatedQueryOptions<TData, TParams>} options - The options for the paginated query. * @param {Array} options.queryKey - The query key for the query. * @param {Function} options.queryFn - The query function that fetches the data. * @param {number} [options.pageSize=10] - The number of items per page. * @param {number} [options.pageIndex=0] - The initial page index. * @param {boolean} [options.keepPreviousData=true] - Whether to keep the previous data while loading new data. * @param {Object} [options.queryOptions] - Additional options to pass to the useQuery hook. * * @returns {PaginatedQueryResult<TData>} The result of the paginated query, including data, pagination info, and navigation helpers. * * @example * const { * data, * totalCount, * pageCount, * pageIndex, * pageSize, * setPageIndex, * previousPage, * nextPage, * canPreviousPage, * canNextPage, * ...queryResult * } = usePaginatedQuery({ * queryKey: ['items'], * queryFn: fetchItems, * pageSize: 20, * }); */ export declare function usePaginatedQuery<TData = unknown, TParams = unknown>(options: PaginatedQueryOptions<TData, TParams>): PaginatedQueryResult<TData>; /** * Custom hook to get the instance of the QueryClient. * * @returns {QueryClient} The instance of the QueryClient. */ export declare function useQueryClient(): QueryClient; /** * Custom hook that returns the number of ongoing fetch requests. * * This hook sets up an interval to periodically check the number of ongoing fetch requests * and updates the state accordingly. The interval is cleared when the component using this hook unmounts. * * @returns {number} The current number of ongoing fetch requests. */ export declare function useIsFetching(): number; //# sourceMappingURL=hooks.d.ts.map