react-query-kit 2.0.7 → 2.0.9-beta.0

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.
Files changed (54) hide show
  1. package/README.md +5 -4
  2. package/build/lib/createInfiniteQuery.esm.js +2 -2
  3. package/build/lib/createInfiniteQuery.esm.js.map +1 -1
  4. package/build/lib/createInfiniteQuery.js +22 -2
  5. package/build/lib/createInfiniteQuery.js.map +1 -1
  6. package/build/lib/createInfiniteQuery.mjs +2 -2
  7. package/build/lib/createInfiniteQuery.mjs.map +1 -1
  8. package/build/lib/createMutation.esm.js +2 -2
  9. package/build/lib/createMutation.esm.js.map +1 -1
  10. package/build/lib/createMutation.js +22 -2
  11. package/build/lib/createMutation.js.map +1 -1
  12. package/build/lib/createMutation.mjs +2 -2
  13. package/build/lib/createMutation.mjs.map +1 -1
  14. package/build/lib/createQuery.esm.js +2 -2
  15. package/build/lib/createQuery.esm.js.map +1 -1
  16. package/build/lib/createQuery.js +22 -2
  17. package/build/lib/createQuery.js.map +1 -1
  18. package/build/lib/createQuery.mjs +2 -2
  19. package/build/lib/createQuery.mjs.map +1 -1
  20. package/build/lib/createSuspenseInfiniteQuery.esm.js +3 -3
  21. package/build/lib/createSuspenseInfiniteQuery.esm.js.map +1 -1
  22. package/build/lib/createSuspenseInfiniteQuery.js +22 -2
  23. package/build/lib/createSuspenseInfiniteQuery.js.map +1 -1
  24. package/build/lib/createSuspenseInfiniteQuery.mjs +3 -3
  25. package/build/lib/createSuspenseInfiniteQuery.mjs.map +1 -1
  26. package/build/lib/createSuspenseQuery.esm.js +3 -3
  27. package/build/lib/createSuspenseQuery.esm.js.map +1 -1
  28. package/build/lib/createSuspenseQuery.js +22 -2
  29. package/build/lib/createSuspenseQuery.js.map +1 -1
  30. package/build/lib/createSuspenseQuery.mjs +3 -3
  31. package/build/lib/createSuspenseQuery.mjs.map +1 -1
  32. package/build/lib/types.d.ts +36 -29
  33. package/build/lib/utils.d.ts +6 -6
  34. package/build/lib/utils.esm.js +7 -10
  35. package/build/lib/utils.esm.js.map +1 -1
  36. package/build/lib/utils.js +27 -9
  37. package/build/lib/utils.js.map +1 -1
  38. package/build/lib/utils.mjs +7 -10
  39. package/build/lib/utils.mjs.map +1 -1
  40. package/build/stats-html.html +1 -1
  41. package/build/stats.json +96 -96
  42. package/build/umd/index.development.js +31 -14
  43. package/build/umd/index.development.js.map +1 -1
  44. package/build/umd/index.production.js +1 -1
  45. package/build/umd/index.production.js.map +1 -1
  46. package/build/zip/zip.esm.js +1 -1
  47. package/package.json +2 -2
  48. package/src/createInfiniteQuery.ts +2 -2
  49. package/src/createMutation.ts +2 -2
  50. package/src/createQuery.tsx +2 -2
  51. package/src/createSuspenseInfiniteQuery.ts +5 -3
  52. package/src/createSuspenseQuery.ts +5 -3
  53. package/src/types.ts +87 -37
  54. package/src/utils.ts +11 -16
@@ -1,4 +1,4 @@
1
- import { useInfiniteQuery } from '@tanstack/react-query'
1
+ import * as ReactQuery from '@tanstack/react-query'
2
2
 
3
3
  import { createBaseQuery } from './createBaseQuery'
