@trpc/react-query 11.0.0-alpha-tmp-export-from-main.212 → 11.0.0-alpha-tmp-export-from-main-nuke-core.236

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 (92) hide show
  1. package/dist/bundle-analysis.json +240 -0
  2. package/dist/createTRPCQueryUtils.d.ts +1 -1
  3. package/dist/createTRPCQueryUtils.d.ts.map +1 -1
  4. package/dist/createTRPCQueryUtils.js +16 -0
  5. package/dist/createTRPCQueryUtils.mjs +14 -0
  6. package/dist/createTRPCReact.d.ts +1 -1
  7. package/dist/createTRPCReact.d.ts.map +1 -1
  8. package/dist/createTRPCReact.js +58 -0
  9. package/dist/createTRPCReact.mjs +36 -0
  10. package/dist/index.js +10 -64
  11. package/dist/index.mjs +3 -40
  12. package/dist/internals/context.d.ts +1 -1
  13. package/dist/internals/context.d.ts.map +1 -1
  14. package/dist/internals/context.js +33 -0
  15. package/dist/internals/context.mjs +11 -0
  16. package/dist/internals/getClientArgs.js +16 -0
  17. package/dist/internals/getClientArgs.mjs +14 -0
  18. package/dist/internals/getQueryKey.d.ts +1 -1
  19. package/dist/internals/getQueryKey.d.ts.map +1 -1
  20. package/dist/internals/getQueryKey.js +57 -0
  21. package/dist/internals/getQueryKey.mjs +54 -0
  22. package/dist/internals/useHookResult.js +32 -0
  23. package/dist/internals/useHookResult.mjs +11 -0
  24. package/dist/internals/useQueries.d.ts +1 -1
  25. package/dist/internals/useQueries.d.ts.map +1 -1
  26. package/dist/server/index.js +2 -102
  27. package/dist/server/index.mjs +1 -103
  28. package/dist/server/ssgProxy.d.ts +1 -1
  29. package/dist/server/ssgProxy.d.ts.map +1 -1
  30. package/dist/server/ssgProxy.js +107 -0
  31. package/dist/server/ssgProxy.mjs +105 -0
  32. package/dist/shared/hooks/createHooksInternal.d.ts +1 -1
  33. package/dist/shared/hooks/createHooksInternal.d.ts.map +1 -1
  34. package/dist/{createHooksInternal-5d2fa367.js → shared/hooks/createHooksInternal.js} +28 -183
  35. package/dist/{createHooksInternal-e0b0564e.mjs → shared/hooks/createHooksInternal.mjs} +8 -158
  36. package/dist/shared/hooks/types.d.ts +1 -1
  37. package/dist/shared/hooks/types.d.ts.map +1 -1
  38. package/dist/shared/index.js +13 -15
  39. package/dist/shared/index.mjs +7 -7
  40. package/dist/shared/polymorphism/mutationLike.d.ts +1 -1
  41. package/dist/shared/polymorphism/mutationLike.d.ts.map +1 -1
  42. package/dist/shared/polymorphism/queryLike.d.ts +1 -1
  43. package/dist/shared/polymorphism/queryLike.d.ts.map +1 -1
  44. package/dist/shared/polymorphism/routerLike.d.ts +1 -1
  45. package/dist/shared/polymorphism/routerLike.d.ts.map +1 -1
  46. package/dist/shared/polymorphism/utilsLike.d.ts +1 -1
  47. package/dist/shared/polymorphism/utilsLike.d.ts.map +1 -1
  48. package/dist/shared/proxy/decorationProxy.d.ts +1 -1
  49. package/dist/shared/proxy/decorationProxy.d.ts.map +1 -1
  50. package/dist/shared/proxy/decorationProxy.js +31 -0
  51. package/dist/shared/proxy/decorationProxy.mjs +29 -0
  52. package/dist/shared/proxy/useQueriesProxy.d.ts +1 -1
  53. package/dist/shared/proxy/useQueriesProxy.d.ts.map +1 -1
  54. package/dist/shared/proxy/useQueriesProxy.js +25 -0
  55. package/dist/shared/proxy/useQueriesProxy.mjs +23 -0
  56. package/dist/shared/proxy/utilsProxy.d.ts +1 -1
  57. package/dist/shared/proxy/utilsProxy.d.ts.map +1 -1
  58. package/dist/shared/proxy/utilsProxy.js +89 -0
  59. package/dist/{utilsProxy-61a4601f.mjs → shared/proxy/utilsProxy.mjs} +4 -52
  60. package/dist/{queryClient-4d766c0c.mjs → shared/queryClient.mjs} +1 -1
  61. package/dist/shared/types.d.ts +1 -1
  62. package/dist/shared/types.d.ts.map +1 -1
  63. package/dist/utils/createUtilityFunctions.d.ts +1 -1
  64. package/dist/utils/createUtilityFunctions.d.ts.map +1 -1
  65. package/dist/utils/createUtilityFunctions.js +94 -0
  66. package/dist/utils/createUtilityFunctions.mjs +92 -0
  67. package/dist/utils/inferReactQueryProcedure.d.ts +1 -1
  68. package/dist/utils/inferReactQueryProcedure.d.ts.map +1 -1
  69. package/package.json +16 -32
  70. package/src/createTRPCQueryUtils.tsx +1 -1
  71. package/src/createTRPCReact.tsx +2 -2
  72. package/src/internals/context.tsx +4 -1
  73. package/src/internals/getQueryKey.ts +20 -2
  74. package/src/internals/useQueries.ts +4 -1
  75. package/src/server/ssgProxy.ts +2 -2
  76. package/src/shared/hooks/createHooksInternal.tsx +1 -1
  77. package/src/shared/hooks/types.ts +4 -1
  78. package/src/shared/polymorphism/mutationLike.ts +1 -1
  79. package/src/shared/polymorphism/queryLike.ts +1 -1
  80. package/src/shared/polymorphism/routerLike.ts +1 -1
  81. package/src/shared/polymorphism/utilsLike.ts +1 -1
  82. package/src/shared/proxy/decorationProxy.ts +2 -2
  83. package/src/shared/proxy/useQueriesProxy.ts +2 -2
  84. package/src/shared/proxy/utilsProxy.ts +5 -2
  85. package/src/shared/types.ts +4 -1
  86. package/src/utils/createUtilityFunctions.ts +1 -1
  87. package/src/utils/inferReactQueryProcedure.ts +1 -1
  88. package/dist/createHooksInternal-4285c71a.js +0 -470
  89. package/dist/queryClient-1c8d7d8a.js +0 -8
  90. package/dist/utilsProxy-0b88c1e3.js +0 -161
  91. package/dist/utilsProxy-ff357a62.js +0 -128
  92. /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-ff357a62.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,8 +0,0 @@
