@trpc/react-query 11.0.0-alpha-tmp-subscription-connection-state.488 → 11.0.0-alpha-tmp-12-06-react.665
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 +59 -76
- package/dist/createTRPCReact.d.ts +47 -17
- package/dist/createTRPCReact.d.ts.map +1 -1
- package/dist/internals/context.d.ts +43 -21
- package/dist/internals/context.d.ts.map +1 -1
- package/dist/internals/getQueryKey.d.ts +2 -2
- package/dist/internals/getQueryKey.js +3 -3
- package/dist/internals/getQueryKey.mjs +3 -3
- package/dist/internals/trpcResult.d.ts +18 -0
- package/dist/internals/trpcResult.d.ts.map +1 -0
- package/dist/internals/trpcResult.js +63 -0
- package/dist/internals/trpcResult.mjs +40 -0
- package/dist/internals/useQueries.d.ts +1 -1
- package/dist/internals/useQueries.d.ts.map +1 -1
- package/dist/rsc.d.ts +1 -1
- package/dist/rsc.d.ts.map +1 -1
- package/dist/rsc.js +4 -4
- package/dist/rsc.mjs +4 -4
- package/dist/server/ssgProxy.d.ts +7 -24
- package/dist/server/ssgProxy.d.ts.map +1 -1
- package/dist/server/ssgProxy.js +21 -4
- package/dist/server/ssgProxy.mjs +21 -4
- package/dist/shared/hooks/createHooksInternal.d.ts +4 -2
- package/dist/shared/hooks/createHooksInternal.d.ts.map +1 -1
- package/dist/shared/hooks/createHooksInternal.js +160 -93
- package/dist/shared/hooks/createHooksInternal.mjs +156 -89
- package/dist/shared/hooks/types.d.ts +38 -159
- package/dist/shared/hooks/types.d.ts.map +1 -1
- package/dist/shared/index.js +0 -7
- package/dist/shared/index.mjs +0 -1
- package/dist/shared/polymorphism/mutationLike.d.ts.map +1 -1
- package/dist/shared/polymorphism/queryLike.d.ts.map +1 -1
- package/dist/shared/polymorphism/routerLike.d.ts.map +1 -1
- package/dist/shared/proxy/decorationProxy.js +1 -1
- package/dist/shared/proxy/decorationProxy.mjs +1 -1
- package/dist/shared/proxy/useQueriesProxy.d.ts.map +1 -1
- package/dist/shared/proxy/utilsProxy.d.ts +45 -20
- package/dist/shared/proxy/utilsProxy.d.ts.map +1 -1
- package/dist/shared/proxy/utilsProxy.js +4 -0
- package/dist/shared/proxy/utilsProxy.mjs +4 -0
- package/dist/shared/types.d.ts +60 -2
- package/dist/shared/types.d.ts.map +1 -1
- package/dist/utils/createUtilityFunctions.d.ts +2 -2
- package/dist/utils/createUtilityFunctions.d.ts.map +1 -1
- package/dist/utils/createUtilityFunctions.js +69 -3
- package/dist/utils/createUtilityFunctions.mjs +69 -3
- package/dist/utils/inferReactQueryProcedure.d.ts +2 -2
- package/dist/utils/inferReactQueryProcedure.d.ts.map +1 -1
- package/package.json +22 -19
- package/src/createTRPCReact.tsx +276 -65
- package/src/internals/context.tsx +105 -24
- package/src/internals/getQueryKey.ts +2 -2
- package/src/internals/trpcResult.ts +55 -0
- package/src/internals/useQueries.ts +36 -31
- package/src/rsc.tsx +6 -6
- package/src/server/ssgProxy.ts +23 -66
- package/src/shared/hooks/createHooksInternal.tsx +222 -151
- package/src/shared/hooks/types.ts +64 -348
- package/src/shared/polymorphism/mutationLike.ts +8 -6
- package/src/shared/polymorphism/queryLike.ts +12 -14
- package/src/shared/polymorphism/routerLike.ts +4 -4
- package/src/shared/proxy/useQueriesProxy.ts +4 -4
- package/src/shared/proxy/utilsProxy.ts +153 -25
- package/src/shared/types.ts +229 -1
- package/src/utils/createUtilityFunctions.ts +90 -2
- package/src/utils/inferReactQueryProcedure.ts +8 -6
- package/dist/internals/useHookResult.d.ts +0 -8
- package/dist/internals/useHookResult.d.ts.map +0 -1
- package/dist/internals/useHookResult.js +0 -35
- package/dist/internals/useHookResult.mjs +0 -14
- package/dist/shared/hooks/types.js +0 -133
- package/dist/shared/hooks/types.mjs +0 -126
- package/src/internals/useHookResult.ts +0 -17
|
@@ -1,14 +1,13 @@
|
|
|
1
|
-
import { useQuery,
|
|
1
|
+
import { useQuery, usePrefetchQuery, useSuspenseQuery, useMutation, skipToken, hashKey, useInfiniteQuery, usePrefetchInfiniteQuery, useSuspenseInfiniteQuery, useQueries, useSuspenseQueries } from '@tanstack/react-query';
|
|
2
2
|
import { createTRPCUntypedClient } from '@trpc/client';
|
|
3
3
|
import { isAsyncIterable } from '@trpc/server/unstable-core-do-not-import';
|
|
4
4
|
import * as React from 'react';
|
|
5
5
|
import { TRPCContext } from '../../internals/context.mjs';
|
|
6
6
|
import { getClientArgs } from '../../internals/getClientArgs.mjs';
|
|
7
7
|
import { getQueryKeyInternal, getMutationKeyInternal } from '../../internals/getQueryKey.mjs';
|
|
8
|
-
import { useHookResult } from '../../internals/
|
|
8
|
+
import { buildQueryFromAsyncIterable, useHookResult } from '../../internals/trpcResult.mjs';
|
|
9
9
|
import { createUtilityFunctions } from '../../utils/createUtilityFunctions.mjs';
|
|
10
10
|
import { createUseQueries } from '../proxy/useQueriesProxy.mjs';
|
|
11
|
-
import { getStartingResult, getIdleResult, getPendingResult, getConnectingResult, getErrorResult } from './types.mjs';
|
|
12
11
|
|
|
13
12
|
const trackResult = (result, onTrackResult)=>{
|
|
14
13
|
const trackedResult = new Proxy(result, {
|
|
@@ -28,7 +27,7 @@ const trackResult = (result, onTrackResult)=>{
|
|
|
28
27
|
return createTRPCUntypedClient(opts);
|
|
29
28
|
};
|
|
30
29
|
const TRPCProvider = (props)=>{
|
|
31
|
-
const { abortOnUnmount =false
|
|
30
|
+
const { abortOnUnmount = false, client, queryClient, ssrContext } = props;
|
|
32
31
|
const [ssrState, setSSRState] = React.useState(props.ssrState ?? false);
|
|
33
32
|
const fns = React.useMemo(()=>createUtilityFunctions({
|
|
34
33
|
client,
|
|
@@ -70,9 +69,9 @@ const trackResult = (result, onTrackResult)=>{
|
|
|
70
69
|
}
|
|
71
70
|
/**
|
|
72
71
|
* Hack to make sure errors return `status`='error` when doing SSR
|
|
73
|
-
* @
|
|
72
|
+
* @see https://github.com/trpc/trpc/pull/1645
|
|
74
73
|
*/ function useSSRQueryOptionsIfNeeded(queryKey, opts) {
|
|
75
|
-
const { queryClient
|
|
74
|
+
const { queryClient, ssrState } = useContext();
|
|
76
75
|
return ssrState && ssrState !== 'mounted' && queryClient.getQueryCache().find({
|
|
77
76
|
queryKey
|
|
78
77
|
})?.state.status === 'error' ? {
|
|
@@ -82,7 +81,7 @@ const trackResult = (result, onTrackResult)=>{
|
|
|
82
81
|
}
|
|
83
82
|
function useQuery$1(path, input, opts) {
|
|
84
83
|
const context = useContext();
|
|
85
|
-
const { abortOnUnmount
|
|
84
|
+
const { abortOnUnmount, client, ssrState, queryClient, prefetchQuery } = context;
|
|
86
85
|
const queryKey = getQueryKeyInternal(path, input, 'query');
|
|
87
86
|
const defaultOpts = queryClient.getQueryDefaults(queryKey);
|
|
88
87
|
const isInputSkipToken = input === skipToken;
|
|
@@ -113,24 +112,7 @@ const trackResult = (result, onTrackResult)=>{
|
|
|
113
112
|
};
|
|
114
113
|
const result = await client.query(...getClientArgs(queryKey, actualOpts));
|
|
115
114
|
if (isAsyncIterable(result)) {
|
|
116
|
-
|
|
117
|
-
const query = queryCache.build(queryFunctionContext.queryKey, {
|
|
118
|
-
queryKey
|
|
119
|
-
});
|
|
120
|
-
query.setState({
|
|
121
|
-
data: [],
|
|
122
|
-
status: 'success'
|
|
123
|
-
});
|
|
124
|
-
const aggregate = [];
|
|
125
|
-
for await (const value of result){
|
|
126
|
-
aggregate.push(value);
|
|
127
|
-
query.setState({
|
|
128
|
-
data: [
|
|
129
|
-
...aggregate
|
|
130
|
-
]
|
|
131
|
-
});
|
|
132
|
-
}
|
|
133
|
-
return aggregate;
|
|
115
|
+
return buildQueryFromAsyncIterable(result, queryClient, queryKey);
|
|
134
116
|
}
|
|
135
117
|
return result;
|
|
136
118
|
}
|
|
@@ -140,6 +122,27 @@ const trackResult = (result, onTrackResult)=>{
|
|
|
140
122
|
});
|
|
141
123
|
return hook;
|
|
142
124
|
}
|
|
125
|
+
function usePrefetchQuery$1(path, input, opts) {
|
|
126
|
+
const context = useContext();
|
|
127
|
+
const queryKey = getQueryKeyInternal(path, input, 'query');
|
|
128
|
+
const isInputSkipToken = input === skipToken;
|
|
129
|
+
const shouldAbortOnUnmount = opts?.trpc?.abortOnUnmount ?? config?.abortOnUnmount ?? context.abortOnUnmount;
|
|
130
|
+
usePrefetchQuery({
|
|
131
|
+
...opts,
|
|
132
|
+
queryKey: queryKey,
|
|
133
|
+
queryFn: isInputSkipToken ? input : (queryFunctionContext)=>{
|
|
134
|
+
const actualOpts = {
|
|
135
|
+
trpc: {
|
|
136
|
+
...opts?.trpc,
|
|
137
|
+
...shouldAbortOnUnmount ? {
|
|
138
|
+
signal: queryFunctionContext.signal
|
|
139
|
+
} : {}
|
|
140
|
+
}
|
|
141
|
+
};
|
|
142
|
+
return context.client.query(...getClientArgs(queryKey, actualOpts));
|
|
143
|
+
}
|
|
144
|
+
});
|
|
145
|
+
}
|
|
143
146
|
function useSuspenseQuery$1(path, input, opts) {
|
|
144
147
|
const context = useContext();
|
|
145
148
|
const queryKey = getQueryKeyInternal(path, input, 'query');
|
|
@@ -149,7 +152,9 @@ const trackResult = (result, onTrackResult)=>{
|
|
|
149
152
|
queryKey: queryKey,
|
|
150
153
|
queryFn: (queryFunctionContext)=>{
|
|
151
154
|
const actualOpts = {
|
|
155
|
+
...opts,
|
|
152
156
|
trpc: {
|
|
157
|
+
...opts?.trpc,
|
|
153
158
|
...shouldAbortOnUnmount ? {
|
|
154
159
|
signal: queryFunctionContext.signal
|
|
155
160
|
} : {
|
|
@@ -169,8 +174,7 @@ const trackResult = (result, onTrackResult)=>{
|
|
|
169
174
|
];
|
|
170
175
|
}
|
|
171
176
|
function useMutation$1(path, opts) {
|
|
172
|
-
const { client
|
|
173
|
-
const queryClient = useQueryClient();
|
|
177
|
+
const { client, queryClient } = useContext();
|
|
174
178
|
const mutationKey = getMutationKeyInternal(path);
|
|
175
179
|
const defaultOpts = queryClient.defaultMutationOptions(queryClient.getMutationDefaults(mutationKey));
|
|
176
180
|
const hook = useMutation({
|
|
@@ -201,37 +205,18 @@ const trackResult = (result, onTrackResult)=>{
|
|
|
201
205
|
/* istanbul ignore next -- @preserve */ function useSubscription(path, input, opts) {
|
|
202
206
|
const enabled = opts?.enabled ?? input !== skipToken;
|
|
203
207
|
const queryKey = hashKey(getQueryKeyInternal(path, input, 'any'));
|
|
208
|
+
const { client } = useContext();
|
|
209
|
+
const optsRef = React.useRef(opts);
|
|
210
|
+
optsRef.current = opts;
|
|
204
211
|
const trackedProps = React.useRef(new Set([]));
|
|
205
212
|
const addTrackedProp = React.useCallback((key)=>{
|
|
206
213
|
trackedProps.current.add(key);
|
|
207
214
|
}, []);
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
const updateSubscriptionState = React.useCallback((opts)=>{
|
|
214
|
-
const oldResult = currentResult.current;
|
|
215
|
-
const newResult = typeof opts === 'function' ? opts(currentResult.current) : opts;
|
|
216
|
-
currentResult.current = newResult;
|
|
217
|
-
let shouldUpdate = false;
|
|
218
|
-
for (const key of trackedProps.current){
|
|
219
|
-
if (oldResult[key] !== newResult[key]) {
|
|
220
|
-
shouldUpdate = true;
|
|
221
|
-
break;
|
|
222
|
-
}
|
|
223
|
-
}
|
|
224
|
-
if (shouldUpdate) {
|
|
225
|
-
setSubscriptionState(trackResult(newResult, addTrackedProp));
|
|
226
|
-
return;
|
|
227
|
-
}
|
|
228
|
-
}, [
|
|
229
|
-
addTrackedProp
|
|
230
|
-
]);
|
|
231
|
-
const { client } = useContext();
|
|
232
|
-
const optsRef = React.useRef(opts);
|
|
233
|
-
optsRef.current = opts;
|
|
234
|
-
React.useEffect(()=>{
|
|
215
|
+
const currentSubscriptionRef = React.useRef(null);
|
|
216
|
+
const reset = React.useCallback(()=>{
|
|
217
|
+
// unsubscribe from the previous subscription
|
|
218
|
+
currentSubscriptionRef.current?.();
|
|
219
|
+
updateState(getInitialState);
|
|
235
220
|
if (!enabled) {
|
|
236
221
|
return;
|
|
237
222
|
}
|
|
@@ -240,60 +225,107 @@ const trackResult = (result, onTrackResult)=>{
|
|
|
240
225
|
onStarted: ()=>{
|
|
241
226
|
if (!isStopped) {
|
|
242
227
|
optsRef.current.onStarted?.();
|
|
228
|
+
updateState((prev)=>({
|
|
229
|
+
...prev,
|
|
230
|
+
status: 'pending',
|
|
231
|
+
error: null
|
|
232
|
+
}));
|
|
243
233
|
}
|
|
244
234
|
},
|
|
245
235
|
onData: (data)=>{
|
|
246
236
|
if (!isStopped) {
|
|
247
|
-
optsRef.current.onData(data);
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
237
|
+
optsRef.current.onData?.(data);
|
|
238
|
+
updateState((prev)=>({
|
|
239
|
+
...prev,
|
|
240
|
+
status: 'pending',
|
|
241
|
+
data,
|
|
242
|
+
error: null
|
|
243
|
+
}));
|
|
254
244
|
}
|
|
255
245
|
},
|
|
256
|
-
onError: (
|
|
246
|
+
onError: (error)=>{
|
|
257
247
|
if (!isStopped) {
|
|
258
|
-
optsRef.current.onError?.(
|
|
248
|
+
optsRef.current.onError?.(error);
|
|
249
|
+
updateState((prev)=>({
|
|
250
|
+
...prev,
|
|
251
|
+
status: 'error',
|
|
252
|
+
error
|
|
253
|
+
}));
|
|
259
254
|
}
|
|
260
255
|
},
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
}
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
}
|
|
272
|
-
if (state.state === 'pending') {
|
|
273
|
-
updateSubscriptionState((prev)=>getPendingResult(prev));
|
|
274
|
-
}
|
|
275
|
-
if (state.state === 'error') {
|
|
276
|
-
updateSubscriptionState((prev)=>{
|
|
277
|
-
return getErrorResult(prev, state.data);
|
|
278
|
-
});
|
|
279
|
-
}
|
|
256
|
+
onConnectionStateChange: (result)=>{
|
|
257
|
+
const delta = {
|
|
258
|
+
status: result.state,
|
|
259
|
+
error: result.error
|
|
260
|
+
};
|
|
261
|
+
updateState((prev)=>{
|
|
262
|
+
return {
|
|
263
|
+
...prev,
|
|
264
|
+
...delta
|
|
265
|
+
};
|
|
266
|
+
});
|
|
280
267
|
}
|
|
281
268
|
});
|
|
282
|
-
|
|
283
|
-
return ()=>{
|
|
269
|
+
currentSubscriptionRef.current = ()=>{
|
|
284
270
|
isStopped = true;
|
|
285
271
|
subscription.unsubscribe();
|
|
286
|
-
updateSubscriptionState(getIdleResult());
|
|
287
272
|
};
|
|
288
273
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
289
274
|
}, [
|
|
290
275
|
queryKey,
|
|
291
276
|
enabled
|
|
292
277
|
]);
|
|
293
|
-
|
|
278
|
+
const getInitialState = React.useCallback(()=>{
|
|
279
|
+
return enabled ? {
|
|
280
|
+
data: undefined,
|
|
281
|
+
error: null,
|
|
282
|
+
status: 'connecting',
|
|
283
|
+
reset
|
|
284
|
+
} : {
|
|
285
|
+
data: undefined,
|
|
286
|
+
error: null,
|
|
287
|
+
status: 'idle',
|
|
288
|
+
reset
|
|
289
|
+
};
|
|
290
|
+
}, [
|
|
291
|
+
enabled,
|
|
292
|
+
reset
|
|
293
|
+
]);
|
|
294
|
+
const resultRef = React.useRef(getInitialState());
|
|
295
|
+
const [state, setState] = React.useState(trackResult(resultRef.current, addTrackedProp));
|
|
296
|
+
state.reset = reset;
|
|
297
|
+
const updateState = React.useCallback((callback)=>{
|
|
298
|
+
const prev = resultRef.current;
|
|
299
|
+
const next = resultRef.current = callback(prev);
|
|
300
|
+
let shouldUpdate = false;
|
|
301
|
+
for (const key of trackedProps.current){
|
|
302
|
+
if (prev[key] !== next[key]) {
|
|
303
|
+
shouldUpdate = true;
|
|
304
|
+
break;
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
if (shouldUpdate) {
|
|
308
|
+
setState(trackResult(next, addTrackedProp));
|
|
309
|
+
}
|
|
310
|
+
}, [
|
|
311
|
+
addTrackedProp
|
|
312
|
+
]);
|
|
313
|
+
React.useEffect(()=>{
|
|
314
|
+
if (!enabled) {
|
|
315
|
+
return;
|
|
316
|
+
}
|
|
317
|
+
reset();
|
|
318
|
+
return ()=>{
|
|
319
|
+
currentSubscriptionRef.current?.();
|
|
320
|
+
};
|
|
321
|
+
}, [
|
|
322
|
+
reset,
|
|
323
|
+
enabled
|
|
324
|
+
]);
|
|
325
|
+
return state;
|
|
294
326
|
}
|
|
295
327
|
function useInfiniteQuery$1(path, input, opts) {
|
|
296
|
-
const { client
|
|
328
|
+
const { client, ssrState, prefetchInfiniteQuery, queryClient, abortOnUnmount } = useContext();
|
|
297
329
|
const queryKey = getQueryKeyInternal(path, input, 'infinite');
|
|
298
330
|
const defaultOpts = queryClient.getQueryDefaults(queryKey);
|
|
299
331
|
const isInputSkipToken = input === skipToken;
|
|
@@ -339,6 +371,38 @@ const trackResult = (result, onTrackResult)=>{
|
|
|
339
371
|
});
|
|
340
372
|
return hook;
|
|
341
373
|
}
|
|
374
|
+
function usePrefetchInfiniteQuery$1(path, input, opts) {
|
|
375
|
+
const context = useContext();
|
|
376
|
+
const queryKey = getQueryKeyInternal(path, input, 'infinite');
|
|
377
|
+
const defaultOpts = context.queryClient.getQueryDefaults(queryKey);
|
|
378
|
+
const isInputSkipToken = input === skipToken;
|
|
379
|
+
const ssrOpts = useSSRQueryOptionsIfNeeded(queryKey, {
|
|
380
|
+
...defaultOpts,
|
|
381
|
+
...opts
|
|
382
|
+
});
|
|
383
|
+
// request option should take priority over global
|
|
384
|
+
const shouldAbortOnUnmount = opts?.trpc?.abortOnUnmount ?? context.abortOnUnmount;
|
|
385
|
+
usePrefetchInfiniteQuery({
|
|
386
|
+
...opts,
|
|
387
|
+
initialPageParam: opts.initialCursor ?? null,
|
|
388
|
+
queryKey,
|
|
389
|
+
queryFn: isInputSkipToken ? input : (queryFunctionContext)=>{
|
|
390
|
+
const actualOpts = {
|
|
391
|
+
...ssrOpts,
|
|
392
|
+
trpc: {
|
|
393
|
+
...ssrOpts?.trpc,
|
|
394
|
+
...shouldAbortOnUnmount ? {
|
|
395
|
+
signal: queryFunctionContext.signal
|
|
396
|
+
} : {}
|
|
397
|
+
}
|
|
398
|
+
};
|
|
399
|
+
return context.client.query(...getClientArgs(queryKey, actualOpts, {
|
|
400
|
+
pageParam: queryFunctionContext.pageParam ?? opts.initialCursor,
|
|
401
|
+
direction: queryFunctionContext.direction
|
|
402
|
+
}));
|
|
403
|
+
}
|
|
404
|
+
});
|
|
405
|
+
}
|
|
342
406
|
function useSuspenseInfiniteQuery$1(path, input, opts) {
|
|
343
407
|
const context = useContext();
|
|
344
408
|
const queryKey = getQueryKeyInternal(path, input, 'infinite');
|
|
@@ -379,7 +443,7 @@ const trackResult = (result, onTrackResult)=>{
|
|
|
379
443
|
];
|
|
380
444
|
}
|
|
381
445
|
const useQueries$1 = (queriesCallback)=>{
|
|
382
|
-
const { ssrState
|
|
446
|
+
const { ssrState, queryClient, prefetchQuery, client } = useContext();
|
|
383
447
|
const proxy = createUseQueries(client);
|
|
384
448
|
const queries = queriesCallback(proxy);
|
|
385
449
|
if (typeof window === 'undefined' && ssrState === 'prepass') {
|
|
@@ -400,12 +464,13 @@ const trackResult = (result, onTrackResult)=>{
|
|
|
400
464
|
}, queryClient);
|
|
401
465
|
};
|
|
402
466
|
const useSuspenseQueries$1 = (queriesCallback)=>{
|
|
403
|
-
const { queryClient
|
|
467
|
+
const { queryClient, client } = useContext();
|
|
404
468
|
const proxy = createUseQueries(client);
|
|
405
469
|
const queries = queriesCallback(proxy);
|
|
406
470
|
const hook = useSuspenseQueries({
|
|
407
471
|
queries: queries.map((query)=>({
|
|
408
472
|
...query,
|
|
473
|
+
queryFn: query.queryFn,
|
|
409
474
|
queryKey: query.queryKey
|
|
410
475
|
}))
|
|
411
476
|
}, queryClient);
|
|
@@ -420,12 +485,14 @@ const trackResult = (result, onTrackResult)=>{
|
|
|
420
485
|
useContext,
|
|
421
486
|
useUtils: useContext,
|
|
422
487
|
useQuery: useQuery$1,
|
|
488
|
+
usePrefetchQuery: usePrefetchQuery$1,
|
|
423
489
|
useSuspenseQuery: useSuspenseQuery$1,
|
|
424
490
|
useQueries: useQueries$1,
|
|
425
491
|
useSuspenseQueries: useSuspenseQueries$1,
|
|
426
492
|
useMutation: useMutation$1,
|
|
427
493
|
useSubscription,
|
|
428
494
|
useInfiniteQuery: useInfiniteQuery$1,
|
|
495
|
+
usePrefetchInfiniteQuery: usePrefetchInfiniteQuery$1,
|
|
429
496
|
useSuspenseInfiniteQuery: useSuspenseInfiniteQuery$1
|
|
430
497
|
};
|
|
431
498
|
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import type { DefinedUseQueryResult, DehydratedState, InfiniteData, InfiniteQueryObserverSuccessResult, InitialDataFunction, QueryObserverSuccessResult, QueryOptions, UseBaseQueryOptions, UseInfiniteQueryOptions, UseInfiniteQueryResult, UseMutationOptions, UseMutationResult, UseQueryResult, UseSuspenseInfiniteQueryOptions, UseSuspenseInfiniteQueryResult, UseSuspenseQueryOptions, UseSuspenseQueryResult } from '@tanstack/react-query';
|
|
2
|
-
import type {
|
|
1
|
+
import type { DefinedUseQueryResult, DehydratedState, FetchInfiniteQueryOptions, FetchQueryOptions, InfiniteData, InfiniteQueryObserverSuccessResult, InitialDataFunction, QueryObserverSuccessResult, QueryOptions, UseBaseQueryOptions, UseInfiniteQueryOptions, UseInfiniteQueryResult, UseMutationOptions, UseMutationResult, UseQueryResult, UseSuspenseInfiniteQueryOptions, UseSuspenseInfiniteQueryResult, UseSuspenseQueryOptions, UseSuspenseQueryResult } from '@tanstack/react-query';
|
|
2
|
+
import type { CreateTRPCClientOptions, TRPCRequestOptions, TRPCUntypedClient } from '@trpc/client';
|
|
3
3
|
import type { AnyRouter, DistributiveOmit } from '@trpc/server/unstable-core-do-not-import';
|
|
4
|
-
import type { ReactNode } from 'react';
|
|
4
|
+
import type { JSX, ReactNode } from 'react';
|
|
5
5
|
import type { TRPCContextProps } from '../../internals/context';
|
|
6
6
|
import type { TRPCQueryKey } from '../../internals/getQueryKey';
|
|
7
7
|
export type OutputWithCursor<TData, TCursor = any> = {
|
|
@@ -28,6 +28,8 @@ export interface UseTRPCQueryOptions<TOutput, TData, TError, TQueryOptsData = TO
|
|
|
28
28
|
}
|
|
29
29
|
export interface UseTRPCSuspenseQueryOptions<TOutput, TData, TError> extends DistributiveOmit<UseSuspenseQueryOptions<TOutput, TError, TData, any>, 'queryKey'>, TRPCUseQueryBaseOptions {
|
|
30
30
|
}
|
|
31
|
+
export interface UseTRPCPrefetchQueryOptions<TOutput, TData, TError> extends DistributiveOmit<FetchQueryOptions<TOutput, TError, TData, any>, 'queryKey'>, TRPCUseQueryBaseOptions {
|
|
32
|
+
}
|
|
31
33
|
/** @internal **/
|
|
32
34
|
export interface DefinedUseTRPCQueryOptions<TOutput, TData, TError, TQueryOptsData = TOutput> extends DistributiveOmit<UseTRPCQueryOptions<TOutput, TData, TError, TQueryOptsData>, 'queryKey'> {
|
|
33
35
|
initialData: InitialDataFunction<TQueryOptsData> | TQueryOptsData;
|
|
@@ -41,193 +43,71 @@ export type ExtractCursorType<TInput> = TInput extends {
|
|
|
41
43
|
export interface UseTRPCInfiniteQueryOptions<TInput, TOutput, TError> extends DistributiveOmit<UseInfiniteQueryOptions<TOutput, TError, TOutput, TOutput, any, ExtractCursorType<TInput>>, 'queryKey' | 'initialPageParam'>, TRPCUseQueryBaseOptions {
|
|
42
44
|
initialCursor?: ExtractCursorType<TInput>;
|
|
43
45
|
}
|
|
46
|
+
export type UseTRPCPrefetchInfiniteQueryOptions<TInput, TOutput, TError> = DistributiveOmit<FetchInfiniteQueryOptions<TOutput, TError, TOutput, any, ExtractCursorType<TInput>>, 'queryKey' | 'initialPageParam'> & TRPCUseQueryBaseOptions & {
|
|
47
|
+
initialCursor?: ExtractCursorType<TInput>;
|
|
48
|
+
};
|
|
44
49
|
export interface UseTRPCSuspenseInfiniteQueryOptions<TInput, TOutput, TError> extends DistributiveOmit<UseSuspenseInfiniteQueryOptions<TOutput, TError, TOutput, TOutput, any, ExtractCursorType<TInput>>, 'queryKey' | 'initialPageParam'>, TRPCUseQueryBaseOptions {
|
|
45
50
|
initialCursor?: ExtractCursorType<TInput>;
|
|
46
51
|
}
|
|
47
52
|
export interface UseTRPCMutationOptions<TInput, TError, TOutput, TContext = unknown> extends UseMutationOptions<TOutput, TError, TInput, TContext>, TRPCUseQueryBaseOptions {
|
|
48
53
|
}
|
|
49
54
|
export interface UseTRPCSubscriptionOptions<TOutput, TError> {
|
|
50
|
-
enabled?: boolean;
|
|
51
|
-
onStarted?: () => void;
|
|
52
55
|
/**
|
|
53
|
-
* @deprecated
|
|
56
|
+
* @deprecated
|
|
57
|
+
* use a `skipToken` from `@tanstack/react-query` instead
|
|
58
|
+
* this will be removed in v12
|
|
54
59
|
*/
|
|
55
|
-
|
|
56
|
-
onData: (data: TOutput) => void;
|
|
57
|
-
onStateChange?: (state: TRPCConnectionStateMessage<TError>) => void;
|
|
58
|
-
}
|
|
59
|
-
export interface restartSubscriptionOptionsBase {
|
|
60
|
-
/**
|
|
61
|
-
* Cancel the current subscription and re-establish a new one
|
|
62
|
-
* - Defaults to `true`
|
|
63
|
-
* - Set to `false` no new subscription will be established if there is already an active subscription
|
|
64
|
-
*/
|
|
65
|
-
cancelSubscription?: boolean;
|
|
66
|
-
}
|
|
67
|
-
export interface restartSubscriptionOptionsWithLastEventId extends restartSubscriptionOptionsBase {
|
|
68
|
-
/**
|
|
69
|
-
* Defaults to `true` in case of failure or if the subscription is still active
|
|
70
|
-
* - When the susbscription has successfully completed, it will be set to `false`
|
|
71
|
-
*/
|
|
72
|
-
sendLastEventId?: boolean;
|
|
73
|
-
}
|
|
74
|
-
export type restartSubscriptionOptions<TInput> = TInput extends {
|
|
75
|
-
lastEventId?: string | null;
|
|
76
|
-
} | void ? restartSubscriptionOptionsWithLastEventId : restartSubscriptionOptionsBase;
|
|
77
|
-
export type restartSubscriptionFn<TInput> = (options?: restartSubscriptionOptions<TInput>) => void;
|
|
78
|
-
export interface TRPCSubscriptionBaseResult<_TInput, TOutput, TError> {
|
|
79
|
-
/**
|
|
80
|
-
* The last data received from the subscription
|
|
81
|
-
*/
|
|
82
|
-
data: TOutput | null;
|
|
83
|
-
/**
|
|
84
|
-
* The timestamp for when the connection was last (re-)established
|
|
85
|
-
*/
|
|
86
|
-
connectionStartedAt: number;
|
|
87
|
-
/**
|
|
88
|
-
* The timestamp for when the connection was initially established
|
|
89
|
-
*/
|
|
90
|
-
initialConnectionStartedAt: number;
|
|
91
|
-
/**
|
|
92
|
-
* The error that caused the subscription to stop
|
|
93
|
-
* - Defaults to `null`
|
|
94
|
-
* - Resets to `null` after the subscription is restarted and the connection is re-established
|
|
95
|
-
*/
|
|
96
|
-
error: TError | null;
|
|
97
|
-
/**
|
|
98
|
-
* The timestamp for when the last error was captured
|
|
99
|
-
*/
|
|
100
|
-
errorUpdatedAt: number;
|
|
101
|
-
/**
|
|
102
|
-
* The reason for the reconnection
|
|
103
|
-
* - Resets to `null` after the subscription is restarted and the connection is re-established
|
|
104
|
-
*/
|
|
105
|
-
connectionError: TError | null;
|
|
106
|
-
/**
|
|
107
|
-
* Reconnection attempts since last successful connection
|
|
108
|
-
*/
|
|
109
|
-
connectionAttemptCount: number;
|
|
110
|
-
/**
|
|
111
|
-
* The timestamp for when the last reconnection error was captured
|
|
112
|
-
*/
|
|
113
|
-
connectionErrorUpdatedAt: number;
|
|
114
|
-
/**
|
|
115
|
-
* Is `true` when the subscription is establishing the initial connection
|
|
116
|
-
*/
|
|
117
|
-
isStarting: boolean;
|
|
118
|
-
/**
|
|
119
|
-
* Is `true` when the subscription has successfully connected at least once
|
|
120
|
-
*/
|
|
121
|
-
isStarted: boolean;
|
|
122
|
-
/**
|
|
123
|
-
* Is `true` if the subscription is (re-)establishing a connection
|
|
124
|
-
* - Alias for `status === 'connecting'`
|
|
125
|
-
*/
|
|
126
|
-
isConnecting: boolean;
|
|
60
|
+
enabled?: boolean;
|
|
127
61
|
/**
|
|
128
|
-
*
|
|
129
|
-
* - Alias for `status === 'pending'`
|
|
62
|
+
* Called when the subscription is started
|
|
130
63
|
*/
|
|
131
|
-
|
|
64
|
+
onStarted?: () => void;
|
|
132
65
|
/**
|
|
133
|
-
*
|
|
134
|
-
* - Alias for `status === 'connecting' && isStarted === true`
|
|
66
|
+
* Called when new data is received
|
|
135
67
|
*/
|
|
136
|
-
|
|
68
|
+
onData?: (data: TOutput) => void;
|
|
137
69
|
/**
|
|
138
|
-
*
|
|
139
|
-
* - Alias for `status === 'error'`
|
|
70
|
+
* Called when an **unrecoverable error** occurs and the subscription is closed
|
|
140
71
|
*/
|
|
141
|
-
|
|
72
|
+
onError?: (err: TError) => void;
|
|
73
|
+
}
|
|
74
|
+
export interface TRPCSubscriptionBaseResult<TOutput, TError> {
|
|
75
|
+
status: 'idle' | 'connecting' | 'pending' | 'error';
|
|
76
|
+
data: undefined | TOutput;
|
|
77
|
+
error: null | TError;
|
|
142
78
|
/**
|
|
143
|
-
*
|
|
144
|
-
* - Will be:
|
|
145
|
-
* - `'idle'` when the subscription is not enabled
|
|
146
|
-
* - `'connecting'` when the subscription is (re-)establishing the connection
|
|
147
|
-
* - `'pending'` when the subscription is connected and receiving data
|
|
148
|
-
* - `'error'` when the subscription has stopped due to an error
|
|
79
|
+
* Reset the subscription
|
|
149
80
|
*/
|
|
150
|
-
|
|
81
|
+
reset: () => void;
|
|
151
82
|
}
|
|
152
|
-
export interface TRPCSubscriptionIdleResult<
|
|
153
|
-
data: null;
|
|
154
|
-
error: null;
|
|
155
|
-
errorUpdatedAt: 0;
|
|
156
|
-
connectionError: null;
|
|
157
|
-
isStarting: false;
|
|
158
|
-
isStarted: false;
|
|
159
|
-
isConnecting: false;
|
|
160
|
-
isPending: false;
|
|
161
|
-
isReconnecting: false;
|
|
162
|
-
isError: false;
|
|
163
|
-
connectionAttemptCount: 0;
|
|
164
|
-
connectionErrorUpdatedAt: 0;
|
|
165
|
-
connectionStartedAt: 0;
|
|
166
|
-
initialConnectionStartedAt: 0;
|
|
83
|
+
export interface TRPCSubscriptionIdleResult<TOutput> extends TRPCSubscriptionBaseResult<TOutput, null> {
|
|
167
84
|
status: 'idle';
|
|
168
|
-
|
|
169
|
-
export declare const getIdleResult: <TInput, TOutput, TError>() => TRPCSubscriptionIdleResult<TInput, TOutput, TError>;
|
|
170
|
-
export interface TRPCSubscriptionStartingResult<TInput, TOutput, TError> extends TRPCSubscriptionBaseResult<TInput, TOutput, TError> {
|
|
171
|
-
connectionStartedAt: 0;
|
|
172
|
-
initialConnectionStartedAt: 0;
|
|
85
|
+
data: undefined;
|
|
173
86
|
error: null;
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
isStarting: true;
|
|
177
|
-
isStarted: false;
|
|
178
|
-
isConnecting: true;
|
|
179
|
-
isPending: false;
|
|
180
|
-
isReconnecting: false;
|
|
181
|
-
isError: false;
|
|
87
|
+
}
|
|
88
|
+
export interface TRPCSubscriptionConnectingResult<TOutput, TError> extends TRPCSubscriptionBaseResult<TOutput, TError> {
|
|
182
89
|
status: 'connecting';
|
|
90
|
+
data: undefined | TOutput;
|
|
91
|
+
error: TError | null;
|
|
183
92
|
}
|
|
184
|
-
export
|
|
185
|
-
export interface TRPCSubscriptionPendingResult<TInput, TOutput, TError> extends TRPCSubscriptionBaseResult<TInput, TOutput, TError> {
|
|
186
|
-
error: null;
|
|
187
|
-
connectionError: null;
|
|
188
|
-
connectionAttemptCount: 0;
|
|
189
|
-
isStarting: false;
|
|
190
|
-
isStarted: true;
|
|
191
|
-
isConnecting: false;
|
|
192
|
-
isPending: true;
|
|
193
|
-
isReconnecting: false;
|
|
194
|
-
isError: false;
|
|
93
|
+
export interface TRPCSubscriptionPendingResult<TOutput> extends TRPCSubscriptionBaseResult<TOutput, undefined> {
|
|
195
94
|
status: 'pending';
|
|
196
|
-
|
|
197
|
-
export declare const getPendingResult: <TInput, TOutput, TError>(previous: UseTRPCSubscriptionResult<TInput, TOutput, TError>, data?: TOutput) => TRPCSubscriptionPendingResult<TInput, TOutput, TError>;
|
|
198
|
-
export interface TRPCSubscriptionReconnectingResult<TInput, TOutput, TError> extends TRPCSubscriptionBaseResult<TInput, TOutput, TError> {
|
|
95
|
+
data: TOutput;
|
|
199
96
|
error: null;
|
|
200
|
-
connectionError: TError;
|
|
201
|
-
isStarting: false;
|
|
202
|
-
isStarted: true;
|
|
203
|
-
isConnecting: true;
|
|
204
|
-
isPending: false;
|
|
205
|
-
isReconnecting: true;
|
|
206
|
-
isError: false;
|
|
207
|
-
status: 'connecting';
|
|
208
97
|
}
|
|
209
|
-
export
|
|
210
|
-
export declare const getConnectingResult: <TInput, TOutput, TError>(previous: UseTRPCSubscriptionResult<TInput, TOutput, TError>, error: TError | null) => TRPCSubscriptionConnectingResult<TInput, TOutput, TError>;
|
|
211
|
-
export interface TRPCSubscriptionErrorResult<TInput, TOutput, TError> extends TRPCSubscriptionBaseResult<TInput, TOutput, TError> {
|
|
212
|
-
error: TError;
|
|
213
|
-
isStarting: false;
|
|
214
|
-
isStarted: true;
|
|
215
|
-
isConnecting: false;
|
|
216
|
-
isPending: false;
|
|
217
|
-
isReconnecting: false;
|
|
218
|
-
isError: true;
|
|
98
|
+
export interface TRPCSubscriptionErrorResult<TOutput, TError> extends TRPCSubscriptionBaseResult<TOutput, TError> {
|
|
219
99
|
status: 'error';
|
|
100
|
+
data: TOutput | undefined;
|
|
101
|
+
error: TError;
|
|
220
102
|
}
|
|
221
|
-
export
|
|
222
|
-
export type TRPCSubscriptionConnectingResult<TInput, TOutput, TError> = TRPCSubscriptionStartingResult<TInput, TOutput, TError> | TRPCSubscriptionReconnectingResult<TInput, TOutput, TError>;
|
|
223
|
-
export type UseTRPCSubscriptionResult<TInput, TOutput, TError> = TRPCSubscriptionIdleResult<TInput, TOutput, TError> | TRPCSubscriptionPendingResult<TInput, TOutput, TError> | TRPCSubscriptionConnectingResult<TInput, TOutput, TError> | TRPCSubscriptionErrorResult<TInput, TOutput, TError>;
|
|
103
|
+
export type TRPCSubscriptionResult<TOutput, TError> = TRPCSubscriptionIdleResult<TOutput> | TRPCSubscriptionConnectingResult<TOutput, TError> | TRPCSubscriptionErrorResult<TOutput, TError> | TRPCSubscriptionPendingResult<TOutput>;
|
|
224
104
|
export interface TRPCProviderProps<TRouter extends AnyRouter, TSSRContext> extends TRPCContextProps<TRouter, TSSRContext> {
|
|
225
105
|
children: ReactNode;
|
|
226
106
|
}
|
|
227
107
|
export type TRPCProvider<TRouter extends AnyRouter, TSSRContext> = (props: TRPCProviderProps<TRouter, TSSRContext>) => JSX.Element;
|
|
228
108
|
export type UseDehydratedState<TRouter extends AnyRouter> = (client: TRPCUntypedClient<TRouter>, trpcState: DehydratedState | undefined) => DehydratedState | undefined;
|
|
229
109
|
export type CreateClient<TRouter extends AnyRouter> = (opts: CreateTRPCClientOptions<TRouter>) => TRPCUntypedClient<TRouter>;
|
|
230
|
-
type coerceAsyncIterableToArray<TValue> = TValue extends AsyncIterable<infer $Inferred> ? $Inferred[] : TValue;
|
|
110
|
+
export type coerceAsyncIterableToArray<TValue> = TValue extends AsyncIterable<infer $Inferred> ? $Inferred[] : TValue;
|
|
231
111
|
/**
|
|
232
112
|
* @internal
|
|
233
113
|
*/
|
|
@@ -271,5 +151,4 @@ export interface TRPCHookResult {
|
|
|
271
151
|
path: string;
|
|
272
152
|
};
|
|
273
153
|
}
|
|
274
|
-
export {};
|
|
275
154
|
//# sourceMappingURL=types.d.ts.map
|