fume-fhir-converter
Version:
FHIR-Utilized Mapping Engine - Community
98 lines • 4.07 kB
JavaScript
"use strict";
/**
* © Copyright Outburn Ltd. 2022-2024 All Rights Reserved
* Project name: FUME-COMMUNITY
*/
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const package_json_1 = require("../../package.json");
const config_1 = tslib_1.__importDefault(require("../config"));
const cache_1 = require("../helpers/cache");
const conformance_1 = require("../helpers/conformance");
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) => {
return res.status(200).json({
fume_version: `FUME Community v${package_json_1.version}`
});
};
const evaluate = async (req, res) => {
try {
const inputJson = await (0, inputConverters_1.convertInputToJson)(req.body.input, req.body.contentType);
const extraBindings = config_1.default.getBindings();
const response = await (0, jsonataFunctions_1.transform)(inputJson, req.body.fume, extraBindings);
return res.status(200).json(response);
}
catch (error) {
const data = {
__isFumeError: true,
message: error.message ?? '',
code: error.code ?? '',
name: error.name ?? '',
token: error.token ?? '',
cause: error.cause ?? '',
position: error.position ?? ''
};
(0, logger_1.getLogger)().error({ error });
return res.status(422).json(data);
}
};
const recache = async (req, res) => {
try {
const recacheSuccess = await (0, conformance_1.recacheFromServer)();
if (recacheSuccess) {
const { tables, compiledMappings } = (0, cache_1.getCache)();
const response = {
message: 'The following Tables & Mappings were loaded to cache',
tables: tables.getDict(),
mappings: compiledMappings.keys()
};
return res.status(200).json(response);
}
else {
console.error('Error loading cache');
return res.status(404).json({ message: 'error loading cache' });
}
}
catch (error) {
console.error('Failed to load cache', { error });
return res.status(404).json({ message: error });
}
};
const operation = async (req, res) => {
try {
const operationName = req.params?.operation;
if (operationName === '$transpile') {
const contentType = req.get('Content-Type');
if (typeof contentType === 'string' && (contentType.startsWith('text/plain') || contentType.startsWith('application/vnd.outburn.fume'))) {
const inputMapping = req.body;
const tranpiled = await (0, toJsonataString_1.toJsonataString)(inputMapping);
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(500).json({ message: 'Error parsing FUME expression' });
}
}
else {
res.status(415).json({ message: `Content-Type '${contentType}' is invalid for the $transpile operation. Please send a valid FUME expression and set Content-Type: 'application/vnd.outburn.fume'` });
}
}
else if (operationName === 'recache' || operationName === '$recache') {
res.redirect('/recache');
}
else {
res.status(500).json({ message: `Unknown operation '${operationName}'` });
}
}
catch (e) {
(0, logger_1.getLogger)().error(e);
res.status(500).json({ message: e });
}
};
exports.default = { get, evaluate, recache, operation };
//# sourceMappingURL=root.js.map