@tanstack/router-core 0.0.1-beta.151 → 0.0.1-beta.153

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/src/route.ts CHANGED
@@ -1,9 +1,8 @@
1
1
  import { ParsePathParams } from './link'
2
- import { AnyRouter, Router, RouteMatch } from './router'
2
+ import { AnyRouter, Router, RouteMatch, RegisteredRouter } from './router'
3
3
  import { IsAny, NoInfer, PickRequired, UnionToIntersection } from './utils'
4
4
  import invariant from 'tiny-invariant'
5
5
  import { joinPaths, trimPath } from './path'
6
- import { AnyRoutesInfo, DefaultRoutesInfo } from './routeInfo'
7
6
 
8
7
  export const rootRouteId = '__root__' as const
9
8
  export type RootRouteId = typeof rootRouteId
@@ -72,7 +71,7 @@ export type ComponentPropsFromRoute<TRoute> = TRoute extends Route<
72
71
  infer TContext,
73
72
  infer TRouterContext,
74
73
  infer TChildren,
75
- infer TRoutesInfo
74
+ infer TRouteTree
76
75
  >
77
76
  ? RouteProps<TLoader, TFullSearchSchema, TAllParams, TRouteContext, TContext>
78
77
  : never
@@ -93,11 +92,11 @@ export type RouteLoaderFromRoute<TRoute extends AnyRoute> = LoaderFn<
93
92
  export type RouteProps<
94
93
  TLoader = unknown,
95
94
  TFullSearchSchema extends AnySearchSchema = AnySearchSchema,
