React 19 + Vite 6 + Router 7 + TanStack Query + Zustand;Semi 二次封装与 Remix 图标统一出口;顶部模块 + 左侧功能菜单布局、Cloud Blue 主题、登录页与 401/403/404/500 页面;请求层含 token 注入、单飞刷新与统一错误提示。
38 lines
987 B
TypeScript
38 lines
987 B
TypeScript
import { notifyError } from '@lingxi/ui';
|
|
import { MutationCache, QueryCache, QueryClient } from '@tanstack/react-query';
|
|
|
|
import { ApiError } from './client';
|
|
|
|
function reportError(error: unknown): void {
|
|
if (error instanceof ApiError) {
|
|
if (error.status === 401) {
|
|
return;
|
|
}
|
|
notifyError(error.message, error.traceId);
|
|
return;
|
|
}
|
|
notifyError(error instanceof Error ? error.message : '网络请求失败,请稍后重试');
|
|
}
|
|
|
|
function shouldRetry(failureCount: number, error: unknown): boolean {
|
|
if (failureCount >= 2) {
|
|
return false;
|
|
}
|
|
if (error instanceof ApiError) {
|
|
return error.status >= 500 || error.status === 429;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
export const queryClient = new QueryClient({
|
|
queryCache: new QueryCache({ onError: reportError }),
|
|
mutationCache: new MutationCache({ onError: reportError }),
|
|
defaultOptions: {
|
|
queries: {
|
|
retry: shouldRetry,
|
|
refetchOnWindowFocus: false,
|
|
staleTime: 30_000,
|
|
},
|
|
},
|
|
});
|