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