@orpc/react 0.0.0

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 (71) hide show
  1. package/dist/index.js +635 -0
  2. package/dist/src/general-hooks.d.ts +24 -0
  3. package/dist/src/general-hooks.d.ts.map +1 -0
  4. package/dist/src/general-utils.d.ts +41 -0
  5. package/dist/src/general-utils.d.ts.map +1 -0
  6. package/dist/src/index.d.ts +9 -0
  7. package/dist/src/index.d.ts.map +1 -0
  8. package/dist/src/orpc-path.d.ts +5 -0
  9. package/dist/src/orpc-path.d.ts.map +1 -0
  10. package/dist/src/procedure-hooks.d.ts +31 -0
  11. package/dist/src/procedure-hooks.d.ts.map +1 -0
  12. package/dist/src/procedure-utils.d.ts +35 -0
  13. package/dist/src/procedure-utils.d.ts.map +1 -0
  14. package/dist/src/react-context.d.ts +13 -0
  15. package/dist/src/react-context.d.ts.map +1 -0
  16. package/dist/src/react-hooks.d.ts +22 -0
  17. package/dist/src/react-hooks.d.ts.map +1 -0
  18. package/dist/src/react-utils.d.ts +22 -0
  19. package/dist/src/react-utils.d.ts.map +1 -0
  20. package/dist/src/react.d.ts +21 -0
  21. package/dist/src/react.d.ts.map +1 -0
  22. package/dist/src/tanstack-key.d.ts +15 -0
  23. package/dist/src/tanstack-key.d.ts.map +1 -0
  24. package/dist/src/tanstack-query.d.ts +19 -0
  25. package/dist/src/tanstack-query.d.ts.map +1 -0
  26. package/dist/src/types.d.ts +5 -0
  27. package/dist/src/types.d.ts.map +1 -0
  28. package/dist/src/use-queries/builder.d.ts +20 -0
  29. package/dist/src/use-queries/builder.d.ts.map +1 -0
  30. package/dist/src/use-queries/builders.d.ts +19 -0
  31. package/dist/src/use-queries/builders.d.ts.map +1 -0
  32. package/dist/src/use-queries/hook.d.ts +16 -0
  33. package/dist/src/use-queries/hook.d.ts.map +1 -0
  34. package/dist/tsconfig.tsbuildinfo +1 -0
  35. package/package.json +56 -0
  36. package/src/general-hooks.test-d.ts +151 -0
  37. package/src/general-hooks.test.tsx +232 -0
  38. package/src/general-hooks.ts +100 -0
  39. package/src/general-utils.test-d.ts +454 -0
  40. package/src/general-utils.test.tsx +818 -0
  41. package/src/general-utils.ts +397 -0
  42. package/src/index.ts +8 -0
  43. package/src/orpc-path.test-d.ts +13 -0
  44. package/src/orpc-path.test.ts +12 -0
  45. package/src/orpc-path.ts +24 -0
  46. package/src/procedure-hooks.test-d.ts +321 -0
  47. package/src/procedure-hooks.test.tsx +388 -0
  48. package/src/procedure-hooks.ts +271 -0
  49. package/src/procedure-utils.test-d.ts +476 -0
  50. package/src/procedure-utils.test.tsx +330 -0
  51. package/src/procedure-utils.ts +315 -0
  52. package/src/react-context.ts +43 -0
  53. package/src/react-hooks.ts +102 -0
  54. package/src/react-utils.ts +110 -0
  55. package/src/react.test-d.ts +89 -0
  56. package/src/react.test.tsx +102 -0
  57. package/src/react.tsx +80 -0
  58. package/src/tanstack-key.test-d.ts +35 -0
  59. package/src/tanstack-key.test.ts +62 -0
  60. package/src/tanstack-key.ts +64 -0
  61. package/src/tanstack-query.ts +27 -0
  62. package/src/types.ts +7 -0
  63. package/src/use-queries/builder.test-d.ts +29 -0
  64. package/src/use-queries/builder.test.ts +25 -0
  65. package/src/use-queries/builder.ts +72 -0
  66. package/src/use-queries/builders.test-d.ts +30 -0
  67. package/src/use-queries/builders.test.tsx +29 -0
  68. package/src/use-queries/builders.ts +101 -0
  69. package/src/use-queries/hook.test-d.ts +64 -0
  70. package/src/use-queries/hook.test.tsx +89 -0
  71. package/src/use-queries/hook.ts +57 -0
