@xpresser/abolish
Version:
Using Abolish validator in Xpresserjs
111 lines (110 loc) • 3.78 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.IsAbolishXpresserError = void 0;
exports.dependsOn = dependsOn;
exports.run = run;
exports.CompileRouteRules = CompileRouteRules;
const plugin_config_1 = require("./plugin-config");
const abolish_1 = require("abolish");
const Compiler_1 = require("abolish/src/Compiler");
const xpresser_1 = require("xpresser");
let dollarSign;
let routes = [];
function dependsOn() {
return ["abolish"];
}
/**
* Xpresser Run Plugin Function
* @param config
* @param $
*/
function run(config, $) {
dollarSign = $;
/**
* Skip connecting to db when running native xpresser commands
*/
if ($.isNativeCliCommand())
return;
// Load abolish Extender
const { Abolish } = require("abolish");
if (plugin_config_1.pluginConfig.has("extendAbolish")) {
$.logDeprecated("1.11.1", "1.11.1", [
"Abolish Config: `extendAbolish` is now deprecated, please rename the function to `provideAbolish` instead"
]);
$.logErrorAndExit("Make changes and restart server to apply changes");
}
let providedAbolish = Abolish;
if (plugin_config_1.pluginConfig.has("provideAbolish")) {
providedAbolish = plugin_config_1.pluginConfig.data.provideAbolish();
}
$.engineData.set("getProvidedAbolish", () => providedAbolish);
// Load Processed Routes
$.on.bootServer((next) => {
routes = $.routerEngine.allProcessedRoutes();
return next();
});
}
/**
* ValidateRoutes
* Parse all rules for performance purposes.
* @constructor
* @param methods
* @param useCompiledRules
*/
function CompileRouteRules(methods, useCompiledRules = false) {
// New rules holder
let parsedRules = {};
for (let [method, rules] of Object.entries(methods)) {
parsedRules[method] = {};
/**
* Loop through rules, find controllers, and replace with a path.
*/
for (let rule in rules) {
if (!rules.hasOwnProperty(rule))
continue;
// Get and Check rule
const thisRule = rules[rule];
// if is controller
if (rule[0] !== "/" && rule.toLowerCase().includes("@"))
// use path of controller instead
rule = ControllerToPath(rule);
// Else use as it is.
parsedRules[method][rule] =
typeof thisRule === "function" || thisRule instanceof Compiler_1.AbolishCompiled
? thisRule
: useCompiledRules
? abolish_1.Abolish.compileObject(thisRule)
: (0, abolish_1.Schema)(thisRule);
}
}
// Return ParsedRules.
return parsedRules;
}
/**
* Get path using controller name.
* @param controller
* @constructor
*/
function ControllerToPath(controller) {
const $ = dollarSign;
// Add Controller to string if not exists.
if (!controller.toLowerCase().includes("controller@")) {
const c = controller.split("@");
c[0] = c[0] + "Controller";
controller = c.join("@");
}
const find = routes.filter((route) => route.controller === controller);
if (!find.length)
return $.logErrorAndExit(`Validation Rules: Controller {${controller}} controller found.`);
if (find.length > 1) {
return $.logErrorAndExit(`Validation Rules: Controller {${controller}} is paired to multiple paths, use exact path instead.`);
}
return find[0].path;
}
class IsAbolishXpresserError extends xpresser_1.InXpresserError {
constructor(abolishError) {
super(abolishError.message);
this.abolishError = abolishError;
}
}
exports.IsAbolishXpresserError = IsAbolishXpresserError;