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.
- package/README.md +28 -22
- package/build/lib/createBaseQuery.esm.js +24 -13
- package/build/lib/createBaseQuery.esm.js.map +1 -1
- package/build/lib/createBaseQuery.js +24 -13
- package/build/lib/createBaseQuery.js.map +1 -1
- package/build/lib/createBaseQuery.mjs +24 -13
- package/build/lib/createBaseQuery.mjs.map +1 -1
- package/build/lib/types.d.ts +1 -0
- package/build/lib/utils.d.ts +3 -3
- package/build/lib/utils.esm.js +7 -10
- package/build/lib/utils.esm.js.map +1 -1
- package/build/lib/utils.js +7 -10
- package/build/lib/utils.js.map +1 -1
- package/build/lib/utils.mjs +7 -10
- package/build/lib/utils.mjs.map +1 -1
- package/build/stats-html.html +1 -1
- package/build/stats.json +88 -88
- package/build/umd/index.development.js +31 -23
- package/build/umd/index.development.js.map +1 -1
- package/build/umd/index.production.js +1 -1
- package/build/umd/index.production.js.map +1 -1
- package/build/zip/zip.esm.js +1 -1
- package/package.json +1 -1
- package/src/createBaseQuery.ts +28 -12
- package/src/types.ts +3 -0
- package/src/utils.ts +9 -10
package/src/createBaseQuery.ts
CHANGED
|
@@ -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
|
-
|
|
78
|
+
options: QueryBaseHookOptions,
|
|
64
79
|
queryClient?: QueryClient
|
|
65
80
|
) => {
|
|
66
81
|
return useRQHook(
|
|
67
82
|
{
|
|
68
|
-
...
|
|
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
|
-
//
|
|
17
|
-
|
|
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
|
-
([
|
|
24
|
-
[...
|
|
25
|
-
{ ...
|
|
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
|
) => {
|