@tanstack/react-router 0.0.1-beta.23 → 0.0.1-beta.231

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