-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathrouterResolver.ts
More file actions
401 lines (363 loc) · 11.7 KB
/
routerResolver.ts
File metadata and controls
401 lines (363 loc) · 11.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
import { log } from "../utils/logger"
import { analyzeFile } from "./analyzer"
import type { FileSystem } from "./filesystem"
import { resolveNamedImport, resolveRouterFromInit } from "./importResolver"
import type {
FileAnalysis,
RouteInfo,
RouterInfo,
RouterNode,
} from "./internal"
import type { Parser } from "./parser"
export type { RouterNode }
interface ResolutionContext {
projectRootUri: string
parser: Parser
fs: FileSystem
visited: Set<string>
}
/**
* Finds the main FastAPI app or APIRouter in the list of routers.
* If targetVariable is specified, only returns the router with that variable name.
* Otherwise, prioritizes FastAPI apps over APIRouters.
*/
function findAppRouter(
routers: RouterInfo[],
targetVariable?: string,
): RouterInfo | undefined {
if (targetVariable) {
return routers.find((r) => r.variableName === targetVariable)
}
return (
routers.find((r) => r.type === "FastAPI") ??
routers.find((r) => r.type === "APIRouter")
)
}
function createRouterNode(
router: RouterInfo,
routes: RouteInfo[],
filePath: string,
): RouterNode {
return {
filePath,
variableName: router.variableName,
type: router.type,
prefix: router.prefix,
tags: router.tags,
line: router.line,
column: router.column,
routes: routes.map((r) => ({
method: r.method,
path: r.path,
function: r.function,
line: r.line,
column: r.column,
docstring: r.docstring,
})),
children: [],
}
}
async function processIncludeRouters(
analysis: FileAnalysis,
ownerRouter: RouterNode,
currentFileUri: string,
ctx: ResolutionContext,
): Promise<void> {
const includes = analysis.includeRouters.filter(
(inc) => inc.owner === ownerRouter.variableName,
)
for (const include of includes) {
log(
`Resolving include_router: ${include.router} (prefix: ${include.prefix || "none"})`,
)
const childRouter = await resolveRouterReference(
include.router,
analysis,
currentFileUri,
ctx,
)
if (childRouter) {
// Merge tags from include_router call with the router's own tags
if (include.tags.length > 0) {
childRouter.tags = [...new Set([...childRouter.tags, ...include.tags])]
}
ownerRouter.children.push({
router: childRouter,
prefix: include.prefix,
tags: include.tags,
})
}
}
}
/**
* Builds a router graph starting from the given entry file.
* If targetVariable is specified, only that specific app/router will be used.
*/
export async function buildRouterGraph(
entryFileUri: string,
parser: Parser,
projectRootUri: string,
fs: FileSystem,
targetVariable?: string,
): Promise<RouterNode | null> {
return buildRouterGraphInternal(
entryFileUri,
{ projectRootUri, parser, fs, visited: new Set() },
targetVariable,
)
}
/**
* Internal recursive function to build the router graph.
*/
async function buildRouterGraphInternal(
entryFileUri: string,
ctx: ResolutionContext,
targetVariable?: string,
): Promise<RouterNode | null> {
const { projectRootUri, parser, fs, visited } = ctx
// Check if file exists
if (!(await fs.exists(entryFileUri))) {
log(`File not found: "${entryFileUri}"`)
return null
}
// Prevent infinite recursion on circular imports
if (visited.has(entryFileUri)) {
log(`Skipping already visited file: "${entryFileUri}"`)
return null
}
visited.add(entryFileUri)
// Helper to analyze a file with the filesystem
const analyzeFileFn = (uri: string) => analyzeFile(uri, parser, fs)
// Analyze the entry file
let analysis = await analyzeFileFn(entryFileUri)
if (!analysis) {
log(`Failed to analyze file: "${entryFileUri}"`)
return null
}
// Track current resolved URI (may change if following re-exports)
let resolvedEntryUri = entryFileUri
log(
`Analyzed "${resolvedEntryUri}": ${analysis.routes.length} routes, ${analysis.routers.length} routers, ${analysis.includeRouters.length} include_router calls`,
)
// Find FastAPI instantiation (filter by targetVariable if specified)
let appRouter = findAppRouter(analysis.routers, targetVariable)
// If no FastAPI/APIRouter found and this is an __init__.py, check for re-exports
if (!appRouter && entryFileUri.endsWith("__init__.py")) {
const actualRouterUri = await resolveRouterFromInit(
entryFileUri,
projectRootUri,
fs,
analyzeFileFn,
)
if (actualRouterUri && !visited.has(actualRouterUri)) {
visited.add(actualRouterUri)
const actualAnalysis = await analyzeFileFn(actualRouterUri)
if (actualAnalysis) {
const actualRouter = findAppRouter(actualAnalysis.routers)
if (actualRouter) {
analysis = actualAnalysis
appRouter = actualRouter
resolvedEntryUri = actualRouterUri
}
}
}
}
// Factory function: if the entrypoint variable (e.g. "app" from "main:app")
// is assigned via a factory function (`app = create_app()`) rather than a direct
// FastAPI() constructor call, static analysis can't determine the type. If routes
// are decorated with @app.get(...) etc. though, we know it must be a FastAPI instance.
if (
!appRouter &&
targetVariable &&
analysis.routes.some((r) => r.owner === targetVariable)
) {
appRouter = {
variableName: targetVariable,
type: "FastAPI",
prefix: "",
tags: [],
line: 0,
column: 0,
}
}
// Factory function in another module: if the entrypoint variable is assigned via
// `app = create_app()` where `create_app` is imported, follow the import to the
// factory file and build the router graph from there. This works because
// routerExtractor and includeRouterExtractor recurse into function bodies, so
// `app = FastAPI()` and `app.include_router(...)` inside `create_app` are visible
// when analyzing the factory file directly.
if (!appRouter && targetVariable) {
const factoryCall = analysis.factoryCalls.find(
(fc) => fc.variableName === targetVariable,
)
if (factoryCall) {
const matchingImport = analysis.imports.find((imp) =>
imp.names.includes(factoryCall.functionName),
)
if (matchingImport) {
const namedImport = matchingImport.namedImports.find(
(ni) => (ni.alias ?? ni.name) === factoryCall.functionName,
)
const originalName = namedImport?.name ?? factoryCall.functionName
const factoryFileUri = await resolveNamedImport(
{
modulePath: matchingImport.modulePath,
names: [originalName],
isRelative: matchingImport.isRelative,
relativeDots: matchingImport.relativeDots,
},
entryFileUri,
projectRootUri,
fs,
analyzeFileFn,
)
if (factoryFileUri && !visited.has(factoryFileUri)) {
const factoryGraph = await buildRouterGraphInternal(
factoryFileUri,
ctx,
)
if (factoryGraph) {
factoryGraph.variableName = targetVariable
return factoryGraph
}
}
}
}
}
if (!appRouter) {
return null
}
// Find all routers included in the app
// Only include routes that belong directly to the app (not to local APIRouters)
const appRoutes = analysis.routes.filter(
(r) => r.owner === appRouter.variableName,
)
const rootRouter = createRouterNode(appRouter, appRoutes, resolvedEntryUri)
// Process include_router calls to find child routers
await processIncludeRouters(analysis, rootRouter, resolvedEntryUri, ctx)
// Process mount() calls for subapps
for (const mount of analysis.mounts) {
const childRouter = await resolveRouterReference(
mount.app,
analysis,
resolvedEntryUri,
ctx,
)
if (childRouter) {
rootRouter.children.push({
router: childRouter,
prefix: mount.path,
tags: [],
})
}
}
return rootRouter
}
/**
* Resolves a router/app reference to its RouterNode.
* Used for include_router and mount calls.
*
* Handles both simple references (e.g., "router") and dotted references
* (e.g., "api_routes.router" where api_routes is an imported module).
*/
async function resolveRouterReference(
reference: string,
analysis: FileAnalysis,
currentFileUri: string,
ctx: ResolutionContext,
): Promise<RouterNode | null> {
const { projectRootUri, parser, fs, visited } = ctx
const parts = reference.split(".")
const moduleName = parts[0]
// For dotted references like "api_routes.router", extract the attribute name
const attributeName = parts.length > 1 ? parts.slice(1).join(".") : null
// First, check if this is a local router defined in the same file
const localRouter = analysis.routers.find(
(r) => r.variableName === moduleName && r.type === "APIRouter",
)
if (localRouter) {
// Filter routes that belong to this router (decorated with @router.method)
const routerRoutes = analysis.routes.filter((r) => r.owner === moduleName)
const routerNode = createRouterNode(
localRouter,
routerRoutes,
currentFileUri,
)
// Process include_router calls owned by this router (nested routers)
await processIncludeRouters(analysis, routerNode, currentFileUri, ctx)
return routerNode
}
// Otherwise, look for an imported router
const matchingImport = analysis.imports.find((imp) =>
imp.names.includes(moduleName),
)
if (!matchingImport) {
log(`No import found for router reference: ${reference}`)
return null
}
// Helper to analyze a file with the filesystem
const analyzeFileFn = (uri: string) => analyzeFile(uri, parser, fs)
// Find the original import name (in case moduleName is an alias)
// e.g., "from .api_tokens import router as api_tokens_router"
// moduleName = "api_tokens_router", originalName = "router"
const namedImport = matchingImport.namedImports.find(
(ni) => (ni.alias ?? ni.name) === moduleName,
)
const originalName = namedImport?.name ?? moduleName
// Resolve the imported module to a file URI
const importedFileUri = await resolveNamedImport(
{
modulePath: matchingImport.modulePath,
names: [originalName],
isRelative: matchingImport.isRelative,
relativeDots: matchingImport.relativeDots,
},
currentFileUri,
projectRootUri,
fs,
analyzeFileFn,
)
if (!importedFileUri) {
log(`Could not resolve import: ${matchingImport.modulePath}`)
return null
}
// For dotted references (e.g., "api_routes.router"), we need to find
// the specific attribute within the resolved module
if (attributeName) {
// Analyze the imported file to find the router by attribute name
const importedAnalysis = await analyzeFileFn(importedFileUri)
if (!importedAnalysis) {
return null
}
// Find the router with the matching variable name
const targetRouter = importedAnalysis.routers.find(
(r) => r.variableName === attributeName,
)
if (targetRouter) {
// Mark as visited to prevent infinite recursion
if (visited.has(importedFileUri)) {
return null
}
visited.add(importedFileUri)
// Get routes belonging to this router
const routerRoutes = importedAnalysis.routes.filter(
(r) => r.owner === attributeName,
)
const routerNode = createRouterNode(
targetRouter,
routerRoutes,
importedFileUri,
)
// Process include_router calls owned by this router (nested routers)
await processIncludeRouters(
importedAnalysis,
routerNode,
importedFileUri,
ctx,
)
return routerNode
}
// If not found as a router, fall through to try building from file
}
return buildRouterGraphInternal(importedFileUri, ctx)
}