@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.
Files changed (65) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +88 -0
  3. package/dist/createHooksInternal-b47ee704.mjs +392 -0
  4. package/dist/createHooksInternal-e8214d72.js +400 -0
  5. package/dist/createTRPCReact.d.ts +38 -0
  6. package/dist/createTRPCReact.d.ts.map +1 -0
  7. package/dist/getArrayQueryKey-062214e0.mjs +19 -0
  8. package/dist/getArrayQueryKey-a2092b3c.js +21 -0
  9. package/dist/index.d.ts +4 -0
  10. package/dist/index.d.ts.map +1 -0
  11. package/dist/index.js +58 -0
  12. package/dist/index.mjs +47 -0
  13. package/dist/internals/context.d.ts +99 -0
  14. package/dist/internals/context.d.ts.map +1 -0
  15. package/dist/internals/getArrayQueryKey.d.ts +9 -0
  16. package/dist/internals/getArrayQueryKey.d.ts.map +1 -0
  17. package/dist/internals/getQueryKey.d.ts +6 -0
  18. package/dist/internals/getQueryKey.d.ts.map +1 -0
  19. package/dist/interop.d.ts +10 -0
  20. package/dist/interop.d.ts.map +1 -0
  21. package/dist/queryClient-0569f6e0.mjs +7 -0
  22. package/dist/queryClient-83576230.js +9 -0
  23. package/dist/shared/hooks/createHooksInternal.d.ts +115 -0
  24. package/dist/shared/hooks/createHooksInternal.d.ts.map +1 -0
  25. package/dist/shared/index.d.ts +6 -0
  26. package/dist/shared/index.d.ts.map +1 -0
  27. package/dist/shared/index.js +18 -0
  28. package/dist/shared/index.mjs +7 -0
  29. package/dist/shared/proxy/decorationProxy.d.ts +8 -0
  30. package/dist/shared/proxy/decorationProxy.d.ts.map +1 -0
  31. package/dist/shared/proxy/utilsProxy.d.ts +80 -0
  32. package/dist/shared/proxy/utilsProxy.d.ts.map +1 -0
  33. package/dist/shared/queryClient.d.ts +16 -0
  34. package/dist/shared/queryClient.d.ts.map +1 -0
  35. package/dist/shared/types.d.ts +26 -0
  36. package/dist/shared/types.d.ts.map +1 -0
  37. package/dist/ssg/index.d.ts +5 -0
  38. package/dist/ssg/index.d.ts.map +1 -0
  39. package/dist/ssg/index.js +126 -0
  40. package/dist/ssg/index.mjs +121 -0
  41. package/dist/ssg/ssg.d.ts +23 -0
  42. package/dist/ssg/ssg.d.ts.map +1 -0
  43. package/dist/ssg/ssgProxy.d.ts +36 -0
  44. package/dist/ssg/ssgProxy.d.ts.map +1 -0
  45. package/package.json +75 -0
  46. package/shared/index.d.ts +1 -0
  47. package/shared/index.js +1 -0
  48. package/src/createTRPCReact.tsx +164 -0
  49. package/src/index.ts +4 -0
  50. package/src/internals/context.tsx +235 -0
  51. package/src/internals/getArrayQueryKey.test.ts +41 -0
  52. package/src/internals/getArrayQueryKey.ts +18 -0
  53. package/src/internals/getQueryKey.ts +10 -0
  54. package/src/interop.ts +25 -0
  55. package/src/shared/hooks/createHooksInternal.tsx +664 -0
  56. package/src/shared/index.ts +8 -0
  57. package/src/shared/proxy/decorationProxy.ts +33 -0
  58. package/src/shared/proxy/utilsProxy.ts +266 -0
  59. package/src/shared/queryClient.ts +20 -0
  60. package/src/shared/types.ts +27 -0
  61. package/src/ssg/index.ts +4 -0
  62. package/src/ssg/ssg.ts +138 -0
  63. package/src/ssg/ssgProxy.ts +105 -0
  64. package/ssg/index.d.ts +1 -0
  65. package/ssg/index.js +1 -0
