-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
query.ts
153 lines (141 loc) · 3.84 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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import {
type DefaultError,
type QueryKey,
type UseInfiniteQueryOptions,
type UseInfiniteQueryResult,
type UseMutationOptions,
type UseMutationResult,
type UseQueryOptions,
type UseQueryResult,
replaceEqualDeep,
useInfiniteQuery as tanstack_useInfiniteQuery,
useQuery as tanstack_useQuery,
} from '@tanstack/react-query'
import {
type Evaluate,
type ExactPartial,
type Omit,
deepEqual,
} from '@wagmi/core/internal'
import { hashFn } from '@wagmi/core/query'
export type UseMutationParameters<
data = unknown,
error = Error,
variables = void,
context = unknown,
> = Evaluate<
Omit<
UseMutationOptions<data, error, Evaluate<variables>, context>,
'mutationFn' | 'mutationKey' | 'throwOnError'
>
>
export type UseMutationReturnType<
data = unknown,
error = Error,
variables = void,
context = unknown,
> = Evaluate<
Omit<
UseMutationResult<data, error, variables, context>,
'mutate' | 'mutateAsync'
>
>
////////////////////////////////////////////////////////////////////////////////
export type UseQueryParameters<
queryFnData = unknown,
error = DefaultError,
data = queryFnData,
queryKey extends QueryKey = QueryKey,
> = Evaluate<
ExactPartial<
Omit<UseQueryOptions<queryFnData, error, data, queryKey>, 'initialData'>
> & {
// Fix `initialData` type
initialData?:
| UseQueryOptions<queryFnData, error, data, queryKey>['initialData']
| undefined
}
>
export type UseQueryReturnType<data = unknown, error = DefaultError> = Evaluate<
UseQueryResult<data, error> & {
queryKey: QueryKey
}
>
// Adding some basic customization.
// Ideally we don't have this function, but `import('@tanstack/react-query').useQuery` currently has some quirks where it is super hard to
// pass down the inferred `initialData` type because of it's discriminated overload in the on `useQuery`.
export function useQuery<queryFnData, error, data, queryKey extends QueryKey>(
parameters: UseQueryParameters<queryFnData, error, data, queryKey> & {
queryKey: QueryKey
},
): UseQueryReturnType<data, error> {
const result = tanstack_useQuery({
...(parameters as any),
queryKeyHashFn: hashFn, // for bigint support
}) as UseQueryReturnType<data, error>
result.queryKey = parameters.queryKey
return result
}
////////////////////////////////////////////////////////////////////////////////
export type UseInfiniteQueryParameters<
queryFnData = unknown,
error = DefaultError,
data = queryFnData,
queryData = queryFnData,
queryKey extends QueryKey = QueryKey,
pageParam = unknown,
> = Evaluate<
Omit<
UseInfiniteQueryOptions<
queryFnData,
error,
data,
queryData,
queryKey,
pageParam
>,
'initialData'
> & {
// Fix `initialData` type
initialData?:
| UseInfiniteQueryOptions<
queryFnData,
error,
data,
queryKey
>['initialData']
| undefined
}
>
export type UseInfiniteQueryReturnType<
data = unknown,
error = DefaultError,
> = UseInfiniteQueryResult<data, error> & {
queryKey: QueryKey
}
// Adding some basic customization.
export function useInfiniteQuery<
queryFnData,
error,
data,
queryKey extends QueryKey,
>(
parameters: UseInfiniteQueryParameters<queryFnData, error, data, queryKey> & {
queryKey: QueryKey
},
): UseInfiniteQueryReturnType<data, error> {
const result = tanstack_useInfiniteQuery({
...(parameters as any),
queryKeyHashFn: hashFn, // for bigint support
}) as UseInfiniteQueryReturnType<data, error>
result.queryKey = parameters.queryKey
return result
}
////////////////////////////////////////////////////////////////////////////////
export function structuralSharing<data>(
oldData: data | undefined,
newData: data,
): data {
if (deepEqual(oldData, newData)) return oldData as data
return replaceEqualDeep(oldData, newData)
}