@mojaloop/ml-schema-transformer-lib
Version:
Shared component for ML schemas translation
219 lines (214 loc) • 7.81 kB
JavaScript
;
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/lib/transforms/extensions.ts
var extensions_exports = {};
__export(extensions_exports, {
applyRollUpUnmappedAsExtensions: () => applyRollUpUnmappedAsExtensions,
applyUnrollExtensions: () => applyUnrollExtensions
});
module.exports = __toCommonJS(extensions_exports);
// src/lib/utils/index.ts
var ilpPacket = __toESM(require("ilp-packet"));
// src/types/index.ts
var logLevelsMap = {
error: "error",
warn: "warn",
info: "info",
verbose: "verbose",
debug: "debug",
silly: "silly",
audit: "audit",
trace: "trace",
perf: "perf"
};
var logLevelValues = Object.values(logLevelsMap);
// src/lib/utils/index.ts
var idGenerator = require("@mojaloop/central-services-shared").Util.id;
var { CreateFSPIOPErrorFromErrorCode } = require("@mojaloop/central-services-error-handling");
var setProp = (obj, path, value) => {
const pathParts = path.split(".");
let current = obj;
for (let i = 0; i < pathParts.length - 1; i++) {
const part = pathParts[i];
if (!current[part]) {
current[part] = {};
}
current = current[part];
}
current[pathParts[pathParts.length - 1]] = value;
};
var getProp = (obj, path) => {
const pathParts = path.split(".");
let current = obj;
for (const part of pathParts) {
if (typeof current === "object" && current !== null && part in current) {
current = current[part];
} else {
return void 0;
}
}
return current;
};
var hasProp = (obj, path) => {
const pathParts = path.split(".");
let current = obj;
for (const part of pathParts) {
if (typeof current === "object" && current !== null && part in current) {
current = current[part];
} else {
return false;
}
}
return true;
};
var deepMerge = (target, source) => {
for (const key in source) {
if (source[key] instanceof Object && key in target) {
Object.assign(source[key], deepMerge(target[key], source[key]));
}
}
Object.assign(target, source);
return target;
};
var unrollExtensions = (extensions) => {
const unrolled = {};
for (const { key, value } of extensions) {
setProp(unrolled, key, value);
}
return unrolled;
};
var rollUpUnmappedAsExtensions = (source, mapping) => {
const extensions = [];
const mappingObj = mapping = typeof mapping === "string" ? JSON.parse(mapping) : mapping;
const mappingValues = extractValues(mappingObj).map((value) => String(value)).filter((value) => value.startsWith("body.") || value.startsWith("$context.")).map((value) => value.replace("body.", "")).map((value) => value.replace(/\$context\.[a-zA-Z0-9]+./, ""));
const sourcePaths = getObjectPaths(source.body);
for (const path of sourcePaths) {
if (!mappingValues.includes(path)) {
const value = getProp(source.body, path);
extensions.push({ key: path, value });
}
}
return extensions;
};
var extractValues = (obj) => {
const values = [];
function recurse(current) {
if (Array.isArray(current)) {
current.forEach((item) => recurse(item));
} else if (typeof current === "object" && current !== null) {
Object.values(current).forEach((value) => recurse(value));
} else {
values.push(current);
}
}
recurse(obj);
return values;
};
var getObjectPaths = (obj, prefix = "") => {
let paths = [];
for (const key in obj) {
if (obj.hasOwnProperty(key)) {
const path = prefix ? `${prefix}.${key}` : key;
if (typeof obj[key] === "object" && obj[key] !== null && !Array.isArray(obj[key])) {
paths = paths.concat(getObjectPaths(obj[key], path));
} else {
paths.push(path);
}
}
}
return paths;
};
var deduplicateObjectsArray = (arr, uniqueKey) => {
const seen = /* @__PURE__ */ new Set();
return arr.reduce((acc, obj) => {
if (!seen.has(obj[uniqueKey])) {
seen.add(obj[uniqueKey]);
acc.push(obj);
}
return acc;
}, []);
};
// src/lib/transforms/extensions.ts
var applyUnrollExtensions = (params) => {
const { source, target, options, logger } = params;
let extensionListProperty;
if (options.applyUnrollExtensions?.extensionListProperty) {
extensionListProperty = options.applyUnrollExtensions?.extensionListProperty;
} else if (source.body?.errorInformation) {
extensionListProperty = "body.errorInformation.extensionList";
} else {
extensionListProperty = "body.extensionList";
}
const extension = getProp(source, `${extensionListProperty}.extension`);
if (!options.unrollExtensions || !extension) {
logger.debug("Skipping unrollExtensions", { source, target, options });
return target;
}
const unrolled = unrollExtensions(extension);
logger.debug("Unrolled extensions", { source, unrolled });
target.body = deepMerge(target.body, unrolled);
return target;
};
var applyRollUpUnmappedAsExtensions = (params) => {
const { source, target, options: { mapping }, options, logger } = params;
if (!options.rollUpUnmappedAsExtensions) {
logger.debug("Skipping rollUpUnmappedAsExtensions", { source, target, mapping, options });
return target;
}
const extensions = rollUpUnmappedAsExtensions(source, mapping);
logger.debug("Rolled up unmapped properties into extensions", { source, mapping, extensions });
if (extensions.length === 0) {
logger.debug("No unmapped properties to roll up", { source, mapping });
return target;
}
let extensionListProperty;
if (options.applyRollUpUnmappedAsExtensions?.extensionListProperty) {
extensionListProperty = options.applyRollUpUnmappedAsExtensions?.extensionListProperty;
} else if (target.body?.errorInformation) {
extensionListProperty = "body.errorInformation.extensionList";
} else {
extensionListProperty = "body.extensionList";
}
const extensionProperty = `${extensionListProperty}.extension`;
if (!hasProp(target, extensionProperty)) {
setProp(target, extensionProperty, []);
}
let allExtensions = getProp(target, extensionProperty);
allExtensions.push(...extensions);
allExtensions = deduplicateObjectsArray(allExtensions, "key");
setProp(target, extensionProperty, allExtensions);
logger.debug("Rolled up unmapped properties into extensions", { source, target, mapping, extensions });
return target;
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
applyRollUpUnmappedAsExtensions,
applyUnrollExtensions
});
//# sourceMappingURL=extensions.js.map