-
-
Notifications
You must be signed in to change notification settings - Fork 900
/
Copy pathroute.ts
1512 lines (1410 loc) · 35.6 KB
/
route.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import invariant from 'tiny-invariant'
import { joinPaths, rootRouteId, trimPathLeft } from '@tanstack/router-core'
import { useLoaderData } from './useLoaderData'
import { useLoaderDeps } from './useLoaderDeps'
import { useParams } from './useParams'
import { useSearch } from './useSearch'
import { notFound } from './not-found'
import { useNavigate } from './useNavigate'
import { useMatch } from './useMatch'
import type {
AnyContext,
AnyPathParams,
AnySchema,
AnyValidator,
Assign,
Constrain,
ConstrainLiteral,
ContextAsyncReturnType,
ContextReturnType,
DefaultValidator,
ErrorComponentProps,
Expand,
InferAllContext,
InferAllParams,
InferFullSearchSchema,
InferFullSearchSchemaInput,
IntersectAssign,
NoInfer,
NotFoundRouteProps,
ParamsOptions,
ParsedLocation,
ResolveId,
ResolveLoaderData,
ResolveParams,
ResolveRouteContext,
ResolveSearchValidatorInput,
ResolveValidatorOutput,
RootRouteId,
RouteContext,
RoutePathOptions,
RoutePathOptionsIntersection,
RoutePrefix,
SearchFilter,
SearchMiddleware,
TrimPathRight,
UpdatableStaticRouteOption,
} from '@tanstack/router-core'
import type { UseLoaderDataRoute } from './useLoaderData'
import type { UseMatchRoute } from './useMatch'
import type { UseLoaderDepsRoute } from './useLoaderDeps'
import type { UseParamsRoute } from './useParams'
import type { UseSearchRoute } from './useSearch'
import type * as React from 'react'
import type { UseNavigateResult } from './useNavigate'
import type {
AnyRouteMatch,
MakeRouteMatchFromRoute,
MakeRouteMatchUnion,
RouteMatch,
} from './Matches'
import type { NavigateOptions, ToMaskOptions } from './link'
import type { ParseRoute, RouteById, RouteIds, RoutePaths } from './routeInfo'
import type { AnyRouter, RegisteredRouter, Router } from './router'
import type { BuildLocationFn, NavigateFn } from './RouterProvider'
import type { NotFoundError } from './not-found'
import type { LazyRoute } from './fileRoute'
import type { UseRouteContextRoute } from './useRouteContext'
export type RouteOptions<
TParentRoute extends AnyRoute = AnyRoute,
TId extends string = string,
TCustomId extends string = string,
TFullPath extends string = string,
TPath extends string = string,
TSearchValidator = undefined,
TParams = AnyPathParams,
TLoaderDeps extends Record<string, any> = {},
TLoaderFn = undefined,
TRouterContext = {},
TRouteContextFn = AnyContext,
TBeforeLoadFn = AnyContext,
> = BaseRouteOptions<
TParentRoute,
TId,
TCustomId,
TPath,
TSearchValidator,
TParams,
TLoaderDeps,
TLoaderFn,
TRouterContext,
TRouteContextFn,
TBeforeLoadFn
> &
UpdatableRouteOptions<
NoInfer<TParentRoute>,
NoInfer<TCustomId>,
NoInfer<TFullPath>,
NoInfer<TParams>,
NoInfer<TSearchValidator>,
NoInfer<TLoaderFn>,
NoInfer<TLoaderDeps>,
NoInfer<TRouterContext>,
NoInfer<TRouteContextFn>,
NoInfer<TBeforeLoadFn>
>
export interface FullSearchSchemaOption<
in out TParentRoute extends AnyRoute,
in out TSearchValidator,
> {
search: Expand<ResolveFullSearchSchema<TParentRoute, TSearchValidator>>
}
export type RouteContextFn<
in out TParentRoute extends AnyRoute,
in out TSearchValidator,
in out TParams,
in out TRouterContext,
> = (
ctx: RouteContextOptions<
TParentRoute,
TSearchValidator,
TParams,
TRouterContext
>,
) => any
export type BeforeLoadFn<
in out TParentRoute extends AnyRoute,
in out TSearchValidator,
in out TParams,
in out TRouterContext,
in out TRouteContextFn,
> = (
ctx: BeforeLoadContextOptions<
TParentRoute,
TSearchValidator,
TParams,
TRouterContext,
TRouteContextFn
>,
) => any
export type FileBaseRouteOptions<
TParentRoute extends AnyRoute = AnyRoute,
TId extends string = string,
TPath extends string = string,
TSearchValidator = undefined,
TParams = {},
TLoaderDeps extends Record<string, any> = {},
TLoaderFn = undefined,
TRouterContext = {},
TRouteContextFn = AnyContext,
TBeforeLoadFn = AnyContext,
TRemountDepsFn = AnyContext,
TBeforeNavigateFn = undefined,
> = ParamsOptions<TPath, TParams> & {
validateSearch?: Constrain<TSearchValidator, AnyValidator, DefaultValidator>
beforeNavigate?: Constrain<
TBeforeNavigateFn,
(
opt: BeforeNavigateOptions<
Expand<ResolveFullSearchSchema<TParentRoute, TSearchValidator>>,
Expand<ResolveAllParamsFromParent<TParentRoute, TParams>>,
TRouterContext
>,
) => void | Promise<void>
>
shouldReload?:
| boolean
| ((
match: LoaderFnContext<
TParentRoute,
TId,
TParams,
TLoaderDeps,
TRouterContext,
TRouteContextFn,
TBeforeLoadFn
>,
) => any)
context?: Constrain<
TRouteContextFn,
(
ctx: RouteContextOptions<
TParentRoute,
TParams,
TRouterContext,
TLoaderDeps
>,
) => any
>
// This async function is called before a route is loaded.
// If an error is thrown here, the route's loader will not be called.
// If thrown during a navigation, the navigation will be cancelled and the error will be passed to the `onError` function.
// If thrown during a preload event, the error will be logged to the console.
beforeLoad?: Constrain<
TBeforeLoadFn,
(
ctx: BeforeLoadContextOptions<
TParentRoute,
TSearchValidator,
TParams,
TRouterContext,
TRouteContextFn
>,
) => any
>
loaderDeps?: (
opts: FullSearchSchemaOption<TParentRoute, TSearchValidator>,
) => TLoaderDeps
remountDeps?: Constrain<
TRemountDepsFn,
(
opt: RemountDepsOptions<
TId,
Expand<ResolveFullSearchSchema<TParentRoute, TSearchValidator>>,
Expand<ResolveAllParamsFromParent<TParentRoute, TParams>>,
TLoaderDeps
>,
) => any
>
loader?: Constrain<
TLoaderFn,
(
ctx: LoaderFnContext<
TParentRoute,
TId,
TParams,
TLoaderDeps,
TRouterContext,
TRouteContextFn,
TBeforeLoadFn
>,
) => any
>
}
export type BaseRouteOptions<
TParentRoute extends AnyRoute = AnyRoute,
TId extends string = string,
TCustomId extends string = string,
TPath extends string = string,
TSearchValidator = undefined,
TParams = {},
TLoaderDeps extends Record<string, any> = {},
TLoaderFn = undefined,
TRouterContext = {},
TRouteContextFn = AnyContext,
TBeforeLoadFn = AnyContext,
> = RoutePathOptions<TCustomId, TPath> &
FileBaseRouteOptions<
TParentRoute,
TId,
TPath,
TSearchValidator,
TParams,
TLoaderDeps,
TLoaderFn,
TRouterContext,
TRouteContextFn,
TBeforeLoadFn
> & {
getParentRoute: () => TParentRoute
}
export interface ContextOptions<
in out TParentRoute extends AnyRoute,
in out TParams,
> {
abortController: AbortController
preload: boolean
params: Expand<ResolveAllParamsFromParent<TParentRoute, TParams>>
location: ParsedLocation
/**
* @deprecated Use `throw redirect({ to: '/somewhere' })` instead
**/
navigate: NavigateFn
buildLocation: BuildLocationFn
cause: 'preload' | 'enter' | 'stay'
matches: Array<MakeRouteMatchUnion>
}
export interface RouteContextOptions<
in out TParentRoute extends AnyRoute,
in out TParams,
in out TRouterContext,
in out TLoaderDeps,
> extends ContextOptions<TParentRoute, TParams> {
deps: TLoaderDeps
context: Expand<RouteContextParameter<TParentRoute, TRouterContext>>
}
export interface BeforeNavigateOptions<
in out TFullSearchSchema,
in out TAllParams,
in out TRouterContext,
> {
search: TFullSearchSchema
params: TAllParams
context: TRouterContext
}
export interface RemountDepsOptions<
in out TRouteId,
in out TFullSearchSchema,
in out TAllParams,
in out TLoaderDeps,
> {
routeId: TRouteId
search: TFullSearchSchema
params: TAllParams
loaderDeps: TLoaderDeps
}
export type MakeRemountDepsOptionsUnion<
TRouteTree extends AnyRoute = RegisteredRouter['routeTree'],
TRoute extends AnyRoute = ParseRoute<TRouteTree>,
> = TRoute extends any
? RemountDepsOptions<
TRoute['id'],
TRoute['types']['fullSearchSchema'],
TRoute['types']['allParams'],
TRoute['types']['loaderDeps']
>
: never
export interface BeforeLoadContextOptions<
in out TParentRoute extends AnyRoute,
in out TSearchValidator,
in out TParams,
in out TRouterContext,
in out TRouteContextFn,
> extends ContextOptions<TParentRoute, TParams>,
FullSearchSchemaOption<TParentRoute, TSearchValidator> {
context: Expand<
BeforeLoadContextParameter<TParentRoute, TRouterContext, TRouteContextFn>
>
}
type AssetFnContextOptions<
in out TRouteId,
in out TFullPath,
in out TParentRoute extends AnyRoute,
in out TParams,
in out TSearchValidator,
in out TLoaderFn,
in out TRouterContext,
in out TRouteContextFn,
in out TBeforeLoadFn,
in out TLoaderDeps,
> = {
matches: Array<
RouteMatch<
TRouteId,
TFullPath,
ResolveAllParamsFromParent<TParentRoute, TParams>,
ResolveFullSearchSchema<TParentRoute, TSearchValidator>,
ResolveLoaderData<TLoaderFn>,
ResolveAllContext<
TParentRoute,
TRouterContext,
TRouteContextFn,
TBeforeLoadFn
>,
TLoaderDeps
>
>
match: RouteMatch<
TRouteId,
TFullPath,
ResolveAllParamsFromParent<TParentRoute, TParams>,
ResolveFullSearchSchema<TParentRoute, TSearchValidator>,
ResolveLoaderData<TLoaderFn>,
ResolveAllContext<
TParentRoute,
TRouterContext,
TRouteContextFn,
TBeforeLoadFn
>,
TLoaderDeps
>
params: ResolveAllParamsFromParent<TParentRoute, TParams>
loaderData: ResolveLoaderData<TLoaderFn>
}
export interface UpdatableRouteOptions<
in out TParentRoute extends AnyRoute,
in out TRouteId,
in out TFullPath,
in out TParams,
in out TSearchValidator,
in out TLoaderFn,
in out TLoaderDeps,
in out TRouterContext,
in out TRouteContextFn,
in out TBeforeLoadFn,
> extends UpdatableStaticRouteOption {
// If true, this route will be matched as case-sensitive
caseSensitive?: boolean
// If true, this route will be forcefully wrapped in a suspense boundary
wrapInSuspense?: boolean
// The content to be rendered when the route is matched. If no component is provided, defaults to `<Outlet />`
component?: RouteComponent
errorComponent?: false | null | ErrorRouteComponent
notFoundComponent?: NotFoundRouteComponent
pendingComponent?: RouteComponent
pendingMs?: number
pendingMinMs?: number
staleTime?: number
gcTime?: number
preload?: boolean
preloadStaleTime?: number
preloadGcTime?: number
search?: {
middlewares?: Array<
SearchMiddleware<
ResolveFullSearchSchemaInput<TParentRoute, TSearchValidator>
>
>
}
/**
@deprecated Use search.middlewares instead
*/
preSearchFilters?: Array<
SearchFilter<ResolveFullSearchSchema<TParentRoute, TSearchValidator>>
>
/**
@deprecated Use search.middlewares instead
*/
postSearchFilters?: Array<
SearchFilter<ResolveFullSearchSchema<TParentRoute, TSearchValidator>>
>
onCatch?: (error: Error, errorInfo: React.ErrorInfo) => void
onError?: (err: any) => void
// These functions are called as route matches are loaded, stick around and leave the active
// matches
onEnter?: (
match: RouteMatch<
TRouteId,
TFullPath,
ResolveAllParamsFromParent<TParentRoute, TParams>,
ResolveFullSearchSchema<TParentRoute, TSearchValidator>,
ResolveLoaderData<TLoaderFn>,
ResolveAllContext<
TParentRoute,
TRouterContext,
TRouteContextFn,
TBeforeLoadFn
>,
TLoaderDeps
>,
) => void
onStay?: (
match: RouteMatch<
TRouteId,
TFullPath,
ResolveAllParamsFromParent<TParentRoute, TParams>,
ResolveFullSearchSchema<TParentRoute, TSearchValidator>,
ResolveLoaderData<TLoaderFn>,
ResolveAllContext<
TParentRoute,
TRouterContext,
TRouteContextFn,
TBeforeLoadFn
>,
TLoaderDeps
>,
) => void
onLeave?: (
match: RouteMatch<
TRouteId,
TFullPath,
ResolveAllParamsFromParent<TParentRoute, TParams>,
ResolveFullSearchSchema<TParentRoute, TSearchValidator>,
ResolveLoaderData<TLoaderFn>,
ResolveAllContext<
TParentRoute,
TRouterContext,
TRouteContextFn,
TBeforeLoadFn
>,
TLoaderDeps
>,
) => void
headers?: (ctx: {
loaderData: ResolveLoaderData<TLoaderFn>
}) => Record<string, string>
head?: (
ctx: AssetFnContextOptions<
TRouteId,
TFullPath,
TParentRoute,
TParams,
TSearchValidator,
TLoaderFn,
TRouterContext,
TRouteContextFn,
TBeforeLoadFn,
TLoaderDeps
>,
) => {
links?: AnyRouteMatch['links']
scripts?: AnyRouteMatch['headScripts']
meta?: AnyRouteMatch['meta']
}
scripts?: (
ctx: AssetFnContextOptions<
TRouteId,
TFullPath,
TParentRoute,
TParams,
TSearchValidator,
TLoaderFn,
TRouterContext,
TRouteContextFn,
TBeforeLoadFn,
TLoaderDeps
>,
) => AnyRouteMatch['scripts']
ssr?: boolean
codeSplitGroupings?: Array<
Array<
| 'loader'
| 'component'
| 'pendingComponent'
| 'notFoundComponent'
| 'errorComponent'
>
>
}
export type RouteLoaderFn<
in out TParentRoute extends AnyRoute = AnyRoute,
in out TId extends string = string,
in out TParams = {},
in out TLoaderDeps = {},
in out TRouterContext = {},
in out TRouteContextFn = AnyContext,
in out TBeforeLoadFn = AnyContext,
> = (
match: LoaderFnContext<
TParentRoute,
TId,
TParams,
TLoaderDeps,
TRouterContext,
TRouteContextFn,
TBeforeLoadFn
>,
) => any
export interface LoaderFnContext<
in out TParentRoute extends AnyRoute = AnyRoute,
in out TId extends string = string,
in out TParams = {},
in out TLoaderDeps = {},
in out TRouterContext = {},
in out TRouteContextFn = AnyContext,
in out TBeforeLoadFn = AnyContext,
> {
abortController: AbortController
preload: boolean
params: Expand<ResolveAllParamsFromParent<TParentRoute, TParams>>
deps: TLoaderDeps
context: Expand<
ResolveAllContext<
TParentRoute,
TRouterContext,
TRouteContextFn,
TBeforeLoadFn
>
>
location: ParsedLocation // Do not supply search schema here so as to demotivate people from trying to shortcut loaderDeps
/**
* @deprecated Use `throw redirect({ to: '/somewhere' })` instead
**/
navigate: (opts: NavigateOptions<AnyRouter>) => Promise<void> | void
// root route does not have a parent match
parentMatchPromise: TId extends RootRouteId
? never
: Promise<MakeRouteMatchFromRoute<TParentRoute>>
cause: 'preload' | 'enter' | 'stay'
route: Route
}
export type ResolveFullSearchSchema<
TParentRoute extends AnyRoute,
TSearchValidator,
> = unknown extends TParentRoute
? ResolveValidatorOutput<TSearchValidator>
: IntersectAssign<
InferFullSearchSchema<TParentRoute>,
ResolveValidatorOutput<TSearchValidator>
>
export type ResolveFullSearchSchemaInput<
TParentRoute extends AnyRoute,
TSearchValidator,
> = IntersectAssign<
InferFullSearchSchemaInput<TParentRoute>,
ResolveSearchValidatorInput<TSearchValidator>
>
export type RouteContextParameter<
TParentRoute extends AnyRoute,
TRouterContext,
> = unknown extends TParentRoute
? TRouterContext
: Assign<TRouterContext, InferAllContext<TParentRoute>>
export type BeforeLoadContextParameter<
TParentRoute extends AnyRoute,
TRouterContext,
TRouteContextFn,
> = Assign<
RouteContextParameter<TParentRoute, TRouterContext>,
ContextReturnType<TRouteContextFn>
>
export type ResolveAllContext<
TParentRoute extends AnyRoute,
TRouterContext,
TRouteContextFn,
TBeforeLoadFn,
> = Assign<
BeforeLoadContextParameter<TParentRoute, TRouterContext, TRouteContextFn>,
ContextAsyncReturnType<TBeforeLoadFn>
>
export interface AnyRoute
extends Route<
any,
any,
any,
any,
any,
any,
any,
any,
any,
any,
any,
any,
any
> {}
export type AnyRouteWithContext<TContext> = AnyRoute & {
types: { allContext: TContext }
}
export type ResolveAllParamsFromParent<
TParentRoute extends AnyRoute,
TParams,
> = Assign<InferAllParams<TParentRoute>, TParams>
export type RouteConstraints = {
TParentRoute: AnyRoute
TPath: string
TFullPath: string
TCustomId: string
TId: string
TSearchSchema: AnySchema
TFullSearchSchema: AnySchema
TParams: Record<string, any>
TAllParams: Record<string, any>
TParentContext: AnyContext
TRouteContext: RouteContext
TAllContext: AnyContext
TRouterContext: AnyContext
TChildren: unknown
TRouteTree: AnyRoute
}
export type RouteTypesById<TRouter extends AnyRouter, TId> = RouteById<
TRouter['routeTree'],
TId
>['types']
export function getRouteApi<
const TId,
TRouter extends AnyRouter = RegisteredRouter,
>(id: ConstrainLiteral<TId, RouteIds<TRouter['routeTree']>>) {
return new RouteApi<TId, TRouter>({ id })
}
export class RouteApi<TId, TRouter extends AnyRouter = RegisteredRouter> {
id: TId
/**
* @deprecated Use the `getRouteApi` function instead.
*/
constructor({ id }: { id: TId }) {
this.id = id as any
}
useMatch: UseMatchRoute<TId> = (opts) => {
return useMatch({
select: opts?.select,
from: this.id,
structuralSharing: opts?.structuralSharing,
} as any) as any
}
useRouteContext: UseRouteContextRoute<TId> = (opts) => {
return useMatch({
from: this.id as any,
select: (d) => (opts?.select ? opts.select(d.context) : d.context),
}) as any
}
useSearch: UseSearchRoute<TId> = (opts) => {
// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
return useSearch({
select: opts?.select,
structuralSharing: opts?.structuralSharing,
from: this.id,
} as any) as any
}
useParams: UseParamsRoute<TId> = (opts) => {
// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
return useParams({
select: opts?.select,
structuralSharing: opts?.structuralSharing,
from: this.id,
} as any) as any
}
useLoaderDeps: UseLoaderDepsRoute<TId> = (opts) => {
return useLoaderDeps({ ...opts, from: this.id, strict: false } as any)
}
useLoaderData: UseLoaderDataRoute<TId> = (opts) => {
return useLoaderData({ ...opts, from: this.id, strict: false } as any)
}
useNavigate = (): UseNavigateResult<
RouteTypesById<TRouter, TId>['fullPath']
> => {
return useNavigate({ from: this.id as string })
}
notFound = (opts?: NotFoundError) => {
return notFound({ routeId: this.id as string, ...opts })
}
}
export class Route<
in out TParentRoute extends RouteConstraints['TParentRoute'] = AnyRoute,
in out TPath extends RouteConstraints['TPath'] = '/',
in out TFullPath extends RouteConstraints['TFullPath'] = ResolveFullPath<
TParentRoute,
TPath
>,
in out TCustomId extends RouteConstraints['TCustomId'] = string,
in out TId extends RouteConstraints['TId'] = ResolveId<
TParentRoute,
TCustomId,
TPath
>,
in out TSearchValidator = undefined,
in out TParams = ResolveParams<TPath>,
in out TRouterContext = AnyContext,
in out TRouteContextFn = AnyContext,
in out TBeforeLoadFn = AnyContext,
in out TLoaderDeps extends Record<string, any> = {},
in out TLoaderFn = undefined,
in out TChildren = unknown,
> {
isRoot: TParentRoute extends Route<any> ? true : false
options: RouteOptions<
TParentRoute,
TId,
TCustomId,
TFullPath,
TPath,
TSearchValidator,
TParams,
TLoaderDeps,
TLoaderFn,
TRouterContext,
TRouteContextFn,
TBeforeLoadFn
>
// The following properties are set up in this.init()
parentRoute!: TParentRoute
private _id!: TId
private _path!: TPath
private _fullPath!: TFullPath
private _to!: TrimPathRight<TFullPath>
private _ssr!: boolean
public get to() {
/* invariant(
this._to,
`trying to access property 'to' on a route which is not initialized yet. Route properties are only available after 'createRouter' completed.`,
)*/
return this._to
}
public get id() {
/* invariant(
this._id,
`trying to access property 'id' on a route which is not initialized yet. Route properties are only available after 'createRouter' completed.`,
)*/
return this._id
}
public get path() {
/* invariant(
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
this.isRoot || this._id || this._path,
`trying to access property 'path' on a route which is not initialized yet. Route properties are only available after 'createRouter' completed.`,
)*/
return this._path
}
public get fullPath() {
/* invariant(
this._fullPath,
`trying to access property 'fullPath' on a route which is not initialized yet. Route properties are only available after 'createRouter' completed.`,
)*/
return this._fullPath
}
public get ssr() {
return this._ssr
}
// Optional
children?: TChildren
originalIndex?: number
rank!: number
lazyFn?: () => Promise<LazyRoute<any>>
_lazyPromise?: Promise<void>
_componentsPromise?: Promise<Array<void>>
/**
* @deprecated Use the `createRoute` function instead.
*/
constructor(
options?: RouteOptions<
TParentRoute,
TId,
TCustomId,
TFullPath,
TPath,
TSearchValidator,
TParams,
TLoaderDeps,
TLoaderFn,
TRouterContext,
TRouteContextFn,
TBeforeLoadFn
>,
) {
this.options = (options as any) || {}
this.isRoot = !options?.getParentRoute as any
invariant(
!((options as any)?.id && (options as any)?.path),
`Route cannot have both an 'id' and a 'path' option.`,
)
;(this as any).$$typeof = Symbol.for('react.memo')
}
types!: {
parentRoute: TParentRoute
path: TPath
to: TrimPathRight<TFullPath>
fullPath: TFullPath
customId: TCustomId
id: TId
searchSchema: ResolveValidatorOutput<TSearchValidator>
searchSchemaInput: ResolveSearchValidatorInput<TSearchValidator>
searchValidator: TSearchValidator
fullSearchSchema: ResolveFullSearchSchema<TParentRoute, TSearchValidator>
fullSearchSchemaInput: ResolveFullSearchSchemaInput<
TParentRoute,
TSearchValidator
>
params: TParams
allParams: ResolveAllParamsFromParent<TParentRoute, TParams>
routerContext: TRouterContext
routeContext: ResolveRouteContext<TRouteContextFn, TBeforeLoadFn>
routeContextFn: TRouteContextFn
beforeLoadFn: TBeforeLoadFn
allContext: ResolveAllContext<
TParentRoute,
TRouterContext,
TRouteContextFn,
TBeforeLoadFn
>
children: TChildren
loaderData: ResolveLoaderData<TLoaderFn>
loaderDeps: TLoaderDeps
}
init = (opts: { originalIndex: number; defaultSsr?: boolean }): void => {
this.originalIndex = opts.originalIndex
const options = this.options as
| (RouteOptions<
TParentRoute,
TId,
TCustomId,
TFullPath,
TPath,
TSearchValidator,
TParams,
TLoaderDeps,
TLoaderFn,
TRouterContext,
TRouteContextFn,
TBeforeLoadFn
> &
RoutePathOptionsIntersection<TCustomId, TPath>)
| undefined
const isRoot = !options?.path && !options?.id
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
this.parentRoute = this.options.getParentRoute?.()
if (isRoot) {
this._path = rootRouteId as TPath
} else {
invariant(
this.parentRoute,
`Child Route instances must pass a 'getParentRoute: () => ParentRoute' option that returns a Route instance.`,
)
}
let path: undefined | string = isRoot ? rootRouteId : options.path
// If the path is anything other than an index path, trim it up
if (path && path !== '/') {
path = trimPathLeft(path)
}
const customId = options?.id || path
// Strip the parentId prefix from the first level of children
let id = isRoot
? rootRouteId
: joinPaths([
this.parentRoute.id === rootRouteId ? '' : this.parentRoute.id,
customId,
])
if (path === rootRouteId) {
path = '/'
}
if (id !== rootRouteId) {
id = joinPaths(['/', id])
}
const fullPath =
id === rootRouteId ? '/' : joinPaths([this.parentRoute.fullPath, path])
this._path = path as TPath
this._id = id as TId
// this.customId = customId as TCustomId
this._fullPath = fullPath as TFullPath
this._to = fullPath as TrimPathRight<TFullPath>
this._ssr = options?.ssr ?? opts.defaultSsr ?? true
}
addChildren<const TNewChildren>(
children: Constrain<
TNewChildren,
ReadonlyArray<AnyRoute> | Record<string, AnyRoute>
>,
): Route<
TParentRoute,
TPath,
TFullPath,
TCustomId,
TId,
TSearchValidator,
TParams,
TRouterContext,
TRouteContextFn,
TBeforeLoadFn,
TLoaderDeps,
TLoaderFn,
TNewChildren
> {
return this._addFileChildren(children) as Route<