Skip to content

Commit 92b8640

Browse files
committed
feat: wire route parameter extraction
1 parent 0255df0 commit 92b8640

1 file changed

Lines changed: 81 additions & 0 deletions

File tree

src/analysis.ts

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
import { MemoryDiagnosticCollector } from "./diagnostics.js";
2+
import { ParameterCollector } from "./analysis/collector.js";
3+
import { createDebugLogger } from "./analysis/debug.js";
4+
import { analyzeFunction } from "./analysis/traversal.js";
5+
import type {
6+
AliasTarget,
7+
DebugConfig,
8+
ExtractionContext,
9+
FunctionDefinition,
10+
RouteExtraction,
11+
RouteRegistration,
12+
} from "./types.js";
13+
14+
export function extractRouteData(
15+
route: RouteRegistration,
16+
functions: ReadonlyMap<string, FunctionDefinition>,
17+
constants: ReadonlyMap<string, string>,
18+
maxCallDepth: number,
19+
debugConfig: DebugConfig,
20+
): RouteExtraction {
21+
const diagnostics = new MemoryDiagnosticCollector();
22+
const definition = functions.get(route.handler);
23+
const routeKey = `${route.method} ${route.pathTemplate}`;
24+
const debug = createDebugLogger(routeKey, debugConfig);
25+
const collector = new ParameterCollector(route);
26+
const initialPathParams = collector.snapshot("path");
27+
let pathParams = initialPathParams;
28+
29+
if (definition === undefined) {
30+
diagnostics.add({
31+
level: "error",
32+
code: "handler_not_found",
33+
message: `Could not find function body for handler ${route.handler}.`,
34+
routeKey,
35+
source: route.source,
36+
});
37+
return {
38+
route,
39+
pathParams: initialPathParams,
40+
queryParams: [],
41+
bodyParams: [],
42+
diagnostics: diagnostics.snapshot(),
43+
};
44+
}
45+
46+
const context: ExtractionContext = {
47+
aliasTargets: new Map<string, AliasTarget>(),
48+
constants,
49+
functions,
50+
diagnostics,
51+
maxCallDepth,
52+
pathParamNames: new Set(initialPathParams.map((parameter) => parameter.canonicalPath)),
53+
};
54+
55+
const visited = new Set<string>();
56+
analyzeFunction(definition, context, collector, visited, 0, routeKey, debug);
57+
pathParams = collector.snapshot("path");
58+
debug(
59+
`path params => ${
60+
pathParams.map((item) => item.canonicalPath).join(", ") || "(none)"
61+
}`,
62+
);
63+
const queryParams = collector.snapshot("query");
64+
const bodyParams = collector.snapshot("body");
65+
debug(
66+
`query params => ${
67+
queryParams.map((item) => item.canonicalPath).join(", ") || "(none)"
68+
}`,
69+
);
70+
debug(
71+
`body params => ${bodyParams.map((item) => item.canonicalPath).join(", ") || "(none)"}`,
72+
);
73+
74+
return {
75+
route,
76+
pathParams,
77+
queryParams,
78+
bodyParams,
79+
diagnostics: diagnostics.snapshot(),
80+
};
81+
}

0 commit comments

Comments
 (0)