feishin/src/renderer/lib/react-query.ts

98 lines
2.9 KiB
TypeScript
Raw Normal View History

2023-01-02 02:04:23 -08:00
import type {
2023-07-01 19:10:05 -07:00
UseQueryOptions,
DefaultOptions,
UseMutationOptions,
UseInfiniteQueryOptions,
2023-01-02 02:04:23 -08:00
} from '@tanstack/react-query';
2022-12-19 15:59:14 -08:00
import { QueryClient, QueryCache } from '@tanstack/react-query';
import { toast } from '/@/renderer/components/toast/index';
2022-12-19 15:59:14 -08:00
const queryCache = new QueryCache({
2023-07-01 19:10:05 -07:00
onError: (error: any, query) => {
if (query.state.data !== undefined) {
toast.show({ message: `${error.message}`, type: 'error' });
}
},
2022-12-19 15:59:14 -08:00
});
const queryConfig: DefaultOptions = {
2023-07-01 19:10:05 -07:00
mutations: {
retry: process.env.NODE_ENV === 'production',
2022-12-19 15:59:14 -08:00
},
2023-07-01 19:10:05 -07:00
queries: {
cacheTime: 1000 * 60 * 3,
onError: (err) => {
console.error('react query error:', err);
},
refetchOnWindowFocus: false,
retry: process.env.NODE_ENV === 'production',
staleTime: 1000 * 5,
useErrorBoundary: (error: any) => {
return error?.response?.status >= 500;
},
},
2022-12-19 15:59:14 -08:00
};
export const queryClient = new QueryClient({
2023-07-01 19:10:05 -07:00
defaultOptions: queryConfig,
queryCache,
2022-12-19 15:59:14 -08:00
});
2023-04-27 20:32:56 -07:00
export type QueryHookArgs<T> = {
2023-07-01 19:10:05 -07:00
options?: QueryOptions;
query: T;
serverId: string | undefined;
2023-04-27 20:32:56 -07:00
};
export type MutationHookArgs = {
2023-07-01 19:10:05 -07:00
options?: MutationOptions;
2023-04-27 20:32:56 -07:00
};
2022-12-19 15:59:14 -08:00
export type QueryOptions = {
2023-07-01 19:10:05 -07:00
cacheTime?: UseQueryOptions['cacheTime'];
enabled?: UseQueryOptions['enabled'];
keepPreviousData?: UseQueryOptions['keepPreviousData'];
meta?: UseQueryOptions['meta'];
onError?: (err: any) => void;
onSettled?: any;
onSuccess?: any;
queryKey?: UseQueryOptions['queryKey'];
refetchInterval?: number;
refetchIntervalInBackground?: UseQueryOptions['refetchIntervalInBackground'];
refetchOnWindowFocus?: boolean;
retry?: UseQueryOptions['retry'];
retryDelay?: UseQueryOptions['retryDelay'];
staleTime?: UseQueryOptions['staleTime'];
suspense?: UseQueryOptions['suspense'];
useErrorBoundary?: boolean;
2022-12-19 15:59:14 -08:00
};
export type MutationOptions = {
2023-07-01 19:10:05 -07:00
mutationKey: UseMutationOptions['mutationKey'];
onError?: (err: any) => void;
onSettled?: any;
onSuccess?: any;
retry?: UseQueryOptions['retry'];
retryDelay?: UseQueryOptions['retryDelay'];
useErrorBoundary?: boolean;
};
2023-01-02 02:04:23 -08:00
export type InfiniteQueryOptions = {
2023-07-01 19:10:05 -07:00
cacheTime?: UseInfiniteQueryOptions['cacheTime'];
enabled?: UseInfiniteQueryOptions['enabled'];
keepPreviousData?: UseInfiniteQueryOptions['keepPreviousData'];
meta?: UseInfiniteQueryOptions['meta'];
onError?: (err: any) => void;
onSettled?: any;
onSuccess?: any;
queryKey?: UseInfiniteQueryOptions['queryKey'];
refetchInterval?: number;
refetchIntervalInBackground?: UseInfiniteQueryOptions['refetchIntervalInBackground'];
refetchOnWindowFocus?: boolean;
retry?: UseInfiniteQueryOptions['retry'];
retryDelay?: UseInfiniteQueryOptions['retryDelay'];
staleTime?: UseInfiniteQueryOptions['staleTime'];
suspense?: UseInfiniteQueryOptions['suspense'];
useErrorBoundary?: boolean;
2023-01-02 02:04:23 -08:00
};