|
1 | 1 | import fs from "node:fs/promises"; |
2 | 2 | import path from "node:path"; |
3 | 3 | import { directoryExists } from "./dir"; |
| 4 | +import { detectMiddlewareName } from "./middleware"; |
4 | 5 | import { transpile } from "./transpile"; |
5 | 6 | import type { OperationObject } from "@omer-x/openapi-types/operation"; |
6 | 7 |
|
@@ -33,11 +34,18 @@ function safeEval(code: string, routePath: string) { |
33 | 34 | } |
34 | 35 |
|
35 | 36 | export async function getRouteExports(routePath: string, routeDefinerName: string, schemas: Record<string, unknown>) { |
36 | | - const content = await fs.readFile(routePath, "utf-8"); |
37 | | - const code = transpile(content, routeDefinerName); |
| 37 | + const rawCode = await fs.readFile(routePath, "utf-8"); |
| 38 | + const middlewareName = detectMiddlewareName(rawCode); |
| 39 | + const code = transpile(rawCode, routeDefinerName, middlewareName); |
38 | 40 | const fixedCode = Object.keys(schemas).reduce(injectSchemas, code); |
39 | 41 | (global as Record<string, unknown>).schemas = schemas; |
| 42 | + if (middlewareName) { |
| 43 | + // (global as Record<string, unknown>)[middlewareName] = () => { /* mock */ }; |
| 44 | + } |
40 | 45 | const result = safeEval(fixedCode, routePath); |
41 | 46 | delete (global as Record<string, unknown>).schemas; |
| 47 | + if (middlewareName) { |
| 48 | + // delete (global as Record<string, unknown>)[middlewareName]; |
| 49 | + } |
42 | 50 | return result as Record<string, { apiData?: OperationObject } | undefined>; |
43 | 51 | } |
0 commit comments