Skip to content

Commit

Permalink
feat: added $capabilities() function (#62)
Browse files Browse the repository at this point in the history
  • Loading branch information
mechanik-daniel authored Apr 14, 2024
1 parent 91a52cc commit 977600a
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 3 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 27 additions & 0 deletions src/helpers/fhirFunctions/capabilities.ts
Original file line number Diff line number Diff line change
@@ -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<string, any | any[]> = {};

export const capabilities = async (): Promise<Record<string, any> | 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;
};
4 changes: 3 additions & 1 deletion src/helpers/fhirFunctions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -21,5 +22,6 @@ export default {
translateCoding,
translateCode,
fhirVersionToMinor,
reference
reference,
capabilities
};
1 change: 1 addition & 0 deletions src/helpers/jsonataFunctions/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 977600a

Please sign in to comment.