react-query-kit 2.0.9-beta.0 → 3.0.0-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.
- package/README.md +105 -48
- package/build/lib/createBaseQuery.d.ts +2 -6
- package/build/lib/createBaseQuery.esm.js +19 -12
- package/build/lib/createBaseQuery.esm.js.map +1 -1
- package/build/lib/createBaseQuery.js +19 -12
- package/build/lib/createBaseQuery.js.map +1 -1
- package/build/lib/createBaseQuery.mjs +19 -12
- package/build/lib/createBaseQuery.mjs.map +1 -1
- package/build/lib/createMutation.d.ts +1 -1
- package/build/lib/createMutation.esm.js +2 -3
- package/build/lib/createMutation.esm.js.map +1 -1
- package/build/lib/createMutation.js +2 -3
- package/build/lib/createMutation.js.map +1 -1
- package/build/lib/createMutation.mjs +2 -3
- 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 +1 -1
- package/build/lib/createSuspenseInfiniteQuery.esm.js.map +1 -1
- package/build/lib/createSuspenseInfiniteQuery.js +1 -1
- package/build/lib/createSuspenseInfiniteQuery.js.map +1 -1
- package/build/lib/createSuspenseInfiniteQuery.mjs +1 -1
- package/build/lib/createSuspenseInfiniteQuery.mjs.map +1 -1
- package/build/lib/createSuspenseQuery.esm.js +1 -1
- package/build/lib/createSuspenseQuery.esm.js.map +1 -1
- package/build/lib/createSuspenseQuery.js +1 -1
- package/build/lib/createSuspenseQuery.js.map +1 -1
- package/build/lib/createSuspenseQuery.mjs +1 -1
- package/build/lib/createSuspenseQuery.mjs.map +1 -1
- package/build/lib/types.d.ts +35 -50
- package/build/lib/utils.d.ts +9 -8
- package/build/lib/utils.esm.js +10 -10
- package/build/lib/utils.esm.js.map +1 -1
- package/build/lib/utils.js +10 -10
- package/build/lib/utils.js.map +1 -1
- package/build/lib/utils.mjs +10 -10
- package/build/lib/utils.mjs.map +1 -1
- package/build/stats-html.html +1 -1
- package/build/stats.json +96 -96
- package/build/umd/index.development.js +33 -27
- 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 +25 -14
- package/src/createMutation.ts +4 -5
- package/src/createSuspenseInfiniteQuery.ts +1 -1
- package/src/createSuspenseQuery.ts +1 -1
- package/src/types.ts +96 -168
- package/src/utils.ts +17 -21
- /package/src/{createQuery.tsx → createQuery.ts} +0 -0
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.production.js","sources":["../../src/utils.ts","../../src/createBaseQuery.ts","../../src/createInfiniteQuery.ts","../../src/createMutation.ts","../../src/createQuery.
|
|
1
|
+
{"version":3,"file":"index.production.js","sources":["../../src/utils.ts","../../src/createBaseQuery.ts","../../src/createInfiniteQuery.ts","../../src/createMutation.ts","../../src/createQuery.ts","../../src/createSuspenseInfiniteQuery.ts","../../src/createSuspenseQuery.ts"],"sourcesContent":["import * as ReactQuery from '@tanstack/react-query'\nimport type { Query, QueryClient, QueryKey } from '@tanstack/react-query'\n\nimport type { Middleware } from './types'\n\nexport const isV5 = !!ReactQuery.useSuspenseQuery\n\n// Compatible with ReactQuery v4\nexport const suspenseOptions = {\n enabled: true,\n suspense: true,\n useErrorBoundary: (_error: unknown, query: Query) =>\n query.state.data === undefined,\n}\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 ReactQuery.useQueryClient(\n // @ts-ignore Compatible with ReactQuery v4\n isV5 ? queryClient : options\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 getKey = (queryKey: QueryKey, variables?: any): QueryKey => {\n return variables === undefined ? [...queryKey] : [...queryKey, variables]\n}\n","import type {\n QueryClient,\n QueryFunctionContext,\n UseBaseQueryOptions,\n UseInfiniteQueryOptions,\n} from '@tanstack/react-query'\n\nimport type { AdditionalQueryOptions, Middleware } from './types'\nimport { getKey as getFullKey, withMiddleware } from './utils'\n\ninterface CreateBaseQueryOptions\n extends Omit<UseInfiniteQueryOptions, 'queryFn'>,\n AdditionalQueryOptions<any, any> {\n use?: Middleware[]\n variables?: any\n}\n\ntype QueryBaseHookOptions = Omit<\n UseBaseQueryOptions,\n 'queryKey' | 'queryFn'\n> & {\n fetcher?: any\n variables?: any\n}\n\nexport const createBaseQuery = (\n defaultOptions: any,\n useRQHook: (options: any, queryClient?: any) => any,\n overrideOptions?: Partial<UseInfiniteQueryOptions>\n): any => {\n if (process.env.NODE_ENV !== 'production') {\n // @ts-ignore\n if (defaultOptions.useDefaultOptions) {\n console.error(\n '[Bug] useDefaultOptions is not supported, please use middleware instead.'\n )\n }\n\n // @ts-ignore\n if (defaultOptions.queryFn) {\n console.error(\n '[Bug] queryFn is not supported, please use fetcher instead.'\n )\n }\n }\n\n const {\n queryKey,\n fetcher,\n queryKeyHashFn,\n getPreviousPageParam,\n getNextPageParam,\n initialPageParam,\n } = defaultOptions as CreateBaseQueryOptions\n\n const getQueryOptions = (fetcherFn: any, variables: any) => {\n return {\n queryFn: (context: QueryFunctionContext) => fetcherFn(variables, context),\n queryKey: getFullKey(queryKey, variables),\n }\n }\n\n const getKey = (variables?: any) => getFullKey(queryKey, variables)\n\n const getOptions = (variables: any) => {\n return {\n ...defaultOptions,\n ...getQueryOptions(fetcher, variables),\n }\n }\n\n const getFetchOptions = (variables: any) => {\n return {\n queryKeyHashFn,\n getPreviousPageParam,\n getNextPageParam,\n initialPageParam,\n ...getQueryOptions(fetcher, variables),\n }\n }\n\n const useBaseHook = (\n options: QueryBaseHookOptions,\n queryClient?: QueryClient\n ) => {\n return useRQHook(\n {\n ...options,\n ...overrideOptions,\n ...getQueryOptions(options.fetcher, options.variables),\n },\n queryClient\n )\n }\n\n return Object.assign(withMiddleware(useBaseHook, defaultOptions, 'queries'), {\n fetcher,\n getKey,\n getOptions,\n getFetchOptions,\n })\n}\n","import * as ReactQuery from '@tanstack/react-query'\n\nimport { createBaseQuery } from './createBaseQuery'\nimport type {\n CompatibleError,\n CreateInfiniteQueryOptions,\n InfiniteQueryHook,\n} from './types'\n\nexport const createInfiniteQuery = <\n TFnData,\n TVariables = void,\n TError = CompatibleError,\n TPageParam = number\n>(\n options: CreateInfiniteQueryOptions<TFnData, TVariables, TError, TPageParam>\n): InfiniteQueryHook<TFnData, TVariables, TError, TPageParam> => {\n return createBaseQuery(options, ReactQuery.useInfiniteQuery)\n}\n","import * as ReactQuery from '@tanstack/react-query'\n\nimport type {\n CompatibleError,\n CreateMutationOptions,\n MutationHook,\n} from './types'\nimport { withMiddleware } from './utils'\n\nexport const createMutation = <\n TData = unknown,\n TVariables = void,\n TError = CompatibleError,\n TContext = unknown\n>(\n defaultOptions: CreateMutationOptions<TData, TVariables, TError, TContext>\n): MutationHook<TData, TVariables, TError, TContext> => {\n return Object.assign(\n withMiddleware(ReactQuery.useMutation, defaultOptions, 'mutations'),\n {\n getKey: () => defaultOptions.mutationKey,\n getOptions: () => defaultOptions,\n mutationFn: defaultOptions.mutationFn,\n }\n ) as MutationHook<TData, TVariables, TError, TContext>\n}\n","import * as ReactQuery from '@tanstack/react-query'\n\nimport { createBaseQuery } from './createBaseQuery'\nimport type { CompatibleError, CreateQueryOptions, QueryHook } from './types'\n\nexport const createQuery = <\n TFnData,\n TVariables = void,\n TError = CompatibleError\n>(\n options: CreateQueryOptions<TFnData, TVariables, TError>\n): QueryHook<TFnData, TVariables, TError> => {\n return createBaseQuery(options, ReactQuery.useQuery)\n}\n","import * as ReactQuery from '@tanstack/react-query'\n\nimport { createBaseQuery } from './createBaseQuery'\nimport type {\n CompatibleError,\n CreateSuspenseInfiniteQueryOptions,\n SuspenseInfiniteQueryHook,\n} from './types'\nimport { isV5, suspenseOptions } from './utils'\n\nexport const createSuspenseInfiniteQuery = <\n TFnData,\n TVariables = void,\n TError = CompatibleError,\n TPageParam = number\n>(\n options: CreateSuspenseInfiniteQueryOptions<\n TFnData,\n TVariables,\n TError,\n TPageParam\n >\n): SuspenseInfiniteQueryHook<TFnData, TVariables, TError, TPageParam> => {\n return isV5\n ? createBaseQuery(options, ReactQuery.useSuspenseInfiniteQuery)\n : createBaseQuery(options, ReactQuery.useInfiniteQuery, suspenseOptions)\n}\n","import * as ReactQuery from '@tanstack/react-query'\n\nimport { createBaseQuery } from './createBaseQuery'\nimport type {\n CompatibleError,\n CreateSuspenseQueryOptions,\n SuspenseQueryHook,\n} from './types'\nimport { isV5, suspenseOptions } from './utils'\n\nexport const createSuspenseQuery = <\n TFnData,\n TVariables = void,\n TError = CompatibleError\n>(\n options: CreateSuspenseQueryOptions<TFnData, TVariables, TError>\n): SuspenseQueryHook<TFnData, TVariables, TError> => {\n return isV5\n ? createBaseQuery(options, ReactQuery.useSuspenseQuery)\n : createBaseQuery(options, ReactQuery.useQuery, suspenseOptions)\n}\n"],"names":["isV5","ReactQuery","useSuspenseQuery","suspenseOptions","enabled","suspense","useErrorBoundary","_error","query","undefined","state","data","withMiddleware","hook","defaultOptions","type","options","queryClient","middleware","opts","useQueryClient","getDefaultOptions","reduce","u1","o1","use","u2","o2","next","i","length","getKey","queryKey","variables","createBaseQuery","useRQHook","overrideOptions","fetcher","queryKeyHashFn","getPreviousPageParam","getNextPageParam","initialPageParam","getQueryOptions","fetcherFn","queryFn","context","getFullKey","Object","assign","getOptions","getFetchOptions","useInfiniteQuery","useMutation","mutationKey","mutationFn","useQuery","useSuspenseInfiniteQuery"],"mappings":"umBAKO,MAAMA,IAASC,EAAWC,iBAGpBC,EAAkB,CAC7BC,SAAS,EACTC,UAAU,EACVC,iBAAkB,CAACC,EAAiBC,SACbC,IAArBD,EAAME,MAAMC,MAGHC,EAAiB,CAC5BC,EACAC,EACAC,IAEO,SACLC,EACAC,GAEA,MAAOC,EAAYC,GAAQ,CACzBlB,EAAWmB,eAETpB,EAAOiB,EAAcD,GACrBK,oBAAoBN,GACtBD,EACAE,GACAM,QACA,EAAEC,EAAIC,IAAOC,IAAKC,EAAK,MAAOC,GAAO,CAAA,IAAO,CAC1C,IAAIJ,KAAOG,GACX,IAAKF,KAAOG,KAEd,CAAC,KAGH,IAAIC,EAAOf,EACX,IAAK,IAAIgB,EAAIX,EAAWY,OAAQD,KAC9BD,EAAOV,EAAWW,GAAGD,GAGvB,OAAOA,EAAKT,EAAMF,IAITc,EAAS,CAACC,EAAoBC,SACpBxB,IAAdwB,EAA0B,IAAID,GAAY,IAAIA,EAAUC,GCxBpDC,EAAkB,CAC7BpB,EACAqB,EACAC,KAkBA,MAAMJ,SACJA,EADIK,QAEJA,EAFIC,eAGJA,EAHIC,qBAIJA,EAJIC,iBAKJA,EALIC,iBAMJA,GACE3B,EAEE4B,EAAkB,CAACC,EAAgBV,KAChC,CACLW,QAAUC,GAAkCF,EAAUV,EAAWY,GACjEb,SAAUc,EAAWd,EAAUC,KAqCnC,OAAOc,OAAOC,OAAOpC,GAdD,CAClBI,EACAC,IAEOkB,EACL,IACKnB,KACAoB,KACAM,EAAgB1B,EAAQqB,QAASrB,EAAQiB,YAE9ChB,IAI6CH,EAAgB,WAAY,CAC3EuB,iBAlCcJ,GAAoBa,EAAWd,EAAUC,GAoCvDgB,WAlCkBhB,IACX,IACFnB,KACA4B,EAAgBL,EAASJ,KAgC9BiB,gBA5BuBjB,IAChB,CACLK,iBACAC,uBACAC,mBACAC,sBACGC,EAAgBL,EAASJ,MAkBhC,wBChFAjB,GAEOkB,EAAgBlB,EAASf,EAAWkD,mCCF3CrC,GAEOiC,OAAOC,OACZpC,EAAeX,EAAWmD,YAAatC,EAAgB,aACvD,CACEiB,OAAQ,IAAMjB,EAAeuC,YAC7BJ,WAAY,IAAMnC,EAClBwC,WAAYxC,EAAewC,2BCZ/BtC,GAEOkB,EAAgBlB,EAASf,EAAWsD,wCCI3CvC,GAOOhB,EACHkC,EAAgBlB,EAASf,EAAWuD,0BACpCtB,EAAgBlB,EAASf,EAAWkD,iBAAkBhD,yBCV1Da,GAEOhB,EACHkC,EAAgBlB,EAASf,EAAWC,kBACpCgC,EAAgBlB,EAASf,EAAWsD,SAAUpD"}
|
package/build/zip/zip.esm.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import*as e from"@tanstack/react-query";const t=(t,u,a)=>function(n
|
|
1
|
+
import*as e from"@tanstack/react-query";const t=!!e.useSuspenseQuery,u={enabled:!0,suspense:!0,useErrorBoundary:(e,t)=>void 0===t.state.data},a=(u,a,r)=>function(s,n){const[i,o]=[e.useQueryClient(t?n:s).getDefaultOptions()[r],a,s].reduce((([e,t],{use:u=[],...a}={})=>[[...e,...u],{...t,...a}]),[[]]);let y=u;for(let e=i.length;e--;)y=i[e](y);return y(o,n)},r=(e,t)=>void 0===t?[...e]:[...e,t],s=(e,t,u)=>{const{queryKey:s,fetcher:n,queryKeyHashFn:i,getPreviousPageParam:o,getNextPageParam:y,initialPageParam:g}=e,P=(e,t)=>({queryFn:u=>e(t,u),queryKey:r(s,t)});return Object.assign(a(((e,a)=>t({...e,...u,...P(e.fetcher,e.variables)},a)),e,"queries"),{fetcher:n,getKey:e=>r(s,e),getOptions:t=>({...e,...P(n,t)}),getFetchOptions:e=>({queryKeyHashFn:i,getPreviousPageParam:o,getNextPageParam:y,initialPageParam:g,...P(n,e)})})},n=t=>s(t,e.useQuery),i=a=>t?s(a,e.useSuspenseQuery):s(a,e.useQuery,u),o=t=>s(t,e.useInfiniteQuery),y=a=>t?s(a,e.useSuspenseInfiniteQuery):s(a,e.useInfiniteQuery,u),g=t=>Object.assign(a(e.useMutation,t,"mutations"),{getKey:()=>t.mutationKey,getOptions:()=>t,mutationFn:t.mutationFn});export{o as createInfiniteQuery,g as createMutation,n as createQuery,y as createSuspenseInfiniteQuery,i as createSuspenseQuery,r as getKey};
|
package/package.json
CHANGED
package/src/createBaseQuery.ts
CHANGED
|
@@ -1,14 +1,15 @@
|
|
|
1
1
|
import type {
|
|
2
2
|
QueryClient,
|
|
3
|
+
QueryFunctionContext,
|
|
3
4
|
UseBaseQueryOptions,
|
|
4
5
|
UseInfiniteQueryOptions,
|
|
5
6
|
} from '@tanstack/react-query'
|
|
6
7
|
|
|
7
8
|
import type { AdditionalQueryOptions, Middleware } from './types'
|
|
8
|
-
import { getKey as
|
|
9
|
+
import { getKey as getFullKey, withMiddleware } from './utils'
|
|
9
10
|
|
|
10
11
|
interface CreateBaseQueryOptions
|
|
11
|
-
extends Omit<UseInfiniteQueryOptions, '
|
|
12
|
+
extends Omit<UseInfiniteQueryOptions, 'queryFn'>,
|
|
12
13
|
AdditionalQueryOptions<any, any> {
|
|
13
14
|
use?: Middleware[]
|
|
14
15
|
variables?: any
|
|
@@ -18,13 +19,14 @@ type QueryBaseHookOptions = Omit<
|
|
|
18
19
|
UseBaseQueryOptions,
|
|
19
20
|
'queryKey' | 'queryFn'
|
|
20
21
|
> & {
|
|
22
|
+
fetcher?: any
|
|
21
23
|
variables?: any
|
|
22
24
|
}
|
|
23
25
|
|
|
24
26
|
export const createBaseQuery = (
|
|
25
27
|
defaultOptions: any,
|
|
26
28
|
useRQHook: (options: any, queryClient?: any) => any,
|
|
27
|
-
overrideOptions?:
|
|
29
|
+
overrideOptions?: Partial<UseInfiniteQueryOptions>
|
|
28
30
|
): any => {
|
|
29
31
|
if (process.env.NODE_ENV !== 'production') {
|
|
30
32
|
// @ts-ignore
|
|
@@ -33,36 +35,47 @@ export const createBaseQuery = (
|
|
|
33
35
|
'[Bug] useDefaultOptions is not supported, please use middleware instead.'
|
|
34
36
|
)
|
|
35
37
|
}
|
|
38
|
+
|
|
39
|
+
// @ts-ignore
|
|
40
|
+
if (defaultOptions.queryFn) {
|
|
41
|
+
console.error(
|
|
42
|
+
'[Bug] queryFn is not supported, please use fetcher instead.'
|
|
43
|
+
)
|
|
44
|
+
}
|
|
36
45
|
}
|
|
37
46
|
|
|
38
47
|
const {
|
|
39
|
-
|
|
40
|
-
|
|
48
|
+
queryKey,
|
|
49
|
+
fetcher,
|
|
41
50
|
queryKeyHashFn,
|
|
42
51
|
getPreviousPageParam,
|
|
43
52
|
getNextPageParam,
|
|
44
53
|
initialPageParam,
|
|
45
54
|
} = defaultOptions as CreateBaseQueryOptions
|
|
46
55
|
|
|
47
|
-
const
|
|
56
|
+
const getQueryOptions = (fetcherFn: any, variables: any) => {
|
|
57
|
+
return {
|
|
58
|
+
queryFn: (context: QueryFunctionContext) => fetcherFn(variables, context),
|
|
59
|
+
queryKey: getFullKey(queryKey, variables),
|
|
60
|
+
}
|
|
61
|
+
}
|
|
48
62
|
|
|
49
|
-
const getKey = (variables?: any) =>
|
|
63
|
+
const getKey = (variables?: any) => getFullKey(queryKey, variables)
|
|
50
64
|
|
|
51
65
|
const getOptions = (variables: any) => {
|
|
52
66
|
return {
|
|
53
67
|
...defaultOptions,
|
|
54
|
-
|
|
68
|
+
...getQueryOptions(fetcher, variables),
|
|
55
69
|
}
|
|
56
70
|
}
|
|
57
71
|
|
|
58
72
|
const getFetchOptions = (variables: any) => {
|
|
59
73
|
return {
|
|
60
|
-
queryKey: getKey(variables),
|
|
61
|
-
queryFn,
|
|
62
74
|
queryKeyHashFn,
|
|
63
75
|
getPreviousPageParam,
|
|
64
76
|
getNextPageParam,
|
|
65
77
|
initialPageParam,
|
|
78
|
+
...getQueryOptions(fetcher, variables),
|
|
66
79
|
}
|
|
67
80
|
}
|
|
68
81
|
|
|
@@ -74,17 +87,15 @@ export const createBaseQuery = (
|
|
|
74
87
|
{
|
|
75
88
|
...options,
|
|
76
89
|
...overrideOptions,
|
|
77
|
-
|
|
90
|
+
...getQueryOptions(options.fetcher, options.variables),
|
|
78
91
|
},
|
|
79
92
|
queryClient
|
|
80
93
|
)
|
|
81
94
|
}
|
|
82
95
|
|
|
83
96
|
return Object.assign(withMiddleware(useBaseHook, defaultOptions, 'queries'), {
|
|
84
|
-
|
|
97
|
+
fetcher,
|
|
85
98
|
getKey,
|
|
86
|
-
queryFn,
|
|
87
|
-
queryKeyHashFn,
|
|
88
99
|
getOptions,
|
|
89
100
|
getFetchOptions,
|
|
90
101
|
})
|
package/src/createMutation.ts
CHANGED
|
@@ -14,14 +14,13 @@ export const createMutation = <
|
|
|
14
14
|
TContext = unknown
|
|
15
15
|
>(
|
|
16
16
|
defaultOptions: CreateMutationOptions<TData, TVariables, TError, TContext>
|
|
17
|
-
): MutationHook<TData, TVariables, TError> => {
|
|
18
|
-
const getKey = () => defaultOptions.mutationKey
|
|
19
|
-
|
|
17
|
+
): MutationHook<TData, TVariables, TError, TContext> => {
|
|
20
18
|
return Object.assign(
|
|
21
19
|
withMiddleware(ReactQuery.useMutation, defaultOptions, 'mutations'),
|
|
22
20
|
{
|
|
23
|
-
getKey,
|
|
21
|
+
getKey: () => defaultOptions.mutationKey,
|
|
22
|
+
getOptions: () => defaultOptions,
|
|
24
23
|
mutationFn: defaultOptions.mutationFn,
|
|
25
24
|
}
|
|
26
|
-
) as MutationHook<TData, TVariables, TError>
|
|
25
|
+
) as MutationHook<TData, TVariables, TError, TContext>
|
|
27
26
|
}
|
|
@@ -21,7 +21,7 @@ export const createSuspenseInfiniteQuery = <
|
|
|
21
21
|
TPageParam
|
|
22
22
|
>
|
|
23
23
|
): SuspenseInfiniteQueryHook<TFnData, TVariables, TError, TPageParam> => {
|
|
24
|
-
return isV5
|
|
24
|
+
return isV5
|
|
25
25
|
? createBaseQuery(options, ReactQuery.useSuspenseInfiniteQuery)
|
|
26
26
|
: createBaseQuery(options, ReactQuery.useInfiniteQuery, suspenseOptions)
|
|
27
27
|
}
|
|
@@ -15,7 +15,7 @@ export const createSuspenseQuery = <
|
|
|
15
15
|
>(
|
|
16
16
|
options: CreateSuspenseQueryOptions<TFnData, TVariables, TError>
|
|
17
17
|
): SuspenseQueryHook<TFnData, TVariables, TError> => {
|
|
18
|
-
return isV5
|
|
18
|
+
return isV5
|
|
19
19
|
? createBaseQuery(options, ReactQuery.useSuspenseQuery)
|
|
20
20
|
: createBaseQuery(options, ReactQuery.useQuery, suspenseOptions)
|
|
21
21
|
}
|
package/src/types.ts
CHANGED
|
@@ -3,14 +3,13 @@ import type {
|
|
|
3
3
|
DefaultError,
|
|
4
4
|
DefinedUseInfiniteQueryResult,
|
|
5
5
|
DefinedUseQueryResult,
|
|
6
|
-
GetNextPageParamFunction,
|
|
7
|
-
GetPreviousPageParamFunction,
|
|
8
6
|
InfiniteData,
|
|
9
7
|
InfiniteQueryObserverSuccessResult,
|
|
10
8
|
MutationFunction,
|
|
11
9
|
MutationKey,
|
|
12
10
|
QueryClient,
|
|
13
11
|
QueryFunction,
|
|
12
|
+
QueryFunctionContext,
|
|
14
13
|
QueryKey,
|
|
15
14
|
QueryKeyHashFunction,
|
|
16
15
|
QueryObserverSuccessResult,
|
|
@@ -31,56 +30,18 @@ type CompatibleWithV4<V5, V4> =
|
|
|
31
30
|
? V5
|
|
32
31
|
: V4
|
|
33
32
|
|
|
34
|
-
type
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
TPageParam = unknown
|
|
46
|
-
> = CompatibleWithV4<
|
|
47
|
-
{
|
|
48
|
-
getPreviousPageParam?: GetPreviousPageParamFunction<
|
|
49
|
-
TPageParam,
|
|
50
|
-
TQueryFnData
|
|
51
|
-
>
|
|
52
|
-
getNextPageParam: GetNextPageParamFunction<TPageParam, TQueryFnData>
|
|
53
|
-
initialPageParam: TPageParam
|
|
54
|
-
},
|
|
55
|
-
{
|
|
56
|
-
getPreviousPageParam?: GetPreviousPageParamFunction<TQueryFnData>
|
|
57
|
-
getNextPageParam: GetNextPageParamFunction<TQueryFnData>
|
|
58
|
-
}
|
|
59
|
-
>
|
|
60
|
-
|
|
61
|
-
type CompatibleUseInfiniteQueryOptions<
|
|
62
|
-
TFnData,
|
|
63
|
-
TVariables,
|
|
64
|
-
TData,
|
|
65
|
-
TError,
|
|
66
|
-
TPageParam
|
|
67
|
-
> = CompatibleWithV4<
|
|
68
|
-
UseInfiniteQueryOptions<
|
|
69
|
-
TFnData,
|
|
70
|
-
TError,
|
|
71
|
-
TData,
|
|
72
|
-
TFnData,
|
|
73
|
-
inferQueryKey<TFnData, TVariables, TPageParam>,
|
|
74
|
-
TPageParam
|
|
75
|
-
>,
|
|
76
|
-
UseInfiniteQueryOptions<
|
|
77
|
-
TFnData,
|
|
78
|
-
TError,
|
|
79
|
-
TData,
|
|
80
|
-
TFnData,
|
|
81
|
-
inferQueryKey<TFnData, TVariables, TPageParam>
|
|
33
|
+
type CompatibleUseInfiniteQueryOptions<TFnData, TData, TError, TPageParam> =
|
|
34
|
+
CompatibleWithV4<
|
|
35
|
+
UseInfiniteQueryOptions<
|
|
36
|
+
TFnData,
|
|
37
|
+
TError,
|
|
38
|
+
TData,
|
|
39
|
+
TFnData,
|
|
40
|
+
QueryKey,
|
|
41
|
+
TPageParam
|
|
42
|
+
>,
|
|
43
|
+
UseInfiniteQueryOptions<TFnData, TError, TData, TFnData, QueryKey>
|
|
82
44
|
>
|
|
83
|
-
>
|
|
84
45
|
|
|
85
46
|
type CompatibleInfiniteData<TFnData, TPageParam> = CompatibleWithV4<
|
|
86
47
|
InfiniteData<TFnData, TPageParam>,
|
|
@@ -95,29 +56,11 @@ type WithRequired<T, K extends keyof T> = T & {
|
|
|
95
56
|
|
|
96
57
|
export type CompatibleError = CompatibleWithV4<DefaultError, Error>
|
|
97
58
|
|
|
98
|
-
type GetQueryKey<TVariables> = TVariables extends void
|
|
99
|
-
? [string]
|
|
100
|
-
: [string, TVariables]
|
|
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
|
-
|
|
114
59
|
export type AdditionalQueryOptions<TFnData, TVariables, TPageParam = never> = {
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
TPageParam
|
|
120
|
-
>
|
|
60
|
+
fetcher: (
|
|
61
|
+
variables: TVariables,
|
|
62
|
+
context: QueryFunctionContext<QueryKey, TPageParam>
|
|
63
|
+
) => TFnData | Promise<TFnData>
|
|
121
64
|
variables?: TVariables
|
|
122
65
|
}
|
|
123
66
|
|
|
@@ -137,68 +80,65 @@ type DeepPartial<T> = T extends object
|
|
|
137
80
|
: T
|
|
138
81
|
|
|
139
82
|
export type ExposeMethods<TFnData, TVariables, TError, TPageParam = never> = {
|
|
140
|
-
|
|
83
|
+
fetcher: (
|
|
84
|
+
variables: TVariables,
|
|
85
|
+
context?: Partial<QueryFunctionContext<QueryKey, TPageParam>>
|
|
86
|
+
) => TFnData | Promise<TFnData>
|
|
141
87
|
getKey: <V extends DeepPartial<TVariables> | void = void>(
|
|
142
88
|
variables?: V
|
|
143
|
-
) =>
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
queryKeyHashFn: QueryKeyHashFunction<
|
|
150
|
-
inferQueryKey<TFnData, TVariables, TPageParam>
|
|
89
|
+
) => CompatibleWithV4<
|
|
90
|
+
DataTag<
|
|
91
|
+
QueryKey,
|
|
92
|
+
[TPageParam] extends [never] ? TFnData : InfiniteData<TFnData, TPageParam>
|
|
93
|
+
>,
|
|
94
|
+
QueryKey
|
|
151
95
|
>
|
|
152
96
|
getFetchOptions: (
|
|
153
97
|
variables: TVariables extends void ? TVariables | void : TVariables
|
|
154
|
-
) =>
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
inferQueryKey<TFnData, TVariables, TPageParam>,
|
|
170
|
-
TPageParam
|
|
171
|
-
>
|
|
172
|
-
queryKeyHashFn?: QueryKeyHashFunction<
|
|
173
|
-
inferQueryKey<TFnData, TVariables, TPageParam>
|
|
174
|
-
>
|
|
175
|
-
} & CompatibleInfiniteQueryPageParamsOptions<TFnData, TPageParam>
|
|
98
|
+
) => Pick<
|
|
99
|
+
ReturnType<
|
|
100
|
+
ExposeMethods<TFnData, TVariables, TError, TPageParam>['getOptions']
|
|
101
|
+
>,
|
|
102
|
+
// @ts-ignore
|
|
103
|
+
[TPageParam] extends [never]
|
|
104
|
+
? 'queryKey' | 'queryFn' | 'queryKeyHashFn'
|
|
105
|
+
:
|
|
106
|
+
| 'queryKey'
|
|
107
|
+
| 'queryFn'
|
|
108
|
+
| 'queryKeyHashFn'
|
|
109
|
+
| 'getNextPageParam'
|
|
110
|
+
| 'getPreviousPageParam'
|
|
111
|
+
| 'initialPageParam'
|
|
112
|
+
>
|
|
176
113
|
getOptions: (
|
|
177
114
|
variables: TVariables extends void ? TVariables | void : TVariables
|
|
178
115
|
) => [TPageParam] extends [never]
|
|
179
116
|
? CompatibleWithV4<
|
|
180
|
-
UseQueryOptions<
|
|
181
|
-
TFnData
|
|
182
|
-
|
|
183
|
-
TFnData,
|
|
184
|
-
DataTag<GetQueryKey<TVariables>, TFnData>
|
|
185
|
-
>,
|
|
117
|
+
UseQueryOptions<TFnData, TError, TFnData, QueryKey> & {
|
|
118
|
+
queryKey: DataTag<QueryKey, TFnData>
|
|
119
|
+
},
|
|
186
120
|
// Not work to infer TError in v4
|
|
187
121
|
{
|
|
188
|
-
queryKey:
|
|
189
|
-
queryFn: QueryFunction<TFnData,
|
|
190
|
-
queryKeyHashFn?: QueryKeyHashFunction<
|
|
122
|
+
queryKey: QueryKey
|
|
123
|
+
queryFn: QueryFunction<TFnData, QueryKey>
|
|
124
|
+
queryKeyHashFn?: QueryKeyHashFunction<QueryKey>
|
|
191
125
|
}
|
|
192
126
|
>
|
|
193
127
|
: CompatibleUseInfiniteQueryOptions<
|
|
194
128
|
TFnData,
|
|
195
|
-
TVariables,
|
|
196
129
|
TFnData,
|
|
197
130
|
TError,
|
|
198
131
|
TPageParam
|
|
199
|
-
>
|
|
132
|
+
> & {
|
|
133
|
+
queryKey: CompatibleWithV4<
|
|
134
|
+
DataTag<QueryKey, InfiniteData<TFnData, TPageParam>>,
|
|
135
|
+
QueryKey
|
|
136
|
+
>
|
|
137
|
+
}
|
|
200
138
|
}
|
|
201
139
|
|
|
140
|
+
type Clone<T> = T extends infer TClone ? TClone : never
|
|
141
|
+
|
|
202
142
|
// query hook
|
|
203
143
|
|
|
204
144
|
export interface CreateQueryOptions<
|
|
@@ -206,22 +146,19 @@ export interface CreateQueryOptions<
|
|
|
206
146
|
TVariables = void,
|
|
207
147
|
TError = CompatibleError
|
|
208
148
|
> extends Omit<
|
|
209
|
-
UseQueryOptions<
|
|
210
|
-
|
|
211
|
-
TError,
|
|
212
|
-
TFnData,
|
|
213
|
-
inferQueryKey<TFnData, TVariables>
|
|
214
|
-
>,
|
|
215
|
-
'queryKey' | 'queryFn' | 'select'
|
|
149
|
+
UseQueryOptions<TFnData, TError, TFnData, QueryKey>,
|
|
150
|
+
'queryFn' | 'select'
|
|
216
151
|
>,
|
|
217
152
|
AdditionalQueryOptions<TFnData, TVariables> {
|
|
218
|
-
use?: Middleware<
|
|
153
|
+
use?: Middleware<
|
|
154
|
+
QueryHook<Clone<TFnData>, Clone<TVariables>, Clone<TError>>
|
|
155
|
+
>[]
|
|
219
156
|
variables?: TVariables
|
|
220
157
|
}
|
|
221
158
|
|
|
222
159
|
export interface QueryHookOptions<TFnData, TError, TData, TVariables>
|
|
223
160
|
extends Omit<
|
|
224
|
-
UseQueryOptions<TFnData, TError, TData,
|
|
161
|
+
UseQueryOptions<TFnData, TError, TData, QueryKey>,
|
|
225
162
|
'queryKey' | 'queryFn' | 'queryKeyHashFn'
|
|
226
163
|
> {
|
|
227
164
|
use?: Middleware<QueryHook<TFnData, TVariables, TError>>[]
|
|
@@ -265,13 +202,7 @@ export interface CreateSuspenseQueryOptions<
|
|
|
265
202
|
TVariables = void,
|
|
266
203
|
TError = CompatibleError
|
|
267
204
|
> extends Omit<
|
|
268
|
-
UseQueryOptions<
|
|
269
|
-
TFnData,
|
|
270
|
-
TError,
|
|
271
|
-
TFnData,
|
|
272
|
-
inferQueryKey<TFnData, TVariables>
|
|
273
|
-
>,
|
|
274
|
-
| 'queryKey'
|
|
205
|
+
UseQueryOptions<TFnData, TError, TFnData, QueryKey>,
|
|
275
206
|
| 'queryFn'
|
|
276
207
|
| 'enabled'
|
|
277
208
|
| 'select'
|
|
@@ -282,12 +213,14 @@ export interface CreateSuspenseQueryOptions<
|
|
|
282
213
|
| 'useErrorBoundary'
|
|
283
214
|
>,
|
|
284
215
|
AdditionalQueryOptions<TFnData, TVariables> {
|
|
285
|
-
use?: Middleware<
|
|
216
|
+
use?: Middleware<
|
|
217
|
+
SuspenseQueryHook<Clone<TFnData>, Clone<TVariables>, Clone<TError>>
|
|
218
|
+
>[]
|
|
286
219
|
}
|
|
287
220
|
|
|
288
221
|
export interface SuspenseQueryHookOptions<TFnData, TError, TData, TVariables>
|
|
289
222
|
extends Omit<
|
|
290
|
-
UseQueryOptions<TFnData, TError, TData,
|
|
223
|
+
UseQueryOptions<TFnData, TError, TData, QueryKey>,
|
|
291
224
|
| 'queryKey'
|
|
292
225
|
| 'queryFn'
|
|
293
226
|
| 'queryKeyHashFn'
|
|
@@ -326,17 +259,18 @@ export interface CreateInfiniteQueryOptions<
|
|
|
326
259
|
TError = CompatibleError,
|
|
327
260
|
TPageParam = number
|
|
328
261
|
> extends Omit<
|
|
329
|
-
CompatibleUseInfiniteQueryOptions<
|
|
330
|
-
|
|
331
|
-
TVariables,
|
|
332
|
-
TFnData,
|
|
333
|
-
TError,
|
|
334
|
-
TPageParam
|
|
335
|
-
>,
|
|
336
|
-
'queryKey' | 'queryFn' | 'select'
|
|
262
|
+
CompatibleUseInfiniteQueryOptions<TFnData, TFnData, TError, TPageParam>,
|
|
263
|
+
'queryFn' | 'select'
|
|
337
264
|
>,
|
|
338
265
|
AdditionalQueryOptions<TFnData, TVariables, TPageParam> {
|
|
339
|
-
use?: Middleware<
|
|
266
|
+
use?: Middleware<
|
|
267
|
+
InfiniteQueryHook<
|
|
268
|
+
Clone<TFnData>,
|
|
269
|
+
Clone<TVariables>,
|
|
270
|
+
Clone<TError>,
|
|
271
|
+
Clone<TPageParam>
|
|
272
|
+
>
|
|
273
|
+
>[]
|
|
340
274
|
}
|
|
341
275
|
|
|
342
276
|
export interface InfiniteQueryHookOptions<
|
|
@@ -346,13 +280,7 @@ export interface InfiniteQueryHookOptions<
|
|
|
346
280
|
TVariables,
|
|
347
281
|
TPageParam = number
|
|
348
282
|
> extends Omit<
|
|
349
|
-
CompatibleUseInfiniteQueryOptions<
|
|
350
|
-
TFnData,
|
|
351
|
-
TVariables,
|
|
352
|
-
TData,
|
|
353
|
-
TError,
|
|
354
|
-
TPageParam
|
|
355
|
-
>,
|
|
283
|
+
CompatibleUseInfiniteQueryOptions<TFnData, TData, TError, TPageParam>,
|
|
356
284
|
| 'queryKey'
|
|
357
285
|
| 'queryFn'
|
|
358
286
|
| 'queryKeyHashFn'
|
|
@@ -425,14 +353,7 @@ export interface CreateSuspenseInfiniteQueryOptions<
|
|
|
425
353
|
TError = CompatibleError,
|
|
426
354
|
TPageParam = number
|
|
427
355
|
> extends Omit<
|
|
428
|
-
CompatibleUseInfiniteQueryOptions<
|
|
429
|
-
TFnData,
|
|
430
|
-
TVariables,
|
|
431
|
-
TFnData,
|
|
432
|
-
TError,
|
|
433
|
-
TPageParam
|
|
434
|
-
>,
|
|
435
|
-
| 'queryKey'
|
|
356
|
+
CompatibleUseInfiniteQueryOptions<TFnData, TFnData, TError, TPageParam>,
|
|
436
357
|
| 'queryFn'
|
|
437
358
|
| 'enabled'
|
|
438
359
|
| 'select'
|
|
@@ -443,7 +364,14 @@ export interface CreateSuspenseInfiniteQueryOptions<
|
|
|
443
364
|
| 'useErrorBoundary'
|
|
444
365
|
>,
|
|
445
366
|
AdditionalQueryOptions<TFnData, TVariables, TPageParam> {
|
|
446
|
-
use?: Middleware<
|
|
367
|
+
use?: Middleware<
|
|
368
|
+
SuspenseInfiniteQueryHook<
|
|
369
|
+
Clone<TFnData>,
|
|
370
|
+
Clone<TVariables>,
|
|
371
|
+
Clone<TError>,
|
|
372
|
+
Clone<TPageParam>
|
|
373
|
+
>
|
|
374
|
+
>[]
|
|
447
375
|
}
|
|
448
376
|
|
|
449
377
|
export interface SuspenseInfiniteQueryHookOptions<
|
|
@@ -453,13 +381,7 @@ export interface SuspenseInfiniteQueryHookOptions<
|
|
|
453
381
|
TVariables,
|
|
454
382
|
TPageParam = number
|
|
455
383
|
> extends Omit<
|
|
456
|
-
CompatibleUseInfiniteQueryOptions<
|
|
457
|
-
TFnData,
|
|
458
|
-
TVariables,
|
|
459
|
-
TData,
|
|
460
|
-
TError,
|
|
461
|
-
TPageParam
|
|
462
|
-
>,
|
|
384
|
+
CompatibleUseInfiniteQueryOptions<TFnData, TData, TError, TPageParam>,
|
|
463
385
|
| 'queryKey'
|
|
464
386
|
| 'queryFn'
|
|
465
387
|
| 'queryKeyHashFn'
|
|
@@ -517,7 +439,6 @@ export interface MutationHookOptions<TData, TError, TVariables, TContext>
|
|
|
517
439
|
'mutationFn' | 'mutationKey'
|
|
518
440
|
> {
|
|
519
441
|
use?: Middleware<MutationHook<TData, TError, TVariables>>[]
|
|
520
|
-
variables?: TVariables
|
|
521
442
|
}
|
|
522
443
|
|
|
523
444
|
export type MutationHookResult<
|
|
@@ -530,13 +451,20 @@ export type MutationHookResult<
|
|
|
530
451
|
export interface MutationHook<
|
|
531
452
|
TData = unknown,
|
|
532
453
|
TVariables = void,
|
|
533
|
-
TError = unknown
|
|
454
|
+
TError = unknown,
|
|
455
|
+
TDefaultContext = unknown
|
|
534
456
|
> {
|
|
535
|
-
<TContext>(
|
|
457
|
+
<TContext = TDefaultContext>(
|
|
536
458
|
options?: MutationHookOptions<TData, TError, TVariables, TContext>,
|
|
537
459
|
queryClient?: CompatibleWithV4<QueryClient, void>
|
|
538
460
|
): MutationHookResult<TData, TError, TVariables, TContext>
|
|
539
461
|
getKey: () => MutationKey | undefined
|
|
462
|
+
getOptions: () => UseMutationOptions<
|
|
463
|
+
TData,
|
|
464
|
+
TError,
|
|
465
|
+
TVariables,
|
|
466
|
+
TDefaultContext
|
|
467
|
+
>
|
|
540
468
|
mutationFn: MutationFunction<TData, TVariables>
|
|
541
469
|
}
|
|
542
470
|
|