UNPKG

fume-fhir-converter

Version:

FHIR-Utilized Mapping Engine - Community

87 lines 3.4 kB
"use strict"; /** * © Copyright Outburn Ltd. 2022-2024 All Rights Reserved * Project name: FUME-COMMUNITY */ Object.defineProperty(exports, "__esModule", { value: true }); const cache_1 = require("../helpers/cache"); const inputConverters_1 = require("../helpers/inputConverters"); const jsonataFunctions_1 = require("../helpers/jsonataFunctions"); const logger_1 = require("../helpers/logger"); const toJsonataString_1 = require("../helpers/parser/toJsonataString"); const get = async (req, res) => { const logger = (0, logger_1.getLogger)(); try { // get mapping id from route const mappingId = req.params.mappingId; // get mapping object from cache const mappingObj = (0, cache_1.getCache)().compiledMappings.get(mappingId); if (mappingObj) { // get expression from mapping object const mapping = mappingObj.expression; res.set('Content-Type', 'application/vnd.outburn.fume'); res.status(200).send(mapping); } else { const message = `Mapping '${mappingId}' could not be found`; logger.error(message); res.status(404).json({ message }); } } catch (error) { logger.error({ error }); res.status(500).json({ message: error }); } }; const transform = async (req, res) => { const logger = (0, logger_1.getLogger)(); try { const mappingId = req.params.mappingId; const mappingFromCache = (0, cache_1.getCache)().compiledMappings.get(mappingId); const contentType = req.get('Content-Type'); const inputJson = await (0, inputConverters_1.convertInputToJson)(req.body, contentType); if (mappingFromCache) { const result = await mappingFromCache.function(inputJson); res.set('Content-Type', 'application/json'); res.status(200).json(result); } else { logger.error(`Mapping '${mappingId}' not found!`); res.status(404).json({ message: 'not found' }); } } catch (error) { logger.error({ error }); res.status(500).json({ message: error }); } }; const operation = async (req, res) => { const logger = (0, logger_1.getLogger)(); const operationName = req.params?.operation; if (operationName === '$transpile') { try { const mappingId = req.params.mappingId; const { compiledMappings } = (0, cache_1.getCache)(); const mappingFromCache = compiledMappings.get(mappingId); const expressionString = mappingFromCache.expression; const tranpiled = await (0, toJsonataString_1.toJsonataString)(expressionString); if (tranpiled) { const prettyExpr = await (0, jsonataFunctions_1.pretty)(tranpiled); res.set('Content-Type', 'application/vnd.outburn.fume'); res.status(200).send(prettyExpr); } else { res.status(404).json({ message: `Mapping '${mappingId}' not found` }); } } catch (e) { logger.error(e); res.status(500).json({ message: e }); } } else { res.status(500).json({ message: `Unknown operation '${operationName}'` }); } }; exports.default = { get, transform, operation }; //# sourceMappingURL=mapping.js.map