@omnia/tooling-composers
Version:
Provide tooling to work with manifest things.
149 lines (148 loc) • 5.5 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Utils = void 0;
//import { LoadByUrlMatchingRule, DomMatchingRule, ClientResolvableLoadRule, FeatureActiveRule, LoadIfManifestLoaded } from "./models";
class Utils {
static getPropertyPathInExpression(exp) {
const result = new Set();
const targetObject = {};
const handler = {
get(target, key) {
if (target[key] === undefined) {
target[key] = new Proxy({}, handler);
}
return target[key];
},
};
const proxiedObject = new Proxy(targetObject, handler);
exp(proxiedObject);
const recursiveExtractPropertyPath = (parentProperty, obj) => {
Object.keys(obj).forEach(propertyName => {
if (Object.keys(obj[propertyName]).length === 0) {
result.add((parentProperty + "." + propertyName).substring(1));
}
else {
recursiveExtractPropertyPath(parentProperty + "." + propertyName, obj[propertyName]);
}
});
};
recursiveExtractPropertyPath("", targetObject);
return Array.from(result);
}
static getPropertyPathAndConfigurationInExpression(exp) {
let apiPath, configuration = null;
const targetObject = {};
const handler = {
get(target, property) {
if (target[property] === undefined) {
if (property === "configure") {
target[property] = new Proxy(function (config) {
configuration = config;
}, handler);
}
else {
target[property] = new Proxy({}, handler);
}
}
return target[property];
},
};
const proxiedObject = new Proxy(targetObject, handler);
exp(proxiedObject);
const recursiveExtractPropertyPath = (parentProperty, obj) => {
Object.keys(obj).forEach(propertyName => {
if (Object.keys(obj[propertyName]).length === 0) {
apiPath = (parentProperty + "." + propertyName).substring(1);
}
else {
recursiveExtractPropertyPath(parentProperty + "." + propertyName, obj[propertyName]);
}
});
};
recursiveExtractPropertyPath("", targetObject);
if (apiPath.endsWith(".configure") && configuration) {
apiPath = apiPath.substring(0, apiPath.lastIndexOf(".configure"));
}
return [apiPath, configuration];
}
}
exports.Utils = Utils;
Utils.ensureValidManifestId = (id, errorMsg) => {
if (!id) {
throw new Error(errorMsg ? errorMsg : "Invalid manifest id, can't be null/empty or undefined");
}
if (!Utils.isValidGuid(id)) {
console.dir(id);
throw new Error("The manifest id must be a valid guid");
}
return id.toLowerCase();
};
Utils.ensureValidServiceId = (id, errorMsg) => {
if (!id) {
throw new Error(errorMsg ? errorMsg : "Invalid service id, can't be null/empty or undefined");
}
return id.toLowerCase();
};
Utils.ensureValidElementName = (name, errorMsg) => {
if (!name) {
throw new Error(errorMsg ? errorMsg : "Invalid element name, can't be null/empty or undefined");
}
return name.toUpperCase();
};
Utils.validateSupportedLoadRuleTypes = (rules) => {
if (rules) {
for (let rule of rules) {
Utils.validateSupportedLoadRuleType(rule);
}
}
};
Utils.validateSupportedLoadRuleType = (rule) => {
if (!rule ||
!rule.rule) {
throw new Error("Can't add rule null/empty/undefined");
}
let testIfValidLoadByUrl = rule.rule;
let testIfValidLoadByElement = rule.rule;
let testIfValidFeatureEnabled = rule.rule;
let testIfValidLicense = rule.rule;
let testIfValidManifestLoaded = rule.rule;
let testIfValidUser = rule.rule;
let testIfValidClientRuntime = rule.rule;
let testIfValidBackendRuntime = rule.rule;
if (testIfValidLoadByUrl.regEx ||
testIfValidLoadByUrl.startsWith) {
}
else if (testIfValidLoadByElement.cssSelector) {
}
else if (testIfValidFeatureEnabled.featureId) {
}
else if (testIfValidUser.userPropertyName) {
}
else if (testIfValidLicense.licenseId) {
}
else if (testIfValidManifestLoaded.omniaServiceId &&
testIfValidManifestLoaded.resourceId) {
}
else if (testIfValidClientRuntime.clientRuntimeTypes) {
}
else if (testIfValidBackendRuntime.backendRuntimeTypes) {
}
else {
throw new Error("ICombinableLoadRule is not matching any of the (OOTB) supported load rules, make sure all mandatory properties has valid values for rule: " + JSON.stringify(rule));
}
};
Utils.isValidGuid = (value) => {
if (!Utils.isNullOrEmpty(value)) {
var result = value.match(/^[{]?[0-9a-fA-F]{8}[-]?([0-9a-fA-F]{4}[-]?){3}[0-9a-fA-F]{12}[}]?$/);
return result !== null && result.length > 0;
}
return false;
};
Utils.isNull = (obj) => {
if (obj === 0 || obj === false)
return false;
return (!obj || typeof obj === "undefined" || obj === null);
};
Utils.isNullOrEmpty = (obj) => {
return Utils.isNull(obj) || obj === "";
};