@trpc/react-query 11.0.0-next-beta.206 → 11.0.0-next-beta.216
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 +28 -28
- package/dist/createTRPCQueryUtils.js +16 -0
- package/dist/createTRPCQueryUtils.mjs +14 -0
- package/dist/createTRPCReact.js +58 -0
- package/dist/createTRPCReact.mjs +36 -0
- package/dist/index.js +10 -64
- package/dist/index.mjs +3 -40
- package/dist/internals/context.js +33 -0
- package/dist/internals/context.mjs +11 -0
- package/dist/internals/getClientArgs.js +16 -0
- package/dist/internals/getClientArgs.mjs +14 -0
- package/dist/internals/getQueryKey.js +57 -0
- package/dist/internals/getQueryKey.mjs +54 -0
- package/dist/internals/useHookResult.js +32 -0
- package/dist/internals/useHookResult.mjs +11 -0
- package/dist/server/index.js +2 -102
- package/dist/server/index.mjs +1 -103
- package/dist/server/ssgProxy.js +107 -0
- package/dist/server/ssgProxy.mjs +105 -0
- package/dist/{createHooksInternal-f77072af.js → shared/hooks/createHooksInternal.js} +28 -183
- package/dist/{createHooksInternal-0df35117.mjs → shared/hooks/createHooksInternal.mjs} +8 -158
- package/dist/shared/index.js +13 -15
- package/dist/shared/index.mjs +7 -7
- package/dist/shared/proxy/decorationProxy.js +31 -0
- package/dist/shared/proxy/decorationProxy.mjs +29 -0
- package/dist/shared/proxy/useQueriesProxy.js +25 -0
- package/dist/shared/proxy/useQueriesProxy.mjs +23 -0
- package/dist/shared/proxy/utilsProxy.js +89 -0
- package/dist/{utilsProxy-12979321.mjs → shared/proxy/utilsProxy.mjs} +3 -63
- package/dist/{queryClient-4d766c0c.mjs → shared/queryClient.mjs} +1 -1
- package/dist/utils/createUtilityFunctions.js +94 -0
- package/dist/utils/createUtilityFunctions.mjs +92 -0
- package/package.json +6 -6
- package/dist/createHooksInternal-3074c6c4.js +0 -470
- package/dist/queryClient-1c8d7d8a.js +0 -8
- package/dist/utilsProxy-13814792.js +0 -142
- package/dist/utilsProxy-4f6da312.js +0 -173
- /package/dist/{queryClient-358a9a75.js → shared/queryClient.js} +0 -0
|
@@ -1,470 +0,0 @@
|
|
|
1
|
-
import { createRecursiveProxy } from '@trpc/core';
|
|
2
|
-
import { useQuery, useSuspenseQuery, useQueryClient, useMutation, hashKey, useInfiniteQuery, useSuspenseInfiniteQuery, useQueries, useSuspenseQueries } from '@tanstack/react-query';
|
|
3
|
-
import { TRPCUntypedClient, getUntypedClient, createTRPCUntypedClient } from '@trpc/client';
|
|
4
|
-
import * as React from 'react';
|
|
5
|
-
import { b as getQueryKeyInternal, T as TRPCContext } from './utilsProxy-13814792.js';
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* Create proxy for decorating procedures
|
|
9
|
-
* @internal
|
|
10
|
-
*/
|
|
11
|
-
function createReactDecoration(name, hooks) {
|
|
12
|
-
return createRecursiveProxy(({ path, args }) => {
|
|
13
|
-
const pathCopy = [name, ...path];
|
|
14
|
-
// The last arg is for instance `.useMutation` or `.useQuery()`
|
|
15
|
-
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
16
|
-
const lastArg = pathCopy.pop();
|
|
17
|
-
if (lastArg === 'useMutation') {
|
|
18
|
-
return hooks[lastArg](pathCopy, ...args);
|
|
19
|
-
}
|
|
20
|
-
if (lastArg === '_def') {
|
|
21
|
-
return {
|
|
22
|
-
path: pathCopy,
|
|
23
|
-
};
|
|
24
|
-
}
|
|
25
|
-
const [input, ...rest] = args;
|
|
26
|
-
const opts = rest[0] || {};
|
|
27
|
-
return hooks[lastArg](pathCopy, input, opts);
|
|
28
|
-
});
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
/**
|
|
32
|
-
* Create proxy for `useQueries` options
|
|
33
|
-
* @internal
|
|
34
|
-
*/
|
|
35
|
-
function createUseQueries(client) {
|
|
36
|
-
return createRecursiveProxy((opts) => {
|
|
37
|
-
const arrayPath = opts.path;
|
|
38
|
-
const dotPath = arrayPath.join('.');
|
|
39
|
-
const [input, _opts] = opts.args;
|
|
40
|
-
const options = {
|
|
41
|
-
queryKey: getQueryKeyInternal(arrayPath, input, 'query'),
|
|
42
|
-
queryFn: () => {
|
|
43
|
-
return client.query(dotPath, input, _opts?.trpc);
|
|
44
|
-
},
|
|
45
|
-
..._opts,
|
|
46
|
-
};
|
|
47
|
-
return options;
|
|
48
|
-
});
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
/**
|
|
52
|
-
* @internal
|
|
53
|
-
*/
|
|
54
|
-
function getClientArgs(queryKey, opts, pageParam) {
|
|
55
|
-
const path = queryKey[0];
|
|
56
|
-
const input = queryKey[1]?.input;
|
|
57
|
-
if (pageParam)
|
|
58
|
-
input.cursor = pageParam;
|
|
59
|
-
return [path.join('.'), input, opts?.trpc];
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
/**
|
|
63
|
-
* Makes a stable reference of the `trpc` prop
|
|
64
|
-
*/
|
|
65
|
-
function useHookResult(value) {
|
|
66
|
-
const ref = React.useRef(value);
|
|
67
|
-
ref.current.path = value.path;
|
|
68
|
-
return ref.current;
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
/**
|
|
72
|
-
* Creates a set of utility functions that can be used to interact with `react-query`
|
|
73
|
-
* @param opts the `TRPCClient` and `QueryClient` to use
|
|
74
|
-
* @returns a set of utility functions that can be used to interact with `react-query`
|
|
75
|
-
* @internal
|
|
76
|
-
*/
|
|
77
|
-
function createUtilityFunctions(opts) {
|
|
78
|
-
const { client, queryClient } = opts;
|
|
79
|
-
const untypedClient = client instanceof TRPCUntypedClient ? client : getUntypedClient(client);
|
|
80
|
-
return {
|
|
81
|
-
fetchQuery: (queryKey, opts) => {
|
|
82
|
-
return queryClient.fetchQuery({
|
|
83
|
-
...opts,
|
|
84
|
-
queryKey,
|
|
85
|
-
queryFn: () => untypedClient.query(...getClientArgs(queryKey, opts)),
|
|
86
|
-
});
|
|
87
|
-
},
|
|
88
|
-
fetchInfiniteQuery: (queryKey, opts) => {
|
|
89
|
-
return queryClient.fetchInfiniteQuery({
|
|
90
|
-
...opts,
|
|
91
|
-
queryKey,
|
|
92
|
-
queryFn: ({ pageParam }) => {
|
|
93
|
-
return untypedClient.query(...getClientArgs(queryKey, opts, pageParam));
|
|
94
|
-
},
|
|
95
|
-
initialPageParam: opts?.initialCursor ?? null,
|
|
96
|
-
});
|
|
97
|
-
},
|
|
98
|
-
prefetchQuery: (queryKey, opts) => {
|
|
99
|
-
return queryClient.prefetchQuery({
|
|
100
|
-
...opts,
|
|
101
|
-
queryKey,
|
|
102
|
-
queryFn: () => untypedClient.query(...getClientArgs(queryKey, opts)),
|
|
103
|
-
});
|
|
104
|
-
},
|
|
105
|
-
prefetchInfiniteQuery: (queryKey, opts) => {
|
|
106
|
-
return queryClient.prefetchInfiniteQuery({
|
|
107
|
-
...opts,
|
|
108
|
-
queryKey,
|
|
109
|
-
queryFn: ({ pageParam }) => {
|
|
110
|
-
return untypedClient.query(...getClientArgs(queryKey, opts, pageParam));
|
|
111
|
-
},
|
|
112
|
-
initialPageParam: opts?.initialCursor ?? null,
|
|
113
|
-
});
|
|
114
|
-
},
|
|
115
|
-
ensureQueryData: (queryKey, opts) => {
|
|
116
|
-
return queryClient.ensureQueryData({
|
|
117
|
-
...opts,
|
|
118
|
-
queryKey,
|
|
119
|
-
queryFn: () => untypedClient.query(...getClientArgs(queryKey, opts)),
|
|
120
|
-
});
|
|
121
|
-
},
|
|
122
|
-
invalidateQueries: (queryKey, filters, options) => {
|
|
123
|
-
return queryClient.invalidateQueries({
|
|
124
|
-
...filters,
|
|
125
|
-
queryKey,
|
|
126
|
-
}, options);
|
|
127
|
-
},
|
|
128
|
-
resetQueries: (queryKey, filters, options) => {
|
|
129
|
-
return queryClient.resetQueries({
|
|
130
|
-
...filters,
|
|
131
|
-
queryKey,
|
|
132
|
-
}, options);
|
|
133
|
-
},
|
|
134
|
-
refetchQueries: (queryKey, filters, options) => {
|
|
135
|
-
return queryClient.refetchQueries({
|
|
136
|
-
...filters,
|
|
137
|
-
queryKey,
|
|
138
|
-
}, options);
|
|
139
|
-
},
|
|
140
|
-
cancelQuery: (queryKey, options) => {
|
|
141
|
-
return queryClient.cancelQueries({
|
|
142
|
-
queryKey,
|
|
143
|
-
}, options);
|
|
144
|
-
},
|
|
145
|
-
setQueryData: (queryKey, updater, options) => {
|
|
146
|
-
return queryClient.setQueryData(queryKey, updater, options);
|
|
147
|
-
},
|
|
148
|
-
getQueryData: (queryKey) => {
|
|
149
|
-
return queryClient.getQueryData(queryKey);
|
|
150
|
-
},
|
|
151
|
-
setInfiniteQueryData: (queryKey, updater, options) => {
|
|
152
|
-
return queryClient.setQueryData(queryKey, updater, options);
|
|
153
|
-
},
|
|
154
|
-
getInfiniteQueryData: (queryKey) => {
|
|
155
|
-
return queryClient.getQueryData(queryKey);
|
|
156
|
-
},
|
|
157
|
-
};
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
/**
|
|
161
|
-
* @internal
|
|
162
|
-
*/
|
|
163
|
-
function createRootHooks(config) {
|
|
164
|
-
const mutationSuccessOverride = config?.overrides?.useMutation?.onSuccess ??
|
|
165
|
-
((options) => options.originalFn());
|
|
166
|
-
const Context = (config?.context ??
|
|
167
|
-
TRPCContext);
|
|
168
|
-
const createClient = (opts) => {
|
|
169
|
-
return createTRPCUntypedClient(opts);
|
|
170
|
-
};
|
|
171
|
-
const TRPCProvider = (props) => {
|
|
172
|
-
const { abortOnUnmount = false, client, queryClient, ssrContext } = props;
|
|
173
|
-
const [ssrState, setSSRState] = React.useState(props.ssrState ?? false);
|
|
174
|
-
const fns = React.useMemo(() => createUtilityFunctions({
|
|
175
|
-
client,
|
|
176
|
-
queryClient,
|
|
177
|
-
}), [client, queryClient]);
|
|
178
|
-
const contextValue = React.useMemo(() => ({
|
|
179
|
-
abortOnUnmount,
|
|
180
|
-
queryClient,
|
|
181
|
-
client,
|
|
182
|
-
ssrContext: ssrContext ?? null,
|
|
183
|
-
ssrState,
|
|
184
|
-
...fns,
|
|
185
|
-
}), [abortOnUnmount, client, fns, queryClient, ssrContext, ssrState]);
|
|
186
|
-
React.useEffect(() => {
|
|
187
|
-
// Only updating state to `mounted` if we are using SSR.
|
|
188
|
-
// This makes it so we don't have an unnecessary re-render when opting out of SSR.
|
|
189
|
-
setSSRState((state) => (state ? 'mounted' : false));
|
|
190
|
-
}, []);
|
|
191
|
-
return (React.createElement(Context.Provider, { value: contextValue }, props.children));
|
|
192
|
-
};
|
|
193
|
-
function useContext() {
|
|
194
|
-
const context = React.useContext(Context);
|
|
195
|
-
if (!context) {
|
|
196
|
-
throw new Error('Unable to find tRPC Context. Did you forget to wrap your App inside `withTRPC` HoC?');
|
|
197
|
-
}
|
|
198
|
-
return context;
|
|
199
|
-
}
|
|
200
|
-
/**
|
|
201
|
-
* Hack to make sure errors return `status`='error` when doing SSR
|
|
202
|
-
* @link https://github.com/trpc/trpc/pull/1645
|
|
203
|
-
*/
|
|
204
|
-
function useSSRQueryOptionsIfNeeded(queryKey, opts) {
|
|
205
|
-
const { queryClient, ssrState } = useContext();
|
|
206
|
-
return ssrState &&
|
|
207
|
-
ssrState !== 'mounted' &&
|
|
208
|
-
queryClient.getQueryCache().find({ queryKey })?.state.status === 'error'
|
|
209
|
-
? {
|
|
210
|
-
retryOnMount: false,
|
|
211
|
-
...opts,
|
|
212
|
-
}
|
|
213
|
-
: opts;
|
|
214
|
-
}
|
|
215
|
-
function useQuery$1(path, input, opts) {
|
|
216
|
-
const context = useContext();
|
|
217
|
-
const { abortOnUnmount, client, ssrState, queryClient, prefetchQuery } = context;
|
|
218
|
-
const queryKey = getQueryKeyInternal(path, input, 'query');
|
|
219
|
-
const defaultOpts = queryClient.getQueryDefaults(queryKey);
|
|
220
|
-
if (typeof window === 'undefined' &&
|
|
221
|
-
ssrState === 'prepass' &&
|
|
222
|
-
opts?.trpc?.ssr !== false &&
|
|
223
|
-
(opts?.enabled ?? defaultOpts?.enabled) !== false &&
|
|
224
|
-
!queryClient.getQueryCache().find({ queryKey })) {
|
|
225
|
-
void prefetchQuery(queryKey, opts);
|
|
226
|
-
}
|
|
227
|
-
const ssrOpts = useSSRQueryOptionsIfNeeded(queryKey, {
|
|
228
|
-
...defaultOpts,
|
|
229
|
-
...opts,
|
|
230
|
-
});
|
|
231
|
-
const shouldAbortOnUnmount = opts?.trpc?.abortOnUnmount ?? config?.abortOnUnmount ?? abortOnUnmount;
|
|
232
|
-
const hook = useQuery({
|
|
233
|
-
...ssrOpts,
|
|
234
|
-
queryKey: queryKey,
|
|
235
|
-
queryFn: (queryFunctionContext) => {
|
|
236
|
-
const actualOpts = {
|
|
237
|
-
...ssrOpts,
|
|
238
|
-
trpc: {
|
|
239
|
-
...ssrOpts?.trpc,
|
|
240
|
-
...(shouldAbortOnUnmount
|
|
241
|
-
? { signal: queryFunctionContext.signal }
|
|
242
|
-
: {}),
|
|
243
|
-
},
|
|
244
|
-
};
|
|
245
|
-
return client.query(...getClientArgs(queryKey, actualOpts));
|
|
246
|
-
},
|
|
247
|
-
}, queryClient);
|
|
248
|
-
hook.trpc = useHookResult({
|
|
249
|
-
path: path.join('.'),
|
|
250
|
-
});
|
|
251
|
-
return hook;
|
|
252
|
-
}
|
|
253
|
-
function useSuspenseQuery$1(path, input, opts) {
|
|
254
|
-
const context = useContext();
|
|
255
|
-
const queryKey = getQueryKeyInternal(path, input, 'query');
|
|
256
|
-
const shouldAbortOnUnmount = opts?.trpc?.abortOnUnmount ??
|
|
257
|
-
config?.abortOnUnmount ??
|
|
258
|
-
context.abortOnUnmount;
|
|
259
|
-
const hook = useSuspenseQuery({
|
|
260
|
-
...opts,
|
|
261
|
-
queryKey: queryKey,
|
|
262
|
-
queryFn: (queryFunctionContext) => {
|
|
263
|
-
const actualOpts = {
|
|
264
|
-
trpc: {
|
|
265
|
-
...(shouldAbortOnUnmount
|
|
266
|
-
? { signal: queryFunctionContext.signal }
|
|
267
|
-
: {}),
|
|
268
|
-
},
|
|
269
|
-
};
|
|
270
|
-
return context.client.query(...getClientArgs(queryKey, actualOpts));
|
|
271
|
-
},
|
|
272
|
-
}, context.queryClient);
|
|
273
|
-
hook.trpc = useHookResult({
|
|
274
|
-
path: path.join('.'),
|
|
275
|
-
});
|
|
276
|
-
return [hook.data, hook];
|
|
277
|
-
}
|
|
278
|
-
function useMutation$1(path, opts) {
|
|
279
|
-
const { client } = useContext();
|
|
280
|
-
const queryClient = useQueryClient();
|
|
281
|
-
const mutationKey = [path];
|
|
282
|
-
const defaultOpts = queryClient.getMutationDefaults(mutationKey);
|
|
283
|
-
const hook = useMutation({
|
|
284
|
-
...opts,
|
|
285
|
-
mutationKey: mutationKey,
|
|
286
|
-
mutationFn: (input) => {
|
|
287
|
-
return client.mutation(...getClientArgs([path, { input }], opts));
|
|
288
|
-
},
|
|
289
|
-
onSuccess(...args) {
|
|
290
|
-
const originalFn = () => opts?.onSuccess?.(...args) ?? defaultOpts?.onSuccess?.(...args);
|
|
291
|
-
return mutationSuccessOverride({
|
|
292
|
-
originalFn,
|
|
293
|
-
queryClient,
|
|
294
|
-
meta: opts?.meta ?? defaultOpts?.meta ?? {},
|
|
295
|
-
});
|
|
296
|
-
},
|
|
297
|
-
}, queryClient);
|
|
298
|
-
hook.trpc = useHookResult({
|
|
299
|
-
path: path.join('.'),
|
|
300
|
-
});
|
|
301
|
-
return hook;
|
|
302
|
-
}
|
|
303
|
-
/* istanbul ignore next -- @preserve */
|
|
304
|
-
function useSubscription(path, input, opts) {
|
|
305
|
-
const enabled = opts?.enabled ?? true;
|
|
306
|
-
const queryKey = hashKey(getQueryKeyInternal(path, input, 'any'));
|
|
307
|
-
const { client } = useContext();
|
|
308
|
-
const optsRef = React.useRef(opts);
|
|
309
|
-
optsRef.current = opts;
|
|
310
|
-
React.useEffect(() => {
|
|
311
|
-
if (!enabled) {
|
|
312
|
-
return;
|
|
313
|
-
}
|
|
314
|
-
let isStopped = false;
|
|
315
|
-
const subscription = client.subscription(path.join('.'), input ?? undefined, {
|
|
316
|
-
onStarted: () => {
|
|
317
|
-
if (!isStopped) {
|
|
318
|
-
optsRef.current.onStarted?.();
|
|
319
|
-
}
|
|
320
|
-
},
|
|
321
|
-
onData: (data) => {
|
|
322
|
-
if (!isStopped) {
|
|
323
|
-
opts.onData(data);
|
|
324
|
-
}
|
|
325
|
-
},
|
|
326
|
-
onError: (err) => {
|
|
327
|
-
if (!isStopped) {
|
|
328
|
-
optsRef.current.onError?.(err);
|
|
329
|
-
}
|
|
330
|
-
},
|
|
331
|
-
});
|
|
332
|
-
return () => {
|
|
333
|
-
isStopped = true;
|
|
334
|
-
subscription.unsubscribe();
|
|
335
|
-
};
|
|
336
|
-
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
337
|
-
}, [queryKey, enabled]);
|
|
338
|
-
}
|
|
339
|
-
function useInfiniteQuery$1(path, input, opts) {
|
|
340
|
-
const { client, ssrState, prefetchInfiniteQuery, queryClient, abortOnUnmount, } = useContext();
|
|
341
|
-
const queryKey = getQueryKeyInternal(path, input, 'infinite');
|
|
342
|
-
const defaultOpts = queryClient.getQueryDefaults(queryKey);
|
|
343
|
-
if (typeof window === 'undefined' &&
|
|
344
|
-
ssrState === 'prepass' &&
|
|
345
|
-
opts?.trpc?.ssr !== false &&
|
|
346
|
-
(opts?.enabled ?? defaultOpts?.enabled) !== false &&
|
|
347
|
-
!queryClient.getQueryCache().find({ queryKey })) {
|
|
348
|
-
void prefetchInfiniteQuery(queryKey, { ...defaultOpts, ...opts });
|
|
349
|
-
}
|
|
350
|
-
const ssrOpts = useSSRQueryOptionsIfNeeded(queryKey, {
|
|
351
|
-
...defaultOpts,
|
|
352
|
-
...opts,
|
|
353
|
-
});
|
|
354
|
-
// request option should take priority over global
|
|
355
|
-
const shouldAbortOnUnmount = opts?.trpc?.abortOnUnmount ?? abortOnUnmount;
|
|
356
|
-
const hook = useInfiniteQuery({
|
|
357
|
-
...ssrOpts,
|
|
358
|
-
initialPageParam: opts.initialCursor ?? null,
|
|
359
|
-
persister: opts.persister,
|
|
360
|
-
queryKey: queryKey,
|
|
361
|
-
queryFn: (queryFunctionContext) => {
|
|
362
|
-
const actualOpts = {
|
|
363
|
-
...ssrOpts,
|
|
364
|
-
trpc: {
|
|
365
|
-
...ssrOpts?.trpc,
|
|
366
|
-
...(shouldAbortOnUnmount
|
|
367
|
-
? { signal: queryFunctionContext.signal }
|
|
368
|
-
: {}),
|
|
369
|
-
},
|
|
370
|
-
};
|
|
371
|
-
return client.query(...getClientArgs(queryKey, actualOpts, queryFunctionContext.pageParam ?? opts.initialCursor));
|
|
372
|
-
},
|
|
373
|
-
}, queryClient);
|
|
374
|
-
hook.trpc = useHookResult({
|
|
375
|
-
path: path.join('.'),
|
|
376
|
-
});
|
|
377
|
-
return hook;
|
|
378
|
-
}
|
|
379
|
-
function useSuspenseInfiniteQuery$1(path, input, opts) {
|
|
380
|
-
const context = useContext();
|
|
381
|
-
const queryKey = getQueryKeyInternal(path, input, 'infinite');
|
|
382
|
-
const defaultOpts = context.queryClient.getQueryDefaults(queryKey);
|
|
383
|
-
const ssrOpts = useSSRQueryOptionsIfNeeded(queryKey, {
|
|
384
|
-
...defaultOpts,
|
|
385
|
-
...opts,
|
|
386
|
-
});
|
|
387
|
-
// request option should take priority over global
|
|
388
|
-
const shouldAbortOnUnmount = opts?.trpc?.abortOnUnmount ?? context.abortOnUnmount;
|
|
389
|
-
const hook = useSuspenseInfiniteQuery({
|
|
390
|
-
...opts,
|
|
391
|
-
initialPageParam: opts.initialCursor ?? null,
|
|
392
|
-
queryKey,
|
|
393
|
-
queryFn: (queryFunctionContext) => {
|
|
394
|
-
const actualOpts = {
|
|
395
|
-
...ssrOpts,
|
|
396
|
-
trpc: {
|
|
397
|
-
...ssrOpts?.trpc,
|
|
398
|
-
...(shouldAbortOnUnmount
|
|
399
|
-
? { signal: queryFunctionContext.signal }
|
|
400
|
-
: {}),
|
|
401
|
-
},
|
|
402
|
-
};
|
|
403
|
-
return context.client.query(...getClientArgs(queryKey, actualOpts, queryFunctionContext.pageParam ?? opts.initialCursor));
|
|
404
|
-
},
|
|
405
|
-
}, context.queryClient);
|
|
406
|
-
hook.trpc = useHookResult({
|
|
407
|
-
path: path.join('.'),
|
|
408
|
-
});
|
|
409
|
-
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
410
|
-
return [hook.data, hook];
|
|
411
|
-
}
|
|
412
|
-
const useQueries$1 = (queriesCallback) => {
|
|
413
|
-
const { ssrState, queryClient, prefetchQuery, client } = useContext();
|
|
414
|
-
const proxy = createUseQueries(client);
|
|
415
|
-
const queries = queriesCallback(proxy);
|
|
416
|
-
if (typeof window === 'undefined' && ssrState === 'prepass') {
|
|
417
|
-
for (const query of queries) {
|
|
418
|
-
const queryOption = query;
|
|
419
|
-
if (queryOption.trpc?.ssr !== false &&
|
|
420
|
-
!queryClient.getQueryCache().find({ queryKey: queryOption.queryKey })) {
|
|
421
|
-
void prefetchQuery(queryOption.queryKey, queryOption);
|
|
422
|
-
}
|
|
423
|
-
}
|
|
424
|
-
}
|
|
425
|
-
return useQueries({
|
|
426
|
-
queries: queries.map((query) => ({
|
|
427
|
-
...query,
|
|
428
|
-
queryKey: query.queryKey,
|
|
429
|
-
})),
|
|
430
|
-
}, queryClient);
|
|
431
|
-
};
|
|
432
|
-
const useSuspenseQueries$1 = (queriesCallback) => {
|
|
433
|
-
const { queryClient, client } = useContext();
|
|
434
|
-
const proxy = createUseQueries(client);
|
|
435
|
-
const queries = queriesCallback(proxy);
|
|
436
|
-
const hook = useSuspenseQueries({
|
|
437
|
-
queries: queries.map((query) => ({
|
|
438
|
-
...query,
|
|
439
|
-
queryKey: query.queryKey,
|
|
440
|
-
})),
|
|
441
|
-
}, queryClient);
|
|
442
|
-
return [hook.map((h) => h.data), hook];
|
|
443
|
-
};
|
|
444
|
-
const useDehydratedState = (client, trpcState) => {
|
|
445
|
-
const transformed = React.useMemo(() => {
|
|
446
|
-
if (!trpcState) {
|
|
447
|
-
return trpcState;
|
|
448
|
-
}
|
|
449
|
-
return client.runtime.transformer.deserialize(trpcState);
|
|
450
|
-
}, [trpcState, client]);
|
|
451
|
-
return transformed;
|
|
452
|
-
};
|
|
453
|
-
return {
|
|
454
|
-
Provider: TRPCProvider,
|
|
455
|
-
createClient,
|
|
456
|
-
useContext,
|
|
457
|
-
useUtils: useContext,
|
|
458
|
-
useQuery: useQuery$1,
|
|
459
|
-
useSuspenseQuery: useSuspenseQuery$1,
|
|
460
|
-
useQueries: useQueries$1,
|
|
461
|
-
useSuspenseQueries: useSuspenseQueries$1,
|
|
462
|
-
useMutation: useMutation$1,
|
|
463
|
-
useSubscription,
|
|
464
|
-
useDehydratedState,
|
|
465
|
-
useInfiniteQuery: useInfiniteQuery$1,
|
|
466
|
-
useSuspenseInfiniteQuery: useSuspenseInfiniteQuery$1,
|
|
467
|
-
};
|
|
468
|
-
}
|
|
469
|
-
|
|
470
|
-
export { createReactDecoration as a, createUtilityFunctions as b, createRootHooks as c, createUseQueries as d, getClientArgs as g };
|
|
@@ -1,142 +0,0 @@
|
|
|
1
|
-
import { createTRPCClientProxy } from '@trpc/client';
|
|
2
|
-
import { createFlatProxy, createRecursiveProxy } from '@trpc/core';
|
|
3
|
-
import * as React from 'react';
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* To allow easy interactions with groups of related queries, such as
|
|
7
|
-
* invalidating all queries of a router, we use an array as the path when
|
|
8
|
-
* storing in tanstack query.
|
|
9
|
-
**/
|
|
10
|
-
function getQueryKeyInternal(path, input, type) {
|
|
11
|
-
// Construct a query key that is easy to destructure and flexible for
|
|
12
|
-
// partial selecting etc.
|
|
13
|
-
// https://github.com/trpc/trpc/issues/3128
|
|
14
|
-
// some parts of the path may be dot-separated, split them up
|
|
15
|
-
const splitPath = path.flatMap((part) => part.split('.'));
|
|
16
|
-
if (!input && (!type || type === 'any')) {
|
|
17
|
-
// for `utils.invalidate()` to match all queries (including vanilla react-query)
|
|
18
|
-
// we don't want nested array if path is empty, i.e. `[]` instead of `[[]]`
|
|
19
|
-
return splitPath.length ? [splitPath] : [];
|
|
20
|
-
}
|
|
21
|
-
if (type === 'infinite' &&
|
|
22
|
-
input &&
|
|
23
|
-
typeof input === 'object' &&
|
|
24
|
-
'cursor' in input) {
|
|
25
|
-
const { cursor: _, ...inputWithoutCursor } = input;
|
|
26
|
-
return [
|
|
27
|
-
splitPath,
|
|
28
|
-
{
|
|
29
|
-
input: inputWithoutCursor,
|
|
30
|
-
type: 'infinite',
|
|
31
|
-
},
|
|
32
|
-
];
|
|
33
|
-
}
|
|
34
|
-
return [
|
|
35
|
-
splitPath,
|
|
36
|
-
{
|
|
37
|
-
...(typeof input !== 'undefined' && { input: input }),
|
|
38
|
-
...(type && type !== 'any' && { type: type }),
|
|
39
|
-
},
|
|
40
|
-
];
|
|
41
|
-
}
|
|
42
|
-
/**
|
|
43
|
-
* Method to extract the query key for a procedure
|
|
44
|
-
* @param procedureOrRouter - procedure or AnyRouter
|
|
45
|
-
* @param input - input to procedureOrRouter
|
|
46
|
-
* @param type - defaults to `any`
|
|
47
|
-
* @link https://trpc.io/docs/v11/getQueryKey
|
|
48
|
-
*/
|
|
49
|
-
function getQueryKey(..._params) {
|
|
50
|
-
const [procedureOrRouter, input, type] = _params;
|
|
51
|
-
// @ts-expect-error - we don't expose _def on the type layer
|
|
52
|
-
const path = procedureOrRouter._def().path;
|
|
53
|
-
const queryKey = getQueryKeyInternal(path, input, type ?? 'any');
|
|
54
|
-
return queryKey;
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
const contextProps = [
|
|
58
|
-
'client',
|
|
59
|
-
'ssrContext',
|
|
60
|
-
'ssrState',
|
|
61
|
-
'abortOnUnmount',
|
|
62
|
-
];
|
|
63
|
-
const TRPCContext = React.createContext?.(null);
|
|
64
|
-
|
|
65
|
-
const getQueryType = (utilName) => {
|
|
66
|
-
switch (utilName) {
|
|
67
|
-
case 'fetch':
|
|
68
|
-
case 'ensureData':
|
|
69
|
-
case 'prefetch':
|
|
70
|
-
case 'getData':
|
|
71
|
-
case 'setData':
|
|
72
|
-
return 'query';
|
|
73
|
-
case 'fetchInfinite':
|
|
74
|
-
case 'prefetchInfinite':
|
|
75
|
-
case 'getInfiniteData':
|
|
76
|
-
case 'setInfiniteData':
|
|
77
|
-
return 'infinite';
|
|
78
|
-
case 'cancel':
|
|
79
|
-
case 'invalidate':
|
|
80
|
-
case 'refetch':
|
|
81
|
-
case 'reset':
|
|
82
|
-
return 'any';
|
|
83
|
-
}
|
|
84
|
-
};
|
|
85
|
-
/**
|
|
86
|
-
* @internal
|
|
87
|
-
*/
|
|
88
|
-
function createRecursiveUtilsProxy(context, key) {
|
|
89
|
-
return createRecursiveProxy((opts) => {
|
|
90
|
-
const path = [key, ...opts.path];
|
|
91
|
-
const utilName = path.pop();
|
|
92
|
-
const args = [...opts.args];
|
|
93
|
-
const input = args.shift(); // args can now be spread when input removed
|
|
94
|
-
const queryType = getQueryType(utilName);
|
|
95
|
-
const queryKey = getQueryKeyInternal(path, input, queryType);
|
|
96
|
-
const contextMap = {
|
|
97
|
-
fetch: () => context.fetchQuery(queryKey, ...args),
|
|
98
|
-
fetchInfinite: () => context.fetchInfiniteQuery(queryKey, args[0]),
|
|
99
|
-
prefetch: () => context.prefetchQuery(queryKey, ...args),
|
|
100
|
-
prefetchInfinite: () => context.prefetchInfiniteQuery(queryKey, args[0]),
|
|
101
|
-
ensureData: () => context.ensureQueryData(queryKey, ...args),
|
|
102
|
-
invalidate: () => context.invalidateQueries(queryKey, ...args),
|
|
103
|
-
reset: () => context.resetQueries(queryKey, ...args),
|
|
104
|
-
refetch: () => context.refetchQueries(queryKey, ...args),
|
|
105
|
-
cancel: () => context.cancelQuery(queryKey, ...args),
|
|
106
|
-
setData: () => {
|
|
107
|
-
context.setQueryData(queryKey, args[0], args[1]);
|
|
108
|
-
},
|
|
109
|
-
setInfiniteData: () => {
|
|
110
|
-
context.setInfiniteQueryData(queryKey, args[0], args[1]);
|
|
111
|
-
},
|
|
112
|
-
getData: () => context.getQueryData(queryKey),
|
|
113
|
-
getInfiniteData: () => context.getInfiniteQueryData(queryKey),
|
|
114
|
-
};
|
|
115
|
-
return contextMap[utilName]();
|
|
116
|
-
});
|
|
117
|
-
}
|
|
118
|
-
/**
|
|
119
|
-
* @internal
|
|
120
|
-
*/
|
|
121
|
-
function createReactQueryUtils(context) {
|
|
122
|
-
return createFlatProxy((key) => {
|
|
123
|
-
const contextName = key;
|
|
124
|
-
if (contextName === 'client') {
|
|
125
|
-
return createTRPCClientProxy(context.client);
|
|
126
|
-
}
|
|
127
|
-
if (contextProps.includes(contextName)) {
|
|
128
|
-
return context[contextName];
|
|
129
|
-
}
|
|
130
|
-
return createRecursiveUtilsProxy(context, key);
|
|
131
|
-
});
|
|
132
|
-
}
|
|
133
|
-
/**
|
|
134
|
-
* @internal
|
|
135
|
-
*/
|
|
136
|
-
function createQueryUtilsProxy(context) {
|
|
137
|
-
return createFlatProxy((key) => {
|
|
138
|
-
return createRecursiveUtilsProxy(context, key);
|
|
139
|
-
});
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
export { TRPCContext as T, createQueryUtilsProxy as a, getQueryKeyInternal as b, createReactQueryUtils as c, getQueryType as d, contextProps as e, getQueryKey as g };
|