UNPKG

@duongtrungnguyen/next-helper

Version:
76 lines 4.4 kB
import type { QueryKey, QueryOptions, QueryFunction } from "./types"; /** * Fetches a query and manages caching, retries, and stale data. * * @template TData - The type of the data returned by the query. * @template TParams - The type of the parameters passed to the query function. * * @param {Omit<QueryOptions<TData, TParams>, "onSuccess" | "onError" | "onSettled" | "refetchInterval" | "refetchOnWindowFocus" | "refetchOnMount" | "refetchOnReconnect">} options * The options for the query, excluding certain properties that are not applicable for server-side fetching. * * @returns {Promise<TData>} A promise that resolves to the fetched data. * * @throws Will throw an error if the query fails after the specified number of retries. * * @example * ```typescript * const data = await fetchQuery({ * queryKey: 'exampleKey', * queryFn: async () => { * const response = await fetch('/api/data'); * return response.json(); * }, * staleTime: 300000, // 5 minutes * cacheTime: 1800000, // 30 minutes * retry: 3, * retryDelay: (attempt) => Math.min(1000 * 2 ** attempt, 30000), * initialData: { example: 'initial' }, * select: (data) => data.example, * }); * ``` */ export declare function fetchQuery<TData = unknown, TParams = unknown>(options: Omit<QueryOptions<TData, TParams>, "onSuccess" | "onError" | "onSettled" | "refetchInterval" | "refetchOnWindowFocus" | "refetchOnMount" | "refetchOnReconnect">): Promise<TData>; /** * Creates a query function that can be used to fetch data from a specified URL with optional query parameters. * * @template TData - The type of data expected to be returned by the query function. * @param {string} url - The URL to fetch data from. If the URL does not include "http", the base URL from the configuration will be used. * @param {Record<string, any>} [params] - Optional query parameters to include in the request. * @returns {QueryFunction<TData, any>} - A query function that can be used to fetch data. */ export declare function createQueryFn<TData = unknown>(url: string, params?: Record<string, any>): QueryFunction<TData, any>; /** * Prefetches a query with the given options. * * This function will attempt to fetch the query data based on the provided options. * It will silently catch and log any errors that occur during the prefetching process. * * @template TData - The type of the data returned by the query. * @template TParams - The type of the parameters required by the query. * * @param {Omit<QueryOptions<TData, TParams>, "onSuccess" | "onError" | "onSettled" | "refetchInterval" | "refetchOnWindowFocus" | "refetchOnMount" | "refetchOnReconnect">} options * The options to configure the query. Certain options such as "onSuccess", "onError", "onSettled", "refetchInterval", "refetchOnWindowFocus", "refetchOnMount", and "refetchOnReconnect" are omitted. * * @returns {Promise<void>} A promise that resolves when the prefetching is complete. */ export declare function prefetchQuery<TData = unknown, TParams = unknown>(options: Omit<QueryOptions<TData, TParams>, "onSuccess" | "onError" | "onSettled" | "refetchInterval" | "refetchOnWindowFocus" | "refetchOnMount" | "refetchOnReconnect">): Promise<void>; /** * Retrieves data from the query cache based on the provided query key. * * @template TData - The type of data expected to be returned. * @param {QueryKey} queryKey - The key used to identify the query in the cache. * @returns {TData | undefined} - The data associated with the query key, or undefined if not found. */ export declare function getQueryData<TData = unknown>(queryKey: QueryKey): TData | undefined; /** * Sets the query data in the cache with the specified query key and data. * * @template TData - The type of the data to be set in the cache. * @param {QueryKey} queryKey - The key to identify the query. * @param {TData} data - The data to be cached. * @param {number} [staleTime=5 * 60 * 1000] - The time in milliseconds after which the data is considered stale. Defaults to 5 minutes. * @param {number} [cacheTime=30 * 60 * 1000] - The time in milliseconds after which the cached data is garbage collected. Defaults to 30 minutes. * @returns {void} */ export declare function setQueryData<TData = unknown>(queryKey: QueryKey, data: TData, staleTime?: number, cacheTime?: number): void; //# sourceMappingURL=server.d.ts.map