@@ -0,0 +1,397 @@
1
+ import type { Schema, SchemaInput, SchemaOutput } from '@orpc/contract'
2
+ import type { PartialDeep, SetOptional } from '@orpc/shared'
3
+ import type {
4
+ CancelOptions,
5
+ DefaultError,
6
+ InfiniteData,
7
+ InvalidateOptions,
8
+ MutationFilters,
9
+ MutationObserverOptions,
10
+ OmitKeyof,
11
+ QueryClient,
12
+ QueryKey,
13
+ QueryObserverOptions,
14
+ RefetchOptions,
15
+ ResetOptions,
16
+ SetDataOptions,
17
+ Updater,
18
+ } from '@tanstack/react-query'
19
+ import { getMutationKeyFromPath, getQueryKeyFromPath } from './tanstack-key'
20
+ import type {
21
+ ORPCInvalidateQueryFilters,
22
+ ORPCQueryFilters,
23
+ } from './tanstack-query'
24
+ import type { SchemaInputForInfiniteQuery } from './types'
25
+
26
+ export interface GeneralUtils<
27
+ TInputSchema extends Schema,
28
+ TOutputSchema extends Schema,
29
+ THandlerOutput extends SchemaOutput<TOutputSchema>,
30
+ > {
31
+ getQueriesData(
32
+ filters?: OmitKeyof<
33
+ ORPCQueryFilters<PartialDeep<SchemaInput<TInputSchema>>>,
34
+ 'queryType'
35
+ >,
36
+ ): [QueryKey, SchemaOutput<TOutputSchema, THandlerOutput> | undefined][]
37
+ getInfiniteQueriesData(
38
+ filters?: OmitKeyof<
39
+ ORPCQueryFilters<PartialDeep<SchemaInputForInfiniteQuery<TInputSchema>>>,
40
+ 'queryType'
41
+ >,
42
+ ): [
43
+ QueryKey,
44
+ (
45
+ | InfiniteData<
46
+ SchemaOutput<TOutputSchema, THandlerOutput>,
47
+ SchemaInput<TInputSchema>['cursor']
48
+ >
49
+ | undefined
50
+ ),
51
+ ][]
52
+
53
+ setQueriesData(
54
+ filters: OmitKeyof<
55
+ ORPCQueryFilters<PartialDeep<SchemaInput<TInputSchema>>>,
56
+ 'queryType'
57
+ >,
58
+ updater: Updater<
59
+ SchemaOutput<TOutputSchema, THandlerOutput> | undefined,
60
+ SchemaOutput<TOutputSchema, THandlerOutput> | undefined
61
+ >,
62
+ options?: SetDataOptions,
63
+ ): [QueryKey, SchemaOutput<TOutputSchema, THandlerOutput> | undefined][]
64
+ setInfiniteQueriesData(
65
+ filters: OmitKeyof<
66
+ ORPCQueryFilters<PartialDeep<SchemaInputForInfiniteQuery<TInputSchema>>>,
67
+ 'queryType'
68
+ >,
69
+ updater: Updater<
70
+ | InfiniteData<
71
+ SchemaOutput<TOutputSchema, THandlerOutput>,
72
+ SchemaInput<TInputSchema>['cursor']
73
+ >
74
+ | undefined,
75
+ | InfiniteData<
76
+ SchemaOutput<TOutputSchema, THandlerOutput>,
77
+ SchemaInput<TInputSchema>['cursor']
78
+ >
79
+ | undefined
80
+ >,
81
+ options?: SetDataOptions,
82
+ ): [
83
+ QueryKey,
84
+ (
85
+ | InfiniteData<
86
+ SchemaOutput<TOutputSchema, THandlerOutput>,
87
+ SchemaInput<TInputSchema>['cursor']
88
+ >
89
+ | undefined
90
+ ),
91
+ ][]
92
+
93
+ invalidate(
94
+ filters?: ORPCInvalidateQueryFilters<
95
+ PartialDeep<SchemaInput<TInputSchema>>
96
+ >,
97
+ options?: InvalidateOptions,
98
+ ): Promise<void>
99
+ refetch(
100
+ filters?: ORPCQueryFilters<PartialDeep<SchemaInput<TInputSchema>>>,
101
+ options?: RefetchOptions,
102
+ ): Promise<void>
103
+ cancel(
104
+ filters?: ORPCQueryFilters<PartialDeep<SchemaInput<TInputSchema>>>,
105
+ options?: CancelOptions,
106
+ ): Promise<void>
107
+ remove(
108
+ filters?: ORPCQueryFilters<PartialDeep<SchemaInput<TInputSchema>>>,
109
+ ): void
110
+ reset(
111
+ filters?: ORPCQueryFilters<PartialDeep<SchemaInput<TInputSchema>>>,
112
+ options?: ResetOptions,
113
+ ): Promise<void>
114
+
115
+ isFetching(
116
+ filters?: ORPCQueryFilters<PartialDeep<SchemaInput<TInputSchema>>>,
117
+ ): number
118
+ isMutating(filters?: SetOptional<MutationFilters, 'mutationKey'>): number
119
+
120
+ getQueryDefaults(
121
+ filters?: Pick<
122
+ ORPCQueryFilters<PartialDeep<SchemaInput<TInputSchema>>>,
123
+ 'input' | 'queryKey'
124
+ >,
125
+ ): OmitKeyof<
126
+ QueryObserverOptions<SchemaOutput<TOutputSchema, THandlerOutput>>,
127
+ 'queryKey'
128
+ >
129
+ getInfiniteQueryDefaults(
130
+ filters?: Pick<
131
+ ORPCQueryFilters<PartialDeep<SchemaInputForInfiniteQuery<TInputSchema>>>,
132
+ 'input' | 'queryKey'
133
+ >,
134
+ ): OmitKeyof<
135
+ QueryObserverOptions<
136
+ SchemaOutput<TOutputSchema, THandlerOutput>,
137
+ DefaultError,
138
+ SchemaOutput<TOutputSchema, THandlerOutput>,
139
+ InfiniteData<SchemaOutput<TOutputSchema, THandlerOutput>>,
140
+ QueryKey,
141
+ SchemaInput<TInputSchema>['cursor']
142
+ >,
143
+ 'queryKey'
144
+ >
145
+
146
+ setQueryDefaults(
147
+ options: Partial<
148
+ OmitKeyof<
149
+ QueryObserverOptions<SchemaOutput<TOutputSchema, THandlerOutput>>,
150
+ 'queryKey'
151
+ >
152
+ >,
153
+ filters?: Pick<
154
+ ORPCQueryFilters<PartialDeep<SchemaInput<TInputSchema>>>,
155
+ 'input' | 'queryKey'
156
+ >,
157
+ ): void
158
+ setInfiniteQueryDefaults(
159
+ options: Partial<
160
+ OmitKeyof<
161
+ QueryObserverOptions<
162
+ SchemaOutput<TOutputSchema, THandlerOutput>,
163
+ DefaultError,
164
+ SchemaOutput<TOutputSchema, THandlerOutput>,
165
+ InfiniteData<SchemaOutput<TOutputSchema, THandlerOutput>>,
166
+ QueryKey,
167
+ SchemaInput<TInputSchema>['cursor']
168
+ >,
169
+ 'queryKey'
170
+ >
171
+ >,
172
+ filters?: Pick<
173
+ ORPCQueryFilters<PartialDeep<SchemaInput<TInputSchema>>>,
174
+ 'input' | 'queryKey'
175
+ >,
176
+ ): void
177
+
178
+ getMutationDefaults: (
179
+ filters?: Pick<MutationFilters, 'mutationKey'>,
180
+ ) => MutationObserverOptions<
181
+ SchemaOutput<TOutputSchema, THandlerOutput>,
182
+ DefaultError,
183
+ SchemaInput<TInputSchema>
184
+ >
185
+ setMutationDefaults: (
186
+ options: OmitKeyof<
187
+ MutationObserverOptions<
188
+ SchemaOutput<TOutputSchema, THandlerOutput>,
189
+ DefaultError,
190
+ SchemaInput<TInputSchema>
191
+ >,
192
+ 'mutationKey'
193
+ >,
194
+ filters?: Pick<MutationFilters, 'mutationKey'>,
195
+ ) => void
196
+ }
197
+
198
+ export interface CreateGeneralUtilsOptions {
199
+ queryClient: QueryClient
200
+
201
+ /**
202
+ * The path of the router or procedure on server.
203
+ *
204
+ * @internal
205
+ */
206
+ path: string[]
207
+ }
208
+
209
+ export function createGeneralUtils<
210
+ TInputSchema extends Schema = undefined,
211
+ TOutputSchema extends Schema = undefined,
212
+ THandlerOutput extends
213
+ SchemaOutput<TOutputSchema> = SchemaOutput<TOutputSchema>,
214
+ >(
215
+ options: CreateGeneralUtilsOptions,
216
+ ): GeneralUtils<TInputSchema, TOutputSchema, THandlerOutput> {
217
+ return {
218
+ getQueriesData(filters) {
219
+ const { input, ...rest } = filters ?? {}
220
+ return options.queryClient.getQueriesData({
221
+ queryKey: getQueryKeyFromPath(options.path, { input, type: 'query' }),
222
+ ...rest,
223
+ }) as any
224
+ },
225
+ getInfiniteQueriesData(filters) {
226
+ const { input, ...rest } = filters ?? {}
227
+ return options.queryClient.getQueriesData({
228
+ queryKey: getQueryKeyFromPath(options.path, {
229
+ input,
230
+ type: 'infinite',
231
+ }),
232
+ ...rest,
233
+ }) as any
234
+ },
235
+
236
+ setQueriesData(filters, updater, options_) {
237
+ const { input, ...rest } = filters
238
+ return options.queryClient.setQueriesData(
239
+ {
240
+ queryKey: getQueryKeyFromPath(options.path, {
241
+ input,
242
+ type: 'query',
243
+ }),
244
+ ...rest,
245
+ },
246
+ updater,
247
+ options_,
248
+ ) as any
249
+ },
250
+ setInfiniteQueriesData(filters, updater, options_) {
251
+ const { input, ...rest } = filters
252
+ return options.queryClient.setQueriesData(
253
+ {
254
+ queryKey: getQueryKeyFromPath(options.path, {
255
+ input,
256
+ type: 'infinite',
257
+ }),
258
+ ...rest,
259
+ },
260
+ updater,
261
+ options_,
262
+ ) as any
263
+ },
264
+
265
+ invalidate(filters, options_) {
266
+ const { input, queryType, ...rest } = filters ?? {}
267
+ return options.queryClient.invalidateQueries(
268
+ {
269
+ queryKey: getQueryKeyFromPath(options.path, {
270
+ input,
271
+ type: queryType,
272
+ }),
273
+ ...rest,
274
+ },
275
+ options_,
276
+ )
277
+ },
278
+ refetch(filters, options_) {
279
+ const { input, queryType, ...rest } = filters ?? {}
280
+ return options.queryClient.refetchQueries(
281
+ {
282
+ queryKey: getQueryKeyFromPath(options.path, {
283
+ input,
284
+ type: queryType,
285
+ }),
286
+ ...rest,
287
+ },
288
+ options_,
289
+ )
290
+ },
291
+ cancel(filters, options_) {
292
+ const { input, queryType, ...rest } = filters ?? {}
293
+ return options.queryClient.cancelQueries(
294
+ {
295
+ queryKey: getQueryKeyFromPath(options.path, {
296
+ input,
297
+ type: queryType,
298
+ }),
299
+ ...rest,
300
+ },
301
+ options_,
302
+ )
303
+ },
304
+ remove(filters) {
305
+ const { input, queryType, ...rest } = filters ?? {}
306
+ return options.queryClient.removeQueries({
307
+ queryKey: getQueryKeyFromPath(options.path, {
308
+ input,
309
+ type: queryType,
310
+ }),
311
+ ...rest,
312
+ })
313
+ },
314
+ reset(filters, options_) {
315
+ const { input, queryType, ...rest } = filters ?? {}
316
+ return options.queryClient.resetQueries(
317
+ {
318
+ queryKey: getQueryKeyFromPath(options.path, {
319
+ input,
320
+ type: queryType,
321
+ }),
322
+ ...rest,
323
+ },
324
+ options_,
325
+ )
326
+ },
327
+
328
+ isFetching(filters) {
329
+ const { input, queryType, ...rest } = filters ?? {}
330
+ return options.queryClient.isFetching({
331
+ queryKey: getQueryKeyFromPath(options.path, {
332
+ input,
333
+ type: queryType,
334
+ }),
335
+ ...rest,
336
+ })
337
+ },
338
+ isMutating(filters) {
339
+ return options.queryClient.isMutating({
340
+ mutationKey: getMutationKeyFromPath(options.path),
341
+ ...filters,
342
+ })
343
+ },
344
+
345
+ getQueryDefaults(filters) {
346
+ return options.queryClient.getQueryDefaults(
347
+ filters?.queryKey ??
348
+ getQueryKeyFromPath(options.path, {
349
+ input: filters?.input,
350
+ type: 'query',
351
+ }),
352
+ )
353
+ },
354
+ getInfiniteQueryDefaults(filters) {
355
+ return options.queryClient.getQueryDefaults(
356
+ filters?.queryKey ??
357
+ getQueryKeyFromPath(options.path, {
358
+ input: filters?.input,
359
+ type: 'infinite',
360
+ }),
361
+ ) as any
362
+ },
363
+
364
+ setQueryDefaults(options_, filters) {
365
+ return options.queryClient.setQueryDefaults(
366
+ filters?.queryKey ??
367
+ getQueryKeyFromPath(options.path, {
368
+ input: filters?.input,
369
+ type: 'query',
370
+ }),
371
+ options_ as any,
372
+ )
373
+ },
374
+ setInfiniteQueryDefaults(options_, filters) {
375
+ return options.queryClient.setQueryDefaults(
376
+ filters?.queryKey ??
377
+ getQueryKeyFromPath(options.path, {
378
+ input: filters?.input,
379
+ type: 'infinite',
380
+ }),
381
+ options_ as any,
382
+ )
383
+ },
384
+
385
+ getMutationDefaults(filters) {
386
+ return options.queryClient.getMutationDefaults(
387
+ filters?.mutationKey ?? getMutationKeyFromPath(options.path),
388
+ )
389
+ },
390
+ setMutationDefaults(options_, filters) {
391
+ return options.queryClient.setMutationDefaults(
392
+ filters?.mutationKey ?? getMutationKeyFromPath(options.path),
393
+ options_,
394
+ )
395
+ },
396
+ }
397
+ }
package/src/index.ts ADDED
@@ -0,0 +1,8 @@
1
+ export * from './general-hooks'
2
+ export * from './general-utils'
3
+ export * from './procedure-hooks'
4
+ export * from './procedure-utils'
5
+ export * from './react-context'
6
+ export * from './react-hooks'
7
+ export * from './react-utils'
8
+ export * from './react'
@@ -0,0 +1,13 @@
1
+ import { orpc } from '../tests/orpc'
2
+ import { getORPCPath } from './orpc-path'
3
+
4
+ it('required valid orpc', () => {
5
+ getORPCPath(orpc.ping)
6
+ getORPCPath(orpc.user.find)
7
+
8
+ // @ts-expect-error invalid orpc
9
+ getORPCPath({})
10
+
11
+ // @ts-expect-error cannot use in root
12
+ getORPCPath(orpc)
13
+ })
@@ -0,0 +1,12 @@
1
+ import { orpc } from '../tests/orpc'
2
+ import { getORPCPath } from './orpc-path'
3
+
4
+ it('can get path', () => {
5
+ expect(getORPCPath(orpc.ping)).toEqual(['ping'])
6
+ expect(getORPCPath(orpc.user.find)).toEqual(['user', 'find'])
7
+ })
8
+
9
+ it('throw on invalid orpc', () => {
10
+ // @ts-expect-error invalid orpc
11
+ expect(() => getORPCPath({})).toThrow()
12
+ })
@@ -0,0 +1,24 @@
1
+ import type { ProcedureHooks } from './procedure-hooks'
2
+ import type {
3
+ ORPCHooksWithContractRouter,
4
+ ORPCHooksWithRouter,
5
+ } from './react-hooks'
6
+
7
+ export const orpcPathSymbol = Symbol('orpcPathSymbol')
8
+
9
+ export function getORPCPath(
10
+ orpc:
11
+ | ORPCHooksWithContractRouter<any>
12
+ | ORPCHooksWithRouter<any>
13
+ | ProcedureHooks<any, any, any>,
14
+ ): string[] {
15
+ const val = Reflect.get(orpc, orpcPathSymbol)
16
+
17
+ if (!Array.isArray(val)) {
18
+ throw new Error(
19
+ 'orpcPathSymbol is not implemented, please use getORPCPath with correct instance.',
20
+ )
21
+ }
22
+
23
+ return val
24
+ }