@trpc/react-query 10.42.0 → 11.0.0-next.91
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
|
@@ -0,0 +1,490 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var shared = require('@trpc/server/shared');
|
|
4
|
+
var reactQuery = require('@tanstack/react-query');
|
|
5
|
+
var client = require('@trpc/client');
|
|
6
|
+
var React = require('react');
|
|
7
|
+
var utilsProxy = require('./utilsProxy-27bd93c1.js');
|
|
8
|
+
|
|
9
|
+
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
|
|
10
|
+
|
|
11
|
+
var React__default = /*#__PURE__*/_interopDefaultLegacy(React);
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Create proxy for decorating procedures
|
|
15
|
+
* @internal
|
|
16
|
+
*/ function createReactDecoration(name, hooks) {
|
|
17
|
+
return shared.createRecursiveProxy(({ path , args })=>{
|
|
18
|
+
const pathCopy = [
|
|
19
|
+
name,
|
|
20
|
+
...path
|
|
21
|
+
];
|
|
22
|
+
// The last arg is for instance `.useMutation` or `.useQuery()`
|
|
23
|
+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
24
|
+
const lastArg = pathCopy.pop();
|
|
25
|
+
if (lastArg === 'useMutation') {
|
|
26
|
+
return hooks[lastArg](pathCopy, ...args);
|
|
27
|
+
}
|
|
28
|
+
if (lastArg === '_def') {
|
|
29
|
+
return {
|
|
30
|
+
path: pathCopy
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
const [input, ...rest] = args;
|
|
34
|
+
const opts = rest[0] || {};
|
|
35
|
+
return hooks[lastArg](pathCopy, input, opts);
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Create proxy for `useQueries` options
|
|
41
|
+
* @internal
|
|
42
|
+
*/ function createUseQueries(client) {
|
|
43
|
+
return shared.createRecursiveProxy((opts)=>{
|
|
44
|
+
const arrayPath = opts.path;
|
|
45
|
+
const dotPath = arrayPath.join('.');
|
|
46
|
+
const [input, _opts] = opts.args;
|
|
47
|
+
const options = {
|
|
48
|
+
queryKey: utilsProxy.getQueryKeyInternal(arrayPath, input, 'query'),
|
|
49
|
+
queryFn: ()=>{
|
|
50
|
+
return client.query(dotPath, input, _opts?.trpc);
|
|
51
|
+
},
|
|
52
|
+
..._opts
|
|
53
|
+
};
|
|
54
|
+
return options;
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function getClientArgs(queryKey, opts, pageParam) {
|
|
59
|
+
const path = queryKey[0];
|
|
60
|
+
const input = queryKey[1]?.input;
|
|
61
|
+
if (pageParam) input.cursor = pageParam;
|
|
62
|
+
return [
|
|
63
|
+
path.join('.'),
|
|
64
|
+
input,
|
|
65
|
+
opts?.trpc
|
|
66
|
+
];
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Makes a stable reference of the `trpc` prop
|
|
71
|
+
*/ function useHookResult(value) {
|
|
72
|
+
const ref = React.useRef(value);
|
|
73
|
+
ref.current.path = value.path;
|
|
74
|
+
return ref.current;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* @internal
|
|
79
|
+
*/ function createRootHooks(config) {
|
|
80
|
+
const mutationSuccessOverride = config?.overrides?.useMutation?.onSuccess ?? ((options)=>options.originalFn());
|
|
81
|
+
const Context = config?.context ?? utilsProxy.TRPCContext;
|
|
82
|
+
const createClient = (opts)=>{
|
|
83
|
+
return client.createTRPCUntypedClient(opts);
|
|
84
|
+
};
|
|
85
|
+
const TRPCProvider = (props)=>{
|
|
86
|
+
const { abortOnUnmount =false , client , queryClient , ssrContext } = props;
|
|
87
|
+
const [ssrState, setSSRState] = React.useState(props.ssrState ?? false);
|
|
88
|
+
React.useEffect(()=>{
|
|
89
|
+
// Only updating state to `mounted` if we are using SSR.
|
|
90
|
+
// This makes it so we don't have an unnecessary re-render when opting out of SSR.
|
|
91
|
+
setSSRState((state)=>state ? 'mounted' : false);
|
|
92
|
+
}, []);
|
|
93
|
+
return /*#__PURE__*/ React__default["default"].createElement(Context.Provider, {
|
|
94
|
+
value: {
|
|
95
|
+
abortOnUnmount,
|
|
96
|
+
queryClient,
|
|
97
|
+
client,
|
|
98
|
+
ssrContext: ssrContext ?? null,
|
|
99
|
+
ssrState,
|
|
100
|
+
fetchQuery: React.useCallback((queryKey, opts)=>{
|
|
101
|
+
return queryClient.fetchQuery({
|
|
102
|
+
...opts,
|
|
103
|
+
queryKey,
|
|
104
|
+
queryFn: ()=>client.query(...getClientArgs(queryKey, opts))
|
|
105
|
+
});
|
|
106
|
+
}, [
|
|
107
|
+
client,
|
|
108
|
+
queryClient
|
|
109
|
+
]),
|
|
110
|
+
fetchInfiniteQuery: React.useCallback((queryKey, opts)=>{
|
|
111
|
+
return queryClient.fetchInfiniteQuery({
|
|
112
|
+
...opts,
|
|
113
|
+
queryKey,
|
|
114
|
+
queryFn: ({ pageParam })=>{
|
|
115
|
+
return client.query(...getClientArgs(queryKey, opts, pageParam));
|
|
116
|
+
},
|
|
117
|
+
initialPageParam: opts?.initialCursor ?? null
|
|
118
|
+
});
|
|
119
|
+
}, [
|
|
120
|
+
client,
|
|
121
|
+
queryClient
|
|
122
|
+
]),
|
|
123
|
+
prefetchQuery: React.useCallback((queryKey, opts)=>{
|
|
124
|
+
return queryClient.prefetchQuery({
|
|
125
|
+
...opts,
|
|
126
|
+
queryKey,
|
|
127
|
+
queryFn: ()=>client.query(...getClientArgs(queryKey, opts))
|
|
128
|
+
});
|
|
129
|
+
}, [
|
|
130
|
+
client,
|
|
131
|
+
queryClient
|
|
132
|
+
]),
|
|
133
|
+
prefetchInfiniteQuery: React.useCallback((queryKey, opts)=>{
|
|
134
|
+
return queryClient.prefetchInfiniteQuery({
|
|
135
|
+
...opts,
|
|
136
|
+
queryKey,
|
|
137
|
+
queryFn: ({ pageParam })=>{
|
|
138
|
+
return client.query(...getClientArgs(queryKey, opts, pageParam));
|
|
139
|
+
},
|
|
140
|
+
initialPageParam: opts?.initialCursor ?? null
|
|
141
|
+
});
|
|
142
|
+
}, [
|
|
143
|
+
client,
|
|
144
|
+
queryClient
|
|
145
|
+
]),
|
|
146
|
+
ensureQueryData: React.useCallback((queryKey, opts)=>{
|
|
147
|
+
return queryClient.ensureQueryData({
|
|
148
|
+
...opts,
|
|
149
|
+
queryKey,
|
|
150
|
+
queryFn: ()=>client.query(...getClientArgs(queryKey, opts))
|
|
151
|
+
});
|
|
152
|
+
}, [
|
|
153
|
+
client,
|
|
154
|
+
queryClient
|
|
155
|
+
]),
|
|
156
|
+
invalidateQueries: React.useCallback((queryKey, filters, options)=>{
|
|
157
|
+
return queryClient.invalidateQueries({
|
|
158
|
+
...filters,
|
|
159
|
+
queryKey
|
|
160
|
+
}, options);
|
|
161
|
+
}, [
|
|
162
|
+
queryClient
|
|
163
|
+
]),
|
|
164
|
+
resetQueries: React.useCallback((queryKey, filters, options)=>{
|
|
165
|
+
return queryClient.resetQueries({
|
|
166
|
+
...filters,
|
|
167
|
+
queryKey
|
|
168
|
+
}, options);
|
|
169
|
+
}, [
|
|
170
|
+
queryClient
|
|
171
|
+
]),
|
|
172
|
+
refetchQueries: React.useCallback((queryKey, filters, options)=>{
|
|
173
|
+
return queryClient.refetchQueries({
|
|
174
|
+
...filters,
|
|
175
|
+
queryKey
|
|
176
|
+
}, options);
|
|
177
|
+
}, [
|
|
178
|
+
queryClient
|
|
179
|
+
]),
|
|
180
|
+
cancelQuery: React.useCallback((queryKey, options)=>{
|
|
181
|
+
return queryClient.cancelQueries({
|
|
182
|
+
queryKey
|
|
183
|
+
}, options);
|
|
184
|
+
}, [
|
|
185
|
+
queryClient
|
|
186
|
+
]),
|
|
187
|
+
setQueryData: React.useCallback((queryKey, updater, options)=>{
|
|
188
|
+
return queryClient.setQueryData(queryKey, updater, options);
|
|
189
|
+
}, [
|
|
190
|
+
queryClient
|
|
191
|
+
]),
|
|
192
|
+
getQueryData: React.useCallback((queryKey)=>{
|
|
193
|
+
return queryClient.getQueryData(queryKey);
|
|
194
|
+
}, [
|
|
195
|
+
queryClient
|
|
196
|
+
]),
|
|
197
|
+
setInfiniteQueryData: React.useCallback((queryKey, updater, options)=>{
|
|
198
|
+
return queryClient.setQueryData(queryKey, updater, options);
|
|
199
|
+
}, [
|
|
200
|
+
queryClient
|
|
201
|
+
]),
|
|
202
|
+
getInfiniteQueryData: React.useCallback((queryKey)=>{
|
|
203
|
+
return queryClient.getQueryData(queryKey);
|
|
204
|
+
}, [
|
|
205
|
+
queryClient
|
|
206
|
+
])
|
|
207
|
+
}
|
|
208
|
+
}, props.children);
|
|
209
|
+
};
|
|
210
|
+
function useContext() {
|
|
211
|
+
const context = React__default["default"].useContext(Context);
|
|
212
|
+
if (!context) {
|
|
213
|
+
throw new Error('Unable to find tRPC Context. Did you forget to wrap your App inside `withTRPC` HoC?');
|
|
214
|
+
}
|
|
215
|
+
return context;
|
|
216
|
+
}
|
|
217
|
+
/**
|
|
218
|
+
* Hack to make sure errors return `status`='error` when doing SSR
|
|
219
|
+
* @link https://github.com/trpc/trpc/pull/1645
|
|
220
|
+
*/ function useSSRQueryOptionsIfNeeded(queryKey, opts) {
|
|
221
|
+
const { queryClient , ssrState } = useContext();
|
|
222
|
+
return ssrState && ssrState !== 'mounted' && queryClient.getQueryCache().find({
|
|
223
|
+
queryKey
|
|
224
|
+
})?.state.status === 'error' ? {
|
|
225
|
+
retryOnMount: false,
|
|
226
|
+
...opts
|
|
227
|
+
} : opts;
|
|
228
|
+
}
|
|
229
|
+
function useQuery(path, input, opts) {
|
|
230
|
+
const context = useContext();
|
|
231
|
+
const { abortOnUnmount , client , ssrState , queryClient , prefetchQuery } = context;
|
|
232
|
+
const queryKey = utilsProxy.getQueryKeyInternal(path, input, 'query');
|
|
233
|
+
const defaultOpts = queryClient.getQueryDefaults(queryKey);
|
|
234
|
+
if (typeof window === 'undefined' && ssrState === 'prepass' && opts?.trpc?.ssr !== false && (opts?.enabled ?? defaultOpts?.enabled) !== false && !queryClient.getQueryCache().find({
|
|
235
|
+
queryKey
|
|
236
|
+
})) {
|
|
237
|
+
void prefetchQuery(queryKey, opts);
|
|
238
|
+
}
|
|
239
|
+
const ssrOpts = useSSRQueryOptionsIfNeeded(queryKey, {
|
|
240
|
+
...defaultOpts,
|
|
241
|
+
...opts
|
|
242
|
+
});
|
|
243
|
+
const shouldAbortOnUnmount = opts?.trpc?.abortOnUnmount ?? config?.abortOnUnmount ?? abortOnUnmount;
|
|
244
|
+
const hook = reactQuery.useQuery({
|
|
245
|
+
...ssrOpts,
|
|
246
|
+
queryKey: queryKey,
|
|
247
|
+
queryFn: (queryFunctionContext)=>{
|
|
248
|
+
const actualOpts = {
|
|
249
|
+
...ssrOpts,
|
|
250
|
+
trpc: {
|
|
251
|
+
...ssrOpts?.trpc,
|
|
252
|
+
...shouldAbortOnUnmount ? {
|
|
253
|
+
signal: queryFunctionContext.signal
|
|
254
|
+
} : {}
|
|
255
|
+
}
|
|
256
|
+
};
|
|
257
|
+
return client.query(...getClientArgs(queryKey, actualOpts));
|
|
258
|
+
}
|
|
259
|
+
}, queryClient);
|
|
260
|
+
hook.trpc = useHookResult({
|
|
261
|
+
path: path.join('.')
|
|
262
|
+
});
|
|
263
|
+
return hook;
|
|
264
|
+
}
|
|
265
|
+
function useSuspenseQuery(path, input, opts) {
|
|
266
|
+
const context = useContext();
|
|
267
|
+
const queryKey = utilsProxy.getQueryKeyInternal(path, input, 'query');
|
|
268
|
+
const shouldAbortOnUnmount = opts?.trpc?.abortOnUnmount ?? config?.abortOnUnmount ?? context.abortOnUnmount;
|
|
269
|
+
const hook = reactQuery.useSuspenseQuery({
|
|
270
|
+
...opts,
|
|
271
|
+
queryKey: queryKey,
|
|
272
|
+
queryFn: (queryFunctionContext)=>{
|
|
273
|
+
const actualOpts = {
|
|
274
|
+
trpc: {
|
|
275
|
+
...shouldAbortOnUnmount ? {
|
|
276
|
+
signal: queryFunctionContext.signal
|
|
277
|
+
} : {}
|
|
278
|
+
}
|
|
279
|
+
};
|
|
280
|
+
return context.client.query(...getClientArgs(queryKey, actualOpts));
|
|
281
|
+
}
|
|
282
|
+
}, context.queryClient);
|
|
283
|
+
hook.trpc = useHookResult({
|
|
284
|
+
path: path.join('.')
|
|
285
|
+
});
|
|
286
|
+
return [
|
|
287
|
+
hook.data,
|
|
288
|
+
hook
|
|
289
|
+
];
|
|
290
|
+
}
|
|
291
|
+
function useMutation(path, opts) {
|
|
292
|
+
const { client } = useContext();
|
|
293
|
+
const queryClient = reactQuery.useQueryClient();
|
|
294
|
+
const mutationKey = [
|
|
295
|
+
path
|
|
296
|
+
];
|
|
297
|
+
const defaultOpts = queryClient.getMutationDefaults(mutationKey);
|
|
298
|
+
const hook = reactQuery.useMutation({
|
|
299
|
+
...opts,
|
|
300
|
+
mutationKey: mutationKey,
|
|
301
|
+
mutationFn: (input)=>{
|
|
302
|
+
return client.mutation(...getClientArgs([
|
|
303
|
+
path,
|
|
304
|
+
{
|
|
305
|
+
input
|
|
306
|
+
}
|
|
307
|
+
], opts));
|
|
308
|
+
},
|
|
309
|
+
onSuccess (...args) {
|
|
310
|
+
const originalFn = ()=>opts?.onSuccess?.(...args) ?? defaultOpts?.onSuccess?.(...args);
|
|
311
|
+
return mutationSuccessOverride({
|
|
312
|
+
originalFn,
|
|
313
|
+
queryClient,
|
|
314
|
+
meta: opts?.meta ?? defaultOpts?.meta ?? {}
|
|
315
|
+
});
|
|
316
|
+
}
|
|
317
|
+
}, queryClient);
|
|
318
|
+
hook.trpc = useHookResult({
|
|
319
|
+
path: path.join('.')
|
|
320
|
+
});
|
|
321
|
+
return hook;
|
|
322
|
+
}
|
|
323
|
+
/* istanbul ignore next -- @preserve */ function useSubscription(path, input, opts) {
|
|
324
|
+
const enabled = opts?.enabled ?? true;
|
|
325
|
+
const queryKey = reactQuery.hashKey(utilsProxy.getQueryKeyInternal(path, input, 'any'));
|
|
326
|
+
const { client } = useContext();
|
|
327
|
+
const optsRef = React.useRef(opts);
|
|
328
|
+
optsRef.current = opts;
|
|
329
|
+
React.useEffect(()=>{
|
|
330
|
+
if (!enabled) {
|
|
331
|
+
return;
|
|
332
|
+
}
|
|
333
|
+
let isStopped = false;
|
|
334
|
+
const subscription = client.subscription(path.join('.'), input ?? undefined, {
|
|
335
|
+
onStarted: ()=>{
|
|
336
|
+
if (!isStopped) {
|
|
337
|
+
optsRef.current.onStarted?.();
|
|
338
|
+
}
|
|
339
|
+
},
|
|
340
|
+
onData: (data)=>{
|
|
341
|
+
if (!isStopped) {
|
|
342
|
+
opts.onData(data);
|
|
343
|
+
}
|
|
344
|
+
},
|
|
345
|
+
onError: (err)=>{
|
|
346
|
+
if (!isStopped) {
|
|
347
|
+
optsRef.current.onError?.(err);
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
});
|
|
351
|
+
return ()=>{
|
|
352
|
+
isStopped = true;
|
|
353
|
+
subscription.unsubscribe();
|
|
354
|
+
};
|
|
355
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
356
|
+
}, [
|
|
357
|
+
queryKey,
|
|
358
|
+
enabled
|
|
359
|
+
]);
|
|
360
|
+
}
|
|
361
|
+
function useInfiniteQuery(path, input, opts) {
|
|
362
|
+
const { client , ssrState , prefetchInfiniteQuery , queryClient , abortOnUnmount , } = useContext();
|
|
363
|
+
const queryKey = utilsProxy.getQueryKeyInternal(path, input, 'infinite');
|
|
364
|
+
const defaultOpts = queryClient.getQueryDefaults(queryKey);
|
|
365
|
+
if (typeof window === 'undefined' && ssrState === 'prepass' && opts?.trpc?.ssr !== false && (opts?.enabled ?? defaultOpts?.enabled) !== false && !queryClient.getQueryCache().find({
|
|
366
|
+
queryKey
|
|
367
|
+
})) {
|
|
368
|
+
void prefetchInfiniteQuery(queryKey, {
|
|
369
|
+
...defaultOpts,
|
|
370
|
+
...opts
|
|
371
|
+
});
|
|
372
|
+
}
|
|
373
|
+
const ssrOpts = useSSRQueryOptionsIfNeeded(queryKey, {
|
|
374
|
+
...defaultOpts,
|
|
375
|
+
...opts
|
|
376
|
+
});
|
|
377
|
+
// request option should take priority over global
|
|
378
|
+
const shouldAbortOnUnmount = opts?.trpc?.abortOnUnmount ?? abortOnUnmount;
|
|
379
|
+
const hook = reactQuery.useInfiniteQuery({
|
|
380
|
+
...ssrOpts,
|
|
381
|
+
initialPageParam: opts.initialCursor ?? null,
|
|
382
|
+
persister: opts.persister,
|
|
383
|
+
queryKey: queryKey,
|
|
384
|
+
queryFn: (queryFunctionContext)=>{
|
|
385
|
+
const actualOpts = {
|
|
386
|
+
...ssrOpts,
|
|
387
|
+
trpc: {
|
|
388
|
+
...ssrOpts?.trpc,
|
|
389
|
+
...shouldAbortOnUnmount ? {
|
|
390
|
+
signal: queryFunctionContext.signal
|
|
391
|
+
} : {}
|
|
392
|
+
}
|
|
393
|
+
};
|
|
394
|
+
return client.query(...getClientArgs(queryKey, actualOpts, queryFunctionContext.pageParam ?? opts.initialCursor));
|
|
395
|
+
}
|
|
396
|
+
}, queryClient);
|
|
397
|
+
hook.trpc = useHookResult({
|
|
398
|
+
path: path.join('.')
|
|
399
|
+
});
|
|
400
|
+
return hook;
|
|
401
|
+
}
|
|
402
|
+
function useSuspenseInfiniteQuery(path, input, opts) {
|
|
403
|
+
const context = useContext();
|
|
404
|
+
const queryKey = utilsProxy.getQueryKeyInternal(path, input, 'infinite');
|
|
405
|
+
const defaultOpts = context.queryClient.getQueryDefaults(queryKey);
|
|
406
|
+
const ssrOpts = useSSRQueryOptionsIfNeeded(queryKey, {
|
|
407
|
+
...defaultOpts,
|
|
408
|
+
...opts
|
|
409
|
+
});
|
|
410
|
+
// request option should take priority over global
|
|
411
|
+
const shouldAbortOnUnmount = opts?.trpc?.abortOnUnmount ?? context.abortOnUnmount;
|
|
412
|
+
const hook = reactQuery.useSuspenseInfiniteQuery({
|
|
413
|
+
...opts,
|
|
414
|
+
initialPageParam: opts.initialCursor ?? null,
|
|
415
|
+
queryKey,
|
|
416
|
+
queryFn: (queryFunctionContext)=>{
|
|
417
|
+
const actualOpts = {
|
|
418
|
+
...ssrOpts,
|
|
419
|
+
trpc: {
|
|
420
|
+
...ssrOpts?.trpc,
|
|
421
|
+
...shouldAbortOnUnmount ? {
|
|
422
|
+
signal: queryFunctionContext.signal
|
|
423
|
+
} : {}
|
|
424
|
+
}
|
|
425
|
+
};
|
|
426
|
+
return context.client.query(...getClientArgs(queryKey, actualOpts, queryFunctionContext.pageParam ?? opts.initialCursor));
|
|
427
|
+
}
|
|
428
|
+
}, context.queryClient);
|
|
429
|
+
hook.trpc = useHookResult({
|
|
430
|
+
path: path.join('.')
|
|
431
|
+
});
|
|
432
|
+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
433
|
+
return [
|
|
434
|
+
hook.data,
|
|
435
|
+
hook
|
|
436
|
+
];
|
|
437
|
+
}
|
|
438
|
+
const useQueries = (queriesCallback)=>{
|
|
439
|
+
const { ssrState , queryClient , prefetchQuery , client } = useContext();
|
|
440
|
+
const proxy = createUseQueries(client);
|
|
441
|
+
const queries = queriesCallback(proxy);
|
|
442
|
+
if (typeof window === 'undefined' && ssrState === 'prepass') {
|
|
443
|
+
for (const query of queries){
|
|
444
|
+
const queryOption = query;
|
|
445
|
+
if (queryOption.trpc?.ssr !== false && !queryClient.getQueryCache().find({
|
|
446
|
+
queryKey: queryOption.queryKey
|
|
447
|
+
})) {
|
|
448
|
+
void prefetchQuery(queryOption.queryKey, queryOption);
|
|
449
|
+
}
|
|
450
|
+
}
|
|
451
|
+
}
|
|
452
|
+
return reactQuery.useQueries({
|
|
453
|
+
queries: queries.map((query)=>({
|
|
454
|
+
...query,
|
|
455
|
+
queryKey: query.queryKey
|
|
456
|
+
}))
|
|
457
|
+
}, queryClient);
|
|
458
|
+
};
|
|
459
|
+
const useDehydratedState = (client, trpcState)=>{
|
|
460
|
+
const transformed = React.useMemo(()=>{
|
|
461
|
+
if (!trpcState) {
|
|
462
|
+
return trpcState;
|
|
463
|
+
}
|
|
464
|
+
return client.runtime.transformer.deserialize(trpcState);
|
|
465
|
+
}, [
|
|
466
|
+
trpcState,
|
|
467
|
+
client
|
|
468
|
+
]);
|
|
469
|
+
return transformed;
|
|
470
|
+
};
|
|
471
|
+
return {
|
|
472
|
+
Provider: TRPCProvider,
|
|
473
|
+
createClient,
|
|
474
|
+
useContext,
|
|
475
|
+
useUtils: useContext,
|
|
476
|
+
useQuery,
|
|
477
|
+
useSuspenseQuery,
|
|
478
|
+
useQueries,
|
|
479
|
+
useMutation,
|
|
480
|
+
useSubscription,
|
|
481
|
+
useDehydratedState,
|
|
482
|
+
useInfiniteQuery,
|
|
483
|
+
useSuspenseInfiniteQuery
|
|
484
|
+
};
|
|
485
|
+
}
|
|
486
|
+
|
|
487
|
+
exports.createReactDecoration = createReactDecoration;
|
|
488
|
+
exports.createRootHooks = createRootHooks;
|
|
489
|
+
exports.createUseQueries = createUseQueries;
|
|
490
|
+
exports.getClientArgs = getClientArgs;
|
|
@@ -1,77 +1,77 @@
|
|
|
1
|
-
import { InfiniteData } from '@tanstack/react-query';
|
|
2
1
|
import { TRPCClientErrorLike } from '@trpc/client';
|
|
3
|
-
import { AnyMutationProcedure, AnyProcedure, AnyQueryProcedure, AnyRouter, AnySubscriptionProcedure, inferProcedureInput, ProcedureRouterRecord, ProtectedIntersection } from '@trpc/server';
|
|
2
|
+
import { AnyMutationProcedure, AnyProcedure, AnyQueryProcedure, AnyRootConfig, AnyRouter, AnySubscriptionProcedure, inferProcedureInput, ProcedureRouterRecord, ProtectedIntersection } from '@trpc/server';
|
|
4
3
|
import { inferTransformedProcedureOutput, inferTransformedSubscriptionOutput } from '@trpc/server/shared';
|
|
5
|
-
import { QueryKey, QueryType } from './internals/getArrayQueryKey';
|
|
6
4
|
import { TRPCUseQueries } from './internals/useQueries';
|
|
7
|
-
import {
|
|
8
|
-
import { CreateReactQueryHooks } from './shared/hooks/
|
|
9
|
-
import { CreateClient, DefinedUseTRPCQueryOptions, DefinedUseTRPCQueryResult, TRPCProvider, UseDehydratedState, UseTRPCInfiniteQueryOptions, UseTRPCInfiniteQueryResult,
|
|
5
|
+
import { CreateReactUtils } from './shared';
|
|
6
|
+
import { CreateReactQueryHooks } from './shared/hooks/createHooksInternal';
|
|
7
|
+
import { CreateClient, DefinedUseTRPCQueryOptions, DefinedUseTRPCQueryResult, TRPCProvider, UseDehydratedState, UseTRPCInfiniteQueryOptions, UseTRPCInfiniteQueryResult, UseTRPCMutationOptions, UseTRPCMutationResult, UseTRPCQueryOptions, UseTRPCQueryResult, UseTRPCSubscriptionOptions, UseTRPCSuspenseInfiniteQueryOptions, UseTRPCSuspenseInfiniteQueryResult, UseTRPCSuspenseQueryOptions, UseTRPCSuspenseQueryResult } from './shared/hooks/types';
|
|
10
8
|
import { CreateTRPCReactOptions } from './shared/types';
|
|
11
9
|
/**
|
|
12
10
|
* @internal
|
|
13
11
|
*/
|
|
14
|
-
export interface ProcedureUseQuery<
|
|
15
|
-
<TQueryFnData extends inferTransformedProcedureOutput<TProcedure> = inferTransformedProcedureOutput<TProcedure>, TData = TQueryFnData>(input: inferProcedureInput<TProcedure>, opts: DefinedUseTRPCQueryOptions<
|
|
16
|
-
<TQueryFnData extends inferTransformedProcedureOutput<TProcedure> = inferTransformedProcedureOutput<TProcedure>, TData = TQueryFnData>(input: inferProcedureInput<TProcedure>, opts?: UseTRPCQueryOptions<
|
|
12
|
+
export interface ProcedureUseQuery<TConfig extends AnyRootConfig, TProcedure extends AnyProcedure> {
|
|
13
|
+
<TQueryFnData extends inferTransformedProcedureOutput<TConfig, TProcedure> = inferTransformedProcedureOutput<TConfig, TProcedure>, TData = TQueryFnData>(input: inferProcedureInput<TProcedure>, opts: DefinedUseTRPCQueryOptions<TQueryFnData, TData, TRPCClientErrorLike<TConfig>, inferTransformedProcedureOutput<TConfig, TProcedure>>): DefinedUseTRPCQueryResult<TData, TRPCClientErrorLike<TConfig>>;
|
|
14
|
+
<TQueryFnData extends inferTransformedProcedureOutput<TConfig, TProcedure> = inferTransformedProcedureOutput<TConfig, TProcedure>, TData = TQueryFnData>(input: inferProcedureInput<TProcedure>, opts?: UseTRPCQueryOptions<TQueryFnData, TData, TRPCClientErrorLike<TConfig>, inferTransformedProcedureOutput<TConfig, TProcedure>>): UseTRPCQueryResult<TData, TRPCClientErrorLike<TConfig>>;
|
|
17
15
|
}
|
|
18
16
|
/**
|
|
19
|
-
* @
|
|
17
|
+
* @remark `void` is here due to https://github.com/trpc/trpc/pull/4374
|
|
20
18
|
*/
|
|
21
|
-
|
|
19
|
+
type CursorInput = {
|
|
22
20
|
cursor?: any;
|
|
23
|
-
} | void
|
|
21
|
+
} | void;
|
|
22
|
+
/**
|
|
23
|
+
* @internal
|
|
24
|
+
*/
|
|
25
|
+
export type MaybeDecoratedInfiniteQuery<TProcedure extends AnyProcedure, TConfig extends AnyRootConfig> = inferProcedureInput<TProcedure> extends CursorInput ? {
|
|
24
26
|
/**
|
|
25
27
|
* @see https://trpc.io/docs/client/react/suspense#useinfinitesuspensequery
|
|
26
28
|
*/
|
|
27
|
-
useInfiniteQuery: (input: Omit<inferProcedureInput<TProcedure>, 'cursor'>, opts
|
|
29
|
+
useInfiniteQuery: (input: Omit<inferProcedureInput<TProcedure>, 'cursor'>, opts: UseTRPCInfiniteQueryOptions<inferProcedureInput<TProcedure>, inferTransformedProcedureOutput<TConfig, TProcedure>, TRPCClientErrorLike<TConfig>>) => UseTRPCInfiniteQueryResult<inferTransformedProcedureOutput<TConfig, TProcedure>, TRPCClientErrorLike<TConfig>, inferProcedureInput<TProcedure>>;
|
|
28
30
|
/**
|
|
29
31
|
* @see https://trpc.io/docs/client/react/suspense
|
|
30
32
|
*/
|
|
31
|
-
useSuspenseInfiniteQuery: (input: Omit<inferProcedureInput<TProcedure>, 'cursor'>, opts
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
* Method to extract the query key for a procedure
|
|
38
|
-
* @param type - defaults to `any`
|
|
39
|
-
* @see https://trpc.io/docs/client/react/getQueryKey
|
|
40
|
-
* @deprecated - import `getQueryKey` from `@trpc/react-query` instead
|
|
41
|
-
*/
|
|
42
|
-
getQueryKey: (input: inferProcedureInput<TProcedure>, type?: QueryType) => QueryKey;
|
|
33
|
+
useSuspenseInfiniteQuery: (input: Omit<inferProcedureInput<TProcedure>, 'cursor'>, opts: UseTRPCSuspenseInfiniteQueryOptions<inferProcedureInput<TProcedure>, inferTransformedProcedureOutput<TConfig, TProcedure>, TRPCClientErrorLike<TConfig>>) => UseTRPCSuspenseInfiniteQueryResult<inferTransformedProcedureOutput<TConfig, TProcedure>, TRPCClientErrorLike<TConfig>, inferProcedureInput<TProcedure>>;
|
|
34
|
+
} : object;
|
|
35
|
+
/**
|
|
36
|
+
* @internal
|
|
37
|
+
*/
|
|
38
|
+
export type DecoratedQueryMethods<TConfig extends AnyRootConfig, TProcedure extends AnyProcedure> = {
|
|
43
39
|
/**
|
|
44
40
|
* @see https://trpc.io/docs/client/react/useQuery
|
|
45
41
|
*/
|
|
46
|
-
useQuery: ProcedureUseQuery<
|
|
42
|
+
useQuery: ProcedureUseQuery<TConfig, TProcedure>;
|
|
47
43
|
/**
|
|
48
44
|
* @see https://trpc.io/docs/client/react/suspense#usesuspensequery
|
|
49
45
|
*/
|
|
50
|
-
useSuspenseQuery: <TQueryFnData extends inferTransformedProcedureOutput<TProcedure> = inferTransformedProcedureOutput<TProcedure>, TData = TQueryFnData>(input: inferProcedureInput<TProcedure>, opts?:
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
46
|
+
useSuspenseQuery: <TQueryFnData extends inferTransformedProcedureOutput<TConfig, TProcedure> = inferTransformedProcedureOutput<TConfig, TProcedure>, TData = TQueryFnData>(input: inferProcedureInput<TProcedure>, opts?: UseTRPCSuspenseQueryOptions<TQueryFnData, TData, TRPCClientErrorLike<TConfig>>) => UseTRPCSuspenseQueryResult<TData, TRPCClientErrorLike<TConfig>>;
|
|
47
|
+
};
|
|
48
|
+
/**
|
|
49
|
+
* @internal
|
|
50
|
+
*/
|
|
51
|
+
export type DecoratedQuery<TConfig extends AnyRootConfig, TProcedure extends AnyProcedure> = MaybeDecoratedInfiniteQuery<TProcedure, TConfig> & DecoratedQueryMethods<TConfig, TProcedure>;
|
|
52
|
+
/**
|
|
53
|
+
* @internal
|
|
54
|
+
*/
|
|
55
|
+
export interface DecoratedMutation<TConfig extends AnyRootConfig, TProcedure extends AnyProcedure> {
|
|
55
56
|
/**
|
|
56
57
|
* @see https://trpc.io/docs/client/react/useMutation
|
|
57
58
|
*/
|
|
58
|
-
useMutation: <TContext = unknown>(opts?: UseTRPCMutationOptions<inferProcedureInput<TProcedure>, TRPCClientErrorLike<
|
|
59
|
-
}
|
|
59
|
+
useMutation: <TContext = unknown>(opts?: UseTRPCMutationOptions<inferProcedureInput<TProcedure>, TRPCClientErrorLike<TConfig>, inferTransformedProcedureOutput<TConfig, TProcedure>, TContext>) => UseTRPCMutationResult<inferTransformedProcedureOutput<TConfig, TProcedure>, TRPCClientErrorLike<TConfig>, inferProcedureInput<TProcedure>, TContext>;
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* @internal
|
|
63
|
+
*/
|
|
64
|
+
export type DecorateProcedure<TConfig extends AnyRootConfig, TProcedure extends AnyProcedure, _TFlags> = TProcedure extends AnyQueryProcedure ? DecoratedQuery<TConfig, TProcedure> : TProcedure extends AnyMutationProcedure ? DecoratedMutation<TConfig, TProcedure> : TProcedure extends AnySubscriptionProcedure ? {
|
|
60
65
|
/**
|
|
61
66
|
* @see https://trpc.io/docs/subscriptions
|
|
62
67
|
*/
|
|
63
|
-
useSubscription: (input: inferProcedureInput<TProcedure>, opts?: UseTRPCSubscriptionOptions<inferTransformedSubscriptionOutput<TProcedure>, TRPCClientErrorLike<
|
|
68
|
+
useSubscription: (input: inferProcedureInput<TProcedure>, opts?: UseTRPCSubscriptionOptions<inferTransformedSubscriptionOutput<TConfig, TProcedure>, TRPCClientErrorLike<TConfig>>) => void;
|
|
64
69
|
} : never;
|
|
65
70
|
/**
|
|
66
71
|
* @internal
|
|
67
72
|
*/
|
|
68
|
-
export type DecoratedProcedureRecord<
|
|
69
|
-
[TKey in keyof TProcedures]: TProcedures[TKey] extends AnyRouter ? DecoratedProcedureRecord<TProcedures[TKey]['_def']['record'], TFlags,
|
|
70
|
-
/**
|
|
71
|
-
* @deprecated - import `getQueryKey` from `@trpc/react-query` instead
|
|
72
|
-
*/
|
|
73
|
-
getQueryKey: () => QueryKey;
|
|
74
|
-
} : TProcedures[TKey] extends AnyProcedure ? DecorateProcedure<TProcedures[TKey], TFlags, `${TPath}${TKey & string}`> : never;
|
|
73
|
+
export type DecoratedProcedureRecord<TConfig extends AnyRootConfig, TProcedures extends ProcedureRouterRecord, TFlags> = {
|
|
74
|
+
[TKey in keyof TProcedures]: TProcedures[TKey] extends AnyRouter ? DecoratedProcedureRecord<TConfig, TProcedures[TKey]['_def']['record'], TFlags> : TProcedures[TKey] extends AnyProcedure ? DecorateProcedure<TConfig, TProcedures[TKey], TFlags> : never;
|
|
75
75
|
};
|
|
76
76
|
/**
|
|
77
77
|
* @internal
|
|
@@ -82,20 +82,21 @@ export type CreateTRPCReactBase<TRouter extends AnyRouter, TSSRContext> = {
|
|
|
82
82
|
*
|
|
83
83
|
* @see https://trpc.io/docs/client/react/useUtils
|
|
84
84
|
*/
|
|
85
|
-
useContext():
|
|
85
|
+
useContext(): CreateReactUtils<TRouter, TSSRContext>;
|
|
86
86
|
/**
|
|
87
87
|
* @see https://trpc.io/docs/client/react/useUtils
|
|
88
88
|
*/
|
|
89
|
-
useUtils():
|
|
89
|
+
useUtils(): CreateReactUtils<TRouter, TSSRContext>;
|
|
90
90
|
Provider: TRPCProvider<TRouter, TSSRContext>;
|
|
91
91
|
createClient: CreateClient<TRouter>;
|
|
92
92
|
useQueries: TRPCUseQueries<TRouter>;
|
|
93
93
|
useDehydratedState: UseDehydratedState<TRouter>;
|
|
94
94
|
};
|
|
95
|
-
export type CreateTRPCReact<TRouter extends AnyRouter, TSSRContext, TFlags> = ProtectedIntersection<CreateTRPCReactBase<TRouter, TSSRContext>, DecoratedProcedureRecord<TRouter['_def']['record'], TFlags>>;
|
|
95
|
+
export type CreateTRPCReact<TRouter extends AnyRouter, TSSRContext, TFlags> = ProtectedIntersection<CreateTRPCReactBase<TRouter, TSSRContext>, DecoratedProcedureRecord<TRouter['_def']['_config'], TRouter['_def']['record'], TFlags>>;
|
|
96
96
|
/**
|
|
97
97
|
* @internal
|
|
98
98
|
*/
|
|
99
|
-
export declare function
|
|
99
|
+
export declare function createHooksInternal<TRouter extends AnyRouter, TSSRContext = unknown, TFlags = null>(trpc: CreateReactQueryHooks<TRouter, TSSRContext>): ProtectedIntersection<CreateTRPCReactBase<TRouter, TSSRContext>, DecoratedProcedureRecord<TRouter["_def"]["_config"], TRouter["_def"]["record"], TFlags>>;
|
|
100
100
|
export declare function createTRPCReact<TRouter extends AnyRouter, TSSRContext = unknown, TFlags = null>(opts?: CreateTRPCReactOptions<TRouter>): CreateTRPCReact<TRouter, TSSRContext, TFlags>;
|
|
101
|
+
export {};
|
|
101
102
|
//# sourceMappingURL=createTRPCReact.d.ts.map
|