@trpc/react-query 10.7.0 → 10.7.1-alpha-next-2023-09-27-02-47-10.74
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/LICENSE +2 -2
- package/README.md +7 -5
- package/dist/createHooksInternal-3565a89f.js +452 -0
- package/dist/createHooksInternal-442a034d.js +396 -0
- package/dist/createHooksInternal-a268b2a2.mjs +443 -0
- package/dist/createTRPCReact.d.ts +95 -59
- package/dist/createTRPCReact.d.ts.map +1 -1
- package/dist/index.d.ts +4 -4
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +5 -17
- package/dist/index.mjs +5 -16
- package/dist/internals/context.d.ts +109 -106
- package/dist/internals/context.d.ts.map +1 -1
- package/dist/internals/getClientArgs.d.ts +3 -0
- package/dist/internals/getClientArgs.d.ts.map +1 -0
- package/dist/internals/getQueryKey.d.ts +45 -5
- package/dist/internals/getQueryKey.d.ts.map +1 -1
- package/dist/internals/useHookResult.d.ts +10 -0
- package/dist/internals/useHookResult.d.ts.map +1 -0
- package/dist/internals/useQueries.d.ts +27 -27
- package/dist/internals/useQueries.d.ts.map +1 -1
- package/dist/queryClient-1c8d7d8a.js +8 -0
- package/dist/server/index.d.ts +2 -0
- package/dist/server/index.d.ts.map +1 -0
- package/dist/server/index.js +108 -0
- package/dist/server/index.mjs +104 -0
- package/dist/server/ssgProxy.d.ts +48 -0
- package/dist/server/ssgProxy.d.ts.map +1 -0
- package/dist/shared/hooks/createHooksInternal.d.ts +49 -129
- package/dist/shared/hooks/createHooksInternal.d.ts.map +1 -1
- package/dist/shared/hooks/createRootHooks.d.ts +2 -0
- package/dist/shared/hooks/createRootHooks.d.ts.map +1 -0
- package/dist/shared/hooks/types.d.ts +100 -0
- package/dist/shared/hooks/types.d.ts.map +1 -0
- package/dist/shared/index.d.ts +21 -8
- package/dist/shared/index.d.ts.map +1 -1
- package/dist/shared/index.js +8 -5
- package/dist/shared/index.mjs +3 -3
- package/dist/shared/polymorphism/index.d.ts +5 -0
- package/dist/shared/polymorphism/index.d.ts.map +1 -0
- package/dist/shared/polymorphism/mutationLike.d.ts +18 -0
- package/dist/shared/polymorphism/mutationLike.d.ts.map +1 -0
- package/dist/shared/polymorphism/queryLike.d.ts +18 -0
- package/dist/shared/polymorphism/queryLike.d.ts.map +1 -0
- package/dist/shared/polymorphism/routerLike.d.ts +11 -0
- package/dist/shared/polymorphism/routerLike.d.ts.map +1 -0
- package/dist/shared/polymorphism/utilsLike.d.ts +7 -0
- package/dist/shared/polymorphism/utilsLike.d.ts.map +1 -0
- package/dist/shared/proxy/decorationProxy.d.ts +7 -7
- package/dist/shared/proxy/decorationProxy.d.ts.map +1 -1
- package/dist/shared/proxy/useQueriesProxy.d.ts +17 -17
- package/dist/shared/proxy/useQueriesProxy.d.ts.map +1 -1
- package/dist/shared/proxy/utilsProxy.d.ts +95 -83
- package/dist/shared/proxy/utilsProxy.d.ts.map +1 -1
- package/dist/shared/queryClient.d.ts +15 -15
- package/dist/shared/queryClient.d.ts.map +1 -1
- package/dist/shared/types.d.ts +40 -37
- package/dist/shared/types.d.ts.map +1 -1
- package/dist/utils/inferReactQueryProcedure.d.ts +23 -10
- package/dist/utils/inferReactQueryProcedure.d.ts.map +1 -1
- package/dist/utilsProxy-11521789.js +114 -0
- package/dist/utilsProxy-981e4875.js +128 -0
- package/dist/utilsProxy-d4d71107.mjs +121 -0
- package/package.json +30 -21
- package/server/index.d.ts +1 -0
- package/server/index.js +1 -0
- package/src/createTRPCReact.tsx +202 -124
- package/src/index.ts +1 -1
- package/src/internals/context.tsx +103 -150
- package/src/internals/getClientArgs.ts +12 -0
- package/src/internals/getQueryKey.ts +125 -8
- package/src/internals/useHookResult.ts +18 -0
- package/src/internals/useQueries.ts +7 -10
- package/src/server/index.ts +1 -0
- package/src/server/ssgProxy.ts +250 -0
- package/src/shared/hooks/createHooksInternal.tsx +286 -454
- package/src/shared/hooks/createRootHooks.tsx +4 -0
- package/src/shared/hooks/types.ts +225 -0
- package/src/shared/index.ts +19 -1
- package/src/shared/polymorphism/index.ts +4 -0
- package/src/shared/polymorphism/mutationLike.ts +34 -0
- package/src/shared/polymorphism/queryLike.ts +37 -0
- package/src/shared/polymorphism/routerLike.ts +29 -0
- package/src/shared/polymorphism/utilsLike.ts +8 -0
- package/src/shared/proxy/decorationProxy.ts +11 -27
- package/src/shared/proxy/useQueriesProxy.ts +25 -33
- package/src/shared/proxy/utilsProxy.ts +155 -93
- package/src/shared/types.ts +10 -6
- package/src/utils/inferReactQueryProcedure.ts +58 -25
- package/dist/createHooksInternal-9b01c277.js +0 -481
- package/dist/createHooksInternal-de11647d.mjs +0 -471
- package/dist/getArrayQueryKey-4bdb5cc2.js +0 -36
- package/dist/getArrayQueryKey-86134f8b.mjs +0 -34
- package/dist/internals/getArrayQueryKey.d.ts +0 -17
- 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/ssg/index.d.ts +0 -5
- package/dist/ssg/index.d.ts.map +0 -1
- package/dist/ssg/index.js +0 -126
- package/dist/ssg/index.mjs +0 -121
- package/dist/ssg/ssg.d.ts +0 -23
- package/dist/ssg/ssg.d.ts.map +0 -1
- package/dist/ssg/ssgProxy.d.ts +0 -37
- package/dist/ssg/ssgProxy.d.ts.map +0 -1
- package/src/internals/getArrayQueryKey.test.ts +0 -87
- package/src/internals/getArrayQueryKey.ts +0 -39
- package/src/interop.ts +0 -29
- package/src/ssg/index.ts +0 -4
- package/src/ssg/ssg.ts +0 -144
- package/src/ssg/ssgProxy.ts +0 -108
- package/ssg/index.d.ts +0 -1
- package/ssg/index.js +0 -1
package/LICENSE
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
MIT License
|
|
2
2
|
|
|
3
|
-
Copyright (c)
|
|
3
|
+
Copyright (c) 2023 Alex Johansson
|
|
4
4
|
|
|
5
5
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
6
|
of this software and associated documentation files (the "Software"), to deal
|
|
@@ -18,4 +18,4 @@ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
18
18
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
19
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
20
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
-
SOFTWARE.
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -31,6 +31,9 @@ yarn add @trpc/react-query @tanstack/react-query
|
|
|
31
31
|
|
|
32
32
|
# pnpm
|
|
33
33
|
pnpm add @trpc/react-query @tanstack/react-query
|
|
34
|
+
|
|
35
|
+
# Bun
|
|
36
|
+
bun add @trpc/react-query @tanstack/react-query
|
|
34
37
|
```
|
|
35
38
|
|
|
36
39
|
## Basic Example
|
|
@@ -38,7 +41,7 @@ pnpm add @trpc/react-query @tanstack/react-query
|
|
|
38
41
|
Create a utils file that exports tRPC hooks and providers.
|
|
39
42
|
|
|
40
43
|
```ts
|
|
41
|
-
import {
|
|
44
|
+
import { createTRPCReact } from '@trpc/react-query';
|
|
42
45
|
import type { AppRouter } from './server';
|
|
43
46
|
|
|
44
47
|
export const trpc = createTRPCReact<AppRouter>();
|
|
@@ -48,9 +51,8 @@ Use the provider to connect to your API.
|
|
|
48
51
|
|
|
49
52
|
```ts
|
|
50
53
|
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
|
|
51
|
-
import React from 'react';
|
|
52
|
-
import { useState } from 'react';
|
|
53
54
|
import { trpc } from '~/utils/trpc';
|
|
55
|
+
import React, { useState } from 'react';
|
|
54
56
|
|
|
55
57
|
export function App() {
|
|
56
58
|
const [queryClient] = useState(() => new QueryClient());
|
|
@@ -72,10 +74,10 @@ export function App() {
|
|
|
72
74
|
Now in any component, you can query your API using the proxy exported from the utils file.
|
|
73
75
|
|
|
74
76
|
```ts
|
|
75
|
-
import {
|
|
77
|
+
import { trpc } from '~/utils/trpc';
|
|
76
78
|
|
|
77
79
|
export function Hello() {
|
|
78
|
-
const { data, error, status } =
|
|
80
|
+
const { data, error, status } = trpc.greeting.useQuery({ name: 'tRPC' });
|
|
79
81
|
|
|
80
82
|
if (error) {
|
|
81
83
|
return <p>{error.message}</p>;
|
|
@@ -0,0 +1,452 @@
|
|
|
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-981e4875.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 createReactProxyDecoration(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 createUseQueriesProxy(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
|
+
defaultPageParam: 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
|
+
defaultPageParam: 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(// REVIEW: Should this take opts?? The types doesn't have it
|
|
193
|
+
(queryKey)=>{
|
|
194
|
+
return queryClient.getQueryData(queryKey);
|
|
195
|
+
}, [
|
|
196
|
+
queryClient
|
|
197
|
+
]),
|
|
198
|
+
setInfiniteQueryData: React.useCallback((queryKey, updater, options)=>{
|
|
199
|
+
return queryClient.setQueryData(queryKey, updater, options);
|
|
200
|
+
}, [
|
|
201
|
+
queryClient
|
|
202
|
+
]),
|
|
203
|
+
getInfiniteQueryData: React.useCallback((queryKey)=>{
|
|
204
|
+
return queryClient.getQueryData(queryKey);
|
|
205
|
+
}, [
|
|
206
|
+
queryClient
|
|
207
|
+
])
|
|
208
|
+
}
|
|
209
|
+
}, props.children);
|
|
210
|
+
};
|
|
211
|
+
function useContext() {
|
|
212
|
+
return React__default["default"].useContext(Context);
|
|
213
|
+
}
|
|
214
|
+
/**
|
|
215
|
+
* Hack to make sure errors return `status`='error` when doing SSR
|
|
216
|
+
* @link https://github.com/trpc/trpc/pull/1645
|
|
217
|
+
*/ function useSSRQueryOptionsIfNeeded(queryKey, opts) {
|
|
218
|
+
const { queryClient , ssrState } = useContext();
|
|
219
|
+
return ssrState && ssrState !== 'mounted' && queryClient.getQueryCache().find({
|
|
220
|
+
queryKey
|
|
221
|
+
})?.state.status === 'error' ? {
|
|
222
|
+
retryOnMount: false,
|
|
223
|
+
...opts
|
|
224
|
+
} : opts;
|
|
225
|
+
}
|
|
226
|
+
function useQuery(path, input, opts) {
|
|
227
|
+
const context = useContext();
|
|
228
|
+
if (!context) {
|
|
229
|
+
throw new Error('Unable to retrieve application context. Did you forget to wrap your App inside `withTRPC` HoC?');
|
|
230
|
+
}
|
|
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 hookResult = useQuery(path, input, {
|
|
267
|
+
...opts,
|
|
268
|
+
suspense: true,
|
|
269
|
+
enabled: true,
|
|
270
|
+
throwOnError: true
|
|
271
|
+
});
|
|
272
|
+
return [
|
|
273
|
+
hookResult.data,
|
|
274
|
+
hookResult
|
|
275
|
+
];
|
|
276
|
+
}
|
|
277
|
+
function useMutation(path, opts) {
|
|
278
|
+
const { client } = useContext();
|
|
279
|
+
const queryClient = reactQuery.useQueryClient();
|
|
280
|
+
const mutationKey = [
|
|
281
|
+
path
|
|
282
|
+
];
|
|
283
|
+
const defaultOpts = queryClient.getMutationDefaults(mutationKey);
|
|
284
|
+
const hook = reactQuery.useMutation({
|
|
285
|
+
...opts,
|
|
286
|
+
mutationKey: mutationKey,
|
|
287
|
+
mutationFn: (input)=>{
|
|
288
|
+
return client.mutation(...getClientArgs([
|
|
289
|
+
path,
|
|
290
|
+
{
|
|
291
|
+
input
|
|
292
|
+
}
|
|
293
|
+
], opts));
|
|
294
|
+
},
|
|
295
|
+
onSuccess (...args) {
|
|
296
|
+
const originalFn = ()=>opts?.onSuccess?.(...args) ?? defaultOpts?.onSuccess?.(...args);
|
|
297
|
+
return mutationSuccessOverride({
|
|
298
|
+
originalFn,
|
|
299
|
+
queryClient,
|
|
300
|
+
meta: opts?.meta ?? defaultOpts?.meta ?? {}
|
|
301
|
+
});
|
|
302
|
+
}
|
|
303
|
+
}, queryClient);
|
|
304
|
+
hook.trpc = useHookResult({
|
|
305
|
+
path: path.join('.')
|
|
306
|
+
});
|
|
307
|
+
return hook;
|
|
308
|
+
}
|
|
309
|
+
/* istanbul ignore next -- @preserve */ function useSubscription(path, input, opts) {
|
|
310
|
+
const enabled = opts?.enabled ?? true;
|
|
311
|
+
const queryKey = reactQuery.hashKey(utilsProxy.getQueryKeyInternal(path, input, 'any'));
|
|
312
|
+
const { client } = useContext();
|
|
313
|
+
const optsRef = React.useRef(opts);
|
|
314
|
+
optsRef.current = opts;
|
|
315
|
+
React.useEffect(()=>{
|
|
316
|
+
if (!enabled) {
|
|
317
|
+
return;
|
|
318
|
+
}
|
|
319
|
+
let isStopped = false;
|
|
320
|
+
const subscription = client.subscription(path.join('.'), input ?? undefined, {
|
|
321
|
+
onStarted: ()=>{
|
|
322
|
+
if (!isStopped) {
|
|
323
|
+
optsRef.current.onStarted?.();
|
|
324
|
+
}
|
|
325
|
+
},
|
|
326
|
+
onData: (data)=>{
|
|
327
|
+
if (!isStopped) {
|
|
328
|
+
opts.onData(data);
|
|
329
|
+
}
|
|
330
|
+
},
|
|
331
|
+
onError: (err)=>{
|
|
332
|
+
if (!isStopped) {
|
|
333
|
+
optsRef.current.onError?.(err);
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
});
|
|
337
|
+
return ()=>{
|
|
338
|
+
isStopped = true;
|
|
339
|
+
subscription.unsubscribe();
|
|
340
|
+
};
|
|
341
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
342
|
+
}, [
|
|
343
|
+
queryKey,
|
|
344
|
+
enabled
|
|
345
|
+
]);
|
|
346
|
+
}
|
|
347
|
+
function useInfiniteQuery(path, input, opts) {
|
|
348
|
+
const { client , ssrState , prefetchInfiniteQuery , queryClient , abortOnUnmount , } = useContext();
|
|
349
|
+
const queryKey = utilsProxy.getQueryKeyInternal(path, input, 'infinite');
|
|
350
|
+
const defaultOpts = queryClient.getQueryDefaults(queryKey);
|
|
351
|
+
if (typeof window === 'undefined' && ssrState === 'prepass' && opts?.trpc?.ssr !== false && (opts?.enabled ?? defaultOpts?.enabled) !== false && !queryClient.getQueryCache().find({
|
|
352
|
+
queryKey
|
|
353
|
+
})) {
|
|
354
|
+
void prefetchInfiniteQuery(queryKey, {
|
|
355
|
+
...defaultOpts,
|
|
356
|
+
...opts
|
|
357
|
+
});
|
|
358
|
+
}
|
|
359
|
+
const ssrOpts = useSSRQueryOptionsIfNeeded(queryKey, {
|
|
360
|
+
...defaultOpts,
|
|
361
|
+
...opts
|
|
362
|
+
});
|
|
363
|
+
// request option should take priority over global
|
|
364
|
+
const shouldAbortOnUnmount = opts?.trpc?.abortOnUnmount ?? abortOnUnmount;
|
|
365
|
+
const hook = reactQuery.useInfiniteQuery({
|
|
366
|
+
...ssrOpts,
|
|
367
|
+
defaultPageParam: opts.initialCursor ?? null,
|
|
368
|
+
queryKey: queryKey,
|
|
369
|
+
queryFn: (queryFunctionContext)=>{
|
|
370
|
+
const actualOpts = {
|
|
371
|
+
...ssrOpts,
|
|
372
|
+
trpc: {
|
|
373
|
+
...ssrOpts?.trpc,
|
|
374
|
+
...shouldAbortOnUnmount ? {
|
|
375
|
+
signal: queryFunctionContext.signal
|
|
376
|
+
} : {}
|
|
377
|
+
}
|
|
378
|
+
};
|
|
379
|
+
return client.query(...getClientArgs(queryKey, actualOpts, queryFunctionContext.pageParam ?? opts.initialCursor));
|
|
380
|
+
}
|
|
381
|
+
}, queryClient);
|
|
382
|
+
hook.trpc = useHookResult({
|
|
383
|
+
// REVIEW: What do we want to return here?
|
|
384
|
+
path: path.join('.')
|
|
385
|
+
});
|
|
386
|
+
return hook;
|
|
387
|
+
}
|
|
388
|
+
function useSuspenseInfiniteQuery(path, input, opts) {
|
|
389
|
+
const hookResult = useInfiniteQuery(path, input, {
|
|
390
|
+
...opts,
|
|
391
|
+
suspense: true,
|
|
392
|
+
enabled: true,
|
|
393
|
+
throwOnError: true
|
|
394
|
+
});
|
|
395
|
+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
396
|
+
return [
|
|
397
|
+
hookResult.data,
|
|
398
|
+
hookResult
|
|
399
|
+
];
|
|
400
|
+
}
|
|
401
|
+
const useQueries = (queriesCallback)=>{
|
|
402
|
+
const { ssrState , queryClient , prefetchQuery , client } = useContext();
|
|
403
|
+
const proxy = createUseQueriesProxy(client);
|
|
404
|
+
const queries = queriesCallback(proxy);
|
|
405
|
+
if (typeof window === 'undefined' && ssrState === 'prepass') {
|
|
406
|
+
for (const query of queries){
|
|
407
|
+
const queryOption = query;
|
|
408
|
+
if (queryOption.trpc?.ssr !== false && !queryClient.getQueryCache().find({
|
|
409
|
+
queryKey: queryOption.queryKey
|
|
410
|
+
})) {
|
|
411
|
+
void prefetchQuery(queryOption.queryKey, queryOption);
|
|
412
|
+
}
|
|
413
|
+
}
|
|
414
|
+
}
|
|
415
|
+
return reactQuery.useQueries({
|
|
416
|
+
queries: queries.map((query)=>({
|
|
417
|
+
...query,
|
|
418
|
+
queryKey: query.queryKey
|
|
419
|
+
}))
|
|
420
|
+
}, queryClient);
|
|
421
|
+
};
|
|
422
|
+
const useDehydratedState = (client, trpcState)=>{
|
|
423
|
+
const transformed = React.useMemo(()=>{
|
|
424
|
+
if (!trpcState) {
|
|
425
|
+
return trpcState;
|
|
426
|
+
}
|
|
427
|
+
return client.runtime.transformer.deserialize(trpcState);
|
|
428
|
+
}, [
|
|
429
|
+
trpcState,
|
|
430
|
+
client
|
|
431
|
+
]);
|
|
432
|
+
return transformed;
|
|
433
|
+
};
|
|
434
|
+
return {
|
|
435
|
+
Provider: TRPCProvider,
|
|
436
|
+
createClient,
|
|
437
|
+
useContext,
|
|
438
|
+
useQuery,
|
|
439
|
+
useSuspenseQuery,
|
|
440
|
+
useQueries,
|
|
441
|
+
useMutation,
|
|
442
|
+
useSubscription,
|
|
443
|
+
useDehydratedState,
|
|
444
|
+
useInfiniteQuery,
|
|
445
|
+
useSuspenseInfiniteQuery
|
|
446
|
+
};
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
exports.createReactProxyDecoration = createReactProxyDecoration;
|
|
450
|
+
exports.createRootHooks = createRootHooks;
|
|
451
|
+
exports.createUseQueriesProxy = createUseQueriesProxy;
|
|
452
|
+
exports.getClientArgs = getClientArgs;
|