react-query-kit 2.0.2 → 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 +34 -29
- package/build/lib/createBaseQuery.d.ts +1 -1
- package/build/lib/createBaseQuery.esm.js +21 -10
- package/build/lib/createBaseQuery.esm.js.map +1 -1
- package/build/lib/createBaseQuery.js +21 -10
- package/build/lib/createBaseQuery.js.map +1 -1
- package/build/lib/createBaseQuery.mjs +21 -10
- package/build/lib/createBaseQuery.mjs.map +1 -1
- package/build/lib/createInfiniteQuery.esm.js.map +1 -1
- package/build/lib/createInfiniteQuery.js.map +1 -1
- package/build/lib/createInfiniteQuery.mjs.map +1 -1
- package/build/lib/createMutation.esm.js.map +1 -1
- package/build/lib/createMutation.js.map +1 -1
- package/build/lib/createMutation.mjs.map +1 -1
- package/build/lib/createQuery.esm.js.map +1 -1
- package/build/lib/createQuery.js.map +1 -1
- package/build/lib/createQuery.mjs.map +1 -1
- package/build/lib/createSuspenseInfiniteQuery.esm.js +2 -8
- package/build/lib/createSuspenseInfiniteQuery.esm.js.map +1 -1
- package/build/lib/createSuspenseInfiniteQuery.js +2 -8
- package/build/lib/createSuspenseInfiniteQuery.js.map +1 -1
- package/build/lib/createSuspenseInfiniteQuery.mjs +2 -8
- package/build/lib/createSuspenseInfiniteQuery.mjs.map +1 -1
- package/build/lib/createSuspenseQuery.esm.js +2 -8
- package/build/lib/createSuspenseQuery.esm.js.map +1 -1
- package/build/lib/createSuspenseQuery.js +2 -8
- package/build/lib/createSuspenseQuery.js.map +1 -1
- package/build/lib/createSuspenseQuery.mjs +2 -8
- package/build/lib/createSuspenseQuery.mjs.map +1 -1
- package/build/lib/index.d.ts +1 -1
- package/build/lib/types.d.ts +18 -17
- package/build/lib/utils.d.ts +10 -4
- package/build/lib/utils.esm.js +14 -11
- package/build/lib/utils.esm.js.map +1 -1
- package/build/lib/utils.js +14 -10
- package/build/lib/utils.js.map +1 -1
- package/build/lib/utils.mjs +14 -11
- package/build/lib/utils.mjs.map +1 -1
- package/build/stats-html.html +1 -1
- package/build/stats.json +117 -92
- package/build/umd/index.development.js +36 -36
- 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 -0
- package/package.json +1 -1
- package/src/createBaseQuery.ts +27 -14
- package/src/createInfiniteQuery.ts +2 -6
- package/src/createMutation.ts +2 -2
- package/src/createQuery.tsx +2 -2
- package/src/createSuspenseInfiniteQuery.ts +3 -10
- package/src/createSuspenseQuery.ts +3 -14
- package/src/index.ts +1 -28
- package/src/types.ts +31 -26
- package/src/utils.ts +17 -11
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.production.js","sources":["../../src/utils.ts","../../src/createBaseQuery.ts","../../src/createInfiniteQuery.ts","../../src/createMutation.ts","../../src/createQuery.tsx","../../src/createSuspenseInfiniteQuery.ts","../../src/createSuspenseQuery.ts"],"sourcesContent":["import { QueryClient, useQueryClient } from '@tanstack/react-query'\n\nimport {
|
|
1
|
+
{"version":3,"file":"index.production.js","sources":["../../src/utils.ts","../../src/createBaseQuery.ts","../../src/createInfiniteQuery.ts","../../src/createMutation.ts","../../src/createQuery.tsx","../../src/createSuspenseInfiniteQuery.ts","../../src/createSuspenseQuery.ts"],"sourcesContent":["import { type QueryClient, useQueryClient } from '@tanstack/react-query'\n\nimport type { Middleware, inferQueryKey } from './types'\n\nexport const withMiddleware = (\n hook: any,\n defaultOptions: any,\n type: 'queries' | 'mutations'\n) => {\n return function useMiddleware(\n options?: { client?: QueryClient; use?: Middleware[] },\n queryClient?: QueryClient\n ) {\n const [middleware, opts] = [\n useQueryClient(\n // @ts-ignore Compatible with ReactQuery v4\n options?.context ? options : queryClient\n ).getDefaultOptions()[type],\n defaultOptions,\n options,\n ].reduce(\n ([u1, o1], { use: u2 = [], ...o2 } = {}) => [\n [...u1, ...u2],\n { ...o1, ...o2 },\n ],\n [[]]\n )\n\n let next = hook\n for (let i = middleware.length; i--; ) {\n next = middleware[i](next)\n }\n\n return next(opts, queryClient)\n }\n}\n\nexport const suspenseOptions = {\n enabled: true,\n suspense: true,\n throwOnError: true,\n useErrorBoundary: true,\n}\n\nexport const getKey = <TVariables = void>(\n primaryKey: string,\n variables?: TVariables\n) => {\n return (\n variables === undefined ? [primaryKey] : [primaryKey, variables]\n ) as inferQueryKey<TVariables>\n}\n","import type {\n QueryClient,\n UseBaseQueryOptions,\n UseInfiniteQueryOptions,\n} from '@tanstack/react-query'\n\nimport type { AdditionalQueryOptions, Middleware } from './types'\nimport { getKey as getQueryKey, withMiddleware } from './utils'\n\ninterface CreateBaseQueryOptions\n extends Omit<UseInfiniteQueryOptions, 'queryKey' | 'queryFn'>,\n AdditionalQueryOptions<any, any> {\n use?: Middleware[]\n variables?: any\n}\n\ntype QueryBaseHookOptions = Omit<\n UseBaseQueryOptions,\n 'queryKey' | 'queryFn'\n> & {\n variables?: any\n}\n\nconst existedPrimaryKeys = new Set<string>()\n\nexport const createBaseQuery = (\n defaultOptions: any,\n useRQHook: (options: any, queryClient?: any) => any,\n overrideOptions?: QueryBaseHookOptions\n): any => {\n const {\n primaryKey,\n queryFn,\n queryKeyHashFn,\n getPreviousPageParam,\n getNextPageParam,\n initialPageParam,\n } = defaultOptions as CreateBaseQueryOptions\n\n if (process.env.NODE_ENV !== 'production') {\n if (existedPrimaryKeys.has(primaryKey)) {\n console.error(`[Bug] Duplicated primaryKey: ${primaryKey}`)\n } else {\n existedPrimaryKeys.add(primaryKey)\n }\n\n // @ts-ignore\n if (defaultOptions.useDefaultOptions) {\n console.error(\n '[Bug] useDefaultOptions is not supported, please use middleware instead.'\n )\n }\n }\n\n const getPrimaryKey = () => primaryKey\n\n const getKey = (variables?: any) => getQueryKey(primaryKey, variables)\n\n const getOptions = (variables: any) => {\n return {\n ...defaultOptions,\n queryKey: getKey(variables),\n }\n }\n\n const getFetchOptions = (variables: any) => {\n return {\n queryKey: getKey(variables),\n queryFn,\n queryKeyHashFn,\n getPreviousPageParam,\n getNextPageParam,\n initialPageParam,\n }\n }\n\n const useBaseHook = (\n options: QueryBaseHookOptions,\n queryClient?: QueryClient\n ) => {\n return useRQHook(\n {\n ...options,\n ...overrideOptions,\n queryKey: getKey(options.variables),\n },\n queryClient\n )\n }\n\n return Object.assign(withMiddleware(useBaseHook, defaultOptions, 'queries'), {\n getPrimaryKey,\n getKey,\n queryFn,\n queryKeyHashFn,\n getOptions,\n getFetchOptions,\n })\n}\n","import { useInfiniteQuery } from '@tanstack/react-query'\n\nimport { createBaseQuery } from './createBaseQuery'\nimport type { CreateInfiniteQueryOptions, InfiniteQueryHook } from './types'\n\nexport const createInfiniteQuery = <\n TFnData,\n TVariables = any,\n TError = Error,\n TPageParam = number\n>(\n options: CreateInfiniteQueryOptions<TFnData, TVariables, TError, TPageParam>\n): InfiniteQueryHook<TFnData, TVariables, TError, TPageParam> => {\n return createBaseQuery(options, useInfiniteQuery)\n}\n","import { useMutation } from '@tanstack/react-query'\n\nimport type { CreateMutationOptions, MutationHook } from './types'\nimport { withMiddleware } from './utils'\n\nexport const createMutation = <\n TData = unknown,\n TVariables = void,\n TError = Error,\n TContext = unknown\n>(\n defaultOptions: CreateMutationOptions<TData, TVariables, TError, TContext>\n): MutationHook<TData, TVariables, TError> => {\n const getKey = () => defaultOptions.mutationKey\n\n return Object.assign(\n withMiddleware(useMutation, defaultOptions, 'mutations'),\n {\n getKey,\n mutationFn: defaultOptions.mutationFn,\n }\n ) as MutationHook<TData, TVariables, TError>\n}\n","import { useQuery } from '@tanstack/react-query'\n\nimport { createBaseQuery } from './createBaseQuery'\nimport type { CreateQueryOptions, QueryHook } from './types'\n\nexport const createQuery = <TFnData, TVariables = any, TError = Error>(\n options: CreateQueryOptions<TFnData, TVariables, TError>\n): QueryHook<TFnData, TVariables, TError> => {\n return createBaseQuery(options, useQuery)\n}\n","import { useInfiniteQuery } from '@tanstack/react-query'\n\nimport { createBaseQuery } from './createBaseQuery'\nimport type {\n CreateSuspenseInfiniteQueryOptions,\n SuspenseInfiniteQueryHook,\n} from './types'\nimport { suspenseOptions } from './utils'\n\nexport const createSuspenseInfiniteQuery = <\n TFnData,\n TVariables = any,\n TError = Error,\n TPageParam = number\n>(\n options: CreateSuspenseInfiniteQueryOptions<\n TFnData,\n TVariables,\n TError,\n TPageParam\n >\n): SuspenseInfiniteQueryHook<TFnData, TVariables, TError, TPageParam> => {\n return createBaseQuery(options, useInfiniteQuery, suspenseOptions)\n}\n","import { useQuery } from '@tanstack/react-query'\n\nimport { createBaseQuery } from './createBaseQuery'\nimport type { CreateSuspenseQueryOptions, SuspenseQueryHook } from './types'\nimport { suspenseOptions } from './utils'\n\nexport const createSuspenseQuery = <TFnData, TVariables = any, TError = Error>(\n options: CreateSuspenseQueryOptions<TFnData, TVariables, TError>\n): SuspenseQueryHook<TFnData, TVariables, TError> => {\n return createBaseQuery(options, useQuery, suspenseOptions)\n}\n"],"names":["withMiddleware","hook","defaultOptions","type","options","queryClient","middleware","opts","useQueryClient","context","getDefaultOptions","reduce","u1","o1","use","u2","o2","next","i","length","suspenseOptions","enabled","suspense","throwOnError","useErrorBoundary","getKey","primaryKey","variables","undefined","createBaseQuery","useRQHook","overrideOptions","queryFn","queryKeyHashFn","getPreviousPageParam","getNextPageParam","initialPageParam","getQueryKey","Object","assign","queryKey","getPrimaryKey","getOptions","getFetchOptions","useInfiniteQuery","useMutation","mutationKey","mutationFn","useQuery"],"mappings":"6TAIO,MAAMA,EAAiB,CAC5BC,EACAC,EACAC,IAEO,SACLC,EACAC,GAEA,MAAOC,EAAYC,GAAQ,CACzBC,EAAcA,eAEZ,MAAAJ,GAAAA,EAASK,QAAUL,EAAUC,GAC7BK,oBAAoBP,GACtBD,EACAE,GACAO,QACA,EAAEC,EAAIC,IAAOC,IAAKC,EAAK,MAAOC,GAAO,CAAA,IAAO,CAC1C,IAAIJ,KAAOG,GACX,IAAKF,KAAOG,KAEd,CAAC,KAGH,IAAIC,EAAOhB,EACX,IAAK,IAAIiB,EAAIZ,EAAWa,OAAQD,KAC9BD,EAAOX,EAAWY,GAAGD,GAGvB,OAAOA,EAAKV,EAAMF,IAITe,EAAkB,CAC7BC,SAAS,EACTC,UAAU,EACVC,cAAc,EACdC,kBAAkB,GAGPC,EAAS,CACpBC,EACAC,SAGgBC,IAAdD,EAA0B,CAACD,GAAc,CAACA,EAAYC,GCxB7CE,EAAkB,CAC7B3B,EACA4B,EACAC,KAEA,MAAML,WACJA,EADIM,QAEJA,EAFIC,eAGJA,EAHIC,qBAIJA,EAJIC,iBAKJA,EALIC,iBAMJA,GACElC,EAmBEuB,EAAUE,GAAoBU,EAAYX,EAAYC,GAkC5D,OAAOW,OAAOC,OAAOvC,GAdD,CAClBI,EACAC,IAEOyB,EACL,IACK1B,KACA2B,EACHS,SAAUf,EAAOrB,EAAQuB,YAE3BtB,IAI6CH,EAAgB,WAAY,CAC3EuC,cArCoB,IAAMf,SAsC1BD,EACAO,UACAC,iBACAS,WArCkBf,IACX,IACFzB,EACHsC,SAAUf,EAAOE,KAmCnBgB,gBA/BuBhB,IAChB,CACLa,SAAUf,EAAOE,GACjBK,UACAC,iBACAC,uBACAC,mBACAC,sBAkBJ,wBC/EAhC,GAEOyB,EAAgBzB,EAASwC,EAAAA,mCCFhC1C,GAIOoC,OAAOC,OACZvC,EAAe6C,EAAAA,YAAa3C,EAAgB,aAC5C,CACEuB,OALW,IAAMvB,EAAe4C,YAMhCC,WAAY7C,EAAe6C,2BCb/B3C,GAEOyB,EAAgBzB,EAAS4C,EAAAA,wCCOhC5C,GAOOyB,EAAgBzB,EAASwC,EAAVA,iBAA4BxB,yBCflDhB,GAEOyB,EAAgBzB,EAAS4C,EAAVA,SAAoB5B"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{useQueryClient as e,useQuery as t,useInfiniteQuery as r,useMutation as a}from"@tanstack/react-query";const n=(t,r,a)=>function(n,u){const[i,s]=[e(null!=n&&n.context?n:u).getDefaultOptions()[a],r,n].reduce((([e,t],{use:r=[],...a}={})=>[[...e,...r],{...t,...a}]),[[]]);let y=t;for(let e=i.length;e--;)y=i[e](y);return y(s,u)},u={enabled:!0,suspense:!0,throwOnError:!0,useErrorBoundary:!0},i=(e,t)=>void 0===t?[e]:[e,t],s=(e,t,r)=>{const{primaryKey:a,queryFn:u,queryKeyHashFn:s,getPreviousPageParam:y,getNextPageParam:o,initialPageParam:g}=e,P=e=>i(a,e);return Object.assign(n(((e,a)=>t({...e,...r,queryKey:P(e.variables)},a)),e,"queries"),{getPrimaryKey:()=>a,getKey:P,queryFn:u,queryKeyHashFn:s,getOptions:t=>({...e,queryKey:P(t)}),getFetchOptions:e=>({queryKey:P(e),queryFn:u,queryKeyHashFn:s,getPreviousPageParam:y,getNextPageParam:o,initialPageParam:g})})},y=e=>s(e,t),o=e=>s(e,t,u),g=e=>s(e,r),P=e=>s(e,r,u),m=e=>Object.assign(n(a,e,"mutations"),{getKey:()=>e.mutationKey,mutationFn:e.mutationFn});export{g as createInfiniteQuery,m as createMutation,y as createQuery,P as createSuspenseInfiniteQuery,o as createSuspenseQuery,i as getKey};
|
package/package.json
CHANGED
package/src/createBaseQuery.ts
CHANGED
|
@@ -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
|
-
|
|
12
|
+
AdditionalQueryOptions<any, any> {
|
|
17
13
|
use?: Middleware[]
|
|
14
|
+
variables?: any
|
|
18
15
|
}
|
|
19
16
|
|
|
20
17
|
type QueryBaseHookOptions = Omit<
|
|
@@ -24,11 +21,13 @@ type QueryBaseHookOptions = Omit<
|
|
|
24
21
|
variables?: any
|
|
25
22
|
}
|
|
26
23
|
|
|
27
|
-
|
|
24
|
+
const existedPrimaryKeys = new Set<string>()
|
|
25
|
+
|
|
26
|
+
export const createBaseQuery = (
|
|
28
27
|
defaultOptions: any,
|
|
29
28
|
useRQHook: (options: any, queryClient?: any) => any,
|
|
30
29
|
overrideOptions?: QueryBaseHookOptions
|
|
31
|
-
): any {
|
|
30
|
+
): any => {
|
|
32
31
|
const {
|
|
33
32
|
primaryKey,
|
|
34
33
|
queryFn,
|
|
@@ -39,10 +38,16 @@ export function createBaseQuery(
|
|
|
39
38
|
} = defaultOptions as CreateBaseQueryOptions
|
|
40
39
|
|
|
41
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
|
+
|
|
42
47
|
// @ts-ignore
|
|
43
48
|
if (defaultOptions.useDefaultOptions) {
|
|
44
|
-
|
|
45
|
-
'useDefaultOptions is not supported, please use middleware instead.'
|
|
49
|
+
console.error(
|
|
50
|
+
'[Bug] useDefaultOptions is not supported, please use middleware instead.'
|
|
46
51
|
)
|
|
47
52
|
}
|
|
48
53
|
}
|
|
@@ -51,6 +56,13 @@ export function createBaseQuery(
|
|
|
51
56
|
|
|
52
57
|
const getKey = (variables?: any) => getQueryKey(primaryKey, variables)
|
|
53
58
|
|
|
59
|
+
const getOptions = (variables: any) => {
|
|
60
|
+
return {
|
|
61
|
+
...defaultOptions,
|
|
62
|
+
queryKey: getKey(variables),
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
54
66
|
const getFetchOptions = (variables: any) => {
|
|
55
67
|
return {
|
|
56
68
|
queryKey: getKey(variables),
|
|
@@ -63,14 +75,14 @@ export function createBaseQuery(
|
|
|
63
75
|
}
|
|
64
76
|
|
|
65
77
|
const useBaseHook = (
|
|
66
|
-
|
|
67
|
-
queryClient?:
|
|
78
|
+
options: QueryBaseHookOptions,
|
|
79
|
+
queryClient?: QueryClient
|
|
68
80
|
) => {
|
|
69
81
|
return useRQHook(
|
|
70
82
|
{
|
|
71
|
-
...
|
|
83
|
+
...options,
|
|
72
84
|
...overrideOptions,
|
|
73
|
-
queryKey: getKey(variables),
|
|
85
|
+
queryKey: getKey(options.variables),
|
|
74
86
|
},
|
|
75
87
|
queryClient
|
|
76
88
|
)
|
|
@@ -81,6 +93,7 @@ export function createBaseQuery(
|
|
|
81
93
|
getKey,
|
|
82
94
|
queryFn,
|
|
83
95
|
queryKeyHashFn,
|
|
96
|
+
getOptions,
|
|
84
97
|
getFetchOptions,
|
|
85
98
|
})
|
|
86
99
|
}
|
|
@@ -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 =
|
|
9
|
+
TError = Error,
|
|
14
10
|
TPageParam = number
|
|
15
11
|
>(
|
|
16
12
|
options: CreateInfiniteQueryOptions<TFnData, TVariables, TError, TPageParam>
|
package/src/createMutation.ts
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
import { useMutation } from '@tanstack/react-query'
|
|
2
2
|
|
|
3
|
-
import type { CreateMutationOptions,
|
|
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 =
|
|
9
|
+
TError = Error,
|
|
10
10
|
TContext = unknown
|
|
11
11
|
>(
|
|
12
12
|
defaultOptions: CreateMutationOptions<TData, TVariables, TError, TContext>
|
package/src/createQuery.tsx
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import { useQuery } from '@tanstack/react-query'
|
|
2
2
|
|
|
3
3
|
import { createBaseQuery } from './createBaseQuery'
|
|
4
|
-
import type { CreateQueryOptions,
|
|
4
|
+
import type { CreateQueryOptions, QueryHook } from './types'
|
|
5
5
|
|
|
6
|
-
export const createQuery = <TFnData, TVariables = any, TError =
|
|
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 =
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
|
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
|
-
|
|
104
|
+
type DeepPartial<T> = T extends object
|
|
107
105
|
? {
|
|
108
106
|
[P in keyof T]?: DeepPartial<T[P]>
|
|
109
107
|
}
|
|
@@ -141,6 +139,9 @@ export type ExposeMethods<TFnData, TVariables, TPageParam = never> = {
|
|
|
141
139
|
>
|
|
142
140
|
queryKeyHashFn?: QueryKeyHashFunction<inferQueryKey<TVariables>>
|
|
143
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']
|
|
144
145
|
}
|
|
145
146
|
|
|
146
147
|
// query hook
|
|
@@ -148,12 +149,12 @@ export type ExposeMethods<TFnData, TVariables, TPageParam = never> = {
|
|
|
148
149
|
export interface CreateQueryOptions<
|
|
149
150
|
TFnData = unknown,
|
|
150
151
|
TVariables = any,
|
|
151
|
-
TError =
|
|
152
|
+
TError = Error
|
|
152
153
|
> extends Omit<
|
|
153
154
|
UseQueryOptions<TFnData, TError, TFnData, inferQueryKey<TVariables>>,
|
|
154
155
|
'queryKey' | 'queryFn' | 'select'
|
|
155
156
|
>,
|
|
156
|
-
|
|
157
|
+
AdditionalQueryOptions<TFnData, TVariables> {
|
|
157
158
|
use?: Middleware<QueryHook<TFnData, TVariables, TError>>[]
|
|
158
159
|
variables?: TVariables
|
|
159
160
|
}
|
|
@@ -169,11 +170,8 @@ export interface QueryHookOptions<TFnData, TError, TData, TVariables>
|
|
|
169
170
|
|
|
170
171
|
export type QueryHookResult<TData, TError> = UseQueryResult<TData, TError>
|
|
171
172
|
|
|
172
|
-
export interface QueryHook<
|
|
173
|
-
TFnData
|
|
174
|
-
TVariables = any,
|
|
175
|
-
TError = DefaultError
|
|
176
|
-
> extends ExposeMethods<TFnData, TVariables> {
|
|
173
|
+
export interface QueryHook<TFnData = unknown, TVariables = any, TError = Error>
|
|
174
|
+
extends ExposeMethods<TFnData, TVariables> {
|
|
177
175
|
<TData = TFnData>(
|
|
178
176
|
options?: QueryHookOptions<TFnData, TError, TData, TVariables>,
|
|
179
177
|
queryClient?: CompatibleWithV4<QueryClient, void>
|
|
@@ -185,7 +183,7 @@ export interface QueryHook<
|
|
|
185
183
|
export interface CreateSuspenseQueryOptions<
|
|
186
184
|
TFnData = unknown,
|
|
187
185
|
TVariables = any,
|
|
188
|
-
TError =
|
|
186
|
+
TError = Error
|
|
189
187
|
> extends Omit<
|
|
190
188
|
UseQueryOptions<TFnData, TError, TFnData, inferQueryKey<TVariables>>,
|
|
191
189
|
| 'queryKey'
|
|
@@ -198,7 +196,7 @@ export interface CreateSuspenseQueryOptions<
|
|
|
198
196
|
| 'keepPreviousData'
|
|
199
197
|
| 'useErrorBoundary'
|
|
200
198
|
>,
|
|
201
|
-
|
|
199
|
+
AdditionalQueryOptions<TFnData, TVariables> {
|
|
202
200
|
use?: Middleware<SuspenseQueryHook<TFnData, TVariables, TVariables>>[]
|
|
203
201
|
}
|
|
204
202
|
|
|
@@ -227,7 +225,7 @@ export type SuspenseQueryHookResult<TData, TError> = Omit<
|
|
|
227
225
|
export interface SuspenseQueryHook<
|
|
228
226
|
TFnData = unknown,
|
|
229
227
|
TVariables = any,
|
|
230
|
-
TError =
|
|
228
|
+
TError = Error
|
|
231
229
|
> extends ExposeMethods<TFnData, TVariables> {
|
|
232
230
|
<TData = TFnData>(
|
|
233
231
|
options?: SuspenseQueryHookOptions<TFnData, TError, TData, TVariables>,
|
|
@@ -240,7 +238,7 @@ export interface SuspenseQueryHook<
|
|
|
240
238
|
export interface CreateInfiniteQueryOptions<
|
|
241
239
|
TFnData = unknown,
|
|
242
240
|
TVariables = any,
|
|
243
|
-
TError =
|
|
241
|
+
TError = Error,
|
|
244
242
|
TPageParam = number
|
|
245
243
|
> extends Omit<
|
|
246
244
|
CompatibleUseInfiniteQueryOptions<
|
|
@@ -252,7 +250,7 @@ export interface CreateInfiniteQueryOptions<
|
|
|
252
250
|
>,
|
|
253
251
|
'queryKey' | 'queryFn' | 'select'
|
|
254
252
|
>,
|
|
255
|
-
|
|
253
|
+
AdditionalQueryOptions<TFnData, TVariables, TPageParam> {
|
|
256
254
|
use?: Middleware<InfiniteQueryHook<TFnData, TVariables, TError, TPageParam>>[]
|
|
257
255
|
}
|
|
258
256
|
|
|
@@ -289,7 +287,7 @@ export type InfiniteQueryHookResult<TData, TError> = UseInfiniteQueryResult<
|
|
|
289
287
|
export interface InfiniteQueryHook<
|
|
290
288
|
TFnData = unknown,
|
|
291
289
|
TVariables = any,
|
|
292
|
-
TError =
|
|
290
|
+
TError = Error,
|
|
293
291
|
TPageParam = number
|
|
294
292
|
> extends ExposeMethods<TFnData, TVariables, TPageParam> {
|
|
295
293
|
<TData = CompatibleWithV4<InfiniteData<TFnData, TPageParam>, TFnData>>(
|
|
@@ -309,7 +307,7 @@ export interface InfiniteQueryHook<
|
|
|
309
307
|
export interface CreateSuspenseInfiniteQueryOptions<
|
|
310
308
|
TFnData = unknown,
|
|
311
309
|
TVariables = any,
|
|
312
|
-
TError =
|
|
310
|
+
TError = Error,
|
|
313
311
|
TPageParam = number
|
|
314
312
|
> extends Omit<
|
|
315
313
|
CompatibleUseInfiniteQueryOptions<
|
|
@@ -329,7 +327,7 @@ export interface CreateSuspenseInfiniteQueryOptions<
|
|
|
329
327
|
| 'keepPreviousData'
|
|
330
328
|
| 'useErrorBoundary'
|
|
331
329
|
>,
|
|
332
|
-
|
|
330
|
+
AdditionalQueryOptions<TFnData, TVariables, TPageParam> {
|
|
333
331
|
use?: Middleware<SuspenseInfiniteQueryHook<TFnData, TVariables, TVariables>>[]
|
|
334
332
|
}
|
|
335
333
|
|
|
@@ -372,7 +370,7 @@ export type SuspenseInfiniteQueryHookResult<TData, TError> = Omit<
|
|
|
372
370
|
export interface SuspenseInfiniteQueryHook<
|
|
373
371
|
TFnData = unknown,
|
|
374
372
|
TVariables = any,
|
|
375
|
-
TError =
|
|
373
|
+
TError = Error,
|
|
376
374
|
TPageParam = number
|
|
377
375
|
> extends ExposeMethods<TFnData, TVariables, TPageParam> {
|
|
378
376
|
<TData = CompatibleWithV4<InfiniteData<TFnData, TPageParam>, TFnData>>(
|
|
@@ -392,7 +390,7 @@ export interface SuspenseInfiniteQueryHook<
|
|
|
392
390
|
export interface CreateMutationOptions<
|
|
393
391
|
TData = unknown,
|
|
394
392
|
TVariables = void,
|
|
395
|
-
TError =
|
|
393
|
+
TError = Error,
|
|
396
394
|
TContext = unknown
|
|
397
395
|
> extends UseMutationOptions<TData, TError, TVariables, TContext> {
|
|
398
396
|
use?: Middleware<MutationHook<TData, TError, TVariables>>[]
|
|
@@ -407,6 +405,13 @@ export interface MutationHookOptions<TData, TError, TVariables, TContext>
|
|
|
407
405
|
variables?: TVariables
|
|
408
406
|
}
|
|
409
407
|
|
|
408
|
+
export type MutationHookResult<
|
|
409
|
+
TData = unknown,
|
|
410
|
+
TError = Error,
|
|
411
|
+
TVariables = unknown,
|
|
412
|
+
TContext = unknown
|
|
413
|
+
> = UseMutationResult<TData, TError, TVariables, TContext>
|
|
414
|
+
|
|
410
415
|
export interface MutationHook<
|
|
411
416
|
TData = unknown,
|
|
412
417
|
TVariables = any,
|
|
@@ -415,7 +420,7 @@ export interface MutationHook<
|
|
|
415
420
|
<TContext>(
|
|
416
421
|
options?: MutationHookOptions<TData, TError, TVariables, TContext>,
|
|
417
422
|
queryClient?: CompatibleWithV4<QueryClient, void>
|
|
418
|
-
):
|
|
423
|
+
): MutationHookResult<TData, TError, TVariables, TContext>
|
|
419
424
|
getKey: () => MutationKey | undefined
|
|
420
425
|
mutationFn: MutationFunction<TData, TVariables>
|
|
421
426
|
}
|
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 {
|
|
3
|
+
import type { Middleware, inferQueryKey } from './types'
|
|
4
4
|
|
|
5
5
|
export const withMiddleware = (
|
|
6
6
|
hook: any,
|
|
@@ -9,22 +9,21 @@ export const withMiddleware = (
|
|
|
9
9
|
) => {
|
|
10
10
|
return function useMiddleware(
|
|
11
11
|
options?: { client?: QueryClient; use?: Middleware[] },
|
|
12
|
-
queryClient?:
|
|
12
|
+
queryClient?: QueryClient
|
|
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
|
|
@@ -36,7 +35,14 @@ export const withMiddleware = (
|
|
|
36
35
|
}
|
|
37
36
|
}
|
|
38
37
|
|
|
39
|
-
export const
|
|
38
|
+
export const suspenseOptions = {
|
|
39
|
+
enabled: true,
|
|
40
|
+
suspense: true,
|
|
41
|
+
throwOnError: true,
|
|
42
|
+
useErrorBoundary: true,
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export const getKey = <TVariables = void>(
|
|
40
46
|
primaryKey: string,
|
|
41
47
|
variables?: TVariables
|
|
42
48
|
) => {
|