Files
LogiFlow/client/src/lib/queryClient.ts
T
2025-08-11 14:43:48 +00:00

81 lines
2.1 KiB
TypeScript

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(
url: string,
method: string = 'GET',
body?: unknown,
headers: Record<string, string> = {}
): Promise<any> {
console.log("🌐 API Request:", { url, method, body });
const res = await fetch(url, {
method,
headers: {
...(body ? { "Content-Type": "application/json" } : {}),
...headers,
},
body: body ? JSON.stringify(body) : undefined,
credentials: "include",
});
console.log("🌐 API Response:", { status: res.status, ok: res.ok });
await throwIfResNotOk(res);
// Return JSON response
if (res.headers.get('content-type')?.includes('application/json')) {
return await res.json();
}
return res;
}
type UnauthorizedBehavior = "returnNull" | "throw";
export const getQueryFn: <T>(options: {
on401: UnauthorizedBehavior;
}) => QueryFunction<T> =
({ on401: unauthorizedBehavior }) =>
async ({ queryKey }) => {
const res = await fetch(queryKey[0] 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: "returnNull" }),
refetchInterval: false,
refetchOnWindowFocus: false,
refetchOnMount: true,
refetchOnReconnect: false,
staleTime: 30 * 1000, // 30 secondes de cache pour de meilleures performances
gcTime: 10 * 60 * 1000, // 10 minutes
retry: (failureCount, error: any) => {
// Ne pas retry les erreurs d'authentification
if (error?.message?.includes('401') || error?.message?.includes('Unauthorized')) {
return false;
}
return failureCount < 2;
},
},
mutations: {
retry: false,
},
},
});