file-routing-expressjs
Version:
A dependency-free, flexible, system-based file routing for Express.
79 lines • 3.4 kB
JavaScript
;
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var builders_exports = {};
__export(builders_exports, {
buildRoutePattern: () => buildRoutePattern,
buildRouteWithOneLeadingSlash: () => buildRouteWithOneLeadingSlash,
filenameToJSorTS: () => filenameToJSorTS
});
module.exports = __toCommonJS(builders_exports);
var import_path = __toESM(require("path"));
var import_validators = require("./validators");
var import_cleaners = require("./cleaners");
var import_regex = require("../constants/regex");
function buildRoutePattern(route, name, isParam, pattern, isFile = false) {
const fileIsIndex = isFile && name.trim().toLowerCase() === "index";
name = fileIsIndex ? "" : buildRouteWithOneLeadingSlash(name.trim());
route = buildRouteWithOneLeadingSlash(route);
if (pattern) {
let raw = pattern.toString().replace(import_regex.REGEX_LEFT_SLASH_REGEX, "").replace(import_regex.REGEX_RIGHT_SLASH_AND_FLAGS_REGEX, "");
pattern = `(${raw})`;
}
let endpoint = `${route}${name}`;
if (pattern) {
endpoint = isParam ? `${endpoint}${pattern}` : `${endpoint}/${pattern}`;
}
return buildRouteWithOneLeadingSlash(endpoint);
}
function buildRouteWithOneLeadingSlash(value) {
return `/${(0, import_cleaners.removeLeadingBackSlashesFromName)(value)}`;
}
function filenameToJSorTS(dir, filename) {
if ((0, import_validators.filenameIsJSorTS)(filename)) {
const file = import_path.default.join(dir, filename);
if ((0, import_validators.pathExistsAndIsFile)(file)) return file;
} else {
const jsFile = import_path.default.join(dir, `${filename}.js`);
const tsFile = import_path.default.join(dir, `${filename}.ts`);
if ((0, import_validators.pathExistsAndIsFile)(jsFile)) {
return jsFile;
}
if ((0, import_validators.pathExistsAndIsFile)(tsFile)) {
return tsFile;
}
}
return void 0;
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
buildRoutePattern,
buildRouteWithOneLeadingSlash,
filenameToJSorTS
});
//# sourceMappingURL=builders.js.map