fastlane
Version:
Fastlane is a fast and flexible API framework for Node.js. It automatically creates Express routes from your project's file structure, making it easy to build APIs quickly and efficiently.
28 lines • 1.25 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.attachRoutes = attachRoutes;
const express_1 = require("express");
const handler_1 = require("./handler");
const fs_1 = require("fs");
const routeFile = /route\.(ts|js)$/;
const methodNames = ["GET", "POST", "PUT", "PATCH", "DELETE"];
function attachRoutes(dir, importFn = (path) => import(path)) {
const router = (0, express_1.Router)();
fs_1.promises.readdir(dir, { recursive: true }).then(async (paths) => {
for (const path of paths) {
if (path.match(routeFile)) {
const pathUrl = `/${path.replace(routeFile, "").replace(/\/$/, "")}`;
const methodImport = await importFn(`${dir}/${path}`);
const methods = methodImport?.default || methodImport;
for (const methodName of methodNames) {
if (typeof methods[methodName] === "function") {
console.log(`Attaching ${methodName} ${pathUrl}`);
router[methodName.toLowerCase()](pathUrl, (0, handler_1.methodHandler)(methods[methodName]));
}
}
}
}
});
return router;
}
//# sourceMappingURL=attach.js.map