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 { const token = localStorage.getItem('token'); const headers: Record = {}; // Don't set Content-Type for FormData - let browser set it with proper boundary if (data && !(data instanceof FormData)) { headers["Content-Type"] = "application/json"; } if (token) { headers["Authorization"] = `Bearer ${token}`; } const res = await fetch(url, { method, headers, body: data instanceof FormData ? data : (data ? JSON.stringify(data) : undefined), credentials: "include", }); await throwIfResNotOk(res); return res; } type UnauthorizedBehavior = "returnNull" | "throw"; export const getQueryFn: (options: { on401: UnauthorizedBehavior; }) => QueryFunction = ({ on401: unauthorizedBehavior }) => async ({ queryKey }) => { const token = localStorage.getItem('token'); const headers: Record = {}; if (token) { headers["Authorization"] = `Bearer ${token}`; } const res = await fetch(queryKey.join("/") as string, { headers, 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, }, }, });