@@ -0,0 +1,235 @@
1
+ import {
2
+ CancelOptions,
3
+ FetchInfiniteQueryOptions,
4
+ FetchQueryOptions,
5
+ InfiniteData,
6
+ InvalidateOptions,
7
+ InvalidateQueryFilters,
8
+ QueryClient,
9
+ RefetchOptions,
10
+ RefetchQueryFilters,
11
+ SetDataOptions,
12
+ Updater,
13
+ } from '@tanstack/react-query';
14
+ import { TRPCClient, TRPCClientError, TRPCRequestOptions } from '@trpc/client';
15
+ import type {
16
+ AnyRouter,
17
+ inferHandlerInput,
18
+ inferProcedureInput,
19
+ inferProcedureOutput,
20
+ } from '@trpc/server';
21
+ import { createContext } from 'react';
22
+
23
+ export interface TRPCFetchQueryOptions<TInput, TError, TOutput>
24
+ extends FetchQueryOptions<TInput, TError, TOutput>,
25
+ TRPCRequestOptions {}
26
+
27
+ export interface TRPCFetchInfiniteQueryOptions<TInput, TError, TOutput>
28
+ extends FetchInfiniteQueryOptions<TInput, TError, TOutput>,
29
+ TRPCRequestOptions {}
30
+
31
+ /** @internal */
32
+ export type SSRState = false | 'prepass' | 'mounting' | 'mounted';
33
+
34
+ export interface ProxyTRPCContextProps<TRouter extends AnyRouter, TSSRContext> {
35
+ /**
36
+ * The `TRPCClient`
37
+ */
38
+ client: TRPCClient<TRouter>;
39
+ /**
40
+ * The SSR context when server-side rendering
41
+ * @default null
42
+ */
43
+ ssrContext?: TSSRContext | null;
44
+ /**
45
+ * State of SSR hydration.
46
+ * - `false` if not using SSR.
47
+ * - `prepass` when doing a prepass to fetch queries' data
48
+ * - `mounting` before TRPCProvider has been rendered on the client
49
+ * - `mounted` when the TRPCProvider has been rendered on the client
50
+ * @default false
51
+ */
52
+ ssrState?: SSRState;
53
+ /**
54
+ * Abort loading query calls when unmounting a component - usually when navigating to a new page
55
+ * @default false
56
+ */
57
+ abortOnUnmount?: boolean;
58
+ }
59
+
60
+ export interface TRPCContextProps<TRouter extends AnyRouter, TSSRContext>
61
+ extends ProxyTRPCContextProps<TRouter, TSSRContext> {
62
+ /**
63
+ * The react-query `QueryClient`
64
+ */
65
+ queryClient: QueryClient;
66
+ }
67
+
68
+ export const contextProps: (keyof ProxyTRPCContextProps<any, any>)[] = [
69
+ 'client',
70
+ 'ssrContext',
71
+ 'ssrState',
72
+ 'abortOnUnmount',
73
+ ];
74
+
75
+ /** @internal */
76
+ export interface TRPCContextState<
77
+ TRouter extends AnyRouter,
78
+ TSSRContext = undefined,
79
+ > extends Required<TRPCContextProps<TRouter, TSSRContext>> {
80
+ /**
81
+ * @link https://react-query.tanstack.com/guides/prefetching
82
+ */
83
+ fetchQuery<
84
+ TPath extends keyof TRouter['_def']['queries'] & string,
85
+ TProcedure extends TRouter['_def']['queries'][TPath],
86
+ TOutput extends inferProcedureOutput<TProcedure>,
87
+ TInput extends inferProcedureInput<TProcedure>,
88
+ >(
89
+ pathAndInput: [path: TPath, ...args: inferHandlerInput<TProcedure>],
90
+ opts?: TRPCFetchQueryOptions<TInput, TRPCClientError<TRouter>, TOutput>,
91
+ ): Promise<TOutput>;
92
+ /**
93
+ * @link https://react-query.tanstack.com/guides/prefetching
94
+ */
95
+ fetchInfiniteQuery<
96
+ TPath extends keyof TRouter['_def']['queries'] & string,
97
+ TProcedure extends TRouter['_def']['queries'][TPath],
98
+ TOutput extends inferProcedureOutput<TProcedure>,
99
+ TInput extends inferProcedureInput<TProcedure>,
100
+ >(
101
+ pathAndInput: [path: TPath, ...args: inferHandlerInput<TProcedure>],
102
+ opts?: TRPCFetchInfiniteQueryOptions<
103
+ TInput,
104
+ TRPCClientError<TRouter>,
105
+ TOutput
106
+ >,
107
+ ): Promise<InfiniteData<TOutput>>;
108
+ /**
109
+ * @link https://react-query.tanstack.com/guides/prefetching
110
+ */
111
+ prefetchQuery<
112
+ TPath extends keyof TRouter['_def']['queries'] & string,
113
+ TProcedure extends TRouter['_def']['queries'][TPath],
114
+ TOutput extends inferProcedureOutput<TProcedure>,
115
+ TInput extends inferProcedureInput<TProcedure>,
116
+ >(
117
+ pathAndInput: [path: TPath, ...args: inferHandlerInput<TProcedure>],
118
+ opts?: TRPCFetchQueryOptions<TInput, TRPCClientError<TRouter>, TOutput>,
119
+ ): Promise<void>;
120
+
121
+ /**
122
+ * @link https://react-query.tanstack.com/guides/prefetching
123
+ */
124
+ prefetchInfiniteQuery<
125
+ TPath extends keyof TRouter['_def']['queries'] & string,
126
+ TProcedure extends TRouter['_def']['queries'][TPath],
127
+ TOutput extends inferProcedureOutput<TProcedure>,
128
+ TInput extends inferProcedureInput<TProcedure>,
129
+ >(
130
+ pathAndInput: [path: TPath, ...args: inferHandlerInput<TProcedure>],
131
+ opts?: TRPCFetchInfiniteQueryOptions<
132
+ TInput,
133
+ TRPCClientError<TRouter>,
134
+ TOutput
135
+ >,
136
+ ): Promise<void>;
137
+
138
+ /**
139
+ * @link https://react-query.tanstack.com/guides/query-invalidation
140
+ */
141
+ invalidateQueries<
142
+ TPath extends keyof TRouter['_def']['queries'] & string,
143
+ TInput extends inferProcedureInput<TRouter['_def']['queries'][TPath]>,
144
+ >(
145
+ pathAndInput?: [TPath, TInput?] | TPath,
146
+ filters?: InvalidateQueryFilters,
147
+ options?: InvalidateOptions,
148
+ ): Promise<void>;
149
+ /**
150
+ * @link https://react-query.tanstack.com/guides/query-invalidation
151
+ */
152
+ invalidateQueries(
153
+ filters?: InvalidateQueryFilters,
154
+ options?: InvalidateOptions,
155
+ ): Promise<void>;
156
+
157
+ /**
158
+ * @link https://react-query.tanstack.com/reference/QueryClient#queryclientrefetchqueries
159
+ */
160
+ refetchQueries<
161
+ TPath extends keyof TRouter['_def']['queries'] & string,
162
+ TInput extends inferProcedureInput<TRouter['_def']['queries'][TPath]>,
163
+ >(
164
+ pathAndInput: [TPath, TInput?],
165
+ filters?: RefetchQueryFilters,
166
+ options?: RefetchOptions,
167
+ ): Promise<void>;
168
+ /**
169
+ * @link https://react-query.tanstack.com/reference/QueryClient#queryclientrefetchqueries
170
+ */
171
+ refetchQueries(
172
+ filters?: RefetchQueryFilters,
173
+ options?: RefetchOptions,
174
+ ): Promise<void>;
175
+
176
+ /**
177
+ * @link https://react-query.tanstack.com/guides/query-cancellation
178
+ */
179
+ cancelQuery<
180
+ TPath extends keyof TRouter['_def']['queries'] & string,
181
+ TInput extends inferProcedureInput<TRouter['_def']['queries'][TPath]>,
182
+ >(
183
+ pathAndInput: [TPath, TInput?],
184
+ options?: CancelOptions,
185
+ ): Promise<void>;
186
+ /**
187
+ * @link https://react-query.tanstack.com/reference/QueryClient#queryclientsetquerydata
188
+ */
189
+ setQueryData<
190
+ TPath extends keyof TRouter['_def']['queries'] & string,
191
+ TInput extends inferProcedureInput<TRouter['_def']['queries'][TPath]>,
192
+ TOutput extends inferProcedureOutput<TRouter['_def']['queries'][TPath]>,
193
+ >(
194
+ pathAndInput: [TPath, TInput?],
195
+ updater: Updater<TOutput | undefined, TOutput | undefined>,
196
+ options?: SetDataOptions,
197
+ ): void;
198
+ /**
199
+ * @link https://react-query.tanstack.com/reference/QueryClient#queryclientgetquerydata
200
+ */
201
+ getQueryData<
202
+ TPath extends keyof TRouter['_def']['queries'] & string,
203
+ TInput extends inferProcedureInput<TRouter['_def']['queries'][TPath]>,
204
+ TOutput extends inferProcedureOutput<TRouter['_def']['queries'][TPath]>,
205
+ >(
206
+ pathAndInput: [TPath, TInput?],
207
+ ): TOutput | undefined;
208
+ /**
209
+ * @link https://react-query.tanstack.com/reference/QueryClient#queryclientgetquerydata
210
+ */
211
+ setInfiniteQueryData<
212
+ TPath extends keyof TRouter['_def']['queries'] & string,
213
+ TInput extends inferProcedureInput<TRouter['_def']['queries'][TPath]>,
214
+ TOutput extends inferProcedureOutput<TRouter['_def']['queries'][TPath]>,
215
+ >(
216
+ pathAndInput: [TPath, TInput?],
217
+ updater: Updater<
218
+ InfiniteData<TOutput> | undefined,
219
+ InfiniteData<TOutput> | undefined
220
+ >,
221
+ options?: SetDataOptions,
222
+ ): void;
223
+ /**
224
+ * @link https://react-query.tanstack.com/reference/QueryClient#queryclientgetquerydata
225
+ */
226
+ getInfiniteQueryData<
227
+ TPath extends keyof TRouter['_def']['queries'] & string,
228
+ TInput extends inferProcedureInput<TRouter['_def']['queries'][TPath]>,
229
+ TOutput extends inferProcedureOutput<TRouter['_def']['queries'][TPath]>,
230
+ >(
231
+ pathAndInput: [TPath, TInput?],
232
+ ): InfiniteData<TOutput> | undefined;
233
+ }
234
+
235
+ export const TRPCContext = createContext(null as any);
@@ -0,0 +1,41 @@
1
+ import { getArrayQueryKey } from './getArrayQueryKey';
2
+
3
+ test('getArrayQueryKey', () => {
4
+ expect(getArrayQueryKey('foo')).toMatchInlineSnapshot(`
5
+ Array [
6
+ Array [
7
+ "foo",
8
+ ],
9
+ ]
10
+ `);
11
+ expect(getArrayQueryKey(['foo'])).toMatchInlineSnapshot(`
12
+ Array [
13
+ Array [
14
+ "foo",
15
+ ],
16
+ ]
17
+ `);
18
+ expect(getArrayQueryKey(['foo', 'bar'])).toMatchInlineSnapshot(`
19
+ Array [
20
+ Array [
21
+ "foo",
22
+ ],
23
+ "bar",
24
+ ]
25
+ `);
26
+ expect(getArrayQueryKey([undefined, 'bar'])).toMatchInlineSnapshot(`
27
+ Array [
28
+ Array [],
29
+ "bar",
30
+ ]
31
+ `);
32
+ expect(getArrayQueryKey(['post.byId', '1'])).toMatchInlineSnapshot(`
33
+ Array [
34
+ Array [
35
+ "post",
36
+ "byId",
37
+ ],
38
+ "1",
39
+ ]
40
+ `);
41
+ });
@@ -0,0 +1,18 @@
1
+ /**
2
+ * To allow easy interactions with groups of related queries, such as
3
+ * invalidating all queries of a router, we use an array as the path when
4
+ * storing in tanstack query. This function converts from the `.` separated
5
+ * path passed around internally by both the legacy and proxy implementation.
6
+ * https://github.com/trpc/trpc/issues/2611
7
+ */
8
+ export function getArrayQueryKey(
9
+ queryKey: string | [string] | [string, ...unknown[]] | unknown[],
10
+ ): [string[]] | [string[], ...unknown[]] | [] {
11
+ const queryKeyArrayed = Array.isArray(queryKey) ? queryKey : [queryKey];
12
+ const [path, ...input] = queryKeyArrayed;
13
+
14
+ const arrayPath =
15
+ typeof path !== 'string' || path === '' ? [] : path.split('.');
16
+
17
+ return [arrayPath, ...input];
18
+ }
@@ -0,0 +1,10 @@
1
+ /**
2
+ * We treat `undefined` as an input the same as omitting an `input`
3
+ * https://github.com/trpc/trpc/issues/2290
4
+ */
5
+ export function getQueryKey(
6
+ path: string,
7
+ input: unknown,
8
+ ): [string] | [string, unknown] {
9
+ return input === undefined ? [path] : [path, input];
10
+ }
package/src/interop.ts ADDED
@@ -0,0 +1,25 @@
1
+ // interop:
2
+ import { AnyRouter } from '@trpc/server';
3
+ import { CreateTRPCReact, createHooksInternalProxy } from './createTRPCReact';
4
+ import {
5
+ CreateReactQueryHooks,
6
+ createHooksInternal,
7
+ } from './shared/hooks/createHooksInternal';
8
+
9
+ /**
10
+ * @deprecated use `createTRPCReact` instead
11
+ */
12
+ export function createReactQueryHooks<
13
+ TRouter extends AnyRouter,
14
+ TSSRContext = unknown,
15
+ >(): CreateReactQueryHooks<TRouter, TSSRContext> & {
16
+ proxy: CreateTRPCReact<TRouter, TSSRContext>;
17
+ } {
18
+ const trpc = createHooksInternal<TRouter, TSSRContext>();
19
+ const proxy = createHooksInternalProxy<TRouter, TSSRContext>(trpc);
20
+
21
+ return {
22
+ ...trpc,
23
+ proxy,
24
+ };
25
+ }