Skip to content

Commit 5c17d1a

Browse files
author
liaoxuan
committed
feat: optimize types of router
1 parent 66f1332 commit 5c17d1a

File tree

4 files changed

+116
-104
lines changed

4 files changed

+116
-104
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
"@rollup/plugin-commonjs": "^22.0.2",
2828
"@rollup/plugin-node-resolve": "^13.2.1",
2929
"@rollup/plugin-replace": "^4.0.0",
30-
"@tanstack/react-query": "^5.0.0",
30+
"@tanstack/react-query": "^5.18.0",
3131
"@testing-library/jest-dom": "^5.16.5",
3232
"@testing-library/react": "^14.0.0",
3333
"@trivago/prettier-plugin-sort-imports": "^4.2.0",

src/router.ts

+22-18
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@ import { createSuspenseInfiniteQuery } from './createSuspenseInfiniteQuery'
55
import { createSuspenseQuery } from './createSuspenseQuery'
66
import type {
77
CompatibleError,
8-
CreateInfiniteQueryOptions,
9-
CreateMutationOptions,
10-
CreateQueryOptions,
118
CreateRouter,
129
RouterConfig,
1310
RouterInfiniteQuery,
11+
RouterInfiniteQueryOptions,
1412
RouterMutation,
13+
RouterMutationOptions,
1514
RouterQuery,
15+
RouterQueryOptions,
1616
} from './types'
1717

1818
const buildRouter = (keys: string[], config: RouterConfig) => {
@@ -54,19 +54,19 @@ const buildRouter = (keys: string[], config: RouterConfig) => {
5454
}
5555

5656
export const router = <TConfig extends RouterConfig>(
57-
scope: string,
57+
key: string,
5858
config: TConfig
5959
): CreateRouter<TConfig> => {
60-
return buildRouter([scope], config)
60+
return buildRouter([key], config)
6161
}
6262

6363
router.query = <TFnData, TVariables = void, TError = CompatibleError>(
64-
options: Omit<CreateQueryOptions<TFnData, TVariables, TError>, 'queryKey'>
65-
): RouterQuery<TFnData, TVariables, TError> => {
64+
options: RouterQueryOptions<TFnData, TVariables, TError>
65+
) => {
6666
return {
6767
...options,
6868
_type: 'q',
69-
}
69+
} as RouterQuery<TFnData, TVariables, TError>
7070
}
7171

7272
router.infiniteQuery = <
@@ -75,12 +75,14 @@ router.infiniteQuery = <
7575
TError = CompatibleError,
7676
TPageParam = number
7777
>(
78-
options: Omit<
79-
CreateInfiniteQueryOptions<TFnData, TVariables, TError, TPageParam>,
80-
'queryKey'
78+
options: RouterInfiniteQueryOptions<TFnData, TVariables, TError, TPageParam>
79+
) => {
80+
return { ...options, _type: 'inf' } as RouterInfiniteQuery<
81+
TFnData,
82+
TVariables,
83+
TError,
84+
TPageParam
8185
>
82-
): RouterInfiniteQuery<TFnData, TVariables, TError, TPageParam> => {
83-
return { ...options, _type: 'inf' }
8486
}
8587

8688
router.mutation = <
@@ -89,10 +91,12 @@ router.mutation = <
8991
TError = CompatibleError,
9092
TContext = unknown
9193
>(
92-
options: Omit<
93-
CreateMutationOptions<TFnData, TVariables, TError, TContext>,
94-
'mutationKey'
94+
options: RouterMutationOptions<TFnData, TVariables, TError, TContext>
95+
) => {
96+
return { ...options, _type: 'm' } as RouterMutation<
97+
TFnData,
98+
TVariables,
99+
TError,
100+
TContext
95101
>
96-
): RouterMutation<TFnData, TVariables, TError, TContext> => {
97-
return { ...options, _type: 'm' }
98102
}

src/types.ts

+84-76
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ type DeepPartial<T> = T extends object
6060
}
6161
: T
6262

63+
type DefaultTo<T, D> = unknown extends T ? D : T
64+
6365
export type CompatibleError = CompatibleWithV4<DefaultError, Error>
6466

