OpenMRS jclient library via Swagger

@pascal… was just wondering what the state of the openmrs.js project you started was, and if you thought it was worth picking back up and considering as a standard API for accessing OpenMRS resources?

Just trying to make sure we don’t rebuild the wheel… :slight_smile:

Take care, Mark

It’s not currently maintained. There was some concern when I first built it that it introduced too much overhead for the value it added, so people ended up just using a thin wrapper around Angular’s builtin http request API.

It’s probably still functional, but my suggestion would be to just use the fetch API directly in your sagas for now, and see if it actually becomes necessary to refactor that code out into a separate module later on.

The whole library can pretty much be rewritten as:

// openmrs.js
export const get = async (model, id) => {
  const config = {
    headers: {
      'Authorization': 'Basic YWRtaW46QWRtaW4xMjM='   // admin:Admin123
    }
  };

  return (await fetch(`${OMRS_API_BASE}/${model}/${id}`, config)).json();
}

export const post = async (model, payload) => {
  const config = {
    headers: {
      'Authorization': 'Basic YWRtaW46QWRtaW4xMjM='   // admin:Admin123
    },
    method: 'POST',
    body: JSON.stringify(payload)
  };

  return (await fetch(`${OMRS_API_BASE}/${model}`, config)).json();
}

Then later:

// saga.js
import { get } from 'openmrs';

const patient123 = await get('patient', 'uuid-123');

// ↪ dispatch action with patient as payload

EDIT: For future reference, to see this code in action, go to the demo, open the JavaScript console, and run the following:

await (await fetch('/openmrs/ws/rest/v1/patient/79e8ef35-5613-434c-91b4-cf5344bc5a09', { headers: { 'Authorization': 'Basic YWRtaW46QWRtaW4xMjM='} })).json()

Tested in Chrome and Safari.

Cool, thanks @pascal! I will play around with this more next week…

Take care, Mark

1 Like