react-query-kit 2.0.3 → 2.0.4

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.
@@ -21,20 +21,13 @@ type QueryBaseHookOptions = Omit<
21
21
  variables?: any
22
22
  }
23
23
 
24
+ const existedPrimaryKeys = new Set<string>()
25
+
24
26
  export const createBaseQuery = (
25
27
  defaultOptions: any,
26
28
  useRQHook: (options: any, queryClient?: any) => any,
27
29
  overrideOptions?: QueryBaseHookOptions
28
30
  ): any => {
29
- if (process.env.NODE_ENV !== 'production') {
30
- // @ts-ignore
31
- if (defaultOptions.useDefaultOptions) {
32
- throw new Error(
33
- 'useDefaultOptions is not supported, please use middleware instead.'
34
- )
35
- }
36
- }
37
-
38
31
  const {
39
32
  primaryKey,
40
33
  queryFn,
@@ -44,10 +37,32 @@ export const createBaseQuery = (
44
37
  initialPageParam,
45
38
  } = defaultOptions as CreateBaseQueryOptions
46
39
 
40
+ if (process.env.NODE_ENV !== 'production') {
41
+ if (existedPrimaryKeys.has(primaryKey)) {
42
+ console.error(`[Bug] Duplicated primaryKey: ${primaryKey}`)
43
+ } else {
44
+ existedPrimaryKeys.add(primaryKey)
45
+ }
46
+
47
+ // @ts-ignore
48
+ if (defaultOptions.useDefaultOptions) {
49
+ console.error(
50
+ '[Bug] useDefaultOptions is not supported, please use middleware instead.'
51
+ )
52
+ }
53
+ }
54
+
47
55
  const getPrimaryKey = () => primaryKey
48
56
 
49
57
  const getKey = (variables?: any) => getQueryKey(primaryKey, variables)
50
58
 
59
+ const getOptions = (variables: any) => {
60
+ return {
61
+ ...defaultOptions,
62
+ queryKey: getKey(variables),
63
+ }
64
+ }
65
+
51
66
  const getFetchOptions = (variables: any) => {
52
67
  return {
53
68
  queryKey: getKey(variables),
@@ -60,14 +75,14 @@ export const createBaseQuery = (
60
75
  }
61
76
 
62
77
  const useBaseHook = (
63
- { variables, ...restOptions }: QueryBaseHookOptions,
78
+ options: QueryBaseHookOptions,
64
79
  queryClient?: QueryClient
65
80
  ) => {
66
81
  return useRQHook(
67
82
  {
68
- ...restOptions,
83
+ ...options,
69
84
  ...overrideOptions,
70
- queryKey: getKey(variables),
85
+ queryKey: getKey(options.variables),
71
86
  },
72
87
  queryClient
73
88
  )
@@ -78,6 +93,7 @@ export const createBaseQuery = (
78
93
  getKey,
79
94
  queryFn,
80
95
  queryKeyHashFn,
96
+ getOptions,
81
97
  getFetchOptions,
82
98
  })
83
99
  }
package/src/types.ts CHANGED
@@ -139,6 +139,9 @@ export type ExposeMethods<TFnData, TVariables, TPageParam = never> = {
139
139
  >
140
140
  queryKeyHashFn?: QueryKeyHashFunction<inferQueryKey<TVariables>>
141
141
  } & CompatibleInfiniteQueryPageParamsOptions<TFnData, TPageParam>
142
+ // TODO This seems pretty hacky for select & useQueries.
143
+ // But not work to infer TError, Hope to fix it.
144
+ getOptions: ExposeMethods<TFnData, TVariables, TPageParam>['getFetchOptions']
142
145
  }
143
146
 
144
147
  // query hook
package/src/utils.ts CHANGED
@@ -1,6 +1,6 @@
1
- import { QueryClient, useQueryClient } from '@tanstack/react-query'
1
+ import { type QueryClient, useQueryClient } from '@tanstack/react-query'
2
2
 
3
- import { Middleware, inferQueryKey } from './types'
3
+ import type { Middleware, inferQueryKey } from './types'
4
4
 
5
5
  export const withMiddleware = (
6
6
  hook: any,
@@ -13,18 +13,17 @@ export const withMiddleware = (
13
13
  ) {
14
14
  const [middleware, opts] = [
15
15
  useQueryClient(
16
- // compatible with ReactQuery v4
17
- // @ts-ignore
18
- options?.context ? { context: options.context } : queryClient
16
+ // @ts-ignore Compatible with ReactQuery v4
17
+ options?.context ? options : queryClient
19
18
  ).getDefaultOptions()[type],
20
19
  defaultOptions,
21
20
  options,
22
21
  ].reduce(
23
- ([_middleware, _opts], { use = [], ...rest } = {}) => [
24
- [..._middleware, ...use],
25
- { ..._opts, ...rest },
22
+ ([u1, o1], { use: u2 = [], ...o2 } = {}) => [
23
+ [...u1, ...u2],
24
+ { ...o1, ...o2 },
26
25
  ],
27
- [[], {}]
26
+ [[]]
28
27
  )
29
28
 
30
29
  let next = hook
@@ -43,7 +42,7 @@ export const suspenseOptions = {
43
42
  useErrorBoundary: true,
44
43
  }
45
44
 
46
- export const getKey = <TVariables>(
45
+ export const getKey = <TVariables = void>(
47
46
  primaryKey: string,
48
47
  variables?: TVariables
49
48
  ) => {