@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/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@tanstack/router-core",
3
3
  "author": "Tanner Linsley",
4
- "version": "0.0.1-beta.151",
4
+ "version": "0.0.1-beta.153",
5
5
  "license": "MIT",
6
6
  "repository": "tanstack/router",
7
7
  "homepage": "https://tanstack.com/router",
@@ -43,7 +43,7 @@
43
43
  "tiny-invariant": "^1.3.1",
44
44
  "tiny-warning": "^1.0.3",
45
45
  "@gisatcz/cross-package-react-context": "^0.2.0",
46
- "@tanstack/react-store": "0.0.1-beta.151"
46
+ "@tanstack/react-store": "0.0.1-beta.153"
47
47
  },
48
48
  "scripts": {
49
49
  "build": "rollup --config rollup.config.js",
package/src/fileRoute.ts CHANGED
@@ -1,8 +1,7 @@
1
- import { Last, ParsePathParams, Split } from './link'
1
+ import { ParsePathParams } from './link'
2
2
  import {
3
3
  AnyRoute,
4
4
  ResolveFullPath,
5
- ResolveId,
6
5
  AnySearchSchema,
7
6
  ResolveFullSearchSchema,
8
7
  MergeParamsFromParent,
@@ -12,8 +11,11 @@ import {
12
11
  InferFullSearchSchema,
13
12
  UpdatableRouteOptions,
14
13
  Route,
14
+ AnyPathParams,
15
+ RootRouteId,
16
+ TrimPathLeft,
17
+ RouteConstraints,
15
18
  } from './route'
16
- import { DefaultRoutesInfo } from './routeInfo'
17
19
 
18
20
  export interface FileRoutesByPath {
19
21
  // '/': {
@@ -21,62 +23,102 @@ export interface FileRoutesByPath {
21
23
  // }
22
24
  }
23
25
 
26
+ type Replace<
27
+ S extends string,
28
+ From extends string,
29
+ To extends string,
30
+ > = S extends `${infer Start}${From}${infer Rest}`
31
+ ? `${Start}${To}${Replace<Rest, From, To>}`
32
+ : S
33
+
34
+ export type TrimLeft<
35
+ T extends string,
36
+ S extends string,
37
+ > = T extends `${S}${infer U}` ? U : T
38
+
39
+ export type TrimRight<
40
+ T extends string,
41
+ S extends string,
42
+ > = T extends `${infer U}${S}` ? U : T
43
+
44
+ export type Trim<T extends string, S extends string> = TrimLeft<
45
+ TrimRight<T, S>,
46
+ S
47
+ >
48
+
49
+ export type RemoveUnderScores<T extends string> = Replace<
50
+ Replace<TrimRight<TrimLeft<T, '/_'>, '_'>, '_/', '/'>,
51
+ '/_',
52
+ '/'
53
+ >
54
+
55
+ export type ResolveFilePath<
56
+ TParentRoute extends AnyRoute,
57
+ TFilePath extends string,
58
+ > = TParentRoute['id'] extends RootRouteId
59
+ ? TrimPathLeft<TFilePath>
60
+ : Replace<
61
+ TrimPathLeft<TFilePath>,
62
+ TrimPathLeft<TParentRoute['__types']['customId']>,
63
+ ''
64
+ >
65
+
66
+ export type FileRoutePath<
67
+ TParentRoute extends AnyRoute,
68
+ TFilePath extends string,
69
+ > = ResolveFilePath<TParentRoute, TFilePath> extends `_${infer _}`
70
+ ? string
71
+ : ResolveFilePath<TParentRoute, TFilePath>
72
+
24
73
  export class FileRoute<
25
74
  TFilePath extends keyof FileRoutesByPath,
26
75
  TParentRoute extends AnyRoute = FileRoutesByPath[TFilePath]['parentRoute'],
27
- TPath extends string = Last<Split<TFilePath>>,
28
- TCustomId extends string = TPath extends `_${infer T}` ? T : string,
76
+ TId extends RouteConstraints['TId'] = TFilePath,
77
+ TPath extends RouteConstraints['TPath'] = FileRoutePath<
78
+ TParentRoute,
79
+ TFilePath
80
+ >,
81
+ TFullPath extends RouteConstraints['TFullPath'] = ResolveFullPath<
82
+ TParentRoute,
83
+ RemoveUnderScores<TPath>
84
+ >,
29
85
  > {
30
86
  constructor(public path: TFilePath) {}
31
87
 
32
88
  createRoute = <
33
- TFullPath extends ResolveFullPath<TParentRoute, TPath> = ResolveFullPath<
34
- TParentRoute,
35
- TPath
36
- >,
37
- TId extends ResolveId<TParentRoute, TCustomId, TPath> = ResolveId<
38
- TParentRoute,
39
- TCustomId,
40
- TPath
41
- >,
42
89
  TLoader = unknown,
43
- TSearchSchema extends AnySearchSchema = {},
44
- TFullSearchSchema extends AnySearchSchema = ResolveFullSearchSchema<
90
+ TSearchSchema extends RouteConstraints['TSearchSchema'] = {},
91
+ TFullSearchSchema extends RouteConstraints['TFullSearchSchema'] = ResolveFullSearchSchema<
45
92
  TParentRoute,
46
93
  TSearchSchema
47
94
  >,
48
- TParams extends Record<ParsePathParams<TPath>, any> = Record<
49
- ParsePathParams<TPath>,
50
- string
51
- >,
52
- TAllParams extends MergeParamsFromParent<
95
+ TParams extends RouteConstraints['TParams'] = ParsePathParams<TPath> extends never
96
+ ? AnyPathParams
97
+ : Record<ParsePathParams<TPath>, RouteConstraints['TPath']>,
98
+ TAllParams extends RouteConstraints['TAllParams'] = MergeParamsFromParent<
53
99
  TParentRoute['__types']['allParams'],
54
100
  TParams
55
- > = MergeParamsFromParent<TParentRoute['__types']['allParams'], TParams>,
56
- TParentContext extends TParentRoute['__types']['routeContext'] = TParentRoute['__types']['routeContext'],
57
- TAllParentContext extends TParentRoute['__types']['context'] = TParentRoute['__types']['context'],
58
- TRouteContext extends RouteContext = RouteContext,
59
- TContext extends MergeParamsFromParent<
60
- TParentRoute['__types']['context'],
61
- TRouteContext
62
- > = MergeParamsFromParent<
101
+ >,
102
+ TParentContext extends RouteConstraints['TParentContext'] = TParentRoute['__types']['routeContext'],
103
+ TAllParentContext extends RouteConstraints['TId'] = TParentRoute['__types']['context'],
104
+ TRouteContext extends RouteConstraints['TRouteContext'] = RouteContext,
105
+ TContext extends RouteConstraints['TContext'] = MergeParamsFromParent<
63
106
  TParentRoute['__types']['context'],
64
107
  TRouteContext
65
108
  >,
66
- TRouterContext extends AnyContext = AnyContext,
67
- TChildren extends unknown = unknown,
68
- TRoutesInfo extends DefaultRoutesInfo = DefaultRoutesInfo,
109
+ TRouterContext extends RouteConstraints['TRouterContext'] = AnyContext,
110
+ TChildren extends RouteConstraints['TChildren'] = unknown,
111
+ TRouteTree extends RouteConstraints['TRouteTree'] = AnyRoute,
69
112
  >(
70
113
  options: Omit<
71
114
  RouteOptions<
72
115
  TParentRoute,
73
- TCustomId,
74
- TPath,
116
+ string,
117
+ string,
75
118
  TLoader,
76
119
  InferFullSearchSchema<TParentRoute>,
77
120
  TSearchSchema,
78
121
  TFullSearchSchema,
79
- TParentRoute['__types']['allParams'],
80
122
  TParams,
81
123
  TAllParams,
82
124
  TParentContext,
@@ -98,7 +140,7 @@ export class FileRoute<
98
140
  TParentRoute,
99
141
  TPath,
100
142
  TFullPath,
101
- TCustomId,
143
+ TFilePath,
102
144
  TId,
103
145
  TLoader,
104
146
  TSearchSchema,
@@ -111,7 +153,7 @@ export class FileRoute<
111
153
  TContext,
112
154
  TRouterContext,
113
155
  TChildren,
114
- TRoutesInfo
156
+ TRouteTree
115
157
  > => {
116
158
  const route = new Route(options as any)
117
159
  ;(route as any).isRoot = false
package/src/link.ts CHANGED
@@ -1,5 +1,13 @@
1
- import { AnyRoutesInfo, RouteByPath } from './routeInfo'
2
- import { ParsedLocation, LocationState, RegisteredRoutesInfo } from './router'
1
+ import { Trim } from './fileRoute'
2
+ import { AnyRoute } from './route'
3
+ import {
4
+ AllParams,
5
+ FullSearchSchema,
6
+ RouteByPath,
7
+ RouteIds,
8
+ RoutePaths,
9
+ } from './routeInfo'
10
+ import { ParsedLocation, LocationState } from './router'
3
11
  import { NoInfer, PickRequired, UnionToIntersection, Updater } from './utils'
4
12
 
5
13
  export type LinkInfo =
@@ -19,7 +27,7 @@ export type LinkInfo =
19
27
  disabled?: boolean
20
28
  }
21
29
 
22
- type CleanPath<T extends string> = T extends `${infer L}//${infer R}`
30
+ export type CleanPath<T extends string> = T extends `${infer L}//${infer R}`
23
31
  ? CleanPath<`${CleanPath<L>}/${CleanPath<R>}`>
24
32
  : T extends `${infer L}//`
25
33
  ? `${CleanPath<L>}/`
@@ -49,11 +57,9 @@ export type Split<S, TIncludeTrailingSlash = true> = S extends unknown
49
57
  : never
50
58
  : never
51
59
 
52
- export type ParsePathParams<T extends string> = Split<T>[number] extends infer U
53
- ? U extends `$${infer V}`
54
- ? V
55
- : never
56
- : never
60
+ export type ParsePathParams<T extends string> = keyof {
61
+ [K in Trim<Split<T>[number], '_'> as K extends `$${infer L}` ? L : never]: K
62
+ }
57
63
 
58
64
  export type Join<T, Delimiter extends string = '/'> = T extends []
59
65
  ? ''
@@ -112,21 +118,21 @@ export type RelativeToPathAutoComplete<
112
118
  | AllPaths
113
119
 
114
120
  export type NavigateOptions<
115
- TRoutesInfo extends AnyRoutesInfo = RegisteredRoutesInfo,
116
- TFrom extends TRoutesInfo['routePaths'] = '/',
121
+ TRouteTree extends AnyRoute = AnyRoute,
122
+ TFrom extends RoutePaths<TRouteTree> = '/',
117
123
  TTo extends string = '',
118
- > = ToOptions<TRoutesInfo, TFrom, TTo> & {
124
+ > = ToOptions<TRouteTree, TFrom, TTo> & {
119
125
  // `replace` is a boolean that determines whether the navigation should replace the current history entry or push a new one.
120
126
  replace?: boolean
121
127
  }
122
128
 
123
129
  export type ToOptions<
124
- TRoutesInfo extends AnyRoutesInfo = RegisteredRoutesInfo,
125
- TFrom extends TRoutesInfo['routePaths'] = '/',
130
+ TRouteTree extends AnyRoute = AnyRoute,
131
+ TFrom extends RoutePaths<TRouteTree> = '/',
126
132
  TTo extends string = '',
127
133
  TResolvedTo = ResolveRelativePath<TFrom, NoInfer<TTo>>,
128
134
  > = {
129
- to?: ToPathOption<TRoutesInfo, TFrom, TTo>
135
+ to?: ToPathOption<TRouteTree, TFrom, TTo>
130
136
  // The new has string or a function to update it
131
137
  hash?: Updater<string>
132
138
  // State to pass to the history stack
@@ -135,37 +141,34 @@ export type ToOptions<
135
141
  from?: TFrom
136
142
  // // When using relative route paths, this option forces resolution from the current path, instead of the route API's path or `from` path
137
143
  // fromCurrent?: boolean
138
- } & CheckPath<TRoutesInfo, NoInfer<TResolvedTo>, {}> &
139
- SearchParamOptions<TRoutesInfo, TFrom, TResolvedTo> &
140
- PathParamOptions<TRoutesInfo, TFrom, TResolvedTo>
144
+ } & CheckPath<TRouteTree, NoInfer<TResolvedTo>, {}> &
145
+ SearchParamOptions<TRouteTree, TFrom, TResolvedTo> &
146
+ PathParamOptions<TRouteTree, TFrom, TResolvedTo>
141
147
 
142
148
  export type SearchParamOptions<
143
- TRoutesInfo extends AnyRoutesInfo,
149
+ TRouteTree extends AnyRoute,
144
150
  TFrom,
145
151
  TTo,
146
152
  TFromSchema = UnionToIntersection<
147
- TRoutesInfo['fullSearchSchema'] &
148
- RouteByPath<TRoutesInfo, TFrom> extends never
153
+ FullSearchSchema<TRouteTree> & RouteByPath<TRouteTree, TFrom> extends never
149
154
  ? {}
150
- : RouteByPath<TRoutesInfo, TFrom>['__types']['fullSearchSchema']
155
+ : RouteByPath<TRouteTree, TFrom>['__types']['fullSearchSchema']
151
156
  >,
152
157
  // Find the schema for the new path, and make optional any keys
153
158
  // that are already defined in the current schema
154
159
  TToSchema = Partial<
155
- RouteByPath<TRoutesInfo, TFrom>['__types']['fullSearchSchema']
160
+ RouteByPath<TRouteTree, TFrom>['__types']['fullSearchSchema']
156
161
  > &
157
162
  Omit<
158
- RouteByPath<TRoutesInfo, TTo>['__types']['fullSearchSchema'],
163
+ RouteByPath<TRouteTree, TTo>['__types']['fullSearchSchema'],
159
164
  keyof PickRequired<
160
- RouteByPath<TRoutesInfo, TFrom>['__types']['fullSearchSchema']
165
+ RouteByPath<TRouteTree, TFrom>['__types']['fullSearchSchema']
161
166
  >
162
167
  >,
163
168
  TFromFullSchema = UnionToIntersection<
164
- TRoutesInfo['fullSearchSchema'] & TFromSchema
165
- >,
166
- TToFullSchema = UnionToIntersection<
167
- TRoutesInfo['fullSearchSchema'] & TToSchema
169
+ FullSearchSchema<TRouteTree> & TFromSchema
168
170
  >,
171
+ TToFullSchema = UnionToIntersection<FullSearchSchema<TRouteTree> & TToSchema>,
169
172
  > = keyof PickRequired<TToSchema> extends never
170
173
  ? {
171
174
  search?: true | SearchReducer<TFromFullSchema, TToFullSchema>
@@ -179,25 +182,23 @@ type SearchReducer<TFrom, TTo> =
179
182
  | ((current: TFrom) => TTo)
180
183
 
181
184
  export type PathParamOptions<
182
- TRoutesInfo extends AnyRoutesInfo,
185
+ TRouteTree extends AnyRoute,
183
186
  TFrom,
184
187
  TTo,
185
188
  TFromSchema = UnionToIntersection<
186
- RouteByPath<TRoutesInfo, TFrom> extends never
189
+ RouteByPath<TRouteTree, TFrom> extends never
187
190
  ? {}
188
- : RouteByPath<TRoutesInfo, TFrom>['__types']['allParams']
191
+ : RouteByPath<TRouteTree, TFrom>['__types']['allParams']
189
192
  >,
190
193
  // Find the schema for the new path, and make optional any keys
191
194
  // that are already defined in the current schema
192
- TToSchema = Partial<RouteByPath<TRoutesInfo, TFrom>['__types']['allParams']> &
195
+ TToSchema = Partial<RouteByPath<TRouteTree, TFrom>['__types']['allParams']> &
193
196
  Omit<
194
- RouteByPath<TRoutesInfo, TTo>['__types']['allParams'],
195
- keyof PickRequired<
196
- RouteByPath<TRoutesInfo, TFrom>['__types']['allParams']
197
- >
197
+ RouteByPath<TRouteTree, TTo>['__types']['allParams'],
198
+ keyof PickRequired<RouteByPath<TRouteTree, TFrom>['__types']['allParams']>
198
199
  >,
199
- TFromFullParams = UnionToIntersection<TRoutesInfo['allParams'] & TFromSchema>,
200
- TToFullParams = UnionToIntersection<TRoutesInfo['allParams'] & TToSchema>,
200
+ TFromFullParams = UnionToIntersection<AllParams<TRouteTree> & TFromSchema>,
201
+ TToFullParams = UnionToIntersection<AllParams<TRouteTree> & TToSchema>,
201
202
  > = keyof PickRequired<TToSchema> extends never
202
203
  ? {
203
204
  params?: ParamsReducer<TFromFullParams, TToFullParams>
@@ -209,25 +210,25 @@ export type PathParamOptions<
209
210
  type ParamsReducer<TFrom, TTo> = TTo | ((current: TFrom) => TTo)
210
211
 
211
212
  export type ToPathOption<
212
- TRoutesInfo extends AnyRoutesInfo = RegisteredRoutesInfo,
213
- TFrom extends TRoutesInfo['routePaths'] = '/',
213
+ TRouteTree extends AnyRoute = AnyRoute,
214
+ TFrom extends RoutePaths<TRouteTree> = '/',
214
215
  TTo extends string = '',
215
216
  > =
216
217
  | TTo
217
218
  | RelativeToPathAutoComplete<
218
- TRoutesInfo['routePaths'],
219
+ RoutePaths<TRouteTree>,
219
220
  NoInfer<TFrom> extends string ? NoInfer<TFrom> : '',
220
221
  NoInfer<TTo> & string
221
222
  >
222
223
 
223
224
  export type ToIdOption<
224
- TRoutesInfo extends AnyRoutesInfo = RegisteredRoutesInfo,
225
- TFrom extends TRoutesInfo['routePaths'] = '/',
225
+ TRouteTree extends AnyRoute = AnyRoute,
226
+ TFrom extends RoutePaths<TRouteTree> = '/',
226
227
  TTo extends string = '',
227
228
  > =
228
229
  | TTo
229
230
  | RelativeToPathAutoComplete<
230
- TRoutesInfo['routeIds'],
231
+ RouteIds<TRouteTree>,
231
232
  NoInfer<TFrom> extends string ? NoInfer<TFrom> : '',
232
233
  NoInfer<TTo> & string
233
234
  >
@@ -239,10 +240,10 @@ export interface ActiveOptions {
239
240
  }
240
241
 
241
242
  export type LinkOptions<
242
- TRoutesInfo extends AnyRoutesInfo = RegisteredRoutesInfo,
243
- TFrom extends TRoutesInfo['routePaths'] = '/',
243
+ TRouteTree extends AnyRoute = AnyRoute,
244
+ TFrom extends RoutePaths<TRouteTree> = '/',
244
245
  TTo extends string = '',
245
- > = NavigateOptions<TRoutesInfo, TFrom, TTo> & {
246
+ > = NavigateOptions<TRouteTree, TFrom, TTo> & {
246
247
  // The standard anchor tag target attribute
247
248
  target?: HTMLAnchorElement['target']
248
249
  // Defaults to `{ exact: false, includeHash: false }`
@@ -256,47 +257,46 @@ export type LinkOptions<
256
257
  }
257
258
 
258
259
  export type CheckRelativePath<
259
- TRoutesInfo extends AnyRoutesInfo,
260
+ TRouteTree extends AnyRoute,
260
261
  TFrom,
261
262
  TTo,
262
263
  > = TTo extends string
263
264
  ? TFrom extends string
264
- ? ResolveRelativePath<TFrom, TTo> extends TRoutesInfo['routePaths']
265
+ ? ResolveRelativePath<TFrom, TTo> extends RoutePaths<TRouteTree>
265
266
  ? {}
266
267
  : {
267
268
  Error: `${TFrom} + ${TTo} resolves to ${ResolveRelativePath<
268
269
  TFrom,
269
270
  TTo
270
271
  >}, which is not a valid route path.`
271
- 'Valid Route Paths': TRoutesInfo['routePaths']
272
+ 'Valid Route Paths': RoutePaths<TRouteTree>
272
273
  }
273
274
  : {}
274
275
  : {}
275
276
 
276
- export type CheckPath<
277
- TRoutesInfo extends AnyRoutesInfo,
277
+ export type CheckPath<TRouteTree extends AnyRoute, TPath, TPass> = Exclude<
278
278
  TPath,
279
- TPass,
280
- > = Exclude<TPath, TRoutesInfo['routePaths']> extends never
279
+ RoutePaths<TRouteTree>
280
+ > extends never
281
281
  ? TPass
282
- : CheckPathError<TRoutesInfo, Exclude<TPath, TRoutesInfo['routePaths']>>
282
+ : CheckPathError<TRouteTree, Exclude<TPath, RoutePaths<TRouteTree>>>
283
283
 
284
- export type CheckPathError<TRoutesInfo extends AnyRoutesInfo, TInvalids> = {
285
- to: TRoutesInfo['routePaths']
284
+ export type CheckPathError<TRouteTree extends AnyRoute, TInvalids> = {
285
+ to: RoutePaths<TRouteTree>
286
286
  }
287
287
 
288
- export type CheckId<TRoutesInfo extends AnyRoutesInfo, TPath, TPass> = Exclude<
288
+ export type CheckId<TRouteTree extends AnyRoute, TPath, TPass> = Exclude<
289
289
  TPath,
290
- TRoutesInfo['routeIds']
290
+ RouteIds<TRouteTree>
291
291
  > extends never
292
292
  ? TPass
293
- : CheckIdError<TRoutesInfo, Exclude<TPath, TRoutesInfo['routeIds']>>
293
+ : CheckIdError<TRouteTree, Exclude<TPath, RouteIds<TRouteTree>>>
294
294
 
295
- export type CheckIdError<TRoutesInfo extends AnyRoutesInfo, TInvalids> = {
295
+ export type CheckIdError<TRouteTree extends AnyRoute, TInvalids> = {
296
296
  Error: `${TInvalids extends string
297
297
  ? TInvalids
298
298
  : never} is not a valid route ID.`
299
- 'Valid Route IDs': TRoutesInfo['routeIds']
299
+ 'Valid Route IDs': RouteIds<TRouteTree>
300
300
  }
301
301
 
302
302
  export type ResolveRelativePath<TFrom, TTo = '.'> = TFrom extends string