@tanstack/router-core 1.111.6 → 1.112.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/route.ts CHANGED
@@ -1,13 +1,29 @@
1
- import type { ParsePathParams } from './link'
1
+ import type { NavigateOptions, ParsePathParams } from './link'
2
+ import type { ParsedLocation } from './location'
3
+ import type {
4
+ AnyRouteMatch,
5
+ MakeRouteMatchFromRoute,
6
+ MakeRouteMatchUnion,
7
+ RouteMatch,
8
+ } from './Matches'
2
9
  import type { RootRouteId } from './root'
3
10
  import type { ParseRoute } from './routeInfo'
4
- import type { RegisteredRouter } from './router'
5
- import type { Assign, Expand, IntersectAssign } from './utils'
11
+ import type { AnyRouter, RegisteredRouter } from './router'
12
+ import type { BuildLocationFn, NavigateFn } from './RouterProvider'
13
+ import type {
14
+ Assign,
15
+ Constrain,
16
+ Expand,
17
+ IntersectAssign,
18
+ NoInfer,
19
+ } from './utils'
6
20
  import type {
7
21
  AnySchema,
8
22
  AnyStandardSchemaValidator,
23
+ AnyValidator,
9
24
  AnyValidatorAdapter,
10
25
  AnyValidatorObj,
26
+ DefaultValidator,
11
27
  ResolveSearchValidatorInput,
12
28
  ResolveValidatorOutput,
13
29
  StandardSchemaValidator,
@@ -503,6 +519,517 @@ export type AnyRouteWithContext<TContext> = AnyRoute & {
503
519
  types: { allContext: TContext }
504
520
  }
505
521
 
522
+ export type RouteOptions<
523
+ TParentRoute extends AnyRoute = AnyRoute,
524
+ TId extends string = string,
525
+ TCustomId extends string = string,
526
+ TFullPath extends string = string,
527
+ TPath extends string = string,
528
+ TSearchValidator = undefined,
529
+ TParams = AnyPathParams,
530
+ TLoaderDeps extends Record<string, any> = {},
531
+ TLoaderFn = undefined,
532
+ TRouterContext = {},
533
+ TRouteContextFn = AnyContext,
534
+ TBeforeLoadFn = AnyContext,
535
+ > = BaseRouteOptions<
536
+ TParentRoute,
537
+ TId,
538
+ TCustomId,
539
+ TPath,
540
+ TSearchValidator,
541
+ TParams,
542
+ TLoaderDeps,
543
+ TLoaderFn,
544
+ TRouterContext,
545
+ TRouteContextFn,
546
+ TBeforeLoadFn
547
+ > &
548
+ UpdatableRouteOptions<
549
+ NoInfer<TParentRoute>,
550
+ NoInfer<TCustomId>,
551
+ NoInfer<TFullPath>,
552
+ NoInfer<TParams>,
553
+ NoInfer<TSearchValidator>,
554
+ NoInfer<TLoaderFn>,
555
+ NoInfer<TLoaderDeps>,
556
+ NoInfer<TRouterContext>,
557
+ NoInfer<TRouteContextFn>,
558
+ NoInfer<TBeforeLoadFn>
559
+ >
560
+
561
+ export type RouteContextFn<
562
+ in out TParentRoute extends AnyRoute,
563
+ in out TSearchValidator,
564
+ in out TParams,
565
+ in out TRouterContext,
566
+ > = (
567
+ ctx: RouteContextOptions<
568
+ TParentRoute,
569
+ TSearchValidator,
570
+ TParams,
571
+ TRouterContext
572
+ >,
573
+ ) => any
574
+
575
+ export type BeforeLoadFn<
576
+ in out TParentRoute extends AnyRoute,
577
+ in out TSearchValidator,
578
+ in out TParams,
579
+ in out TRouterContext,
580
+ in out TRouteContextFn,
581
+ > = (
582
+ ctx: BeforeLoadContextOptions<
583
+ TParentRoute,
584
+ TSearchValidator,
585
+ TParams,
586
+ TRouterContext,
587
+ TRouteContextFn
588
+ >,
589
+ ) => any
590
+
591
+ export type FileBaseRouteOptions<
592
+ TParentRoute extends AnyRoute = AnyRoute,
593
+ TId extends string = string,
594
+ TPath extends string = string,
595
+ TSearchValidator = undefined,
596
+ TParams = {},
597
+ TLoaderDeps extends Record<string, any> = {},
598
+ TLoaderFn = undefined,
599
+ TRouterContext = {},
600
+ TRouteContextFn = AnyContext,
601
+ TBeforeLoadFn = AnyContext,
602
+ TRemountDepsFn = AnyContext,
603
+ > = ParamsOptions<TPath, TParams> & {
604
+ validateSearch?: Constrain<TSearchValidator, AnyValidator, DefaultValidator>
605
+
606
+ shouldReload?:
607
+ | boolean
608
+ | ((
609
+ match: LoaderFnContext<
610
+ TParentRoute,
611
+ TId,
612
+ TParams,
613
+ TLoaderDeps,
614
+ TRouterContext,
615
+ TRouteContextFn,
616
+ TBeforeLoadFn
617
+ >,
618
+ ) => any)
619
+
620
+ context?: Constrain<
621
+ TRouteContextFn,
622
+ (
623
+ ctx: RouteContextOptions<
624
+ TParentRoute,
625
+ TParams,
626
+ TRouterContext,
627
+ TLoaderDeps
628
+ >,
629
+ ) => any
630
+ >
631
+
632
+ // This async function is called before a route is loaded.
633
+ // If an error is thrown here, the route's loader will not be called.
634
+ // If thrown during a navigation, the navigation will be cancelled and the error will be passed to the `onError` function.
635
+ // If thrown during a preload event, the error will be logged to the console.
636
+ beforeLoad?: Constrain<
637
+ TBeforeLoadFn,
638
+ (
639
+ ctx: BeforeLoadContextOptions<
640
+ TParentRoute,
641
+ TSearchValidator,
642
+ TParams,
643
+ TRouterContext,
644
+ TRouteContextFn
645
+ >,
646
+ ) => any
647
+ >
648
+
649
+ loaderDeps?: (
650
+ opts: FullSearchSchemaOption<TParentRoute, TSearchValidator>,
651
+ ) => TLoaderDeps
652
+
653
+ remountDeps?: Constrain<
654
+ TRemountDepsFn,
655
+ (
656
+ opt: RemountDepsOptions<
657
+ TId,
658
+ FullSearchSchemaOption<TParentRoute, TSearchValidator>,
659
+ Expand<ResolveAllParamsFromParent<TParentRoute, TParams>>,
660
+ TLoaderDeps
661
+ >,
662
+ ) => any
663
+ >
664
+
665
+ loader?: Constrain<
666
+ TLoaderFn,
667
+ (
668
+ ctx: LoaderFnContext<
669
+ TParentRoute,
670
+ TId,
671
+ TParams,
672
+ TLoaderDeps,
673
+ TRouterContext,
674
+ TRouteContextFn,
675
+ TBeforeLoadFn
676
+ >,
677
+ ) => any
678
+ >
679
+ }
680
+
681
+ export type BaseRouteOptions<
682
+ TParentRoute extends AnyRoute = AnyRoute,
683
+ TId extends string = string,
684
+ TCustomId extends string = string,
685
+ TPath extends string = string,
686
+ TSearchValidator = undefined,
687
+ TParams = {},
688
+ TLoaderDeps extends Record<string, any> = {},
689
+ TLoaderFn = undefined,
690
+ TRouterContext = {},
691
+ TRouteContextFn = AnyContext,
692
+ TBeforeLoadFn = AnyContext,
693
+ > = RoutePathOptions<TCustomId, TPath> &
694
+ FileBaseRouteOptions<
695
+ TParentRoute,
696
+ TId,
697
+ TPath,
698
+ TSearchValidator,
699
+ TParams,
700
+ TLoaderDeps,
701
+ TLoaderFn,
702
+ TRouterContext,
703
+ TRouteContextFn,
704
+ TBeforeLoadFn
705
+ > & {
706
+ getParentRoute: () => TParentRoute
707
+ }
708
+
709
+ export interface ContextOptions<
710
+ in out TParentRoute extends AnyRoute,
711
+ in out TParams,
712
+ > {
713
+ abortController: AbortController
714
+ preload: boolean
715
+ params: Expand<ResolveAllParamsFromParent<TParentRoute, TParams>>
716
+ location: ParsedLocation
717
+ /**
718
+ * @deprecated Use `throw redirect({ to: '/somewhere' })` instead
719
+ **/
720
+ navigate: NavigateFn
721
+ buildLocation: BuildLocationFn
722
+ cause: 'preload' | 'enter' | 'stay'
723
+ matches: Array<MakeRouteMatchUnion>
724
+ }
725
+
726
+ export interface RouteContextOptions<
727
+ in out TParentRoute extends AnyRoute,
728
+ in out TParams,
729
+ in out TRouterContext,
730
+ in out TLoaderDeps,
731
+ > extends ContextOptions<TParentRoute, TParams> {
732
+ deps: TLoaderDeps
733
+ context: Expand<RouteContextParameter<TParentRoute, TRouterContext>>
734
+ }
735
+
736
+ export interface BeforeLoadContextOptions<
737
+ in out TParentRoute extends AnyRoute,
738
+ in out TSearchValidator,
739
+ in out TParams,
740
+ in out TRouterContext,
741
+ in out TRouteContextFn,
742
+ > extends ContextOptions<TParentRoute, TParams>,
743
+ FullSearchSchemaOption<TParentRoute, TSearchValidator> {
744
+ context: Expand<
745
+ BeforeLoadContextParameter<TParentRoute, TRouterContext, TRouteContextFn>
746
+ >
747
+ }
748
+
749
+ type AssetFnContextOptions<
750
+ in out TRouteId,
751
+ in out TFullPath,
752
+ in out TParentRoute extends AnyRoute,
753
+ in out TParams,
754
+ in out TSearchValidator,
755
+ in out TLoaderFn,
756
+ in out TRouterContext,
757
+ in out TRouteContextFn,
758
+ in out TBeforeLoadFn,
759
+ in out TLoaderDeps,
760
+ > = {
761
+ matches: Array<
762
+ RouteMatch<
763
+ TRouteId,
764
+ TFullPath,
765
+ ResolveAllParamsFromParent<TParentRoute, TParams>,
766
+ ResolveFullSearchSchema<TParentRoute, TSearchValidator>,
767
+ ResolveLoaderData<TLoaderFn>,
768
+ ResolveAllContext<
769
+ TParentRoute,
770
+ TRouterContext,
771
+ TRouteContextFn,
772
+ TBeforeLoadFn
773
+ >,
774
+ TLoaderDeps
775
+ >
776
+ >
777
+ match: RouteMatch<
778
+ TRouteId,
779
+ TFullPath,
780
+ ResolveAllParamsFromParent<TParentRoute, TParams>,
781
+ ResolveFullSearchSchema<TParentRoute, TSearchValidator>,
782
+ ResolveLoaderData<TLoaderFn>,
783
+ ResolveAllContext<
784
+ TParentRoute,
785
+ TRouterContext,
786
+ TRouteContextFn,
787
+ TBeforeLoadFn
788
+ >,
789
+ TLoaderDeps
790
+ >
791
+ params: ResolveAllParamsFromParent<TParentRoute, TParams>
792
+ loaderData: ResolveLoaderData<TLoaderFn>
793
+ }
794
+
795
+ export interface DefaultUpdatableRouteOptionsExtensions {
796
+ component?: unknown
797
+ errorComponent?: unknown
798
+ notFoundComponent?: unknown
799
+ pendingComponent?: unknown
800
+ }
801
+
802
+ export interface UpdatableRouteOptionsExtensions {}
803
+
804
+ export interface UpdatableRouteOptions<
805
+ in out TParentRoute extends AnyRoute,
806
+ in out TRouteId,
807
+ in out TFullPath,
808
+ in out TParams,
809
+ in out TSearchValidator,
810
+ in out TLoaderFn,
811
+ in out TLoaderDeps,
812
+ in out TRouterContext,
813
+ in out TRouteContextFn,
814
+ in out TBeforeLoadFn,
815
+ > extends UpdatableStaticRouteOption,
816
+ UpdatableRouteOptionsExtensions {
817
+ // If true, this route will be matched as case-sensitive
818
+ caseSensitive?: boolean
819
+ // If true, this route will be forcefully wrapped in a suspense boundary
820
+ wrapInSuspense?: boolean
821
+ // The content to be rendered when the route is matched. If no component is provided, defaults to `<Outlet />`
822
+
823
+ pendingMs?: number
824
+ pendingMinMs?: number
825
+ staleTime?: number
826
+ gcTime?: number
827
+ preload?: boolean
828
+ preloadStaleTime?: number
829
+ preloadGcTime?: number
830
+ search?: {
831
+ middlewares?: Array<
832
+ SearchMiddleware<
833
+ ResolveFullSearchSchemaInput<TParentRoute, TSearchValidator>
834
+ >
835
+ >
836
+ }
837
+ /**
838
+ @deprecated Use search.middlewares instead
839
+ */
840
+ preSearchFilters?: Array<
841
+ SearchFilter<ResolveFullSearchSchema<TParentRoute, TSearchValidator>>
842
+ >
843
+ /**
844
+ @deprecated Use search.middlewares instead
845
+ */
846
+ postSearchFilters?: Array<
847
+ SearchFilter<ResolveFullSearchSchema<TParentRoute, TSearchValidator>>
848
+ >
849
+ onCatch?: (error: Error) => void
850
+ onError?: (err: any) => void
851
+ // These functions are called as route matches are loaded, stick around and leave the active
852
+ // matches
853
+ onEnter?: (
854
+ match: RouteMatch<
855
+ TRouteId,
856
+ TFullPath,
857
+ ResolveAllParamsFromParent<TParentRoute, TParams>,
858
+ ResolveFullSearchSchema<TParentRoute, TSearchValidator>,
859
+ ResolveLoaderData<TLoaderFn>,
860
+ ResolveAllContext<
861
+ TParentRoute,
862
+ TRouterContext,
863
+ TRouteContextFn,
864
+ TBeforeLoadFn
865
+ >,
866
+ TLoaderDeps
867
+ >,
868
+ ) => void
869
+ onStay?: (
870
+ match: RouteMatch<
871
+ TRouteId,
872
+ TFullPath,
873
+ ResolveAllParamsFromParent<TParentRoute, TParams>,
874
+ ResolveFullSearchSchema<TParentRoute, TSearchValidator>,
875
+ ResolveLoaderData<TLoaderFn>,
876
+ ResolveAllContext<
877
+ TParentRoute,
878
+ TRouterContext,
879
+ TRouteContextFn,
880
+ TBeforeLoadFn
881
+ >,
882
+ TLoaderDeps
883
+ >,
884
+ ) => void
885
+ onLeave?: (
886
+ match: RouteMatch<
887
+ TRouteId,
888
+ TFullPath,
889
+ ResolveAllParamsFromParent<TParentRoute, TParams>,
890
+ ResolveFullSearchSchema<TParentRoute, TSearchValidator>,
891
+ ResolveLoaderData<TLoaderFn>,
892
+ ResolveAllContext<
893
+ TParentRoute,
894
+ TRouterContext,
895
+ TRouteContextFn,
896
+ TBeforeLoadFn
897
+ >,
898
+ TLoaderDeps
899
+ >,
900
+ ) => void
901
+ headers?: (ctx: {
902
+ loaderData: ResolveLoaderData<TLoaderFn>
903
+ }) => Record<string, string>
904
+ head?: (
905
+ ctx: AssetFnContextOptions<
906
+ TRouteId,
907
+ TFullPath,
908
+ TParentRoute,
909
+ TParams,
910
+ TSearchValidator,
911
+ TLoaderFn,
912
+ TRouterContext,
913
+ TRouteContextFn,
914
+ TBeforeLoadFn,
915
+ TLoaderDeps
916
+ >,
917
+ ) => {
918
+ links?: AnyRouteMatch['links']
919
+ scripts?: AnyRouteMatch['headScripts']
920
+ meta?: AnyRouteMatch['meta']
921
+ }
922
+ scripts?: (
923
+ ctx: AssetFnContextOptions<
924
+ TRouteId,
925
+ TFullPath,
926
+ TParentRoute,
927
+ TParams,
928
+ TSearchValidator,
929
+ TLoaderFn,
930
+ TRouterContext,
931
+ TRouteContextFn,
932
+ TBeforeLoadFn,
933
+ TLoaderDeps
934
+ >,
935
+ ) => AnyRouteMatch['scripts']
936
+ ssr?: boolean
937
+ codeSplitGroupings?: Array<
938
+ Array<
939
+ | 'loader'
940
+ | 'component'
941
+ | 'pendingComponent'
942
+ | 'notFoundComponent'
943
+ | 'errorComponent'
944
+ >
945
+ >
946
+ }
947
+
948
+ export type RouteLoaderFn<
949
+ in out TParentRoute extends AnyRoute = AnyRoute,
950
+ in out TId extends string = string,
951
+ in out TParams = {},
952
+ in out TLoaderDeps = {},
953
+ in out TRouterContext = {},
954
+ in out TRouteContextFn = AnyContext,
955
+ in out TBeforeLoadFn = AnyContext,
956
+ > = (
957
+ match: LoaderFnContext<
958
+ TParentRoute,
959
+ TId,
960
+ TParams,
961
+ TLoaderDeps,
962
+ TRouterContext,
963
+ TRouteContextFn,
964
+ TBeforeLoadFn
965
+ >,
966
+ ) => any
967
+
968
+ export interface LoaderFnContext<
969
+ in out TParentRoute extends AnyRoute = AnyRoute,
970
+ in out TId extends string = string,
971
+ in out TParams = {},
972
+ in out TLoaderDeps = {},
973
+ in out TRouterContext = {},
974
+ in out TRouteContextFn = AnyContext,
975
+ in out TBeforeLoadFn = AnyContext,
976
+ > {
977
+ abortController: AbortController
978
+ preload: boolean
979
+ params: Expand<ResolveAllParamsFromParent<TParentRoute, TParams>>
980
+ deps: TLoaderDeps
981
+ context: Expand<
982
+ ResolveAllContext<
983
+ TParentRoute,
984
+ TRouterContext,
985
+ TRouteContextFn,
986
+ TBeforeLoadFn
987
+ >
988
+ >
989
+ location: ParsedLocation // Do not supply search schema here so as to demotivate people from trying to shortcut loaderDeps
990
+ /**
991
+ * @deprecated Use `throw redirect({ to: '/somewhere' })` instead
992
+ **/
993
+ navigate: (opts: NavigateOptions<AnyRouter>) => Promise<void> | void
994
+ // root route does not have a parent match
995
+ parentMatchPromise: TId extends RootRouteId
996
+ ? never
997
+ : Promise<MakeRouteMatchFromRoute<TParentRoute>>
998
+ cause: 'preload' | 'enter' | 'stay'
999
+ route: AnyRoute
1000
+ }
1001
+
1002
+ export type RootRouteOptions<
1003
+ TSearchValidator = undefined,
1004
+ TRouterContext = {},
1005
+ TRouteContextFn = AnyContext,
1006
+ TBeforeLoadFn = AnyContext,
1007
+ TLoaderDeps extends Record<string, any> = {},
1008
+ TLoaderFn = undefined,
1009
+ > = Omit<
1010
+ RouteOptions<
1011
+ any, // TParentRoute
1012
+ RootRouteId, // TId
1013
+ RootRouteId, // TCustomId
1014
+ '', // TFullPath
1015
+ '', // TPath
1016
+ TSearchValidator,
1017
+ {}, // TParams
1018
+ TLoaderDeps,
1019
+ TLoaderFn,
1020
+ TRouterContext,
1021
+ TRouteContextFn,
1022
+ TBeforeLoadFn
1023
+ >,
1024
+ | 'path'
1025
+ | 'id'
1026
+ | 'getParentRoute'
1027
+ | 'caseSensitive'
1028
+ | 'parseParams'
1029
+ | 'stringifyParams'
1030
+ | 'params'
1031
+ >
1032
+
506
1033
  /**
507
1034
  * @deprecated Use `ErrorComponentProps` instead.
508
1035
  */
package/src/serializer.ts CHANGED
@@ -17,13 +17,7 @@ export type SerializerParseBy<T, TSerializable> = T extends TSerializable
17
17
  ? ReadableStream
18
18
  : { [K in keyof T]: SerializerParseBy<T[K], TSerializable> }
19
19
 
20
- export type Serializable =
21
- | Date
22
- | undefined
23
- | Error
24
- | FormData
25
- | Response
26
- | bigint
20
+ export type Serializable = Date | undefined | Error | FormData | bigint
27
21
 
28
22
  export type SerializerStringify<T> = SerializerStringifyBy<T, Serializable>
29
23
 
File without changes