Skip to content

Commit

Permalink
prettier lint
Browse files Browse the repository at this point in the history
  • Loading branch information
eriksLapins committed Jan 22, 2024
1 parent 76aa104 commit 4b6f151
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 53 deletions.
2 changes: 1 addition & 1 deletion src/runtime/route-rules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,5 +72,5 @@ export function getRouteRulesForPath(path: string): NitroRouteRules {
}

export function defineRouteMeta(meta: ServerRouteMeta) {
return meta
return meta;
}
4 changes: 2 additions & 2 deletions src/runtime/routes/openapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ function getPaths(): PathsObject {
if (h.meta && h.meta.openAPI) {
paths[route][method] = {
...paths[route][method],
...h.meta.openAPI
}
...h.meta.openAPI,
};
}
}

Expand Down
7 changes: 3 additions & 4 deletions src/runtime/virtual/server-handlers.d.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import type { H3EventHandler, LazyEventHandler, RouterMethod } from "h3";
import type { OperationObject } from "openapi-typescript";


export type ServerRouteMeta = {
openAPI?: OperationObject,
[key: string] : any,
}
openAPI?: OperationObject;
[key: string]: any;
};

export type HandlerDefinition = {
route: string;
Expand Down
105 changes: 60 additions & 45 deletions src/scan.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import fs from "node:fs";
import { runInNewContext } from "node:vm";
import { relative, join } from "pathe";
import acorn from 'acorn';
import acorn from "acorn";
import { transform } from "esbuild";
import { CallExpression, Node } from "estree";
import { walk } from 'estree-walker';
import { walk } from "estree-walker";
import { globby } from "globby";
import { withBase, withLeadingSlash, withoutTrailingSlash } from "ufo";
import type { Nitro } from "./types";
import type { ServerRouteMeta } from "#internal/nitro/virtual/server-handlers";

export const GLOB_SCAN_PATTERN = "**/*.{js,mjs,cjs,ts,mts,cts,tsx,jsx}";
type FileInfo = { path: string; fullPath: string};
type FileInfo = { path: string; fullPath: string };

const httpMethodRegex =
/\.(connect|delete|get|head|options|patch|post|put|trace)$/;
Expand Down Expand Up @@ -54,34 +54,35 @@ export async function scanServerRoutes(
prefix = "/"
) {
const files = await scanFiles(nitro, dir);
return await Promise.all(files.map(async (file) => {
let route = file.path
.replace(/\.[A-Za-z]+$/, "")
.replace(/\[\.{3}]/g, "**")
.replace(/\[\.{3}(\w+)]/g, "**:$1")
.replace(/\[(\w+)]/g, ":$1");
route = withLeadingSlash(withoutTrailingSlash(withBase(route, prefix)));

const meta = await scanRouteMeta(file.fullPath)

let method;
const methodMatch = route.match(httpMethodRegex);
if (methodMatch) {
route = route.slice(0, Math.max(0, methodMatch.index));
method = methodMatch[1];
}
return await Promise.all(
files.map(async (file) => {
let route = file.path
.replace(/\.[A-Za-z]+$/, "")
.replace(/\[\.{3}]/g, "**")
.replace(/\[\.{3}(\w+)]/g, "**:$1")
.replace(/\[(\w+)]/g, ":$1");
route = withLeadingSlash(withoutTrailingSlash(withBase(route, prefix)));

const meta = await scanRouteMeta(file.fullPath);

let method;
const methodMatch = route.match(httpMethodRegex);
if (methodMatch) {
route = route.slice(0, Math.max(0, methodMatch.index));
method = methodMatch[1];
}

route = route.replace(/\/index$/, "") || "/";
route = route.replace(/\/index$/, "") || "/";

return {
handler: file.fullPath,
lazy: true,
middleware: false,
route,
method,
meta
};
})
return {
handler: file.fullPath,
lazy: true,
middleware: false,
route,
method,
meta,
};
})
);
}

Expand Down Expand Up @@ -135,32 +136,46 @@ async function scanDir(
}

export async function scanRouteMeta(
fullPath: string,
fullPath: string
): Promise<ServerRouteMeta> {
const file = fs.readFileSync(fullPath, { encoding: 'utf8', flag: 'r' });

let routeMeta: ServerRouteMeta | null = null;
const file = fs.readFileSync(fullPath, { encoding: "utf8", flag: "r" });

const js = await transform(file, { loader: 'ts' });
const fileAST = acorn.parse(js.code, { ecmaVersion: "latest", sourceType: "module" }) as Node;
let routeMeta: ServerRouteMeta | null = null;

const js = await transform(file, { loader: "ts" });
const fileAST = acorn.parse(js.code, {
ecmaVersion: "latest",
sourceType: "module",
}) as Node;

walk(fileAST, {
enter(_node) {
if (_node.type !== "CallExpression" || (_node as CallExpression).callee.type !== 'Identifier') { return }
const node = _node as CallExpression & { start: number, end: number};
const name = 'name' in node.callee && node.callee.name
if (
_node.type !== "CallExpression" ||
(_node as CallExpression).callee.type !== "Identifier"
) {
return;
}
const node = _node as CallExpression & { start: number; end: number };
const name = "name" in node.callee && node.callee.name;

if (name === "defineRouteMeta") {
const metaString = js.code.slice(node.start, node.end)
const metaString = js.code.slice(node.start, node.end);
try {
routeMeta = JSON.parse(runInNewContext(metaString.replace('defineRouteMeta', 'JSON.stringify'), {}))
routeMeta = JSON.parse(
runInNewContext(
metaString.replace("defineRouteMeta", "JSON.stringify"),
{}
)
);
} catch {
throw new Error("[nitro] Error parsing route meta. They should be JSON-serializable")
throw new Error(
"[nitro] Error parsing route meta. They should be JSON-serializable"
);
}
}
}
})
},
});

return routeMeta
}
return routeMeta;
}
2 changes: 1 addition & 1 deletion src/types/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export interface NitroEventHandler {
* Router method matcher
*/
method?: string;

/**
* Route meta
*/
Expand Down

0 comments on commit 4b6f151

Please sign in to comment.