Skip to content

Commit c184b58

Browse files
committed
add a better error handling in eval
1 parent 36cd6e1 commit c184b58

File tree

4 files changed

+16
-20
lines changed

4 files changed

+16
-20
lines changed

src/core/next.ts

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,18 @@ export async function findAppFolderPath() {
1515
return null;
1616
}
1717

18-
function advancedEval(code: string) {
19-
try {
20-
return eval(code);
21-
} catch (error) {
22-
if (error instanceof ReferenceError) {
23-
const refName = error.message.replace("is not defined", "").trim();
24-
return advancedEval(code.replace(new RegExp(`\\b${refName}\\b`, "g"), `"${refName}"`));
25-
}
26-
throw error;
27-
// console.error(error);
28-
// return {};
29-
}
18+
function injectSchemas(code: string, refName: string) {
19+
return code
20+
.replace(new RegExp(`\\b${refName}\\.`, "g"), `global.schemas[${refName}].`)
21+
.replace(new RegExp(`\\b${refName}\\b`, "g"), `"${refName}"`);
3022
}
3123

32-
export async function getRouteExports(routePath: string) {
24+
export async function getRouteExports(routePath: string, schemas: Record<string, unknown>) {
3325
const content = await fs.readFile(routePath, "utf-8");
3426
const code = transpile(content);
35-
return advancedEval(code) as Record<string, { apiData?: unknown } | undefined>;
27+
const fixedCode = Object.keys(schemas).reduce(injectSchemas, code);
28+
(global as Record<string, unknown>).schemas = schemas;
29+
const result = eval(fixedCode);
30+
delete (global as Record<string, unknown>).schemas;
31+
return result as Record<string, { apiData?: unknown } | undefined>;
3632
}

src/core/route.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import type { PathsObject } from "@omer-x/openapi-types/dist/paths";
1+
import type { PathsObject } from "@omer-x/openapi-types/paths";
22

3-
type RouteRecord = {
3+
export type RouteRecord = {
44
method: string,
55
path: string,
66
apiData: object,

src/core/schema.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import zodToJsonSchema from "zod-to-json-schema";
2-
import type { SchemaObject } from "@omer-x/openapi-types/dist/schema";
2+
import type { SchemaObject } from "@omer-x/openapi-types/schema";
33
import type { ZodType } from "zod";
44

55
export function bundleSchemas(schemas: Record<string, ZodType>) {

src/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import getPackageMetadata from "@omer-x/package-metadata";
22
import { getDirectoryItems } from "./core/dir";
33
import { findAppFolderPath, getRouteExports } from "./core/next";
4-
import { bundlePaths, createRouteRecord } from "./core/route";
4+
import { type RouteRecord, bundlePaths, createRouteRecord } from "./core/route";
55
import { bundleSchemas } from "./core/schema";
66
import type { OpenApiDocument } from "@omer-x/openapi-types";
77
import type { ZodType } from "zod";
@@ -10,9 +10,9 @@ export default async function generateOpenApiSpec(schemas: Record<string, ZodTyp
1010
const appFolderPath = await findAppFolderPath();
1111
if (!appFolderPath) throw new Error("This is not a Next.js application!");
1212
const routes = await getDirectoryItems(appFolderPath, "route.ts");
13-
const validRoutes = [];
13+
const validRoutes: RouteRecord[] = [];
1414
for (const route of routes) {
15-
const exportedRouteHandlers = await getRouteExports(route);
15+
const exportedRouteHandlers = await getRouteExports(route, schemas);
1616
for (const [method, routeHandler] of Object.entries(exportedRouteHandlers)) {
1717
if (!routeHandler || !routeHandler.apiData) continue;
1818
validRoutes.push(createRouteRecord(

0 commit comments

Comments
 (0)