@analogjs/router 3.0.0-alpha.90 → 3.0.0-alpha.92
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-server.mjs +295 -257
- package/fesm2022/analogjs-router-server.mjs.map +1 -1
- package/fesm2022/analogjs-router-tanstack-query-server.mjs.map +1 -1
- package/fesm2022/analogjs-router.mjs +295 -357
- package/fesm2022/analogjs-router.mjs.map +1 -1
- package/fesm2022/src.mjs +8 -9
- package/fesm2022/src.mjs.map +1 -1
- package/package.json +11 -7
- package/types/server/actions/src/actions.d.ts +1 -1
- package/types/server/actions/src/define-action.d.ts +1 -1
- package/types/server/actions/src/define-api-route.d.ts +1 -1
- package/types/server/actions/src/define-page-load.d.ts +1 -1
- package/types/server/actions/src/define-server-route.d.ts +1 -1
- package/types/server/src/server-fn/dispatch.d.ts +1 -1
- package/types/server/src/server-fn/event-handler.d.ts +1 -1
- package/types/server/src/server-fn/node-context.d.ts +3 -2
- package/types/server/src/utils/stream-request.d.ts +2 -1
- 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/route-types.d.ts +1 -1
- package/types/src/lib/to-route.d.ts +246 -0
- package/types/tanstack-query/server/src/define-page-load-queries.d.ts +1 -1
- 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,13 +1,14 @@
|
|
|
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
|
-
import { isPlatformServer } from "@angular/common";
|
|
11
|
+
import { isPlatformBrowser, isPlatformServer } from "@angular/common";
|
|
11
12
|
import { toSignal } from "@angular/core/rxjs-interop";
|
|
12
13
|
//#region packages/router/src/lib/define-route.ts
|
|
13
14
|
/**
|
|
@@ -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.");
|
|
@@ -369,6 +370,7 @@ function requestContextInterceptor(req, next) {
|
|
|
369
370
|
const apiPrefix = injectAPIPrefix();
|
|
370
371
|
const baseUrl = injectBaseURL();
|
|
371
372
|
const transferState = inject(TransferState);
|
|
373
|
+
const isBrowser = isPlatformBrowser(inject(PLATFORM_ID));
|
|
372
374
|
const nitroGlobal = globalThis;
|
|
373
375
|
const serverFetch = injectInternalServerFetch() ?? nitroGlobal.$fetch;
|
|
374
376
|
if (serverFetch && baseUrl && (req.url.startsWith("/") || req.url.startsWith(baseUrl) || req.url.startsWith(`/${apiPrefix}`))) {
|
|
@@ -402,7 +404,7 @@ function requestContextInterceptor(req, next) {
|
|
|
402
404
|
return transferResponse;
|
|
403
405
|
}));
|
|
404
406
|
}
|
|
405
|
-
if (req.url.startsWith("/") || req.url.includes("/_analog/")) {
|
|
407
|
+
if (isBrowser && (req.url.startsWith("/") || req.url.includes("/_analog/"))) {
|
|
406
408
|
const toAbsoluteUrl = (url) => new URL(url, window.location.origin);
|
|
407
409
|
const requestUrl = toAbsoluteUrl(req.url).href;
|
|
408
410
|
const { pathname, search } = toAbsoluteUrl(req.urlWithParams);
|
|
@@ -759,17 +761,7 @@ function createServerFnRef(config) {
|
|
|
759
761
|
});
|
|
760
762
|
}
|
|
761
763
|
//#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
|
-
*/
|
|
764
|
+
//#region packages/router/src/lib/to-route.ts
|
|
773
765
|
/**
|
|
774
766
|
* Builds a typed route link object from a route path pattern and options.
|
|
775
767
|
*
|
|
@@ -777,22 +769,26 @@ function createServerFnRef(config) {
|
|
|
777
769
|
* direct use with Angular's routerLink directive inputs.
|
|
778
770
|
*
|
|
779
771
|
* @example
|
|
780
|
-
*
|
|
781
|
-
* // → { path: '/about', queryParams: null, fragment: undefined }
|
|
772
|
+
* toRoute('/about')
|
|
773
|
+
* // → { path: ['/', 'about'], queryParams: null, fragment: undefined }
|
|
782
774
|
*
|
|
783
|
-
*
|
|
784
|
-
* // → { path: '/users
|
|
775
|
+
* toRoute('/users/[id]', { params: { id: '42' } })
|
|
776
|
+
* // → { path: ['/', 'users', '42'], queryParams: null, fragment: undefined }
|
|
785
777
|
*
|
|
786
|
-
*
|
|
787
|
-
* // → { path: '/users
|
|
778
|
+
* toRoute('/users/[id]', { params: { id: '42' }, query: { tab: 'settings' }, hash: 'bio' })
|
|
779
|
+
* // → { path: ['/', 'users', '42'], queryParams: { tab: 'settings' }, fragment: 'bio' }
|
|
788
780
|
*
|
|
789
781
|
* @example Template usage
|
|
782
|
+
* Compute the link in the component:
|
|
783
|
+
* ```ts
|
|
784
|
+
* readonly link = toRoute('/users/[id]', { params: { id: '42' } });
|
|
785
|
+
* ```
|
|
786
|
+
*
|
|
790
787
|
* ```html
|
|
791
|
-
* @let link = routePath('/users/[id]', { params: { id: userId } });
|
|
792
788
|
* <a [routerLink]="link.path" [queryParams]="link.queryParams" [fragment]="link.fragment">
|
|
793
789
|
* ```
|
|
794
790
|
*/
|
|
795
|
-
function
|
|
791
|
+
function toRoute(path, ...args) {
|
|
796
792
|
const options = args[0];
|
|
797
793
|
return buildRouteLink(path, options);
|
|
798
794
|
}
|
|
@@ -801,100 +797,280 @@ function routePath(path, ...args) {
|
|
|
801
797
|
* Exported for direct use in tests (avoids generic constraints).
|
|
802
798
|
*/
|
|
803
799
|
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
800
|
return {
|
|
816
|
-
path:
|
|
817
|
-
queryParams,
|
|
801
|
+
path: buildPath(path, options?.params),
|
|
802
|
+
queryParams: options?.query ?? null,
|
|
818
803
|
fragment: options?.hash
|
|
819
804
|
};
|
|
820
805
|
}
|
|
806
|
+
function buildPath(path, params = {}) {
|
|
807
|
+
return ["/", ...path.split("/").filter(Boolean).flatMap((segment) => {
|
|
808
|
+
const optional = segment.match(/^\[\[\.\.\.([^\]]+)\]\]$/);
|
|
809
|
+
const catchAll = segment.match(/^\[\.\.\.([^\]]+)\]$/);
|
|
810
|
+
const dynamic = segment.match(/^\[([^\]]+)\]$/);
|
|
811
|
+
const match = optional ?? catchAll ?? dynamic;
|
|
812
|
+
if (!match) return [segment];
|
|
813
|
+
const value = params[match[1]];
|
|
814
|
+
if (value == null || Array.isArray(value) && !value.length) {
|
|
815
|
+
if (optional) return [];
|
|
816
|
+
throw new Error(`Missing required ${catchAll ? "catch-all " : ""}param "${match[1]}" for path "${path}"`);
|
|
817
|
+
}
|
|
818
|
+
return Array.isArray(value) ? value.map(String) : [String(value)];
|
|
819
|
+
})];
|
|
820
|
+
}
|
|
821
|
+
/** Internal: resolves `.` and `..` segments in `to` against the `from` route path. */
|
|
822
|
+
function resolveRoutePath(from, to) {
|
|
823
|
+
if (to.startsWith("/")) return to;
|
|
824
|
+
const segments = from.split("/").filter(Boolean);
|
|
825
|
+
for (const part of to.split("/")) if (part === "..") segments.pop();
|
|
826
|
+
else if (part && part !== ".") segments.push(part);
|
|
827
|
+
return "/" + segments.join("/");
|
|
828
|
+
}
|
|
821
829
|
/**
|
|
822
|
-
*
|
|
823
|
-
*
|
|
830
|
+
* Internal: resolves `target` against the `from` route path. Params in the
|
|
831
|
+
* leading segments shared with `from` default to the `current` values.
|
|
824
832
|
*/
|
|
825
|
-
function
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
833
|
+
function resolveScopedRoute(from, target, current, options) {
|
|
834
|
+
const path = resolveRoutePath(from, target);
|
|
835
|
+
const fromSegments = from.split("/").filter(Boolean);
|
|
836
|
+
const toSegments = path.split("/").filter(Boolean);
|
|
837
|
+
const params = {};
|
|
838
|
+
for (let i = 0; i < toSegments.length && toSegments[i] === fromSegments[i]; i++) {
|
|
839
|
+
const name = paramName(toSegments[i]);
|
|
840
|
+
if (name) params[name] = current[name];
|
|
841
|
+
}
|
|
842
|
+
return {
|
|
843
|
+
path,
|
|
844
|
+
options: {
|
|
845
|
+
...options,
|
|
846
|
+
params: {
|
|
847
|
+
...params,
|
|
848
|
+
...options?.params
|
|
840
849
|
}
|
|
841
|
-
|
|
850
|
+
}
|
|
851
|
+
};
|
|
852
|
+
}
|
|
853
|
+
function paramName(segment) {
|
|
854
|
+
return (segment.match(/^\[\[\.\.\.([^\]]+)\]\]$/) ?? segment.match(/^\[\.\.\.([^\]]+)\]$/) ?? segment.match(/^\[([^\]]+)\]$/))?.[1];
|
|
855
|
+
}
|
|
856
|
+
//#endregion
|
|
857
|
+
//#region packages/router/src/lib/inject-typed-params.ts
|
|
858
|
+
function extractCatchAllParams(routePath) {
|
|
859
|
+
const params = [];
|
|
860
|
+
for (const segment of routePath.split("/")) {
|
|
861
|
+
const optional = segment.match(/^\[\[\.\.\.([^\]]+)\]\]$/);
|
|
862
|
+
const required = segment.match(/^\[\.\.\.([^\]]+)\]$/);
|
|
863
|
+
if (optional) params.push({
|
|
864
|
+
name: optional[1],
|
|
865
|
+
type: "optionalCatchAll"
|
|
842
866
|
});
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
return encodeURIComponent(String(value));
|
|
867
|
+
else if (required) params.push({
|
|
868
|
+
name: required[1],
|
|
869
|
+
type: "catchAll"
|
|
847
870
|
});
|
|
848
|
-
} else {
|
|
849
|
-
url = url.replace(/\[\[\.\.\.([^\]]+)\]\]/g, "");
|
|
850
|
-
url = url.replace(/\[\.\.\.([^\]]+)\]/g, "");
|
|
851
|
-
url = url.replace(/\[([^\]]+)\]/g, "");
|
|
852
871
|
}
|
|
853
|
-
|
|
854
|
-
if (url.length > 1 && url.endsWith("/")) url = url.slice(0, -1);
|
|
855
|
-
if (!url.startsWith("/")) url = "/" + url;
|
|
856
|
-
return url;
|
|
872
|
+
return params;
|
|
857
873
|
}
|
|
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)}`);
|
|
874
|
+
/** Internal: params for `route` and its ancestors, with catch-alls as arrays. */
|
|
875
|
+
function observeRouteParams(route, routePath) {
|
|
876
|
+
const ancestors = route.pathFromRoot ?? [route];
|
|
877
|
+
const catchAllParams = extractCatchAllParams(routePath);
|
|
878
|
+
return combineLatest([combineLatest(ancestors.map((entry) => entry.params)), combineLatest(ancestors.map((entry) => entry.url ?? of([])))]).pipe(map(([values, segments]) => {
|
|
879
|
+
const params = Object.assign({}, ...values);
|
|
880
|
+
for (const param of catchAllParams) {
|
|
881
|
+
const source = ancestors.findIndex((entry, index) => param.type === "catchAll" ? entry.routeConfig?.path === "**" : !!entry.routeConfig?.matcher && values[index][param.name] != null);
|
|
882
|
+
if (source !== -1) params[param.name] = segments[source].map((segment) => segment.path);
|
|
870
883
|
}
|
|
871
|
-
|
|
872
|
-
}
|
|
873
|
-
if (options?.hash) url += "#" + options.hash;
|
|
874
|
-
return url;
|
|
884
|
+
return params;
|
|
885
|
+
}));
|
|
875
886
|
}
|
|
887
|
+
function resolveArgs(fromOrOptions, options) {
|
|
888
|
+
const typed = typeof fromOrOptions === "string";
|
|
889
|
+
const injector = (typed ? options : fromOrOptions)?.injector;
|
|
890
|
+
return {
|
|
891
|
+
from: typed ? fromOrOptions : "",
|
|
892
|
+
injector,
|
|
893
|
+
route: injector ? injector.get(ActivatedRoute) : inject(ActivatedRoute)
|
|
894
|
+
};
|
|
895
|
+
}
|
|
896
|
+
function injectParams(fromOrOptions, options) {
|
|
897
|
+
const { from, injector, route } = resolveArgs(fromOrOptions, options);
|
|
898
|
+
return toSignal(observeRouteParams(route, from), {
|
|
899
|
+
requireSync: true,
|
|
900
|
+
injector
|
|
901
|
+
});
|
|
902
|
+
}
|
|
903
|
+
function injectQuery(fromOrOptions, options) {
|
|
904
|
+
const { injector, route } = resolveArgs(fromOrOptions, options);
|
|
905
|
+
return toSignal(route.queryParams, {
|
|
906
|
+
requireSync: true,
|
|
907
|
+
injector
|
|
908
|
+
});
|
|
909
|
+
}
|
|
910
|
+
function injectRouteData(fromOrOptions, options) {
|
|
911
|
+
const { injector, route } = resolveArgs(fromOrOptions, options);
|
|
912
|
+
return toSignal(route.data, {
|
|
913
|
+
requireSync: true,
|
|
914
|
+
injector
|
|
915
|
+
});
|
|
916
|
+
}
|
|
917
|
+
function injectResources(fromOrOptions, options) {
|
|
918
|
+
const { route } = resolveArgs(fromOrOptions, options);
|
|
919
|
+
return "resources" in route ? route.resources ?? {} : {};
|
|
920
|
+
}
|
|
921
|
+
//#endregion
|
|
922
|
+
//#region packages/router/src/lib/link-to.directive.ts
|
|
923
|
+
var LinkTo = class LinkTo {
|
|
924
|
+
constructor() {
|
|
925
|
+
this.linkTo = input.required(...[]);
|
|
926
|
+
/** The current route's path. Enables relative destinations and param inheritance. */
|
|
927
|
+
this.from = input(...[]);
|
|
928
|
+
this.routerLink = inject(RouterLink);
|
|
929
|
+
this.route = inject(ActivatedRoute);
|
|
930
|
+
this.currentParams = {};
|
|
931
|
+
}
|
|
932
|
+
ngOnChanges() {
|
|
933
|
+
const from = this.from();
|
|
934
|
+
if (from !== this.paramsFrom) {
|
|
935
|
+
this.paramsFrom = from;
|
|
936
|
+
this.paramsSubscription?.unsubscribe();
|
|
937
|
+
this.paramsSubscription = from ? observeRouteParams(this.route, from).subscribe((params) => {
|
|
938
|
+
this.currentParams = params;
|
|
939
|
+
this.update();
|
|
940
|
+
}) : void 0;
|
|
941
|
+
}
|
|
942
|
+
this.update();
|
|
943
|
+
}
|
|
944
|
+
ngOnDestroy() {
|
|
945
|
+
this.paramsSubscription?.unsubscribe();
|
|
946
|
+
}
|
|
947
|
+
update() {
|
|
948
|
+
const destination = this.linkTo();
|
|
949
|
+
const from = this.from();
|
|
950
|
+
let link = null;
|
|
951
|
+
if (destination) {
|
|
952
|
+
const { path, ...options } = typeof destination === "string" ? { path: destination } : destination;
|
|
953
|
+
if (from) {
|
|
954
|
+
const scoped = resolveScopedRoute(from, path, this.currentParams, options);
|
|
955
|
+
link = buildRouteLink(scoped.path, scoped.options);
|
|
956
|
+
} else link = buildRouteLink(path, options);
|
|
957
|
+
}
|
|
958
|
+
this.routerLink.routerLink = link?.path ?? null;
|
|
959
|
+
this.routerLink.queryParams = link?.queryParams ?? null;
|
|
960
|
+
this.routerLink.fragment = link?.fragment;
|
|
961
|
+
this.routerLink.ngOnChanges({});
|
|
962
|
+
}
|
|
963
|
+
static {
|
|
964
|
+
this.ɵfac = i0.ɵɵngDeclareFactory({
|
|
965
|
+
minVersion: "12.0.0",
|
|
966
|
+
version: "22.1.5",
|
|
967
|
+
ngImport: i0,
|
|
968
|
+
type: LinkTo,
|
|
969
|
+
deps: [],
|
|
970
|
+
target: i0.ɵɵFactoryTarget.Directive
|
|
971
|
+
});
|
|
972
|
+
}
|
|
973
|
+
static {
|
|
974
|
+
this.ɵdir = i0.ɵɵngDeclareDirective({
|
|
975
|
+
minVersion: "17.1.0",
|
|
976
|
+
version: "22.1.5",
|
|
977
|
+
type: LinkTo,
|
|
978
|
+
isStandalone: true,
|
|
979
|
+
selector: "[linkTo]",
|
|
980
|
+
inputs: {
|
|
981
|
+
linkTo: {
|
|
982
|
+
classPropertyName: "linkTo",
|
|
983
|
+
publicName: "linkTo",
|
|
984
|
+
isSignal: true,
|
|
985
|
+
isRequired: true,
|
|
986
|
+
transformFunction: null
|
|
987
|
+
},
|
|
988
|
+
from: {
|
|
989
|
+
classPropertyName: "from",
|
|
990
|
+
publicName: "from",
|
|
991
|
+
isSignal: true,
|
|
992
|
+
isRequired: false,
|
|
993
|
+
transformFunction: null
|
|
994
|
+
}
|
|
995
|
+
},
|
|
996
|
+
usesOnChanges: true,
|
|
997
|
+
hostDirectives: [{
|
|
998
|
+
directive: i1.RouterLink,
|
|
999
|
+
inputs: [
|
|
1000
|
+
"target",
|
|
1001
|
+
"target",
|
|
1002
|
+
"queryParamsHandling",
|
|
1003
|
+
"queryParamsHandling",
|
|
1004
|
+
"preserveFragment",
|
|
1005
|
+
"preserveFragment",
|
|
1006
|
+
"skipLocationChange",
|
|
1007
|
+
"skipLocationChange",
|
|
1008
|
+
"replaceUrl",
|
|
1009
|
+
"replaceUrl",
|
|
1010
|
+
"state",
|
|
1011
|
+
"state"
|
|
1012
|
+
]
|
|
1013
|
+
}],
|
|
1014
|
+
ngImport: i0
|
|
1015
|
+
});
|
|
1016
|
+
}
|
|
1017
|
+
};
|
|
1018
|
+
i0.ɵɵngDeclareClassMetadata({
|
|
1019
|
+
minVersion: "12.0.0",
|
|
1020
|
+
version: "22.1.5",
|
|
1021
|
+
ngImport: i0,
|
|
1022
|
+
type: LinkTo,
|
|
1023
|
+
decorators: [{
|
|
1024
|
+
type: Directive,
|
|
1025
|
+
args: [{
|
|
1026
|
+
selector: "[linkTo]",
|
|
1027
|
+
standalone: true,
|
|
1028
|
+
hostDirectives: [{
|
|
1029
|
+
directive: RouterLink,
|
|
1030
|
+
inputs: [
|
|
1031
|
+
"target",
|
|
1032
|
+
"queryParamsHandling",
|
|
1033
|
+
"preserveFragment",
|
|
1034
|
+
"skipLocationChange",
|
|
1035
|
+
"replaceUrl",
|
|
1036
|
+
"state"
|
|
1037
|
+
]
|
|
1038
|
+
}]
|
|
1039
|
+
}]
|
|
1040
|
+
}],
|
|
1041
|
+
propDecorators: {
|
|
1042
|
+
linkTo: [{
|
|
1043
|
+
type: i0.Input,
|
|
1044
|
+
args: [{
|
|
1045
|
+
isSignal: true,
|
|
1046
|
+
alias: "linkTo",
|
|
1047
|
+
required: true
|
|
1048
|
+
}]
|
|
1049
|
+
}],
|
|
1050
|
+
from: [{
|
|
1051
|
+
type: i0.Input,
|
|
1052
|
+
args: [{
|
|
1053
|
+
isSignal: true,
|
|
1054
|
+
alias: "from",
|
|
1055
|
+
required: false
|
|
1056
|
+
}]
|
|
1057
|
+
}]
|
|
1058
|
+
}
|
|
1059
|
+
});
|
|
876
1060
|
//#endregion
|
|
877
1061
|
//#region packages/router/src/lib/inject-navigate.ts
|
|
878
1062
|
function isRoutePathOptionsBase(value) {
|
|
879
1063
|
return !!value && typeof value === "object" && ("params" in value || "query" in value || "hash" in value);
|
|
880
1064
|
}
|
|
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) => {
|
|
1065
|
+
function update(value, current) {
|
|
1066
|
+
return typeof value === "function" ? value(current) : value;
|
|
1067
|
+
}
|
|
1068
|
+
function injectNavigate(from, options) {
|
|
1069
|
+
const injector = options?.injector ?? inject(Injector);
|
|
1070
|
+
const router = injector.get(Router);
|
|
1071
|
+
const currentParams = from ? injectParams(from, { injector }) : void 0;
|
|
1072
|
+
const currentQuery = from ? injectQuery(from, { injector }) : void 0;
|
|
1073
|
+
return (target, ...args) => {
|
|
898
1074
|
let options;
|
|
899
1075
|
let extras;
|
|
900
1076
|
if (args.length > 1) {
|
|
@@ -902,262 +1078,24 @@ function injectNavigate() {
|
|
|
902
1078
|
extras = args[1];
|
|
903
1079
|
} else if (args.length === 1) if (isRoutePathOptionsBase(args[0])) options = args[0];
|
|
904
1080
|
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;
|
|
1081
|
+
let path = target;
|
|
1082
|
+
if (from && currentParams && currentQuery) {
|
|
1083
|
+
const params = currentParams();
|
|
1084
|
+
({path, options} = resolveScopedRoute(from, target, params, {
|
|
1085
|
+
...options,
|
|
1086
|
+
params: update(options?.params, params),
|
|
1087
|
+
query: update(options?.query, currentQuery())
|
|
1088
|
+
}));
|
|
1050
1089
|
}
|
|
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);
|
|
1090
|
+
const link = buildRouteLink(path, options);
|
|
1091
|
+
return router.navigate(link.path, {
|
|
1092
|
+
...extras,
|
|
1093
|
+
queryParams: link.queryParams,
|
|
1094
|
+
fragment: link.fragment
|
|
1095
|
+
});
|
|
1096
|
+
};
|
|
1159
1097
|
}
|
|
1160
1098
|
//#endregion
|
|
1161
|
-
export {
|
|
1099
|
+
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
1100
|
|
|
1163
1101
|
//# sourceMappingURL=analogjs-router.mjs.map
|