react-query-kit 2.0.7 → 2.0.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/types.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  import type {
2
+ DataTag,
2
3
  DefaultError,
3
4
  DefinedUseInfiniteQueryResult,
4
5
  DefinedUseQueryResult,
@@ -69,7 +70,7 @@ type CompatibleUseInfiniteQueryOptions<
69
70
  TError,
70
71
  TData,
71
72
  TFnData,
72
- inferQueryKey<TVariables>,
73
+ inferQueryKey<TFnData, TVariables, TPageParam>,
73
74
  TPageParam
74
75
  >,
75
76
  UseInfiniteQueryOptions<
@@ -77,7 +78,7 @@ type CompatibleUseInfiniteQueryOptions<
77
78
  TError,
78
79
  TData,
79
80
  TFnData,
80
- inferQueryKey<TVariables>
81
+ inferQueryKey<TFnData, TVariables, TPageParam>
81
82
  >
82
83
  >
83
84
 
@@ -94,15 +95,29 @@ type WithRequired<T, K extends keyof T> = T & {
94
95
 
95
96
  export type CompatibleError = CompatibleWithV4<DefaultError, Error>
96
97
 
97
- export type inferQueryKey<TVariables> = TVariables extends void
98
+ type GetQueryKey<TVariables> = TVariables extends void
98
99
  ? [string]
99
100
  : [string, TVariables]
100
101
 
102
+ export type inferQueryKey<
103
+ TFnData,
104
+ TVariables,
105
+ TPageParam = never
106
+ > = CompatibleWithV4<
107
+ DataTag<
108
+ GetQueryKey<TVariables>,
109
+ [TPageParam] extends [never]
110
+ ? TFnData
111
+ : CompatibleInfiniteData<TFnData, TPageParam>
112
+ >,
113
+ GetQueryKey<TVariables>
114
+ >
115
+
101
116
  export type AdditionalQueryOptions<TFnData, TVariables, TPageParam = never> = {
102
117
  primaryKey: string
103
118
  queryFn: CompatibleQueryFunction<
104
119
  TFnData,
105
- inferQueryKey<TVariables>,
120
+ inferQueryKey<TFnData, TVariables, TPageParam>,
106
121
  TPageParam
107
122
  >
108
123
  variables?: TVariables
@@ -123,41 +138,79 @@ type DeepPartial<T> = T extends object
123
138
  }
124
139
  : T
125
140
 
126
- export type ExposeMethods<TFnData, TVariables, TPageParam = never> = {
141
+ export type ExposeMethods<TFnData, TVariables, TError, TPageParam = never> = {
127
142
  getPrimaryKey: () => string
128
143
  getKey: <V extends DeepPartial<TVariables> | void = void>(
129
144
  variables?: V
130
- ) => inferQueryKey<V>
145
+ ) => inferQueryKey<TFnData, V, TPageParam>
131
146
  queryFn: CompatibleQueryFunction<
132
147
  TFnData,
133
- inferQueryKey<TVariables>,
148
+ inferQueryKey<TFnData, TVariables, TPageParam>,
134
149
  TPageParam
135
150
  >
136
- queryKeyHashFn: QueryKeyHashFunction<inferQueryKey<TVariables>>
151
+ queryKeyHashFn: QueryKeyHashFunction<
152
+ inferQueryKey<TFnData, TVariables, TPageParam>
153
+ >
137
154
  getFetchOptions: (
138
155
  variables: TVariables extends void ? TVariables | void : TVariables
139
156
  ) => [TPageParam] extends [never]
140
157
  ? {
141
- queryKey: inferQueryKey<TVariables>
158
+ queryKey: inferQueryKey<TFnData, TVariables, TPageParam>
142
159
  queryFn: CompatibleQueryFunction<
143
160
  TFnData,
144
- inferQueryKey<TVariables>,
161
+ inferQueryKey<TFnData, TVariables, TPageParam>,
145
162
  TPageParam
146
163
  >
147
- queryKeyHashFn?: QueryKeyHashFunction<inferQueryKey<TVariables>>
164
+ queryKeyHashFn?: QueryKeyHashFunction<
165
+ inferQueryKey<TFnData, TVariables, TPageParam>
166
+ >
148
167
  }
149
168
  : {
150
- queryKey: inferQueryKey<TVariables>
169
+ queryKey: inferQueryKey<TFnData, TVariables, TPageParam>
151
170
  queryFn: CompatibleQueryFunction<
152
171
  TFnData,
153
- inferQueryKey<TVariables>,
172
+ inferQueryKey<TFnData, TVariables, TPageParam>,
154
173
  TPageParam
155
174
  >
156
- queryKeyHashFn?: QueryKeyHashFunction<inferQueryKey<TVariables>>
175
+ queryKeyHashFn?: QueryKeyHashFunction<
176
+ inferQueryKey<TFnData, TVariables, TPageParam>
177
+ >
157
178
  } & CompatibleInfiniteQueryPageParamsOptions<TFnData, TPageParam>
158
- // TODO This seems pretty hacky for select & useQueries.
159
- // But not work to infer TError, Hope to fix it.
160
- getOptions: ExposeMethods<TFnData, TVariables, TPageParam>['getFetchOptions']
179
+ getOptions: (
180
+ variables: TVariables extends void ? TVariables | void : TVariables
181
+ ) => [TPageParam] extends [never]
182
+ ? CompatibleWithV4<
183
+ UseQueryOptions<
184
+ TFnData,
185
+ TError,
186
+ TFnData,
187
+ inferQueryKey<
188
+ TVariables,
189
+ [TPageParam] extends [never]
190
+ ? TFnData
191
+ : CompatibleInfiniteData<TFnData, TPageParam>
192
+ >
193
+ >,
194
+ // Not work to infer TError in v4
195
+ {
196
+ queryKey: inferQueryKey<TFnData, TVariables, TPageParam>
197
+ queryFn: CompatibleQueryFunction<
198
+ TFnData,
199
+ inferQueryKey<TFnData, TVariables, TPageParam>,
200
+ TPageParam
201
+ >
202
+ queryKeyHashFn?: QueryKeyHashFunction<
203
+ inferQueryKey<TFnData, TVariables, TPageParam>
204
+ >
205
+ }
206
+ >
207
+ : CompatibleUseInfiniteQueryOptions<
208
+ TFnData,
209
+ TVariables,
210
+ TFnData,
211
+ TError,
212
+ TPageParam
213
+ >
161
214
  }
162
215
 
163
216
  // query hook
@@ -167,7 +220,12 @@ export interface CreateQueryOptions<
167
220
  TVariables = void,
168
221
  TError = CompatibleError
169
222
  > extends Omit<
170
- UseQueryOptions<TFnData, TError, TFnData, inferQueryKey<TVariables>>,
223
+ UseQueryOptions<
224
+ TFnData,
225
+ TError,
226
+ TFnData,
227
+ inferQueryKey<TFnData, TVariables>
228
+ >,
171
229
  'queryKey' | 'queryFn' | 'select'
172
230
  >,
173
231
  AdditionalQueryOptions<TFnData, TVariables> {
@@ -177,17 +235,18 @@ export interface CreateQueryOptions<
177
235
 
178
236
  export interface QueryHookOptions<TFnData, TError, TData, TVariables>
179
237
  extends Omit<
180
- UseQueryOptions<TFnData, TError, TData, inferQueryKey<TVariables>>,
238
+ UseQueryOptions<TFnData, TError, TData, inferQueryKey<TFnData, TVariables>>,
181
239
  'queryKey' | 'queryFn' | 'queryKeyHashFn'
182
240
  > {
183
241
  use?: Middleware<QueryHook<TFnData, TVariables, TError>>[]
184
242
  variables?: TVariables
185
243
  }
186
244
 
187
- export type DefinedQueryHookOptions<TFnData, TError, TData, TVariables> = Omit<
188
- QueryHookOptions<TFnData, TError, TData, TVariables>,
189
- 'initialData'
190
- > & {
245
+ export interface DefinedQueryHookOptions<TFnData, TError, TData, TVariables>
246
+ extends Omit<
247
+ QueryHookOptions<TFnData, TError, TData, TVariables>,
248
+ 'initialData'
249
+ > {
191
250
  initialData: NonUndefinedGuard<TFnData> | (() => NonUndefinedGuard<TFnData>)
192
251
  }
193
252
 
@@ -202,7 +261,7 @@ export interface QueryHook<
202
261
  TFnData = unknown,
203
262
  TVariables = any,
204
263
  TError = CompatibleError
205
- > extends ExposeMethods<TFnData, TVariables> {
264
+ > extends ExposeMethods<TFnData, TVariables, TError> {
206
265
  <TData = TFnData>(
207
266
  options: DefinedQueryHookOptions<TFnData, TError, TData, TVariables>,
208
267
  queryClient?: CompatibleWithV4<QueryClient, void>
@@ -220,7 +279,12 @@ export interface CreateSuspenseQueryOptions<
220
279
  TVariables = void,
221
280
  TError = CompatibleError
222
281
  > extends Omit<
223
- UseQueryOptions<TFnData, TError, TFnData, inferQueryKey<TVariables>>,
282
+ UseQueryOptions<
283
+ TFnData,
284
+ TError,
285
+ TFnData,
286
+ inferQueryKey<TFnData, TVariables>
287
+ >,
224
288
  | 'queryKey'
225
289
  | 'queryFn'
226
290
  | 'enabled'
@@ -237,7 +301,7 @@ export interface CreateSuspenseQueryOptions<
237
301
 
238
302
  export interface SuspenseQueryHookOptions<TFnData, TError, TData, TVariables>
239
303
  extends Omit<
240
- UseQueryOptions<TFnData, TError, TData, inferQueryKey<TVariables>>,
304
+ UseQueryOptions<TFnData, TError, TData, inferQueryKey<TFnData, TVariables>>,
241
305
  | 'queryKey'
242
306
  | 'queryFn'
243
307
  | 'queryKeyHashFn'
@@ -261,7 +325,7 @@ export interface SuspenseQueryHook<
261
325
  TFnData = unknown,
262
326
  TVariables = void,
263
327
  TError = CompatibleError
264
- > extends ExposeMethods<TFnData, TVariables> {
328
+ > extends ExposeMethods<TFnData, TVariables, TError> {
265
329
  <TData = TFnData>(
266
330
  options?: SuspenseQueryHookOptions<TFnData, TError, TData, TVariables>,
267
331
  queryClient?: CompatibleWithV4<QueryClient, void>
@@ -314,18 +378,18 @@ export interface InfiniteQueryHookOptions<
314
378
  variables?: TVariables
315
379
  }
316
380
 
317
- export type DefinedInfiniteQueryHookOptions<
381
+ export interface DefinedInfiniteQueryHookOptions<
318
382
  TFnData,
319
383
  TError,
320
384
  TData,
321
385
  TVariables,
322
386
  TPageParam = number
323
- > = Omit<
324
- InfiniteQueryHookOptions<TFnData, TError, TData, TVariables, TPageParam>,
325
- 'initialData'
326
- > & {
387
+ > extends Omit<
388
+ InfiniteQueryHookOptions<TFnData, TError, TData, TVariables, TPageParam>,
389
+ 'initialData'
390
+ > {
327
391
  initialData:
328
- | CompatibleInfiniteData<TFnData, TPageParam>
392
+ | NonUndefinedGuard<CompatibleInfiniteData<TFnData, TPageParam>>
329
393
  | (() => NonUndefinedGuard<CompatibleInfiniteData<TFnData, TPageParam>>)
330
394
  }
331
395
 
@@ -344,7 +408,7 @@ export interface InfiniteQueryHook<
344
408
  TVariables = void,
345
409
  TError = CompatibleError,
346
410
  TPageParam = number
347
- > extends ExposeMethods<TFnData, TVariables, TPageParam> {
411
+ > extends ExposeMethods<TFnData, TVariables, TError, TPageParam> {
348
412
  <TData = CompatibleWithV4<InfiniteData<TFnData, TPageParam>, TFnData>>(
349
413
  options: DefinedInfiniteQueryHookOptions<
350
414
  TFnData,
@@ -437,7 +501,7 @@ export interface SuspenseInfiniteQueryHook<
437
501
  TVariables = void,
438
502
  TError = CompatibleError,
439
503
  TPageParam = number
440
- > extends ExposeMethods<TFnData, TVariables, TPageParam> {
504
+ > extends ExposeMethods<TFnData, TVariables, TError, TPageParam> {
441
505
  <TData = CompatibleWithV4<InfiniteData<TFnData, TPageParam>, TFnData>>(
442
506
  options?: SuspenseInfiniteQueryHookOptions<
443
507
  TFnData,
package/src/utils.ts CHANGED
@@ -2,6 +2,7 @@ import {
2
2
  type Query,
3
3
  type QueryClient,
4
4
  useQueryClient,
5
+ useSuspenseQuery,
5
6
  } from '@tanstack/react-query'
6
7
 
7
8
  import type { Middleware, inferQueryKey } from './types'
@@ -18,7 +19,7 @@ export const withMiddleware = (
18
19
  const [middleware, opts] = [
19
20
  useQueryClient(
20
21
  // @ts-ignore Compatible with ReactQuery v4
21
- options?.context ? options : queryClient
22
+ useSuspenseQuery ? queryClient : options
22
23
  ).getDefaultOptions()[type],
23
24
  defaultOptions,
24
25
  options,
@@ -39,15 +40,12 @@ export const withMiddleware = (
39
40
  }
40
41
  }
41
42
 
42
- const defaultThrowOnError = (_error: unknown, query: Query) =>
43
- query.state.data === undefined
44
-
43
+ // Compatible with ReactQuery v4
45
44
  export const suspenseOptions = {
46
45
  enabled: true,
47
46
  suspense: true,
48
- throwOnError: defaultThrowOnError,
49
- // Compatible with ReactQuery v4
50
- useErrorBoundary: defaultThrowOnError,
47
+ useErrorBoundary: (_error: unknown, query: Query) =>
48
+ query.state.data === undefined,
51
49
  }
52
50
 
53
51
  export const getKey = <TVariables = void>(
@@ -56,5 +54,5 @@ export const getKey = <TVariables = void>(
56
54
  ) => {
57
55
  return (
58
56
  variables === undefined ? [primaryKey] : [primaryKey, variables]
59
- ) as inferQueryKey<TVariables>
57
+ ) as inferQueryKey<unknown, TVariables>
60
58
  }