4
4
  import type {
@@ -15,5 +15,5 @@ export const createInfiniteQuery = <
15
15
  >(
16
16
  options: CreateInfiniteQueryOptions<TFnData, TVariables, TError, TPageParam>
17
17
  ): InfiniteQueryHook<TFnData, TVariables, TError, TPageParam> => {
18
- return createBaseQuery(options, useInfiniteQuery)
18
+ return createBaseQuery(options, ReactQuery.useInfiniteQuery)
19
19
  }
@@ -1,4 +1,4 @@
1
- import { useMutation } from '@tanstack/react-query'
1
+ import * as ReactQuery from '@tanstack/react-query'
2
2
 
3
3
  import type {
4
4
  CompatibleError,
@@ -18,7 +18,7 @@ export const createMutation = <
18
18
  const getKey = () => defaultOptions.mutationKey
19
19
 
20
20
  return Object.assign(
21
- withMiddleware(useMutation, defaultOptions, 'mutations'),
21
+ withMiddleware(ReactQuery.useMutation, defaultOptions, 'mutations'),
22
22
  {
23
23
  getKey,
24
24
  mutationFn: defaultOptions.mutationFn,
@@ -1,4 +1,4 @@
1
- import { useQuery } from '@tanstack/react-query'
1
+ import * as ReactQuery from '@tanstack/react-query'
2
2
 
3
3
  import { createBaseQuery } from './createBaseQuery'
4
4
  import type { CompatibleError, CreateQueryOptions, QueryHook } from './types'
@@ -10,5 +10,5 @@ export const createQuery = <
10
10
  >(
11
11
  options: CreateQueryOptions<TFnData, TVariables, TError>
12
12
  ): QueryHook<TFnData, TVariables, TError> => {
13
- return createBaseQuery(options, useQuery)
13
+ return createBaseQuery(options, ReactQuery.useQuery)
14
14
  }
@@ -1,4 +1,4 @@
1
- import { useInfiniteQuery } from '@tanstack/react-query'
1
+ import * as ReactQuery from '@tanstack/react-query'
2
2
 
3
3
  import { createBaseQuery } from './createBaseQuery'
4
4
  import type {
@@ -6,7 +6,7 @@ import type {
6
6
  CreateSuspenseInfiniteQueryOptions,
7
7
  SuspenseInfiniteQueryHook,
8
8
  } from './types'
9
- import { suspenseOptions } from './utils'
9
+ import { isV5, suspenseOptions } from './utils'
10
10
 
11
11
  export const createSuspenseInfiniteQuery = <
12
12
  TFnData,
@@ -21,5 +21,7 @@ export const createSuspenseInfiniteQuery = <
21
21
  TPageParam
22
22
  >
23
23
  ): SuspenseInfiniteQueryHook<TFnData, TVariables, TError, TPageParam> => {
24
- return createBaseQuery(options, useInfiniteQuery, suspenseOptions)
24
+ return isV5()
25
+ ? createBaseQuery(options, ReactQuery.useSuspenseInfiniteQuery)
26
+ : createBaseQuery(options, ReactQuery.useInfiniteQuery, suspenseOptions)
25
27
  }
@@ -1,4 +1,4 @@
1
- import { useQuery } from '@tanstack/react-query'
1
+ import * as ReactQuery from '@tanstack/react-query'
2
2
 
3
3
  import { createBaseQuery } from './createBaseQuery'
4
4
  import type {
@@ -6,7 +6,7 @@ import type {
6
6
  CreateSuspenseQueryOptions,
7
7
  SuspenseQueryHook,
8
8
  } from './types'
9
- import { suspenseOptions } from './utils'
9
+ import { isV5, suspenseOptions } from './utils'
10
10
 
11
11
  export const createSuspenseQuery = <
12
12
  TFnData,
@@ -15,5 +15,7 @@ export const createSuspenseQuery = <
15
15
  >(
16
16
  options: CreateSuspenseQueryOptions<TFnData, TVariables, TError>
17
17
  ): SuspenseQueryHook<TFnData, TVariables, TError> => {
18
- return createBaseQuery(options, useQuery, suspenseOptions)
18
+ return isV5()
19
+ ? createBaseQuery(options, ReactQuery.useSuspenseQuery)
20
+ : createBaseQuery(options, ReactQuery.useQuery, suspenseOptions)
19
21
  }
package/src/types.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  import type {
2
+ DataTag,
2
3
  DefaultError,
3
4
  DefinedUseInfiniteQueryResult,
4
5
  DefinedUseQueryResult,
@@ -33,7 +34,7 @@ type CompatibleWithV4<V5, V4> =
33
34
  type CompatibleQueryFunction<
34
35
  T = unknown,
35
36
  TQueryKey extends QueryKey = QueryKey,
36
- TPageParam = unknown
37
+ TPageParam = never
37
38
  > = CompatibleWithV4<
38
39
  QueryFunction<T, TQueryKey, TPageParam>,
39
40
  QueryFunction<T, TQueryKey>
@@ -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,27 @@ 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] ? TFnData : InfiniteData<TFnData, TPageParam>
110
+ >,
111
+ GetQueryKey<TVariables>
112
+ >
113
+
101
114
  export type AdditionalQueryOptions<TFnData, TVariables, TPageParam = never> = {
102
115
  primaryKey: string
103
116
  queryFn: CompatibleQueryFunction<
104
117
  TFnData,
105
- inferQueryKey<TVariables>,
118
+ inferQueryKey<TFnData, TVariables, TPageParam>,
106
119
  TPageParam
107
120
  >
108
121
  variables?: TVariables
@@ -123,41 +136,67 @@ type DeepPartial<T> = T extends object
123
136
  }
124
137
  : T
125
138
 
126
- export type ExposeMethods<TFnData, TVariables, TPageParam = never> = {
139
+ export type ExposeMethods<TFnData, TVariables, TError, TPageParam = never> = {
127
140
  getPrimaryKey: () => string
128
141
  getKey: <V extends DeepPartial<TVariables> | void = void>(
129
142
  variables?: V
130
- ) => inferQueryKey<V>
143
+ ) => inferQueryKey<TFnData, V, TPageParam>
131
144
  queryFn: CompatibleQueryFunction<
132
145
  TFnData,
133
- inferQueryKey<TVariables>,
146
+ inferQueryKey<TFnData, TVariables, TPageParam>,
134
147
  TPageParam
135
148
  >
136
- queryKeyHashFn: QueryKeyHashFunction<inferQueryKey<TVariables>>
149
+ queryKeyHashFn: QueryKeyHashFunction<
150
+ inferQueryKey<TFnData, TVariables, TPageParam>
151
+ >
137
152
  getFetchOptions: (
138
153
  variables: TVariables extends void ? TVariables | void : TVariables
139
154
  ) => [TPageParam] extends [never]
140
155
  ? {
141
- queryKey: inferQueryKey<TVariables>
156
+ queryKey: inferQueryKey<TFnData, TVariables>
142
157
  queryFn: CompatibleQueryFunction<
143
158
  TFnData,
144
- inferQueryKey<TVariables>,
145
- TPageParam
159
+ inferQueryKey<TFnData, TVariables>
160
+ >
161
+ queryKeyHashFn?: QueryKeyHashFunction<
162
+ inferQueryKey<TFnData, TVariables>
146
163
  >
147
- queryKeyHashFn?: QueryKeyHashFunction<inferQueryKey<TVariables>>
148
164
  }
149
165
  : {
150
- queryKey: inferQueryKey<TVariables>
166
+ queryKey: inferQueryKey<TFnData, TVariables, TPageParam>
151
167
  queryFn: CompatibleQueryFunction<
152
168
  TFnData,
153
- inferQueryKey<TVariables>,
169
+ inferQueryKey<TFnData, TVariables, TPageParam>,
154
170
  TPageParam
155
171
  >
156
- queryKeyHashFn?: QueryKeyHashFunction<inferQueryKey<TVariables>>
172
+ queryKeyHashFn?: QueryKeyHashFunction<
173
+ inferQueryKey<TFnData, TVariables, TPageParam>
174
+ >
157
175
  } & 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']
176
+ getOptions: (
177
+ variables: TVariables extends void ? TVariables | void : TVariables
178
+ ) => [TPageParam] extends [never]
179
+ ? CompatibleWithV4<
180
+ UseQueryOptions<
181
+ TFnData,
182
+ TError,
183
+ TFnData,
184
+ DataTag<GetQueryKey<TVariables>, TFnData>
185
+ >,
186
+ // Not work to infer TError in v4
187
+ {
188
+ queryKey: GetQueryKey<TVariables>
189
+ queryFn: QueryFunction<TFnData, GetQueryKey<TVariables>>
190
+ queryKeyHashFn?: QueryKeyHashFunction<GetQueryKey<TVariables>>
191
+ }
192
+ >
193
+ : CompatibleUseInfiniteQueryOptions<
194
+ TFnData,
195
+ TVariables,
196
+ TFnData,
197
+ TError,
198
+ TPageParam
199
+ >
161
200
  }
162
201
 
163
202
  // query hook
@@ -167,7 +206,12 @@ export interface CreateQueryOptions<
167
206
  TVariables = void,
168
207
  TError = CompatibleError
169
208
  > extends Omit<
170
- UseQueryOptions<TFnData, TError, TFnData, inferQueryKey<TVariables>>,
209
+ UseQueryOptions<
210
+ TFnData,
211
+ TError,
212
+ TFnData,
213
+ inferQueryKey<TFnData, TVariables>
214
+ >,
171
215
  'queryKey' | 'queryFn' | 'select'
172
216
  >,
173
217
  AdditionalQueryOptions<TFnData, TVariables> {
@@ -177,17 +221,18 @@ export interface CreateQueryOptions<
177
221
 
178
222
  export interface QueryHookOptions<TFnData, TError, TData, TVariables>
179
223
  extends Omit<
180
- UseQueryOptions<TFnData, TError, TData, inferQueryKey<TVariables>>,
224
+ UseQueryOptions<TFnData, TError, TData, inferQueryKey<TFnData, TVariables>>,
181
225
  'queryKey' | 'queryFn' | 'queryKeyHashFn'
182
226
  > {
183
227
  use?: Middleware<QueryHook<TFnData, TVariables, TError>>[]
184
228
  variables?: TVariables
185
229
  }
186
230
 
187
- export type DefinedQueryHookOptions<TFnData, TError, TData, TVariables> = Omit<
188
- QueryHookOptions<TFnData, TError, TData, TVariables>,
189
- 'initialData'
190
- > & {
231
+ export interface DefinedQueryHookOptions<TFnData, TError, TData, TVariables>
232
+ extends Omit<
233
+ QueryHookOptions<TFnData, TError, TData, TVariables>,
234
+ 'initialData'
235
+ > {
191
236
  initialData: NonUndefinedGuard<TFnData> | (() => NonUndefinedGuard<TFnData>)
192
237
  }
193
238
 
@@ -202,7 +247,7 @@ export interface QueryHook<
202
247
  TFnData = unknown,
203
248
  TVariables = any,
204
249
  TError = CompatibleError
205
- > extends ExposeMethods<TFnData, TVariables> {
250
+ > extends ExposeMethods<TFnData, TVariables, TError> {
206
251
  <TData = TFnData>(
207
252
  options: DefinedQueryHookOptions<TFnData, TError, TData, TVariables>,
208
253
  queryClient?: CompatibleWithV4<QueryClient, void>
@@ -220,7 +265,12 @@ export interface CreateSuspenseQueryOptions<
220
265
  TVariables = void,
221
266
  TError = CompatibleError
222
267
  > extends Omit<
223
- UseQueryOptions<TFnData, TError, TFnData, inferQueryKey<TVariables>>,
268
+ UseQueryOptions<
269
+ TFnData,
270
+ TError,
271
+ TFnData,
272
+ inferQueryKey<TFnData, TVariables>
273
+ >,
224
274
  | 'queryKey'
225
275
  | 'queryFn'
226
276
  | 'enabled'
@@ -237,7 +287,7 @@ export interface CreateSuspenseQueryOptions<
237
287
 
238
288
  export interface SuspenseQueryHookOptions<TFnData, TError, TData, TVariables>
239
289
  extends Omit<
240
- UseQueryOptions<TFnData, TError, TData, inferQueryKey<TVariables>>,
290
+ UseQueryOptions<TFnData, TError, TData, inferQueryKey<TFnData, TVariables>>,
241
291
  | 'queryKey'
242
292
  | 'queryFn'
243
293
  | 'queryKeyHashFn'
@@ -261,7 +311,7 @@ export interface SuspenseQueryHook<
261
311
  TFnData = unknown,
262
312
  TVariables = void,
263
313
  TError = CompatibleError
264
- > extends ExposeMethods<TFnData, TVariables> {
314
+ > extends ExposeMethods<TFnData, TVariables, TError> {
265
315
  <TData = TFnData>(
266
316
  options?: SuspenseQueryHookOptions<TFnData, TError, TData, TVariables>,
267
317
  queryClient?: CompatibleWithV4<QueryClient, void>
@@ -314,18 +364,18 @@ export interface InfiniteQueryHookOptions<
314
364
  variables?: TVariables
315
365
  }
316
366
 
317
- export type DefinedInfiniteQueryHookOptions<
367
+ export interface DefinedInfiniteQueryHookOptions<
318
368
  TFnData,
319
369
  TError,
320
370
  TData,
321
371
  TVariables,
322
372
  TPageParam = number
323
- > = Omit<
324
- InfiniteQueryHookOptions<TFnData, TError, TData, TVariables, TPageParam>,
325
- 'initialData'
326
- > & {
373
+ > extends Omit<
374
+ InfiniteQueryHookOptions<TFnData, TError, TData, TVariables, TPageParam>,
375
+ 'initialData'
376
+ > {
327
377
  initialData:
328
- | CompatibleInfiniteData<TFnData, TPageParam>
378
+ | NonUndefinedGuard<CompatibleInfiniteData<TFnData, TPageParam>>
329
379
  | (() => NonUndefinedGuard<CompatibleInfiniteData<TFnData, TPageParam>>)
330
380
  }
331
381
 
@@ -344,7 +394,7 @@ export interface InfiniteQueryHook<
344
394
  TVariables = void,
345
395
  TError = CompatibleError,
346
396
  TPageParam = number
347
- > extends ExposeMethods<TFnData, TVariables, TPageParam> {
397
+ > extends ExposeMethods<TFnData, TVariables, TError, TPageParam> {
348
398
  <TData = CompatibleWithV4<InfiniteData<TFnData, TPageParam>, TFnData>>(
349
399
  options: DefinedInfiniteQueryHookOptions<
350
400
  TFnData,
@@ -437,7 +487,7 @@ export interface SuspenseInfiniteQueryHook<
437
487
  TVariables = void,
438
488
  TError = CompatibleError,
439
489
  TPageParam = number
440
- > extends ExposeMethods<TFnData, TVariables, TPageParam> {
490
+ > extends ExposeMethods<TFnData, TVariables, TError, TPageParam> {
441
491
  <TData = CompatibleWithV4<InfiniteData<TFnData, TPageParam>, TFnData>>(
442
492
  options?: SuspenseInfiniteQueryHookOptions<
443
493
  TFnData,
package/src/utils.ts CHANGED
@@ -1,8 +1,4 @@
1
- import {
2
- type Query,
3
- type QueryClient,
4
- useQueryClient,
5
- } from '@tanstack/react-query'
1
+ import * as ReactQuery from '@tanstack/react-query'
6
2
 
7
3
  import type { Middleware, inferQueryKey } from './types'
8
4
 
@@ -12,13 +8,13 @@ export const withMiddleware = (
12
8
  type: 'queries' | 'mutations'
13
9
  ) => {
14
10
  return function useMiddleware(
15
- options?: { client?: QueryClient; use?: Middleware[] },
16
- queryClient?: QueryClient
11
+ options?: { client?: ReactQuery.QueryClient; use?: Middleware[] },
12
+ queryClient?: ReactQuery.QueryClient
17
13
  ) {
18
14
  const [middleware, opts] = [
19
- useQueryClient(
15
+ ReactQuery.useQueryClient(
20
16
  // @ts-ignore Compatible with ReactQuery v4
21
- options?.context ? options : queryClient
17
+ isV5() ? queryClient : options
22
18
  ).getDefaultOptions()[type],
23
19
  defaultOptions,
24
20
  options,
@@ -39,22 +35,21 @@ export const withMiddleware = (
39
35
  }
40
36
  }
41
37
 
42
- const defaultThrowOnError = (_error: unknown, query: Query) =>
43
- query.state.data === undefined
44
-
38
+ // Compatible with ReactQuery v4
45
39
  export const suspenseOptions = {
46
40
  enabled: true,
47
41
  suspense: true,
48
- throwOnError: defaultThrowOnError,
49
- // Compatible with ReactQuery v4
50
- useErrorBoundary: defaultThrowOnError,
42
+ useErrorBoundary: (_error: unknown, query: ReactQuery.Query) =>
43
+ query.state.data === undefined,
51
44
  }
52
45
 
46
+ export const isV5 = () => !!ReactQuery.useSuspenseQuery
47
+
53
48
  export const getKey = <TVariables = void>(
54
49
  primaryKey: string,
55
50
  variables?: TVariables
56
51
  ) => {
57
52
  return (
58
53
  variables === undefined ? [primaryKey] : [primaryKey, variables]
59
- ) as inferQueryKey<TVariables>
54
+ ) as inferQueryKey<unknown, TVariables>
60
55
  }