UseSafeQueryFn
katanakit-js / useSafeQueryFn
Function: useSafeQueryFn()
Section titled “Function: useSafeQueryFn()”function useSafeQueryFn<T>(fn): (context) => Promise<T>;Defined in: core/services/query.service.ts:246
Bridges a KatanaKit Safe Result fetcher into a TanStack queryFn.
TanStack expects queryFn to resolve with data and throw on failure;
KatanaKit returns { data, error, ok }. This adapter unwraps the Safe Result
so the two fit together. Throws a real Error (with status) so instanceof
checks and error boundaries work; the upstream response body is not placed
on the thrown error.
Receives TanStack’s QueryFunctionContext, so you can forward
signal for real request cancellation.
Type Parameters
Section titled “Type Parameters”| Type Parameter | Description |
|---|---|
T |
The resolved data type. |
Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
fn |
(context) => Promise<FetchResult<T>> |
A function returning a Safe Result (e.g. () => useGetApi(...)). |
Returns
Section titled “Returns”A queryFn that throws on error.
(context) => Promise<T>
Example
Section titled “Example”import { useSafeQueryFn, useGetApi, useFetch } from "katanakit-js";
// Simple: the Safe Result gate.queryFn: useSafeQueryFn(() => useGetApi("pokeapi", "pokemonById", { params: { id } }),);
// With cancellation: forward TanStack's AbortSignal into the request.queryFn: useSafeQueryFn(({ signal }) => useFetch("pokeapi", "pokemonById", { method: "GET", params: { id }, signal }),);