@trpc/react-query 10.43.0 → 11.0.0-next.92
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/bundle-analysis.json +208 -0
- package/dist/createHooksInternal-3d8ebfcf.mjs +481 -0
- package/dist/createHooksInternal-dce6e141.js +435 -0
- package/dist/createHooksInternal-e2034194.js +490 -0
- package/dist/createTRPCReact.d.ts +46 -45
- package/dist/createTRPCReact.d.ts.map +1 -1
- package/dist/index.d.ts +0 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +8 -21
- package/dist/index.mjs +9 -21
- package/dist/internals/context.d.ts +42 -54
- package/dist/internals/context.d.ts.map +1 -1
- package/dist/internals/getClientArgs.d.ts +2 -1
- package/dist/internals/getClientArgs.d.ts.map +1 -1
- package/dist/internals/getQueryKey.d.ts +28 -13
- package/dist/internals/getQueryKey.d.ts.map +1 -1
- package/dist/internals/useQueries.d.ts +4 -4
- package/dist/internals/useQueries.d.ts.map +1 -1
- package/dist/server/index.d.ts +0 -6
- package/dist/server/index.d.ts.map +1 -1
- package/dist/server/index.js +52 -69
- package/dist/server/index.mjs +52 -69
- package/dist/server/ssgProxy.d.ts +23 -13
- package/dist/server/ssgProxy.d.ts.map +1 -1
- package/dist/shared/hooks/createHooksInternal.d.ts +35 -12
- package/dist/shared/hooks/createHooksInternal.d.ts.map +1 -1
- package/dist/shared/hooks/createRootHooks.d.ts +0 -5
- package/dist/shared/hooks/createRootHooks.d.ts.map +1 -1
- package/dist/shared/hooks/types.d.ts +33 -18
- package/dist/shared/hooks/types.d.ts.map +1 -1
- package/dist/shared/index.js +9 -9
- package/dist/shared/index.mjs +3 -3
- package/dist/shared/polymorphism/mutationLike.d.ts +5 -5
- package/dist/shared/polymorphism/mutationLike.d.ts.map +1 -1
- package/dist/shared/polymorphism/queryLike.d.ts +5 -5
- package/dist/shared/polymorphism/queryLike.d.ts.map +1 -1
- package/dist/shared/polymorphism/routerLike.d.ts +4 -3
- package/dist/shared/polymorphism/routerLike.d.ts.map +1 -1
- package/dist/shared/proxy/decorationProxy.d.ts +2 -2
- package/dist/shared/proxy/decorationProxy.d.ts.map +1 -1
- package/dist/shared/proxy/useQueriesProxy.d.ts +6 -6
- package/dist/shared/proxy/useQueriesProxy.d.ts.map +1 -1
- package/dist/shared/proxy/utilsProxy.d.ts +34 -31
- package/dist/shared/proxy/utilsProxy.d.ts.map +1 -1
- package/dist/shared/types.d.ts +0 -12
- package/dist/shared/types.d.ts.map +1 -1
- package/dist/utils/inferReactQueryProcedure.d.ts +7 -7
- package/dist/utils/inferReactQueryProcedure.d.ts.map +1 -1
- package/dist/utilsProxy-00894da2.mjs +121 -0
- package/dist/utilsProxy-27bd93c1.js +128 -0
- package/dist/utilsProxy-7e396600.js +114 -0
- package/package.json +8 -14
- package/src/createTRPCReact.tsx +156 -135
- package/src/index.ts +0 -1
- package/src/internals/context.tsx +93 -164
- package/src/internals/getClientArgs.ts +9 -4
- package/src/internals/getQueryKey.ts +53 -24
- package/src/internals/useQueries.ts +7 -10
- package/src/server/index.ts +0 -8
- package/src/server/ssgProxy.ts +179 -34
- package/src/shared/hooks/createHooksInternal.tsx +298 -207
- package/src/shared/hooks/createRootHooks.tsx +0 -6
- package/src/shared/hooks/types.ts +87 -36
- package/src/shared/polymorphism/mutationLike.ts +11 -8
- package/src/shared/polymorphism/queryLike.ts +17 -12
- package/src/shared/polymorphism/routerLike.ts +15 -9
- package/src/shared/proxy/decorationProxy.ts +9 -31
- package/src/shared/proxy/useQueriesProxy.ts +23 -37
- package/src/shared/proxy/utilsProxy.ts +121 -95
- package/src/shared/types.ts +0 -12
- package/src/utils/inferReactQueryProcedure.ts +28 -30
- package/dist/context-4557b3d3.js +0 -38
- package/dist/createHooksInternal-2e69c447.js +0 -582
- package/dist/createHooksInternal-37b068e0.js +0 -510
- package/dist/createHooksInternal-bdff7171.mjs +0 -568
- package/dist/getArrayQueryKey-4bdb5cc2.js +0 -36
- package/dist/getArrayQueryKey-86134f8b.mjs +0 -34
- package/dist/internals/getArrayQueryKey.d.ts +0 -25
- package/dist/internals/getArrayQueryKey.d.ts.map +0 -1
- package/dist/interop.d.ts +0 -11
- package/dist/interop.d.ts.map +0 -1
- package/dist/server/types.d.ts +0 -14
- package/dist/server/types.d.ts.map +0 -1
- package/dist/shared/hooks/deprecated/createHooksInternal.d.ts +0 -63
- package/dist/shared/hooks/deprecated/createHooksInternal.d.ts.map +0 -1
- package/dist/ssg/index.d.ts +0 -15
- package/dist/ssg/index.d.ts.map +0 -1
- package/dist/ssg/ssg.d.ts +0 -16
- package/dist/ssg/ssg.d.ts.map +0 -1
- package/src/internals/getArrayQueryKey.ts +0 -46
- package/src/interop.ts +0 -29
- package/src/server/types.ts +0 -29
- package/src/shared/hooks/deprecated/createHooksInternal.tsx +0 -641
- package/src/ssg/index.tsx +0 -16
- package/src/ssg/ssg.ts +0 -139
- package/ssg/index.d.ts +0 -1
- package/ssg/index.js +0 -1
|
@@ -1,16 +1,16 @@
|
|
|
1
|
-
/* eslint-disable @typescript-eslint/no-non-null-assertion */
|
|
2
1
|
import {
|
|
3
2
|
useInfiniteQuery as __useInfiniteQuery,
|
|
4
3
|
useMutation as __useMutation,
|
|
5
4
|
useQueries as __useQueries,
|
|
6
5
|
useQuery as __useQuery,
|
|
6
|
+
useSuspenseInfiniteQuery as __useSuspenseInfiniteQuery,
|
|
7
|
+
useSuspenseQuery as __useSuspenseQuery,
|
|
7
8
|
DehydratedState,
|
|
8
|
-
|
|
9
|
+
hashKey,
|
|
9
10
|
useQueryClient,
|
|
10
11
|
} from '@tanstack/react-query';
|
|
11
|
-
import {
|
|
12
|
+
import { createTRPCUntypedClient, TRPCClientErrorLike } from '@trpc/client';
|
|
12
13
|
import type { AnyRouter } from '@trpc/server';
|
|
13
|
-
import { Observable } from '@trpc/server/observable';
|
|
14
14
|
import React, {
|
|
15
15
|
useCallback,
|
|
16
16
|
useEffect,
|
|
@@ -23,11 +23,11 @@ import {
|
|
|
23
23
|
TRPCContext,
|
|
24
24
|
TRPCContextState,
|
|
25
25
|
} from '../../internals/context';
|
|
26
|
-
import { getArrayQueryKey, QueryType } from '../../internals/getArrayQueryKey';
|
|
27
26
|
import { getClientArgs } from '../../internals/getClientArgs';
|
|
27
|
+
import { getQueryKeyInternal, TRPCQueryKey } from '../../internals/getQueryKey';
|
|
28
28
|
import { useHookResult } from '../../internals/useHookResult';
|
|
29
29
|
import { TRPCUseQueries } from '../../internals/useQueries';
|
|
30
|
-
import {
|
|
30
|
+
import { createUseQueries } from '../proxy/useQueriesProxy';
|
|
31
31
|
import { CreateTRPCReactOptions, UseMutationOverride } from '../types';
|
|
32
32
|
import {
|
|
33
33
|
CreateClient,
|
|
@@ -41,6 +41,10 @@ import {
|
|
|
41
41
|
UseTRPCQueryOptions,
|
|
42
42
|
UseTRPCQueryResult,
|
|
43
43
|
UseTRPCSubscriptionOptions,
|
|
44
|
+
UseTRPCSuspenseInfiniteQueryOptions,
|
|
45
|
+
UseTRPCSuspenseInfiniteQueryResult,
|
|
46
|
+
UseTRPCSuspenseQueryOptions,
|
|
47
|
+
UseTRPCSuspenseQueryResult,
|
|
44
48
|
} from './types';
|
|
45
49
|
|
|
46
50
|
/**
|
|
@@ -51,7 +55,7 @@ export function createRootHooks<
|
|
|
51
55
|
TSSRContext = unknown,
|
|
52
56
|
>(config?: CreateTRPCReactOptions<TRouter>) {
|
|
53
57
|
const mutationSuccessOverride: UseMutationOverride['onSuccess'] =
|
|
54
|
-
|
|
58
|
+
config?.overrides?.useMutation?.onSuccess ??
|
|
55
59
|
((options) => options.originalFn());
|
|
56
60
|
|
|
57
61
|
type TError = TRPCClientErrorLike<TRouter>;
|
|
@@ -60,10 +64,9 @@ export function createRootHooks<
|
|
|
60
64
|
|
|
61
65
|
const Context = (config?.context ??
|
|
62
66
|
TRPCContext) as React.Context<ProviderContext>;
|
|
63
|
-
const ReactQueryContext = config?.reactQueryContext;
|
|
64
67
|
|
|
65
68
|
const createClient: CreateClient<TRouter> = (opts) => {
|
|
66
|
-
return
|
|
69
|
+
return createTRPCUntypedClient(opts);
|
|
67
70
|
};
|
|
68
71
|
|
|
69
72
|
const TRPCProvider: TRPCProvider<TRouter, TSSRContext> = (props) => {
|
|
@@ -83,66 +86,61 @@ export function createRootHooks<
|
|
|
83
86
|
ssrContext: ssrContext ?? null,
|
|
84
87
|
ssrState,
|
|
85
88
|
fetchQuery: useCallback(
|
|
86
|
-
(
|
|
89
|
+
(queryKey, opts) => {
|
|
87
90
|
return queryClient.fetchQuery({
|
|
88
91
|
...opts,
|
|
89
|
-
queryKey
|
|
90
|
-
queryFn: () =>
|
|
91
|
-
(client as any).query(...getClientArgs(pathAndInput, opts)),
|
|
92
|
+
queryKey,
|
|
93
|
+
queryFn: () => client.query(...getClientArgs(queryKey, opts)),
|
|
92
94
|
});
|
|
93
95
|
},
|
|
94
96
|
[client, queryClient],
|
|
95
97
|
),
|
|
96
98
|
fetchInfiniteQuery: useCallback(
|
|
97
|
-
(
|
|
99
|
+
(queryKey, opts) => {
|
|
98
100
|
return queryClient.fetchInfiniteQuery({
|
|
99
101
|
...opts,
|
|
100
|
-
queryKey
|
|
102
|
+
queryKey,
|
|
101
103
|
queryFn: ({ pageParam }) => {
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
return (client as any).query(
|
|
105
|
-
...getClientArgs([path, actualInput], opts),
|
|
104
|
+
return client.query(
|
|
105
|
+
...getClientArgs(queryKey, opts, pageParam),
|
|
106
106
|
);
|
|
107
107
|
},
|
|
108
|
+
initialPageParam: opts?.initialCursor ?? null,
|
|
108
109
|
});
|
|
109
110
|
},
|
|
110
111
|
[client, queryClient],
|
|
111
112
|
),
|
|
112
113
|
prefetchQuery: useCallback(
|
|
113
|
-
(
|
|
114
|
+
(queryKey, opts) => {
|
|
114
115
|
return queryClient.prefetchQuery({
|
|
115
116
|
...opts,
|
|
116
|
-
queryKey
|
|
117
|
-
queryFn: () =>
|
|
118
|
-
(client as any).query(...getClientArgs(pathAndInput, opts)),
|
|
117
|
+
queryKey,
|
|
118
|
+
queryFn: () => client.query(...getClientArgs(queryKey, opts)),
|
|
119
119
|
});
|
|
120
120
|
},
|
|
121
121
|
[client, queryClient],
|
|
122
122
|
),
|
|
123
123
|
prefetchInfiniteQuery: useCallback(
|
|
124
|
-
(
|
|
124
|
+
(queryKey, opts) => {
|
|
125
125
|
return queryClient.prefetchInfiniteQuery({
|
|
126
126
|
...opts,
|
|
127
|
-
queryKey
|
|
127
|
+
queryKey,
|
|
128
128
|
queryFn: ({ pageParam }) => {
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
return (client as any).query(
|
|
132
|
-
...getClientArgs([path, actualInput], opts),
|
|
129
|
+
return client.query(
|
|
130
|
+
...getClientArgs(queryKey, opts, pageParam),
|
|
133
131
|
);
|
|
134
132
|
},
|
|
133
|
+
initialPageParam: opts?.initialCursor ?? null,
|
|
135
134
|
});
|
|
136
135
|
},
|
|
137
136
|
[client, queryClient],
|
|
138
137
|
),
|
|
139
138
|
ensureQueryData: useCallback(
|
|
140
|
-
(
|
|
139
|
+
(queryKey, opts) => {
|
|
141
140
|
return queryClient.ensureQueryData({
|
|
142
141
|
...opts,
|
|
143
|
-
queryKey
|
|
144
|
-
queryFn: () =>
|
|
145
|
-
(client as any).query(...getClientArgs(pathAndInput, opts)),
|
|
142
|
+
queryKey,
|
|
143
|
+
queryFn: () => client.query(...getClientArgs(queryKey, opts)),
|
|
146
144
|
});
|
|
147
145
|
},
|
|
148
146
|
[client, queryClient],
|
|
@@ -152,7 +150,7 @@ export function createRootHooks<
|
|
|
152
150
|
return queryClient.invalidateQueries(
|
|
153
151
|
{
|
|
154
152
|
...filters,
|
|
155
|
-
queryKey
|
|
153
|
+
queryKey,
|
|
156
154
|
},
|
|
157
155
|
options,
|
|
158
156
|
);
|
|
@@ -160,13 +158,11 @@ export function createRootHooks<
|
|
|
160
158
|
[queryClient],
|
|
161
159
|
),
|
|
162
160
|
resetQueries: useCallback(
|
|
163
|
-
(
|
|
164
|
-
const [queryKey, filters, options] = args;
|
|
165
|
-
|
|
161
|
+
(queryKey, filters, options) => {
|
|
166
162
|
return queryClient.resetQueries(
|
|
167
163
|
{
|
|
168
164
|
...filters,
|
|
169
|
-
queryKey
|
|
165
|
+
queryKey,
|
|
170
166
|
},
|
|
171
167
|
options,
|
|
172
168
|
);
|
|
@@ -174,13 +170,11 @@ export function createRootHooks<
|
|
|
174
170
|
[queryClient],
|
|
175
171
|
),
|
|
176
172
|
refetchQueries: useCallback(
|
|
177
|
-
(
|
|
178
|
-
const [queryKey, filters, options] = args;
|
|
179
|
-
|
|
173
|
+
(queryKey, filters, options) => {
|
|
180
174
|
return queryClient.refetchQueries(
|
|
181
175
|
{
|
|
182
176
|
...filters,
|
|
183
|
-
queryKey
|
|
177
|
+
queryKey,
|
|
184
178
|
},
|
|
185
179
|
options,
|
|
186
180
|
);
|
|
@@ -188,53 +182,45 @@ export function createRootHooks<
|
|
|
188
182
|
[queryClient],
|
|
189
183
|
),
|
|
190
184
|
cancelQuery: useCallback(
|
|
191
|
-
(
|
|
192
|
-
return queryClient.cancelQueries(
|
|
193
|
-
|
|
194
|
-
|
|
185
|
+
(queryKey, options) => {
|
|
186
|
+
return queryClient.cancelQueries(
|
|
187
|
+
{
|
|
188
|
+
queryKey,
|
|
189
|
+
},
|
|
190
|
+
options,
|
|
191
|
+
);
|
|
195
192
|
},
|
|
196
193
|
[queryClient],
|
|
197
194
|
),
|
|
198
195
|
setQueryData: useCallback(
|
|
199
|
-
(
|
|
200
|
-
const [queryKey, ...rest] = args;
|
|
196
|
+
(queryKey, updater, options) => {
|
|
201
197
|
return queryClient.setQueryData(
|
|
202
|
-
|
|
203
|
-
|
|
198
|
+
queryKey,
|
|
199
|
+
updater as any,
|
|
200
|
+
options,
|
|
204
201
|
);
|
|
205
202
|
},
|
|
206
203
|
[queryClient],
|
|
207
204
|
),
|
|
208
205
|
getQueryData: useCallback(
|
|
209
|
-
(
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
return queryClient.getQueryData(
|
|
213
|
-
getArrayQueryKey(queryKey, 'query'),
|
|
214
|
-
...rest,
|
|
215
|
-
);
|
|
206
|
+
(queryKey) => {
|
|
207
|
+
return queryClient.getQueryData(queryKey);
|
|
216
208
|
},
|
|
217
209
|
[queryClient],
|
|
218
210
|
),
|
|
219
211
|
setInfiniteQueryData: useCallback(
|
|
220
|
-
(
|
|
221
|
-
const [queryKey, ...rest] = args;
|
|
222
|
-
|
|
212
|
+
(queryKey, updater, options) => {
|
|
223
213
|
return queryClient.setQueryData(
|
|
224
|
-
|
|
225
|
-
|
|
214
|
+
queryKey,
|
|
215
|
+
updater as any,
|
|
216
|
+
options,
|
|
226
217
|
);
|
|
227
218
|
},
|
|
228
219
|
[queryClient],
|
|
229
220
|
),
|
|
230
221
|
getInfiniteQueryData: useCallback(
|
|
231
|
-
(
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
return queryClient.getQueryData(
|
|
235
|
-
getArrayQueryKey(queryKey, 'infinite'),
|
|
236
|
-
...rest,
|
|
237
|
-
);
|
|
222
|
+
(queryKey) => {
|
|
223
|
+
return queryClient.getQueryData(queryKey);
|
|
238
224
|
},
|
|
239
225
|
[queryClient],
|
|
240
226
|
),
|
|
@@ -246,7 +232,14 @@ export function createRootHooks<
|
|
|
246
232
|
};
|
|
247
233
|
|
|
248
234
|
function useContext() {
|
|
249
|
-
|
|
235
|
+
const context = React.useContext(Context);
|
|
236
|
+
|
|
237
|
+
if (!context) {
|
|
238
|
+
throw new Error(
|
|
239
|
+
'Unable to find tRPC Context. Did you forget to wrap your App inside `withTRPC` HoC?',
|
|
240
|
+
);
|
|
241
|
+
}
|
|
242
|
+
return context;
|
|
250
243
|
}
|
|
251
244
|
|
|
252
245
|
/**
|
|
@@ -255,16 +248,11 @@ export function createRootHooks<
|
|
|
255
248
|
*/
|
|
256
249
|
function useSSRQueryOptionsIfNeeded<
|
|
257
250
|
TOptions extends { retryOnMount?: boolean } | undefined,
|
|
258
|
-
>(
|
|
259
|
-
pathAndInput: unknown[],
|
|
260
|
-
type: Exclude<QueryType, 'any'>,
|
|
261
|
-
opts: TOptions,
|
|
262
|
-
): TOptions {
|
|
251
|
+
>(queryKey: TRPCQueryKey, opts: TOptions): TOptions {
|
|
263
252
|
const { queryClient, ssrState } = useContext();
|
|
264
253
|
return ssrState &&
|
|
265
254
|
ssrState !== 'mounted' &&
|
|
266
|
-
queryClient.getQueryCache().find(
|
|
267
|
-
?.state.status === 'error'
|
|
255
|
+
queryClient.getQueryCache().find({ queryKey })?.state.status === 'error'
|
|
268
256
|
? {
|
|
269
257
|
retryOnMount: false,
|
|
270
258
|
...opts,
|
|
@@ -273,33 +261,27 @@ export function createRootHooks<
|
|
|
273
261
|
}
|
|
274
262
|
|
|
275
263
|
function useQuery(
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
opts?: UseTRPCQueryOptions<unknown, unknown,
|
|
264
|
+
path: string[],
|
|
265
|
+
input: unknown,
|
|
266
|
+
opts?: UseTRPCQueryOptions<unknown, unknown, TError>,
|
|
279
267
|
): UseTRPCQueryResult<unknown, TError> {
|
|
280
268
|
const context = useContext();
|
|
281
|
-
if (!context) {
|
|
282
|
-
throw new Error(
|
|
283
|
-
'Unable to retrieve application context. Did you forget to wrap your App inside `withTRPC` HoC?',
|
|
284
|
-
);
|
|
285
|
-
}
|
|
286
269
|
const { abortOnUnmount, client, ssrState, queryClient, prefetchQuery } =
|
|
287
270
|
context;
|
|
271
|
+
const queryKey = getQueryKeyInternal(path, input, 'query');
|
|
288
272
|
|
|
289
|
-
const defaultOpts = queryClient.getQueryDefaults(
|
|
290
|
-
getArrayQueryKey(pathAndInput, 'query'),
|
|
291
|
-
);
|
|
273
|
+
const defaultOpts = queryClient.getQueryDefaults(queryKey);
|
|
292
274
|
|
|
293
275
|
if (
|
|
294
276
|
typeof window === 'undefined' &&
|
|
295
277
|
ssrState === 'prepass' &&
|
|
296
278
|
opts?.trpc?.ssr !== false &&
|
|
297
279
|
(opts?.enabled ?? defaultOpts?.enabled) !== false &&
|
|
298
|
-
!queryClient.getQueryCache().find(
|
|
280
|
+
!queryClient.getQueryCache().find({ queryKey })
|
|
299
281
|
) {
|
|
300
|
-
void prefetchQuery(
|
|
282
|
+
void prefetchQuery(queryKey, opts as any);
|
|
301
283
|
}
|
|
302
|
-
const ssrOpts = useSSRQueryOptionsIfNeeded(
|
|
284
|
+
const ssrOpts = useSSRQueryOptionsIfNeeded(queryKey, {
|
|
303
285
|
...defaultOpts,
|
|
304
286
|
...opts,
|
|
305
287
|
});
|
|
@@ -307,70 +289,106 @@ export function createRootHooks<
|
|
|
307
289
|
const shouldAbortOnUnmount =
|
|
308
290
|
opts?.trpc?.abortOnUnmount ?? config?.abortOnUnmount ?? abortOnUnmount;
|
|
309
291
|
|
|
310
|
-
const hook = __useQuery(
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
292
|
+
const hook = __useQuery(
|
|
293
|
+
{
|
|
294
|
+
...ssrOpts,
|
|
295
|
+
queryKey: queryKey as any,
|
|
296
|
+
queryFn: (queryFunctionContext) => {
|
|
297
|
+
const actualOpts = {
|
|
298
|
+
...ssrOpts,
|
|
299
|
+
trpc: {
|
|
300
|
+
...ssrOpts?.trpc,
|
|
301
|
+
...(shouldAbortOnUnmount
|
|
302
|
+
? { signal: queryFunctionContext.signal }
|
|
303
|
+
: {}),
|
|
304
|
+
},
|
|
305
|
+
};
|
|
323
306
|
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
);
|
|
307
|
+
return client.query(...getClientArgs(queryKey, actualOpts));
|
|
308
|
+
},
|
|
327
309
|
},
|
|
328
|
-
|
|
329
|
-
|
|
310
|
+
queryClient,
|
|
311
|
+
) as UseTRPCQueryResult<unknown, TError>;
|
|
330
312
|
|
|
331
313
|
hook.trpc = useHookResult({
|
|
332
|
-
path:
|
|
314
|
+
path: path.join('.'),
|
|
333
315
|
});
|
|
334
316
|
|
|
335
317
|
return hook;
|
|
336
318
|
}
|
|
337
319
|
|
|
320
|
+
function useSuspenseQuery(
|
|
321
|
+
path: string[],
|
|
322
|
+
input: unknown,
|
|
323
|
+
opts?: UseTRPCSuspenseQueryOptions<unknown, unknown, TError>,
|
|
324
|
+
): UseTRPCSuspenseQueryResult<unknown, TError> {
|
|
325
|
+
const context = useContext();
|
|
326
|
+
const queryKey = getQueryKeyInternal(path, input, 'query');
|
|
327
|
+
|
|
328
|
+
const shouldAbortOnUnmount =
|
|
329
|
+
opts?.trpc?.abortOnUnmount ??
|
|
330
|
+
config?.abortOnUnmount ??
|
|
331
|
+
context.abortOnUnmount;
|
|
332
|
+
|
|
333
|
+
const hook = __useSuspenseQuery(
|
|
334
|
+
{
|
|
335
|
+
...opts,
|
|
336
|
+
queryKey: queryKey as any,
|
|
337
|
+
queryFn: (queryFunctionContext) => {
|
|
338
|
+
const actualOpts = {
|
|
339
|
+
trpc: {
|
|
340
|
+
...(shouldAbortOnUnmount
|
|
341
|
+
? { signal: queryFunctionContext.signal }
|
|
342
|
+
: {}),
|
|
343
|
+
},
|
|
344
|
+
};
|
|
345
|
+
|
|
346
|
+
return context.client.query(...getClientArgs(queryKey, actualOpts));
|
|
347
|
+
},
|
|
348
|
+
},
|
|
349
|
+
context.queryClient,
|
|
350
|
+
) as UseTRPCQueryResult<unknown, TError>;
|
|
351
|
+
|
|
352
|
+
hook.trpc = useHookResult({
|
|
353
|
+
path: path.join('.'),
|
|
354
|
+
});
|
|
355
|
+
|
|
356
|
+
return [hook.data, hook as any];
|
|
357
|
+
}
|
|
358
|
+
|
|
338
359
|
function useMutation(
|
|
339
|
-
|
|
340
|
-
path: string | [string],
|
|
360
|
+
path: string[],
|
|
341
361
|
opts?: UseTRPCMutationOptions<unknown, TError, unknown, unknown>,
|
|
342
362
|
): UseTRPCMutationResult<unknown, TError, unknown, unknown> {
|
|
343
363
|
const { client } = useContext();
|
|
344
|
-
const queryClient = useQueryClient(
|
|
345
|
-
const actualPath = Array.isArray(path) ? path[0] : path;
|
|
346
|
-
|
|
347
|
-
const defaultOpts = queryClient.getMutationDefaults([
|
|
348
|
-
actualPath.split('.'),
|
|
349
|
-
]);
|
|
364
|
+
const queryClient = useQueryClient();
|
|
350
365
|
|
|
351
|
-
const
|
|
352
|
-
|
|
353
|
-
mutationKey: [actualPath.split('.')],
|
|
354
|
-
mutationFn: (input) => {
|
|
355
|
-
return (client.mutation as any)(
|
|
356
|
-
...getClientArgs([actualPath, input], opts),
|
|
357
|
-
);
|
|
358
|
-
},
|
|
359
|
-
context: ReactQueryContext,
|
|
360
|
-
onSuccess(...args) {
|
|
361
|
-
const originalFn = () =>
|
|
362
|
-
opts?.onSuccess?.(...args) ?? defaultOpts?.onSuccess?.(...args);
|
|
366
|
+
const mutationKey = [path];
|
|
367
|
+
const defaultOpts = queryClient.getMutationDefaults(mutationKey);
|
|
363
368
|
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
+
const hook = __useMutation(
|
|
370
|
+
{
|
|
371
|
+
...opts,
|
|
372
|
+
mutationKey: mutationKey,
|
|
373
|
+
mutationFn: (input) => {
|
|
374
|
+
return client.mutation(...getClientArgs([path, { input }], opts));
|
|
375
|
+
},
|
|
376
|
+
onSuccess(...args) {
|
|
377
|
+
const originalFn = () =>
|
|
378
|
+
opts?.onSuccess?.(...args) ?? defaultOpts?.onSuccess?.(...args);
|
|
379
|
+
|
|
380
|
+
return mutationSuccessOverride({
|
|
381
|
+
originalFn,
|
|
382
|
+
queryClient,
|
|
383
|
+
meta: opts?.meta ?? defaultOpts?.meta ?? {},
|
|
384
|
+
});
|
|
385
|
+
},
|
|
369
386
|
},
|
|
370
|
-
|
|
387
|
+
queryClient,
|
|
388
|
+
) as UseTRPCMutationResult<unknown, TError, unknown, unknown>;
|
|
371
389
|
|
|
372
390
|
hook.trpc = useHookResult({
|
|
373
|
-
path:
|
|
391
|
+
path: path.join('.'),
|
|
374
392
|
});
|
|
375
393
|
|
|
376
394
|
return hook;
|
|
@@ -378,15 +396,12 @@ export function createRootHooks<
|
|
|
378
396
|
|
|
379
397
|
/* istanbul ignore next -- @preserve */
|
|
380
398
|
function useSubscription(
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
...args: unknown[],
|
|
385
|
-
],
|
|
386
|
-
opts: UseTRPCSubscriptionOptions<Observable<unknown, unknown>, TError>,
|
|
399
|
+
path: string[],
|
|
400
|
+
input: unknown,
|
|
401
|
+
opts: UseTRPCSubscriptionOptions<unknown, TError>,
|
|
387
402
|
) {
|
|
388
403
|
const enabled = opts?.enabled ?? true;
|
|
389
|
-
const queryKey =
|
|
404
|
+
const queryKey = hashKey(getQueryKeyInternal(path, input, 'any'));
|
|
390
405
|
const { client } = useContext();
|
|
391
406
|
|
|
392
407
|
const optsRef = useRef<typeof opts>(opts);
|
|
@@ -396,11 +411,10 @@ export function createRootHooks<
|
|
|
396
411
|
if (!enabled) {
|
|
397
412
|
return;
|
|
398
413
|
}
|
|
399
|
-
const [path, input] = pathAndInput;
|
|
400
414
|
let isStopped = false;
|
|
401
415
|
const subscription = client.subscription(
|
|
402
|
-
path,
|
|
403
|
-
|
|
416
|
+
path.join('.'),
|
|
417
|
+
input ?? undefined,
|
|
404
418
|
{
|
|
405
419
|
onStarted: () => {
|
|
406
420
|
if (!isStopped) {
|
|
@@ -409,8 +423,7 @@ export function createRootHooks<
|
|
|
409
423
|
},
|
|
410
424
|
onData: (data) => {
|
|
411
425
|
if (!isStopped) {
|
|
412
|
-
|
|
413
|
-
optsRef.current.onData(data as any);
|
|
426
|
+
opts.onData(data);
|
|
414
427
|
}
|
|
415
428
|
},
|
|
416
429
|
onError: (err) => {
|
|
@@ -429,14 +442,10 @@ export function createRootHooks<
|
|
|
429
442
|
}
|
|
430
443
|
|
|
431
444
|
function useInfiniteQuery(
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
],
|
|
437
|
-
opts?: UseTRPCInfiniteQueryOptions<unknown, unknown, unknown, TError>,
|
|
438
|
-
): UseTRPCInfiniteQueryResult<unknown, TError> {
|
|
439
|
-
const [path, input] = pathAndInput;
|
|
445
|
+
path: string[],
|
|
446
|
+
input: unknown,
|
|
447
|
+
opts: UseTRPCInfiniteQueryOptions<unknown, unknown, TError>,
|
|
448
|
+
): UseTRPCInfiniteQueryResult<unknown, TError, unknown> {
|
|
440
449
|
const {
|
|
441
450
|
client,
|
|
442
451
|
ssrState,
|
|
@@ -444,27 +453,21 @@ export function createRootHooks<
|
|
|
444
453
|
queryClient,
|
|
445
454
|
abortOnUnmount,
|
|
446
455
|
} = useContext();
|
|
456
|
+
const queryKey = getQueryKeyInternal(path, input, 'infinite');
|
|
447
457
|
|
|
448
|
-
const defaultOpts = queryClient.getQueryDefaults(
|
|
449
|
-
getArrayQueryKey(pathAndInput, 'infinite'),
|
|
450
|
-
);
|
|
458
|
+
const defaultOpts = queryClient.getQueryDefaults(queryKey);
|
|
451
459
|
|
|
452
460
|
if (
|
|
453
461
|
typeof window === 'undefined' &&
|
|
454
462
|
ssrState === 'prepass' &&
|
|
455
463
|
opts?.trpc?.ssr !== false &&
|
|
456
464
|
(opts?.enabled ?? defaultOpts?.enabled) !== false &&
|
|
457
|
-
!queryClient
|
|
458
|
-
.getQueryCache()
|
|
459
|
-
.find(getArrayQueryKey(pathAndInput, 'infinite'))
|
|
465
|
+
!queryClient.getQueryCache().find({ queryKey })
|
|
460
466
|
) {
|
|
461
|
-
void prefetchInfiniteQuery(
|
|
462
|
-
pathAndInput as any,
|
|
463
|
-
{ ...defaultOpts, ...opts } as any,
|
|
464
|
-
);
|
|
467
|
+
void prefetchInfiniteQuery(queryKey, { ...defaultOpts, ...opts } as any);
|
|
465
468
|
}
|
|
466
469
|
|
|
467
|
-
const ssrOpts = useSSRQueryOptionsIfNeeded(
|
|
470
|
+
const ssrOpts = useSSRQueryOptionsIfNeeded(queryKey, {
|
|
468
471
|
...defaultOpts,
|
|
469
472
|
...opts,
|
|
470
473
|
});
|
|
@@ -472,67 +475,124 @@ export function createRootHooks<
|
|
|
472
475
|
// request option should take priority over global
|
|
473
476
|
const shouldAbortOnUnmount = opts?.trpc?.abortOnUnmount ?? abortOnUnmount;
|
|
474
477
|
|
|
475
|
-
const hook = __useInfiniteQuery(
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
...
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
478
|
+
const hook = __useInfiniteQuery(
|
|
479
|
+
{
|
|
480
|
+
...ssrOpts,
|
|
481
|
+
initialPageParam: opts.initialCursor ?? null,
|
|
482
|
+
persister: opts.persister,
|
|
483
|
+
queryKey: queryKey as any,
|
|
484
|
+
queryFn: (queryFunctionContext) => {
|
|
485
|
+
const actualOpts = {
|
|
486
|
+
...ssrOpts,
|
|
487
|
+
trpc: {
|
|
488
|
+
...ssrOpts?.trpc,
|
|
489
|
+
...(shouldAbortOnUnmount
|
|
490
|
+
? { signal: queryFunctionContext.signal }
|
|
491
|
+
: {}),
|
|
492
|
+
},
|
|
493
|
+
};
|
|
494
|
+
|
|
495
|
+
return client.query(
|
|
496
|
+
...getClientArgs(
|
|
497
|
+
queryKey,
|
|
498
|
+
actualOpts,
|
|
499
|
+
queryFunctionContext.pageParam ?? opts.initialCursor,
|
|
500
|
+
),
|
|
501
|
+
);
|
|
502
|
+
},
|
|
503
|
+
},
|
|
504
|
+
queryClient,
|
|
505
|
+
) as UseTRPCInfiniteQueryResult<unknown, TError, unknown>;
|
|
488
506
|
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
507
|
+
hook.trpc = useHookResult({
|
|
508
|
+
path: path.join('.'),
|
|
509
|
+
});
|
|
510
|
+
return hook;
|
|
511
|
+
}
|
|
512
|
+
|
|
513
|
+
function useSuspenseInfiniteQuery(
|
|
514
|
+
path: string[],
|
|
515
|
+
input: unknown,
|
|
516
|
+
opts: UseTRPCSuspenseInfiniteQueryOptions<unknown, unknown, TError>,
|
|
517
|
+
): UseTRPCSuspenseInfiniteQueryResult<unknown, TError, unknown> {
|
|
518
|
+
const context = useContext();
|
|
519
|
+
const queryKey = getQueryKeyInternal(path, input, 'infinite');
|
|
520
|
+
|
|
521
|
+
const defaultOpts = context.queryClient.getQueryDefaults(queryKey);
|
|
522
|
+
|
|
523
|
+
const ssrOpts = useSSRQueryOptionsIfNeeded(queryKey, {
|
|
524
|
+
...defaultOpts,
|
|
525
|
+
...opts,
|
|
526
|
+
});
|
|
493
527
|
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
528
|
+
// request option should take priority over global
|
|
529
|
+
const shouldAbortOnUnmount =
|
|
530
|
+
opts?.trpc?.abortOnUnmount ?? context.abortOnUnmount;
|
|
531
|
+
|
|
532
|
+
const hook = __useSuspenseInfiniteQuery(
|
|
533
|
+
{
|
|
534
|
+
...opts,
|
|
535
|
+
initialPageParam: opts.initialCursor ?? null,
|
|
536
|
+
queryKey,
|
|
537
|
+
queryFn: (queryFunctionContext) => {
|
|
538
|
+
const actualOpts = {
|
|
539
|
+
...ssrOpts,
|
|
540
|
+
trpc: {
|
|
541
|
+
...ssrOpts?.trpc,
|
|
542
|
+
...(shouldAbortOnUnmount
|
|
543
|
+
? { signal: queryFunctionContext.signal }
|
|
544
|
+
: {}),
|
|
545
|
+
},
|
|
546
|
+
};
|
|
547
|
+
|
|
548
|
+
return context.client.query(
|
|
549
|
+
...getClientArgs(
|
|
550
|
+
queryKey,
|
|
551
|
+
actualOpts,
|
|
552
|
+
queryFunctionContext.pageParam ?? opts.initialCursor,
|
|
553
|
+
),
|
|
554
|
+
);
|
|
555
|
+
},
|
|
498
556
|
},
|
|
499
|
-
context
|
|
500
|
-
|
|
557
|
+
context.queryClient,
|
|
558
|
+
) as UseTRPCInfiniteQueryResult<unknown, TError, unknown>;
|
|
501
559
|
|
|
502
560
|
hook.trpc = useHookResult({
|
|
503
|
-
path,
|
|
561
|
+
path: path.join('.'),
|
|
504
562
|
});
|
|
505
|
-
|
|
563
|
+
|
|
564
|
+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
565
|
+
return [hook.data!, hook as any];
|
|
506
566
|
}
|
|
507
567
|
|
|
508
|
-
const useQueries: TRPCUseQueries<TRouter> = (queriesCallback
|
|
568
|
+
const useQueries: TRPCUseQueries<TRouter> = (queriesCallback) => {
|
|
509
569
|
const { ssrState, queryClient, prefetchQuery, client } = useContext();
|
|
510
570
|
|
|
511
|
-
const proxy =
|
|
571
|
+
const proxy = createUseQueries(client);
|
|
512
572
|
|
|
513
573
|
const queries = queriesCallback(proxy);
|
|
514
574
|
|
|
515
575
|
if (typeof window === 'undefined' && ssrState === 'prepass') {
|
|
516
576
|
for (const query of queries) {
|
|
517
|
-
const queryOption = query as TRPCQueryOptions<any, any
|
|
577
|
+
const queryOption = query as TRPCQueryOptions<any, any>;
|
|
518
578
|
if (
|
|
519
579
|
queryOption.trpc?.ssr !== false &&
|
|
520
|
-
!queryClient
|
|
521
|
-
.getQueryCache()
|
|
522
|
-
.find(getArrayQueryKey(queryOption.queryKey!, 'query'))
|
|
580
|
+
!queryClient.getQueryCache().find({ queryKey: queryOption.queryKey })
|
|
523
581
|
) {
|
|
524
|
-
void prefetchQuery(queryOption.queryKey
|
|
582
|
+
void prefetchQuery(queryOption.queryKey, queryOption as any);
|
|
525
583
|
}
|
|
526
584
|
}
|
|
527
585
|
}
|
|
528
586
|
|
|
529
|
-
return __useQueries(
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
587
|
+
return __useQueries(
|
|
588
|
+
{
|
|
589
|
+
queries: queries.map((query) => ({
|
|
590
|
+
...query,
|
|
591
|
+
queryKey: (query as TRPCQueryOptions<any, any>).queryKey,
|
|
592
|
+
})),
|
|
593
|
+
},
|
|
594
|
+
queryClient,
|
|
595
|
+
);
|
|
536
596
|
};
|
|
537
597
|
|
|
538
598
|
const useDehydratedState: UseDehydratedState<TRouter> = (
|
|
@@ -555,10 +615,41 @@ export function createRootHooks<
|
|
|
555
615
|
useContext,
|
|
556
616
|
useUtils: useContext,
|
|
557
617
|
useQuery,
|
|
618
|
+
useSuspenseQuery,
|
|
558
619
|
useQueries,
|
|
559
620
|
useMutation,
|
|
560
621
|
useSubscription,
|
|
561
622
|
useDehydratedState,
|
|
562
623
|
useInfiniteQuery,
|
|
624
|
+
useSuspenseInfiniteQuery,
|
|
563
625
|
};
|
|
564
626
|
}
|
|
627
|
+
/* istanbul ignore next */
|
|
628
|
+
/**
|
|
629
|
+
* Hack to infer the type of `createReactQueryHooks`
|
|
630
|
+
* @link https://stackoverflow.com/a/59072991
|
|
631
|
+
*/
|
|
632
|
+
class GnClass<TRouter extends AnyRouter, TSSRContext = unknown> {
|
|
633
|
+
fn() {
|
|
634
|
+
return createRootHooks<TRouter, TSSRContext>();
|
|
635
|
+
}
|
|
636
|
+
}
|
|
637
|
+
|
|
638
|
+
type returnTypeInferer<TType> = TType extends (
|
|
639
|
+
a: Record<string, string>,
|
|
640
|
+
) => infer U
|
|
641
|
+
? U
|
|
642
|
+
: never;
|
|
643
|
+
type fooType<TRouter extends AnyRouter, TSSRContext = unknown> = GnClass<
|
|
644
|
+
TRouter,
|
|
645
|
+
TSSRContext
|
|
646
|
+
>['fn'];
|
|
647
|
+
|
|
648
|
+
/**
|
|
649
|
+
* Infer the type of a `createReactQueryHooks` function
|
|
650
|
+
* @internal
|
|
651
|
+
*/
|
|
652
|
+
export type CreateReactQueryHooks<
|
|
653
|
+
TRouter extends AnyRouter,
|
|
654
|
+
TSSRContext = unknown,
|
|
655
|
+
> = returnTypeInferer<fooType<TRouter, TSSRContext>>;
|