react-query-kit 2.0.1 → 2.0.3

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 (55) hide show
  1. package/README.md +44 -21
  2. package/build/lib/createBaseQuery.d.ts +1 -1
  3. package/build/lib/createBaseQuery.esm.js +11 -11
  4. package/build/lib/createBaseQuery.esm.js.map +1 -1
  5. package/build/lib/createBaseQuery.js +11 -11
  6. package/build/lib/createBaseQuery.js.map +1 -1
  7. package/build/lib/createBaseQuery.mjs +11 -11
  8. package/build/lib/createBaseQuery.mjs.map +1 -1
  9. package/build/lib/createInfiniteQuery.esm.js.map +1 -1
  10. package/build/lib/createInfiniteQuery.js.map +1 -1
  11. package/build/lib/createInfiniteQuery.mjs.map +1 -1
  12. package/build/lib/createMutation.esm.js.map +1 -1
  13. package/build/lib/createMutation.js.map +1 -1
  14. package/build/lib/createMutation.mjs.map +1 -1
  15. package/build/lib/createQuery.esm.js.map +1 -1
  16. package/build/lib/createQuery.js.map +1 -1
  17. package/build/lib/createQuery.mjs.map +1 -1
  18. package/build/lib/createSuspenseInfiniteQuery.esm.js +2 -8
  19. package/build/lib/createSuspenseInfiniteQuery.esm.js.map +1 -1
  20. package/build/lib/createSuspenseInfiniteQuery.js +2 -8
  21. package/build/lib/createSuspenseInfiniteQuery.js.map +1 -1
  22. package/build/lib/createSuspenseInfiniteQuery.mjs +2 -8
  23. package/build/lib/createSuspenseInfiniteQuery.mjs.map +1 -1
  24. package/build/lib/createSuspenseQuery.esm.js +2 -8
  25. package/build/lib/createSuspenseQuery.esm.js.map +1 -1
  26. package/build/lib/createSuspenseQuery.js +2 -8
  27. package/build/lib/createSuspenseQuery.js.map +1 -1
  28. package/build/lib/createSuspenseQuery.mjs +2 -8
  29. package/build/lib/createSuspenseQuery.mjs.map +1 -1
  30. package/build/lib/index.d.ts +1 -1
  31. package/build/lib/types.d.ts +17 -17
  32. package/build/lib/utils.d.ts +8 -2
  33. package/build/lib/utils.esm.js +9 -3
  34. package/build/lib/utils.esm.js.map +1 -1
  35. package/build/lib/utils.js +9 -2
  36. package/build/lib/utils.js.map +1 -1
  37. package/build/lib/utils.mjs +9 -3
  38. package/build/lib/utils.mjs.map +1 -1
  39. package/build/stats-html.html +1 -1
  40. package/build/stats.json +116 -91
  41. package/build/umd/index.development.js +21 -29
  42. package/build/umd/index.development.js.map +1 -1
  43. package/build/umd/index.production.js +1 -1
  44. package/build/umd/index.production.js.map +1 -1
  45. package/build/zip/zip.esm.js +1 -0
  46. package/package.json +2 -2
  47. package/src/createBaseQuery.ts +17 -20
  48. package/src/createInfiniteQuery.ts +2 -6
  49. package/src/createMutation.ts +2 -2
  50. package/src/createQuery.tsx +2 -2
  51. package/src/createSuspenseInfiniteQuery.ts +3 -10
  52. package/src/createSuspenseQuery.ts +3 -14
  53. package/src/index.ts +1 -28
  54. package/src/types.ts +28 -26
  55. package/src/utils.ts +12 -5
@@ -4,17 +4,14 @@ import type {
4
4
  UseInfiniteQueryOptions,
5
5
  } from '@tanstack/react-query'
6
6
 
7
- import type {
8
- AdditionalCreateOptions,
9
- CompatibleWithV4,
10
- Middleware,
11
- } from './types'
7
+ import type { AdditionalQueryOptions, Middleware } from './types'
12
8
  import { getKey as getQueryKey, withMiddleware } from './utils'
