78 lines
2.7 KiB
TypeScript
78 lines
2.7 KiB
TypeScript
import fs from "fs";
|
|
import path from "path";
|
|
import type { AddressCapabilityLayer, AddressCapabilityRouteMode, AddressIntent } from "../types/addressQuery";
|
|
|
|
export interface AddressRouteBaselineEntry {
|
|
intent: AddressIntent;
|
|
capability_id: string;
|
|
capability_layer: AddressCapabilityLayer;
|
|
capability_route_mode: AddressCapabilityRouteMode;
|
|
}
|
|
|
|
export interface AddressRouteBaselineContract {
|
|
schema_version: "address_route_baseline_v1";
|
|
updated_at: string;
|
|
entries: AddressRouteBaselineEntry[];
|
|
}
|
|
|
|
const BASELINE_FILE = path.resolve(__dirname, "..", "..", "..", "..", "docs", "TECH", "address_route_baseline_v1.json");
|
|
|
|
function toObject(value: unknown): Record<string, unknown> | null {
|
|
if (!value || typeof value !== "object" || Array.isArray(value)) {
|
|
return null;
|
|
}
|
|
return value as Record<string, unknown>;
|
|
}
|
|
|
|
function toNonEmptyString(value: unknown): string | null {
|
|
if (typeof value !== "string") {
|
|
return null;
|
|
}
|
|
const trimmed = value.trim();
|
|
return trimmed.length > 0 ? trimmed : null;
|
|
}
|
|
|
|
function parseEntry(value: unknown): AddressRouteBaselineEntry | null {
|
|
const object = toObject(value);
|
|
if (!object) {
|
|
return null;
|
|
}
|
|
const intent = toNonEmptyString(object.intent) as AddressIntent | null;
|
|
const capabilityId = toNonEmptyString(object.capability_id);
|
|
const layer = toNonEmptyString(object.capability_layer) as AddressCapabilityLayer | null;
|
|
const routeMode = toNonEmptyString(object.capability_route_mode) as AddressCapabilityRouteMode | null;
|
|
if (!intent || !capabilityId || !layer || !routeMode) {
|
|
return null;
|
|
}
|
|
return {
|
|
intent,
|
|
capability_id: capabilityId,
|
|
capability_layer: layer,
|
|
capability_route_mode: routeMode
|
|
};
|
|
}
|
|
|
|
export function loadAddressRouteBaselineContract(): AddressRouteBaselineContract {
|
|
const raw = fs.readFileSync(BASELINE_FILE, "utf-8");
|
|
const parsed = JSON.parse(raw) as unknown;
|
|
const root = toObject(parsed);
|
|
if (!root) {
|
|
throw new Error("address_route_baseline_v1: invalid root payload");
|
|
}
|
|
const schemaVersion = toNonEmptyString(root.schema_version);
|
|
if (schemaVersion !== "address_route_baseline_v1") {
|
|
throw new Error(`address_route_baseline_v1: unexpected schema version '${schemaVersion ?? "null"}'`);
|
|
}
|
|
const updatedAt = toNonEmptyString(root.updated_at) ?? new Date().toISOString();
|
|
const entriesRaw = Array.isArray(root.entries) ? root.entries : [];
|
|
const entries = entriesRaw.map(parseEntry).filter((entry): entry is AddressRouteBaselineEntry => entry !== null);
|
|
if (entries.length === 0) {
|
|
throw new Error("address_route_baseline_v1: no valid entries");
|
|
}
|
|
return {
|
|
schema_version: "address_route_baseline_v1",
|
|
updated_at: updatedAt,
|
|
entries
|
|
};
|
|
}
|