1
- import { QueryClient } from '@tanstack/react-query';
2
-
3
- /**
4
- * @internal
5
- */
6
- const getQueryClient = (config) => config.queryClient ?? new QueryClient(config.queryClientConfig);
7
-
8
- export { getQueryClient as g };
@@ -1,161 +0,0 @@
1
- 'use strict';
2
-
3
- var client = require('@trpc/client');
4
- var core = require('@trpc/core');
5
- var React = require('react');
6
-
7
- function _interopNamespace(e) {
8
- if (e && e.__esModule) return e;
9
- var n = Object.create(null);
10
- if (e) {
11
- Object.keys(e).forEach(function (k) {
12
- if (k !== 'default') {
13
- var d = Object.getOwnPropertyDescriptor(e, k);
14
- Object.defineProperty(n, k, d.get ? d : {
15
- enumerable: true,
16
- get: function () { return e[k]; }
17
- });
18
- }
19
- });
20
- }
21
- n["default"] = e;
22
- return Object.freeze(n);
23
- }
24
-
25
- var React__namespace = /*#__PURE__*/_interopNamespace(React);
26
-
27
- /**
28
- * To allow easy interactions with groups of related queries, such as
29
- * invalidating all queries of a router, we use an array as the path when
30
- * storing in tanstack query.
31
- **/ function getQueryKeyInternal(path, input, type) {
32
- // Construct a query key that is easy to destructure and flexible for
33
- // partial selecting etc.
34
- // https://github.com/trpc/trpc/issues/3128
35
- // some parts of the path may be dot-separated, split them up
36
- const splitPath = path.flatMap((part)=>part.split('.'));
37
- if (!input && (!type || type === 'any')) // for `utils.invalidate()` to match all queries (including vanilla react-query)
38
- // we don't want nested array if path is empty, i.e. `[]` instead of `[[]]`
39
- return splitPath.length ? [
40
- splitPath
41
- ] : [];
42
- return [
43
- splitPath,
44
- {
45
- ...typeof input !== 'undefined' && {
46
- input: input
47
- },
48
- ...type && type !== 'any' && {
49
- type: type
50
- }
51
- }
52
- ];
53
- }
54
- /**
55
- * Method to extract the query key for a procedure
56
- * @param procedureOrRouter - procedure or AnyRouter
57
- * @param input - input to procedureOrRouter
58
- * @param type - defaults to `any`
59
- * @link https://trpc.io/docs/v11/getQueryKey
60
- */ function getQueryKey(..._params) {
61
- const [procedureOrRouter, input, type] = _params;
62
- // @ts-expect-error - we don't expose _def on the type layer
63
- const path = procedureOrRouter._def().path;
64
- const queryKey = getQueryKeyInternal(path, input, type ?? 'any');
65
- return queryKey;
66
- }
67
-
68
- const contextProps = [
69
- 'client',
70
- 'ssrContext',
71
- 'ssrState',
72
- 'abortOnUnmount'
73
- ];
74
- const TRPCContext = React__namespace.createContext?.(null);
75
-
76
- const getQueryType = (utilName)=>{
77
- switch(utilName){
78
- case 'fetch':
79
- case 'ensureData':
80
- case 'prefetch':
81
- case 'getData':
82
- case 'setData':
83
- return 'query';
84
- case 'fetchInfinite':
85
- case 'prefetchInfinite':
86
- case 'getInfiniteData':
87
- case 'setInfiniteData':
88
- return 'infinite';
89
- case 'cancel':
90
- case 'invalidate':
91
- case 'refetch':
92
- case 'reset':
93
- return 'any';
94
- }
95
- };
96
- /**
97
- * @internal
98
- */ function createRecursiveUtilsProxy(context, key) {
99
- return core.createRecursiveProxy((opts)=>{
100
- const path = [
101
- key,
102
- ...opts.path
103
- ];
104
- const utilName = path.pop();
105
- const args = [
106
- ...opts.args
107
- ];
108
- const input = args.shift(); // args can now be spread when input removed
109
- const queryType = getQueryType(utilName);
110
- const queryKey = getQueryKeyInternal(path, input, queryType);
111
- const contextMap = {
112
- fetch: ()=>context.fetchQuery(queryKey, ...args),
113
- fetchInfinite: ()=>context.fetchInfiniteQuery(queryKey, args[0]),
114
- prefetch: ()=>context.prefetchQuery(queryKey, ...args),
115
- prefetchInfinite: ()=>context.prefetchInfiniteQuery(queryKey, args[0]),
116
- ensureData: ()=>context.ensureQueryData(queryKey, ...args),
117
- invalidate: ()=>context.invalidateQueries(queryKey, ...args),
118
- reset: ()=>context.resetQueries(queryKey, ...args),
119
- refetch: ()=>context.refetchQueries(queryKey, ...args),
120
- cancel: ()=>context.cancelQuery(queryKey, ...args),
121
- setData: ()=>{
122
- context.setQueryData(queryKey, args[0], args[1]);
123
- },
124
- setInfiniteData: ()=>{
125
- context.setInfiniteQueryData(queryKey, args[0], args[1]);
126
- },
127
- getData: ()=>context.getQueryData(queryKey),
128
- getInfiniteData: ()=>context.getInfiniteQueryData(queryKey)
129
- };
130
- return contextMap[utilName]();
131
- });
132
- }
133
- /**
134
- * @internal
135
- */ function createReactQueryUtils(context) {
136
- return core.createFlatProxy((key)=>{
137
- const contextName = key;
138
- if (contextName === 'client') {
139
- return client.createTRPCClientProxy(context.client);
140
- }
141
- if (contextProps.includes(contextName)) {
142
- return context[contextName];
143
- }
144
- return createRecursiveUtilsProxy(context, key);
145
- });
146
- }
147
- /**
148
- * @internal
149
- */ function createQueryUtilsProxy(context) {
150
- return core.createFlatProxy((key)=>{
151
- return createRecursiveUtilsProxy(context, key);
152
- });
153
- }
154
-
155
- exports.TRPCContext = TRPCContext;
156
- exports.contextProps = contextProps;
157
- exports.createQueryUtilsProxy = createQueryUtilsProxy;
158
- exports.createReactQueryUtils = createReactQueryUtils;
159
- exports.getQueryKey = getQueryKey;
160
- exports.getQueryKeyInternal = getQueryKeyInternal;
161
- exports.getQueryType = getQueryType;