@trpc/react-query 10.6.0 → 10.8.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/dist/{createHooksInternal-10b1b7ef.mjs → createHooksInternal-9c1f8ad9.mjs} +25 -11
- package/dist/{createHooksInternal-860b8183.js → createHooksInternal-d8e5577b.js} +26 -9
- package/dist/createTRPCReact.d.ts +30 -9
- package/dist/createTRPCReact.d.ts.map +1 -1
- package/dist/{getArrayQueryKey-671d083c.js → getArrayQueryKey-4bdb5cc2.js} +6 -1
- package/dist/{getArrayQueryKey-30a7f2f6.mjs → getArrayQueryKey-86134f8b.mjs} +6 -1
- package/dist/index.js +2 -2
- package/dist/index.mjs +2 -2
- package/dist/internals/context.d.ts +35 -23
- package/dist/internals/context.d.ts.map +1 -1
- package/dist/internals/getArrayQueryKey.d.ts +9 -5
- package/dist/internals/getArrayQueryKey.d.ts.map +1 -1
- package/dist/internals/getClientArgs.d.ts +2 -0
- package/dist/internals/getClientArgs.d.ts.map +1 -0
- package/dist/internals/getQueryKey.d.ts.map +1 -1
- package/dist/internals/useHookResult.d.ts +10 -0
- package/dist/internals/useHookResult.d.ts.map +1 -0
- package/dist/shared/hooks/createHooksInternal.d.ts +19 -116
- package/dist/shared/hooks/createHooksInternal.d.ts.map +1 -1
- package/dist/shared/hooks/deprecated/createHooksInternal.d.ts +61 -0
- package/dist/shared/hooks/deprecated/createHooksInternal.d.ts.map +1 -0
- package/dist/shared/hooks/types.d.ts +78 -0
- package/dist/shared/hooks/types.d.ts.map +1 -0
- package/dist/shared/index.d.ts +12 -0
- package/dist/shared/index.d.ts.map +1 -1
- package/dist/shared/index.js +5 -2
- package/dist/shared/index.mjs +2 -2
- package/dist/shared/proxy/decorationProxy.d.ts.map +1 -1
- package/dist/shared/proxy/utilsProxy.d.ts +6 -5
- package/dist/shared/proxy/utilsProxy.d.ts.map +1 -1
- package/dist/ssg/index.js +1 -1
- package/dist/ssg/index.mjs +1 -1
- package/dist/ssg/ssgProxy.d.ts +3 -3
- package/dist/ssg/ssgProxy.d.ts.map +1 -1
- package/dist/utils/inferReactQueryProcedure.d.ts +1 -1
- package/dist/utils/inferReactQueryProcedure.d.ts.map +1 -1
- package/package.json +8 -6
- package/src/createTRPCReact.tsx +79 -24
- package/src/internals/context.tsx +53 -52
- package/src/internals/getArrayQueryKey.test.ts +11 -0
- package/src/internals/getArrayQueryKey.ts +11 -2
- package/src/internals/getClientArgs.ts +7 -0
- package/src/internals/getQueryKey.ts +2 -1
- package/src/internals/useHookResult.ts +18 -0
- package/src/shared/hooks/createHooksInternal.tsx +71 -309
- package/src/shared/hooks/deprecated/createHooksInternal.tsx +628 -0
- package/src/shared/hooks/types.ts +151 -0
- package/src/shared/index.ts +17 -0
- package/src/shared/proxy/decorationProxy.ts +7 -0
- package/src/shared/proxy/utilsProxy.ts +7 -3
- package/src/ssg/ssgProxy.ts +8 -4
- package/src/utils/inferReactQueryProcedure.ts +1 -1
package/src/createTRPCReact.tsx
CHANGED
|
@@ -7,6 +7,7 @@ import {
|
|
|
7
7
|
AnyRouter,
|
|
8
8
|
AnySubscriptionProcedure,
|
|
9
9
|
ProcedureRouterRecord,
|
|
10
|
+
ProtectedIntersection,
|
|
10
11
|
inferProcedureInput,
|
|
11
12
|
} from '@trpc/server';
|
|
12
13
|
import {
|
|
@@ -15,6 +16,7 @@ import {
|
|
|
15
16
|
inferTransformedSubscriptionOutput,
|
|
16
17
|
} from '@trpc/server/shared';
|
|
17
18
|
import { useMemo } from 'react';
|
|
19
|
+
import { QueryKey, QueryType } from './internals/getArrayQueryKey';
|
|
18
20
|
import { TRPCUseQueries } from './internals/useQueries';
|
|
19
21
|
import {
|
|
20
22
|
CreateReactUtilsProxy,
|
|
@@ -22,8 +24,13 @@ import {
|
|
|
22
24
|
createReactQueryUtilsProxy,
|
|
23
25
|
} from './shared';
|
|
24
26
|
import {
|
|
25
|
-
CreateClient,
|
|
26
27
|
CreateReactQueryHooks,
|
|
28
|
+
createHooksInternal,
|
|
29
|
+
} from './shared/hooks/createHooksInternal';
|
|
30
|
+
import {
|
|
31
|
+
CreateClient,
|
|
32
|
+
DefinedUseTRPCQueryOptions,
|
|
33
|
+
DefinedUseTRPCQueryResult,
|
|
27
34
|
TRPCProvider,
|
|
28
35
|
UseDehydratedState,
|
|
29
36
|
UseTRPCInfiniteQueryOptions,
|
|
@@ -35,10 +42,45 @@ import {
|
|
|
35
42
|
UseTRPCQueryResult,
|
|
36
43
|
UseTRPCQuerySuccessResult,
|
|
37
44
|
UseTRPCSubscriptionOptions,
|
|
38
|
-
|
|
39
|
-
} from './shared/hooks/createHooksInternal';
|
|
45
|
+
} from './shared/hooks/types';
|
|
40
46
|
import { CreateTRPCReactOptions } from './shared/types';
|
|
41
47
|
|
|
48
|
+
/**
|
|
49
|
+
* @internal
|
|
50
|
+
*/
|
|
51
|
+
export interface ProcedureUseQuery<
|
|
52
|
+
TProcedure extends AnyProcedure,
|
|
53
|
+
TPath extends string,
|
|
54
|
+
> {
|
|
55
|
+
<
|
|
56
|
+
TQueryFnData = inferTransformedProcedureOutput<TProcedure>,
|
|
57
|
+
TData = inferTransformedProcedureOutput<TProcedure>,
|
|
58
|
+
>(
|
|
59
|
+
input: inferProcedureInput<TProcedure>,
|
|
60
|
+
opts: DefinedUseTRPCQueryOptions<
|
|
61
|
+
TPath,
|
|
62
|
+
inferProcedureInput<TProcedure>,
|
|
63
|
+
TQueryFnData,
|
|
64
|
+
TData,
|
|
65
|
+
TRPCClientErrorLike<TProcedure>
|
|
66
|
+
>,
|
|
67
|
+
): DefinedUseTRPCQueryResult<TData, TRPCClientErrorLike<TProcedure>>;
|
|
68
|
+
|
|
69
|
+
<
|
|
70
|
+
TQueryFnData = inferTransformedProcedureOutput<TProcedure>,
|
|
71
|
+
TData = inferTransformedProcedureOutput<TProcedure>,
|
|
72
|
+
>(
|
|
73
|
+
input: inferProcedureInput<TProcedure>,
|
|
74
|
+
opts?: UseTRPCQueryOptions<
|
|
75
|
+
TPath,
|
|
76
|
+
inferProcedureInput<TProcedure>,
|
|
77
|
+
TQueryFnData,
|
|
78
|
+
TData,
|
|
79
|
+
TRPCClientErrorLike<TProcedure>
|
|
80
|
+
>,
|
|
81
|
+
): UseTRPCQueryResult<TData, TRPCClientErrorLike<TProcedure>>;
|
|
82
|
+
}
|
|
83
|
+
|
|
42
84
|
/**
|
|
43
85
|
* @internal
|
|
44
86
|
*/
|
|
@@ -48,19 +90,16 @@ export type DecorateProcedure<
|
|
|
48
90
|
TPath extends string,
|
|
49
91
|
> = TProcedure extends AnyQueryProcedure
|
|
50
92
|
? {
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
93
|
+
/**
|
|
94
|
+
* Method to extract the query key for a procedure
|
|
95
|
+
* @param type - defaults to `any`
|
|
96
|
+
* @link https://trpc.io/docs/useContext#-the-function-i-want-isnt-here
|
|
97
|
+
*/
|
|
98
|
+
getQueryKey: (
|
|
55
99
|
input: inferProcedureInput<TProcedure>,
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
TQueryFnData,
|
|
60
|
-
TData,
|
|
61
|
-
TRPCClientErrorLike<TProcedure>
|
|
62
|
-
>,
|
|
63
|
-
) => UseTRPCQueryResult<TData, TRPCClientErrorLike<TProcedure>>;
|
|
100
|
+
type?: QueryType,
|
|
101
|
+
) => QueryKey;
|
|
102
|
+
useQuery: ProcedureUseQuery<TProcedure, TPath>;
|
|
64
103
|
} & (inferProcedureInput<TProcedure> extends { cursor?: any }
|
|
65
104
|
? {
|
|
66
105
|
useInfiniteQuery: <
|
|
@@ -102,8 +141,8 @@ export type DecorateProcedure<
|
|
|
102
141
|
>,
|
|
103
142
|
];
|
|
104
143
|
}
|
|
105
|
-
:
|
|
106
|
-
:
|
|
144
|
+
: object)
|
|
145
|
+
: object) &
|
|
107
146
|
(TFlags extends 'ExperimentalSuspense'
|
|
108
147
|
? {
|
|
109
148
|
useSuspenseQuery: <
|
|
@@ -126,7 +165,7 @@ export type DecorateProcedure<
|
|
|
126
165
|
UseTRPCQuerySuccessResult<TData, TRPCClientErrorLike<TProcedure>>,
|
|
127
166
|
];
|
|
128
167
|
}
|
|
129
|
-
:
|
|
168
|
+
: object)
|
|
130
169
|
: TProcedure extends AnyMutationProcedure
|
|
131
170
|
? {
|
|
132
171
|
useMutation: <TContext = unknown>(
|
|
@@ -164,7 +203,9 @@ export type DecoratedProcedureRecord<
|
|
|
164
203
|
TPath extends string = '',
|
|
165
204
|
> = {
|
|
166
205
|
[TKey in keyof TProcedures]: TProcedures[TKey] extends AnyRouter
|
|
167
|
-
?
|
|
206
|
+
? {
|
|
207
|
+
getQueryKey: () => QueryKey;
|
|
208
|
+
} & DecoratedProcedureRecord<
|
|
168
209
|
TProcedures[TKey]['_def']['record'],
|
|
169
210
|
TFlags,
|
|
170
211
|
`${TPath}${TKey & string}.`
|
|
@@ -174,13 +215,25 @@ export type DecoratedProcedureRecord<
|
|
|
174
215
|
: never;
|
|
175
216
|
};
|
|
176
217
|
|
|
177
|
-
|
|
218
|
+
/**
|
|
219
|
+
* @internal
|
|
220
|
+
*/
|
|
221
|
+
export type CreateTRPCReactBase<TRouter extends AnyRouter, TSSRContext> = {
|
|
178
222
|
useContext(): CreateReactUtilsProxy<TRouter, TSSRContext>;
|
|
179
223
|
Provider: TRPCProvider<TRouter, TSSRContext>;
|
|
180
224
|
createClient: CreateClient<TRouter>;
|
|
181
225
|
useQueries: TRPCUseQueries<TRouter>;
|
|
182
226
|
useDehydratedState: UseDehydratedState<TRouter>;
|
|
183
|
-
}
|
|
227
|
+
};
|
|
228
|
+
|
|
229
|
+
export type CreateTRPCReact<
|
|
230
|
+
TRouter extends AnyRouter,
|
|
231
|
+
TSSRContext,
|
|
232
|
+
TFlags,
|
|
233
|
+
> = ProtectedIntersection<
|
|
234
|
+
CreateTRPCReactBase<TRouter, TSSRContext>,
|
|
235
|
+
DecoratedProcedureRecord<TRouter['_def']['record'], TFlags>
|
|
236
|
+
>;
|
|
184
237
|
|
|
185
238
|
/**
|
|
186
239
|
* @internal
|
|
@@ -207,7 +260,7 @@ export function createHooksInternalProxy<
|
|
|
207
260
|
return (trpc as any)[key];
|
|
208
261
|
}
|
|
209
262
|
|
|
210
|
-
return createReactProxyDecoration(key
|
|
263
|
+
return createReactProxyDecoration(key, trpc);
|
|
211
264
|
});
|
|
212
265
|
}
|
|
213
266
|
|
|
@@ -215,9 +268,11 @@ export function createTRPCReact<
|
|
|
215
268
|
TRouter extends AnyRouter,
|
|
216
269
|
TSSRContext = unknown,
|
|
217
270
|
TFlags = null,
|
|
218
|
-
>(
|
|
271
|
+
>(
|
|
272
|
+
opts?: CreateTRPCReactOptions<TRouter>,
|
|
273
|
+
): CreateTRPCReact<TRouter, TSSRContext, TFlags> {
|
|
219
274
|
const hooks = createHooksInternal<TRouter, TSSRContext>(opts);
|
|
220
275
|
const proxy = createHooksInternalProxy<TRouter, TSSRContext, TFlags>(hooks);
|
|
221
276
|
|
|
222
|
-
return proxy;
|
|
277
|
+
return proxy as any;
|
|
223
278
|
}
|
|
@@ -1,11 +1,13 @@
|
|
|
1
1
|
import {
|
|
2
|
-
CancelOptions,
|
|
3
2
|
FetchInfiniteQueryOptions,
|
|
4
3
|
FetchQueryOptions,
|
|
4
|
+
QueryClient,
|
|
5
|
+
} from '@tanstack/react-query';
|
|
6
|
+
import {
|
|
7
|
+
CancelOptions,
|
|
5
8
|
InfiniteData,
|
|
6
9
|
InvalidateOptions,
|
|
7
10
|
InvalidateQueryFilters,
|
|
8
|
-
QueryClient,
|
|
9
11
|
RefetchOptions,
|
|
10
12
|
RefetchQueryFilters,
|
|
11
13
|
ResetOptions,
|
|
@@ -15,16 +17,13 @@ import {
|
|
|
15
17
|
} from '@tanstack/react-query';
|
|
16
18
|
import {
|
|
17
19
|
TRPCClient,
|
|
18
|
-
TRPCClientError,
|
|
19
20
|
TRPCRequestOptions,
|
|
20
21
|
inferRouterProxyClient,
|
|
21
22
|
} from '@trpc/client';
|
|
22
|
-
import
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
} from '@trpc/server';
|
|
27
|
-
import type { inferTransformedProcedureOutput } from '@trpc/server/shared';
|
|
23
|
+
import { TRPCClientError } from '@trpc/client';
|
|
24
|
+
import type { AnyRouter } from '@trpc/server';
|
|
25
|
+
import { inferHandlerInput, inferProcedureInput } from '@trpc/server';
|
|
26
|
+
import { inferTransformedProcedureOutput } from '@trpc/server/shared';
|
|
28
27
|
import { createContext } from 'react';
|
|
29
28
|
|
|
30
29
|
export interface TRPCFetchQueryOptions<TInput, TError, TOutput>
|
|
@@ -64,6 +63,9 @@ export interface ProxyTRPCContextProps<TRouter extends AnyRouter, TSSRContext> {
|
|
|
64
63
|
abortOnUnmount?: boolean;
|
|
65
64
|
}
|
|
66
65
|
|
|
66
|
+
/**
|
|
67
|
+
* @internal
|
|
68
|
+
*/
|
|
67
69
|
export type DecoratedProxyTRPCContextProps<
|
|
68
70
|
TRouter extends AnyRouter,
|
|
69
71
|
TSSRContext,
|
|
@@ -87,6 +89,27 @@ export const contextProps: (keyof ProxyTRPCContextProps<any, any>)[] = [
|
|
|
87
89
|
];
|
|
88
90
|
|
|
89
91
|
/** @internal */
|
|
92
|
+
type TRPCContextResetQueries<TRouter extends AnyRouter> =
|
|
93
|
+
/**
|
|
94
|
+
* @link https://react-query.tanstack.com/reference/QueryClient#queryclientresetqueries
|
|
95
|
+
*/
|
|
96
|
+
(<
|
|
97
|
+
TPath extends keyof TRouter['_def']['queries'] & string,
|
|
98
|
+
TInput extends inferProcedureInput<TRouter['_def']['queries'][TPath]>,
|
|
99
|
+
>(
|
|
100
|
+
pathAndInput?: [TPath, TInput?] | TPath,
|
|
101
|
+
filters?: ResetQueryFilters,
|
|
102
|
+
options?: ResetOptions,
|
|
103
|
+
) => Promise<void>) &
|
|
104
|
+
/**
|
|
105
|
+
* @link https://react-query.tanstack.com/reference/QueryClient#queryclientresetqueries
|
|
106
|
+
*/
|
|
107
|
+
((filters?: ResetQueryFilters, options?: ResetOptions) => Promise<void>);
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* @deprecated
|
|
111
|
+
* @internal
|
|
112
|
+
**/
|
|
90
113
|
export interface TRPCContextState<
|
|
91
114
|
TRouter extends AnyRouter,
|
|
92
115
|
TSSRContext = undefined,
|
|
@@ -94,7 +117,7 @@ export interface TRPCContextState<
|
|
|
94
117
|
/**
|
|
95
118
|
* @link https://tanstack.com/query/v4/docs/reference/QueryClient#queryclientfetchquery
|
|
96
119
|
*/
|
|
97
|
-
fetchQuery<
|
|
120
|
+
fetchQuery: <
|
|
98
121
|
TPath extends keyof TRouter['_def']['queries'] & string,
|
|
99
122
|
TProcedure extends TRouter['_def']['queries'][TPath],
|
|
100
123
|
TOutput extends inferTransformedProcedureOutput<TProcedure>,
|
|
@@ -102,11 +125,11 @@ export interface TRPCContextState<
|
|
|
102
125
|
>(
|
|
103
126
|
pathAndInput: [path: TPath, ...args: inferHandlerInput<TProcedure>],
|
|
104
127
|
opts?: TRPCFetchQueryOptions<TInput, TRPCClientError<TRouter>, TOutput>,
|
|
105
|
-
)
|
|
128
|
+
) => Promise<TOutput>;
|
|
106
129
|
/**
|
|
107
130
|
* @link https://tanstack.com/query/v4/docs/reference/QueryClient#queryclientfetchinfinitequery
|
|
108
131
|
*/
|
|
109
|
-
fetchInfiniteQuery<
|
|
132
|
+
fetchInfiniteQuery: <
|
|
110
133
|
TPath extends keyof TRouter['_def']['queries'] & string,
|
|
111
134
|
TProcedure extends TRouter['_def']['queries'][TPath],
|
|
112
135
|
TOutput extends inferTransformedProcedureOutput<TProcedure>,
|
|
@@ -118,11 +141,11 @@ export interface TRPCContextState<
|
|
|
118
141
|
TRPCClientError<TRouter>,
|
|
119
142
|
TOutput
|
|
120
143
|
>,
|
|
121
|
-
)
|
|
144
|
+
) => Promise<InfiniteData<TOutput>>;
|
|
122
145
|
/**
|
|
123
146
|
* @link https://react-query.tanstack.com/guides/prefetching
|
|
124
147
|
*/
|
|
125
|
-
prefetchQuery<
|
|
148
|
+
prefetchQuery: <
|
|
126
149
|
TPath extends keyof TRouter['_def']['queries'] & string,
|
|
127
150
|
TProcedure extends TRouter['_def']['queries'][TPath],
|
|
128
151
|
TOutput extends inferTransformedProcedureOutput<TProcedure>,
|
|
@@ -130,12 +153,12 @@ export interface TRPCContextState<
|
|
|
130
153
|
>(
|
|
131
154
|
pathAndInput: [path: TPath, ...args: inferHandlerInput<TProcedure>],
|
|
132
155
|
opts?: TRPCFetchQueryOptions<TInput, TRPCClientError<TRouter>, TOutput>,
|
|
133
|
-
)
|
|
156
|
+
) => Promise<void>;
|
|
134
157
|
|
|
135
158
|
/**
|
|
136
159
|
* @link https://tanstack.com/query/v4/docs/reference/QueryClient#queryclientprefetchinfinitequery
|
|
137
160
|
*/
|
|
138
|
-
prefetchInfiniteQuery<
|
|
161
|
+
prefetchInfiniteQuery: <
|
|
139
162
|
TPath extends keyof TRouter['_def']['queries'] & string,
|
|
140
163
|
TProcedure extends TRouter['_def']['queries'][TPath],
|
|
141
164
|
TOutput extends inferTransformedProcedureOutput<TProcedure>,
|
|
@@ -147,46 +170,24 @@ export interface TRPCContextState<
|
|
|
147
170
|
TRPCClientError<TRouter>,
|
|
148
171
|
TOutput
|
|
149
172
|
>,
|
|
150
|
-
)
|
|
173
|
+
) => Promise<void>;
|
|
151
174
|
|
|
152
175
|
/**
|
|
153
176
|
* @link https://react-query.tanstack.com/guides/query-invalidation
|
|
154
177
|
*/
|
|
155
|
-
invalidateQueries<
|
|
178
|
+
invalidateQueries: <
|
|
156
179
|
TPath extends keyof TRouter['_def']['queries'] & string,
|
|
157
180
|
TInput extends inferProcedureInput<TRouter['_def']['queries'][TPath]>,
|
|
158
181
|
>(
|
|
159
182
|
pathAndInput?: [TPath, TInput?] | TPath,
|
|
160
183
|
filters?: InvalidateQueryFilters,
|
|
161
184
|
options?: InvalidateOptions,
|
|
162
|
-
)
|
|
163
|
-
/**
|
|
164
|
-
* @link https://react-query.tanstack.com/guides/query-invalidation
|
|
165
|
-
*/
|
|
166
|
-
invalidateQueries(
|
|
167
|
-
filters?: InvalidateQueryFilters,
|
|
168
|
-
options?: InvalidateOptions,
|
|
169
|
-
): Promise<void>;
|
|
170
|
-
|
|
171
|
-
/**
|
|
172
|
-
* @link https://react-query.tanstack.com/reference/QueryClient#queryclientresetqueries
|
|
173
|
-
*/
|
|
174
|
-
resetQueries<
|
|
175
|
-
TPath extends keyof TRouter['_def']['queries'] & string,
|
|
176
|
-
TInput extends inferProcedureInput<TRouter['_def']['queries'][TPath]>,
|
|
177
|
-
>(
|
|
178
|
-
pathAndInput?: [TPath, TInput?] | TPath,
|
|
179
|
-
filters?: ResetQueryFilters,
|
|
180
|
-
options?: ResetOptions,
|
|
181
|
-
): Promise<void>;
|
|
185
|
+
) => Promise<void>;
|
|
182
186
|
|
|
183
187
|
/**
|
|
184
188
|
* @link https://react-query.tanstack.com/reference/QueryClient#queryclientresetqueries
|
|
185
189
|
*/
|
|
186
|
-
resetQueries
|
|
187
|
-
filters?: ResetQueryFilters,
|
|
188
|
-
options?: ResetOptions,
|
|
189
|
-
): Promise<void>;
|
|
190
|
+
resetQueries: TRPCContextResetQueries<TRouter>;
|
|
190
191
|
|
|
191
192
|
/**
|
|
192
193
|
* @link https://react-query.tanstack.com/reference/QueryClient#queryclientrefetchqueries
|
|
@@ -210,17 +211,17 @@ export interface TRPCContextState<
|
|
|
210
211
|
/**
|
|
211
212
|
* @link https://react-query.tanstack.com/guides/query-cancellation
|
|
212
213
|
*/
|
|
213
|
-
cancelQuery<
|
|
214
|
+
cancelQuery: <
|
|
214
215
|
TPath extends keyof TRouter['_def']['queries'] & string,
|
|
215
216
|
TInput extends inferProcedureInput<TRouter['_def']['queries'][TPath]>,
|
|
216
217
|
>(
|
|
217
218
|
pathAndInput: [TPath, TInput?],
|
|
218
219
|
options?: CancelOptions,
|
|
219
|
-
)
|
|
220
|
+
) => Promise<void>;
|
|
220
221
|
/**
|
|
221
222
|
* @link https://react-query.tanstack.com/reference/QueryClient#queryclientsetquerydata
|
|
222
223
|
*/
|
|
223
|
-
setQueryData<
|
|
224
|
+
setQueryData: <
|
|
224
225
|
TPath extends keyof TRouter['_def']['queries'] & string,
|
|
225
226
|
TInput extends inferProcedureInput<TRouter['_def']['queries'][TPath]>,
|
|
226
227
|
TOutput extends inferTransformedProcedureOutput<
|
|
@@ -230,11 +231,11 @@ export interface TRPCContextState<
|
|
|
230
231
|
pathAndInput: [TPath, TInput?],
|
|
231
232
|
updater: Updater<TOutput | undefined, TOutput | undefined>,
|
|
232
233
|
options?: SetDataOptions,
|
|
233
|
-
)
|
|
234
|
+
) => void;
|
|
234
235
|
/**
|
|
235
236
|
* @link https://react-query.tanstack.com/reference/QueryClient#queryclientgetquerydata
|
|
236
237
|
*/
|
|
237
|
-
getQueryData<
|
|
238
|
+
getQueryData: <
|
|
238
239
|
TPath extends keyof TRouter['_def']['queries'] & string,
|
|
239
240
|
TInput extends inferProcedureInput<TRouter['_def']['queries'][TPath]>,
|
|
240
241
|
TOutput extends inferTransformedProcedureOutput<
|
|
@@ -242,11 +243,11 @@ export interface TRPCContextState<
|
|
|
242
243
|
>,
|
|
243
244
|
>(
|
|
244
245
|
pathAndInput: [TPath, TInput?],
|
|
245
|
-
)
|
|
246
|
+
) => TOutput | undefined;
|
|
246
247
|
/**
|
|
247
248
|
* @link https://react-query.tanstack.com/reference/QueryClient#queryclientsetquerydata
|
|
248
249
|
*/
|
|
249
|
-
setInfiniteQueryData<
|
|
250
|
+
setInfiniteQueryData: <
|
|
250
251
|
TPath extends keyof TRouter['_def']['queries'] & string,
|
|
251
252
|
TInput extends inferProcedureInput<TRouter['_def']['queries'][TPath]>,
|
|
252
253
|
TOutput extends inferTransformedProcedureOutput<
|
|
@@ -259,11 +260,11 @@ export interface TRPCContextState<
|
|
|
259
260
|
InfiniteData<TOutput> | undefined
|
|
260
261
|
>,
|
|
261
262
|
options?: SetDataOptions,
|
|
262
|
-
)
|
|
263
|
+
) => void;
|
|
263
264
|
/**
|
|
264
265
|
* @link https://react-query.tanstack.com/reference/QueryClient#queryclientgetquerydata
|
|
265
266
|
*/
|
|
266
|
-
getInfiniteQueryData<
|
|
267
|
+
getInfiniteQueryData: <
|
|
267
268
|
TPath extends keyof TRouter['_def']['queries'] & string,
|
|
268
269
|
TInput extends inferProcedureInput<TRouter['_def']['queries'][TPath]>,
|
|
269
270
|
TOutput extends inferTransformedProcedureOutput<
|
|
@@ -271,7 +272,7 @@ export interface TRPCContextState<
|
|
|
271
272
|
>,
|
|
272
273
|
>(
|
|
273
274
|
pathAndInput: [TPath, TInput?],
|
|
274
|
-
)
|
|
275
|
+
) => InfiniteData<TOutput> | undefined;
|
|
275
276
|
}
|
|
276
277
|
|
|
277
278
|
export const TRPCContext = createContext(null as any);
|
|
@@ -1,6 +1,17 @@
|
|
|
1
1
|
import { getArrayQueryKey } from './getArrayQueryKey';
|
|
2
2
|
|
|
3
3
|
test('getArrayQueryKey', () => {
|
|
4
|
+
// empty path should not nest an extra array
|
|
5
|
+
expect(getArrayQueryKey('', 'any')).toMatchInlineSnapshot(`Array []`);
|
|
6
|
+
|
|
7
|
+
// should not nest an empty object
|
|
8
|
+
expect(getArrayQueryKey('foo', 'any')).toMatchInlineSnapshot(`
|
|
9
|
+
Array [
|
|
10
|
+
Array [
|
|
11
|
+
"foo",
|
|
12
|
+
],
|
|
13
|
+
]
|
|
14
|
+
`);
|
|
4
15
|
expect(getArrayQueryKey('foo', 'query')).toMatchInlineSnapshot(`
|
|
5
16
|
Array [
|
|
6
17
|
Array [
|
|
@@ -1,16 +1,21 @@
|
|
|
1
1
|
export type QueryType = 'query' | 'infinite' | 'any';
|
|
2
2
|
|
|
3
|
+
export type QueryKey = [
|
|
4
|
+
string[],
|
|
5
|
+
{ input?: unknown; type?: Exclude<QueryType, 'any'> }?,
|
|
6
|
+
];
|
|
7
|
+
|
|
3
8
|
/**
|
|
4
9
|
* To allow easy interactions with groups of related queries, such as
|
|
5
10
|
* invalidating all queries of a router, we use an array as the path when
|
|
6
11
|
* storing in tanstack query. This function converts from the `.` separated
|
|
7
12
|
* path passed around internally by both the legacy and proxy implementation.
|
|
8
13
|
* https://github.com/trpc/trpc/issues/2611
|
|
9
|
-
|
|
14
|
+
**/
|
|
10
15
|
export function getArrayQueryKey(
|
|
11
16
|
queryKey: string | [string] | [string, ...unknown[]] | unknown[],
|
|
12
17
|
type: QueryType,
|
|
13
|
-
):
|
|
18
|
+
): QueryKey {
|
|
14
19
|
const queryKeyArrayed = Array.isArray(queryKey) ? queryKey : [queryKey];
|
|
15
20
|
const [path, input] = queryKeyArrayed;
|
|
16
21
|
|
|
@@ -20,6 +25,10 @@ export function getArrayQueryKey(
|
|
|
20
25
|
// Construct a query key that is easy to destructure and flexible for
|
|
21
26
|
// partial selecting etc.
|
|
22
27
|
// https://github.com/trpc/trpc/issues/3128
|
|
28
|
+
if (!input && (!type || type === 'any'))
|
|
29
|
+
// for `utils.invalidate()` to match all queries (including vanilla react-query)
|
|
30
|
+
// we don't want nested array if path is empty, i.e. `[]` instead of `[[]]`
|
|
31
|
+
return arrayPath.length ? [arrayPath] : ([] as unknown as QueryKey);
|
|
23
32
|
return [
|
|
24
33
|
arrayPath,
|
|
25
34
|
{
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { useRef } from 'react';
|
|
2
|
+
|
|
3
|
+
export interface TRPCHookResult {
|
|
4
|
+
trpc: {
|
|
5
|
+
path: string;
|
|
6
|
+
};
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Makes a stable reference of the `trpc` prop
|
|
11
|
+
*/
|
|
12
|
+
export function useHookResult(
|
|
13
|
+
value: TRPCHookResult['trpc'],
|
|
14
|
+
): TRPCHookResult['trpc'] {
|
|
15
|
+
const ref = useRef(value);
|
|
16
|
+
ref.current.path = value.path;
|
|
17
|
+
return ref.current;
|
|
18
|
+
}
|