Files
clean-architecture-frontend…/src/apps/deployable-app-module.ts
T
DongHyeonka 851fdedbcd fix(apps): 대소문자·끝 슬래시만 다른 기능 라우트도 같은 경로로 거절한다 (1103/08-04)
문제
같은 모양 검사가 파라미터 이름만 지운 문자열로 비교했다. React Router는
대소문자를 무시하고 끝 슬래시를 같은 경로로 본다. 그래서 /shop/items/:itemId와
/shop/Items/:id, /shop/items와 /shop/items/가 조립을 통과했고 라우터는 그중
하나만 그렸다. 메뉴·레지스트리에는 둘 다 나왔다.

고른 방법
모양 키를 만들 때 소문자로 바꾸고 끝 슬래시를 지운다. 라우트 경로 문법을
소문자·끝 슬래시 없음으로 제한하는 검사는 기존 라우트에 새 규칙을 거는
것이라 버렸다.

위험·한계
라우터가 caseSensitive를 켜면 이 검사가 과하게 거절한다. src에 caseSensitive
사용은 없다.

검증
- RED: context-route-roots.test.ts의 새 두 행이 수정 전 실패.
- tests/architecture, router, context-layouts → 9 files / 64 tests 통과.
  /a/new와 /a/:id 허용 행은 그대로 통과. eslint 통과.

Refs: docs/reviews/2026-11-03-maturity-review/08-presentation.md 08-04
2026-11-04 20:19:07 +09:00

133 lines
5.5 KiB
TypeScript

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<Context extends BoundedContextModule = BoundedContextModule> = Readonly<{
id: string;
contexts: readonly Context[];
budget: Readonly<{ maxContexts: number }>;
}>;
export function defineDeployableAppModule<const Context extends BoundedContextModule>(
definition: Readonly<{
id: string;
contexts: readonly Context[];
budget: Readonly<{ maxContexts: number }>;
}>,
): DeployableAppModule<Context> {
assertWithinCardinalityBudget(
`${definition.id}.contexts`,
definition.contexts.length,
definition.budget.maxContexts,
);
const seen = new Set<string>();
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<string, string>();
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}`);
}
}
}
}