import type { ReactiveActionSource } from '@solana/kit'; import { type QueryKey, type UseQueryOptions, type UseQueryResult } from '@tanstack/react-query'; import type { UseRequestOptions } from '../useRequest'; /** * Options accepted by {@link useRequestQuery}: every option `@tanstack/react-query`'s `useQuery` * takes (minus `queryKey` and `queryFn`, which this hook owns) plus the Kit-specific * {@link UseRequestOptions}. * * @typeParam T - The value the underlying request resolves to. * @typeParam TError - The error type TanStack Query will surface on failure. * @typeParam TData - The shape of `data` after any `select` transform. Defaults to `T` when no * `select` is supplied. */ export type UseRequestQueryOptions = Omit, 'queryFn' | 'queryKey'> & UseRequestOptions; /** * TanStack Query-backed counterpart to core's `useRequest`. Accepts the same source shape — a * {@link ReactiveActionSource} (satisfied by `PendingRpcRequest`) or a * `(signal: AbortSignal) => Promise` function — and routes it through TanStack Query's cache, so * components reading the same `key` dedupe into one in-flight request and the result becomes * Suspense-capable and visible in TanStack Query's devtools. * * The query's `queryFn` is handed TanStack Query's own `AbortSignal`, which aborts when the query * is cancelled (unmount, garbage collection, or a superseding refetch). That signal is threaded * into the source so cancellation propagates all the way down. When a `getAbortSignal` factory is * also supplied (e.g. for a per-attempt timeout), the two signals are combined via * {@link AbortSignal.any} — aborting either one cancels the attempt. * * Pass `null` for `source` to disable — useful when one of the source's inputs isn't yet known. * This maps to TanStack's `enabled: false` and combines with any `enabled` you pass yourself (the * query runs only when the source is non-null *and* your `enabled` is not `false`). Use * `result.refetch()` to refresh. * * @typeParam T - The value the underlying request resolves to. * @typeParam TError - The error type TanStack Query will surface on failure. * @typeParam TData - The shape of `data` after any `select` transform. Defaults to `T` when no * `select` is supplied; pass a `select: (data: T) => TData` option to project the cached value into * a different shape and `result.data` narrows accordingly. * * @example * ```tsx * function LatestBlockhash() { * const client = useClient>(); * const { data, error, isLoading, refetch } = useRequestQuery( * ['latestBlockhash'], * client.rpc.getLatestBlockhash(), * { getAbortSignal: () => AbortSignal.timeout(5_000) }, * ); * if (error) return ; * if (isLoading) return

Loading…

; * return

Blockhash: {data!.value.blockhash}

; * } * * // Function shape — wraps an arbitrary async call. The source can be an inline closure recreated * // each render; the hook reads the latest one when the query fires and keys the cache off `key`, * // so there's no need to memoize it. * function Profile({ userId }: { userId: string }) { * const { data, error, isLoading, refetch } = useRequestQuery( * ['users', userId], * (signal: AbortSignal) => fetch(`/api/users/${userId}`, { signal }).then(r => r.json()), * ); * if (error) return ; * if (isLoading) return

Loading…

; * return

{data!.name}

; * } * ``` * * @see {@link UseRequestQueryOptions} */ export declare function useRequestQuery(key: QueryKey, source: ReactiveActionSource | ((signal: AbortSignal) => Promise) | null, options?: UseRequestQueryOptions): UseQueryResult; //# sourceMappingURL=useRequestQuery.d.ts.map