Download client/src/lib/queryClient.ts from AK270802/ChatMind: direct link, hf CLI and curl.
- Browser
- Download file 1.64 kB
-
https://huggingface.co/spaces/AK270802/ChatMind/resolve/main/client/src/lib/queryClient.ts
- Command line
-
hf download hf://spaces/AK270802/ChatMind/client/src/lib/queryClient.ts
-
curl -L -o queryClient.ts https://huggingface.co/spaces/AK270802/ChatMind/resolve/main/client/src/lib/queryClient.ts
1.64 kB
| 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}`); | |
| } | |
| } | |
| const API_BASE = import.meta.env.VITE_API_BASE_URL || ""; | |
| console.log("API_BASE:", API_BASE); | |
| export async function apiRequest( | |
| method: string, | |
| url: string, | |
| data?: unknown, | |
| ): Promise<Response> { | |
| const fullUrl = url.startsWith("http") | |
| ? url | |
| : `${API_BASE}${url}`; | |
| const res = await fetch(fullUrl, { | |
| method, | |
| headers: data ? { "Content-Type": "application/json" } : {}, | |
| body: data ? JSON.stringify(data) : undefined, | |
| credentials: "include", | |
| }); | |
| await throwIfResNotOk(res); | |
| return res; | |
| } | |
| type UnauthorizedBehavior = "returnNull" | "throw"; | |
| export const getQueryFn: <T>(options: { | |
| on401: UnauthorizedBehavior; | |
| }) => QueryFunction<T> = | |
| ({ on401: unauthorizedBehavior }) => | |
| async ({ queryKey }) => { | |
| const url = queryKey.join("/"); | |
| const fullUrl = url.startsWith("http") | |
| ? url | |
| : `${API_BASE}${url}`; | |
| const res = await fetch(fullUrl, { | |
| 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, | |
| }, | |
| }, | |
| }); | |