@tanstack/react-router 0.0.1-beta.28 → 0.0.1-beta.280

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.
Files changed (106) hide show
  1. package/LICENSE +21 -0
  2. package/build/cjs/CatchBoundary.js +128 -0
  3. package/build/cjs/CatchBoundary.js.map +1 -0
  4. package/build/cjs/Matches.js +233 -0
  5. package/build/cjs/Matches.js.map +1 -0
  6. package/build/cjs/RouterProvider.js +170 -0
  7. package/build/cjs/RouterProvider.js.map +1 -0
  8. package/build/cjs/_virtual/_rollupPluginBabelHelpers.js +2 -22
  9. package/build/cjs/_virtual/_rollupPluginBabelHelpers.js.map +1 -1
  10. package/build/cjs/awaited.js +43 -0
  11. package/build/cjs/awaited.js.map +1 -0
  12. package/build/cjs/defer.js +37 -0
  13. package/build/cjs/defer.js.map +1 -0
  14. package/build/cjs/fileRoute.js +27 -0
  15. package/build/cjs/fileRoute.js.map +1 -0
  16. package/build/cjs/index.js +130 -0
  17. package/build/cjs/index.js.map +1 -0
  18. package/build/cjs/lazyRouteComponent.js +54 -0
  19. package/build/cjs/lazyRouteComponent.js.map +1 -0
  20. package/build/cjs/link.js +223 -0
  21. package/build/cjs/link.js.map +1 -0
  22. package/build/cjs/path.js +214 -0
  23. package/build/cjs/path.js.map +1 -0
  24. package/build/cjs/qss.js +63 -0
  25. package/build/cjs/qss.js.map +1 -0
  26. package/build/cjs/redirects.js +28 -0
  27. package/build/cjs/redirects.js.map +1 -0
  28. package/build/cjs/route.js +191 -0
  29. package/build/cjs/route.js.map +1 -0
  30. package/build/cjs/router.js +1049 -0
  31. package/build/cjs/router.js.map +1 -0
  32. package/build/cjs/scroll-restoration.js +202 -0
  33. package/build/cjs/scroll-restoration.js.map +1 -0
  34. package/build/cjs/searchParams.js +81 -0
  35. package/build/cjs/searchParams.js.map +1 -0
  36. package/build/cjs/useBlocker.js +55 -0
  37. package/build/cjs/useBlocker.js.map +1 -0
  38. package/build/cjs/useNavigate.js +86 -0
  39. package/build/cjs/useNavigate.js.map +1 -0
  40. package/build/cjs/useParams.js +26 -0
  41. package/build/cjs/useParams.js.map +1 -0
  42. package/build/cjs/useSearch.js +25 -0
  43. package/build/cjs/useSearch.js.map +1 -0
  44. package/build/cjs/utils.js +239 -0
  45. package/build/cjs/utils.js.map +1 -0
  46. package/build/esm/index.js +2255 -2571
  47. package/build/esm/index.js.map +1 -1
  48. package/build/stats-html.html +3498 -2694
  49. package/build/stats-react.json +1204 -44
  50. package/build/types/CatchBoundary.d.ts +36 -0
  51. package/build/types/Matches.d.ts +62 -0
  52. package/build/types/RouterProvider.d.ts +35 -0
  53. package/build/types/awaited.d.ts +9 -0
  54. package/build/types/defer.d.ts +19 -0
  55. package/build/types/fileRoute.d.ts +38 -0
  56. package/build/types/history.d.ts +7 -0
  57. package/build/types/index.d.ts +27 -114
  58. package/build/types/lazyRouteComponent.d.ts +2 -0
  59. package/build/types/link.d.ts +91 -0
  60. package/build/types/location.d.ts +12 -0
  61. package/build/types/path.d.ts +17 -0
  62. package/build/types/qss.d.ts +2 -0
  63. package/build/types/redirects.d.ts +11 -0
  64. package/build/types/route.d.ts +282 -0
  65. package/build/types/routeInfo.d.ts +22 -0
  66. package/build/types/router.d.ts +188 -0
  67. package/build/types/scroll-restoration.d.ts +18 -0
  68. package/build/types/searchParams.d.ts +7 -0
  69. package/build/types/useBlocker.d.ts +9 -0
  70. package/build/types/useNavigate.d.ts +19 -0
  71. package/build/types/useParams.d.ts +7 -0
  72. package/build/types/useSearch.d.ts +7 -0
  73. package/build/types/utils.d.ts +66 -0
  74. package/build/umd/index.development.js +2858 -2548
  75. package/build/umd/index.development.js.map +1 -1
  76. package/build/umd/index.production.js +4 -4
  77. package/build/umd/index.production.js.map +1 -1
  78. package/package.json +11 -10
  79. package/src/CatchBoundary.tsx +101 -0
  80. package/src/Matches.tsx +421 -0
  81. package/src/RouterProvider.tsx +252 -0
  82. package/src/awaited.tsx +40 -0
  83. package/src/defer.ts +55 -0
  84. package/src/fileRoute.ts +152 -0
  85. package/src/history.ts +8 -0
  86. package/src/index.tsx +28 -761
  87. package/src/lazyRouteComponent.tsx +33 -0
  88. package/src/link.tsx +598 -0
  89. package/src/location.ts +13 -0
  90. package/src/path.ts +261 -0
  91. package/src/qss.ts +53 -0
  92. package/src/redirects.ts +39 -0
  93. package/src/route.ts +872 -0
  94. package/src/routeInfo.ts +70 -0
  95. package/src/router.ts +1630 -0
  96. package/src/scroll-restoration.tsx +230 -0
  97. package/src/searchParams.ts +79 -0
  98. package/src/useBlocker.tsx +27 -0
  99. package/src/useNavigate.tsx +111 -0
  100. package/src/useParams.tsx +25 -0
  101. package/src/useSearch.tsx +25 -0
  102. package/src/utils.ts +350 -0
  103. package/build/cjs/react-router/src/index.js +0 -508
  104. package/build/cjs/react-router/src/index.js.map +0 -1
  105. package/build/cjs/router-core/build/esm/index.js +0 -2530
  106. package/build/cjs/router-core/build/esm/index.js.map +0 -1
