react-query-kit 3.3.4 → 3.4.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 CHANGED
@@ -50,6 +50,7 @@ English | [简体中文](./README-zh_CN.md)
50
50
  - [TypeScript](#typescript)
51
51
  - [Type inference](#type-inference)
52
52
  - [Disabling Queries](#disabling-queries)
53
+ - [Strict Variables](#strict-variables)
53
54
  - [FAQ](#faq)
54
55
  - [Migration](#migration)
55
56
  - [Issues](#issues)
@@ -667,6 +668,35 @@ const queries = useQueries({
667
668
  })
668
669
  ```
669
670
 
671
+ ## Strict Variables
672
+
673
+ By default `variables` are optional at the call site, since they may also come from the definition, a middleware or the `QueryClient`'s default options. If your hooks always get their variables at the call site, register `strictVariables` to make TypeScript require them wherever the fetcher needs them:
674
+
675
+ ```ts
676
+ declare module 'react-query-kit' {
677
+ interface Register {
678
+ strictVariables: true
679
+ }
680
+ }
681
+ ```
682
+
683
+ ```ts
684
+ const usePost = createQuery({
685
+ queryKey: ['posts'],
686
+ fetcher: (variables: { id: number }): Promise<Data> => {
687
+ return fetch(`/posts/${variables.id}`).then(res => res.json())
688
+ },
689
+ })
690
+
691
+ usePost({ variables: { id: 1 } })
692
+ usePost({ variables: id ? { id } : skipToken })
693
+
694
+ usePost() // ❌ Expected 1-2 arguments, but got 0.
695
+ usePost({ enabled: !!id }) // ❌ Property 'variables' is missing
696
+ ```
697
+
698
+ This applies to the hooks of `createQuery`, `createInfiniteQuery`, `createSuspenseQuery`, `createSuspenseInfiniteQuery` and `router`, and to `inferOptions`. Hooks without variables are not affected. To keep `variables` optional for a fetcher that can run without them, include `undefined` in their type, e.g. `(variables: Variables | undefined) => ...`; an optional parameter `(variables?: Variables)` is inferred as `Variables`.
699
+
670
700
  ## FAQ
671
701
 
672
702
  ### What is the difference between `getFetchOptions` and `getOptions`?
@@ -11,6 +11,22 @@ type DeepPartial<T> = T extends object ? {
11
11
  [P in keyof T]?: DeepPartial<T[P]>;
12
12
  } : T;
13
13
  type DefaultTo<T, D> = unknown extends T ? D : T;
14
+ /**
15
+ * Register global options through declaration merging, e.g. set
16
+ * `strictVariables: true` to require `variables` at the call site.
17
+ */
18
+ export interface Register {
19
+ }
20
+ type RequiresVariables<TVariables> = Register extends {
21
+ strictVariables: true;
22
+ } ? undefined extends TVariables ? false : true : false;
23
+ type WithRequiredVariables<TOptions, TVariables> = RequiresVariables<TVariables> extends true ? TOptions & {
24
+ variables: CompatibleWithV4<TVariables | SkipToken, TVariables>;
25
+ } : TOptions;
26
+ type QueryHookArgs<TOptions, TVariables> = RequiresVariables<TVariables> extends true ? [
27
+ options: WithRequiredVariables<TOptions, TVariables>,
28
+ queryClient?: CompatibleWithV4<QueryClient, void>
29
+ ] : [options?: TOptions, queryClient?: CompatibleWithV4<QueryClient, void>];
14
30
  export type CompatibleError = CompatibleWithV4<DefaultError, Error>;
15
31
  export type Fetcher<TFnData, TVariables = void, TPageParam = never> = (variables: TVariables, context: QueryFunctionContext<QueryKey, TPageParam>) => TFnData | Promise<TFnData>;
16
32
  export type AdditionalQueryOptions<TFnData, TVariables, TPageParam = never> = {
@@ -37,6 +53,7 @@ export type ExposeMethods<TFnData, TVariables, TError, TPageParam = never> = {
37
53
  queryKeyHashFn?: QueryKeyHashFunction<QueryKey>;
38
54
  }> : CompatibleUseInfiniteQueryOptions<TFnData, TFnData, TError, TPageParam> & {
39
55
  queryKey: CompatibleWithV4<DataTag<QueryKey, InfiniteData<TFnData, TPageParam>>, QueryKey>;
56
+ queryFn?: Exclude<CompatibleUseInfiniteQueryOptions<TFnData, TFnData, TError, TPageParam>['queryFn'], SkipToken>;
40
57
  };
41
58
  };
42
59
  type Clone<T> = T extends infer TClone ? TClone : never;
@@ -55,8 +72,8 @@ export interface DefinedQueryHookOptions<TFnData, TError, TData, TVariables> ext
55
72
  export type QueryHookResult<TData, TError> = UseQueryResult<TData, TError>;
56
73
  export type DefinedQueryHookResult<TData, TError> = DefinedUseQueryResult<TData, TError>;
57
74
  export interface QueryHook<TFnData = unknown, TVariables = void, TError = CompatibleError> extends ExposeMethods<TFnData, TVariables, TError> {
58
- <TData = TFnData>(options: DefinedQueryHookOptions<TFnData, TError, TData, TVariables>, queryClient?: CompatibleWithV4<QueryClient, void>): DefinedQueryHookResult<TData, TError>;
59
- <TData = TFnData>(options?: QueryHookOptions<TFnData, TError, TData, TVariables>, queryClient?: CompatibleWithV4<QueryClient, void>): QueryHookResult<TData, TError>;
75
+ <TData = TFnData>(options: WithRequiredVariables<DefinedQueryHookOptions<TFnData, TError, TData, TVariables>, TVariables>, queryClient?: CompatibleWithV4<QueryClient, void>): DefinedQueryHookResult<TData, TError>;
76
+ <TData = TFnData>(...args: QueryHookArgs<QueryHookOptions<TFnData, TError, TData, TVariables>, TVariables>): QueryHookResult<TData, TError>;
60
77
  }
61
78
  export interface CreateSuspenseQueryOptions<TFnData = unknown, TVariables = void, TError = CompatibleError> extends Omit<UseQueryOptions<TFnData, TError, TFnData, QueryKey>, 'queryKey' | 'queryFn' | 'enabled' | 'select' | 'suspense' | 'throwOnError' | 'placeholderData' | 'keepPreviousData' | 'useErrorBoundary'>, AdditionalQueryOptions<TFnData, TVariables> {
62
79
  queryKey: QueryKey;
@@ -69,7 +86,7 @@ export interface SuspenseQueryHookOptions<TFnData, TError, TData, TVariables> ex
69
86
  }
70
87
  export type SuspenseQueryHookResult<TData, TError> = Omit<QueryObserverSuccessResult<TData, TError>, 'isPlaceholderData' | 'isPreviousData'>;
71
88
  export interface SuspenseQueryHook<TFnData = unknown, TVariables = void, TError = CompatibleError> extends ExposeMethods<TFnData, TVariables, TError> {
72
- <TData = TFnData>(options?: SuspenseQueryHookOptions<TFnData, TError, TData, TVariables>, queryClient?: CompatibleWithV4<QueryClient, void>): SuspenseQueryHookResult<TData, TError>;
89
+ <TData = TFnData>(...args: QueryHookArgs<SuspenseQueryHookOptions<TFnData, TError, TData, TVariables>, TVariables>): SuspenseQueryHookResult<TData, TError>;
73
90
  }
74
91
  export interface CreateInfiniteQueryOptions<TFnData = unknown, TVariables = void, TError = CompatibleError, TPageParam = number> extends Omit<CompatibleUseInfiniteQueryOptions<TFnData, TFnData, TError, TPageParam>, 'queryKey' | 'queryFn' | 'select'>, AdditionalQueryOptions<TFnData, TVariables, TPageParam> {
75
92
  queryKey: QueryKey;
@@ -86,8 +103,8 @@ export interface DefinedInfiniteQueryHookOptions<TFnData, TError, TData, TVariab
86
103
  export type InfiniteQueryHookResult<TData, TError> = UseInfiniteQueryResult<TData, TError>;
87
104
  export type DefinedInfiniteQueryHookResult<TData, TError> = CompatibleWithV4<DefinedUseInfiniteQueryResult<TData, TError>, WithRequired<UseInfiniteQueryResult<TData, TError>, 'data'>>;
88
105
  export interface InfiniteQueryHook<TFnData = unknown, TVariables = void, TError = CompatibleError, TPageParam = number> extends ExposeMethods<TFnData, TVariables, TError, TPageParam> {
89
- <TData = CompatibleWithV4<InfiniteData<TFnData, TPageParam>, TFnData>>(options: DefinedInfiniteQueryHookOptions<TFnData, TError, TData, TVariables, TPageParam>, queryClient?: CompatibleWithV4<QueryClient, void>): DefinedInfiniteQueryHookResult<TData, TError>;
90
- <TData = CompatibleWithV4<InfiniteData<TFnData, TPageParam>, TFnData>>(options?: InfiniteQueryHookOptions<TFnData, TError, TData, TVariables, TPageParam>, queryClient?: CompatibleWithV4<QueryClient, void>): InfiniteQueryHookResult<TData, TError>;
106
+ <TData = CompatibleWithV4<InfiniteData<TFnData, TPageParam>, TFnData>>(options: WithRequiredVariables<DefinedInfiniteQueryHookOptions<TFnData, TError, TData, TVariables, TPageParam>, TVariables>, queryClient?: CompatibleWithV4<QueryClient, void>): DefinedInfiniteQueryHookResult<TData, TError>;
107
+ <TData = CompatibleWithV4<InfiniteData<TFnData, TPageParam>, TFnData>>(...args: QueryHookArgs<InfiniteQueryHookOptions<TFnData, TError, TData, TVariables, TPageParam>, TVariables>): InfiniteQueryHookResult<TData, TError>;
91
108
  }
92
109
  export interface CreateSuspenseInfiniteQueryOptions<TFnData = unknown, TVariables = void, TError = CompatibleError, TPageParam = number> extends Omit<CompatibleUseInfiniteQueryOptions<TFnData, TFnData, TError, TPageParam>, 'queryKey' | 'queryFn' | 'enabled' | 'select' | 'suspense' | 'throwOnError' | 'placeholderData' | 'keepPreviousData' | 'useErrorBoundary'>, AdditionalQueryOptions<TFnData, TVariables, TPageParam> {
93
110
  queryKey: QueryKey;
@@ -100,7 +117,7 @@ export interface SuspenseInfiniteQueryHookOptions<TFnData, TError, TData, TVaria
100
117
  }
101
118
  export type SuspenseInfiniteQueryHookResult<TData, TError> = Omit<InfiniteQueryObserverSuccessResult<TData, TError>, 'isPlaceholderData' | 'isPreviousData'>;
102
119
  export interface SuspenseInfiniteQueryHook<TFnData = unknown, TVariables = void, TError = CompatibleError, TPageParam = number> extends ExposeMethods<TFnData, TVariables, TError, TPageParam> {
103
- <TData = CompatibleWithV4<InfiniteData<TFnData, TPageParam>, TFnData>>(options?: SuspenseInfiniteQueryHookOptions<TFnData, TError, TData, TVariables, TPageParam>, queryClient?: CompatibleWithV4<QueryClient, void>): SuspenseInfiniteQueryHookResult<TData, TError>;
120
+ <TData = CompatibleWithV4<InfiniteData<TFnData, TPageParam>, TFnData>>(...args: QueryHookArgs<SuspenseInfiniteQueryHookOptions<TFnData, TError, TData, TVariables, TPageParam>, TVariables>): SuspenseInfiniteQueryHookResult<TData, TError>;
104
121
  }
105
122
  export interface CreateMutationOptions<TData = unknown, TVariables = void, TError = CompatibleError, TContext = unknown> extends UseMutationOptions<TData, TError, TVariables, TContext> {
106
123
  use?: Middleware<MutationHook<TData, TVariables, TError>>[];
@@ -130,7 +147,7 @@ export type inferFnData<T> = T extends {
130
147
  fetcher: ExposeFetcher<infer TFnData, any, infer _TPageParam>;
131
148
  } ? TFnData : T extends ExposeMutationMethods<infer TFnData, any, any, any> ? TFnData : never;
132
149
  export type inferError<T> = T extends ExposeMethods<any, any, infer TError> ? TError : T extends ExposeMethods<any, any, infer TError, any> ? TError : T extends ExposeMutationMethods<any, any, infer TError, any> ? TError : never;
133
- export type inferOptions<T> = T extends QueryHook<infer TFnData, infer TVariables, infer TError> ? QueryHookOptions<TFnData, TError, TFnData, TVariables> : T extends SuspenseQueryHook<infer TFnData, infer TVariables, infer TError> ? SuspenseQueryHookOptions<TFnData, TError, TFnData, TVariables> : T extends InfiniteQueryHook<infer TFnData, infer TVariables, infer TError, infer TPageParam> ? InfiniteQueryHookOptions<TFnData, TError, CompatibleWithV4<InfiniteData<TFnData, TPageParam>, TFnData>, TVariables, TPageParam> : T extends SuspenseInfiniteQueryHook<infer TFnData, infer TVariables, infer TError, infer TPageParam> ? SuspenseInfiniteQueryHookOptions<TFnData, TError, CompatibleWithV4<InfiniteData<TFnData, TPageParam>, TFnData>, TVariables, TPageParam> : T extends MutationHook<infer TFnData, infer TVariables, infer TError> ? MutationHookOptions<TFnData, TError, TVariables, unknown> : never;
150
+ export type inferOptions<T> = T extends QueryHook<infer TFnData, infer TVariables, infer TError> ? WithRequiredVariables<QueryHookOptions<TFnData, TError, TFnData, TVariables>, TVariables> : T extends SuspenseQueryHook<infer TFnData, infer TVariables, infer TError> ? WithRequiredVariables<SuspenseQueryHookOptions<TFnData, TError, TFnData, TVariables>, TVariables> : T extends InfiniteQueryHook<infer TFnData, infer TVariables, infer TError, infer TPageParam> ? WithRequiredVariables<InfiniteQueryHookOptions<TFnData, TError, CompatibleWithV4<InfiniteData<TFnData, TPageParam>, TFnData>, TVariables, TPageParam>, TVariables> : T extends SuspenseInfiniteQueryHook<infer TFnData, infer TVariables, infer TError, infer TPageParam> ? WithRequiredVariables<SuspenseInfiniteQueryHookOptions<TFnData, TError, CompatibleWithV4<InfiniteData<TFnData, TPageParam>, TFnData>, TVariables, TPageParam>, TVariables> : T extends MutationHook<infer TFnData, infer TVariables, infer TError> ? MutationHookOptions<TFnData, TError, TVariables, unknown> : never;
134
151
  export type inferCreateOptions<T> = T extends QueryHook<infer TFnData, infer TVariables, infer TError> ? CreateQueryOptions<TFnData, TVariables, TError> : T extends SuspenseQueryHook<infer TFnData, infer TVariables, infer TError> ? CreateSuspenseQueryOptions<TFnData, TVariables, TError> : T extends InfiniteQueryHook<infer TFnData, infer TVariables, infer TError, infer TPageParam> ? CreateInfiniteQueryOptions<TFnData, TVariables, TError, TPageParam> : T extends SuspenseInfiniteQueryHook<infer TFnData, infer TVariables, infer TError, infer TPageParam> ? CreateSuspenseInfiniteQueryOptions<TFnData, TVariables, TError, TPageParam> : T extends MutationHook<infer TFnData, infer TVariables, infer TError, infer TContext> ? CreateMutationOptions<TFnData, TVariables, TError, TContext> : never;
135
152
  export type RouterQueryOptions<TFnData, TVariables = void, TError = CompatibleError> = Omit<CreateQueryOptions<TFnData, TVariables, TError>, 'queryKey'>;
136
153
  export type RouterQuery<TFnData, TVariables = void, TError = CompatibleError> = RouterQueryOptions<TFnData, TVariables, TError> & {
@@ -4014,7 +4014,7 @@ var drawChart = (function (exports) {
4014
4014
  </script>
4015
4015
  <script>
4016
4016
  /*<!--*/
4017
- const data = {"version":2,"tree":{"name":"root","children":[{"name":"index.production.js","children":[{"name":"src","children":[{"uid":"efa5-1","name":"utils.ts"},{"uid":"efa5-3","name":"createBaseQuery.ts"},{"uid":"efa5-5","name":"createQuery.ts"},{"uid":"efa5-7","name":"createSuspenseQuery.ts"},{"uid":"efa5-9","name":"createInfiniteQuery.ts"},{"uid":"efa5-11","name":"createSuspenseInfiniteQuery.ts"},{"uid":"efa5-13","name":"createMutation.ts"},{"uid":"efa5-15","name":"router.ts"},{"uid":"efa5-17","name":"index.ts"}]}]}],"isRoot":true},"nodeParts":{"efa5-1":{"renderedLength":925,"gzipLength":457,"brotliLength":0,"mainUid":"efa5-0"},"efa5-3":{"renderedLength":1450,"gzipLength":443,"brotliLength":0,"mainUid":"efa5-2"},"efa5-5":{"renderedLength":95,"gzipLength":84,"brotliLength":0,"mainUid":"efa5-4"},"efa5-7":{"renderedLength":183,"gzipLength":115,"brotliLength":0,"mainUid":"efa5-6"},"efa5-9":{"renderedLength":111,"gzipLength":93,"brotliLength":0,"mainUid":"efa5-8"},"efa5-11":{"renderedLength":207,"gzipLength":125,"brotliLength":0,"mainUid":"efa5-10"},"efa5-13":{"renderedLength":283,"gzipLength":170,"brotliLength":0,"mainUid":"efa5-12"},"efa5-15":{"renderedLength":1574,"gzipLength":505,"brotliLength":0,"mainUid":"efa5-14"},"efa5-17":{"renderedLength":0,"gzipLength":0,"brotliLength":0,"mainUid":"efa5-16"}},"nodeMetas":{"efa5-0":{"id":"/src/utils.ts","moduleParts":{"index.production.js":"efa5-1"},"imported":[{"uid":"efa5-19"}],"importedBy":[{"uid":"efa5-16"},{"uid":"efa5-4"},{"uid":"efa5-6"},{"uid":"efa5-8"},{"uid":"efa5-10"},{"uid":"efa5-12"},{"uid":"efa5-2"}]},"efa5-2":{"id":"/src/createBaseQuery.ts","moduleParts":{"index.production.js":"efa5-3"},"imported":[{"uid":"efa5-0"}],"importedBy":[{"uid":"efa5-4"},{"uid":"efa5-6"},{"uid":"efa5-8"},{"uid":"efa5-10"}]},"efa5-4":{"id":"/src/createQuery.ts","moduleParts":{"index.production.js":"efa5-5"},"imported":[{"uid":"efa5-2"},{"uid":"efa5-0"}],"importedBy":[{"uid":"efa5-16"},{"uid":"efa5-14"}]},"efa5-6":{"id":"/src/createSuspenseQuery.ts","moduleParts":{"index.production.js":"efa5-7"},"imported":[{"uid":"efa5-2"},{"uid":"efa5-0"}],"importedBy":[{"uid":"efa5-16"},{"uid":"efa5-14"}]},"efa5-8":{"id":"/src/createInfiniteQuery.ts","moduleParts":{"index.production.js":"efa5-9"},"imported":[{"uid":"efa5-2"},{"uid":"efa5-0"}],"importedBy":[{"uid":"efa5-16"},{"uid":"efa5-14"}]},"efa5-10":{"id":"/src/createSuspenseInfiniteQuery.ts","moduleParts":{"index.production.js":"efa5-11"},"imported":[{"uid":"efa5-2"},{"uid":"efa5-0"}],"importedBy":[{"uid":"efa5-16"},{"uid":"efa5-14"}]},"efa5-12":{"id":"/src/createMutation.ts","moduleParts":{"index.production.js":"efa5-13"},"imported":[{"uid":"efa5-0"}],"importedBy":[{"uid":"efa5-16"},{"uid":"efa5-14"}]},"efa5-14":{"id":"/src/router.ts","moduleParts":{"index.production.js":"efa5-15"},"imported":[{"uid":"efa5-8"},{"uid":"efa5-12"},{"uid":"efa5-4"},{"uid":"efa5-10"},{"uid":"efa5-6"}],"importedBy":[{"uid":"efa5-16"}]},"efa5-16":{"id":"/src/index.ts","moduleParts":{"index.production.js":"efa5-17"},"imported":[{"uid":"efa5-4"},{"uid":"efa5-6"},{"uid":"efa5-8"},{"uid":"efa5-10"},{"uid":"efa5-12"},{"uid":"efa5-18"},{"uid":"efa5-14"},{"uid":"efa5-0"}],"importedBy":[],"isEntry":true},"efa5-18":{"id":"/src/types.ts","moduleParts":{},"imported":[],"importedBy":[{"uid":"efa5-16"}]},"efa5-19":{"id":"@tanstack/react-query","moduleParts":{},"imported":[],"importedBy":[{"uid":"efa5-0"}],"isExternal":true}},"env":{"rollup":"2.77.2"},"options":{"gzip":true,"brotli":false,"sourcemap":false}};
4017
+ const data = {"version":2,"tree":{"name":"root","children":[{"name":"index.production.js","children":[{"name":"src","children":[{"uid":"372a-1","name":"utils.ts"},{"uid":"372a-3","name":"createBaseQuery.ts"},{"uid":"372a-5","name":"createQuery.ts"},{"uid":"372a-7","name":"createSuspenseQuery.ts"},{"uid":"372a-9","name":"createInfiniteQuery.ts"},{"uid":"372a-11","name":"createSuspenseInfiniteQuery.ts"},{"uid":"372a-13","name":"createMutation.ts"},{"uid":"372a-15","name":"router.ts"},{"uid":"372a-17","name":"index.ts"}]}]}],"isRoot":true},"nodeParts":{"372a-1":{"renderedLength":925,"gzipLength":457,"brotliLength":0,"mainUid":"372a-0"},"372a-3":{"renderedLength":1450,"gzipLength":443,"brotliLength":0,"mainUid":"372a-2"},"372a-5":{"renderedLength":95,"gzipLength":84,"brotliLength":0,"mainUid":"372a-4"},"372a-7":{"renderedLength":183,"gzipLength":115,"brotliLength":0,"mainUid":"372a-6"},"372a-9":{"renderedLength":111,"gzipLength":93,"brotliLength":0,"mainUid":"372a-8"},"372a-11":{"renderedLength":207,"gzipLength":125,"brotliLength":0,"mainUid":"372a-10"},"372a-13":{"renderedLength":283,"gzipLength":170,"brotliLength":0,"mainUid":"372a-12"},"372a-15":{"renderedLength":1574,"gzipLength":505,"brotliLength":0,"mainUid":"372a-14"},"372a-17":{"renderedLength":0,"gzipLength":0,"brotliLength":0,"mainUid":"372a-16"}},"nodeMetas":{"372a-0":{"id":"/src/utils.ts","moduleParts":{"index.production.js":"372a-1"},"imported":[{"uid":"372a-19"}],"importedBy":[{"uid":"372a-16"},{"uid":"372a-4"},{"uid":"372a-6"},{"uid":"372a-8"},{"uid":"372a-10"},{"uid":"372a-12"},{"uid":"372a-2"}]},"372a-2":{"id":"/src/createBaseQuery.ts","moduleParts":{"index.production.js":"372a-3"},"imported":[{"uid":"372a-0"}],"importedBy":[{"uid":"372a-4"},{"uid":"372a-6"},{"uid":"372a-8"},{"uid":"372a-10"}]},"372a-4":{"id":"/src/createQuery.ts","moduleParts":{"index.production.js":"372a-5"},"imported":[{"uid":"372a-2"},{"uid":"372a-0"}],"importedBy":[{"uid":"372a-16"},{"uid":"372a-14"}]},"372a-6":{"id":"/src/createSuspenseQuery.ts","moduleParts":{"index.production.js":"372a-7"},"imported":[{"uid":"372a-2"},{"uid":"372a-0"}],"importedBy":[{"uid":"372a-16"},{"uid":"372a-14"}]},"372a-8":{"id":"/src/createInfiniteQuery.ts","moduleParts":{"index.production.js":"372a-9"},"imported":[{"uid":"372a-2"},{"uid":"372a-0"}],"importedBy":[{"uid":"372a-16"},{"uid":"372a-14"}]},"372a-10":{"id":"/src/createSuspenseInfiniteQuery.ts","moduleParts":{"index.production.js":"372a-11"},"imported":[{"uid":"372a-2"},{"uid":"372a-0"}],"importedBy":[{"uid":"372a-16"},{"uid":"372a-14"}]},"372a-12":{"id":"/src/createMutation.ts","moduleParts":{"index.production.js":"372a-13"},"imported":[{"uid":"372a-0"}],"importedBy":[{"uid":"372a-16"},{"uid":"372a-14"}]},"372a-14":{"id":"/src/router.ts","moduleParts":{"index.production.js":"372a-15"},"imported":[{"uid":"372a-8"},{"uid":"372a-12"},{"uid":"372a-4"},{"uid":"372a-10"},{"uid":"372a-6"}],"importedBy":[{"uid":"372a-16"}]},"372a-16":{"id":"/src/index.ts","moduleParts":{"index.production.js":"372a-17"},"imported":[{"uid":"372a-4"},{"uid":"372a-6"},{"uid":"372a-8"},{"uid":"372a-10"},{"uid":"372a-12"},{"uid":"372a-18"},{"uid":"372a-14"},{"uid":"372a-0"}],"importedBy":[],"isEntry":true},"372a-18":{"id":"/src/types.ts","moduleParts":{},"imported":[],"importedBy":[{"uid":"372a-16"}]},"372a-19":{"id":"@tanstack/react-query","moduleParts":{},"imported":[],"importedBy":[{"uid":"372a-0"}],"isExternal":true}},"env":{"rollup":"2.77.2"},"options":{"gzip":true,"brotli":false,"sourcemap":false}};
4018
4018
 
4019
4019
  const run = () => {
4020
4020
  const width = window.innerWidth;
package/build/stats.json CHANGED
@@ -10,39 +10,39 @@
10
10
  "name": "src",
11
11
  "children": [
12
12
  {
13
- "uid": "efa5-21",
13
+ "uid": "372a-21",
14
14
  "name": "utils.ts"
15
15
  },
16
16
  {
17
- "uid": "efa5-23",
17
+ "uid": "372a-23",
18
18
  "name": "createBaseQuery.ts"
19
19
  },
20
20
  {
21
- "uid": "efa5-25",
21
+ "uid": "372a-25",
22
22
  "name": "createQuery.ts"
23
23
  },
24
24
  {
25
- "uid": "efa5-27",
25
+ "uid": "372a-27",
26
26
  "name": "createSuspenseQuery.ts"
27
27
  },
28
28
  {
29
- "uid": "efa5-29",
29
+ "uid": "372a-29",
30
30
  "name": "createInfiniteQuery.ts"
31
31
  },
32
32
  {
33
- "uid": "efa5-31",
33
+ "uid": "372a-31",
34
34
  "name": "createSuspenseInfiniteQuery.ts"
35
35
  },
36
36
  {
37
- "uid": "efa5-33",
37
+ "uid": "372a-33",
38
38
  "name": "createMutation.ts"
39
39
  },
40
40
  {
41
- "uid": "efa5-35",
41
+ "uid": "372a-35",
42
42
  "name": "router.ts"
43
43
  },
44
44
  {
45
- "uid": "efa5-37",
45
+ "uid": "372a-37",
46
46
  "name": "index.ts"
47
47
  }
48
48
  ]
@@ -53,307 +53,307 @@
53
53
  "isRoot": true
54
54
  },
55
55
  "nodeParts": {
56
- "efa5-21": {
56
+ "372a-21": {
57
57
  "renderedLength": 925,
58
58
  "gzipLength": 457,
59
59
  "brotliLength": 0,
60
- "mainUid": "efa5-20"
60
+ "mainUid": "372a-20"
61
61
  },
62
- "efa5-23": {
62
+ "372a-23": {
63
63
  "renderedLength": 1450,
64
64
  "gzipLength": 443,
65
65
  "brotliLength": 0,
66
- "mainUid": "efa5-22"
66
+ "mainUid": "372a-22"
67
67
  },
68
- "efa5-25": {
68
+ "372a-25": {
69
69
  "renderedLength": 95,
70
70
  "gzipLength": 84,
71
71
  "brotliLength": 0,
72
- "mainUid": "efa5-24"
72
+ "mainUid": "372a-24"
73
73
  },
74
- "efa5-27": {
74
+ "372a-27": {
75
75
  "renderedLength": 183,
76
76
  "gzipLength": 115,
77
77
  "brotliLength": 0,
78
- "mainUid": "efa5-26"
78
+ "mainUid": "372a-26"
79
79
  },
80
- "efa5-29": {
80
+ "372a-29": {
81
81
  "renderedLength": 111,
82
82
  "gzipLength": 93,
83
83
  "brotliLength": 0,
84
- "mainUid": "efa5-28"
84
+ "mainUid": "372a-28"
85
85
  },
86
- "efa5-31": {
86
+ "372a-31": {
87
87
  "renderedLength": 207,
88
88
  "gzipLength": 125,
89
89
  "brotliLength": 0,
90
- "mainUid": "efa5-30"
90
+ "mainUid": "372a-30"
91
91
  },
92
- "efa5-33": {
92
+ "372a-33": {
93
93
  "renderedLength": 283,
94
94
  "gzipLength": 170,
95
95
  "brotliLength": 0,
96
- "mainUid": "efa5-32"
96
+ "mainUid": "372a-32"
97
97
  },
98
- "efa5-35": {
98
+ "372a-35": {
99
99
  "renderedLength": 1574,
100
100
  "gzipLength": 505,
101
101
  "brotliLength": 0,
102
- "mainUid": "efa5-34"
102
+ "mainUid": "372a-34"
103
103
  },
104
- "efa5-37": {
104
+ "372a-37": {
105
105
  "renderedLength": 0,
106
106
  "gzipLength": 0,
107
107
  "brotliLength": 0,
108
- "mainUid": "efa5-36"
108
+ "mainUid": "372a-36"
109
109
  }
110
110
  },
111
111
  "nodeMetas": {
112
- "efa5-20": {
112
+ "372a-20": {
113
113
  "id": "/src/utils.ts",
114
114
  "moduleParts": {
115
- "index.production.js": "efa5-21"
115
+ "index.production.js": "372a-21"
116
116
  },
117
117
  "imported": [
118
118
  {
119
- "uid": "efa5-39"
119
+ "uid": "372a-39"
120
120
  }
121
121
  ],
122
122
  "importedBy": [
123
123
  {
124
- "uid": "efa5-36"
124
+ "uid": "372a-36"
125
125
  },
126
126
  {
127
- "uid": "efa5-24"
127
+ "uid": "372a-24"
128
128
  },
129
129
  {
130
- "uid": "efa5-26"
130
+ "uid": "372a-26"
131
131
  },
132
132
  {
133
- "uid": "efa5-28"
133
+ "uid": "372a-28"
134
134
  },
135
135
  {
136
- "uid": "efa5-30"
136
+ "uid": "372a-30"
137
137
  },
138
138
  {
139
- "uid": "efa5-32"
139
+ "uid": "372a-32"
140
140
  },
141
141
  {
142
- "uid": "efa5-22"
142
+ "uid": "372a-22"
143
143
  }
144
144
  ]
145
145
  },
146
- "efa5-22": {
146
+ "372a-22": {
147
147
  "id": "/src/createBaseQuery.ts",
148
148
  "moduleParts": {
149
- "index.production.js": "efa5-23"
149
+ "index.production.js": "372a-23"
150
150
  },
151
151
  "imported": [
152
152
  {
153
- "uid": "efa5-20"
153
+ "uid": "372a-20"
154
154
  }
155
155
  ],
156
156
  "importedBy": [
157
157
  {
158
- "uid": "efa5-24"
158
+ "uid": "372a-24"
159
159
  },
160
160
  {
161
- "uid": "efa5-26"
161
+ "uid": "372a-26"
162
162
  },
163
163
  {
164
- "uid": "efa5-28"
164
+ "uid": "372a-28"
165
165
  },
166
166
  {
167
- "uid": "efa5-30"
167
+ "uid": "372a-30"
168
168
  }
169
169
  ]
170
170
  },
171
- "efa5-24": {
171
+ "372a-24": {
172
172
  "id": "/src/createQuery.ts",
173
173
  "moduleParts": {
174
- "index.production.js": "efa5-25"
174
+ "index.production.js": "372a-25"
175
175
  },
176
176
  "imported": [
177
177
  {
178
- "uid": "efa5-22"
178
+ "uid": "372a-22"
179
179
  },
180
180
  {
181
- "uid": "efa5-20"
181
+ "uid": "372a-20"
182
182
  }
183
183
  ],
184
184
  "importedBy": [
185
185
  {
186
- "uid": "efa5-36"
186
+ "uid": "372a-36"
187
187
  },
188
188
  {
189
- "uid": "efa5-34"
189
+ "uid": "372a-34"
190
190
  }
191
191
  ]
192
192
  },
193
- "efa5-26": {
193
+ "372a-26": {
194
194
  "id": "/src/createSuspenseQuery.ts",
195
195
  "moduleParts": {
196
- "index.production.js": "efa5-27"
196
+ "index.production.js": "372a-27"
197
197
  },
198
198
  "imported": [
199
199
  {
200
- "uid": "efa5-22"
200
+ "uid": "372a-22"
201
201
  },
202
202
  {
203
- "uid": "efa5-20"
203
+ "uid": "372a-20"
204
204
  }
205
205
  ],
206
206
  "importedBy": [
207
207
  {
208
- "uid": "efa5-36"
208
+ "uid": "372a-36"
209
209
  },
210
210
  {
211
- "uid": "efa5-34"
211
+ "uid": "372a-34"
212
212
  }
213
213
  ]
214
214
  },
215
- "efa5-28": {
215
+ "372a-28": {
216
216
  "id": "/src/createInfiniteQuery.ts",
217
217
  "moduleParts": {
218
- "index.production.js": "efa5-29"
218
+ "index.production.js": "372a-29"
219
219
  },
220
220
  "imported": [
221
221
  {
222
- "uid": "efa5-22"
222
+ "uid": "372a-22"
223
223
  },
224
224
  {
225
- "uid": "efa5-20"
225
+ "uid": "372a-20"
226
226
  }
227
227
  ],
228
228
  "importedBy": [
229
229
  {
230
- "uid": "efa5-36"
230
+ "uid": "372a-36"
231
231
  },
232
232
  {
233
- "uid": "efa5-34"
233
+ "uid": "372a-34"
234
234
  }
235
235
  ]
236
236
  },
237
- "efa5-30": {
237
+ "372a-30": {
238
238
  "id": "/src/createSuspenseInfiniteQuery.ts",
239
239
  "moduleParts": {
240
- "index.production.js": "efa5-31"
240
+ "index.production.js": "372a-31"
241
241
  },
242
242
  "imported": [
243
243
  {
244
- "uid": "efa5-22"
244
+ "uid": "372a-22"
245
245
  },
246
246
  {
247
- "uid": "efa5-20"
247
+ "uid": "372a-20"
248
248
  }
249
249
  ],
250
250
  "importedBy": [
251
251
  {
252
- "uid": "efa5-36"
252
+ "uid": "372a-36"
253
253
  },
254
254
  {
255
- "uid": "efa5-34"
255
+ "uid": "372a-34"
256
256
  }
257
257
  ]
258
258
  },
259
- "efa5-32": {
259
+ "372a-32": {
260
260
  "id": "/src/createMutation.ts",
261
261
  "moduleParts": {
262
- "index.production.js": "efa5-33"
262
+ "index.production.js": "372a-33"
263
263
  },
264
264
  "imported": [
265
265
  {
266
- "uid": "efa5-20"
266
+ "uid": "372a-20"
267
267
  }
268
268
  ],
269
269
  "importedBy": [
270
270
  {
271
- "uid": "efa5-36"
271
+ "uid": "372a-36"
272
272
  },
273
273
  {
274
- "uid": "efa5-34"
274
+ "uid": "372a-34"
275
275
  }
276
276
  ]
277
277
  },
278
- "efa5-34": {
278
+ "372a-34": {
279
279
  "id": "/src/router.ts",
280
280
  "moduleParts": {
281
- "index.production.js": "efa5-35"
281
+ "index.production.js": "372a-35"
282
282
  },
283
283
  "imported": [
284
284
  {
285
- "uid": "efa5-28"
285
+ "uid": "372a-28"
286
286
  },
287
287
  {
288
- "uid": "efa5-32"
288
+ "uid": "372a-32"
289
289
  },
290
290
  {
291
- "uid": "efa5-24"
291
+ "uid": "372a-24"
292
292
  },
293
293
  {
294
- "uid": "efa5-30"
294
+ "uid": "372a-30"
295
295
  },
296
296
  {
297
- "uid": "efa5-26"
297
+ "uid": "372a-26"
298
298
  }
299
299
  ],
300
300
  "importedBy": [
301
301
  {
302
- "uid": "efa5-36"
302
+ "uid": "372a-36"
303
303
  }
304
304
  ]
305
305
  },
306
- "efa5-36": {
306
+ "372a-36": {
307
307
  "id": "/src/index.ts",
308
308
  "moduleParts": {
309
- "index.production.js": "efa5-37"
309
+ "index.production.js": "372a-37"
310
310
  },
311
311
  "imported": [
312
312
  {
313
- "uid": "efa5-24"
313
+ "uid": "372a-24"
314
314
  },
315
315
  {
316
- "uid": "efa5-26"
316
+ "uid": "372a-26"
317
317
  },
318
318
  {
319
- "uid": "efa5-28"
319
+ "uid": "372a-28"
320
320
  },
321
321
  {
322
- "uid": "efa5-30"
322
+ "uid": "372a-30"
323
323
  },
324
324
  {
325
- "uid": "efa5-32"
325
+ "uid": "372a-32"
326
326
  },
327
327
  {
328
- "uid": "efa5-38"
328
+ "uid": "372a-38"
329
329
  },
330
330
  {
331
- "uid": "efa5-34"
331
+ "uid": "372a-34"
332
332
  },
333
333
  {
334
- "uid": "efa5-20"
334
+ "uid": "372a-20"
335
335
  }
336
336
  ],
337
337
  "importedBy": [],
338
338
  "isEntry": true
339
339
  },
340
- "efa5-38": {
340
+ "372a-38": {
341
341
  "id": "/src/types.ts",
342
342
  "moduleParts": {},
343
343
  "imported": [],
344
344
  "importedBy": [
345
345
  {
346
- "uid": "efa5-36"
346
+ "uid": "372a-36"
347
347
  }
348
348
  ]
349
349
  },
350
- "efa5-39": {
350
+ "372a-39": {
351
351
  "id": "@tanstack/react-query",
352
352
  "moduleParts": {},
353
353
  "imported": [],
354
354
  "importedBy": [
355
355
  {
356
- "uid": "efa5-20"
356
+ "uid": "372a-20"
357
357
  }
358
358
  ],
359
359
  "isExternal": true
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-query-kit",
3
- "version": "3.3.4",
3
+ "version": "3.4.0",
4
4
  "description": "🕊️ A toolkit for ReactQuery that make ReactQuery hooks more reusable and typesafe",
5
5
  "author": "liaoliao666",
6
6
  "repository": "liaoliao666/react-query-kit",
@@ -66,7 +66,7 @@
66
66
  "sideEffects": false,
67
67
  "scripts": {
68
68
  "build": "rollup --config rollup.config.js && npm run typecheck",
69
- "typecheck": "tsc -b && tsc -p tsconfig.types.json --pretty false",
69
+ "typecheck": "tsc -b && tsc -p tsconfig.types.json --pretty false && tsc -p tsconfig.types.strict.json --pretty false",
70
70
  "stats": "open ./build/stats-html.html",
71
71
  "eslint": "eslint --fix '*.{js,json}' '{src,tests,benchmarks}/**/*.{ts,tsx}'",
72
72
  "test": "jest"
package/src/types.ts CHANGED
@@ -78,6 +78,36 @@ type DeepPartial<T> = T extends object
78
78
 
79
79
  type DefaultTo<T, D> = unknown extends T ? D : T
80
80
 
81
+ /**
82
+ * Register global options through declaration merging, e.g. set
83
+ * `strictVariables: true` to require `variables` at the call site.
84
+ */
85
+ // eslint-disable-next-line @typescript-eslint/no-empty-interface
86
+ export interface Register {}
87
+
88
+ // Variables can only be left out when the fetcher accepts `undefined`,
89
+ // which is what it receives then
90
+ type RequiresVariables<TVariables> = Register extends { strictVariables: true }
91
+ ? undefined extends TVariables
92
+ ? false
93
+ : true
94
+ : false
95
+
96
+ type WithRequiredVariables<TOptions, TVariables> =
97
+ RequiresVariables<TVariables> extends true
98
+ ? TOptions & {
99
+ variables: CompatibleWithV4<TVariables | SkipToken, TVariables>
100
+ }
101
+ : TOptions
102
+
103
+ type QueryHookArgs<TOptions, TVariables> =
104
+ RequiresVariables<TVariables> extends true
105
+ ? [
106
+ options: WithRequiredVariables<TOptions, TVariables>,
107
+ queryClient?: CompatibleWithV4<QueryClient, void>
108
+ ]
109
+ : [options?: TOptions, queryClient?: CompatibleWithV4<QueryClient, void>]
110
+
81
111
  export type CompatibleError = CompatibleWithV4<DefaultError, Error>
82
112
 
83
113
  export type Fetcher<TFnData, TVariables = void, TPageParam = never> = (
@@ -166,6 +196,15 @@ export type ExposeMethods<TFnData, TVariables, TError, TPageParam = never> = {
166
196
  DataTag<QueryKey, InfiniteData<TFnData, TPageParam>>,
167
197
  QueryKey
168
198
  >
199
+ queryFn?: Exclude<
200
+ CompatibleUseInfiniteQueryOptions<
201
+ TFnData,
202
+ TFnData,
203
+ TError,
204
+ TPageParam
205
+ >['queryFn'],
206
+ SkipToken
207
+ >
169
208
  }
170
209
  }
171
210
 
@@ -219,12 +258,17 @@ export interface QueryHook<
219
258
  TError = CompatibleError
220
259
  > extends ExposeMethods<TFnData, TVariables, TError> {
221
260
  <TData = TFnData>(
222
- options: DefinedQueryHookOptions<TFnData, TError, TData, TVariables>,
261
+ options: WithRequiredVariables<
262
+ DefinedQueryHookOptions<TFnData, TError, TData, TVariables>,
263
+ TVariables
264
+ >,
223
265
  queryClient?: CompatibleWithV4<QueryClient, void>
224
266
  ): DefinedQueryHookResult<TData, TError>
225
267
  <TData = TFnData>(
226
- options?: QueryHookOptions<TFnData, TError, TData, TVariables>,
227
- queryClient?: CompatibleWithV4<QueryClient, void>
268
+ ...args: QueryHookArgs<
269
+ QueryHookOptions<TFnData, TError, TData, TVariables>,
270
+ TVariables
271
+ >
228
272
  ): QueryHookResult<TData, TError>
229
273
  }
230
274
 
@@ -282,8 +326,10 @@ export interface SuspenseQueryHook<
282
326
  TError = CompatibleError
283
327
  > extends ExposeMethods<TFnData, TVariables, TError> {
284
328
  <TData = TFnData>(
285
- options?: SuspenseQueryHookOptions<TFnData, TError, TData, TVariables>,
286
- queryClient?: CompatibleWithV4<QueryClient, void>
329
+ ...args: QueryHookArgs<
330
+ SuspenseQueryHookOptions<TFnData, TError, TData, TVariables>,
331
+ TVariables
332
+ >
287
333
  ): SuspenseQueryHookResult<TData, TError>
288
334
  }
289
335
 
@@ -362,24 +408,23 @@ export interface InfiniteQueryHook<
362
408
  TPageParam = number
363
409
  > extends ExposeMethods<TFnData, TVariables, TError, TPageParam> {
364
410
  <TData = CompatibleWithV4<InfiniteData<TFnData, TPageParam>, TFnData>>(
365
- options: DefinedInfiniteQueryHookOptions<
366
- TFnData,
367
- TError,
368
- TData,
369
- TVariables,
370
- TPageParam
411
+ options: WithRequiredVariables<
412
+ DefinedInfiniteQueryHookOptions<
413
+ TFnData,
414
+ TError,
415
+ TData,
416
+ TVariables,
417
+ TPageParam
418
+ >,
419
+ TVariables
371
420
  >,
372
421
  queryClient?: CompatibleWithV4<QueryClient, void>
373
422
  ): DefinedInfiniteQueryHookResult<TData, TError>
374
423
  <TData = CompatibleWithV4<InfiniteData<TFnData, TPageParam>, TFnData>>(
375
- options?: InfiniteQueryHookOptions<
376
- TFnData,
377
- TError,
378
- TData,
379
- TVariables,
380
- TPageParam
381
- >,
382
- queryClient?: CompatibleWithV4<QueryClient, void>
424
+ ...args: QueryHookArgs<
425
+ InfiniteQueryHookOptions<TFnData, TError, TData, TVariables, TPageParam>,
426
+ TVariables
427
+ >
383
428
  ): InfiniteQueryHookResult<TData, TError>
384
429
  }
385
430
 
@@ -452,14 +497,16 @@ export interface SuspenseInfiniteQueryHook<
452
497
  TPageParam = number
453
498
  > extends ExposeMethods<TFnData, TVariables, TError, TPageParam> {
454
499
  <TData = CompatibleWithV4<InfiniteData<TFnData, TPageParam>, TFnData>>(
455
- options?: SuspenseInfiniteQueryHookOptions<
456
- TFnData,
457
- TError,
458
- TData,
459
- TVariables,
460
- TPageParam
461
- >,
462
- queryClient?: CompatibleWithV4<QueryClient, void>
500
+ ...args: QueryHookArgs<
501
+ SuspenseInfiniteQueryHookOptions<
502
+ TFnData,
503
+ TError,
504
+ TData,
505
+ TVariables,
506
+ TPageParam
507
+ >,
508
+ TVariables
509
+ >
463
510
  ): SuspenseInfiniteQueryHookResult<TData, TError>
464
511
  }
465
512
 
@@ -570,21 +617,30 @@ export type inferOptions<T> = T extends QueryHook<
570
617
  infer TVariables,
571
618
  infer TError
572
619
  >
573
- ? QueryHookOptions<TFnData, TError, TFnData, TVariables>
620
+ ? WithRequiredVariables<
621
+ QueryHookOptions<TFnData, TError, TFnData, TVariables>,
622
+ TVariables
623
+ >
574
624
  : T extends SuspenseQueryHook<infer TFnData, infer TVariables, infer TError>
575
- ? SuspenseQueryHookOptions<TFnData, TError, TFnData, TVariables>
625
+ ? WithRequiredVariables<
626
+ SuspenseQueryHookOptions<TFnData, TError, TFnData, TVariables>,
627
+ TVariables
628
+ >
576
629
  : T extends InfiniteQueryHook<
577
630
  infer TFnData,
578
631
  infer TVariables,
579
632
  infer TError,
580
633
  infer TPageParam
581
634
  >
582
- ? InfiniteQueryHookOptions<
583
- TFnData,
584
- TError,
585
- CompatibleWithV4<InfiniteData<TFnData, TPageParam>, TFnData>,
586
- TVariables,
587
- TPageParam
635
+ ? WithRequiredVariables<
636
+ InfiniteQueryHookOptions<
637
+ TFnData,
638
+ TError,
639
+ CompatibleWithV4<InfiniteData<TFnData, TPageParam>, TFnData>,
640
+ TVariables,
641
+ TPageParam
642
+ >,
643
+ TVariables
588
644
  >
589
645
  : T extends SuspenseInfiniteQueryHook<
590
646
  infer TFnData,
@@ -592,12 +648,15 @@ export type inferOptions<T> = T extends QueryHook<
592
648
  infer TError,
593
649
  infer TPageParam
594
650
  >
595
- ? SuspenseInfiniteQueryHookOptions<
596
- TFnData,
597
- TError,
598
- CompatibleWithV4<InfiniteData<TFnData, TPageParam>, TFnData>,
599
- TVariables,
600
- TPageParam
651
+ ? WithRequiredVariables<
652
+ SuspenseInfiniteQueryHookOptions<
653
+ TFnData,
654
+ TError,
655
+ CompatibleWithV4<InfiniteData<TFnData, TPageParam>, TFnData>,
656
+ TVariables,
657
+ TPageParam
658
+ >,
659
+ TVariables
601
660
  >
602
661
  : T extends MutationHook<infer TFnData, infer TVariables, infer TError>
603
662
  ? MutationHookOptions<TFnData, TError, TVariables, unknown>