@trpc/react-query 10.0.0-proxy-beta.21
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 +21 -0
- package/README.md +88 -0
- package/dist/createHooksInternal-b47ee704.mjs +392 -0
- package/dist/createHooksInternal-e8214d72.js +400 -0
- package/dist/createTRPCReact.d.ts +38 -0
- package/dist/createTRPCReact.d.ts.map +1 -0
- package/dist/getArrayQueryKey-062214e0.mjs +19 -0
- package/dist/getArrayQueryKey-a2092b3c.js +21 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +58 -0
- package/dist/index.mjs +47 -0
- package/dist/internals/context.d.ts +99 -0
- package/dist/internals/context.d.ts.map +1 -0
- package/dist/internals/getArrayQueryKey.d.ts +9 -0
- package/dist/internals/getArrayQueryKey.d.ts.map +1 -0
- package/dist/internals/getQueryKey.d.ts +6 -0
- package/dist/internals/getQueryKey.d.ts.map +1 -0
- package/dist/interop.d.ts +10 -0
- package/dist/interop.d.ts.map +1 -0
- package/dist/queryClient-0569f6e0.mjs +7 -0
- package/dist/queryClient-83576230.js +9 -0
- package/dist/shared/hooks/createHooksInternal.d.ts +115 -0
- package/dist/shared/hooks/createHooksInternal.d.ts.map +1 -0
- package/dist/shared/index.d.ts +6 -0
- package/dist/shared/index.d.ts.map +1 -0
- package/dist/shared/index.js +18 -0
- package/dist/shared/index.mjs +7 -0
- package/dist/shared/proxy/decorationProxy.d.ts +8 -0
- package/dist/shared/proxy/decorationProxy.d.ts.map +1 -0
- package/dist/shared/proxy/utilsProxy.d.ts +80 -0
- package/dist/shared/proxy/utilsProxy.d.ts.map +1 -0
- package/dist/shared/queryClient.d.ts +16 -0
- package/dist/shared/queryClient.d.ts.map +1 -0
- package/dist/shared/types.d.ts +26 -0
- package/dist/shared/types.d.ts.map +1 -0
- package/dist/ssg/index.d.ts +5 -0
- package/dist/ssg/index.d.ts.map +1 -0
- package/dist/ssg/index.js +126 -0
- package/dist/ssg/index.mjs +121 -0
- package/dist/ssg/ssg.d.ts +23 -0
- package/dist/ssg/ssg.d.ts.map +1 -0
- package/dist/ssg/ssgProxy.d.ts +36 -0
- package/dist/ssg/ssgProxy.d.ts.map +1 -0
- package/package.json +75 -0
- package/shared/index.d.ts +1 -0
- package/shared/index.js +1 -0
- package/src/createTRPCReact.tsx +164 -0
- package/src/index.ts +4 -0
- package/src/internals/context.tsx +235 -0
- package/src/internals/getArrayQueryKey.test.ts +41 -0
- package/src/internals/getArrayQueryKey.ts +18 -0
- package/src/internals/getQueryKey.ts +10 -0
- package/src/interop.ts +25 -0
- package/src/shared/hooks/createHooksInternal.tsx +664 -0
- package/src/shared/index.ts +8 -0
- package/src/shared/proxy/decorationProxy.ts +33 -0
- package/src/shared/proxy/utilsProxy.ts +266 -0
- package/src/shared/queryClient.ts +20 -0
- package/src/shared/types.ts +27 -0
- package/src/ssg/index.ts +4 -0
- package/src/ssg/ssg.ts +138 -0
- package/src/ssg/ssgProxy.ts +105 -0
- package/ssg/index.d.ts +1 -0
- package/ssg/index.js +1 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) <year> <author>
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
<p align="center">
|
|
2
|
+
<a href="https://trpc.io/"><img src="../../www/static/img/logo-text.svg" alt="tRPC" height="130"/></a>
|
|
3
|
+
</p>
|
|
4
|
+
|
|
5
|
+
<p align="center">
|
|
6
|
+
<strong>End-to-end typesafe APIs made easy</strong>
|
|
7
|
+
</p>
|
|
8
|
+
|
|
9
|
+
<p align="center">
|
|
10
|
+
<img src="https://assets.trpc.io/www/v10/preview-dark.gif" alt="Demo" />
|
|
11
|
+
</p>
|
|
12
|
+
|
|
13
|
+
# `@trpc/react`
|
|
14
|
+
|
|
15
|
+
> Connect a tRPC server to React.
|
|
16
|
+
|
|
17
|
+
## Documentation
|
|
18
|
+
|
|
19
|
+
Full documentation for `@trpc/react-query` can be found [here](https://trpc.io/docs/react-queries)
|
|
20
|
+
|
|
21
|
+
## Installation
|
|
22
|
+
|
|
23
|
+
```bash
|
|
24
|
+
# npm
|
|
25
|
+
npm install @trpc/react-query@next @tanstack/react-query
|
|
26
|
+
|
|
27
|
+
# Yarn
|
|
28
|
+
yarn add @trpc/react-query@next @tanstack/react-query
|
|
29
|
+
|
|
30
|
+
# pnpm
|
|
31
|
+
pnpm add @trpc/react-query@next @tanstack/react-query
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
## Basic Example
|
|
35
|
+
|
|
36
|
+
Create a utils file that exports tRPC hooks and providers.
|
|
37
|
+
|
|
38
|
+
```ts
|
|
39
|
+
import { createReactQueryHooks, createTRPCReact } from '@trpc/react-query';
|
|
40
|
+
import type { AppRouter } from './server';
|
|
41
|
+
|
|
42
|
+
export const trpc = createTRPCReact<AppRouter>();
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
Use the provider to connect to your API.
|
|
46
|
+
|
|
47
|
+
```ts
|
|
48
|
+
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
|
|
49
|
+
import React from 'react';
|
|
50
|
+
import { useState } from 'react';
|
|
51
|
+
import { trpc } from '~/utils/trpc';
|
|
52
|
+
|
|
53
|
+
export function App() {
|
|
54
|
+
const [queryClient] = useState(() => new QueryClient());
|
|
55
|
+
const [trpcClient] = useState(() =>
|
|
56
|
+
trpc.createClient({
|
|
57
|
+
url: 'http://localhost:5000/trpc',
|
|
58
|
+
}),
|
|
59
|
+
);
|
|
60
|
+
return (
|
|
61
|
+
<trpc.Provider client={trpcClient} queryClient={queryClient}>
|
|
62
|
+
<QueryClientProvider client={queryClient}>
|
|
63
|
+
{/* Your app here */}
|
|
64
|
+
</QueryClientProvider>
|
|
65
|
+
</trpc.Provider>
|
|
66
|
+
);
|
|
67
|
+
}
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
Now in any component, you can query your API using the proxy exported from the utils file.
|
|
71
|
+
|
|
72
|
+
```ts
|
|
73
|
+
import { proxy } from '~/utils/trpc';
|
|
74
|
+
|
|
75
|
+
export function Hello() {
|
|
76
|
+
const { data, error, status } = proxy.greeting.useQuery({ name: 'tRPC' });
|
|
77
|
+
|
|
78
|
+
if (error) {
|
|
79
|
+
return <p>{error.message}</p>;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
if (status !== 'success') {
|
|
83
|
+
return <p>Loading...</p>;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
return <div>{data && <p>{data.greeting}</p>}</div>;
|
|
87
|
+
}
|
|
88
|
+
```
|
|
@@ -0,0 +1,392 @@
|
|
|
1
|
+
import { createRecursiveProxy, createFlatProxy } from '@trpc/server/shared';
|
|
2
|
+
import { useQuery, useQueryClient, useMutation, hashQueryKey, useInfiniteQuery } from '@tanstack/react-query';
|
|
3
|
+
import { createTRPCClient } from '@trpc/client';
|
|
4
|
+
import React, { createContext, useState, useEffect, useCallback, useMemo, useRef } from 'react';
|
|
5
|
+
import { g as getArrayQueryKey } from './getArrayQueryKey-062214e0.mjs';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* We treat `undefined` as an input the same as omitting an `input`
|
|
9
|
+
* https://github.com/trpc/trpc/issues/2290
|
|
10
|
+
*/ function getQueryKey(path, input) {
|
|
11
|
+
return input === undefined ? [
|
|
12
|
+
path
|
|
13
|
+
] : [
|
|
14
|
+
path,
|
|
15
|
+
input
|
|
16
|
+
];
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Create proxy for decorating procedures
|
|
21
|
+
* @internal
|
|
22
|
+
*/ function createReactProxyDecoration(name, hooks) {
|
|
23
|
+
return createRecursiveProxy((opts)=>{
|
|
24
|
+
const args = opts.args;
|
|
25
|
+
const pathCopy = [
|
|
26
|
+
name,
|
|
27
|
+
...opts.path
|
|
28
|
+
];
|
|
29
|
+
// The last arg is for instance `.useMutation` or `.useQuery()`
|
|
30
|
+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
31
|
+
const lastArg = pathCopy.pop();
|
|
32
|
+
// The `path` ends up being something like `post.byId`
|
|
33
|
+
const path = pathCopy.join('.');
|
|
34
|
+
if (lastArg === 'useMutation') {
|
|
35
|
+
return hooks[lastArg](path, ...args);
|
|
36
|
+
}
|
|
37
|
+
const [input, ...rest] = args;
|
|
38
|
+
const queryKey = getQueryKey(path, input);
|
|
39
|
+
return hooks[lastArg](queryKey, ...rest);
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
const contextProps = [
|
|
44
|
+
'client',
|
|
45
|
+
'ssrContext',
|
|
46
|
+
'ssrState',
|
|
47
|
+
'abortOnUnmount'
|
|
48
|
+
];
|
|
49
|
+
const TRPCContext = /*#__PURE__*/ createContext(null);
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* @internal
|
|
53
|
+
*/ function createReactQueryUtilsProxy(context) {
|
|
54
|
+
return createFlatProxy((key)=>{
|
|
55
|
+
const contextName = key;
|
|
56
|
+
if (contextProps.includes(contextName)) {
|
|
57
|
+
return context[contextName];
|
|
58
|
+
}
|
|
59
|
+
return createRecursiveProxy(({ path , args })=>{
|
|
60
|
+
const pathCopy = [
|
|
61
|
+
key,
|
|
62
|
+
...path
|
|
63
|
+
];
|
|
64
|
+
const utilName = pathCopy.pop();
|
|
65
|
+
const fullPath = pathCopy.join('.');
|
|
66
|
+
const getOpts = (name)=>{
|
|
67
|
+
if ([
|
|
68
|
+
'setData',
|
|
69
|
+
'setInfiniteData'
|
|
70
|
+
].includes(name)) {
|
|
71
|
+
const [updater, input, ...rest] = args;
|
|
72
|
+
const queryKey = getQueryKey(fullPath, input);
|
|
73
|
+
return {
|
|
74
|
+
queryKey,
|
|
75
|
+
updater,
|
|
76
|
+
rest
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
const [input1, ...rest1] = args;
|
|
80
|
+
const queryKey1 = getQueryKey(fullPath, input1);
|
|
81
|
+
return {
|
|
82
|
+
queryKey: queryKey1,
|
|
83
|
+
rest: rest1
|
|
84
|
+
};
|
|
85
|
+
};
|
|
86
|
+
const { queryKey , rest , updater } = getOpts(utilName);
|
|
87
|
+
const contextMap = {
|
|
88
|
+
fetch: ()=>context.fetchQuery(queryKey, ...rest),
|
|
89
|
+
fetchInfinite: ()=>context.fetchInfiniteQuery(queryKey, ...rest),
|
|
90
|
+
prefetch: ()=>context.prefetchQuery(queryKey, ...rest),
|
|
91
|
+
prefetchInfinite: ()=>context.prefetchInfiniteQuery(queryKey, ...rest),
|
|
92
|
+
invalidate: ()=>context.invalidateQueries(queryKey, ...rest),
|
|
93
|
+
refetch: ()=>context.refetchQueries(queryKey, ...rest),
|
|
94
|
+
cancel: ()=>context.cancelQuery(queryKey, ...rest),
|
|
95
|
+
setData: ()=>context.setQueryData(queryKey, updater, ...rest),
|
|
96
|
+
setInfiniteData: ()=>context.setInfiniteQueryData(queryKey, updater, ...rest),
|
|
97
|
+
getData: ()=>context.getQueryData(queryKey),
|
|
98
|
+
getInfiniteData: ()=>context.getInfiniteQueryData(queryKey)
|
|
99
|
+
};
|
|
100
|
+
return contextMap[utilName]();
|
|
101
|
+
});
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
function getClientArgs(pathAndInput, opts) {
|
|
106
|
+
const [path, input] = pathAndInput;
|
|
107
|
+
return [
|
|
108
|
+
path,
|
|
109
|
+
input,
|
|
110
|
+
opts?.trpc
|
|
111
|
+
];
|
|
112
|
+
}
|
|
113
|
+
/**
|
|
114
|
+
* Makes a stable reference of the `trpc` prop
|
|
115
|
+
*/ function useHookResult(value) {
|
|
116
|
+
const ref = useRef(value);
|
|
117
|
+
ref.current.path = value.path;
|
|
118
|
+
return ref.current;
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* Create strongly typed react hooks
|
|
122
|
+
* @internal
|
|
123
|
+
*/ function createHooksInternal(config) {
|
|
124
|
+
const mutationSuccessOverride = config?.unstable_overrides?.useMutation?.onSuccess ?? ((options)=>options.originalFn());
|
|
125
|
+
const Context = TRPCContext;
|
|
126
|
+
const createClient = (opts)=>{
|
|
127
|
+
return createTRPCClient(opts);
|
|
128
|
+
};
|
|
129
|
+
const TRPCProvider = (props)=>{
|
|
130
|
+
const { abortOnUnmount =false , client , queryClient , ssrContext } = props;
|
|
131
|
+
const [ssrState, setSSRState] = useState(props.ssrState ?? false);
|
|
132
|
+
useEffect(()=>{
|
|
133
|
+
// Only updating state to `mounted` if we are using SSR.
|
|
134
|
+
// This makes it so we don't have an unnecessary re-render when opting out of SSR.
|
|
135
|
+
setSSRState((state)=>state ? 'mounted' : false);
|
|
136
|
+
}, []);
|
|
137
|
+
return /*#__PURE__*/ React.createElement(Context.Provider, {
|
|
138
|
+
value: {
|
|
139
|
+
abortOnUnmount,
|
|
140
|
+
queryClient,
|
|
141
|
+
client,
|
|
142
|
+
ssrContext: ssrContext || null,
|
|
143
|
+
ssrState,
|
|
144
|
+
fetchQuery: useCallback((pathAndInput, opts)=>{
|
|
145
|
+
return queryClient.fetchQuery(getArrayQueryKey(pathAndInput), ()=>client.query(...getClientArgs(pathAndInput, opts)), opts);
|
|
146
|
+
}, [
|
|
147
|
+
client,
|
|
148
|
+
queryClient
|
|
149
|
+
]),
|
|
150
|
+
fetchInfiniteQuery: useCallback((pathAndInput, opts)=>{
|
|
151
|
+
return queryClient.fetchInfiniteQuery(getArrayQueryKey(pathAndInput), ({ pageParam })=>{
|
|
152
|
+
const [path, input] = pathAndInput;
|
|
153
|
+
const actualInput = {
|
|
154
|
+
...input,
|
|
155
|
+
cursor: pageParam
|
|
156
|
+
};
|
|
157
|
+
return client.query(...getClientArgs([
|
|
158
|
+
path,
|
|
159
|
+
actualInput
|
|
160
|
+
], opts));
|
|
161
|
+
}, opts);
|
|
162
|
+
}, [
|
|
163
|
+
client,
|
|
164
|
+
queryClient
|
|
165
|
+
]),
|
|
166
|
+
prefetchQuery: useCallback((pathAndInput, opts)=>{
|
|
167
|
+
return queryClient.prefetchQuery(getArrayQueryKey(pathAndInput), ()=>client.query(...getClientArgs(pathAndInput, opts)), opts);
|
|
168
|
+
}, [
|
|
169
|
+
client,
|
|
170
|
+
queryClient
|
|
171
|
+
]),
|
|
172
|
+
prefetchInfiniteQuery: useCallback((pathAndInput, opts)=>{
|
|
173
|
+
return queryClient.prefetchInfiniteQuery(getArrayQueryKey(pathAndInput), ({ pageParam })=>{
|
|
174
|
+
const [path, input] = pathAndInput;
|
|
175
|
+
const actualInput = {
|
|
176
|
+
...input,
|
|
177
|
+
cursor: pageParam
|
|
178
|
+
};
|
|
179
|
+
return client.query(...getClientArgs([
|
|
180
|
+
path,
|
|
181
|
+
actualInput
|
|
182
|
+
], opts));
|
|
183
|
+
}, opts);
|
|
184
|
+
}, [
|
|
185
|
+
client,
|
|
186
|
+
queryClient
|
|
187
|
+
]),
|
|
188
|
+
invalidateQueries: useCallback((...args)=>{
|
|
189
|
+
const [queryKey, ...rest] = args;
|
|
190
|
+
return queryClient.invalidateQueries(getArrayQueryKey(queryKey), ...rest);
|
|
191
|
+
}, [
|
|
192
|
+
queryClient
|
|
193
|
+
]),
|
|
194
|
+
refetchQueries: useCallback((...args)=>{
|
|
195
|
+
const [queryKey, ...rest] = args;
|
|
196
|
+
return queryClient.refetchQueries(getArrayQueryKey(queryKey), ...rest);
|
|
197
|
+
}, [
|
|
198
|
+
queryClient
|
|
199
|
+
]),
|
|
200
|
+
cancelQuery: useCallback((pathAndInput)=>{
|
|
201
|
+
return queryClient.cancelQueries(getArrayQueryKey(pathAndInput));
|
|
202
|
+
}, [
|
|
203
|
+
queryClient
|
|
204
|
+
]),
|
|
205
|
+
setQueryData: useCallback((...args)=>{
|
|
206
|
+
const [queryKey, ...rest] = args;
|
|
207
|
+
return queryClient.setQueryData(getArrayQueryKey(queryKey), ...rest);
|
|
208
|
+
}, [
|
|
209
|
+
queryClient
|
|
210
|
+
]),
|
|
211
|
+
getQueryData: useCallback((...args)=>{
|
|
212
|
+
const [queryKey, ...rest] = args;
|
|
213
|
+
return queryClient.getQueryData(getArrayQueryKey(queryKey), ...rest);
|
|
214
|
+
}, [
|
|
215
|
+
queryClient
|
|
216
|
+
]),
|
|
217
|
+
setInfiniteQueryData: useCallback((...args)=>{
|
|
218
|
+
const [queryKey, ...rest] = args;
|
|
219
|
+
return queryClient.setQueryData(getArrayQueryKey(queryKey), ...rest);
|
|
220
|
+
}, [
|
|
221
|
+
queryClient
|
|
222
|
+
]),
|
|
223
|
+
getInfiniteQueryData: useCallback((...args)=>{
|
|
224
|
+
const [queryKey, ...rest] = args;
|
|
225
|
+
return queryClient.getQueryData(getArrayQueryKey(queryKey), ...rest);
|
|
226
|
+
}, [
|
|
227
|
+
queryClient
|
|
228
|
+
])
|
|
229
|
+
}
|
|
230
|
+
}, props.children);
|
|
231
|
+
};
|
|
232
|
+
function useContext() {
|
|
233
|
+
return React.useContext(Context);
|
|
234
|
+
}
|
|
235
|
+
/**
|
|
236
|
+
* Hack to make sure errors return `status`='error` when doing SSR
|
|
237
|
+
* @link https://github.com/trpc/trpc/pull/1645
|
|
238
|
+
*/ function useSSRQueryOptionsIfNeeded(pathAndInput, opts) {
|
|
239
|
+
const { queryClient , ssrState } = useContext();
|
|
240
|
+
return ssrState && ssrState !== 'mounted' && queryClient.getQueryCache().find(getArrayQueryKey(pathAndInput))?.state.status === 'error' ? {
|
|
241
|
+
retryOnMount: false,
|
|
242
|
+
...opts
|
|
243
|
+
} : opts;
|
|
244
|
+
}
|
|
245
|
+
function useQuery$1(pathAndInput, opts) {
|
|
246
|
+
const { abortOnUnmount , client , ssrState , queryClient , prefetchQuery } = useContext();
|
|
247
|
+
if (typeof window === 'undefined' && ssrState === 'prepass' && opts?.trpc?.ssr !== false && opts?.enabled !== false && !queryClient.getQueryCache().find(getArrayQueryKey(pathAndInput))) {
|
|
248
|
+
void prefetchQuery(pathAndInput, opts);
|
|
249
|
+
}
|
|
250
|
+
const ssrOpts = useSSRQueryOptionsIfNeeded(pathAndInput, opts);
|
|
251
|
+
// request option should take priority over global
|
|
252
|
+
const shouldAbortOnUnmount = opts?.trpc?.abortOnUnmount ?? abortOnUnmount;
|
|
253
|
+
const hook = useQuery(getArrayQueryKey(pathAndInput), (queryFunctionContext)=>{
|
|
254
|
+
const actualOpts = {
|
|
255
|
+
...ssrOpts,
|
|
256
|
+
trpc: {
|
|
257
|
+
...ssrOpts?.trpc,
|
|
258
|
+
...shouldAbortOnUnmount ? {
|
|
259
|
+
signal: queryFunctionContext.signal
|
|
260
|
+
} : {}
|
|
261
|
+
}
|
|
262
|
+
};
|
|
263
|
+
return client.query(...getClientArgs(pathAndInput, actualOpts));
|
|
264
|
+
}, ssrOpts);
|
|
265
|
+
hook.trpc = useHookResult({
|
|
266
|
+
path: pathAndInput[0]
|
|
267
|
+
});
|
|
268
|
+
return hook;
|
|
269
|
+
}
|
|
270
|
+
function useMutation$1(path, opts) {
|
|
271
|
+
const { client } = useContext();
|
|
272
|
+
const queryClient = useQueryClient();
|
|
273
|
+
const hook = useMutation((input)=>{
|
|
274
|
+
const actualPath = Array.isArray(path) ? path[0] : path;
|
|
275
|
+
return client.mutation(...getClientArgs([
|
|
276
|
+
actualPath,
|
|
277
|
+
input
|
|
278
|
+
], opts));
|
|
279
|
+
}, {
|
|
280
|
+
...opts,
|
|
281
|
+
onSuccess (...args) {
|
|
282
|
+
const originalFn = ()=>opts?.onSuccess?.(...args);
|
|
283
|
+
return mutationSuccessOverride({
|
|
284
|
+
originalFn,
|
|
285
|
+
queryClient
|
|
286
|
+
});
|
|
287
|
+
}
|
|
288
|
+
});
|
|
289
|
+
hook.trpc = useHookResult({
|
|
290
|
+
path: Array.isArray(path) ? path[0] : path
|
|
291
|
+
});
|
|
292
|
+
return hook;
|
|
293
|
+
}
|
|
294
|
+
/* istanbul ignore next */ /**
|
|
295
|
+
* ⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️
|
|
296
|
+
* **Experimental.** API might change without major version bump
|
|
297
|
+
* ⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠
|
|
298
|
+
*/ function useSubscription(pathAndInput, opts) {
|
|
299
|
+
const enabled = opts?.enabled ?? true;
|
|
300
|
+
const queryKey = hashQueryKey(pathAndInput);
|
|
301
|
+
const { client } = useContext();
|
|
302
|
+
return useEffect(()=>{
|
|
303
|
+
if (!enabled) {
|
|
304
|
+
return;
|
|
305
|
+
}
|
|
306
|
+
const [path, input] = pathAndInput;
|
|
307
|
+
let isStopped = false;
|
|
308
|
+
const subscription = client.subscription(path, input ?? undefined, {
|
|
309
|
+
onStarted: ()=>{
|
|
310
|
+
if (!isStopped) {
|
|
311
|
+
opts.onStarted?.();
|
|
312
|
+
}
|
|
313
|
+
},
|
|
314
|
+
onData: (data)=>{
|
|
315
|
+
if (!isStopped) {
|
|
316
|
+
opts.onData(data);
|
|
317
|
+
}
|
|
318
|
+
},
|
|
319
|
+
onError: (err)=>{
|
|
320
|
+
if (!isStopped) {
|
|
321
|
+
opts.onError?.(err);
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
});
|
|
325
|
+
return ()=>{
|
|
326
|
+
isStopped = true;
|
|
327
|
+
subscription.unsubscribe();
|
|
328
|
+
};
|
|
329
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
330
|
+
}, [
|
|
331
|
+
queryKey,
|
|
332
|
+
enabled
|
|
333
|
+
]);
|
|
334
|
+
}
|
|
335
|
+
function useInfiniteQuery$1(pathAndInput, opts) {
|
|
336
|
+
const [path, input] = pathAndInput;
|
|
337
|
+
const { client , ssrState , prefetchInfiniteQuery , queryClient , abortOnUnmount , } = useContext();
|
|
338
|
+
if (typeof window === 'undefined' && ssrState === 'prepass' && opts?.trpc?.ssr !== false && opts?.enabled !== false && !queryClient.getQueryCache().find(getArrayQueryKey(pathAndInput))) {
|
|
339
|
+
void prefetchInfiniteQuery(pathAndInput, opts);
|
|
340
|
+
}
|
|
341
|
+
const ssrOpts = useSSRQueryOptionsIfNeeded(pathAndInput, opts);
|
|
342
|
+
// request option should take priority over global
|
|
343
|
+
const shouldAbortOnUnmount = opts?.trpc?.abortOnUnmount ?? abortOnUnmount;
|
|
344
|
+
const hook = useInfiniteQuery(getArrayQueryKey(pathAndInput), (queryFunctionContext)=>{
|
|
345
|
+
const actualOpts = {
|
|
346
|
+
...ssrOpts,
|
|
347
|
+
trpc: {
|
|
348
|
+
...ssrOpts?.trpc,
|
|
349
|
+
...shouldAbortOnUnmount ? {
|
|
350
|
+
signal: queryFunctionContext.signal
|
|
351
|
+
} : {}
|
|
352
|
+
}
|
|
353
|
+
};
|
|
354
|
+
const actualInput = {
|
|
355
|
+
...input ?? {},
|
|
356
|
+
cursor: queryFunctionContext.pageParam
|
|
357
|
+
};
|
|
358
|
+
return client.query(...getClientArgs([
|
|
359
|
+
path,
|
|
360
|
+
actualInput
|
|
361
|
+
], actualOpts));
|
|
362
|
+
}, ssrOpts);
|
|
363
|
+
hook.trpc = useHookResult({
|
|
364
|
+
path
|
|
365
|
+
});
|
|
366
|
+
return hook;
|
|
367
|
+
}
|
|
368
|
+
const useDehydratedState = (client, trpcState)=>{
|
|
369
|
+
const transformed = useMemo(()=>{
|
|
370
|
+
if (!trpcState) {
|
|
371
|
+
return trpcState;
|
|
372
|
+
}
|
|
373
|
+
return client.runtime.transformer.deserialize(trpcState);
|
|
374
|
+
}, [
|
|
375
|
+
trpcState,
|
|
376
|
+
client
|
|
377
|
+
]);
|
|
378
|
+
return transformed;
|
|
379
|
+
};
|
|
380
|
+
return {
|
|
381
|
+
Provider: TRPCProvider,
|
|
382
|
+
createClient,
|
|
383
|
+
useContext,
|
|
384
|
+
useQuery: useQuery$1,
|
|
385
|
+
useMutation: useMutation$1,
|
|
386
|
+
useSubscription,
|
|
387
|
+
useDehydratedState,
|
|
388
|
+
useInfiniteQuery: useInfiniteQuery$1
|
|
389
|
+
};
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
export { createReactQueryUtilsProxy as a, createReactProxyDecoration as b, createHooksInternal as c };
|