fume-fhir-converter
Version:
FHIR-Utilized Mapping Engine - Community
66 lines • 3.1 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getStructureDefinition = exports.getStructureDefinitionPath = void 0;
const tslib_1 = require("tslib");
/**
* © Copyright Outburn Ltd. 2022-2024 All Rights Reserved
* Project name: FUME-COMMUNITY
*/
const fs_extra_1 = tslib_1.__importDefault(require("fs-extra"));
const fhirFunctions_1 = tslib_1.__importDefault(require("../fhirFunctions"));
// import { getLogger } from '../logger';
const thrower_1 = tslib_1.__importDefault(require("../thrower"));
const getStructureDefinitionPath = (definitionId, fhirVersion, fhirPackageIndex, logger) => {
// const serverConfig = config.getServerConfig();
const fhirVersionMinor = fhirFunctions_1.default.fhirVersionToMinor(fhirVersion);
// const fhirPackageIndex = getFhirPackageIndex();
const cached = fhirPackageIndex[fhirVersionMinor];
const indexed = cached.structureDefinitions.byId[definitionId] ??
cached.structureDefinitions.byUrl[definitionId] ??
cached.structureDefinitions.byName[definitionId];
if (!indexed) { // if not indexed, throw warning and return nothing
const msg = 'Definition "' + definitionId + '" not found!';
logger.warn(msg);
return undefined;
}
else if (Array.isArray(indexed)) {
const error = new Error(`Found multiple definition with the same id "${definitionId}"!`);
logger.error(error);
throw (error);
}
else {
return indexed;
}
};
exports.getStructureDefinitionPath = getStructureDefinitionPath;
const getStructureDefinition = (definitionId, fhirVersion, fhirPackageIndex, logger) => {
// First check if this is a BackboneElement referenced by contentRefernce (strats with #)
try {
if (definitionId.startsWith('#')) {
// Take the base type name
const elementId = definitionId.substring(1);
const baseType = elementId.split('.')[0];
const baseSnapshot = (0, exports.getStructureDefinition)(baseType, fhirVersion, fhirPackageIndex, logger);
const allElements = baseSnapshot?.snapshot?.element;
const backboneElements = allElements.filter((e) => e?.id === elementId || String(e?.id).startsWith(elementId + '.'));
return {
derivation: 'specialization',
differential: { element: backboneElements },
snapshot: { element: backboneElements }
};
}
;
const path = (0, exports.getStructureDefinitionPath)(definitionId, fhirVersion, fhirPackageIndex, logger);
if (path) {
const fullDef = JSON.parse(fs_extra_1.default.readFileSync(path).toString()); // load file
return fullDef;
}
else
return undefined;
}
catch (e) {
return thrower_1.default.throwParseError(`A Problem occured while getting the structure definition of '${definitionId}'. The error is: ${e}`);
}
};
exports.getStructureDefinition = getStructureDefinition;
//# sourceMappingURL=getStructureDefinition.js.map