96
- TAllParams = AnyPathParams,
97
- TRouteContext = AnyContext,
98
- TContext = AnyContext,
95
+ TAllParams extends AnyPathParams = AnyPathParams,
96
+ TRouteContext extends AnyContext = AnyContext,
97
+ TContext extends AnyContext = AnyContext,
99
98
  > = {
100
- useMatch: () => RouteMatch<AnyRoutesInfo, AnyRoute>
99
+ useMatch: () => RouteMatch<any, any>
101
100
  useLoader: () => UseLoaderResult<TLoader>
102
101
  useSearch: <
103
102
  TStrict extends boolean = true,
@@ -135,24 +134,12 @@ export type RouteOptions<
135
134
  TParentSearchSchema extends AnySearchSchema = {},
136
135
  TSearchSchema extends AnySearchSchema = {},
137
136
  TFullSearchSchema extends AnySearchSchema = TSearchSchema,
138
- TParentParams extends AnyPathParams = AnyPathParams,
139
- TParams extends AnyPathParams = Record<ParsePathParams<TPath>, string>,
137
+ TParams extends AnyPathParams = AnyPathParams,
140
138
  TAllParams extends AnyPathParams = TParams,
141
139
  TParentContext extends AnyContext = AnyContext,
142
- TAllParentContext extends IsAny<
143
- TParentRoute['__types']['allParams'],
144
- TParentContext,
145
- TParentRoute['__types']['allParams'] & TParentContext
146
- > = IsAny<
147
- TParentRoute['__types']['allParams'],
148
- TParentContext,
149
- TParentRoute['__types']['allParams'] & TParentContext
150
- >,
140
+ TAllParentContext extends AnyContext = AnyContext,
151
141
  TRouteContext extends RouteContext = RouteContext,
152
- TContext extends MergeParamsFromParent<
153
- TAllParentContext,
154
- TRouteContext
155
- > = MergeParamsFromParent<TAllParentContext, TRouteContext>,
142
+ TContext extends AnyContext = AnyContext,
156
143
  > = BaseRouteOptions<
157
144
  TParentRoute,
158
145
  TCustomId,
@@ -161,7 +148,6 @@ export type RouteOptions<
161
148
  TParentSearchSchema,
162
149
  TSearchSchema,
163
150
  TFullSearchSchema,
164
- TParentParams,
165
151
  TParams,
166
152
  TAllParams,
167
153
  TParentContext,
@@ -183,6 +169,14 @@ export type ParamsFallback<
183
169
  TParams,
184
170
  > = unknown extends TParams ? Record<ParsePathParams<TPath>, string> : TParams
185
171
 
172
+ type Prefix<T extends string, U extends string> = U extends `${T}${infer _}`
173
+ ? U
174
+ : never
175
+
176
+ type PrefixOrExact<T extends string, U extends string> = U extends T
177
+ ? U
178
+ : Prefix<T, U>
179
+
186
180
  export type BaseRouteOptions<
187
181
  TParentRoute extends AnyRoute = AnyRoute,
188
182
  TCustomId extends string = string,
@@ -191,25 +185,14 @@ export type BaseRouteOptions<
191
185
  TParentSearchSchema extends AnySearchSchema = {},
192
186
  TSearchSchema extends AnySearchSchema = {},
193
187
  TFullSearchSchema extends AnySearchSchema = TSearchSchema,
194
- TParentParams extends AnyPathParams = AnyPathParams,
195
188
  TParams = unknown,
196
189
  TAllParams = ParamsFallback<TPath, TParams>,
197
190
  TParentContext extends AnyContext = AnyContext,
198
- TAllParentContext extends IsAny<
199
- TParentRoute['__types']['allParams'],
200
- TParentContext,
201
- TParentRoute['__types']['allParams'] & TParentContext
202
- > = IsAny<
203
- TParentRoute['__types']['allParams'],
204
- TParentContext,
205
- TParentRoute['__types']['allParams'] & TParentContext
206
- >,
191
+ TAllParentContext extends AnyContext = AnyContext,
207
192
  TRouteContext extends RouteContext = RouteContext,
208
- TContext extends MergeParamsFromParent<
209
- TAllParentContext,
210
- TRouteContext
211
- > = MergeParamsFromParent<TAllParentContext, TRouteContext>,
193
+ TContext extends AnyContext = AnyContext,
212
194
  > = RoutePathOptions<TCustomId, TPath> & {
195
+ layoutLimit?: string
213
196
  getParentRoute: () => TParentRoute
214
197
  validateSearch?: SearchSchemaValidator<TSearchSchema, TParentSearchSchema>
215
198
  loader?: LoaderFn<
@@ -515,27 +498,6 @@ export interface AnyRoute
515
498
  any
516
499
  > {}
517
500
 
518
- export type AnyRouteWithRouterContext<TRouterContext extends AnyContext> =
519
- Route<
520
- any,
521
- any,
522
- any,
523
- any,
524
- any,
525
- any,
526
- any,
527
- any,
528
- any,
529
- any,
530
- any,
531
- any,
532
- any,
533
- any,
534
- TRouterContext,
535
- any,
536
- any
537
- >
538
-
539
501
  export type MergeParamsFromParent<T, U> = IsAny<T, U, T & U>
540
502
 
541
503
  export type UseLoaderResult<T> = T extends Record<PropertyKey, infer U>
@@ -555,43 +517,62 @@ export type StreamedPromise<T> = {
555
517
  resolve: (value: T) => void
556
518
  }
557
519
 
520
+ export type RouteConstraints = {
521
+ TParentRoute: AnyRoute
522
+ TPath: string
523
+ TFullPath: string
524
+ TCustomId: string
525
+ TId: string
526
+ TSearchSchema: AnySearchSchema
527
+ TFullSearchSchema: AnySearchSchema
528
+ TParams: Record<string, any>
529
+ TAllParams: Record<string, any>
530
+ TParentContext: AnyContext
531
+ TAllParentContext: AnyContext
532
+ TRouteContext: RouteContext
533
+ TContext: AnyContext
534
+ TRouterContext: AnyContext
535
+ TChildren: unknown
536
+ TRouteTree: AnyRoute
537
+ }
538
+
558
539
  export class Route<
559
- TParentRoute extends AnyRoute = AnyRoute,
560
- TPath extends string = '/',
561
- TFullPath extends ResolveFullPath<TParentRoute, TPath> = ResolveFullPath<
540
+ TParentRoute extends RouteConstraints['TParentRoute'] = AnyRoute,
541
+ TPath extends RouteConstraints['TPath'] = '/',
542
+ TFullPath extends RouteConstraints['TFullPath'] = ResolveFullPath<
562
543
  TParentRoute,
563
544
  TPath
564
545
  >,
565
- TCustomId extends string = string,
566
- TId extends ResolveId<TParentRoute, TCustomId, TPath> = ResolveId<
546
+ TCustomId extends RouteConstraints['TCustomId'] = string,
547
+ TId extends RouteConstraints['TId'] = ResolveId<
567
548
  TParentRoute,
568
549
  TCustomId,
569
550
  TPath
570
551
  >,
571
552
  TLoader = unknown,
572
- TSearchSchema extends AnySearchSchema = {},
573
- TFullSearchSchema extends AnySearchSchema = ResolveFullSearchSchema<
553
+ TSearchSchema extends RouteConstraints['TSearchSchema'] = {},
554
+ TFullSearchSchema extends RouteConstraints['TFullSearchSchema'] = ResolveFullSearchSchema<
574
555
  TParentRoute,
575
556
  TSearchSchema
576
557
  >,
577
- TParams extends Record<ParsePathParams<TPath>, any> = Record<
558
+ TParams extends RouteConstraints['TParams'] = Record<
578
559
  ParsePathParams<TPath>,
579
560
  string
580
561
  >,
581
- TAllParams extends MergeParamsFromParent<
562
+ TAllParams extends RouteConstraints['TAllParams'] = MergeParamsFromParent<
582
563
  TParentRoute['__types']['allParams'],
583
564
  TParams
584
- > = MergeParamsFromParent<TParentRoute['__types']['allParams'], TParams>,
585
- TParentContext extends TParentRoute['__types']['routeContext'] = TParentRoute['__types']['routeContext'],
586
- TAllParentContext extends TParentRoute['__types']['context'] = TParentRoute['__types']['context'],
587
- TRouteContext extends RouteContext = RouteContext,
588
- TContext extends MergeParamsFromParent<
565
+ >,
566
+ TParentContext extends RouteConstraints['TParentContext'] = TParentRoute['__types']['routeContext'],
567
+ TAllParentContext extends RouteConstraints['TAllParentContext'] = TParentRoute['__types']['context'],
568
+ TRouteContext extends RouteConstraints['TRouteContext'] = RouteContext,
569
+ TContext extends RouteConstraints['TContext'] = MergeParamsFromParent<
589
570
  TParentRoute['__types']['context'],
590
571
  TRouteContext
591
- > = MergeParamsFromParent<TParentRoute['__types']['context'], TRouteContext>,
592
- TRouterContext extends AnyContext = AnyContext,
593
- TChildren extends unknown = unknown,
594
- TRoutesInfo extends DefaultRoutesInfo = DefaultRoutesInfo,
572
+ >,
573
+ TRouterContext extends RouteConstraints['TRouterContext'] = AnyContext,
574
+ TChildren extends RouteConstraints['TChildren'] = unknown,
575
+ TRouteTree extends RouteConstraints['TRouteTree'] = AnyRoute,
595
576
  > {
596
577
  __types!: {
597
578
  parentRoute: TParentRoute
@@ -610,7 +591,7 @@ export class Route<
610
591
  routeContext: TRouteContext
611
592
  context: TContext
612
593
  children: TChildren
613
- routesInfo: TRoutesInfo
594
+ routeTree: TRouteTree
614
595
  routerContext: TRouterContext
615
596
  }
616
597
  isRoot: TParentRoute extends Route<any> ? true : false
@@ -622,7 +603,6 @@ export class Route<
622
603
  InferFullSearchSchema<TParentRoute>,
623
604
  TSearchSchema,
624
605
  TFullSearchSchema,
625
- TParentRoute['__types']['allParams'],
626
606
  TParams,
627
607
  TAllParams,
628
608
  TParentContext,
@@ -650,7 +630,7 @@ export class Route<
650
630
  // Optional
651
631
  children?: TChildren
652
632
  originalIndex?: number
653
- router?: Router<TRoutesInfo['routeTree'], TRoutesInfo>
633
+ router?: AnyRouter
654
634
  rank!: number
655
635
 
656
636
  constructor(
@@ -662,7 +642,6 @@ export class Route<
662
642
  InferFullSearchSchema<TParentRoute>,
663
643
  TSearchSchema,
664
644
  TFullSearchSchema,
665
- TParentRoute['__types']['allParams'],
666
645
  TParams,
667
646
  TAllParams,
668
647
  TParentContext,
@@ -694,7 +673,6 @@ export class Route<
694
673
  TPath,
695
674
  InferFullSearchSchema<TParentRoute>,
696
675
  TSearchSchema,
697
- TParentRoute['__types']['allParams'],
698
676
  TParams
699
677
  > &
700
678
  RoutePathOptionsIntersection<TCustomId, TPath>
@@ -768,7 +746,7 @@ export class Route<
768
746
  TContext,
769
747
  TRouterContext,
770
748
  TNewChildren,
771
- TRoutesInfo
749
+ TRouteTree
772
750
  > => {
773
751
  this.children = children as any
774
752
  return this as any
@@ -805,7 +783,7 @@ export class RouterContext<TRouterContext extends {}> {
805
783
  TContext extends RouteContext = RouteContext,
806
784
  >(
807
785
  options?: Omit<
808
- RouteOptions<AnyRoute, RootRouteId, '', {}, TSearchSchema, {}, {}>,
786
+ RouteOptions<AnyRoute, RootRouteId, '', {}, TSearchSchema, {}>,
809
787
  | 'path'
810
788
  | 'id'
811
789
  | 'getParentRoute'
@@ -846,7 +824,7 @@ export class RootRoute<
846
824
  > {
847
825
  constructor(
848
826
  options?: Omit<
849
- RouteOptions<AnyRoute, RootRouteId, '', TLoader, TSearchSchema, {}, {}>,
827
+ RouteOptions<AnyRoute, RootRouteId, '', TLoader, TSearchSchema, {}>,
850
828
  | 'path'
851
829
  | 'id'
852
830
  | 'getParentRoute'
@@ -862,10 +840,7 @@ export class RootRoute<
862
840
  export type ResolveFullPath<
863
841
  TParentRoute extends AnyRoute,
864
842
  TPath extends string,
865
- TPrefixed extends RoutePrefix<TParentRoute['fullPath'], TPath> = RoutePrefix<
866
- TParentRoute['fullPath'],
867
- TPath
868
- >,
843
+ TPrefixed = RoutePrefix<TParentRoute['fullPath'], TPath>,
869
844
  > = TPrefixed extends RootRouteId ? '/' : TPrefixed
870
845
 
871
846
  type RoutePrefix<
package/src/routeInfo.ts CHANGED
@@ -1,152 +1,71 @@
1
1
  import { AnyRoute, Route } from './route'
2
- import { AnyPathParams, AnySearchSchema, RootRouteId } from './route'
3
- import { IsAny, MergeUnion, Values } from './utils'
2
+ import { MergeUnion } from './utils'
4
3
 
5
- export interface AnyRoutesInfo {
6
- routeTree: AnyRoute
7
- routeUnion: AnyRoute
8
- routesById: Record<string, AnyRoute>
9
- routesByFullPath: Record<string, AnyRoute>
10
- routeIds: any
11
- routePaths: any
12
- routeIntersection: AnyRoute
13
- fullSearchSchema: Record<string, any>
14
- allParams: Record<string, any>
15
- }
16
-
17
- export interface DefaultRoutesInfo {
18
- routeTree: AnyRoute
19
- routeUnion: AnyRoute
20
- routesById: Record<string, Route>
21
- routesByFullPath: Record<string, Route>
22
- routeIds: string
23
- routePaths: string
24
- routeIntersection: AnyRoute
25
- fullSearchSchema: AnySearchSchema
26
- allParams: AnyPathParams
27
- }
4
+ export type ParseRoute<TRouteTree extends AnyRoute> =
5
+ | TRouteTree
6
+ | ParseRouteChildren<TRouteTree>
28
7
 
29
- export interface RoutesInfo<TRouteTree extends AnyRoute = Route>
30
- extends RoutesInfoInner<TRouteTree, ParseRoute<TRouteTree>> {}
31
-
32
- export interface RoutesInfoInner<
33
- TRouteTree extends AnyRoute,
34
- TRouteUnion extends AnyRoute = Route,
35
- TRoutesById = { '/': TRouteUnion } & {
36
- [TRoute in TRouteUnion as TRoute['id']]: TRoute
37
- },
38
- // RoutePaths should always use index routes if possible, but not
39
- // force trailing slashes. To do this, we check if each route
40
- // has an index route registered and if it does, we omit the layout
41
- // route. Then for any index routes, we remove the trailing slash
42
- TRoutesByFullPath = { '/': TRouteUnion } & {
43
- [TRoute in TRouteUnion as TRoute['fullPath'] extends RootRouteId
44
- ? never
45
- : string extends TRoute['fullPath']
46
- ? never
47
- : `${TRoute['fullPath']}/` extends keyof TRoutesById
48
- ? never
49
- : TRoute['fullPath'] extends `${infer Trimmed}/`
50
- ? Trimmed
51
- : TRoute['fullPath']]: TRoute
52
- },
53
- > {
54
- routeTree: TRouteTree
55
- routeUnion: TRouteUnion
56
- routesById: TRoutesById
57
- routesByFullPath: TRoutesByFullPath
58
- routeIds: keyof TRoutesById
59
- routePaths: keyof TRoutesByFullPath
60
- routeIntersection: Route<
61
- TRouteUnion['__types']['parentRoute'], // TParentRoute,
62
- TRouteUnion['__types']['path'], // TPath,
63
- TRouteUnion['__types']['fullPath'], // TFullPath,
64
- TRouteUnion['__types']['customId'], // TCustomId,
65
- TRouteUnion['__types']['id'], // TId,
66
- TRouteUnion['__types']['loader'], // TId,
67
- MergeUnion<TRouteUnion['__types']['searchSchema']> & {}, // TSearchSchema,
68
- MergeUnion<TRouteUnion['__types']['fullSearchSchema']> & {}, // TFullSearchSchema,
69
- MergeUnion<TRouteUnion['__types']['params']>, // TParams,
70
- MergeUnion<TRouteUnion['__types']['allParams']>, // TAllParams,
71
- MergeUnion<TRouteUnion['__types']['parentContext']>, // TParentContext,
72
- MergeUnion<TRouteUnion['__types']['allParentContext']>, // TAllParentContext,
73
- MergeUnion<TRouteUnion['__types']['routeContext']> & {}, // TRouteContext,
74
- MergeUnion<TRouteUnion['__types']['context']> & {}, // TContext,
75
- MergeUnion<TRouteUnion['__types']['routerContext']> & {}, // TRouterContext,
76
- TRouteUnion['__types']['children'], // TChildren,
77
- TRouteUnion['__types']['routesInfo'] // TRoutesInfo,
8
+ export type ParseRouteChildren<TRouteTree extends AnyRoute> =
9
+ TRouteTree extends Route<
10
+ any,
11
+ any,
12
+ any,
13
+ any,
14
+ any,
15
+ any,
16
+ any,
17
+ any,
18
+ any,
19
+ any,
20
+ any,
21
+ any,
22
+ any,
23
+ any,
24
+ any,
25
+ infer TChildren,
26
+ any
78
27
  >
79
- fullSearchSchema: Partial<
80
- MergeUnion<TRouteUnion['__types']['fullSearchSchema']>
81
- >
82
- allParams: Partial<MergeUnion<TRouteUnion['__types']['allParams']>>
83
- }
28
+ ? unknown extends TChildren
29
+ ? never
30
+ : TChildren extends AnyRoute[]
31
+ ? {
32
+ [TId in TChildren[number]['id'] as string]: ParseRoute<
33
+ TChildren[number]
34
+ >
35
+ }[string]
36
+ : never
37
+ : never
84
38
 
85
- export type ParseRoute<TRouteTree> = TRouteTree extends AnyRoute
86
- ? TRouteTree | ParseRouteChildren<TRouteTree>
87
- : never
39
+ export type RoutesById<TRouteTree extends AnyRoute> = {
40
+ [K in ParseRoute<TRouteTree>['id'] as K['id']]: K
41
+ }
88
42
 
89
- export type ParseRouteChildren<TRouteTree> = TRouteTree extends Route<
90
- any,
91
- any,
92
- any,
93
- any,
94
- any,
95
- any,
96
- any,
97
- any,
98
- any,
99
- any,
100
- any,
101
- any,
102
- any,
103
- any,
104
- any,
105
- infer TChildren,
106
- any
43
+ export type RouteById<TRouteTree extends AnyRoute, TId> = Extract<
44
+ ParseRoute<TRouteTree>,
45
+ { id: TId }
107
46
  >
108
- ? unknown extends TChildren
109
- ? never
110
- : TChildren extends AnyRoute[]
111
- ? Values<{
112
- [TId in TChildren[number]['id']]: ParseRouteChild<
113
- TChildren[number],
114
- TId
115
- >
116
- }>
117
- : never
118
- : never
119
47
 
120
- export type ParseRouteChild<TRoute, TId> = TRoute extends AnyRoute
121
- ? ParseRoute<TRoute>
122
- : never
48
+ export type RouteIds<TRouteTree extends AnyRoute> = AnyRoute extends TRouteTree
49
+ ? string
50
+ : ParseRoute<TRouteTree>['id']
123
51
 
124
- export type RoutesById<TRoutesInfo extends AnyRoutesInfo> = {
125
- [K in keyof TRoutesInfo['routesById']]: TRoutesInfo['routesById'][K]
52
+ export type RoutesByPath<TRouteTree extends AnyRoute> = {
53
+ [K in ParseRoute<TRouteTree>['fullPath'] as K['fullPath']]: K
126
54
  }
127
55
 
128
- export type RouteById<
129
- TRoutesInfo extends AnyRoutesInfo,
130
- TId,
131
- > = TId extends keyof TRoutesInfo['routesById']
132
- ? IsAny<
133
- TRoutesInfo['routesById'][TId]['id'],
134
- Route,
135
- TRoutesInfo['routesById'][TId]
136
- >
137
- : never
56
+ export type RouteByPath<TRouteTree extends AnyRoute, TPath> = Extract<
57
+ ParseRoute<TRouteTree>,
58
+ { fullPath: TPath }
59
+ >
138
60
 
139
- export type RoutesByPath<TRoutesInfo extends AnyRoutesInfo> = {
140
- [K in keyof TRoutesInfo['routesByFullPath']]: TRoutesInfo['routesByFullPath'][K]
141
- }
61
+ export type RoutePaths<TRouteTree extends AnyRoute> =
62
+ | ParseRoute<TRouteTree>['fullPath']
63
+ | '/'
142
64
 
143
- export type RouteByPath<
144
- TRoutesInfo extends AnyRoutesInfo,
145
- TPath,
146
- > = TPath extends keyof TRoutesInfo['routesByFullPath']
147
- ? IsAny<
148
- TRoutesInfo['routesByFullPath'][TPath]['id'],
149
- Route,
150
- TRoutesInfo['routesByFullPath'][TPath]
151
- >
152
- : never
65
+ export type FullSearchSchema<TRouteTree extends AnyRoute> = MergeUnion<
66
+ ParseRoute<TRouteTree>['__types']['fullSearchSchema']
67
+ > & {}
68
+
69
+ export type AllParams<TRouteTree extends AnyRoute> = MergeUnion<
70
+ ParseRoute<TRouteTree>['__types']['allParams']
71
+ >