Skip to content

Commit 59674fe

Browse files
committed
refactor: move injectSchemas
1 parent 0f0e288 commit 59674fe

File tree

2 files changed

+6
-12
lines changed

2 files changed

+6
-12
lines changed

src/core/injectSchemas.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export default function injectSchemas(code: string, refName: string) {
2+
return code
3+
.replace(new RegExp(`\\b${refName}\\.`, "g"), `global.schemas[${refName}].`)
4+
.replace(new RegExp(`\\b${refName}\\b`, "g"), `"${refName}"`);
5+
}

src/core/next.ts

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import fs from "node:fs/promises";
22
import path from "node:path";
33
import { directoryExists } from "./dir";
4+
import injectSchemas from "./injectSchemas";
45
import { detectMiddlewareName } from "./middleware";
56
import { transpile } from "./transpile";
67
import type { OperationObject } from "@omer-x/openapi-types/operation";
@@ -17,12 +18,6 @@ export async function findAppFolderPath() {
1718
return null;
1819
}
1920

20-
function injectSchemas(code: string, refName: string) {
21-
return code
22-
.replace(new RegExp(`\\b${refName}\\.`, "g"), `global.schemas[${refName}].`)
23-
.replace(new RegExp(`\\b${refName}\\b`, "g"), `"${refName}"`);
24-
}
25-
2621
function safeEval(code: string, routePath: string) {
2722
try {
2823
return eval(code);
@@ -39,13 +34,7 @@ export async function getRouteExports(routePath: string, routeDefinerName: strin
3934
const code = transpile(rawCode, routeDefinerName, middlewareName);
4035
const fixedCode = Object.keys(schemas).reduce(injectSchemas, code);
4136
(global as Record<string, unknown>).schemas = schemas;
42-
if (middlewareName) {
43-
// (global as Record<string, unknown>)[middlewareName] = () => { /* mock */ };
44-
}
4537
const result = safeEval(fixedCode, routePath);
4638
delete (global as Record<string, unknown>).schemas;
47-
if (middlewareName) {
48-
// delete (global as Record<string, unknown>)[middlewareName];
49-
}
5039
return result as Record<string, { apiData?: OperationObject } | undefined>;
5140
}

0 commit comments

Comments
 (0)