@orpc/react 0.27.0 → 0.53.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.
package/dist/index.js DELETED
@@ -1,672 +0,0 @@
1
- // src/general-hooks.ts
2
- import {
3
- useIsFetching,
4
- useIsMutating,
5
- useMutationState
6
- } from "@tanstack/react-query";
7
-
8
- // src/react-context.ts
9
- import { createContext, useContext } from "react";
10
- function createORPCContext() {
11
- return createContext(void 0);
12
- }
13
- function useORPCContext(context) {
14
- const value = useContext(context);
15
- if (!value) {
16
- throw new Error(
17
- "useORPCContext must be used within a <ORPCContext.Provider>, please see the docs"
18
- );
19
- }
20
- return value;
21
- }
22
-
23
- // src/orpc-path.ts
24
- var orpcPathSymbol = Symbol("orpcPathSymbol");
25
-
26
- // src/tanstack-key.ts
27
- function getQueryKeyFromPath(path, options) {
28
- const withInput = options?.input !== void 0 ? { input: options?.input } : {};
29
- const withType = options?.type !== void 0 ? { type: options?.type } : {};
30
- return [
31
- path,
32
- {
33
- ...withInput,
34
- ...withType
35
- }
36
- ];
37
- }
38
- function getMutationKeyFromPath(path) {
39
- return [path];
40
- }
41
-
42
- // src/general-hooks.ts
43
- function createGeneralHooks(options) {
44
- return {
45
- useIsFetching(filters) {
46
- const { queryType, input, ...rest } = filters ?? {};
47
- const context = useORPCContext(options.context);
48
- return useIsFetching(
49
- {
50
- queryKey: getQueryKeyFromPath(options.path, {
51
- input,
52
- type: queryType
53
- }),
54
- ...rest
55
- },
56
- context.queryClient
57
- );
58
- },
59
- useIsMutating(filters) {
60
- const context = useORPCContext(options.context);
61
- return useIsMutating(
62
- { mutationKey: getMutationKeyFromPath(options.path), ...filters },
63
- context.queryClient
64
- );
65
- },
66
- useMutationState(options_) {
67
- const context = useORPCContext(options.context);
68
- return useMutationState(
69
- {
70
- ...options_,
71
- filters: {
72
- mutationKey: getMutationKeyFromPath(options.path),
73
- ...options_?.filters
74
- }
75
- },
76
- context.queryClient
77
- );
78
- }
79
- };
80
- }
81
-
82
- // src/general-utils.ts
83
- function createGeneralUtils(options) {
84
- return {
85
- getQueriesData(filters) {
86
- const { input, ...rest } = filters ?? {};
87
- return options.queryClient.getQueriesData({
88
- queryKey: getQueryKeyFromPath(options.path, { input, type: "query" }),
89
- ...rest
90
- });
91
- },
92
- getInfiniteQueriesData(filters) {
93
- const { input, ...rest } = filters ?? {};
94
- return options.queryClient.getQueriesData({
95
- queryKey: getQueryKeyFromPath(options.path, {
96
- input,
97
- type: "infinite"
98
- }),
99
- ...rest
100
- });
101
- },
102
- setQueriesData(filters, updater, options_) {
103
- const { input, ...rest } = filters;
104
- return options.queryClient.setQueriesData(
105
- {
106
- queryKey: getQueryKeyFromPath(options.path, {
107
- input,
108
- type: "query"
109
- }),
110
- ...rest
111
- },
112
- updater,
113
- options_
114
- );
115
- },
116
- setInfiniteQueriesData(filters, updater, options_) {
117
- const { input, ...rest } = filters;
118
- return options.queryClient.setQueriesData(
119
- {
120
- queryKey: getQueryKeyFromPath(options.path, {
121
- input,
122
- type: "infinite"
123
- }),
124
- ...rest
125
- },
126
- updater,
127
- options_
128
- );
129
- },
130
- invalidate(filters, options_) {
131
- const { input, queryType, ...rest } = filters ?? {};
132
- return options.queryClient.invalidateQueries(
133
- {
134
- queryKey: getQueryKeyFromPath(options.path, {
135
- input,
136
- type: queryType
137
- }),
138
- ...rest
139
- },
140
- options_
141
- );
142
- },
143
- refetch(filters, options_) {
144
- const { input, queryType, ...rest } = filters ?? {};
145
- return options.queryClient.refetchQueries(
146
- {
147
- queryKey: getQueryKeyFromPath(options.path, {
148
- input,
149
- type: queryType
150
- }),
151
- ...rest
152
- },
153
- options_
154
- );
155
- },
156
- cancel(filters, options_) {
157
- const { input, queryType, ...rest } = filters ?? {};
158
- return options.queryClient.cancelQueries(
159
- {
160
- queryKey: getQueryKeyFromPath(options.path, {
161
- input,
162
- type: queryType
163
- }),
164
- ...rest
165
- },
166
- options_
167
- );
168
- },
169
- remove(filters) {
170
- const { input, queryType, ...rest } = filters ?? {};
171
- return options.queryClient.removeQueries({
172
- queryKey: getQueryKeyFromPath(options.path, {
173
- input,
174
- type: queryType
175
- }),
176
- ...rest
177
- });
178
- },
179
- reset(filters, options_) {
180
- const { input, queryType, ...rest } = filters ?? {};
181
- return options.queryClient.resetQueries(
182
- {
183
- queryKey: getQueryKeyFromPath(options.path, {
184
- input,
185
- type: queryType
186
- }),
187
- ...rest
188
- },
189
- options_
190
- );
191
- },
192
- isFetching(filters) {
193
- const { input, queryType, ...rest } = filters ?? {};
194
- return options.queryClient.isFetching({
195
- queryKey: getQueryKeyFromPath(options.path, {
196
- input,
197
- type: queryType
198
- }),
199
- ...rest
200
- });
201
- },
202
- isMutating(filters) {
203
- return options.queryClient.isMutating({
204
- mutationKey: getMutationKeyFromPath(options.path),
205
- ...filters
206
- });
207
- },
208
- getQueryDefaults(filters) {
209
- return options.queryClient.getQueryDefaults(
210
- filters?.queryKey ?? getQueryKeyFromPath(options.path, {
211
- input: filters?.input,
212
- type: "query"
213
- })
214
- );
215
- },
216
- getInfiniteQueryDefaults(filters) {
217
- return options.queryClient.getQueryDefaults(
218
- filters?.queryKey ?? getQueryKeyFromPath(options.path, {
219
- input: filters?.input,
220
- type: "infinite"
221
- })
222
- );
223
- },
224
- setQueryDefaults(options_, filters) {
225
- return options.queryClient.setQueryDefaults(
226
- filters?.queryKey ?? getQueryKeyFromPath(options.path, {
227
- input: filters?.input,
228
- type: "query"
229
- }),
230
- options_
231
- );
232
- },
233
- setInfiniteQueryDefaults(options_, filters) {
234
- return options.queryClient.setQueryDefaults(
235
- filters?.queryKey ?? getQueryKeyFromPath(options.path, {
236
- input: filters?.input,
237
- type: "infinite"
238
- }),
239
- options_
240
- );
241
- },
242
- getMutationDefaults(filters) {
243
- return options.queryClient.getMutationDefaults(
244
- filters?.mutationKey ?? getMutationKeyFromPath(options.path)
245
- );
246
- },
247
- setMutationDefaults(options_, filters) {
248
- return options.queryClient.setMutationDefaults(
249
- filters?.mutationKey ?? getMutationKeyFromPath(options.path),
250
- options_
251
- );
252
- }
253
- };
254
- }
255
-
256
- // src/procedure-hooks.ts
257
- import {
258
- get
259
- } from "@orpc/shared";
260
- import {
261
- useInfiniteQuery,
262
- useMutation,
263
- usePrefetchInfiniteQuery,
264
- usePrefetchQuery,
265
- useQuery,
266
- useSuspenseInfiniteQuery,
267
- useSuspenseQuery
268
- } from "@tanstack/react-query";
269
- function createProcedureHooks(options) {
270
- return {
271
- [orpcPathSymbol]: options.path,
272
- useQuery(input, options_) {
273
- const context = useORPCContext(options.context);
274
- const client = get(context.client, options.path);
275
- return useQuery(
276
- {
277
- queryKey: getQueryKeyFromPath(options.path, { input, type: "query" }),
278
- queryFn: ({ signal }) => client(input, { signal }),
279
- ...options_
280
- },
281
- context.queryClient
282
- );
283
- },
284
- useInfiniteQuery(options_) {
285
- const { input, ...rest } = options_;
286
- const context = useORPCContext(options.context);
287
- const client = get(context.client, options.path);
288
- return useInfiniteQuery(
289
- {
290
- queryKey: getQueryKeyFromPath(options.path, {
291
- input,
292
- type: "infinite"
293
- }),
294
- queryFn: ({ pageParam, signal }) => client({ ...input, cursor: pageParam }, { signal }),
295
- ...rest
296
- },
297
- context.queryClient
298
- );
299
- },
300
- useSuspenseQuery(input, options_) {
301
- const context = useORPCContext(options.context);
302
- const client = get(context.client, options.path);
303
- return useSuspenseQuery(
304
- {
305
- queryKey: getQueryKeyFromPath(options.path, { input, type: "query" }),
306
- queryFn: ({ signal }) => client(input, { signal }),
307
- ...options_
308
- },
309
- context.queryClient
310
- );
311
- },
312
- useSuspenseInfiniteQuery(options_) {
313
- const { input, ...rest } = options_;
314
- const context = useORPCContext(options.context);
315
- const client = get(context.client, options.path);
316
- return useSuspenseInfiniteQuery(
317
- {
318
- queryKey: getQueryKeyFromPath(options.path, {
319
- input,
320
- type: "infinite"
321
- }),
322
- queryFn: ({ pageParam, signal }) => client({ ...input, cursor: pageParam }, { signal }),
323
- ...rest
324
- },
325
- context.queryClient
326
- );
327
- },
328
- usePrefetchQuery(input, options_) {
329
- const context = useORPCContext(options.context);
330
- const client = get(context.client, options.path);
331
- return usePrefetchQuery(
332
- {
333
- queryKey: getQueryKeyFromPath(options.path, { input, type: "query" }),
334
- queryFn: ({ signal }) => client(input, { signal }),
335
- ...options_
336
- },
337
- context.queryClient
338
- );
339
- },
340
- usePrefetchInfiniteQuery(options_) {
341
- const { input, ...rest } = options_;
342
- const context = useORPCContext(options.context);
343
- const client = get(context.client, options.path);
344
- return usePrefetchInfiniteQuery(
345
- {
346
- queryKey: getQueryKeyFromPath(options.path, {
347
- input,
348
- type: "infinite"
349
- }),
350
- queryFn: ({ pageParam, signal }) => client({ ...input, cursor: pageParam }, { signal }),
351
- ...rest
352
- },
353
- context.queryClient
354
- );
355
- },
356
- useMutation(options_) {
357
- const context = useORPCContext(options.context);
358
- const client = get(context.client, options.path);
359
- return useMutation(
360
- {
361
- mutationKey: getMutationKeyFromPath(options.path),
362
- mutationFn: (input) => client(input),
363
- ...options_
364
- },
365
- context.queryClient
366
- );
367
- }
368
- };
369
- }
370
-
371
- // src/procedure-utils.ts
372
- function createProcedureUtils(options) {
373
- return {
374
- fetchQuery(input, options_) {
375
- return options.queryClient.fetchQuery({
376
- queryKey: getQueryKeyFromPath(options.path, { input, type: "query" }),
377
- queryFn: ({ signal }) => options.client(input, { signal }),
378
- ...options_
379
- });
380
- },
381
- fetchInfiniteQuery(options_) {
382
- const { input, ...rest } = options_;
383
- return options.queryClient.fetchInfiniteQuery({
384
- queryKey: getQueryKeyFromPath(options.path, {
385
- input,
386
- type: "infinite"
387
- }),
388
- queryFn: ({ pageParam, signal }) => {
389
- return options.client({ ...input, pageParam }, { signal });
390
- },
391
- ...rest
392
- });
393
- },
394
- prefetchQuery(input, options_) {
395
- return options.queryClient.prefetchQuery({
396
- queryKey: getQueryKeyFromPath(options.path, {
397
- input,
398
- type: "query"
399
- }),
400
- queryFn: ({ signal }) => options.client(input, { signal }),
401
- ...options_
402
- });
403
- },
404
- prefetchInfiniteQuery(options_) {
405
- const { input, ...rest } = options_;
406
- return options.queryClient.prefetchInfiniteQuery({
407
- queryKey: getQueryKeyFromPath(options.path, {
408
- input,
409
- type: "infinite"
410
- }),
411
- queryFn: ({ pageParam, signal }) => {
412
- return options.client({ ...input, cursor: pageParam }, { signal });
413
- },
414
- ...rest
415
- });
416
- },
417
- getQueryData(input) {
418
- return options.queryClient.getQueryData(
419
- getQueryKeyFromPath(options.path, {
420
- input,
421
- type: "query"
422
- })
423
- );
424
- },
425
- getInfiniteQueryData(input) {
426
- return options.queryClient.getQueryData(
427
- getQueryKeyFromPath(options.path, {
428
- input,
429
- type: "infinite"
430
- })
431
- );
432
- },
433
- ensureQueryData(input, options_) {
434
- return options.queryClient.ensureQueryData({
435
- queryKey: getQueryKeyFromPath(options.path, {
436
- input,
437
- type: "query"
438
- }),
439
- queryFn: ({ signal }) => options.client(input, { signal }),
440
- ...options_
441
- });
442
- },
443
- ensureInfiniteQueryData(options_) {
444
- const { input, ...rest } = options_;
445
- return options.queryClient.ensureInfiniteQueryData({
446
- queryKey: getQueryKeyFromPath(options.path, {
447
- input,
448
- type: "infinite"
449
- }),
450
- queryFn: ({ pageParam, signal }) => {
451
- return options.client({ ...input, pageParam }, { signal });
452
- },
453
- ...rest
454
- });
455
- },
456
- getQueryState(input) {
457
- return options.queryClient.getQueryState(
458
- getQueryKeyFromPath(options.path, {
459
- input,
460
- type: "query"
461
- })
462
- );
463
- },
464
- getInfiniteQueryState(input) {
465
- return options.queryClient.getQueryState(
466
- getQueryKeyFromPath(options.path, {
467
- input,
468
- type: "infinite"
469
- })
470
- );
471
- },
472
- setQueryData(input, updater, options_) {
473
- return options.queryClient.setQueryData(
474
- getQueryKeyFromPath(options.path, {
475
- input,
476
- type: "query"
477
- }),
478
- updater,
479
- options_
480
- );
481
- },
482
- setInfiniteQueryData(input, updater, options_) {
483
- return options.queryClient.setQueryData(
484
- getQueryKeyFromPath(options.path, {
485
- input,
486
- type: "infinite"
487
- }),
488
- updater,
489
- options_
490
- );
491
- }
492
- };
493
- }
494
-
495
- // src/react-hooks.ts
496
- function createORPCHooks(options) {
497
- const path = options.path ?? [];
498
- const generalHooks = createGeneralHooks({ context: options.context, path });
499
- const procedureHooks = createProcedureHooks({
500
- context: options.context,
501
- path
502
- });
503
- return new Proxy(
504
- {
505
- [orpcPathSymbol]: path,
506
- ...generalHooks,
507
- ...procedureHooks
508
- },
509
- {
510
- get(target, key) {
511
- const value = Reflect.get(target, key);
512
- if (typeof key !== "string") {
513
- return value;
514
- }
515
- const nextHooks = createORPCHooks({
516
- context: options.context,
517
- path: [...path, key]
518
- });
519
- if (typeof value !== "function") {
520
- return nextHooks;
521
- }
522
- return new Proxy(value, {
523
- get(_, key2) {
524
- return Reflect.get(nextHooks, key2);
525
- }
526
- });
527
- }
528
- }
529
- );
530
- }
531
-
532
- // src/react-utils.ts
533
- function createORPCUtils(options) {
534
- const path = options.path ?? [];
535
- const client = options.contextValue.client;
536
- const generalUtils = createGeneralUtils({
537
- queryClient: options.contextValue.queryClient,
538
- path
539
- });
540
- const procedureUtils = path.length ? createProcedureUtils({
541
- client,
542
- queryClient: options.contextValue.queryClient,
543
- path
544
- }) : {};
545
- return new Proxy(
546
- {
547
- ...generalUtils,
548
- ...procedureUtils
549
- },
550
- {
551
- get(target, key) {
552
- const value = Reflect.get(target, key);
553
- if (typeof key !== "string") {
554
- return value;
555
- }
556
- const nextUtils = createORPCUtils({
557
- ...options,
558
- contextValue: {
559
- ...options.contextValue,
560
- client: client[key]
561
- },
562
- path: [...path, key]
563
- });
564
- if (typeof value !== "function") {
565
- return nextUtils;
566
- }
567
- return new Proxy(value, {
568
- get(_, key2) {
569
- return Reflect.get(nextUtils, key2);
570
- }
571
- });
572
- }
573
- }
574
- );
575
- }
576
-
577
- // src/use-queries/hook.ts
578
- import { useQueries } from "@tanstack/react-query";
579
-
580
- // src/use-queries/builder.ts
581
- function createUseQueriesBuilder(options) {
582
- return (input, options_) => {
583
- return {
584
- queryKey: getQueryKeyFromPath(options.path, { input, type: "query" }),
585
- queryFn: ({ signal }) => options.client(input, { signal }),
586
- ...options_
587
- };
588
- };
589
- }
590
-
591
- // src/use-queries/builders.ts
592
- function createUseQueriesBuilders(options) {
593
- const path = options.path ?? [];
594
- const client = options.client;
595
- const builder = path.length ? createUseQueriesBuilder({ client, path }) : {};
596
- return new Proxy(builder, {
597
- get(target, key) {
598
- const value = Reflect.get(target, key);
599
- if (typeof key !== "string") {
600
- return value;
601
- }
602
- const nextBuilders = createUseQueriesBuilders({
603
- client: client[key],
604
- path: [...path, key]
605
- });
606
- if (typeof value !== "function") {
607
- return nextBuilders;
608
- }
609
- return new Proxy(value, {
610
- get(_, key2) {
611
- return Reflect.get(nextBuilders, key2);
612
- }
613
- });
614
- }
615
- });
616
- }
617
-
618
- // src/use-queries/hook.ts
619
- function useQueriesFactory(options) {
620
- const Hook = (build, combine) => {
621
- const orpc = useORPCContext(options.context);
622
- const builders = createUseQueriesBuilders({ client: orpc.client });
623
- return useQueries({
624
- queries: build(builders),
625
- combine
626
- });
627
- };
628
- return Hook;
629
- }
630
-
631
- // src/react.tsx
632
- function createORPCReact() {
633
- const Context = createORPCContext();
634
- const useContext2 = () => useORPCContext(Context);
635
- const useUtils = () => createORPCUtils({ contextValue: useContext2() });
636
- const useQueries2 = useQueriesFactory({ context: Context });
637
- const hooks = createORPCHooks({ context: Context });
638
- const orpc = new Proxy(
639
- {
640
- useContext: useContext2,
641
- useUtils,
642
- useQueries: useQueries2
643
- },
644
- {
645
- get(target, key) {
646
- const value = Reflect.get(target, key);
647
- const nextHooks = Reflect.get(hooks, key);
648
- if (typeof value !== "function") {
649
- return nextHooks;
650
- }
651
- return new Proxy(value, {
652
- get(_, key2) {
653
- return Reflect.get(nextHooks, key2);
654
- }
655
- });
656
- }
657
- }
658
- );
659
- return { orpc, ORPCContext: Context };
660
- }
661
- export {
662
- createGeneralHooks,
663
- createGeneralUtils,
664
- createORPCContext,
665
- createORPCHooks,
666
- createORPCReact,
667
- createORPCUtils,
668
- createProcedureHooks,
669
- createProcedureUtils,
670
- useORPCContext
671
- };
672
- //# sourceMappingURL=index.js.map
@@ -1,23 +0,0 @@
1
- import type { PartialDeep, SetOptional } from '@orpc/shared';
2
- import type { ORPCQueryFilters } from './tanstack-query';
3
- import { type DefaultError, type Mutation, type MutationFilters, type MutationState } from '@tanstack/react-query';
4
- import { type ORPCContext } from './react-context';
5
- export interface GeneralHooks<TInput, TOutput> {
6
- useIsFetching: (filers?: ORPCQueryFilters<PartialDeep<TInput>>) => number;
7
- useIsMutating: (filters?: SetOptional<MutationFilters, 'mutationKey'>) => number;
8
- useMutationState: <UResult = MutationState<TOutput, DefaultError, TInput>>(options?: {
9
- filters?: SetOptional<MutationFilters, 'mutationKey'>;
10
- select?: (mutation: Mutation<TOutput, DefaultError, TInput>) => UResult;
11
- }) => UResult[];
12
- }
13
- export interface CreateGeneralHooksOptions {
14
- context: ORPCContext<any>;
15
- /**
16
- * The path of the router or procedure on server.
17
- *
18
- * @internal
19
- */
20
- path: string[];
21
- }
22
- export declare function createGeneralHooks<TInput, TOutput>(options: CreateGeneralHooksOptions): GeneralHooks<TInput, TOutput>;
23
- //# sourceMappingURL=general-hooks.d.ts.map