1.0.0 • Published 3 years ago

@bpac/valida-client v1.0.0

Weekly downloads
5
License
-
Repository
-
Last release
3 years ago

Valida Client

Usage instructions

Please refer to the implementation guide for more comprehensive API documentation.

Including in your project

Import this package into one of the files in your application.

import '@bpac/valida-client';

When the website is launched from Valida Client the FHIR and OpenEHR APIs will be available via window.ds2.pms.

If the website was not launched from Valida Client then window.ds2 will be null.

Example

Load the current patient as a FHIR Patient resource and output the name.

Note the @bpac/valida-client import statement only needs to be done once per webpage - suggest it it put into whatever file is loaded first.

import { Component, OnInit } from '@angular/core';
import { SearchParameter } from '@bpac/pms-api';
import '@bpac/valida-client';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html'
})
export class AppComponent implements OnInit {
  public async ngOnInit(): Promise<void> {
    let patientId = await this.getOpenEhr('PatientId');

    let patientSearchParameters = [{ name: 'patientId', comparator: 'eq', value: patientId.id }];

    //load asynchronously
    let patientsPromise = this.getFhir<fhir.Patient>('Patient', patientSearchParameters);
    let observationsPromise = this.getFhir<fhir.Observation>('Observation', patientSearchParameters);
    let conditionsPromise = this.getFhir<fhir.Condition>('Condition', patientSearchParameters);

    //await all async requests
    let [patients, observations, conditions] = await Promise.all([patientsPromise, observationsPromise, conditionsPromise]);

    //do something with the data
    let message = `Patient: ${patientId.id} - ${patients[0].name[0].given} - has ${observations.length} Observation resources and ${conditions.length} Condition resources.`;

    alert(message);
  }

  private getOpenEhr(type: string): Promise<any> {
    return new Promise((resolve, reject) => {
      window.ds2.pms.get({ type }, response => {
        //status.success will be true when the request succeeded
        if (response.status.success) {
          resolve(response.instance);
        } else {
          //status.message contains the error message when status.success is false
          reject(response.status.message);
        }
      });
    });
  }

  private getFhir<T>(type: string, searchParameter: SearchParameter[]): Promise<T[]> {
    return new Promise((resolve, reject) => {
      window.ds2.pms.getFhir(type, searchParameter, result => {
        //an OperationOutcome will be returned when there is an error
        if (result.resourceType === 'OperationOutcome') {
          reject(result);
        } else {
          //otherwise a bundle of results will be returned
          let bundle = result as fhir.Bundle;

          if (bundle.entry == null) {
            return [];
          }

          //the bundle will always have an array of entries even when there is only a single result
          resolve(bundle.entry.map(entry => entry.resource as T));
        }
      }, 0);
    });
  }
}

FHIR API

getFhirApiDefinition

Calls though to window.FhirApi.getApiDefinition

window.ds2.pms.getFhirApiDefinition((definition: FhirDataProviderDefinition[]) => {
    //do something with the definition
});

getFhir

Calls though to window.FhirApi.getFhir

window.ds2.pms.getFhir('Patient', [], (patientBundle: fhir.Bundle) => {
    //do something with the result
});

putFhir

Calls though to window.FhirApi.putFhir

window.ds2.pms.putFhir('patientId', {resourceType:'Bundle'}, (result: fhir.OperationOutcome) => {
    //check the writeback result
});

cancelOutstandingRequests

Calls though to window.FhirApi.cancelOutstandingRequests

window.ds2.pms.cancelOutstandingRequests('Observation', (result: fhir.OperationOutcome) => {
    //observation requests will be canceled
});

OpenEHR API

getOpenEhrApiDefinition

Calls though to window.PmsInterface.getApiDefinition

window.ds2.pms.getOpenEhrApiDefinition((definition: OpenEhrDataProviderDefinition[]) => {
    //do something with the definition
});

get

Calls though to window.PmsInterface.get

window.ds2.pms.get({type:'Patient', patientId:{id:'123'}}, (response: PmsApiResponse) => {
    //do something with the response
});

list

Calls though to window.PmsInterface.list

window.ds2.pms.list({type:'Blood pressure', patientId:{id:'123'}}, (response: PmsApiResponse) => {
    //do something with the response
});

put

Calls though to window.PmsInterface.put

window.ds2.pms.put({type:'Blood pressure', patientId:{id:'123'}, instance:{}}, (response: PutResult) => {
    //do something with the response
});

putBatch

Calls though to window.PmsInterface.putBatch

window.ds2.pms.putBatch([{type:'Blood pressure', patientId:{id:'123'}, instance:{}}], (response: PutResult) => {
    //do something with the response
});

Miscellaneous

flushCache

Calls though to window.PmsInterface.flushCache

window.ds2.pms.flushCache(() => {
    //any cached patient data has now been cleared within Valida Client
});

registerCallback

Calls though to window.MosaicClient.registerCallback

window.ds2.pms.registerCallback('patientOpened', () => {
    //the current patient has been changed
});