@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
|
@@ -0,0 +1,266 @@
|
|
|
1
|
+
import {
|
|
2
|
+
CancelOptions,
|
|
3
|
+
InfiniteData,
|
|
4
|
+
InvalidateOptions,
|
|
5
|
+
InvalidateQueryFilters,
|
|
6
|
+
RefetchOptions,
|
|
7
|
+
RefetchQueryFilters,
|
|
8
|
+
SetDataOptions,
|
|
9
|
+
Updater,
|
|
10
|
+
} from '@tanstack/react-query';
|
|
11
|
+
import { TRPCClientError } from '@trpc/client';
|
|
12
|
+
import {
|
|
13
|
+
AnyQueryProcedure,
|
|
14
|
+
AnyRouter,
|
|
15
|
+
Filter,
|
|
16
|
+
ProcedureOptions,
|
|
17
|
+
inferProcedureInput,
|
|
18
|
+
inferProcedureOutput,
|
|
19
|
+
} from '@trpc/server';
|
|
20
|
+
import { createFlatProxy, createRecursiveProxy } from '@trpc/server/shared';
|
|
21
|
+
import {
|
|
22
|
+
ProxyTRPCContextProps,
|
|
23
|
+
TRPCContextState,
|
|
24
|
+
TRPCFetchInfiniteQueryOptions,
|
|
25
|
+
TRPCFetchQueryOptions,
|
|
26
|
+
contextProps,
|
|
27
|
+
} from '../../internals/context';
|
|
28
|
+
import { getQueryKey } from '../../internals/getQueryKey';
|
|
29
|
+
|
|
30
|
+
type DecorateProcedure<
|
|
31
|
+
TRouter extends AnyRouter,
|
|
32
|
+
TProcedure extends AnyQueryProcedure,
|
|
33
|
+
> = {
|
|
34
|
+
/**
|
|
35
|
+
* @link https://react-query.tanstack.com/guides/prefetching
|
|
36
|
+
*/
|
|
37
|
+
fetch(
|
|
38
|
+
input: inferProcedureInput<TProcedure>,
|
|
39
|
+
opts?: TRPCFetchQueryOptions<
|
|
40
|
+
inferProcedureInput<TProcedure>,
|
|
41
|
+
TRPCClientError<TRouter>,
|
|
42
|
+
inferProcedureOutput<TProcedure>
|
|
43
|
+
>,
|
|
44
|
+
): Promise<inferProcedureOutput<TProcedure>>;
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* @link https://react-query.tanstack.com/guides/prefetching
|
|
48
|
+
*/
|
|
49
|
+
fetchInfinite(
|
|
50
|
+
input: inferProcedureInput<TProcedure>,
|
|
51
|
+
opts?: TRPCFetchInfiniteQueryOptions<
|
|
52
|
+
inferProcedureInput<TProcedure>,
|
|
53
|
+
TRPCClientError<TRouter>,
|
|
54
|
+
inferProcedureOutput<TProcedure>
|
|
55
|
+
>,
|
|
56
|
+
): Promise<InfiniteData<inferProcedureOutput<TProcedure>>>;
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* @link https://react-query.tanstack.com/guides/prefetching
|
|
60
|
+
*/
|
|
61
|
+
prefetch(
|
|
62
|
+
input: inferProcedureInput<TProcedure>,
|
|
63
|
+
opts?: TRPCFetchQueryOptions<
|
|
64
|
+
inferProcedureInput<TProcedure>,
|
|
65
|
+
TRPCClientError<TRouter>,
|
|
66
|
+
inferProcedureOutput<TProcedure>
|
|
67
|
+
>,
|
|
68
|
+
): Promise<void>;
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* @link https://react-query.tanstack.com/guides/prefetching
|
|
72
|
+
*/
|
|
73
|
+
prefetchInfinite(
|
|
74
|
+
input: inferProcedureInput<TProcedure>,
|
|
75
|
+
procedureOpts?: ProcedureOptions,
|
|
76
|
+
opts?: TRPCFetchInfiniteQueryOptions<
|
|
77
|
+
inferProcedureInput<TProcedure>,
|
|
78
|
+
TRPCClientError<TRouter>,
|
|
79
|
+
inferProcedureOutput<TProcedure>
|
|
80
|
+
>,
|
|
81
|
+
): Promise<void>;
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* @link https://react-query.tanstack.com/guides/query-invalidation
|
|
85
|
+
*/
|
|
86
|
+
invalidate(
|
|
87
|
+
input?: inferProcedureInput<TProcedure>,
|
|
88
|
+
filters?: InvalidateQueryFilters,
|
|
89
|
+
options?: InvalidateOptions,
|
|
90
|
+
): Promise<void>;
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* @link https://react-query.tanstack.com/reference/QueryClient#queryclientrefetchqueries
|
|
94
|
+
*/
|
|
95
|
+
refetch(
|
|
96
|
+
input?: inferProcedureInput<TProcedure>,
|
|
97
|
+
filters?: RefetchQueryFilters,
|
|
98
|
+
options?: RefetchOptions,
|
|
99
|
+
): Promise<void>;
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* @link https://react-query.tanstack.com/guides/query-cancellation
|
|
103
|
+
*/
|
|
104
|
+
cancel(
|
|
105
|
+
input?: inferProcedureInput<TProcedure>,
|
|
106
|
+
options?: CancelOptions,
|
|
107
|
+
): Promise<void>;
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* @link https://react-query.tanstack.com/reference/QueryClient#queryclientsetquerydata
|
|
111
|
+
*/
|
|
112
|
+
setData(
|
|
113
|
+
updater: Updater<
|
|
114
|
+
inferProcedureOutput<TProcedure> | undefined,
|
|
115
|
+
inferProcedureOutput<TProcedure> | undefined
|
|
116
|
+
>,
|
|
117
|
+
input?: inferProcedureInput<TProcedure>,
|
|
118
|
+
options?: SetDataOptions,
|
|
119
|
+
): void;
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* @link https://react-query.tanstack.com/reference/QueryClient#queryclientgetquerydata
|
|
123
|
+
*/
|
|
124
|
+
setInfiniteData(
|
|
125
|
+
updater: Updater<
|
|
126
|
+
InfiniteData<inferProcedureOutput<TProcedure>> | undefined,
|
|
127
|
+
InfiniteData<inferProcedureOutput<TProcedure>> | undefined
|
|
128
|
+
>,
|
|
129
|
+
input?: inferProcedureInput<TProcedure>,
|
|
130
|
+
options?: SetDataOptions,
|
|
131
|
+
): void;
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* @link https://react-query.tanstack.com/reference/QueryClient#queryclientgetquerydata
|
|
135
|
+
*/
|
|
136
|
+
getData(
|
|
137
|
+
input?: inferProcedureInput<TProcedure>,
|
|
138
|
+
): inferProcedureOutput<TProcedure> | undefined;
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* @link https://react-query.tanstack.com/reference/QueryClient#queryclientgetquerydata
|
|
142
|
+
*/
|
|
143
|
+
getInfiniteData(
|
|
144
|
+
input?: inferProcedureInput<TProcedure>,
|
|
145
|
+
): InfiniteData<inferProcedureOutput<TProcedure>> | undefined;
|
|
146
|
+
};
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* A type that will traverse all procedures and sub routers of a given router to create a union of
|
|
150
|
+
* their possible input types
|
|
151
|
+
*/
|
|
152
|
+
type InferAllRouterQueryInputTypes<TRouter extends AnyRouter> = {
|
|
153
|
+
[TKey in keyof Filter<
|
|
154
|
+
TRouter['_def']['record'],
|
|
155
|
+
AnyRouter | AnyQueryProcedure
|
|
156
|
+
>]: TRouter['_def']['record'][TKey] extends AnyQueryProcedure
|
|
157
|
+
? inferProcedureInput<TRouter['_def']['record'][TKey]>
|
|
158
|
+
: InferAllRouterQueryInputTypes<TRouter['_def']['record'][TKey]>; // Recurse as we have a sub router!
|
|
159
|
+
}[keyof Filter<TRouter['_def']['record'], AnyRouter | AnyQueryProcedure>]; // This flattens results into a big union
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* this is the type that is used to add in procedures that can be used on
|
|
163
|
+
* an entire router
|
|
164
|
+
*/
|
|
165
|
+
type DecorateRouterProcedure<TRouter extends AnyRouter> = {
|
|
166
|
+
/**
|
|
167
|
+
* @link https://react-query.tanstack.com/guides/query-invalidation
|
|
168
|
+
*/
|
|
169
|
+
invalidate(
|
|
170
|
+
input?: Partial<InferAllRouterQueryInputTypes<TRouter>>,
|
|
171
|
+
filters?: InvalidateQueryFilters,
|
|
172
|
+
options?: InvalidateOptions,
|
|
173
|
+
): Promise<void>;
|
|
174
|
+
};
|
|
175
|
+
|
|
176
|
+
/**
|
|
177
|
+
* @internal
|
|
178
|
+
*/
|
|
179
|
+
export type DecoratedProcedureUtilsRecord<TRouter extends AnyRouter> = {
|
|
180
|
+
[TKey in keyof Filter<
|
|
181
|
+
TRouter['_def']['record'],
|
|
182
|
+
AnyRouter | AnyQueryProcedure
|
|
183
|
+
>]: TRouter['_def']['record'][TKey] extends AnyRouter
|
|
184
|
+
? DecoratedProcedureUtilsRecord<TRouter['_def']['record'][TKey]> &
|
|
185
|
+
DecorateRouterProcedure<TRouter['_def']['record'][TKey]>
|
|
186
|
+
: // utils only apply to queries
|
|
187
|
+
DecorateProcedure<TRouter, TRouter['_def']['record'][TKey]>;
|
|
188
|
+
} & DecorateRouterProcedure<TRouter>; // Add functions that should be available at utils root
|
|
189
|
+
|
|
190
|
+
type AnyDecoratedProcedure = DecorateProcedure<any, any>;
|
|
191
|
+
|
|
192
|
+
export type CreateReactUtilsProxy<
|
|
193
|
+
TRouter extends AnyRouter,
|
|
194
|
+
TSSRContext,
|
|
195
|
+
> = DecoratedProcedureUtilsRecord<TRouter> &
|
|
196
|
+
ProxyTRPCContextProps<TRouter, TSSRContext>;
|
|
197
|
+
|
|
198
|
+
/**
|
|
199
|
+
* @internal
|
|
200
|
+
*/
|
|
201
|
+
export function createReactQueryUtilsProxy<
|
|
202
|
+
TRouter extends AnyRouter,
|
|
203
|
+
TSSRContext,
|
|
204
|
+
>(context: TRPCContextState<AnyRouter, unknown>) {
|
|
205
|
+
type CreateReactUtilsProxyReturnType = CreateReactUtilsProxy<
|
|
206
|
+
TRouter,
|
|
207
|
+
TSSRContext
|
|
208
|
+
>;
|
|
209
|
+
|
|
210
|
+
return createFlatProxy<CreateReactUtilsProxyReturnType>((key) => {
|
|
211
|
+
const contextName = key as typeof contextProps[number];
|
|
212
|
+
if (contextProps.includes(contextName)) {
|
|
213
|
+
return context[contextName];
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
return createRecursiveProxy(({ path, args }) => {
|
|
217
|
+
const pathCopy = [key, ...path];
|
|
218
|
+
const utilName = pathCopy.pop() as keyof AnyDecoratedProcedure;
|
|
219
|
+
|
|
220
|
+
const fullPath = pathCopy.join('.');
|
|
221
|
+
|
|
222
|
+
const getOpts = (name: typeof utilName) => {
|
|
223
|
+
if (['setData', 'setInfiniteData'].includes(name)) {
|
|
224
|
+
const [updater, input, ...rest] = args as Parameters<
|
|
225
|
+
AnyDecoratedProcedure[typeof utilName]
|
|
226
|
+
>;
|
|
227
|
+
const queryKey = getQueryKey(fullPath, input);
|
|
228
|
+
return {
|
|
229
|
+
queryKey,
|
|
230
|
+
updater,
|
|
231
|
+
rest,
|
|
232
|
+
};
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
const [input, ...rest] = args as Parameters<
|
|
236
|
+
AnyDecoratedProcedure[typeof utilName]
|
|
237
|
+
>;
|
|
238
|
+
const queryKey = getQueryKey(fullPath, input);
|
|
239
|
+
return {
|
|
240
|
+
queryKey,
|
|
241
|
+
rest,
|
|
242
|
+
};
|
|
243
|
+
};
|
|
244
|
+
|
|
245
|
+
const { queryKey, rest, updater } = getOpts(utilName);
|
|
246
|
+
|
|
247
|
+
const contextMap: Record<keyof AnyDecoratedProcedure, () => unknown> = {
|
|
248
|
+
fetch: () => context.fetchQuery(queryKey, ...rest),
|
|
249
|
+
fetchInfinite: () => context.fetchInfiniteQuery(queryKey, ...rest),
|
|
250
|
+
prefetch: () => context.prefetchQuery(queryKey, ...rest),
|
|
251
|
+
prefetchInfinite: () =>
|
|
252
|
+
context.prefetchInfiniteQuery(queryKey, ...rest),
|
|
253
|
+
invalidate: () => context.invalidateQueries(queryKey, ...rest),
|
|
254
|
+
refetch: () => context.refetchQueries(queryKey, ...rest),
|
|
255
|
+
cancel: () => context.cancelQuery(queryKey, ...rest),
|
|
256
|
+
setData: () => context.setQueryData(queryKey, updater, ...rest),
|
|
257
|
+
setInfiniteData: () =>
|
|
258
|
+
context.setInfiniteQueryData(queryKey, updater, ...rest),
|
|
259
|
+
getData: () => context.getQueryData(queryKey),
|
|
260
|
+
getInfiniteData: () => context.getInfiniteQueryData(queryKey),
|
|
261
|
+
};
|
|
262
|
+
|
|
263
|
+
return contextMap[utilName]();
|
|
264
|
+
});
|
|
265
|
+
});
|
|
266
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { QueryClient, QueryClientConfig } from '@tanstack/react-query';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @internal
|
|
5
|
+
*/
|
|
6
|
+
export type CreateTRPCReactQueryClientConfig =
|
|
7
|
+
| {
|
|
8
|
+
queryClient?: QueryClient;
|
|
9
|
+
queryClientConfig?: never;
|
|
10
|
+
}
|
|
11
|
+
| {
|
|
12
|
+
queryClientConfig?: QueryClientConfig;
|
|
13
|
+
queryClient?: never;
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* @internal
|
|
18
|
+
*/
|
|
19
|
+
export const getQueryClient = (config: CreateTRPCReactQueryClientConfig) =>
|
|
20
|
+
config.queryClient ?? new QueryClient(config.queryClientConfig);
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { QueryClient } from '@tanstack/react-query';
|
|
2
|
+
import { AnyRouter, MaybePromise } from '@trpc/server';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* @internal
|
|
6
|
+
*/
|
|
7
|
+
export interface UseMutationOverride {
|
|
8
|
+
onSuccess: (opts: {
|
|
9
|
+
/**
|
|
10
|
+
* Calls the original function that was defined in the query's `onSuccess` option
|
|
11
|
+
*/
|
|
12
|
+
originalFn: () => MaybePromise<unknown>;
|
|
13
|
+
queryClient: QueryClient;
|
|
14
|
+
}) => MaybePromise<unknown>;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* @internal
|
|
19
|
+
*/
|
|
20
|
+
export interface CreateTRPCReactOptions<_TRouter extends AnyRouter> {
|
|
21
|
+
/**
|
|
22
|
+
* Override behaviors of the built-in hooks
|
|
23
|
+
*/
|
|
24
|
+
unstable_overrides?: {
|
|
25
|
+
useMutation?: Partial<UseMutationOverride>;
|
|
26
|
+
};
|
|
27
|
+
}
|
package/src/ssg/index.ts
ADDED
package/src/ssg/ssg.ts
ADDED
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
import {
|
|
2
|
+
DehydrateOptions,
|
|
3
|
+
DehydratedState,
|
|
4
|
+
InfiniteData,
|
|
5
|
+
dehydrate,
|
|
6
|
+
} from '@tanstack/react-query';
|
|
7
|
+
import {
|
|
8
|
+
AnyRouter,
|
|
9
|
+
ClientDataTransformerOptions,
|
|
10
|
+
callProcedure,
|
|
11
|
+
inferHandlerInput,
|
|
12
|
+
inferProcedureOutput,
|
|
13
|
+
inferRouterContext,
|
|
14
|
+
} from '@trpc/server';
|
|
15
|
+
import { getArrayQueryKey } from '../internals/getArrayQueryKey';
|
|
16
|
+
import { CreateTRPCReactQueryClientConfig, getQueryClient } from '../shared';
|
|
17
|
+
|
|
18
|
+
interface CreateSSGHelpersOptionsBase<TRouter extends AnyRouter> {
|
|
19
|
+
router: TRouter;
|
|
20
|
+
ctx: inferRouterContext<TRouter>;
|
|
21
|
+
transformer?: ClientDataTransformerOptions;
|
|
22
|
+
}
|
|
23
|
+
export type CreateSSGHelpersOptions<TRouter extends AnyRouter> =
|
|
24
|
+
CreateSSGHelpersOptionsBase<TRouter> & CreateTRPCReactQueryClientConfig;
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Create functions you can use for server-side rendering / static generation
|
|
28
|
+
* @deprecated use `createProxySSGHelpers` instead
|
|
29
|
+
*/
|
|
30
|
+
export function createSSGHelpers<TRouter extends AnyRouter>(
|
|
31
|
+
opts: CreateSSGHelpersOptions<TRouter>,
|
|
32
|
+
) {
|
|
33
|
+
const { router, transformer, ctx } = opts;
|
|
34
|
+
type TQueries = TRouter['_def']['queries'];
|
|
35
|
+
const queryClient = getQueryClient(opts);
|
|
36
|
+
|
|
37
|
+
const serialize = transformer
|
|
38
|
+
? ('input' in transformer ? transformer.input : transformer).serialize
|
|
39
|
+
: (obj: unknown) => obj;
|
|
40
|
+
|
|
41
|
+
const prefetchQuery = async <
|
|
42
|
+
TPath extends keyof TQueries & string,
|
|
43
|
+
TProcedure extends TQueries[TPath],
|
|
44
|
+
>(
|
|
45
|
+
...pathAndInput: [path: TPath, ...args: inferHandlerInput<TProcedure>]
|
|
46
|
+
) => {
|
|
47
|
+
return queryClient.prefetchQuery(getArrayQueryKey(pathAndInput), () => {
|
|
48
|
+
return callProcedure({
|
|
49
|
+
procedures: router._def.procedures,
|
|
50
|
+
path: pathAndInput[0],
|
|
51
|
+
rawInput: pathAndInput[1],
|
|
52
|
+
ctx,
|
|
53
|
+
type: 'query',
|
|
54
|
+
});
|
|
55
|
+
});
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
const prefetchInfiniteQuery = async <
|
|
59
|
+
TPath extends keyof TQueries & string,
|
|
60
|
+
TProcedure extends TQueries[TPath],
|
|
61
|
+
>(
|
|
62
|
+
...pathAndInput: [path: TPath, ...args: inferHandlerInput<TProcedure>]
|
|
63
|
+
) => {
|
|
64
|
+
return queryClient.prefetchInfiniteQuery(
|
|
65
|
+
getArrayQueryKey(pathAndInput),
|
|
66
|
+
() => {
|
|
67
|
+
return callProcedure({
|
|
68
|
+
procedures: router._def.procedures,
|
|
69
|
+
path: pathAndInput[0],
|
|
70
|
+
rawInput: pathAndInput[1],
|
|
71
|
+
ctx,
|
|
72
|
+
type: 'query',
|
|
73
|
+
});
|
|
74
|
+
},
|
|
75
|
+
);
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
const fetchQuery = async <
|
|
79
|
+
TPath extends keyof TQueries & string,
|
|
80
|
+
TProcedure extends TQueries[TPath],
|
|
81
|
+
TOutput extends inferProcedureOutput<TProcedure>,
|
|
82
|
+
>(
|
|
83
|
+
...pathAndInput: [path: TPath, ...args: inferHandlerInput<TProcedure>]
|
|
84
|
+
): Promise<TOutput> => {
|
|
85
|
+
return queryClient.fetchQuery(getArrayQueryKey(pathAndInput), () => {
|
|
86
|
+
return callProcedure({
|
|
87
|
+
procedures: router._def.procedures,
|
|
88
|
+
path: pathAndInput[0],
|
|
89
|
+
rawInput: pathAndInput[1],
|
|
90
|
+
ctx,
|
|
91
|
+
type: 'query',
|
|
92
|
+
});
|
|
93
|
+
});
|
|
94
|
+
};
|
|
95
|
+
|
|
96
|
+
const fetchInfiniteQuery = async <
|
|
97
|
+
TPath extends keyof TQueries & string,
|
|
98
|
+
TProcedure extends TQueries[TPath],
|
|
99
|
+
TOutput extends inferProcedureOutput<TProcedure>,
|
|
100
|
+
>(
|
|
101
|
+
...pathAndInput: [path: TPath, ...args: inferHandlerInput<TProcedure>]
|
|
102
|
+
): Promise<InfiniteData<TOutput>> => {
|
|
103
|
+
return queryClient.fetchInfiniteQuery(
|
|
104
|
+
getArrayQueryKey(pathAndInput),
|
|
105
|
+
() => {
|
|
106
|
+
return callProcedure({
|
|
107
|
+
procedures: router._def.procedures,
|
|
108
|
+
path: pathAndInput[0],
|
|
109
|
+
rawInput: pathAndInput[1],
|
|
110
|
+
ctx,
|
|
111
|
+
type: 'query',
|
|
112
|
+
});
|
|
113
|
+
},
|
|
114
|
+
);
|
|
115
|
+
};
|
|
116
|
+
|
|
117
|
+
function _dehydrate(
|
|
118
|
+
opts: DehydrateOptions = {
|
|
119
|
+
shouldDehydrateQuery() {
|
|
120
|
+
// makes sure to serialize errors
|
|
121
|
+
return true;
|
|
122
|
+
},
|
|
123
|
+
},
|
|
124
|
+
): DehydratedState {
|
|
125
|
+
const before = dehydrate(queryClient, opts);
|
|
126
|
+
const after = serialize(before);
|
|
127
|
+
return after;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
return {
|
|
131
|
+
prefetchQuery,
|
|
132
|
+
prefetchInfiniteQuery,
|
|
133
|
+
fetchQuery,
|
|
134
|
+
fetchInfiniteQuery,
|
|
135
|
+
dehydrate: _dehydrate,
|
|
136
|
+
queryClient,
|
|
137
|
+
};
|
|
138
|
+
}
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import {
|
|
2
|
+
DehydrateOptions,
|
|
3
|
+
DehydratedState,
|
|
4
|
+
InfiniteData,
|
|
5
|
+
QueryClient,
|
|
6
|
+
} from '@tanstack/react-query';
|
|
7
|
+
import {
|
|
8
|
+
AnyProcedure,
|
|
9
|
+
AnyQueryProcedure,
|
|
10
|
+
AnyRouter,
|
|
11
|
+
Filter,
|
|
12
|
+
inferHandlerInput,
|
|
13
|
+
inferProcedureOutput,
|
|
14
|
+
} from '@trpc/server';
|
|
15
|
+
import { createFlatProxy, createRecursiveProxy } from '@trpc/server/shared';
|
|
16
|
+
import { CreateSSGHelpersOptions, createSSGHelpers } from './ssg';
|
|
17
|
+
|
|
18
|
+
type DecorateProcedure<TProcedure extends AnyProcedure> = {
|
|
19
|
+
/**
|
|
20
|
+
* @link https://react-query.tanstack.com/guides/prefetching
|
|
21
|
+
*/
|
|
22
|
+
fetch(
|
|
23
|
+
...args: inferHandlerInput<TProcedure>
|
|
24
|
+
): Promise<inferProcedureOutput<TProcedure>>;
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* @link https://react-query.tanstack.com/guides/prefetching
|
|
28
|
+
*/
|
|
29
|
+
fetchInfinite(
|
|
30
|
+
...args: inferHandlerInput<TProcedure>
|
|
31
|
+
): Promise<InfiniteData<inferProcedureOutput<TProcedure>>>;
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* @link https://react-query.tanstack.com/guides/prefetching
|
|
35
|
+
*/
|
|
36
|
+
prefetch(...args: inferHandlerInput<TProcedure>): Promise<void>;
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* @link https://react-query.tanstack.com/guides/prefetching
|
|
40
|
+
*/
|
|
41
|
+
prefetchInfinite(...args: inferHandlerInput<TProcedure>): Promise<void>;
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* @internal
|
|
46
|
+
*/
|
|
47
|
+
export type DecoratedProcedureSSGRecord<TRouter extends AnyRouter> = {
|
|
48
|
+
[TKey in keyof Filter<
|
|
49
|
+
TRouter['_def']['record'],
|
|
50
|
+
AnyRouter | AnyQueryProcedure
|
|
51
|
+
>]: TRouter['_def']['record'][TKey] extends AnyRouter
|
|
52
|
+
? DecoratedProcedureSSGRecord<TRouter['_def']['record'][TKey]>
|
|
53
|
+
: // utils only apply to queries
|
|
54
|
+
DecorateProcedure<TRouter['_def']['record'][TKey]>;
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
type AnyDecoratedProcedure = DecorateProcedure<any>;
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Create functions you can use for server-side rendering / static generation
|
|
61
|
+
*/
|
|
62
|
+
export function createProxySSGHelpers<TRouter extends AnyRouter>(
|
|
63
|
+
opts: CreateSSGHelpersOptions<TRouter>,
|
|
64
|
+
) {
|
|
65
|
+
const helpers = createSSGHelpers(opts);
|
|
66
|
+
|
|
67
|
+
type CreateProxySSGHelpers = {
|
|
68
|
+
queryClient: QueryClient;
|
|
69
|
+
dehydrate: (opts?: DehydrateOptions) => DehydratedState;
|
|
70
|
+
} & DecoratedProcedureSSGRecord<TRouter>;
|
|
71
|
+
|
|
72
|
+
return createFlatProxy<CreateProxySSGHelpers>((key) => {
|
|
73
|
+
if (key === 'queryClient') {
|
|
74
|
+
return helpers.queryClient;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
if (key === 'dehydrate') {
|
|
78
|
+
return helpers.dehydrate;
|
|
79
|
+
}
|
|
80
|
+
return createRecursiveProxy((opts) => {
|
|
81
|
+
const args = opts.args;
|
|
82
|
+
|
|
83
|
+
const pathCopy = [key, ...opts.path];
|
|
84
|
+
|
|
85
|
+
const utilName = pathCopy.pop() as keyof AnyDecoratedProcedure;
|
|
86
|
+
|
|
87
|
+
const fullPath = pathCopy.join('.');
|
|
88
|
+
|
|
89
|
+
switch (utilName) {
|
|
90
|
+
case 'fetch': {
|
|
91
|
+
return helpers.fetchQuery(fullPath, ...(args as any));
|
|
92
|
+
}
|
|
93
|
+
case 'fetchInfinite': {
|
|
94
|
+
return helpers.fetchInfiniteQuery(fullPath, ...(args as any));
|
|
95
|
+
}
|
|
96
|
+
case 'prefetch': {
|
|
97
|
+
return helpers.prefetchQuery(fullPath, ...(args as any));
|
|
98
|
+
}
|
|
99
|
+
case 'prefetchInfinite': {
|
|
100
|
+
return helpers.prefetchInfiniteQuery(fullPath, ...(args as any));
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
});
|
|
104
|
+
});
|
|
105
|
+
}
|
package/ssg/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from '../dist/ssg';
|
package/ssg/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
module.exports = require('../dist/ssg');
|