import { matchRoutes } from "react-router-dom"; import { contractRoutePaths, type BoundedContextModule, } from "../contexts/bounded-context-module.ts"; import { FEATURE_REPLACEABLE_PLATFORM_ROUTE_IDS, PLATFORM_ROUTE_REGISTRY, } from "../contracts/routes.ts"; import { assertWithinCardinalityBudget } from "../platform/cardinality/cardinality-budget.ts"; export type DeployableAppModule = Readonly<{ id: string; contexts: readonly Context[]; budget: Readonly<{ maxContexts: number }>; }>; export function defineDeployableAppModule( definition: Readonly<{ id: string; contexts: readonly Context[]; budget: Readonly<{ maxContexts: number }>; }>, ): DeployableAppModule { assertWithinCardinalityBudget( `${definition.id}.contexts`, definition.contexts.length, definition.budget.maxContexts, ); const seen = new Set(); for (const context of definition.contexts) { if (seen.has(context.id)) { throw new TypeError(`Duplicate ${definition.id}.contexts child id: ${context.id}`); } seen.add(context.id); } assertFeatureRoutesReachOwningContext(definition.contexts); return Object.freeze({ ...definition, contexts: Object.freeze([...definition.contexts]), budget: Object.freeze({ ...definition.budget }), }); } /** * 09-06. The router mounts one gateway per context at its route root and * renders a feature route only inside that gateway, while the route registry * and navigation list every feature route wherever it points. A route its * owning root does not reach (outside the root, at the platform home, or * under another context's identical or more specific root) would be a menu * link to NOT_FOUND. Matching uses the router's own ranking, so nested roots * stay allowed as long as each route lands in its owner. Platform routes are * not checked here. * * 09-03. Contexts with a layout of their own are mounted at their registered * paths under it, and layouts sit beside AppShell at `/`: several roots may * share a layout, and a root still takes the URLs under it whichever layout * either context has, so this check spans every layout. A platform route a * feature took over (its home) is mounted at its own path and is exempt. * * 09-03 (1101/14.4). AppShell's platform routes come before every context in * the router (`app-router.tsx`), so a route a platform route also answers is * never rendered: the same path, `/` under another id than `APP_HOME`, or any * route of a `*` root without a layout (NOT_FOUND's splat ties and wins). The * context is matched where the router mounts it: at its root, or under a * layout at the route's own path. Two feature routes with one path shape tie * the same way, and the router renders only the first. */ function assertFeatureRoutesReachOwningContext( contexts: readonly BoundedContextModule[], ): void { const roots = contexts.flatMap((context) => context.route ? [{ id: context.id, path: context.route.path }] : [], ); const featureRouteIds = new Set( contexts.flatMap((context) => context.features.flatMap((feature) => contractRoutePaths(feature.contracts).map(([routeId]) => routeId), ), ), ); const platformRoutes = Object.values(PLATFORM_ROUTE_REGISTRY).flatMap( ({ routeId, path }) => FEATURE_REPLACEABLE_PLATFORM_ROUTE_IDS.includes(routeId) && featureRouteIds.has(routeId) ? [] : [{ id: routeId, path }], ); const claimed = new Map(); for (const context of contexts) { for (const feature of context.features) { for (const [routeId, path] of contractRoutePaths(feature.contracts)) { if (FEATURE_REPLACEABLE_PLATFORM_ROUTE_IDS.includes(routeId)) continue; const reached = matchRoutes(roots, path)?.[0]?.route; if (reached?.id !== context.id || !context.route) { throw new TypeError( `Route ${routeId} (${path}) of feature ${feature.id} is not under the route root of its context ${context.id} (${context.route?.path ?? "no route root"}): ` + (reached ? `context ${reached.id} (${reached.path}) takes that URL.` : "the router would answer NOT_FOUND."), ); } const mount = { id: context.id, path: context.route.loadLayout ? path : context.route.path, }; const answered = matchRoutes([...platformRoutes, mount], path)?.[0]?.route; if (answered !== mount) { throw new TypeError( `Route ${routeId} (${path}) of feature ${feature.id} in context ${context.id} is answered by platform route ${answered?.id} (${answered?.path}). ` + `A feature replaces a platform route only by declaring its id (${FEATURE_REPLACEABLE_PLATFORM_ROUTE_IDS.join(", ")}).`, ); } // Parameter names do not change how the router ranks a path, and // (1103/08-04) neither do letter case or a trailing slash: the router // matches case-insensitively and ignores the slash. const shape = path .replace(/:[^/]+/g, ":") .toLowerCase() .replace(/\/+$/, ""); const earlier = claimed.get(shape); if (earlier !== undefined) { throw new TypeError( `Route ${routeId} (${path}) of feature ${feature.id} has the same path as route ${earlier}: the router renders only the first.`, ); } claimed.set(shape, `${routeId} (${path}) of feature ${feature.id}`); } } } }