6567
export type Fetcher<TFnData, TVariables = void, TPageParam = never> = (
@@ -596,92 +598,122 @@ export type inferCreateOptions<T> = T extends QueryHook<
596598

597599
// router
598600

601+
export type RouterQueryOptions<
602+
TFnData,
603+
TVariables = void,
604+
TError = CompatibleError
605+
> = Omit<CreateQueryOptions<TFnData, TVariables, TError>, 'queryKey'>
606+
599607
export type RouterQuery<
600608
TFnData,
601609
TVariables = void,
602610
TError = CompatibleError
603-
> = Omit<CreateQueryOptions<TFnData, TVariables, TError>, 'queryKey'> & {
611+
> = RouterQueryOptions<TFnData, TVariables, TError> & {
604612
_type: `q`
605613
}
606614

607-
export type RouterInfiniteQuery<
615+
export type ResolvedRouterQuery<
616+
TFnData,
617+
TVariables = void,
618+
TError = CompatibleError
619+
> = {
620+
useQuery: QueryHook<TFnData, TVariables, TError>
621+
useSuspenseQuery: SuspenseQueryHook<TFnData, TVariables, TError>
622+
} & ExposeMethods<TFnData, TVariables, TError>
623+
624+
export type RouterInfiniteQueryOptions<
608625
TFnData,
609626
TVariables = void,
610627
TError = CompatibleError,
611628
TPageParam = number
612629
> = Omit<
613630
CreateInfiniteQueryOptions<TFnData, TVariables, TError, TPageParam>,
614631
'queryKey'
632+
>
633+
634+
export type RouterInfiniteQuery<
635+
TFnData,
636+
TVariables = void,
637+
TError = CompatibleError,
638+
TPageParam = number
639+
> = RouterInfiniteQueryOptions<
640+
TFnData,
641+
TVariables,
642+
TError,
643+
Clone<TPageParam>
615644
> & {
616645
_type: `inf`
617646
}
618647

619-
export type RouterMutation<
648+
export type ResolvedRouterInfiniteQuery<
649+
TFnData,
650+
TVariables = void,
651+
TError = CompatibleError,
652+
TPageParam = number
653+
> = {
654+
useInfiniteQuery: InfiniteQueryHook<TFnData, TVariables, TError, TPageParam>
655+
useSuspenseInfiniteQuery: SuspenseInfiniteQueryHook<
656+
TFnData,
657+
TVariables,
658+
TError,
659+
TPageParam
660+
>
661+
} & ExposeMethods<TFnData, TVariables, TError, TPageParam>
662+
663+
export type RouterMutationOptions<
620664
TData = unknown,
621665
TVariables = void,
622666
TError = CompatibleError,
623667
TContext = unknown
624668
> = Omit<
625669
CreateMutationOptions<TData, TVariables, TError, TContext>,
626670
'mutationKey'
627-
> & {
671+
>
672+
673+
export type RouterMutation<
674+
TData = unknown,
675+
TVariables = void,
676+
TError = CompatibleError,
677+
TContext = unknown
678+
> = RouterMutationOptions<TData, TVariables, TError, TContext> & {
628679
_type: `m`
629680
}
630681

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-
| Record<
639-
string,
640-
| RouterQuery<any, any, any>
641-
| RouterInfiniteQuery<any, any, any, any>
642-
| RouterMutation<any, any, any, any>
643-
| Record<
644-
string,
645-
| RouterQuery<any, any, any>
646-
| RouterInfiniteQuery<any, any, any, any>
647-
| RouterMutation<any, any, any, any>
648-
| Record<
649-
string,
650-
| RouterQuery<any, any, any>
651-
| RouterInfiniteQuery<any, any, any, any>
652-
| RouterMutation<any, any, any, any>
653-
| Record<
654-
string,
655-
| RouterQuery<any, any, any>
656-
| RouterInfiniteQuery<any, any, any, any>
657-
| RouterMutation<any, any, any, any>
658-
| Record<
659-
string,
660-
| RouterQuery<any, any, any>
661-
| RouterInfiniteQuery<any, any, any, any>
662-
| RouterMutation<any, any, any, any>
663-
>
664-
>
665-
>
666-
>
667-
>
682+
export type ResolvedRouterMutation<
683+
TData = unknown,
684+
TVariables = void,
685+
TError = CompatibleError,
686+
TContext = unknown
687+
> = {
688+
useMutation: MutationHook<
689+
TData,
690+
DefaultTo<TVariables, void>,
691+
DefaultTo<TError, CompatibleError>,
692+
TContext
693+
>
694+
} & ExposeMutationMethods<
695+
TData,
696+
DefaultTo<TVariables, void>,
697+
DefaultTo<TError, CompatibleError>,
698+
TContext
668699
>
669700

701+
export interface RouterConfig {
702+
[k: string]:
703+
| RouterQuery<any, any, any>
704+
| RouterInfiniteQuery<any, any, any, any>
705+
| RouterMutation<any, any, any, any>
706+
| RouterConfig
707+
}
708+
670709
export type CreateRouter<TConfig extends RouterConfig> = {
671710
[K in keyof TConfig]: TConfig[K] extends RouterMutation<
672711
infer TFnData,
673712
infer TVariables,
674713
infer TError,
675714
infer TContext
676715
>
677-
? {
678-
useMutation: MutationHook<
679-
TFnData,
680-
DefaultTo<TVariables, void>,
681-
DefaultTo<TError, CompatibleError>,
682-
TContext
683-
>
684-
} & ExposeMutationMethods<
716+
? ResolvedRouterMutation<
685717
TFnData,
686718
DefaultTo<TVariables, void>,
687719
DefaultTo<TError, CompatibleError>,
@@ -693,20 +725,7 @@ export type CreateRouter<TConfig extends RouterConfig> = {
693725
infer TError,
694726
infer TPageParam
695727
>
696-
? {
697-
useInfiniteQuery: InfiniteQueryHook<
698-
TFnData,
699-
DefaultTo<TVariables, void>,
700-
DefaultTo<TError, CompatibleError>,
701-
DefaultTo<TPageParam, number>
702-
>
703-
useSuspenseInfiniteQuery: SuspenseInfiniteQueryHook<
704-
TFnData,
705-
DefaultTo<TVariables, void>,
706-
DefaultTo<TError, CompatibleError>,
707-
DefaultTo<TPageParam, number>
708-
>
709-
} & ExposeMethods<
728+
? ResolvedRouterInfiniteQuery<
710729
TFnData,
711730
DefaultTo<TVariables, void>,
712731
DefaultTo<TError, CompatibleError>,
@@ -716,23 +735,12 @@ export type CreateRouter<TConfig extends RouterConfig> = {
716735
RouterQuery<infer TFnData, infer TVariables, infer TError>,
717736
'queryKey'
718737
>
719-
? {
720-
useQuery: QueryHook<
721-
TFnData,
722-
DefaultTo<TVariables, void>,
723-
DefaultTo<TError, CompatibleError>
724-
>
725-
useSuspenseQuery: SuspenseQueryHook<
726-
TFnData,
727-
DefaultTo<TVariables, void>,
728-
DefaultTo<TError, CompatibleError>
729-
>
730-
} & ExposeMethods<
738+
? ResolvedRouterQuery<
731739
TFnData,
732740
DefaultTo<TVariables, void>,
733741
DefaultTo<TError, CompatibleError>
734742
>
735743
: TConfig[K] extends RouterConfig
736744
? CreateRouter<TConfig[K]>
737745
: never
738-
} & { getKey: () => [string] }
746+
} & { getKey: () => string[] }

yarn.lock

+9-9
Original file line numberDiff line numberDiff line change
@@ -1676,17 +1676,17 @@
16761676
dependencies:
16771677
"@sinonjs/commons" "^3.0.0"
16781678

1679-
"@tanstack/query-core@5.8.3":
1680-
version "5.8.3"
1681-
resolved "https://registry.yarnpkg.com/@tanstack/query-core/-/query-core-5.8.3.tgz#7c6d62721518223e8b27f1a0b5e9bd4e3bb7ecd8"
1682-
integrity sha512-SWFMFtcHfttLYif6pevnnMYnBvxKf3C+MHMH7bevyYfpXpTMsLB9O6nNGBdWSoPwnZRXFNyNeVZOw25Wmdasow==
1679+
"@tanstack/query-core@5.18.0":
1680+
version "5.18.0"
1681+
resolved "https://registry.yarnpkg.com/@tanstack/query-core/-/query-core-5.18.0.tgz#445a0cbf94b49d57132255c74570cfb63cf444c2"
1682+
integrity sha512-8c6nxeAnGHxIDZIyDmHdmgFt4D+LprAQaJmjsnM4szcIjsWOyFlzxdqQUuQ/XuQRvUgqYaqlJTtDADlSS7pTPQ==
16831683

1684-
"@tanstack/react-query@^5.0.0":
1685-
version "5.8.4"
1686-
resolved "https://registry.yarnpkg.com/@tanstack/react-query/-/react-query-5.8.4.tgz#7d5ef4dc4e2985fdc607d0f30ff79a8453abe652"
1687-
integrity sha512-CD+AkXzg8J72JrE6ocmuBEJfGzEzu/bzkD6sFXFDDB5yji9N20JofXZlN6n0+CaPJuIi+e4YLCbGsyPFKkfNQA==
1684+
"@tanstack/react-query@^5.18.0":
1685+
version "5.18.0"
1686+
resolved "https://registry.yarnpkg.com/@tanstack/react-query/-/react-query-5.18.0.tgz#da948737c904e7796df2aab2e52a92bdd755ffb1"
1687+
integrity sha512-7FKxNfxxKEL7n3ADpwp81Fy4FX85hNkYVzQQVQsF0JAPl93c3d1gmNZMIbEtOqgYfom1/ontGh3FiZGYj3xyWA==
16881688
dependencies:
1689-
"@tanstack/query-core" "5.8.3"
1689+
"@tanstack/query-core" "5.18.0"
16901690

16911691
"@testing-library/dom@^9.0.0":
16921692
version "9.3.0"

0 commit comments

Comments
 (0)