UNPKG

file-routing-expressjs

Version:

A dependency-free, flexible, system-based file routing for Express.

43 lines 1.54 kB
import path from "path"; import { filenameIsJSorTS, pathExistsAndIsFile } from "./validators"; import { removeLeadingBackSlashesFromName } from "./cleaners"; import { REGEX_RIGHT_SLASH_AND_FLAGS_REGEX, REGEX_LEFT_SLASH_REGEX } from "../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(REGEX_LEFT_SLASH_REGEX, "").replace(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 `/${removeLeadingBackSlashesFromName(value)}`; } function filenameToJSorTS(dir, filename) { if (filenameIsJSorTS(filename)) { const file = path.join(dir, filename); if (pathExistsAndIsFile(file)) return file; } else { const jsFile = path.join(dir, `${filename}.js`); const tsFile = path.join(dir, `${filename}.ts`); if (pathExistsAndIsFile(jsFile)) { return jsFile; } if (pathExistsAndIsFile(tsFile)) { return tsFile; } } return void 0; } export { buildRoutePattern, buildRouteWithOneLeadingSlash, filenameToJSorTS }; //# sourceMappingURL=builders.mjs.map