Skip to content

Commit 902f600

Browse files
committed
add safeEval
1 parent ff975b8 commit 902f600

File tree

3 files changed

+14
-4
lines changed

3 files changed

+14
-4
lines changed

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@omer-x/next-openapi-json-generator",
3-
"version": "0.2.5",
3+
"version": "0.2.6",
44
"description": "a Next.js plugin to generate OpenAPI documentation from route handlers",
55
"keywords": [
66
"next.js",

src/core/next.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,22 @@ function injectSchemas(code: string, refName: string) {
2222
.replace(new RegExp(`\\b${refName}\\b`, "g"), `"${refName}"`);
2323
}
2424

25+
function safeEval(code: string, routePath: string) {
26+
try {
27+
return eval(code);
28+
} catch (error) {
29+
// eslint-disable-next-line no-console
30+
console.log(`An error occured while evaluating the route exports from "${routePath}"`);
31+
throw error;
32+
}
33+
}
34+
2535
export async function getRouteExports(routePath: string, schemas: Record<string, unknown>) {
2636
const content = await fs.readFile(routePath, "utf-8");
2737
const code = transpile(content);
2838
const fixedCode = Object.keys(schemas).reduce(injectSchemas, code);
2939
(global as Record<string, unknown>).schemas = schemas;
30-
const result = eval(fixedCode);
40+
const result = safeEval(fixedCode, routePath);
3141
delete (global as Record<string, unknown>).schemas;
3242
return result as Record<string, { apiData?: OperationObject } | undefined>;
3343
}

0 commit comments

Comments
 (0)