@analogjs/router 3.0.0-alpha.90 → 3.0.0-alpha.91
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/fesm2022/analogjs-router.mjs +292 -355
- package/fesm2022/analogjs-router.mjs.map +1 -1
- package/package.json +4 -4
- package/types/src/index.d.ts +4 -6
- package/types/src/lib/inject-load.d.ts +6 -1
- package/types/src/lib/inject-navigate.d.ts +38 -5
- package/types/src/lib/inject-typed-params.d.ts +28 -58
- package/types/src/lib/link-to.directive.d.ts +38 -0
- package/types/src/lib/to-route.d.ts +246 -0
- package/types/src/lib/experimental.d.ts +0 -140
- package/types/src/lib/inject-route-context.d.ts +0 -32
- package/types/src/lib/route-path.d.ts +0 -124
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
import { API_PREFIX, injectAPIPrefix, injectBaseURL, injectInternalServerFetch, injectRequest } from "./analogjs-router-tokens.mjs";
|
|
2
2
|
import { a as updateMetaTagsOnRouteChange, i as injectRouteEndpointURL, n as ANALOG_ROUTE_FILES, o as updateJsonLdOnRouteChange, r as createRoutes$1, t as ANALOG_EXTRA_ROUTE_FILE_SOURCES } from "./route-files.mjs";
|
|
3
3
|
import { n as createRoutes, r as routes, t as injectDebugRoutes } from "./routes.mjs";
|
|
4
|
-
import
|
|
4
|
+
import * as i1 from "@angular/router";
|
|
5
|
+
import { ActivatedRoute, ROUTES, Router, RouterLink, provideRouter } from "@angular/router";
|
|
5
6
|
import * as i0 from "@angular/core";
|
|
6
|
-
import { DOCUMENT, Directive, Injectable, InjectionToken, Injector, PLATFORM_ID, TransferState, assertInInjectionContext, inject, input,
|
|
7
|
+
import { DOCUMENT, Directive, Injectable, InjectionToken, Injector, PLATFORM_ID, TransferState, assertInInjectionContext, inject, input, makeEnvironmentProviders, makeStateKey, output, provideAppInitializer, resource, signal } from "@angular/core";
|
|
7
8
|
import { HttpClient, HttpHeaders, HttpResponse, ɵHTTP_ROOT_INTERCEPTOR_FNS } from "@angular/common/http";
|
|
8
|
-
import { firstValueFrom, from, map, of
|
|
9
|
+
import { combineLatest, firstValueFrom, from, map, of } from "rxjs";
|
|
9
10
|
import { Meta } from "@angular/platform-browser";
|
|
10
11
|
import { isPlatformServer } from "@angular/common";
|
|
11
12
|
import { toSignal } from "@angular/core/rxjs-interop";
|
|
@@ -138,12 +139,12 @@ function provideFileRouter(...features) {
|
|
|
138
139
|
}
|
|
139
140
|
//#endregion
|
|
140
141
|
//#region packages/router/src/lib/inject-load.ts
|
|
142
|
+
function injectLoad(pathOrOptions, options) {
|
|
143
|
+
return ((typeof pathOrOptions === "string" ? options : pathOrOptions)?.injector ?? inject(Injector)).get(ActivatedRoute).data.pipe(map((data) => data["load"]));
|
|
144
|
+
}
|
|
141
145
|
function isResponse(value) {
|
|
142
146
|
return typeof value === "object" && value instanceof Response;
|
|
143
147
|
}
|
|
144
|
-
function injectLoad(options) {
|
|
145
|
-
return (options?.injector ?? inject(Injector)).get(ActivatedRoute).data.pipe(map((data) => data["load"]));
|
|
146
|
-
}
|
|
147
148
|
function injectLoadData(options) {
|
|
148
149
|
return injectLoad(options).pipe(map((result) => {
|
|
149
150
|
if (isResponse(result)) throw new Error("Expected page load data but received a response.");
|
|
@@ -759,17 +760,7 @@ function createServerFnRef(config) {
|
|
|
759
760
|
});
|
|
760
761
|
}
|
|
761
762
|
//#endregion
|
|
762
|
-
//#region packages/router/src/lib/route
|
|
763
|
-
/**
|
|
764
|
-
* Typed route path utilities for Analog.
|
|
765
|
-
*
|
|
766
|
-
* This module provides:
|
|
767
|
-
* - The `AnalogRouteTable` base interface (augmented by generated code)
|
|
768
|
-
* - The `AnalogRoutePath` union type
|
|
769
|
-
* - The `routePath()` URL builder function
|
|
770
|
-
*
|
|
771
|
-
* No Angular dependencies — can be used in any context.
|
|
772
|
-
*/
|
|
763
|
+
//#region packages/router/src/lib/to-route.ts
|
|
773
764
|
/**
|
|
774
765
|
* Builds a typed route link object from a route path pattern and options.
|
|
775
766
|
*
|
|
@@ -777,22 +768,26 @@ function createServerFnRef(config) {
|
|
|
777
768
|
* direct use with Angular's routerLink directive inputs.
|
|
778
769
|
*
|
|
779
770
|
* @example
|
|
780
|
-
*
|
|
781
|
-
* // → { path: '/about', queryParams: null, fragment: undefined }
|
|
771
|
+
* toRoute('/about')
|
|
772
|
+
* // → { path: ['/', 'about'], queryParams: null, fragment: undefined }
|
|
782
773
|
*
|
|
783
|
-
*
|
|
784
|
-
* // → { path: '/users
|
|
774
|
+
* toRoute('/users/[id]', { params: { id: '42' } })
|
|
775
|
+
* // → { path: ['/', 'users', '42'], queryParams: null, fragment: undefined }
|
|
785
776
|
*
|
|
786
|
-
*
|
|
787
|
-
* // → { path: '/users
|
|
777
|
+
* toRoute('/users/[id]', { params: { id: '42' }, query: { tab: 'settings' }, hash: 'bio' })
|
|
778
|
+
* // → { path: ['/', 'users', '42'], queryParams: { tab: 'settings' }, fragment: 'bio' }
|
|
788
779
|
*
|
|
789
780
|
* @example Template usage
|
|
781
|
+
* Compute the link in the component:
|
|
782
|
+
* ```ts
|
|
783
|
+
* readonly link = toRoute('/users/[id]', { params: { id: '42' } });
|
|
784
|
+
* ```
|
|
785
|
+
*
|
|
790
786
|
* ```html
|
|
791
|
-
* @let link = routePath('/users/[id]', { params: { id: userId } });
|
|
792
787
|
* <a [routerLink]="link.path" [queryParams]="link.queryParams" [fragment]="link.fragment">
|
|
793
788
|
* ```
|
|
794
789
|
*/
|
|
795
|
-
function
|
|
790
|
+
function toRoute(path, ...args) {
|
|
796
791
|
const options = args[0];
|
|
797
792
|
return buildRouteLink(path, options);
|
|
798
793
|
}
|
|
@@ -801,100 +796,280 @@ function routePath(path, ...args) {
|
|
|
801
796
|
* Exported for direct use in tests (avoids generic constraints).
|
|
802
797
|
*/
|
|
803
798
|
function buildRouteLink(path, options) {
|
|
804
|
-
const resolvedPath = buildPath(path, options?.params);
|
|
805
|
-
let queryParams = null;
|
|
806
|
-
if (options?.query) {
|
|
807
|
-
const filtered = {};
|
|
808
|
-
let hasEntries = false;
|
|
809
|
-
for (const [key, value] of Object.entries(options.query)) if (value !== void 0) {
|
|
810
|
-
filtered[key] = value;
|
|
811
|
-
hasEntries = true;
|
|
812
|
-
}
|
|
813
|
-
if (hasEntries) queryParams = filtered;
|
|
814
|
-
}
|
|
815
799
|
return {
|
|
816
|
-
path:
|
|
817
|
-
queryParams,
|
|
800
|
+
path: buildPath(path, options?.params),
|
|
801
|
+
queryParams: options?.query ?? null,
|
|
818
802
|
fragment: options?.hash
|
|
819
803
|
};
|
|
820
804
|
}
|
|
805
|
+
function buildPath(path, params = {}) {
|
|
806
|
+
return ["/", ...path.split("/").filter(Boolean).flatMap((segment) => {
|
|
807
|
+
const optional = segment.match(/^\[\[\.\.\.([^\]]+)\]\]$/);
|
|
808
|
+
const catchAll = segment.match(/^\[\.\.\.([^\]]+)\]$/);
|
|
809
|
+
const dynamic = segment.match(/^\[([^\]]+)\]$/);
|
|
810
|
+
const match = optional ?? catchAll ?? dynamic;
|
|
811
|
+
if (!match) return [segment];
|
|
812
|
+
const value = params[match[1]];
|
|
813
|
+
if (value == null || Array.isArray(value) && !value.length) {
|
|
814
|
+
if (optional) return [];
|
|
815
|
+
throw new Error(`Missing required ${catchAll ? "catch-all " : ""}param "${match[1]}" for path "${path}"`);
|
|
816
|
+
}
|
|
817
|
+
return Array.isArray(value) ? value.map(String) : [String(value)];
|
|
818
|
+
})];
|
|
819
|
+
}
|
|
820
|
+
/** Internal: resolves `.` and `..` segments in `to` against the `from` route path. */
|
|
821
|
+
function resolveRoutePath(from, to) {
|
|
822
|
+
if (to.startsWith("/")) return to;
|
|
823
|
+
const segments = from.split("/").filter(Boolean);
|
|
824
|
+
for (const part of to.split("/")) if (part === "..") segments.pop();
|
|
825
|
+
else if (part && part !== ".") segments.push(part);
|
|
826
|
+
return "/" + segments.join("/");
|
|
827
|
+
}
|
|
821
828
|
/**
|
|
822
|
-
*
|
|
823
|
-
*
|
|
829
|
+
* Internal: resolves `target` against the `from` route path. Params in the
|
|
830
|
+
* leading segments shared with `from` default to the `current` values.
|
|
824
831
|
*/
|
|
825
|
-
function
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
832
|
+
function resolveScopedRoute(from, target, current, options) {
|
|
833
|
+
const path = resolveRoutePath(from, target);
|
|
834
|
+
const fromSegments = from.split("/").filter(Boolean);
|
|
835
|
+
const toSegments = path.split("/").filter(Boolean);
|
|
836
|
+
const params = {};
|
|
837
|
+
for (let i = 0; i < toSegments.length && toSegments[i] === fromSegments[i]; i++) {
|
|
838
|
+
const name = paramName(toSegments[i]);
|
|
839
|
+
if (name) params[name] = current[name];
|
|
840
|
+
}
|
|
841
|
+
return {
|
|
842
|
+
path,
|
|
843
|
+
options: {
|
|
844
|
+
...options,
|
|
845
|
+
params: {
|
|
846
|
+
...params,
|
|
847
|
+
...options?.params
|
|
840
848
|
}
|
|
841
|
-
|
|
849
|
+
}
|
|
850
|
+
};
|
|
851
|
+
}
|
|
852
|
+
function paramName(segment) {
|
|
853
|
+
return (segment.match(/^\[\[\.\.\.([^\]]+)\]\]$/) ?? segment.match(/^\[\.\.\.([^\]]+)\]$/) ?? segment.match(/^\[([^\]]+)\]$/))?.[1];
|
|
854
|
+
}
|
|
855
|
+
//#endregion
|
|
856
|
+
//#region packages/router/src/lib/inject-typed-params.ts
|
|
857
|
+
function extractCatchAllParams(routePath) {
|
|
858
|
+
const params = [];
|
|
859
|
+
for (const segment of routePath.split("/")) {
|
|
860
|
+
const optional = segment.match(/^\[\[\.\.\.([^\]]+)\]\]$/);
|
|
861
|
+
const required = segment.match(/^\[\.\.\.([^\]]+)\]$/);
|
|
862
|
+
if (optional) params.push({
|
|
863
|
+
name: optional[1],
|
|
864
|
+
type: "optionalCatchAll"
|
|
842
865
|
});
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
return encodeURIComponent(String(value));
|
|
866
|
+
else if (required) params.push({
|
|
867
|
+
name: required[1],
|
|
868
|
+
type: "catchAll"
|
|
847
869
|
});
|
|
848
|
-
} else {
|
|
849
|
-
url = url.replace(/\[\[\.\.\.([^\]]+)\]\]/g, "");
|
|
850
|
-
url = url.replace(/\[\.\.\.([^\]]+)\]/g, "");
|
|
851
|
-
url = url.replace(/\[([^\]]+)\]/g, "");
|
|
852
870
|
}
|
|
853
|
-
|
|
854
|
-
if (url.length > 1 && url.endsWith("/")) url = url.slice(0, -1);
|
|
855
|
-
if (!url.startsWith("/")) url = "/" + url;
|
|
856
|
-
return url;
|
|
871
|
+
return params;
|
|
857
872
|
}
|
|
858
|
-
/**
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
if (value === void 0) continue;
|
|
868
|
-
if (Array.isArray(value)) for (const v of value) parts.push(`${encodeURIComponent(key)}=${encodeURIComponent(v)}`);
|
|
869
|
-
else parts.push(`${encodeURIComponent(key)}=${encodeURIComponent(value)}`);
|
|
873
|
+
/** Internal: params for `route` and its ancestors, with catch-alls as arrays. */
|
|
874
|
+
function observeRouteParams(route, routePath) {
|
|
875
|
+
const ancestors = route.pathFromRoot ?? [route];
|
|
876
|
+
const catchAllParams = extractCatchAllParams(routePath);
|
|
877
|
+
return combineLatest([combineLatest(ancestors.map((entry) => entry.params)), combineLatest(ancestors.map((entry) => entry.url ?? of([])))]).pipe(map(([values, segments]) => {
|
|
878
|
+
const params = Object.assign({}, ...values);
|
|
879
|
+
for (const param of catchAllParams) {
|
|
880
|
+
const source = ancestors.findIndex((entry, index) => param.type === "catchAll" ? entry.routeConfig?.path === "**" : !!entry.routeConfig?.matcher && values[index][param.name] != null);
|
|
881
|
+
if (source !== -1) params[param.name] = segments[source].map((segment) => segment.path);
|
|
870
882
|
}
|
|
871
|
-
|
|
872
|
-
}
|
|
873
|
-
if (options?.hash) url += "#" + options.hash;
|
|
874
|
-
return url;
|
|
883
|
+
return params;
|
|
884
|
+
}));
|
|
875
885
|
}
|
|
886
|
+
function resolveArgs(fromOrOptions, options) {
|
|
887
|
+
const typed = typeof fromOrOptions === "string";
|
|
888
|
+
const injector = (typed ? options : fromOrOptions)?.injector;
|
|
889
|
+
return {
|
|
890
|
+
from: typed ? fromOrOptions : "",
|
|
891
|
+
injector,
|
|
892
|
+
route: injector ? injector.get(ActivatedRoute) : inject(ActivatedRoute)
|
|
893
|
+
};
|
|
894
|
+
}
|
|
895
|
+
function injectParams(fromOrOptions, options) {
|
|
896
|
+
const { from, injector, route } = resolveArgs(fromOrOptions, options);
|
|
897
|
+
return toSignal(observeRouteParams(route, from), {
|
|
898
|
+
requireSync: true,
|
|
899
|
+
injector
|
|
900
|
+
});
|
|
901
|
+
}
|
|
902
|
+
function injectQuery(fromOrOptions, options) {
|
|
903
|
+
const { injector, route } = resolveArgs(fromOrOptions, options);
|
|
904
|
+
return toSignal(route.queryParams, {
|
|
905
|
+
requireSync: true,
|
|
906
|
+
injector
|
|
907
|
+
});
|
|
908
|
+
}
|
|
909
|
+
function injectRouteData(fromOrOptions, options) {
|
|
910
|
+
const { injector, route } = resolveArgs(fromOrOptions, options);
|
|
911
|
+
return toSignal(route.data, {
|
|
912
|
+
requireSync: true,
|
|
913
|
+
injector
|
|
914
|
+
});
|
|
915
|
+
}
|
|
916
|
+
function injectResources(fromOrOptions, options) {
|
|
917
|
+
const { route } = resolveArgs(fromOrOptions, options);
|
|
918
|
+
return "resources" in route ? route.resources ?? {} : {};
|
|
919
|
+
}
|
|
920
|
+
//#endregion
|
|
921
|
+
//#region packages/router/src/lib/link-to.directive.ts
|
|
922
|
+
var LinkTo = class LinkTo {
|
|
923
|
+
constructor() {
|
|
924
|
+
this.linkTo = input.required(...[]);
|
|
925
|
+
/** The current route's path. Enables relative destinations and param inheritance. */
|
|
926
|
+
this.from = input(...[]);
|
|
927
|
+
this.routerLink = inject(RouterLink);
|
|
928
|
+
this.route = inject(ActivatedRoute);
|
|
929
|
+
this.currentParams = {};
|
|
930
|
+
}
|
|
931
|
+
ngOnChanges() {
|
|
932
|
+
const from = this.from();
|
|
933
|
+
if (from !== this.paramsFrom) {
|
|
934
|
+
this.paramsFrom = from;
|
|
935
|
+
this.paramsSubscription?.unsubscribe();
|
|
936
|
+
this.paramsSubscription = from ? observeRouteParams(this.route, from).subscribe((params) => {
|
|
937
|
+
this.currentParams = params;
|
|
938
|
+
this.update();
|
|
939
|
+
}) : void 0;
|
|
940
|
+
}
|
|
941
|
+
this.update();
|
|
942
|
+
}
|
|
943
|
+
ngOnDestroy() {
|
|
944
|
+
this.paramsSubscription?.unsubscribe();
|
|
945
|
+
}
|
|
946
|
+
update() {
|
|
947
|
+
const destination = this.linkTo();
|
|
948
|
+
const from = this.from();
|
|
949
|
+
let link = null;
|
|
950
|
+
if (destination) {
|
|
951
|
+
const { path, ...options } = typeof destination === "string" ? { path: destination } : destination;
|
|
952
|
+
if (from) {
|
|
953
|
+
const scoped = resolveScopedRoute(from, path, this.currentParams, options);
|
|
954
|
+
link = buildRouteLink(scoped.path, scoped.options);
|
|
955
|
+
} else link = buildRouteLink(path, options);
|
|
956
|
+
}
|
|
957
|
+
this.routerLink.routerLink = link?.path ?? null;
|
|
958
|
+
this.routerLink.queryParams = link?.queryParams ?? null;
|
|
959
|
+
this.routerLink.fragment = link?.fragment;
|
|
960
|
+
this.routerLink.ngOnChanges({});
|
|
961
|
+
}
|
|
962
|
+
static {
|
|
963
|
+
this.ɵfac = i0.ɵɵngDeclareFactory({
|
|
964
|
+
minVersion: "12.0.0",
|
|
965
|
+
version: "22.1.5",
|
|
966
|
+
ngImport: i0,
|
|
967
|
+
type: LinkTo,
|
|
968
|
+
deps: [],
|
|
969
|
+
target: i0.ɵɵFactoryTarget.Directive
|
|
970
|
+
});
|
|
971
|
+
}
|
|
972
|
+
static {
|
|
973
|
+
this.ɵdir = i0.ɵɵngDeclareDirective({
|
|
974
|
+
minVersion: "17.1.0",
|
|
975
|
+
version: "22.1.5",
|
|
976
|
+
type: LinkTo,
|
|
977
|
+
isStandalone: true,
|
|
978
|
+
selector: "[linkTo]",
|
|
979
|
+
inputs: {
|
|
980
|
+
linkTo: {
|
|
981
|
+
classPropertyName: "linkTo",
|
|
982
|
+
publicName: "linkTo",
|
|
983
|
+
isSignal: true,
|
|
984
|
+
isRequired: true,
|
|
985
|
+
transformFunction: null
|
|
986
|
+
},
|
|
987
|
+
from: {
|
|
988
|
+
classPropertyName: "from",
|
|
989
|
+
publicName: "from",
|
|
990
|
+
isSignal: true,
|
|
991
|
+
isRequired: false,
|
|
992
|
+
transformFunction: null
|
|
993
|
+
}
|
|
994
|
+
},
|
|
995
|
+
usesOnChanges: true,
|
|
996
|
+
hostDirectives: [{
|
|
997
|
+
directive: i1.RouterLink,
|
|
998
|
+
inputs: [
|
|
999
|
+
"target",
|
|
1000
|
+
"target",
|
|
1001
|
+
"queryParamsHandling",
|
|
1002
|
+
"queryParamsHandling",
|
|
1003
|
+
"preserveFragment",
|
|
1004
|
+
"preserveFragment",
|
|
1005
|
+
"skipLocationChange",
|
|
1006
|
+
"skipLocationChange",
|
|
1007
|
+
"replaceUrl",
|
|
1008
|
+
"replaceUrl",
|
|
1009
|
+
"state",
|
|
1010
|
+
"state"
|
|
1011
|
+
]
|
|
1012
|
+
}],
|
|
1013
|
+
ngImport: i0
|
|
1014
|
+
});
|
|
1015
|
+
}
|
|
1016
|
+
};
|
|
1017
|
+
i0.ɵɵngDeclareClassMetadata({
|
|
1018
|
+
minVersion: "12.0.0",
|
|
1019
|
+
version: "22.1.5",
|
|
1020
|
+
ngImport: i0,
|
|
1021
|
+
type: LinkTo,
|
|
1022
|
+
decorators: [{
|
|
1023
|
+
type: Directive,
|
|
1024
|
+
args: [{
|
|
1025
|
+
selector: "[linkTo]",
|
|
1026
|
+
standalone: true,
|
|
1027
|
+
hostDirectives: [{
|
|
1028
|
+
directive: RouterLink,
|
|
1029
|
+
inputs: [
|
|
1030
|
+
"target",
|
|
1031
|
+
"queryParamsHandling",
|
|
1032
|
+
"preserveFragment",
|
|
1033
|
+
"skipLocationChange",
|
|
1034
|
+
"replaceUrl",
|
|
1035
|
+
"state"
|
|
1036
|
+
]
|
|
1037
|
+
}]
|
|
1038
|
+
}]
|
|
1039
|
+
}],
|
|
1040
|
+
propDecorators: {
|
|
1041
|
+
linkTo: [{
|
|
1042
|
+
type: i0.Input,
|
|
1043
|
+
args: [{
|
|
1044
|
+
isSignal: true,
|
|
1045
|
+
alias: "linkTo",
|
|
1046
|
+
required: true
|
|
1047
|
+
}]
|
|
1048
|
+
}],
|
|
1049
|
+
from: [{
|
|
1050
|
+
type: i0.Input,
|
|
1051
|
+
args: [{
|
|
1052
|
+
isSignal: true,
|
|
1053
|
+
alias: "from",
|
|
1054
|
+
required: false
|
|
1055
|
+
}]
|
|
1056
|
+
}]
|
|
1057
|
+
}
|
|
1058
|
+
});
|
|
876
1059
|
//#endregion
|
|
877
1060
|
//#region packages/router/src/lib/inject-navigate.ts
|
|
878
1061
|
function isRoutePathOptionsBase(value) {
|
|
879
1062
|
return !!value && typeof value === "object" && ("params" in value || "query" in value || "hash" in value);
|
|
880
1063
|
}
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
*
|
|
891
|
-
* // With navigation extras
|
|
892
|
-
* navigate('/users/[id]', { params: { id: '42' } }, { replaceUrl: true });
|
|
893
|
-
* ```
|
|
894
|
-
*/
|
|
895
|
-
function injectNavigate() {
|
|
896
|
-
const router = inject(Router);
|
|
897
|
-
const navigate = ((path, ...args) => {
|
|
1064
|
+
function update(value, current) {
|
|
1065
|
+
return typeof value === "function" ? value(current) : value;
|
|
1066
|
+
}
|
|
1067
|
+
function injectNavigate(from, options) {
|
|
1068
|
+
const injector = options?.injector ?? inject(Injector);
|
|
1069
|
+
const router = injector.get(Router);
|
|
1070
|
+
const currentParams = from ? injectParams(from, { injector }) : void 0;
|
|
1071
|
+
const currentQuery = from ? injectQuery(from, { injector }) : void 0;
|
|
1072
|
+
return (target, ...args) => {
|
|
898
1073
|
let options;
|
|
899
1074
|
let extras;
|
|
900
1075
|
if (args.length > 1) {
|
|
@@ -902,262 +1077,24 @@ function injectNavigate() {
|
|
|
902
1077
|
extras = args[1];
|
|
903
1078
|
} else if (args.length === 1) if (isRoutePathOptionsBase(args[0])) options = args[0];
|
|
904
1079
|
else extras = args[0];
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
var EXPERIMENTAL_TYPED_ROUTER = new InjectionToken("EXPERIMENTAL_TYPED_ROUTER");
|
|
914
|
-
/** @experimental */
|
|
915
|
-
var EXPERIMENTAL_ROUTE_CONTEXT = new InjectionToken("EXPERIMENTAL_ROUTE_CONTEXT");
|
|
916
|
-
/** @experimental */
|
|
917
|
-
var EXPERIMENTAL_LOADER_CACHE = new InjectionToken("EXPERIMENTAL_LOADER_CACHE");
|
|
918
|
-
/**
|
|
919
|
-
* Enables experimental typed router features.
|
|
920
|
-
*
|
|
921
|
-
* When active, `routePath()`, `injectNavigate()`, `injectParams()`,
|
|
922
|
-
* and `injectQuery()` will enforce route table constraints and
|
|
923
|
-
* optionally log warnings in strict mode.
|
|
924
|
-
*
|
|
925
|
-
* Inspired by TanStack Router's `Register` interface and strict type
|
|
926
|
-
* checking across the entire navigation surface.
|
|
927
|
-
*
|
|
928
|
-
* @example
|
|
929
|
-
* ```ts
|
|
930
|
-
* provideFileRouter(
|
|
931
|
-
* withTypedRouter({ strictRouteParams: true }),
|
|
932
|
-
* )
|
|
933
|
-
* ```
|
|
934
|
-
*
|
|
935
|
-
* @experimental
|
|
936
|
-
*/
|
|
937
|
-
function withTypedRouter(options) {
|
|
938
|
-
return {
|
|
939
|
-
ɵkind: 102,
|
|
940
|
-
ɵproviders: [{
|
|
941
|
-
provide: EXPERIMENTAL_TYPED_ROUTER,
|
|
942
|
-
useValue: {
|
|
943
|
-
strictRouteParams: false,
|
|
944
|
-
...options
|
|
945
|
-
}
|
|
946
|
-
}]
|
|
947
|
-
};
|
|
948
|
-
}
|
|
949
|
-
/**
|
|
950
|
-
* Provides root-level route context available to all route loaders
|
|
951
|
-
* and components via `injectRouteContext()`.
|
|
952
|
-
*
|
|
953
|
-
* Inspired by TanStack Router's `createRootRouteWithContext<T>()` where
|
|
954
|
-
* a typed context object is required at router creation and automatically
|
|
955
|
-
* available in every route's `beforeLoad` and `loader`.
|
|
956
|
-
*
|
|
957
|
-
* In Angular terms, this creates a DI token that server-side load
|
|
958
|
-
* functions and components can inject to access shared services
|
|
959
|
-
* without importing them individually.
|
|
960
|
-
*
|
|
961
|
-
* @example
|
|
962
|
-
* ```ts
|
|
963
|
-
* // app.config.ts
|
|
964
|
-
* provideFileRouter(
|
|
965
|
-
* withRouteContext({
|
|
966
|
-
* auth: inject(AuthService),
|
|
967
|
-
* db: inject(DatabaseService),
|
|
968
|
-
* }),
|
|
969
|
-
* )
|
|
970
|
-
*
|
|
971
|
-
* // In a component
|
|
972
|
-
* const ctx = injectRouteContext<{ auth: AuthService; db: DatabaseService }>();
|
|
973
|
-
* ```
|
|
974
|
-
*
|
|
975
|
-
* @experimental
|
|
976
|
-
*/
|
|
977
|
-
function withRouteContext(context) {
|
|
978
|
-
return {
|
|
979
|
-
ɵkind: 103,
|
|
980
|
-
ɵproviders: [{
|
|
981
|
-
provide: EXPERIMENTAL_ROUTE_CONTEXT,
|
|
982
|
-
useValue: context
|
|
983
|
-
}]
|
|
984
|
-
};
|
|
985
|
-
}
|
|
986
|
-
/**
|
|
987
|
-
* Configures experimental loader caching behavior for server-loaded
|
|
988
|
-
* route data.
|
|
989
|
-
*
|
|
990
|
-
* Inspired by TanStack Router's built-in cache where `createRouter()`
|
|
991
|
-
* accepts `defaultStaleTime` and `defaultGcTime` to control when
|
|
992
|
-
* loaders re-execute and when cached data is discarded.
|
|
993
|
-
*
|
|
994
|
-
* @example
|
|
995
|
-
* ```ts
|
|
996
|
-
* provideFileRouter(
|
|
997
|
-
* withLoaderCaching({
|
|
998
|
-
* defaultStaleTime: 30_000, // 30s before re-fetch
|
|
999
|
-
* defaultGcTime: 300_000, // 5min cache retention
|
|
1000
|
-
* defaultPendingMs: 200, // 200ms loading delay
|
|
1001
|
-
* }),
|
|
1002
|
-
* )
|
|
1003
|
-
* ```
|
|
1004
|
-
*
|
|
1005
|
-
* @experimental
|
|
1006
|
-
*/
|
|
1007
|
-
function withLoaderCaching(options) {
|
|
1008
|
-
return {
|
|
1009
|
-
ɵkind: 104,
|
|
1010
|
-
ɵproviders: [{
|
|
1011
|
-
provide: EXPERIMENTAL_LOADER_CACHE,
|
|
1012
|
-
useValue: {
|
|
1013
|
-
defaultStaleTime: 0,
|
|
1014
|
-
defaultGcTime: 3e5,
|
|
1015
|
-
defaultPendingMs: 0,
|
|
1016
|
-
...options
|
|
1017
|
-
}
|
|
1018
|
-
}]
|
|
1019
|
-
};
|
|
1020
|
-
}
|
|
1021
|
-
//#endregion
|
|
1022
|
-
//#region packages/router/src/lib/inject-typed-params.ts
|
|
1023
|
-
function extractRouteParams(routePath) {
|
|
1024
|
-
const params = [];
|
|
1025
|
-
for (const match of routePath.matchAll(/\[\[\.\.\.([^\]]+)\]\]/g)) params.push({
|
|
1026
|
-
name: match[1],
|
|
1027
|
-
type: "optionalCatchAll"
|
|
1028
|
-
});
|
|
1029
|
-
for (const match of routePath.matchAll(/(?<!\[)\[\.\.\.([^\]]+)\](?!\])/g)) params.push({
|
|
1030
|
-
name: match[1],
|
|
1031
|
-
type: "catchAll"
|
|
1032
|
-
});
|
|
1033
|
-
for (const match of routePath.matchAll(/(?<!\[)\[(?!\.)([^\]]+)\](?!\])/g)) params.push({
|
|
1034
|
-
name: match[1],
|
|
1035
|
-
type: "dynamic"
|
|
1036
|
-
});
|
|
1037
|
-
return params;
|
|
1038
|
-
}
|
|
1039
|
-
/**
|
|
1040
|
-
* When `strictRouteParams` is enabled, warns if expected params from the
|
|
1041
|
-
* `_from` pattern are missing from the active `ActivatedRoute`.
|
|
1042
|
-
*/
|
|
1043
|
-
function assertRouteMatch(from, route, kind) {
|
|
1044
|
-
const expectedParams = extractRouteParams(from).filter((param) => param.type === "dynamic" || param.type === "catchAll").map((param) => param.name);
|
|
1045
|
-
if (expectedParams.length === 0) return;
|
|
1046
|
-
route.params.pipe(take(1)).subscribe((params) => {
|
|
1047
|
-
for (const name of expectedParams) if (!(name in params)) {
|
|
1048
|
-
console.warn(`[Analog] ${kind}('${from}'): expected param "${name}" is not present in the active route's params. Ensure this hook is used inside a component rendered by '${from}'.`);
|
|
1049
|
-
break;
|
|
1080
|
+
let path = target;
|
|
1081
|
+
if (from && currentParams && currentQuery) {
|
|
1082
|
+
const params = currentParams();
|
|
1083
|
+
({path, options} = resolveScopedRoute(from, target, params, {
|
|
1084
|
+
...options,
|
|
1085
|
+
params: update(options?.params, params),
|
|
1086
|
+
query: update(options?.query, currentQuery())
|
|
1087
|
+
}));
|
|
1050
1088
|
}
|
|
1051
|
-
|
|
1052
|
-
|
|
1053
|
-
|
|
1054
|
-
|
|
1055
|
-
|
|
1056
|
-
|
|
1057
|
-
|
|
1058
|
-
* the params defined for that route.
|
|
1059
|
-
*
|
|
1060
|
-
* The `from` parameter is used purely for TypeScript type inference —
|
|
1061
|
-
* at runtime, params are read from the current `ActivatedRoute`. This
|
|
1062
|
-
* means it works correctly when used inside a component rendered by
|
|
1063
|
-
* the specified route.
|
|
1064
|
-
*
|
|
1065
|
-
* When `withTypedRouter({ strictRouteParams: true })` is configured,
|
|
1066
|
-
* a dev-mode assertion checks that the expected params from `from`
|
|
1067
|
-
* exist in the active route and warns on mismatch.
|
|
1068
|
-
*
|
|
1069
|
-
* @example
|
|
1070
|
-
* ```ts
|
|
1071
|
-
* // In a component rendered at /users/[id]
|
|
1072
|
-
* const params = injectParams('/users/[id]');
|
|
1073
|
-
* // params() → { id: string }
|
|
1074
|
-
*
|
|
1075
|
-
* // With schema validation output types
|
|
1076
|
-
* const params = injectParams('/products/[slug]');
|
|
1077
|
-
* // params() → validated output type from routeParamsSchema
|
|
1078
|
-
* ```
|
|
1079
|
-
*
|
|
1080
|
-
* @experimental
|
|
1081
|
-
*/
|
|
1082
|
-
function injectParams(_from, options) {
|
|
1083
|
-
const injector = options?.injector;
|
|
1084
|
-
const route = injector ? injector.get(ActivatedRoute) : inject(ActivatedRoute);
|
|
1085
|
-
if (isDevMode()) {
|
|
1086
|
-
if ((injector ? injector.get(EXPERIMENTAL_TYPED_ROUTER, null) : inject(EXPERIMENTAL_TYPED_ROUTER, { optional: true }))?.strictRouteParams) assertRouteMatch(_from, route, "injectParams");
|
|
1087
|
-
}
|
|
1088
|
-
return toSignal(route.params.pipe(map((params) => params)), { requireSync: true });
|
|
1089
|
-
}
|
|
1090
|
-
/**
|
|
1091
|
-
* Injects typed route query params as a signal, constrained by the
|
|
1092
|
-
* route table.
|
|
1093
|
-
*
|
|
1094
|
-
* Inspired by TanStack Router's `useSearch({ from: '/issues' })` pattern
|
|
1095
|
-
* where search params are validated and typed per-route via
|
|
1096
|
-
* `validateSearch` schemas.
|
|
1097
|
-
*
|
|
1098
|
-
* In Analog, the typing comes from `routeQuerySchema` exports that are
|
|
1099
|
-
* detected at build time and recorded in the generated route table.
|
|
1100
|
-
*
|
|
1101
|
-
* The `from` parameter is used purely for TypeScript type inference.
|
|
1102
|
-
* When `withTypedRouter({ strictRouteParams: true })` is configured,
|
|
1103
|
-
* a dev-mode assertion checks that the expected params from `from`
|
|
1104
|
-
* exist in the active route and warns on mismatch.
|
|
1105
|
-
*
|
|
1106
|
-
* @example
|
|
1107
|
-
* ```ts
|
|
1108
|
-
* // In a component rendered at /issues
|
|
1109
|
-
* // (where routeQuerySchema validates { page: number, status: string })
|
|
1110
|
-
* const query = injectQuery('/issues');
|
|
1111
|
-
* // query() → { page: number; status: string }
|
|
1112
|
-
* ```
|
|
1113
|
-
*
|
|
1114
|
-
* @experimental
|
|
1115
|
-
*/
|
|
1116
|
-
function injectQuery(_from, options) {
|
|
1117
|
-
const injector = options?.injector;
|
|
1118
|
-
const route = injector ? injector.get(ActivatedRoute) : inject(ActivatedRoute);
|
|
1119
|
-
if (isDevMode()) {
|
|
1120
|
-
if ((injector ? injector.get(EXPERIMENTAL_TYPED_ROUTER, null) : inject(EXPERIMENTAL_TYPED_ROUTER, { optional: true }))?.strictRouteParams) assertRouteMatch(_from, route, "injectQuery");
|
|
1121
|
-
}
|
|
1122
|
-
return toSignal(route.queryParams.pipe(map((params) => params)), { requireSync: true });
|
|
1123
|
-
}
|
|
1124
|
-
//#endregion
|
|
1125
|
-
//#region packages/router/src/lib/inject-route-context.ts
|
|
1126
|
-
/**
|
|
1127
|
-
* Injects the root route context provided via `withRouteContext()`.
|
|
1128
|
-
*
|
|
1129
|
-
* Inspired by TanStack Router's context inheritance where
|
|
1130
|
-
* `createRootRouteWithContext<T>()` makes a typed context available
|
|
1131
|
-
* to every route's `beforeLoad` and `loader` callbacks.
|
|
1132
|
-
*
|
|
1133
|
-
* In Angular, this uses DI under the hood — `withRouteContext(ctx)`
|
|
1134
|
-
* provides the value, and `injectRouteContext<T>()` retrieves it
|
|
1135
|
-
* with the expected type.
|
|
1136
|
-
*
|
|
1137
|
-
* @example
|
|
1138
|
-
* ```ts
|
|
1139
|
-
* // app.config.ts
|
|
1140
|
-
* provideFileRouter(
|
|
1141
|
-
* withRouteContext({
|
|
1142
|
-
* auth: inject(AuthService),
|
|
1143
|
-
* analytics: inject(AnalyticsService),
|
|
1144
|
-
* }),
|
|
1145
|
-
* )
|
|
1146
|
-
*
|
|
1147
|
-
* // any-page.page.ts
|
|
1148
|
-
* const ctx = injectRouteContext<{
|
|
1149
|
-
* auth: AuthService;
|
|
1150
|
-
* analytics: AnalyticsService;
|
|
1151
|
-
* }>();
|
|
1152
|
-
* ctx.analytics.trackPageView();
|
|
1153
|
-
* ```
|
|
1154
|
-
*
|
|
1155
|
-
* @experimental
|
|
1156
|
-
*/
|
|
1157
|
-
function injectRouteContext() {
|
|
1158
|
-
return inject(EXPERIMENTAL_ROUTE_CONTEXT);
|
|
1089
|
+
const link = buildRouteLink(path, options);
|
|
1090
|
+
return router.navigate(link.path, {
|
|
1091
|
+
...extras,
|
|
1092
|
+
queryParams: link.queryParams,
|
|
1093
|
+
fragment: link.fragment
|
|
1094
|
+
});
|
|
1095
|
+
};
|
|
1159
1096
|
}
|
|
1160
1097
|
//#endregion
|
|
1161
|
-
export {
|
|
1098
|
+
export { FormAction, LinkTo, SERVER_FN_DISPATCHER, ServerFnClient, createRoutes, createServerFnRef, defineRouteMeta, getLoadResolver, injectActivatedRoute, injectDebugRoutes, injectLoad, injectLoadData, injectNavigate, injectParams, injectQuery, injectResources, injectResources as injectRouteResources, injectRouteData, injectRouteEndpointURL, injectRouter, injectServerFn, injectServerFnMutation, issuePathToFieldName, issuesToFieldErrors, issuesToFormErrors, provideFileRouter, provideServerFnClient, requestContextInterceptor, routes, toRoute, withDebugRoutes, withExtraRoutes };
|
|
1162
1099
|
|
|
1163
1100
|
//# sourceMappingURL=analogjs-router.mjs.map
|