fume-fhir-converter
Version:
FHIR-Utilized Mapping Engine - Community
138 lines • 7.17 kB
JavaScript
"use strict";
/**
* © Copyright Outburn Ltd. 2022-2024 All Rights Reserved
* Project name: FUME-COMMUNITY
*/
/* eslint-disable no-trailing-spaces */
/**
* © Copyright Outburn Ltd. 2022-2023 All Rights Reserved
* Project name: FUME
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.transform = void 0;
const tslib_1 = require("tslib");
const hl7_dictionary_1 = tslib_1.__importDefault(require("hl7-dictionary"));
const jsonata_1 = tslib_1.__importDefault(require("jsonata"));
const config_1 = tslib_1.__importDefault(require("../../config"));
const cache_1 = require("../cache");
const conformance = tslib_1.__importStar(require("../conformance"));
const conformance_1 = require("../conformance/conformance");
const fhirFunctions_1 = tslib_1.__importDefault(require("../fhirFunctions"));
const inputConverters_1 = require("../inputConverters");
const v2 = tslib_1.__importStar(require("../inputConverters/hl7v2"));
const logger_1 = require("../logger");
const objectFuncs = tslib_1.__importStar(require("../objectFunctions"));
const parser_1 = tslib_1.__importDefault(require("../parser"));
const removeComments_1 = require("../parser/removeComments");
const runtime_1 = tslib_1.__importDefault(require("../runtime"));
const stringFuncs = tslib_1.__importStar(require("../stringFunctions"));
const getStructureDefinition_1 = require("./getStructureDefinition");
const log_1 = require("./log");
const registerTable_1 = require("./registerTable");
const dev = process.env.NODE_ENV === 'dev';
const compiledExpression = async (expression) => {
const { compiledExpressions } = (0, cache_1.getCache)();
// takes a fume expression string and compiles it into a jsonata expression
// or returns the already compiled expression from cache
const key = stringFuncs.hashKey(expression); // turn expression string to a key
let compiled = compiledExpressions.get(key); // get from cache
if (compiled === undefined) { // not cached
(0, logger_1.getLogger)().info('expression not cached, compiling it...');
const parsedAsJsonataStr = await parser_1.default.toJsonataString(expression);
compiled = (0, jsonata_1.default)(parsedAsJsonataStr);
compiledExpressions.set(key, compiled);
}
;
return compiled;
};
const transform = async (input, expression, extraBindings = {}) => {
try {
(0, logger_1.getLogger)().info('Running transformation...');
const expr = await compiledExpression(expression);
let bindings = {};
// bind all mappings from cache
const { compiledMappings } = (0, cache_1.getCache)();
const mappingIds = Array.from(compiledMappings.keys());
if (mappingIds) {
mappingIds.forEach((mappingId) => {
const mapping = compiledMappings.get(mappingId);
expr.registerFunction(mappingId, mapping?.function, '<x-o?:x>');
});
}
// bind functions
bindings.__checkResourceId = runtime_1.default.checkResourceId;
bindings.__finalize = runtime_1.default.finalize;
bindings.__castToFhir = runtime_1.default.castToFhir;
bindings.__flashMerge = runtime_1.default.flashMerge;
bindings.wait = runtime_1.default.wait;
bindings.resourceId = fhirFunctions_1.default.resourceId;
bindings.initCap = stringFuncs.initCap;
bindings.isEmpty = objectFuncs.isEmpty;
bindings.matches = stringFuncs.matches;
bindings.stringify = JSON.stringify;
bindings.selectKeys = objectFuncs.selectKeys;
bindings.omitKeys = objectFuncs.omitKeys;
bindings.uuid = stringFuncs.uuid;
bindings.registerTable = registerTable_1.registerTable;
bindings.translateCode = fhirFunctions_1.default.translateCode;
bindings.translate = fhirFunctions_1.default.translateCode;
bindings.translateCoding = fhirFunctions_1.default.translateCoding;
bindings.search = fhirFunctions_1.default.search;
bindings.searchSingle = fhirFunctions_1.default.searchSingle;
bindings.literal = fhirFunctions_1.default.literal;
bindings.resolve = fhirFunctions_1.default.resolve;
bindings.warning = log_1.logWarn;
bindings.info = log_1.logInfo;
bindings.parseCsv = inputConverters_1.parseCsv;
bindings.v2parse = v2.v2parse;
bindings.v2json = v2.v2json;
bindings.capabilities = fhirFunctions_1.default.capabilities;
const { aliases } = (0, cache_1.getCache)();
// these are debug functions, should be removed in production versions
bindings.fhirCacheIndex = conformance.getFhirPackageIndex();
bindings.getTable = conformance.getTable;
bindings.v2dictionary = hl7_dictionary_1.default.definitions;
bindings.v2codeLookup = v2.v2codeLookup;
bindings.v2tableUrl = v2.v2tableUrl;
bindings.toJsonataString = parser_1.default.toJsonataString;
if (dev)
bindings.getMandatoriesOfElement = parser_1.default.getMandatoriesOfElement;
if (dev)
bindings.getMandatoriesOfStructure = parser_1.default.getMandatoriesOfStructure;
if (dev)
bindings.getElementDefinition = parser_1.default.getElementDefinition;
if (dev)
bindings.replaceColonsWithBrackets = parser_1.default.replaceColonsWithBrackets;
if (dev)
bindings.removeComments = removeComments_1.removeComments;
if (dev)
bindings.valueSetExpandDictionary = conformance_1.valueSetExpandDictionary;
if (dev)
bindings.codeSystemDictionary = conformance_1.codeSystemDictionary;
bindings.getCodeSystem = conformance.getCodeSystem;
bindings.getValueSet = conformance.getValueSet;
// end of debug functions
// bind all aliases from cache
bindings = {
...aliases.getDict(),
...bindings,
...extraBindings
};
expr.registerFunction('base64encode', stringFuncs.base64encode, '<s-:s>');
expr.registerFunction('base64decode', stringFuncs.base64decode, '<s-:s>');
expr.registerFunction('startsWith', stringFuncs.startsWith, '<s-s:b>');
expr.registerFunction('endsWith', stringFuncs.endsWith, '<s-s:b>');
expr.registerFunction('isNumeric', stringFuncs.isNumeric, '<j-:b>');
expr.registerFunction('reference', fhirFunctions_1.default.reference, '<o-:s>');
expr.registerFunction('getStructureDefinition', (defId) => (0, getStructureDefinition_1.getStructureDefinition)(defId, config_1.default.getFhirVersion(), conformance.getFhirPackageIndex(), (0, logger_1.getLogger)()), '<s-:o>');
expr.registerFunction('getSnapshot', async (defId) => await parser_1.default.getSnapshot(defId, config_1.default.getFhirVersion(), conformance.getFhirPackageIndex(), (0, logger_1.getLogger)()), '<s-:o>');
const res = await expr.evaluate(input, bindings);
return res;
}
catch (error) {
(0, logger_1.getLogger)().error(error);
throw error;
}
};
exports.transform = transform;
//# sourceMappingURL=transform.js.map