react-query-kit 3.0.0-beta.3 → 3.0.0-rc.2
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/README.md +74 -8
- package/build/lib/index.d.ts +1 -0
- package/build/lib/index.esm.js +1 -0
- package/build/lib/index.esm.js.map +1 -1
- package/build/lib/index.js +2 -0
- package/build/lib/index.js.map +1 -1
- package/build/lib/index.mjs +1 -0
- package/build/lib/index.mjs.map +1 -1
- package/build/lib/router.d.ts +7 -0
- package/build/lib/router.esm.js +49 -0
- package/build/lib/router.esm.js.map +1 -0
- package/build/lib/router.js +53 -0
- package/build/lib/router.js.map +1 -0
- package/build/lib/router.mjs +49 -0
- package/build/lib/router.mjs.map +1 -0
- package/build/lib/types.d.ts +48 -13
- package/build/stats-html.html +1 -1
- package/build/stats.json +134 -78
- package/build/umd/index.development.js +42 -0
- package/build/umd/index.development.js.map +1 -1
- package/build/umd/index.production.js +1 -1
- package/build/umd/index.production.js.map +1 -1
- package/build/zip/zip.esm.js +1 -1
- package/package.json +1 -1
- package/src/index.ts +1 -0
- package/src/router.ts +90 -0
- package/src/types.ts +167 -55
package/src/index.ts
CHANGED
package/src/router.ts
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import { createInfiniteQuery } from './createInfiniteQuery'
|
|
2
|
+
import { createMutation } from './createMutation'
|
|
3
|
+
import { createQuery } from './createQuery'
|
|
4
|
+
import { createSuspenseInfiniteQuery } from './createSuspenseInfiniteQuery'
|
|
5
|
+
import { createSuspenseQuery } from './createSuspenseQuery'
|
|
6
|
+
import type {
|
|
7
|
+
CompatibleError,
|
|
8
|
+
CreateInfiniteQueryOptions,
|
|
9
|
+
CreateMutationOptions,
|
|
10
|
+
CreateQueryOptions,
|
|
11
|
+
CreateRouter,
|
|
12
|
+
RouterConfig,
|
|
13
|
+
RouterInfiniteQuery,
|
|
14
|
+
RouterMutation,
|
|
15
|
+
RouterQuery,
|
|
16
|
+
} from './types'
|
|
17
|
+
|
|
18
|
+
export const router = <TConfig extends RouterConfig>(
|
|
19
|
+
scope: string,
|
|
20
|
+
config: TConfig
|
|
21
|
+
): CreateRouter<TConfig> => {
|
|
22
|
+
return Object.entries(config).reduce(
|
|
23
|
+
(acc, [key, opts]) => {
|
|
24
|
+
const options: any = {
|
|
25
|
+
...opts,
|
|
26
|
+
[opts._type === `m` ? `mutationKey` : `queryKey`]: [scope, key],
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
acc[key] =
|
|
30
|
+
opts._type === `m`
|
|
31
|
+
? {
|
|
32
|
+
useMutation: createMutation(options),
|
|
33
|
+
...createMutation(options),
|
|
34
|
+
}
|
|
35
|
+
: opts._type === `q`
|
|
36
|
+
? {
|
|
37
|
+
useQuery: createQuery(options),
|
|
38
|
+
useSuspenseQuery: createSuspenseQuery(options),
|
|
39
|
+
...createQuery(options),
|
|
40
|
+
}
|
|
41
|
+
: {
|
|
42
|
+
useInfiniteQuery: createInfiniteQuery(options),
|
|
43
|
+
useSuspenseInfiniteQuery: createSuspenseInfiniteQuery(options),
|
|
44
|
+
...createInfiniteQuery(options),
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
return acc
|
|
48
|
+
},
|
|
49
|
+
{
|
|
50
|
+
getKey: () => [scope],
|
|
51
|
+
} as any
|
|
52
|
+
)
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
router.query = <TFnData, TVariables = void, TError = CompatibleError>(
|
|
56
|
+
options: Omit<CreateQueryOptions<TFnData, TVariables, TError>, 'queryKey'>
|
|
57
|
+
): RouterQuery<TFnData, TVariables, TError> => {
|
|
58
|
+
return {
|
|
59
|
+
...options,
|
|
60
|
+
_type: 'q',
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
router.infiniteQuery = <
|
|
65
|
+
TFnData,
|
|
66
|
+
TVariables = void,
|
|
67
|
+
TError = CompatibleError,
|
|
68
|
+
TPageParam = number
|
|
69
|
+
>(
|
|
70
|
+
options: Omit<
|
|
71
|
+
CreateInfiniteQueryOptions<TFnData, TVariables, TError, TPageParam>,
|
|
72
|
+
'queryKey'
|
|
73
|
+
>
|
|
74
|
+
): RouterInfiniteQuery<TFnData, TVariables, TError, TPageParam> => {
|
|
75
|
+
return { ...options, _type: 'inf' }
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
router.mutation = <
|
|
79
|
+
TFnData = unknown,
|
|
80
|
+
TVariables = void,
|
|
81
|
+
TError = CompatibleError,
|
|
82
|
+
TContext = unknown
|
|
83
|
+
>(
|
|
84
|
+
options: Omit<
|
|
85
|
+
CreateMutationOptions<TFnData, TVariables, TError, TContext>,
|
|
86
|
+
'mutationKey'
|
|
87
|
+
>
|
|
88
|
+
): RouterMutation<TFnData, TVariables, TError, TContext> => {
|
|
89
|
+
return { ...options, _type: 'm' }
|
|
90
|
+
}
|
package/src/types.ts
CHANGED
|
@@ -54,9 +54,15 @@ type WithRequired<T, K extends keyof T> = T & {
|
|
|
54
54
|
[_ in K]: {}
|
|
55
55
|
}
|
|
56
56
|
|
|
57
|
+
type DeepPartial<T> = T extends object
|
|
58
|
+
? {
|
|
59
|
+
[P in keyof T]?: DeepPartial<T[P]>
|
|
60
|
+
}
|
|
61
|
+
: T
|
|
62
|
+
|
|
57
63
|
export type CompatibleError = CompatibleWithV4<DefaultError, Error>
|
|
58
64
|
|
|
59
|
-
export type Fetcher<TFnData, TVariables = void, TPageParam =
|
|
65
|
+
export type Fetcher<TFnData, TVariables = void, TPageParam = never> = (
|
|
60
66
|
variables: TVariables,
|
|
61
67
|
context: QueryFunctionContext<QueryKey, TPageParam>
|
|
62
68
|
) => TFnData | Promise<TFnData>
|
|
@@ -75,19 +81,15 @@ export type Middleware<
|
|
|
75
81
|
T extends (...args: any) => any = QueryHook<any, any, any>
|
|
76
82
|
> = (hook: inferMiddlewareHook<T>) => inferMiddlewareHook<T>
|
|
77
83
|
|
|
78
|
-
type
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
: T
|
|
84
|
+
export type ExposeFetcher<TFnData, TVariables = void, TPageParam = never> = (
|
|
85
|
+
variables: TVariables,
|
|
86
|
+
context?: Partial<QueryFunctionContext<QueryKey, TPageParam>>
|
|
87
|
+
) => TFnData | Promise<TFnData>
|
|
83
88
|
|
|
84
89
|
export type ExposeMethods<TFnData, TVariables, TError, TPageParam = never> = {
|
|
85
|
-
fetcher:
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
) => TFnData | Promise<TFnData>
|
|
89
|
-
getKey: <V extends DeepPartial<TVariables> | void = void>(
|
|
90
|
-
variables?: V
|
|
90
|
+
fetcher: ExposeFetcher<TFnData, TVariables, TPageParam>
|
|
91
|
+
getKey: (
|
|
92
|
+
variables?: DeepPartial<TVariables>
|
|
91
93
|
) => CompatibleWithV4<
|
|
92
94
|
DataTag<
|
|
93
95
|
QueryKey,
|
|
@@ -459,16 +461,12 @@ export type MutationHookResult<
|
|
|
459
461
|
TContext = unknown
|
|
460
462
|
> = UseMutationResult<TData, TError, TVariables, TContext>
|
|
461
463
|
|
|
462
|
-
export interface
|
|
464
|
+
export interface ExposeMutationMethods<
|
|
463
465
|
TData = unknown,
|
|
464
466
|
TVariables = void,
|
|
465
467
|
TError = unknown,
|
|
466
468
|
TDefaultContext = unknown
|
|
467
469
|
> {
|
|
468
|
-
<TContext = TDefaultContext>(
|
|
469
|
-
options?: MutationHookOptions<TData, TError, TVariables, TContext>,
|
|
470
|
-
queryClient?: CompatibleWithV4<QueryClient, void>
|
|
471
|
-
): MutationHookResult<TData, TError, TVariables, TContext>
|
|
472
470
|
getKey: () => MutationKey | undefined
|
|
473
471
|
getOptions: () => UseMutationOptions<
|
|
474
472
|
TData,
|
|
@@ -479,53 +477,51 @@ export interface MutationHook<
|
|
|
479
477
|
mutationFn: MutationFunction<TData, TVariables>
|
|
480
478
|
}
|
|
481
479
|
|
|
480
|
+
export interface MutationHook<
|
|
481
|
+
TData = unknown,
|
|
482
|
+
TVariables = void,
|
|
483
|
+
TError = unknown,
|
|
484
|
+
TDefaultContext = unknown
|
|
485
|
+
> extends ExposeMutationMethods<TData, TVariables, TError, TDefaultContext> {
|
|
486
|
+
<TContext = TDefaultContext>(
|
|
487
|
+
options?: MutationHookOptions<TData, TError, TVariables, TContext>,
|
|
488
|
+
queryClient?: CompatibleWithV4<QueryClient, void>
|
|
489
|
+
): MutationHookResult<TData, TError, TVariables, TContext>
|
|
490
|
+
}
|
|
491
|
+
|
|
482
492
|
// infer types
|
|
483
493
|
|
|
484
|
-
export type inferVariables<T> = T extends
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
? TVariables
|
|
488
|
-
: T extends InfiniteQueryHook<any, infer TVariables, any>
|
|
489
|
-
? TVariables
|
|
490
|
-
: T extends SuspenseInfiniteQueryHook<any, infer TVariables, any>
|
|
494
|
+
export type inferVariables<T> = T extends {
|
|
495
|
+
fetcher: ExposeFetcher<any, infer TVariables, any>
|
|
496
|
+
}
|
|
491
497
|
? TVariables
|
|
492
|
-
: T extends
|
|
498
|
+
: T extends ExposeMutationMethods<any, infer TVariables, any, any>
|
|
493
499
|
? TVariables
|
|
494
500
|
: never
|
|
495
501
|
|
|
496
|
-
export type inferData<T> = T extends
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
?
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
: T extends
|
|
503
|
-
?
|
|
504
|
-
: T extends MutationHook<infer TData, any, any>
|
|
505
|
-
? TData
|
|
502
|
+
export type inferData<T> = T extends {
|
|
503
|
+
fetcher: ExposeFetcher<infer TFnData, any, infer TPageParam>
|
|
504
|
+
}
|
|
505
|
+
? [TPageParam] extends [never]
|
|
506
|
+
? TFnData
|
|
507
|
+
: CompatibleInfiniteData<TFnData, TPageParam>
|
|
508
|
+
: T extends ExposeMutationMethods<infer TFnData, any, any, any>
|
|
509
|
+
? TFnData
|
|
506
510
|
: never
|
|
507
511
|
|
|
508
|
-
export type inferFnData<T> = T extends
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
?
|
|
512
|
-
: T extends
|
|
513
|
-
?
|
|
514
|
-
: T extends SuspenseInfiniteQueryHook<infer TData, any, any>
|
|
515
|
-
? TData
|
|
516
|
-
: T extends MutationHook<infer TData, any, any>
|
|
517
|
-
? TData
|
|
512
|
+
export type inferFnData<T> = T extends {
|
|
513
|
+
fetcher: ExposeFetcher<infer TFnData, any, any>
|
|
514
|
+
}
|
|
515
|
+
? TFnData
|
|
516
|
+
: T extends ExposeMutationMethods<infer TFnData, any, any, any>
|
|
517
|
+
? TFnData
|
|
518
518
|
: never
|
|
519
519
|
|
|
520
|
-
export type inferError<T> = T extends
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
? TError
|
|
524
|
-
: T extends InfiniteQueryHook<any, any, infer TError>
|
|
525
|
-
? TError
|
|
526
|
-
: T extends SuspenseInfiniteQueryHook<any, any, infer TError>
|
|
520
|
+
export type inferError<T> = T extends {
|
|
521
|
+
fetcher: ExposeFetcher<any, infer TError, any>
|
|
522
|
+
}
|
|
527
523
|
? TError
|
|
528
|
-
: T extends
|
|
524
|
+
: T extends ExposeMutationMethods<any, any, infer TError, any>
|
|
529
525
|
? TError
|
|
530
526
|
: never
|
|
531
527
|
|
|
@@ -589,6 +585,122 @@ export type inferCreateOptions<T> = T extends QueryHook<
|
|
|
589
585
|
infer TPageParam
|
|
590
586
|
>
|
|
591
587
|
? CreateSuspenseInfiniteQueryOptions<TFnData, TVariables, TError, TPageParam>
|
|
592
|
-
: T extends MutationHook<
|
|
593
|
-
|
|
588
|
+
: T extends MutationHook<
|
|
589
|
+
infer TFnData,
|
|
590
|
+
infer TVariables,
|
|
591
|
+
infer TError,
|
|
592
|
+
infer TContext
|
|
593
|
+
>
|
|
594
|
+
? CreateMutationOptions<TFnData, TError, TVariables, TContext>
|
|
594
595
|
: never
|
|
596
|
+
|
|
597
|
+
// router
|
|
598
|
+
|
|
599
|
+
export type RouterQuery<
|
|
600
|
+
TFnData,
|
|
601
|
+
TVariables = void,
|
|
602
|
+
TError = CompatibleError
|
|
603
|
+
> = Omit<CreateQueryOptions<TFnData, TVariables, TError>, 'queryKey'> & {
|
|
604
|
+
_type: `q`
|
|
605
|
+
}
|
|
606
|
+
|
|
607
|
+
export type RouterInfiniteQuery<
|
|
608
|
+
TFnData,
|
|
609
|
+
TVariables = void,
|
|
610
|
+
TError = CompatibleError,
|
|
611
|
+
TPageParam = number
|
|
612
|
+
> = Omit<
|
|
613
|
+
CreateInfiniteQueryOptions<TFnData, TVariables, TError, TPageParam>,
|
|
614
|
+
'queryKey'
|
|
615
|
+
> & {
|
|
616
|
+
_type: `inf`
|
|
617
|
+
}
|
|
618
|
+
|
|
619
|
+
export type RouterMutation<
|
|
620
|
+
TData = unknown,
|
|
621
|
+
TVariables = void,
|
|
622
|
+
TError = CompatibleError,
|
|
623
|
+
TContext = unknown
|
|
624
|
+
> = Omit<
|
|
625
|
+
CreateMutationOptions<TData, TVariables, TError, TContext>,
|
|
626
|
+
'mutationKey'
|
|
627
|
+
> & {
|
|
628
|
+
_type: `m`
|
|
629
|
+
}
|
|
630
|
+
|
|
631
|
+
type DefaultTo<T, D> = unknown extends T ? D : T
|
|
632
|
+
|
|
633
|
+
export type RouterConfig = Record<
|
|
634
|
+
string,
|
|
635
|
+
| RouterQuery<any, any, any>
|
|
636
|
+
| RouterInfiniteQuery<any, any, any, any>
|
|
637
|
+
| RouterMutation<any, any, any, any>
|
|
638
|
+
>
|
|
639
|
+
|
|
640
|
+
export type CreateRouter<TConfig extends RouterConfig> = {
|
|
641
|
+
[K in keyof TConfig]: TConfig[K] extends RouterMutation<
|
|
642
|
+
infer TFnData,
|
|
643
|
+
infer TVariables,
|
|
644
|
+
infer TError,
|
|
645
|
+
infer TContext
|
|
646
|
+
>
|
|
647
|
+
? {
|
|
648
|
+
useMutation: MutationHook<
|
|
649
|
+
TFnData,
|
|
650
|
+
DefaultTo<TVariables, void>,
|
|
651
|
+
DefaultTo<TError, CompatibleError>,
|
|
652
|
+
TContext
|
|
653
|
+
>
|
|
654
|
+
} & ExposeMutationMethods<
|
|
655
|
+
TFnData,
|
|
656
|
+
DefaultTo<TVariables, void>,
|
|
657
|
+
DefaultTo<TError, CompatibleError>,
|
|
658
|
+
TContext
|
|
659
|
+
>
|
|
660
|
+
: TConfig[K] extends RouterInfiniteQuery<
|
|
661
|
+
infer TFnData,
|
|
662
|
+
infer TVariables,
|
|
663
|
+
infer TError,
|
|
664
|
+
infer TPageParam
|
|
665
|
+
>
|
|
666
|
+
? {
|
|
667
|
+
useInfiniteQuery: InfiniteQueryHook<
|
|
668
|
+
TFnData,
|
|
669
|
+
DefaultTo<TVariables, void>,
|
|
670
|
+
DefaultTo<TError, CompatibleError>,
|
|
671
|
+
TPageParam
|
|
672
|
+
>
|
|
673
|
+
useSuspenseInfiniteQuery: SuspenseInfiniteQueryHook<
|
|
674
|
+
TFnData,
|
|
675
|
+
DefaultTo<TVariables, void>,
|
|
676
|
+
DefaultTo<TError, CompatibleError>,
|
|
677
|
+
TPageParam
|
|
678
|
+
>
|
|
679
|
+
} & ExposeMethods<
|
|
680
|
+
TFnData,
|
|
681
|
+
DefaultTo<TVariables, void>,
|
|
682
|
+
DefaultTo<TError, CompatibleError>,
|
|
683
|
+
TPageParam
|
|
684
|
+
>
|
|
685
|
+
: TConfig[K] extends Omit<
|
|
686
|
+
RouterQuery<infer TFnData, infer TVariables, infer TError>,
|
|
687
|
+
'queryKey'
|
|
688
|
+
>
|
|
689
|
+
? {
|
|
690
|
+
useQuery: QueryHook<
|
|
691
|
+
TFnData,
|
|
692
|
+
DefaultTo<TVariables, void>,
|
|
693
|
+
DefaultTo<TError, CompatibleError>
|
|
694
|
+
>
|
|
695
|
+
useSuspenseQuery: SuspenseQueryHook<
|
|
696
|
+
TFnData,
|
|
697
|
+
DefaultTo<TVariables, void>,
|
|
698
|
+
DefaultTo<TError, CompatibleError>
|
|
699
|
+
>
|
|
700
|
+
} & ExposeMethods<
|
|
701
|
+
TFnData,
|
|
702
|
+
DefaultTo<TVariables, void>,
|
|
703
|
+
DefaultTo<TError, CompatibleError>
|
|
704
|
+
>
|
|
705
|
+
: never
|
|
706
|
+
} & { getKey: () => [string] }
|