/
masterOK
/
FEF
Обзор
Документация
Войти
/
masterOK
/
FEF
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
queryClient.ts
63 строки
1 KB
masterOK
upload files
10 ноя 2025, 19:52
10 ноя 2025, 19:52
2cb4a49
Код
Авторство
О чём код?
import { QueryClient, QueryFunction } from "@tanstack/react-query"; async function throwIfResNotOk(res: Response) { if (!res.ok) { const text = (await res.text()) || res.statusText; throw new Error(`${res.status}: ${text}`); } } export async function apiRequest( method: string, url: string, data?: unknown | undefined, ): Promise<any> { const headers: Record<string, string> = {}; if (data) { headers["Content-Type"] = "application/json"; } const res = await fetch(url, { method, headers, body: data ? JSON.stringify(data) : undefined, credentials: "include", }); await throwIfResNotOk(res); return await res.json(); } type UnauthorizedBehavior = "returnNull" | "throw"; export const getQueryFn: <T>(options: { on401: UnauthorizedBehavior; }) => QueryFunction<T> = ({ on401: unauthorizedBehavior }) => async ({ queryKey }) => { const res = await fetch(queryKey.join("/") as string, { credentials: "include", }); if (unauthorizedBehavior === "returnNull" && res.status === 401) { return null; } await throwIfResNotOk(res); return await res.json(); }; export const queryClient = new QueryClient({ defaultOptions: { queries: { queryFn: getQueryFn({ on401: "throw" }), refetchInterval: false, refetchOnWindowFocus: false, staleTime: Infinity, retry: false, }, mutations: { retry: false, }, }, });