diff --git a/package.json b/package.json index b321278..1c20b30 100644 --- a/package.json +++ b/package.json @@ -32,13 +32,13 @@ "start": "tsdx watch", "build": "tsdx build", "test": "tsdx test", + "test-watch": "tsdx test --watch", "lint": "tsdx lint --fix", "prepare": "tsdx build", "docs": "typedoc --out docs src", "size": "size-limit", "analyze": "size-limit --why" }, - "peerDependencies": {}, "husky": { "hooks": { "//": "pre-commit tsdx lint" @@ -62,16 +62,17 @@ } ], "devDependencies": { - "@size-limit/preset-small-lib": "^4.6.0", - "husky": "^4.3.0", - "size-limit": "^4.6.0", + "@size-limit/preset-small-lib": "^4.10.2", + "husky": "^4.3.8", + "size-limit": "^4.10.2", "tsdx": "^0.14.1", - "tslib": "^2.0.1", - "typedoc": "^0.20.0-beta.32", - "typescript": "^4.0.3" + "tslib": "^2.2.0", + "typedoc": "^0.20.36", + "typescript": "^4.2.4" }, "dependencies": { - "@ahryman40k/ts-fhir-types": "^4.0.32", - "uuidv4": "^6.2.3" + "@ahryman40k/ts-fhir-types": "^4.0.34", + "axios": "^0.21.1", + "uuidv4": "^6.2.7" } } diff --git a/src/fhir-backend.ts b/src/fhir-backend.ts new file mode 100644 index 0000000..0901c09 --- /dev/null +++ b/src/fhir-backend.ts @@ -0,0 +1,38 @@ +import axios from 'axios' +import { R4 } from '@ahryman40k/ts-fhir-types'; + +/** + * + */ +export class FhirBackend { + + baseUrl: string = "http://hapi.fhir.org/baseR4" + questionnaireBundle: R4.IBundle = { + resourceType: "Bundle" + } + + constructor(baseUrl: string){ + if(baseUrl != '') + this.baseUrl = baseUrl; + }; + + async initialize() { + const response = await axios.get(this.baseUrl + '/Questionnaire') + this.questionnaireBundle = response.data + } + + getQuestionnaires(): R4.IBundle { + return this.questionnaireBundle; + } + + getTableOfContents(){ + return this.questionnaireBundle?.entry?.map(entry => {return {fullUrl: entry.fullUrl, id: entry.resource?.id}}) + } + + getQuestionnaire(id: string){ + return this.questionnaireBundle?.entry?.find(entry => { + return entry.resource?.id === id + })?.resource + } +} + diff --git a/src/index.ts b/src/index.ts index df3308c..02e387e 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,2 +1,3 @@ export { FhirJsonForm } from './ques-mapper'; export { FhirJsonResp } from './resp-mapper'; +export { FhirBackend } from './fhir-backend'; diff --git a/test/fhir-backend.test.ts b/test/fhir-backend.test.ts new file mode 100644 index 0000000..e0fdf3d --- /dev/null +++ b/test/fhir-backend.test.ts @@ -0,0 +1,34 @@ +import { FhirBackend } from '../src/fhir-backend' +import { R4 } from '@ahryman40k/ts-fhir-types'; + +describe('Testing Fhir Backend', () => { + + const backend = new FhirBackend(''); + beforeAll( async () => { + await backend.initialize() + }); + + it('gets questionnaires on FHIR server', async () => { + + if(backend.getQuestionnaires() != undefined){ + const bundle: R4.IBundle = backend.getQuestionnaires() + expect(bundle.resourceType).toBe('Bundle') + }else{ + throw new Error("Bundle not found"); + } + }); + + it('gets table of contents', async () => { + const toc: unknown = backend.getTableOfContents() + expect(toc).toBeTruthy() + }); + + it('gets single questionnaire', async () => { + if (backend.getQuestionnaire('2050148') != undefined) { + const questionnaire: R4.IResourceList = backend.getQuestionnaire('2050148')! + expect(questionnaire.id).toBe('2050148') + } else { + throw new Error("Bundle not found"); + } + }); +}); \ No newline at end of file