import { MutationCache, QueryCache, QueryClient } from "@tanstack/react-query"; import { ApiError } from "@/api/types"; function shouldRetry(failureCount: number, error: unknown): boolean { if (failureCount >= 2) return false; if (error instanceof ApiError) { const status = error.status ?? 0; if ([400, 401, 403, 404, 409, 422].includes(status)) return false; if (status === 0 || status === 408 || status === 429 || status >= 500) return true; } return true; } export const queryClient = new QueryClient({ queryCache: new QueryCache({ onError: (error) => console.error("[Query error]", error), }), mutationCache: new MutationCache({ onError: (error) => console.error("[Mutation error]", error), }), defaultOptions: { queries: { staleTime: 5 * 60 * 1000, gcTime: 30 * 60 * 1000, retry: shouldRetry, refetchOnWindowFocus: false, refetchOnReconnect: true, }, mutations: { retry: false, }, }, });