@trpc/react-query 11.0.0-alpha-tmp-ahmed-rollup-v4.203 → 11.0.0-alpha-tmp-opt-peers.203

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.
Files changed (36) hide show
  1. package/dist/createHooksInternal-5cb13e16.js +470 -0
  2. package/dist/{shared/hooks/createHooksInternal.js → createHooksInternal-6c0232d7.js} +185 -27
  3. package/dist/{shared/hooks/createHooksInternal.mjs → createHooksInternal-df9ecb1f.mjs} +159 -9
  4. package/dist/index.js +44 -10
  5. package/dist/index.mjs +40 -3
  6. package/dist/queryClient-1c8d7d8a.js +8 -0
  7. package/dist/{shared/queryClient.mjs → queryClient-4d766c0c.mjs} +1 -1
  8. package/dist/server/index.js +102 -2
  9. package/dist/server/index.mjs +103 -1
  10. package/dist/shared/index.js +15 -13
  11. package/dist/shared/index.mjs +7 -7
  12. package/dist/{shared/proxy/utilsProxy.mjs → utilsProxy-5402e992.mjs} +51 -3
  13. package/dist/utilsProxy-c08ebe6e.js +128 -0
  14. package/dist/{shared/proxy/utilsProxy.js → utilsProxy-f0fe7c68.js} +60 -8
  15. package/package.json +6 -6
  16. package/dist/createTRPCQueryUtils.js +0 -15
  17. package/dist/createTRPCQueryUtils.mjs +0 -13
  18. package/dist/createTRPCReact.js +0 -38
  19. package/dist/createTRPCReact.mjs +0 -35
  20. package/dist/internals/context.js +0 -14
  21. package/dist/internals/context.mjs +0 -11
  22. package/dist/internals/getClientArgs.js +0 -16
  23. package/dist/internals/getClientArgs.mjs +0 -14
  24. package/dist/internals/getQueryKey.js +0 -45
  25. package/dist/internals/getQueryKey.mjs +0 -42
  26. package/dist/internals/useHookResult.js +0 -13
  27. package/dist/internals/useHookResult.mjs +0 -11
  28. package/dist/server/ssgProxy.js +0 -106
  29. package/dist/server/ssgProxy.mjs +0 -104
  30. package/dist/shared/proxy/decorationProxy.js +0 -31
  31. package/dist/shared/proxy/decorationProxy.mjs +0 -29
  32. package/dist/shared/proxy/useQueriesProxy.js +0 -25
  33. package/dist/shared/proxy/useQueriesProxy.mjs +0 -23
  34. package/dist/utils/createUtilityFunctions.js +0 -94
  35. package/dist/utils/createUtilityFunctions.mjs +0 -92
  36. /package/dist/{shared/queryClient.js → queryClient-358a9a75.js} +0 -0
@@ -0,0 +1,470 @@
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 React, { useRef, useState, useMemo, useEffect } from 'react';
5
+ import { b as getQueryKeyInternal, T as TRPCContext } from './utilsProxy-c08ebe6e.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 = 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] = useState(props.ssrState ?? false);
174
+ const fns = useMemo(() => createUtilityFunctions({
175
+ client,
176
+ queryClient,
177
+ }), [client, queryClient]);
178
+ const contextValue = useMemo(() => ({
179
+ abortOnUnmount,
180
+ queryClient,
181
+ client,
182
+ ssrContext: ssrContext ?? null,
183
+ ssrState,
184
+ ...fns,
185
+ }), [abortOnUnmount, client, fns, queryClient, ssrContext, ssrState]);
186
+ 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 = useRef(opts);
309
+ optsRef.current = opts;
310
+ 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 = 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 };