13
9
 
14
10
  interface CreateBaseQueryOptions
15
11
  extends Omit<UseInfiniteQueryOptions, 'queryKey' | 'queryFn'>,
16
- AdditionalCreateOptions<any, any> {
12
+ AdditionalQueryOptions<any, any> {
17
13
  use?: Middleware[]
14
+ variables?: any
18
15
  }
19
16
 
20
17
  type QueryBaseHookOptions = Omit<
@@ -24,20 +21,11 @@ type QueryBaseHookOptions = Omit<
24
21
  variables?: any
25
22
  }
26
23
 
27
- export function createBaseQuery(
24
+ export const createBaseQuery = (
28
25
  defaultOptions: any,
29
26
  useRQHook: (options: any, queryClient?: any) => any,
30
27
  overrideOptions?: QueryBaseHookOptions
31
- ): any {
32
- const {
33
- primaryKey,
34
- queryFn,
35
- queryKeyHashFn,
36
- getPreviousPageParam,
37
- getNextPageParam,
38
- initialPageParam,
39
- } = defaultOptions as CreateBaseQueryOptions
40
-
28
+ ): any => {
41
29
  if (process.env.NODE_ENV !== 'production') {
42
30
  // @ts-ignore
43
31
  if (defaultOptions.useDefaultOptions) {
@@ -47,6 +35,15 @@ export function createBaseQuery(
47
35
  }
48
36
  }
49
37
 
38
+ const {
39
+ primaryKey,
40
+ queryFn,
41
+ queryKeyHashFn,
42
+ getPreviousPageParam,
43
+ getNextPageParam,
44
+ initialPageParam,
45
+ } = defaultOptions as CreateBaseQueryOptions
46
+
50
47
  const getPrimaryKey = () => primaryKey
51
48
 
52
49
  const getKey = (variables?: any) => getQueryKey(primaryKey, variables)
@@ -63,12 +60,12 @@ export function createBaseQuery(
63
60
  }
64
61
 
65
62
  const useBaseHook = (
66
- { variables, ...rest }: QueryBaseHookOptions,
67
- queryClient?: CompatibleWithV4<QueryClient, void>
63
+ { variables, ...restOptions }: QueryBaseHookOptions,
64
+ queryClient?: QueryClient
68
65
  ) => {
69
66
  return useRQHook(
70
67
  {
71
- ...rest,
68
+ ...restOptions,
72
69
  ...overrideOptions,
73
70
  queryKey: getKey(variables),
74
71
  },
@@ -1,16 +1,12 @@
1
1
  import { useInfiniteQuery } from '@tanstack/react-query'
2
2
 
3
3
  import { createBaseQuery } from './createBaseQuery'
4
- import type {
5
- CreateInfiniteQueryOptions,
6
- DefaultError,
7
- InfiniteQueryHook,
8
- } from './types'
4
+ import type { CreateInfiniteQueryOptions, InfiniteQueryHook } from './types'
9
5
 
10
6
  export const createInfiniteQuery = <
11
7
  TFnData,
12
8
  TVariables = any,
13
- TError = DefaultError,
9
+ TError = Error,
14
10
  TPageParam = number
15
11
  >(
16
12
  options: CreateInfiniteQueryOptions<TFnData, TVariables, TError, TPageParam>
@@ -1,12 +1,12 @@
1
1
  import { useMutation } from '@tanstack/react-query'
2
2
 
3
- import type { CreateMutationOptions, DefaultError, MutationHook } from './types'
3
+ import type { CreateMutationOptions, MutationHook } from './types'
4
4
  import { withMiddleware } from './utils'
5
5
 
6
6
  export const createMutation = <
7
7
  TData = unknown,
8
8
  TVariables = void,
9
- TError = DefaultError,
9
+ TError = Error,
10
10
  TContext = unknown
11
11
  >(
12
12
  defaultOptions: CreateMutationOptions<TData, TVariables, TError, TContext>
@@ -1,9 +1,9 @@
1
1
  import { useQuery } from '@tanstack/react-query'
2
2
 
3
3
  import { createBaseQuery } from './createBaseQuery'
4
- import type { CreateQueryOptions, DefaultError, QueryHook } from './types'
4
+ import type { CreateQueryOptions, QueryHook } from './types'
5
5
 
6
- export const createQuery = <TFnData, TVariables = any, TError = DefaultError>(
6
+ export const createQuery = <TFnData, TVariables = any, TError = Error>(
7
7
  options: CreateQueryOptions<TFnData, TVariables, TError>
8
8
  ): QueryHook<TFnData, TVariables, TError> => {
9
9
  return createBaseQuery(options, useQuery)
@@ -3,14 +3,14 @@ import { useInfiniteQuery } from '@tanstack/react-query'
3
3
  import { createBaseQuery } from './createBaseQuery'
4
4
  import type {
5
5
  CreateSuspenseInfiniteQueryOptions,
6
- DefaultError,
7
6
  SuspenseInfiniteQueryHook,
8
7
  } from './types'
8
+ import { suspenseOptions } from './utils'
9
9
 
10
10
  export const createSuspenseInfiniteQuery = <
11
11
  TFnData,
12
12
  TVariables = any,
13
- TError = DefaultError,
13
+ TError = Error,
14
14
  TPageParam = number
15
15
  >(
16
16
  options: CreateSuspenseInfiniteQueryOptions<
@@ -20,12 +20,5 @@ export const createSuspenseInfiniteQuery = <
20
20
  TPageParam
21
21
  >
22
22
  ): SuspenseInfiniteQueryHook<TFnData, TVariables, TError, TPageParam> => {
23
- return createBaseQuery(options, useInfiniteQuery, {
24
- enabled: true,
25
- suspense: true,
26
- throwOnError: true,
27
- // compatible with ReactQuery v4
28
- // @ts-ignore
29
- useErrorBoundary: true,
30
- })
23
+ return createBaseQuery(options, useInfiniteQuery, suspenseOptions)
31
24
  }
@@ -1,22 +1,11 @@
1
- import type { DefaultError } from '@tanstack/react-query'
2
1
  import { useQuery } from '@tanstack/react-query'
3
2
 
4
3
  import { createBaseQuery } from './createBaseQuery'
5
4
  import type { CreateSuspenseQueryOptions, SuspenseQueryHook } from './types'
5
+ import { suspenseOptions } from './utils'
6
6
 
7
- export const createSuspenseQuery = <
8
- TFnData,
9
- TVariables = any,
10
- TError = DefaultError
11
- >(
7
+ export const createSuspenseQuery = <TFnData, TVariables = any, TError = Error>(
12
8
  options: CreateSuspenseQueryOptions<TFnData, TVariables, TError>
13
9
  ): SuspenseQueryHook<TFnData, TVariables, TError> => {
14
- return createBaseQuery(options, useQuery, {
15
- enabled: true,
16
- suspense: true,
17
- throwOnError: true,
18
- // compatible with ReactQuery v4
19
- // @ts-ignore
20
- useErrorBoundary: true,
21
- })
10
+ return createBaseQuery(options, useQuery, suspenseOptions)
22
11
  }
package/src/index.ts CHANGED
@@ -3,32 +3,5 @@ export * from './createSuspenseQuery'
3
3
  export * from './createInfiniteQuery'
4
4
  export * from './createSuspenseInfiniteQuery'
5
5
  export * from './createMutation'
6
+ export * from './types'
6
7
  export { getKey } from './utils'
7
- export type {
8
- CreateQueryOptions,
9
- QueryHookOptions,
10
- QueryHook,
11
- QueryHookResult,
12
- CreateInfiniteQueryOptions,
13
- InfiniteQueryHookOptions,
14
- InfiniteQueryHook,
15
- InfiniteQueryHookResult,
16
- CreateSuspenseQueryOptions,
17
- SuspenseQueryHook,
18
- SuspenseQueryHookOptions,
19
- SuspenseQueryHookResult,
20
- CreateSuspenseInfiniteQueryOptions,
21
- SuspenseInfiniteQueryHook,
22
- SuspenseInfiniteQueryHookOptions,
23
- SuspenseInfiniteQueryHookResult,
24
- CreateMutationOptions,
25
- MutationHookOptions,
26
- MutationHook,
27
- inferVariables,
28
- inferData,
29
- inferFnData,
30
- inferOptions,
31
- inferError,
32
- inferCreateOptions,
33
- Middleware,
34
- } from './types'
package/src/types.ts CHANGED
@@ -20,16 +20,14 @@ import type {
20
20
 
21
21
  // utils
22
22
 
23
- export type DefaultError = Error
24
-
25
- export type CompatibleWithV4<V5, V4> =
23
+ type CompatibleWithV4<V5, V4> =
26
24
  InfiniteData<unknown> extends UseInfiniteQueryResult<
27
25
  InfiniteData<unknown>
28
26
  >['data']
29
27
  ? V5
30
28
  : V4
31
29
 
32
- export type CompatibleQueryFunction<
30
+ type CompatibleQueryFunction<
33
31
  T = unknown,
34
32
  TQueryKey extends QueryKey = QueryKey,
35
33
  TPageParam = unknown
@@ -38,7 +36,7 @@ export type CompatibleQueryFunction<
38
36
  QueryFunction<T, TQueryKey>
39
37
  >
40
38
 
41
- export type CompatibleInfiniteQueryPageParamsOptions<
39
+ type CompatibleInfiniteQueryPageParamsOptions<
42
40
  TQueryFnData = unknown,
43
41
  TPageParam = unknown
44
42
  > = CompatibleWithV4<
@@ -56,7 +54,7 @@ export type CompatibleInfiniteQueryPageParamsOptions<
56
54
  }
57
55
  >
58
56
 
59
- export type CompatibleUseInfiniteQueryOptions<
57
+ type CompatibleUseInfiniteQueryOptions<
60
58
  TFnData,
61
59
  TVariables,
62
60
  TData,
@@ -84,7 +82,7 @@ export type inferQueryKey<TVariables> = TVariables extends void
84
82
  ? [string]
85
83
  : [string, TVariables]
86
84
 
87
- export type AdditionalCreateOptions<TFnData, TVariables, TPageParam = never> = {
85
+ export type AdditionalQueryOptions<TFnData, TVariables, TPageParam = never> = {
88
86
  primaryKey: string
89
87
  queryFn: CompatibleQueryFunction<
90
88
  TFnData,
@@ -103,7 +101,7 @@ export type Middleware<
103
101
  T extends (...args: any) => any = QueryHook<any, any, any>
104
102
  > = (hook: inferMiddlewareHook<T>) => inferMiddlewareHook<T>
105
103
 
106
- export type DeepPartial<T> = T extends object
104
+ type DeepPartial<T> = T extends object
107
105
  ? {
108
106
  [P in keyof T]?: DeepPartial<T[P]>
109
107
  }
@@ -148,12 +146,12 @@ export type ExposeMethods<TFnData, TVariables, TPageParam = never> = {
148
146
  export interface CreateQueryOptions<
149
147
  TFnData = unknown,
150
148
  TVariables = any,
151
- TError = DefaultError
149
+ TError = Error
152
150
  > extends Omit<
153
151
  UseQueryOptions<TFnData, TError, TFnData, inferQueryKey<TVariables>>,
154
152
  'queryKey' | 'queryFn' | 'select'
155
153
  >,
156
- AdditionalCreateOptions<TFnData, TVariables> {
154
+ AdditionalQueryOptions<TFnData, TVariables> {
157
155
  use?: Middleware<QueryHook<TFnData, TVariables, TError>>[]
158
156
  variables?: TVariables
159
157
  }
@@ -169,11 +167,8 @@ export interface QueryHookOptions<TFnData, TError, TData, TVariables>
169
167
 
170
168
  export type QueryHookResult<TData, TError> = UseQueryResult<TData, TError>
171
169
 
172
- export interface QueryHook<
173
- TFnData = unknown,
174
- TVariables = any,
175
- TError = DefaultError
176
- > extends ExposeMethods<TFnData, TVariables> {
170
+ export interface QueryHook<TFnData = unknown, TVariables = any, TError = Error>
171
+ extends ExposeMethods<TFnData, TVariables> {
177
172
  <TData = TFnData>(
178
173
  options?: QueryHookOptions<TFnData, TError, TData, TVariables>,
179
174
  queryClient?: CompatibleWithV4<QueryClient, void>
@@ -185,7 +180,7 @@ export interface QueryHook<
185
180
  export interface CreateSuspenseQueryOptions<
186
181
  TFnData = unknown,
187
182
  TVariables = any,
188
- TError = DefaultError
183
+ TError = Error
189
184
  > extends Omit<
190
185
  UseQueryOptions<TFnData, TError, TFnData, inferQueryKey<TVariables>>,
191
186
  | 'queryKey'
@@ -198,7 +193,7 @@ export interface CreateSuspenseQueryOptions<
198
193
  | 'keepPreviousData'
199
194
  | 'useErrorBoundary'
200
195
  >,
201
- AdditionalCreateOptions<TFnData, TVariables> {
196
+ AdditionalQueryOptions<TFnData, TVariables> {
202
197
  use?: Middleware<SuspenseQueryHook<TFnData, TVariables, TVariables>>[]
203
198
  }
204
199
 
@@ -227,7 +222,7 @@ export type SuspenseQueryHookResult<TData, TError> = Omit<
227
222
  export interface SuspenseQueryHook<
228
223
  TFnData = unknown,
229
224
  TVariables = any,
230
- TError = DefaultError
225
+ TError = Error
231
226
  > extends ExposeMethods<TFnData, TVariables> {
232
227
  <TData = TFnData>(
233
228
  options?: SuspenseQueryHookOptions<TFnData, TError, TData, TVariables>,
@@ -240,7 +235,7 @@ export interface SuspenseQueryHook<
240
235
  export interface CreateInfiniteQueryOptions<
241
236
  TFnData = unknown,
242
237
  TVariables = any,
243
- TError = DefaultError,
238
+ TError = Error,
244
239
  TPageParam = number
245
240
  > extends Omit<
246
241
  CompatibleUseInfiniteQueryOptions<
@@ -252,7 +247,7 @@ export interface CreateInfiniteQueryOptions<
252
247
  >,
253
248
  'queryKey' | 'queryFn' | 'select'
254
249
  >,
255
- AdditionalCreateOptions<TFnData, TVariables, TPageParam> {
250
+ AdditionalQueryOptions<TFnData, TVariables, TPageParam> {
256
251
  use?: Middleware<InfiniteQueryHook<TFnData, TVariables, TError, TPageParam>>[]
257
252
  }
258
253
 
@@ -289,7 +284,7 @@ export type InfiniteQueryHookResult<TData, TError> = UseInfiniteQueryResult<
289
284
  export interface InfiniteQueryHook<
290
285
  TFnData = unknown,
291
286
  TVariables = any,
292
- TError = DefaultError,
287
+ TError = Error,
293
288
  TPageParam = number
294
289
  > extends ExposeMethods<TFnData, TVariables, TPageParam> {
295
290
  <TData = CompatibleWithV4<InfiniteData<TFnData, TPageParam>, TFnData>>(
@@ -309,7 +304,7 @@ export interface InfiniteQueryHook<
309
304
  export interface CreateSuspenseInfiniteQueryOptions<
310
305
  TFnData = unknown,
311
306
  TVariables = any,
312
- TError = DefaultError,
307
+ TError = Error,
313
308
  TPageParam = number
314
309
  > extends Omit<
315
310
  CompatibleUseInfiniteQueryOptions<
@@ -329,7 +324,7 @@ export interface CreateSuspenseInfiniteQueryOptions<
329
324
  | 'keepPreviousData'
330
325
  | 'useErrorBoundary'
331
326
  >,
332
- AdditionalCreateOptions<TFnData, TVariables, TPageParam> {
327
+ AdditionalQueryOptions<TFnData, TVariables, TPageParam> {
333
328
  use?: Middleware<SuspenseInfiniteQueryHook<TFnData, TVariables, TVariables>>[]
334
329
  }
335
330
 
@@ -372,7 +367,7 @@ export type SuspenseInfiniteQueryHookResult<TData, TError> = Omit<
372
367
  export interface SuspenseInfiniteQueryHook<
373
368
  TFnData = unknown,
374
369
  TVariables = any,
375
- TError = DefaultError,
370
+ TError = Error,
376
371
  TPageParam = number
377
372
  > extends ExposeMethods<TFnData, TVariables, TPageParam> {
378
373
  <TData = CompatibleWithV4<InfiniteData<TFnData, TPageParam>, TFnData>>(
@@ -392,7 +387,7 @@ export interface SuspenseInfiniteQueryHook<
392
387
  export interface CreateMutationOptions<
393
388
  TData = unknown,
394
389
  TVariables = void,
395
- TError = DefaultError,
390
+ TError = Error,
396
391
  TContext = unknown
397
392
  > extends UseMutationOptions<TData, TError, TVariables, TContext> {
398
393
  use?: Middleware<MutationHook<TData, TError, TVariables>>[]
@@ -407,6 +402,13 @@ export interface MutationHookOptions<TData, TError, TVariables, TContext>
407
402
  variables?: TVariables
408
403
  }
409
404
 
405
+ export type MutationHookResult<
406
+ TData = unknown,
407
+ TError = Error,
408
+ TVariables = unknown,
409
+ TContext = unknown
410
+ > = UseMutationResult<TData, TError, TVariables, TContext>
411
+
410
412
  export interface MutationHook<
411
413
  TData = unknown,
412
414
  TVariables = any,
@@ -415,7 +417,7 @@ export interface MutationHook<
415
417
  <TContext>(
416
418
  options?: MutationHookOptions<TData, TError, TVariables, TContext>,
417
419
  queryClient?: CompatibleWithV4<QueryClient, void>
418
- ): UseMutationResult<TData, TError, TVariables, TContext>
420
+ ): MutationHookResult<TData, TError, TVariables, TContext>
419
421
  getKey: () => MutationKey | undefined
420
422
  mutationFn: MutationFunction<TData, TVariables>
421
423
  }
package/src/utils.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import { QueryClient, useQueryClient } from '@tanstack/react-query'
2
2
 
3
- import { CompatibleWithV4, Middleware, inferQueryKey } from './types'
3
+ import { Middleware, inferQueryKey } from './types'
4
4
 
5
5
  export const withMiddleware = (
6
6
  hook: any,
@@ -9,7 +9,7 @@ export const withMiddleware = (
9
9
  ) => {
10
10
  return function useMiddleware(
11
11
  options?: { client?: QueryClient; use?: Middleware[] },
12
- queryClient?: CompatibleWithV4<QueryClient, void>
12
+ queryClient?: QueryClient
13
13
  ) {
14
14
  const [middleware, opts] = [
15
15
  useQueryClient(
@@ -20,9 +20,9 @@ export const withMiddleware = (
20
20
  defaultOptions,
21
21
  options,
22
22
  ].reduce(
23
- ([middleware, opts], { use = [], ...rest } = {}) => [
24
- [...middleware, ...use],
25
- { ...opts, ...rest },
23
+ ([_middleware, _opts], { use = [], ...rest } = {}) => [
24
+ [..._middleware, ...use],
25
+ { ..._opts, ...rest },
26
26
  ],
27
27
  [[], {}]
28
28
  )
@@ -36,6 +36,13 @@ export const withMiddleware = (
36
36
  }
37
37
  }
38
38
 
39
+ export const suspenseOptions = {
40
+ enabled: true,
41
+ suspense: true,
42
+ throwOnError: true,
43
+ useErrorBoundary: true,
44
+ }
45
+
39
46
  export const getKey = <TVariables>(
40
47
  primaryKey: string,
41
48
  variables?: TVariables