-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquery.ts
39 lines (35 loc) · 1.08 KB
/
query.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import {
useInfiniteQuery as useReactInfiniteQuery,
useQuery as useReactQuery,
useQueryClient as useReactQueryClient,
} from '@tanstack/react-query';
import type { InfiniteQueryOptions, QueryKey, QueryOptions } from '@/libs/query/type';
export function useQueryClient() {
return useReactQueryClient();
}
export function useQuery<
Data = unknown,
Err = unknown,
Keys extends QueryKey = QueryKey,
>(options: QueryOptions<Data, Err, Keys>) {
return useReactQuery(options);
}
export function useInfiniteQuery<
Data = unknown,
Err = unknown,
PageParam = unknown,
Keys extends QueryKey = QueryKey,
>(options: InfiniteQueryOptions<Data, Err, PageParam, Keys>) {
const query = useReactInfiniteQuery({
...options,
queryFn: (
typeof options?.queryFn === 'function'
? (ctx) => options.queryFn({ ...ctx, pageParam: ctx.pageParam || options.initialPageParam })
: undefined
),
});
if (query.data?.pageParams?.length && typeof query.data?.pageParams[0] === 'undefined') {
query.data.pageParams[0] = options.initialPageParam;
}
return query;
}