UNPKG

@omnia/tooling-composers

Version:

Provide tooling to work with manifest things.

117 lines (116 loc) 4.71 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.CombinableRuleComposer = void 0; //import { CombinableLoadRule, LoadByUrlMatchingRule, DomMatchingRule, FeatureActiveRule, ClientResolvableLoadRule, LoadIfManifestLoaded } from "./models"; const Enums_1 = require("./models/Enums"); const Utils_1 = require("./Utils"); let UserTypeObject = {}; class CombinableRuleComposer { constructor(newRuleCb, composerForDone) { this.newRuleCb = newRuleCb; this.composerForDone = composerForDone; this.rules = []; this.selectedLogicalOperator = null; this.done = () => { this.newRuleCb(this.rules); return this.composerForDone; }; this.loadByUrlMatching = (urlRule) => { if (!urlRule) { throw new Error("Can't add load by url rule null/empty/undefined"); } return this.addRule(urlRule); }; this.loadIfFeatureActivated = (featureRule) => { if (!featureRule) { throw new Error("Can't add feature rule null/empty/undefined"); } return this.addRule(featureRule); }; this.loadWhenHavingLicense = (licenseRule) => { if (!licenseRule) { throw new Error("Can't add license rule null/empty/undefined"); } return this.addRule(licenseRule); }; this.loadByDomMatching = (domRule) => { if (!domRule) { throw new Error("Can't add load by element rule null/empty/undefined"); } return this.addRule(domRule); }; this.loadIfManifestLoaded = (manifestLoadedRule) => { if (!manifestLoadedRule) { throw new Error("Can't add manifest loaded rule null/empty/undefined"); } Utils_1.Utils.ensureValidManifestId(manifestLoadedRule.resourceId); return this.addRule(manifestLoadedRule); }; this.loadByClientRuntimeMatching = (clientRuntimes) => { return this.addRule({ clientRuntimeTypes: Array.isArray(clientRuntimes) ? clientRuntimes : [clientRuntimes] }); }; this.loadByBackendRuntimeMatching = (backendRuntimes) => { return this.addRule({ backendRuntimeTypes: Array.isArray(backendRuntimes) ? backendRuntimes : [backendRuntimes] }); }; this.loadByUserMatching = (...args) => { // we need to populate dynamic arguments to support overload methods like //.loadByUserMatching(UserPropertyBag, "primary", "") //.loadByUserMatching("preferredLanguage", LanguageTags.AfZa) let rule; let propertyBagObject = args[0]; if (args.length === 3) { // check if is user property bag object if (propertyBagObject.omniaServiceId && propertyBagObject.uniqueModelName) { rule = { userPropertyBagModel: args[0], userPropertyName: args[1], value: args[2] }; } else { rule = { userPropertyName: args[1], value: args[2] }; } } else { rule = { userPropertyName: args[0], value: args[1] }; } return this.addRule(rule); }; this.and = () => { this.selectedLogicalOperator = Enums_1.RuleLogicalOperator.And; return this; }; this.or = () => { this.selectedLogicalOperator = Enums_1.RuleLogicalOperator.Or; return this; }; if (!newRuleCb) { throw new Error("Unexpected, CombinableRuleComposer needs a new rule callback, one was not provided"); } } addRule(rule) { if (this.selectedLogicalOperator == null && this.rules.length > 0) { throw new Error("Can't add additional rule without first selecting logical operator."); } if (this.selectedLogicalOperator == null) { //Default to and for first rule this.selectedLogicalOperator = Enums_1.RuleLogicalOperator.And; } this.rules.push({ logicalOperator: this.selectedLogicalOperator, rule: rule }); //Reset this.selectedLogicalOperator = null; this.newRuleCb(this.rules); return this; } } exports.CombinableRuleComposer = CombinableRuleComposer;