fume-fhir-converter
Version:
FHIR-Utilized Mapping Engine - Community
103 lines • 3.61 kB
JavaScript
;
/**
* © Copyright Outburn Ltd. 2022-2024 All Rights Reserved
* Project name: FUME-COMMUNITY
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.getFhirClient = exports.setFhirClient = exports.FhirClient = void 0;
const tslib_1 = require("tslib");
const axios_1 = tslib_1.__importDefault(require("axios"));
const config_1 = tslib_1.__importDefault(require("../../config"));
const logger_1 = require("../logger");
class FhirClient {
contentType;
fhirServer;
serverConfig;
isStateless;
constructor() {
this.serverConfig = config_1.default.getServerConfig();
this.contentType = `application/fhir+json;fhirVersion=${config_1.default.getFhirVersionMinor()}`;
this.isStateless = this.serverConfig.SERVER_STATELESS;
this.init();
}
authHeader(authType, username, password) {
if (authType === 'BASIC' && username && password && username > '' && password > '') {
const buff = Buffer.from(`${username}:${password}`, 'utf-8');
return `Basic ${buff.toString('base64')}`;
}
else {
return undefined;
}
}
;
init() {
if (!this.isStateless) {
const { FHIR_SERVER_BASE, FHIR_SERVER_TIMEOUT, FHIR_SERVER_AUTH_TYPE, FHIR_SERVER_UN, FHIR_SERVER_PW } = this.serverConfig;
const server = axios_1.default.create({
baseURL: FHIR_SERVER_BASE,
timeout: FHIR_SERVER_TIMEOUT,
headers: {
'Content-Type': this.contentType,
Accept: this.contentType,
Authorization: this.authHeader(FHIR_SERVER_AUTH_TYPE, FHIR_SERVER_UN, FHIR_SERVER_PW)
}
});
(0, logger_1.getLogger)().info(`Using FHIR server: ${FHIR_SERVER_BASE}`);
this.fhirServer = server;
}
}
checkStateless() {
if (this.isStateless) {
(0, logger_1.getLogger)().warn('Server is stateless, not performing the operation');
return true;
}
return false;
}
async read(url) {
if (this.checkStateless()) {
return;
}
const response = await this.fhirServer.get(`${url}`);
return response.data;
}
;
async search(query, params) {
if (this.checkStateless()) {
return;
}
const { SEARCH_BUNDLE_PAGE_SIZE } = this.serverConfig;
let queryConfigParams = { _count: SEARCH_BUNDLE_PAGE_SIZE };
if (params) {
queryConfigParams = { ...queryConfigParams, ...params };
}
;
(0, logger_1.getLogger)().info(`Performing search, page size: ${queryConfigParams._count}`);
const response = await this.fhirServer.get(`/${query}`, { params: queryConfigParams });
return response.data;
}
;
async create(resource, resourceType) {
throw new Error('Method not implemented.');
}
async update(resourceType, resourceId, resource) {
throw new Error('Method not implemented.');
}
async simpleDelete(resourceType, resourceId) {
throw new Error('Method not implemented.');
}
}
exports.FhirClient = FhirClient;
/** Global fhir client */
let fhirClient;
const setFhirClient = (client) => {
fhirClient = client;
};
exports.setFhirClient = setFhirClient;
const getFhirClient = () => {
if (!fhirClient) {
throw new Error('FHIR client not initialized');
}
return fhirClient;
};
exports.getFhirClient = getFhirClient;
//# sourceMappingURL=client.js.map