2023-01-06 09:30:03 +01:00
|
|
|
const express = require("express");
|
2023-04-15 17:51:52 +02:00
|
|
|
const path = require("path");
|
|
|
|
const { traverseDirectory } = require("lambert-server");
|
2023-01-06 09:30:03 +01:00
|
|
|
const RouteUtility = require("../../dist/api/util/handlers/route.js");
|
|
|
|
|
2023-04-15 17:51:52 +02:00
|
|
|
const methods = ["get", "post", "put", "delete", "patch"];
|
2023-01-06 09:30:03 +01:00
|
|
|
const routes = new Map();
|
|
|
|
let currentFile = "";
|
2023-04-15 17:51:52 +02:00
|
|
|
let currentPath = "";
|
2023-01-06 09:30:03 +01:00
|
|
|
|
2023-04-15 17:51:52 +02:00
|
|
|
/*
|
|
|
|
For some reason, if a route exports multiple functions, it won't be registered here!
|
|
|
|
If someone could fix that I'd really appreciate it, but for now just, don't do that :p
|
|
|
|
*/
|
2023-01-06 09:30:03 +01:00
|
|
|
|
2023-04-15 17:51:52 +02:00
|
|
|
const proxy = (file, method, prefix, path, ...args) => {
|
|
|
|
const opts = args.find((x) => x?.prototype?.OPTS_MARKER == true);
|
|
|
|
if (!opts)
|
|
|
|
return console.error(
|
|
|
|
`${file} has route without route() description middleware`,
|
|
|
|
);
|
2023-01-06 09:30:03 +01:00
|
|
|
|
2023-04-15 17:51:52 +02:00
|
|
|
console.log(prefix + path + " - " + method);
|
|
|
|
opts.file = file.replace("/dist/", "/src/").replace(".js", ".ts");
|
|
|
|
routes.set(prefix + path + "|" + method, opts());
|
|
|
|
};
|
2023-01-06 09:30:03 +01:00
|
|
|
|
2023-04-15 17:51:52 +02:00
|
|
|
express.Router = () => {
|
|
|
|
return Object.fromEntries(
|
|
|
|
methods.map((method) => [
|
|
|
|
method,
|
|
|
|
proxy.bind(null, currentFile, method, currentPath),
|
|
|
|
]),
|
|
|
|
);
|
|
|
|
};
|
2023-01-06 09:30:03 +01:00
|
|
|
|
2023-04-15 17:51:52 +02:00
|
|
|
RouteUtility.route = (opts) => {
|
|
|
|
const func = function () {
|
|
|
|
return opts;
|
|
|
|
};
|
|
|
|
func.prototype.OPTS_MARKER = true;
|
|
|
|
return func;
|
2023-01-06 09:30:03 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = function getRouteDescriptions() {
|
|
|
|
const root = path.join(__dirname, "..", "..", "dist", "api", "routes", "/");
|
|
|
|
traverseDirectory({ dirname: root, recursive: true }, (file) => {
|
|
|
|
currentFile = file;
|
2023-04-15 17:51:52 +02:00
|
|
|
|
|
|
|
currentPath = file.replace(root.slice(0, -1), "");
|
|
|
|
currentPath = currentPath.split(".").slice(0, -1).join("."); // trancate .js/.ts file extension of path
|
|
|
|
currentPath = currentPath.replaceAll("#", ":").replaceAll("\\", "/"); // replace # with : for path parameters and windows paths with slashes
|
|
|
|
if (currentPath.endsWith("/index"))
|
|
|
|
currentPath = currentPath.slice(0, "/index".length * -1); // delete index from path
|
2023-01-06 09:30:03 +01:00
|
|
|
|
|
|
|
try {
|
|
|
|
require(file);
|
2023-04-15 17:51:52 +02:00
|
|
|
} catch (e) {
|
|
|
|
console.error(e);
|
2023-01-06 09:30:03 +01:00
|
|
|
}
|
|
|
|
});
|
2023-04-15 17:51:52 +02:00
|
|
|
|
2023-01-06 09:30:03 +01:00
|
|
|
return routes;
|
|
|
|
};
|