package/src/route.ts ADDED
@@ -0,0 +1,872 @@
1
+ import * as React from 'react'
2
+ import invariant from 'tiny-invariant'
3
+ import { useLoaderData, useLoaderDeps, useMatch } from './Matches'
4
+ import { AnyRouteMatch } from './Matches'
5
+ import { NavigateOptions, ParsePathParams, ToSubOptions } from './link'
6
+ import { ParsedLocation } from './location'
7
+ import { joinPaths, trimPath } from './path'
8
+ import { RouteById, RouteIds, RoutePaths } from './routeInfo'
9
+ import { AnyRouter, RegisteredRouter } from './router'
10
+ import { useParams } from './useParams'
11
+ import { useSearch } from './useSearch'
12
+ import {
13
+ Assign,
14
+ Expand,
15
+ IsAny,
16
+ NoInfer,
17
+ PickRequired,
18
+ UnionToIntersection,
19
+ } from './utils'
20
+ import { BuildLocationFn, NavigateFn } from './RouterProvider'
21
+
22
+ export const rootRouteId = '__root__' as const
23
+ export type RootRouteId = typeof rootRouteId
24
+ export type AnyPathParams = {}
25
+
26
+ export type AnySearchSchema = {}
27
+
28
+ export type AnyContext = {}
29
+
30
+ export interface RouteContext {}
31
+
32
+ export interface RouteMeta {}
33
+
34
+ export type PreloadableObj = { preload?: () => Promise<void> }
35
+
36
+ export type RoutePathOptions<TCustomId, TPath> =
37
+ | {
38
+ path: TPath
39
+ }
40
+ | {
41
+ id: TCustomId
42
+ }
43
+
44
+ export type RoutePathOptionsIntersection<TCustomId, TPath> =
45
+ UnionToIntersection<RoutePathOptions<TCustomId, TPath>>
46
+
47
+ export type MetaOptions = keyof PickRequired<RouteMeta> extends never
48
+ ? {
49
+ meta?: RouteMeta
50
+ }
51
+ : {
52
+ meta: RouteMeta
53
+ }
54
+
55
+ export type RouteOptions<
56
+ TParentRoute extends AnyRoute = AnyRoute,
57
+ TCustomId extends string = string,
58
+ TPath extends string = string,
59
+ TSearchSchema extends Record<string, any> = {},
60
+ TFullSearchSchema extends Record<string, any> = TSearchSchema,
61
+ TParams extends AnyPathParams = AnyPathParams,
62
+ TAllParams extends AnyPathParams = TParams,
63
+ TRouteContext extends RouteContext = RouteContext,
64
+ TAllContext extends Record<string, any> = AnyContext,
65
+ TLoaderDeps extends Record<string, any> = {},
66
+ TLoaderData extends any = unknown,
67
+ > = BaseRouteOptions<
68
+ TParentRoute,
69
+ TCustomId,
70
+ TPath,
71
+ TSearchSchema,
72
+ TFullSearchSchema,
73
+ TParams,
74
+ TAllParams,
75
+ TRouteContext,
76
+ TAllContext,
77
+ TLoaderDeps,
78
+ TLoaderData
79
+ > &
80
+ UpdatableRouteOptions<NoInfer<TFullSearchSchema>>
81
+
82
+ export type ParamsFallback<
83
+ TPath extends string,
84
+ TParams,
85
+ > = unknown extends TParams ? Record<ParsePathParams<TPath>, string> : TParams
86
+
87
+ export type BaseRouteOptions<
88
+ TParentRoute extends AnyRoute = AnyRoute,
89
+ TCustomId extends string = string,
90
+ TPath extends string = string,
91
+ TSearchSchema extends Record<string, any> = {},
92
+ TFullSearchSchema extends Record<string, any> = TSearchSchema,
93
+ TParams extends AnyPathParams = {},
94
+ TAllParams = ParamsFallback<TPath, TParams>,
95
+ TRouteContext extends RouteContext = RouteContext,
96
+ TAllContext extends Record<string, any> = AnyContext,
97
+ TLoaderDeps extends Record<string, any> = {},
98
+ TLoaderData extends any = unknown,
99
+ > = RoutePathOptions<TCustomId, TPath> & {
100
+ getParentRoute: () => TParentRoute
101
+ validateSearch?: SearchSchemaValidator<TSearchSchema>
102
+ } & (keyof PickRequired<RouteContext> extends never
103
+ ? // This async function is called before a route is loaded.
104
+ // If an error is thrown here, the route's loader will not be called.
105
+ // If thrown during a navigation, the navigation will be cancelled and the error will be passed to the `onError` function.
106
+ // If thrown during a preload event, the error will be logged to the console.
107
+ {
108
+ beforeLoad?: BeforeLoadFn<
109
+ TFullSearchSchema,
110
+ TParentRoute,
111
+ TAllParams,
112
+ TRouteContext
113
+ >
114
+ }
115
+ : {
116
+ beforeLoad: BeforeLoadFn<
117
+ TFullSearchSchema,
118
+ TParentRoute,
119
+ TAllParams,
120
+ TRouteContext
121
+ >
122
+ }) & {
123
+ loaderDeps?: (opts: { search: TFullSearchSchema }) => TLoaderDeps
124
+ loader?: RouteLoaderFn<
125
+ TAllParams,
126
+ NoInfer<TLoaderDeps>,
127
+ NoInfer<TAllContext>,
128
+ NoInfer<TRouteContext>,
129
+ TLoaderData
130
+ >
131
+ } & (
132
+ | {
133
+ // Both or none
134
+ parseParams?: (
135
+ rawParams: IsAny<TPath, any, Record<ParsePathParams<TPath>, string>>,
136
+ ) => TParams extends Record<ParsePathParams<TPath>, any>
137
+ ? TParams
138
+ : 'parseParams must return an object'
139
+ stringifyParams?: (
140
+ params: NoInfer<ParamsFallback<TPath, TParams>>,
141
+ ) => Record<ParsePathParams<TPath>, string>
142
+ }
143
+ | {
144
+ stringifyParams?: never
145
+ parseParams?: never
146
+ }
147
+ )
148
+
149
+ type BeforeLoadFn<
150
+ TFullSearchSchema extends Record<string, any>,
151
+ TParentRoute extends AnyRoute,
152
+ TAllParams,
153
+ TRouteContext,
154
+ > = (opts: {
155
+ search: TFullSearchSchema
156
+ abortController: AbortController
157
+ preload: boolean
158
+ params: TAllParams
159
+ context: TParentRoute['types']['allContext']
160
+ location: ParsedLocation
161
+ navigate: NavigateFn<AnyRoute>
162
+ buildLocation: BuildLocationFn<TParentRoute>
163
+ cause: 'preload' | 'enter' | 'stay'
164
+ }) => Promise<TRouteContext> | TRouteContext | void
165
+
166
+ export type UpdatableRouteOptions<
167
+ TFullSearchSchema extends Record<string, any>,
168
+ > = MetaOptions & {
169
+ // test?: (args: TAllContext) => void
170
+ // If true, this route will be matched as case-sensitive
171
+ caseSensitive?: boolean
172
+ // If true, this route will be forcefully wrapped in a suspense boundary
173
+ wrapInSuspense?: boolean
174
+ // The content to be rendered when the route is matched. If no component is provided, defaults to `<Outlet />`
175
+ component?: RouteComponent
176
+ errorComponent?: false | null | ErrorRouteComponent
177
+ pendingComponent?: RouteComponent
178
+ pendingMs?: number
179
+ pendingMinMs?: number
180
+ staleTime?: number
181
+ gcTime?: number
182
+ preloadStaleTime?: number
183
+ preloadGcTime?: number
184
+ // Filter functions that can manipulate search params *before* they are passed to links and navigate
185
+ // calls that match this route.
186
+ preSearchFilters?: SearchFilter<TFullSearchSchema>[]
187
+ // Filter functions that can manipulate search params *after* they are passed to links and navigate
188
+ // calls that match this route.
189
+ postSearchFilters?: SearchFilter<TFullSearchSchema>[]
190
+ onError?: (err: any) => void
191
+ // These functions are called as route matches are loaded, stick around and leave the active
192
+ // matches
193
+ onEnter?: (match: AnyRouteMatch) => void
194
+ onStay?: (match: AnyRouteMatch) => void
195
+ onLeave?: (match: AnyRouteMatch) => void
196
+ }
197
+
198
+ export type ParseParamsOption<TPath extends string, TParams> = ParseParamsFn<
199
+ TPath,
200
+ TParams
201
+ >
202
+
203
+ export type ParseParamsFn<TPath extends string, TParams> = (
204
+ rawParams: IsAny<TPath, any, Record<ParsePathParams<TPath>, string>>,
205
+ ) => TParams extends Record<ParsePathParams<TPath>, any>
206
+ ? TParams
207
+ : 'parseParams must return an object'
208
+
209
+ export type ParseParamsObj<TPath extends string, TParams> = {
210
+ parse?: ParseParamsFn<TPath, TParams>
211
+ }
212
+
213
+ // The parse type here allows a zod schema to be passed directly to the validator
214
+ export type SearchSchemaValidator<TReturn> =
215
+ | SearchSchemaValidatorObj<TReturn>
216
+ | SearchSchemaValidatorFn<TReturn>
217
+
218
+ export type SearchSchemaValidatorObj<TReturn> = {
219
+ parse?: SearchSchemaValidatorFn<TReturn>
220
+ }
221
+
222
+ export type SearchSchemaValidatorFn<TReturn> = (
223
+ searchObj: Record<string, unknown>,
224
+ ) => TReturn
225
+
226
+ export type DefinedPathParamWarning =
227
+ 'Path params cannot be redefined by child routes!'
228
+
229
+ export type ParentParams<TParentParams> = AnyPathParams extends TParentParams
230
+ ? {}
231
+ : {
232
+ [Key in keyof TParentParams]?: DefinedPathParamWarning
233
+ }
234
+
235
+ export type RouteLoaderFn<
236
+ TAllParams = {},
237
+ TLoaderDeps extends Record<string, any> = {},
238
+ TAllContext extends Record<string, any> = AnyContext,
239
+ TRouteContext extends Record<string, any> = AnyContext,
240
+ TLoaderData extends any = unknown,
241
+ > = (
242
+ match: LoaderFnContext<TAllParams, TLoaderDeps, TAllContext, TRouteContext>,
243
+ ) => Promise<TLoaderData> | TLoaderData
244
+
245
+ export interface LoaderFnContext<
246
+ TAllParams = {},
247
+ TLoaderDeps extends Record<string, any> = {},
248
+ TAllContext extends Record<string, any> = AnyContext,
249
+ TRouteContext extends Record<string, any> = AnyContext,
250
+ > {
251
+ abortController: AbortController
252
+ preload: boolean
253
+ params: TAllParams
254
+ deps: TLoaderDeps
255
+ context: Expand<Assign<TAllContext, TRouteContext>>
256
+ location: ParsedLocation // Do not supply search schema here so as to demotivate people from trying to shortcut loaderDeps
257
+ navigate: (opts: NavigateOptions<AnyRoute>) => Promise<void>
258
+ parentMatchPromise?: Promise<void>
259
+ cause: 'preload' | 'enter' | 'stay'
260
+ }
261
+
262
+ export type SearchFilter<T, U = T> = (prev: T) => U
263
+
264
+ export type ResolveId<
265
+ TParentRoute,
266
+ TCustomId extends string,
267
+ TPath extends string,
268
+ > = TParentRoute extends { id: infer TParentId extends string }
269
+ ? RoutePrefix<TParentId, string extends TCustomId ? TPath : TCustomId>
270
+ : RootRouteId
271
+
272
+ export type InferFullSearchSchema<TRoute> = TRoute extends {
273
+ types: {
274
+ fullSearchSchema: infer TFullSearchSchema
275
+ }
276
+ }
277
+ ? TFullSearchSchema
278
+ : {}
279
+
280
+ export type ResolveFullSearchSchema<TParentRoute, TSearchSchema> = Expand<
281
+ Assign<InferFullSearchSchema<TParentRoute>, TSearchSchema>
282
+ >
283
+
284
+ export interface AnyRoute
285
+ extends Route<
286
+ any,
287
+ any,
288
+ any,
289
+ any,
290
+ any,
291
+ any,
292
+ any,
293
+ any,
294
+ any,
295
+ any,
296
+ any,
297
+ any,
298
+ any,
299
+ any,
300
+ any,
301
+ any
302
+ > {}
303
+
304
+ export type MergeFromFromParent<T, U> = IsAny<T, U, T & U>
305
+
306
+ export type ResolveAllParams<
307
+ TParentRoute extends AnyRoute,
308
+ TParams extends AnyPathParams,
309
+ > = Record<never, string> extends TParentRoute['types']['allParams']
310
+ ? TParams
311
+ : Expand<
312
+ UnionToIntersection<TParentRoute['types']['allParams'] & TParams> & {}
313
+ >
314
+
315
+ export type RouteConstraints = {
316
+ TParentRoute: AnyRoute
317
+ TPath: string
318
+ TFullPath: string
319
+ TCustomId: string
320
+ TId: string
321
+ TSearchSchema: AnySearchSchema
322
+ TFullSearchSchema: AnySearchSchema
323
+ TParams: Record<string, any>
324
+ TAllParams: Record<string, any>
325
+ TParentContext: AnyContext
326
+ TRouteContext: RouteContext
327
+ TAllContext: AnyContext
328
+ TRouterContext: AnyContext
329
+ TChildren: unknown
330
+ TRouteTree: AnyRoute
331
+ }
332
+
333
+ export class RouteApi<
334
+ TId extends RouteIds<RegisteredRouter['routeTree']>,
335
+ TRoute extends AnyRoute = RouteById<RegisteredRouter['routeTree'], TId>,
336
+ TFullSearchSchema extends Record<
337
+ string,
338
+ any
339
+ > = TRoute['types']['fullSearchSchema'],
340
+ TAllParams extends AnyPathParams = TRoute['types']['allParams'],
341
+ TAllContext extends Record<string, any> = TRoute['types']['allContext'],
342
+ TLoaderDeps extends Record<string, any> = TRoute['types']['loaderDeps'],
343
+ TLoaderData extends any = TRoute['types']['loaderData'],
344
+ > {
345
+ id: TId
346
+
347
+ constructor({ id }: { id: TId }) {
348
+ this.id = id as any
349
+ }
350
+
351
+ useMatch = <TSelected = TAllContext>(opts?: {
352
+ select?: (s: TAllContext) => TSelected
353
+ }): TSelected => {
354
+ return useMatch({ ...opts, from: this.id }) as any
355
+ }
356
+
357
+ useRouteContext = <TSelected = TAllContext>(opts?: {
358
+ select?: (s: TAllContext) => TSelected
359
+ }): TSelected => {
360
+ return useMatch({
361
+ ...opts,
362
+ from: this.id,
363
+ select: (d: any) => (opts?.select ? opts.select(d.context) : d.context),
364
+ } as any)
365
+ }
366
+
367
+ useSearch = <TSelected = TFullSearchSchema>(opts?: {
368
+ select?: (s: TFullSearchSchema) => TSelected
369
+ }): TSelected => {
370
+ return useSearch({ ...opts, from: this.id } as any)
371
+ }
372
+
373
+ useParams = <TSelected = TAllParams>(opts?: {
374
+ select?: (s: TAllParams) => TSelected
375
+ }): TSelected => {
376
+ return useParams({ ...opts, from: this.id } as any)
377
+ }
378
+
379
+ useLoaderDeps = <TSelected = TLoaderDeps>(opts?: {
380
+ select?: (s: TLoaderDeps) => TSelected
381
+ }): TSelected => {
382
+ return useLoaderDeps({ ...opts, from: this.id } as any) as any
383
+ }
384
+
385
+ useLoaderData = <TSelected = TLoaderData>(opts?: {
386
+ select?: (s: TLoaderData) => TSelected
387
+ }): TSelected => {
388
+ return useLoaderData({ ...opts, from: this.id } as any) as any
389
+ }
390
+ }
391
+
392
+ export class Route<
393
+ TParentRoute extends RouteConstraints['TParentRoute'] = AnyRoute,
394
+ TPath extends RouteConstraints['TPath'] = '/',
395
+ TFullPath extends RouteConstraints['TFullPath'] = ResolveFullPath<
396
+ TParentRoute,
397
+ TPath
398
+ >,
399
+ TCustomId extends RouteConstraints['TCustomId'] = string,
400
+ TId extends RouteConstraints['TId'] = ResolveId<
401
+ TParentRoute,
402
+ TCustomId,
403
+ TPath
404
+ >,
405
+ TSearchSchema extends RouteConstraints['TSearchSchema'] = {},
406
+ TFullSearchSchema extends
407
+ RouteConstraints['TFullSearchSchema'] = ResolveFullSearchSchema<
408
+ TParentRoute,
409
+ TSearchSchema
410
+ >,
411
+ TParams extends RouteConstraints['TParams'] = Expand<
412
+ Record<ParsePathParams<TPath>, string>
413
+ >,
414
+ TAllParams extends RouteConstraints['TAllParams'] = ResolveAllParams<
415
+ TParentRoute,
416
+ TParams
417
+ >,
418
+ TRouteContext extends RouteConstraints['TRouteContext'] = RouteContext,
419
+ TAllContext extends Expand<
420
+ Assign<IsAny<TParentRoute['types']['allContext'], {}>, TRouteContext>
421
+ > = Expand<
422
+ Assign<IsAny<TParentRoute['types']['allContext'], {}>, TRouteContext>
423
+ >,
424
+ TRouterContext extends RouteConstraints['TRouterContext'] = AnyContext,
425
+ TLoaderDeps extends Record<string, any> = {},
426
+ TLoaderData extends any = unknown,
427
+ TChildren extends RouteConstraints['TChildren'] = unknown,
428
+ TRouteTree extends RouteConstraints['TRouteTree'] = AnyRoute,
429
+ > {
430
+ isRoot: TParentRoute extends Route<any> ? true : false
431
+ options: RouteOptions<
432
+ TParentRoute,
433
+ TCustomId,
434
+ TPath,
435
+ TSearchSchema,
436
+ TFullSearchSchema,
437
+ TParams,
438
+ TAllParams,
439
+ TRouteContext,
440
+ TAllContext,
441
+ TLoaderDeps,
442
+ TLoaderData
443
+ >
444
+
445
+ test!: Expand<
446
+ Assign<IsAny<TParentRoute['types']['allContext'], {}>, TRouteContext>
447
+ >
448
+
449
+ // Set up in this.init()
450
+ parentRoute!: TParentRoute
451
+ id!: TId
452
+ // customId!: TCustomId
453
+ path!: TPath
454
+ fullPath!: TFullPath
455
+ to!: TrimPathRight<TFullPath>
456
+
457
+ // Optional
458
+ children?: TChildren
459
+ originalIndex?: number
460
+ router?: AnyRouter
461
+ rank!: number
462
+
463
+ constructor(
464
+ options: RouteOptions<
465
+ TParentRoute,
466
+ TCustomId,
467
+ TPath,
468
+ TSearchSchema,
469
+ TFullSearchSchema,
470
+ TParams,
471
+ TAllParams,
472
+ TRouteContext,
473
+ TAllContext,
474
+ TLoaderDeps,
475
+ TLoaderData
476
+ >,
477
+ ) {
478
+ this.options = (options as any) || {}
479
+ this.isRoot = !options?.getParentRoute as any
480
+ invariant(
481
+ !((options as any)?.id && (options as any)?.path),
482
+ `Route cannot have both an 'id' and a 'path' option.`,
483
+ )
484
+ ;(this as any).$$typeof = Symbol.for('react.memo')
485
+ }
486
+
487
+ types!: {
488
+ parentRoute: TParentRoute
489
+ path: TPath
490
+ to: TrimPathRight<TFullPath>
491
+ fullPath: TFullPath
492
+ customId: TCustomId
493
+ id: TId
494
+ searchSchema: TSearchSchema
495
+ fullSearchSchema: TFullSearchSchema
496
+ params: TParams
497
+ allParams: TAllParams
498
+ routeContext: TRouteContext
499
+ allContext: TAllContext
500
+ children: TChildren
501
+ routeTree: TRouteTree
502
+ routerContext: TRouterContext
503
+ loaderData: TLoaderData
504
+ loaderDeps: TLoaderDeps
505
+ }
506
+
507
+ init = (opts: { originalIndex: number }) => {
508
+ this.originalIndex = opts.originalIndex
509
+
510
+ const options = this.options as RouteOptions<
511
+ TParentRoute,
512
+ TCustomId,
513
+ TPath,
514
+ TSearchSchema,
515
+ TFullSearchSchema,
516
+ TParams,
517
+ TAllParams,
518
+ TRouteContext,
519
+ TAllContext,
520
+ TLoaderDeps,
521
+ TLoaderData
522
+ > &
523
+ RoutePathOptionsIntersection<TCustomId, TPath>
524
+
525
+ const isRoot = !options?.path && !options?.id
526
+
527
+ this.parentRoute = this.options?.getParentRoute?.()
528
+
529
+ if (isRoot) {
530
+ this.path = rootRouteId as TPath
531
+ } else {
532
+ invariant(
533
+ this.parentRoute,
534
+ `Child Route instances must pass a 'getParentRoute: () => ParentRoute' option that returns a Route instance.`,
535
+ )
536
+ }
537
+
538
+ let path: undefined | string = isRoot ? rootRouteId : options.path
539
+
540
+ // If the path is anything other than an index path, trim it up
541
+ if (path && path !== '/') {
542
+ path = trimPath(path)
543
+ }
544
+
545
+ const customId = options?.id || path
546
+
547
+ // Strip the parentId prefix from the first level of children
548
+ let id = isRoot
549
+ ? rootRouteId
550
+ : joinPaths([
551
+ (this.parentRoute.id as any) === rootRouteId
552
+ ? ''
553
+ : this.parentRoute.id,
554
+ customId,
555
+ ])
556
+
557
+ if (path === rootRouteId) {
558
+ path = '/'
559
+ }
560
+
561
+ if (id !== rootRouteId) {
562
+ id = joinPaths(['/', id])
563
+ }
564
+
565
+ const fullPath =
566
+ id === rootRouteId ? '/' : joinPaths([this.parentRoute.fullPath, path])
567
+
568
+ this.path = path as TPath
569
+ this.id = id as TId
570
+ // this.customId = customId as TCustomId
571
+ this.fullPath = fullPath as TFullPath
572
+ this.to = fullPath as TrimPathRight<TFullPath>
573
+ }
574
+
575
+ addChildren = <TNewChildren extends AnyRoute[]>(
576
+ children: TNewChildren,
577
+ ): Route<
578
+ TParentRoute,
579
+ TPath,
580
+ TFullPath,
581
+ TCustomId,
582
+ TId,
583
+ TSearchSchema,
584
+ TFullSearchSchema,
585
+ TParams,
586
+ TAllParams,
587
+ TRouteContext,
588
+ TAllContext,
589
+ TRouterContext,
590
+ TLoaderDeps,
591
+ TLoaderData,
592
+ TNewChildren,
593
+ TRouteTree
594
+ > => {
595
+ this.children = children as any
596
+ return this as any
597
+ }
598
+
599
+ update = (options: UpdatableRouteOptions<TFullSearchSchema>) => {
600
+ Object.assign(this.options, options)
601
+ return this
602
+ }
603
+
604
+ useMatch = <TSelected = TAllContext>(opts?: {
605
+ select?: (search: TAllContext) => TSelected
606
+ }): TSelected => {
607
+ return useMatch({ ...opts, from: this.id }) as any
608
+ }
609
+
610
+ useRouteContext = <TSelected = TAllContext>(opts?: {
611
+ select?: (search: TAllContext) => TSelected
612
+ }): TSelected => {
613
+ return useMatch({
614
+ ...opts,
615
+ from: this.id,
616
+ select: (d: any) => (opts?.select ? opts.select(d.context) : d.context),
617
+ } as any)
618
+ }
619
+
620
+ useSearch = <TSelected = TFullSearchSchema>(opts?: {
621
+ select?: (search: TFullSearchSchema) => TSelected
622
+ }): TSelected => {
623
+ return useSearch({ ...opts, from: this.id } as any)
624
+ }
625
+
626
+ useParams = <TSelected = TAllParams>(opts?: {
627
+ select?: (search: TAllParams) => TSelected
628
+ }): TSelected => {
629
+ return useParams({ ...opts, from: this.id } as any)
630
+ }
631
+
632
+ useLoaderDeps = <TSelected = TLoaderDeps>(opts?: {
633
+ select?: (s: TLoaderDeps) => TSelected
634
+ }): TSelected => {
635
+ return useLoaderDeps({ ...opts, from: this.id } as any) as any
636
+ }
637
+
638
+ useLoaderData = <TSelected = TLoaderData>(opts?: {
639
+ select?: (search: TLoaderData) => TSelected
640
+ }): TSelected => {
641
+ return useLoaderData({ ...opts, from: this.id } as any) as any
642
+ }
643
+ }
644
+
645
+ export type AnyRootRoute = RootRoute<any, any, any, any>
646
+
647
+ export function rootRouteWithContext<TRouterContext extends {}>() {
648
+ return <
649
+ TSearchSchema extends Record<string, any> = {},
650
+ TRouteContext extends RouteContext = RouteContext,
651
+ TLoaderDeps extends Record<string, any> = {},
652
+ TLoaderData extends any = unknown,
653
+ >(
654
+ options?: Omit<
655
+ RouteOptions<
656
+ AnyRoute, // TParentRoute
657
+ RootRouteId, // TCustomId
658
+ '', // TPath
659
+ TSearchSchema, // TSearchSchema
660
+ TSearchSchema, // TFullSearchSchema
661
+ {}, // TParams
662
+ {}, // TAllParams
663
+ TRouteContext, // TRouteContext
664
+ Assign<TRouterContext, TRouteContext>, // TAllContext
665
+ TLoaderDeps,
666
+ TLoaderData // TLoaderData,
667
+ >,
668
+ | 'path'
669
+ | 'id'
670
+ | 'getParentRoute'
671
+ | 'caseSensitive'
672
+ | 'parseParams'
673
+ | 'stringifyParams'
674
+ >,
675
+ ): RootRoute<TSearchSchema, TRouteContext, TRouterContext> => {
676
+ return new RootRoute(options) as any
677
+ }
678
+ }
679
+
680
+ export class RootRoute<
681
+ TSearchSchema extends Record<string, any> = {},
682
+ TRouteContext extends RouteContext = RouteContext,
683
+ TRouterContext extends {} = {},
684
+ TLoaderDeps extends Record<string, any> = {},
685
+ TLoaderData extends any = unknown,
686
+ > extends Route<
687
+ any, // TParentRoute
688
+ '/', // TPath
689
+ '/', // TFullPath
690
+ string, // TCustomId
691
+ RootRouteId, // TId
692
+ TSearchSchema, // TSearchSchema
693
+ TSearchSchema, // TFullSearchSchema
694
+ {}, // TParams
695
+ {}, // TAllParams
696
+ TRouteContext, // TRouteContext
697
+ Expand<Assign<TRouterContext, TRouteContext>>, // TAllContext
698
+ TRouterContext, // TRouterContext
699
+ TLoaderDeps,
700
+ TLoaderData,
701
+ any, // TChildren
702
+ any // TRouteTree
703
+ > {
704
+ constructor(
705
+ options?: Omit<
706
+ RouteOptions<
707
+ AnyRoute, // TParentRoute
708
+ RootRouteId, // TCustomId
709
+ '', // TPath
710
+ TSearchSchema, // TSearchSchema
711
+ TSearchSchema, // TFullSearchSchema
712
+ {}, // TParams
713
+ {}, // TAllParams
714
+ TRouteContext, // TRouteContext
715
+ Assign<TRouterContext, TRouteContext>, // TAllContext
716
+ TLoaderDeps,
717
+ TLoaderData
718
+ >,
719
+ | 'path'
720
+ | 'id'
721
+ | 'getParentRoute'
722
+ | 'caseSensitive'
723
+ | 'parseParams'
724
+ | 'stringifyParams'
725
+ >,
726
+ ) {
727
+ super(options as any)
728
+ }
729
+ }
730
+
731
+ export type ResolveFullPath<
732
+ TParentRoute extends AnyRoute,
733
+ TPath extends string,
734
+ TPrefixed = RoutePrefix<TParentRoute['fullPath'], TPath>,
735
+ > = TPrefixed extends RootRouteId ? '/' : TPrefixed
736
+
737
+ type RoutePrefix<
738
+ TPrefix extends string,
739
+ TPath extends string,
740
+ > = string extends TPath
741
+ ? RootRouteId
742
+ : TPath extends string
743
+ ? TPrefix extends RootRouteId
744
+ ? TPath extends '/'
745
+ ? '/'
746
+ : `/${TrimPath<TPath>}`
747
+ : `${TPrefix}/${TPath}` extends '/'
748
+ ? '/'
749
+ : `/${TrimPathLeft<`${TrimPathRight<TPrefix>}/${TrimPath<TPath>}`>}`
750
+ : never
751
+
752
+ export type TrimPath<T extends string> = '' extends T
753
+ ? ''
754
+ : TrimPathRight<TrimPathLeft<T>>
755
+
756
+ export type TrimPathLeft<T extends string> =
757
+ T extends `${RootRouteId}/${infer U}`
758
+ ? TrimPathLeft<U>
759
+ : T extends `/${infer U}`
760
+ ? TrimPathLeft<U>
761
+ : T
762
+ export type TrimPathRight<T extends string> = T extends '/'
763
+ ? '/'
764
+ : T extends `${infer U}/`
765
+ ? TrimPathRight<U>
766
+ : T
767
+
768
+ export type RouteMask<TRouteTree extends AnyRoute> = {
769
+ routeTree: TRouteTree
770
+ from: RoutePaths<TRouteTree>
771
+ to?: any
772
+ params?: any
773
+ search?: any
774
+ hash?: any
775
+ state?: any
776
+ unmaskOnReload?: boolean
777
+ }
778
+
779
+ export function createRouteMask<
780
+ TRouteTree extends AnyRoute,
781
+ TFrom extends RoutePaths<TRouteTree>,
782
+ TTo extends string,
783
+ >(
784
+ opts: {
785
+ routeTree: TRouteTree
786
+ } & ToSubOptions<TRouteTree, TFrom, TTo>,
787
+ ): RouteMask<TRouteTree> {
788
+ return opts as any
789
+ }
790
+
791
+ export type ErrorRouteProps = {
792
+ error: unknown
793
+ info: { componentStack: string }
794
+ }
795
+ //
796
+
797
+ export type ReactNode = any
798
+
799
+ export type SyncRouteComponent<TProps> =
800
+ | ((props: TProps) => ReactNode)
801
+ | React.LazyExoticComponent<(props: TProps) => ReactNode>
802
+
803
+ export type AsyncRouteComponent<TProps> = SyncRouteComponent<TProps> & {
804
+ preload?: () => Promise<void>
805
+ }
806
+
807
+ export type RouteComponent<TProps = any> = SyncRouteComponent<TProps> &
808
+ AsyncRouteComponent<TProps>
809
+
810
+ export type ErrorRouteComponent = RouteComponent<ErrorRouteProps>
811
+
812
+ export class NotFoundRoute<
813
+ TParentRoute extends AnyRootRoute,
814
+ TSearchSchema extends RouteConstraints['TSearchSchema'] = {},
815
+ TFullSearchSchema extends
816
+ RouteConstraints['TFullSearchSchema'] = ResolveFullSearchSchema<
817
+ TParentRoute,
818
+ TSearchSchema
819
+ >,
820
+ TRouteContext extends RouteConstraints['TRouteContext'] = RouteContext,
821
+ TAllContext extends Expand<
822
+ Assign<IsAny<TParentRoute['types']['allContext'], {}>, TRouteContext>
823
+ > = Expand<
824
+ Assign<IsAny<TParentRoute['types']['allContext'], {}>, TRouteContext>
825
+ >,
826
+ TRouterContext extends RouteConstraints['TRouterContext'] = AnyContext,
827
+ TLoaderDeps extends Record<string, any> = {},
828
+ TLoaderData extends any = unknown,
829
+ TChildren extends RouteConstraints['TChildren'] = unknown,
830
+ TRouteTree extends RouteConstraints['TRouteTree'] = AnyRoute,
831
+ > extends Route<
832
+ TParentRoute,
833
+ '/404',
834
+ '/404',
835
+ '404',
836
+ '404',
837
+ TSearchSchema,
838
+ TFullSearchSchema,
839
+ {},
840
+ {},
841
+ TRouteContext,
842
+ TAllContext,
843
+ TRouterContext,
844
+ TLoaderDeps,
845
+ TLoaderData,
846
+ TChildren,
847
+ TRouteTree
848
+ > {
849
+ constructor(
850
+ options: Omit<
851
+ RouteOptions<
852
+ TParentRoute,
853
+ string,
854
+ string,
855
+ TSearchSchema,
856
+ TFullSearchSchema,
857
+ {},
858
+ {},
859
+ TRouteContext,
860
+ TAllContext,
861
+ TLoaderDeps,
862
+ TLoaderData
863
+ >,
864
+ 'caseSensitive' | 'parseParams' | 'stringifyParams' | 'path' | 'id'
865
+ >,
866
+ ) {
867
+ super({
868
+ ...(options as any),
869
+ id: '404',
870
+ })
871
+ }
872
+ }