file-routing-expressjs
Version:
A dependency-free, flexible, system-based file routing for Express.
72 lines • 2.01 kB
JavaScript
import fs from "fs";
import { JAVASCRIPT_FILES_EXTENSION_REGEX } from "../constants/regex";
import { EXPRESS_METHODS_ARRAY } from "../constants";
import { resolveTarget } from "./resolvers";
const VALID_OUTPUT_TYPES = ["tree", "table", "json"];
function isFunction(value) {
return typeof value === "function";
}
function isRequestHandler(handler) {
return isFunction(handler);
}
function isErrorHandler(handler) {
return isFunction(handler) && handler.length === 4;
}
function filenameIsJSorTS(filename) {
return !!filename.match(JAVASCRIPT_FILES_EXTENSION_REGEX);
}
function pathExistsAndIsFile(path) {
return fs.existsSync(path) && fs.statSync(path).isFile();
}
function fileExistsAndIsJSorTS(filePath) {
return !!filePath.match(JAVASCRIPT_FILES_EXTENSION_REGEX) && pathExistsAndIsFile(filePath);
}
function methodIsExpressMethod(method) {
return EXPRESS_METHODS_ARRAY.includes(method);
}
function isDefined(value) {
return value !== null && value !== void 0;
}
function isNotDefined(value) {
return value === null || value === void 0;
}
function isObject(value) {
return isDefined(value) && typeof value === "object";
}
function validateCLIList(value, allowed, name) {
if (!value) return [];
const list = value.split(",").map((v) => v.trim()).filter(Boolean);
for (const item of list) {
if (!allowed.includes(item)) {
throw new Error(`Invalid ${name} value: ${item}`);
}
}
return list;
}
function normalizeCLIOptions(options) {
var _a;
const output = (_a = options.output) != null ? _a : "tree";
const target = resolveTarget(options.target);
if (!VALID_OUTPUT_TYPES.includes(output)) {
throw new Error(`Invalid output: ${output}`);
}
return {
target,
output
};
}
export {
fileExistsAndIsJSorTS,
filenameIsJSorTS,
isDefined,
isErrorHandler,
isFunction,
isNotDefined,
isObject,
isRequestHandler,
methodIsExpressMethod,
normalizeCLIOptions,
pathExistsAndIsFile,
validateCLIList
};
//# sourceMappingURL=validators.mjs.map