From e15c48a50f2787115fd4787821ad396cfe83b0c2 Mon Sep 17 00:00:00 2001 From: Daniel Mechanik Date: Sun, 14 Apr 2024 14:09:07 +0300 Subject: [PATCH] feat: added $capabilities() function --- package-lock.json | 4 ++-- src/helpers/fhirFunctions/capabilities.ts | 27 +++++++++++++++++++++++ src/helpers/fhirFunctions/index.ts | 4 +++- src/helpers/jsonataFunctions/transform.ts | 1 + 4 files changed, 33 insertions(+), 3 deletions(-) create mode 100644 src/helpers/fhirFunctions/capabilities.ts diff --git a/package-lock.json b/package-lock.json index 5ba7510..a6dfb62 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "fume-fhir-converter", - "version": "2.0.11", + "version": "2.0.12", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "fume-fhir-converter", - "version": "2.0.11", + "version": "2.0.12", "license": "AGPL-3.0", "dependencies": { "axios": "^1.6.7", diff --git a/src/helpers/fhirFunctions/capabilities.ts b/src/helpers/fhirFunctions/capabilities.ts new file mode 100644 index 0000000..5920a0e --- /dev/null +++ b/src/helpers/fhirFunctions/capabilities.ts @@ -0,0 +1,27 @@ +/** + * © Copyright Outburn Ltd. 2022-2024 All Rights Reserved + * Project name: FUME-COMMUNITY + */ +import { getFhirClient } from '../fhirServer'; +import thrower from '../thrower'; + +// cached copy of the capability statement resource +let capabilityStatement: Record = {}; + +export const capabilities = async (): Promise | undefined> => { + // check if capability statement cached copy is empty + if (Object.keys(capabilityStatement).length === 0) { + // try to fetch from server + try { + capabilityStatement = await getFhirClient().read('metadata'); + } catch (error) { + return thrower.throwRuntimeError(`Failed to fetch CapabilityStatement from FHIR server. ${JSON.stringify(error)}`); + } + }; + // check that the object is actually a capability statement + if (!capabilityStatement?.resourceType || typeof capabilityStatement.resourceType !== 'string' || capabilityStatement.resourceType !== 'CapabilityStatement') { + return thrower.throwRuntimeError('Invalid response from FHIR server: The \'/metadata\' endpoint did not return a CapabilityStatement resource'); + }; + // return the cached capability statement + return capabilityStatement; +}; diff --git a/src/helpers/fhirFunctions/index.ts b/src/helpers/fhirFunctions/index.ts index 5a672eb..dbbb39d 100644 --- a/src/helpers/fhirFunctions/index.ts +++ b/src/helpers/fhirFunctions/index.ts @@ -2,6 +2,7 @@ * © Copyright Outburn Ltd. 2022-2024 All Rights Reserved * Project name: FUME-COMMUNITY */ +import { capabilities } from './capabilities'; import { fhirVersionToMinor } from './fhirVersionToMinor'; import { literal } from './literal'; import { reference } from './reference'; @@ -21,5 +22,6 @@ export default { translateCoding, translateCode, fhirVersionToMinor, - reference + reference, + capabilities }; diff --git a/src/helpers/jsonataFunctions/transform.ts b/src/helpers/jsonataFunctions/transform.ts index d13bed2..638dfc8 100644 --- a/src/helpers/jsonataFunctions/transform.ts +++ b/src/helpers/jsonataFunctions/transform.ts @@ -90,6 +90,7 @@ export const transform = async (input, expression: string, extraBindings: Record bindings.v2parse = v2.v2parse; bindings.v2json = v2.v2json; bindings.isNumeric = stringFuncs.isNumeric; + bindings.capabilities = fhirFuncs.capabilities; const { aliases } = getCache(); // these are debug functions, should be removed in production versions