This project will show you how to get data from our data source(HMS v2) via npm
.
We require some dependencies below :
- @hmsconnect/hmshealthapi -
The library makes you easy to get the data. To see more detail of the repository on
Github
, please refer to here. - IAS token - The token is JSON data object that contains
username
,password
andclient_id
. You can get the token here. It using for authentication to IAS. - dotenv - It using for declare/reference system environment variable.
At the root directory of your project :
$ npm install @hmsconnect/hmshealthapi dotenv --save
const hmshealthapi = require('@hmsconnect/hmshealthapi');
require('dotenv').config();
Don't forget declare system environment variable into .env
file :
# Example .env
USERNAME=admin
PASSWORD=admin
CLIENT_ID=1234567890
HOST_DATA_SRC=127.0.0.1
let tokens = {
username: process.env.USERNAME,
password: process.env.PASSWORD,
client_id: process.env.CLIENT_ID
};
let hms = hmshealthapi();
hms.Initial(tokens, (error, response) => {
// 1. Point to specific server
// 2. Get data
});
In case you would like to point to specific server, you can calling setConfig
to config the object insstance :
hms.Initial(tokens, (error, response) => {
// ...
hms.SetConfig({
host: process.env.HOST_DATA_SRC,
endpoints: {
get_domain_resource : '/sandbox/api/v2',
get_version : '/sandbox/version'
}
});
// ...
});
For DATA_SOURCE_IP_OR_HOSTNAME
, you can request IP/DNS of the data source instance via HMS's admin
// ...
try {
// Set header for authentication
const headers = {
Authorization: `Bearer ${response.access_token ? response.access_token : ''}`
};
// Test get some practitioner
hms.get('practitioner', headers, 'name.givenName:contain="Silas S9"&sort=-identifier.start')
.then((response) => { console.log('Searching with request URL:', JSON.stringify(response, null, 2)); })
.catch((err) => { console.log('Error :', err); });
} catch(err) {
console.log(err)
}
// ...
// Declare HMS instance object
const hmshealthapi = require('@hmsconnect/hmshealthapi');
require('dotenv').config();
let tokens = {
username: process.env.USERNAME,
password: process.env.PASSWORD,
client_id: process.env.CLIENT_ID
};
let hms = hmshealthapi();
hms.Initial(tokens, (error, response) => {
if(error) {
console.log('Initial error : ', error);
} else {
console.log('response :', response);
// Config data source
hms.SetConfig({
host: process.env.HOST_DATA_SRC,
endpoints: {
get_domain_resource : '/sandbox/api/v2',
get_version : '/sandbox/version'
}
});
try {
const headers = {
Authorization: `Bearer ${response.access_token ? response.access_token : ''}`
};
console.log('Header : ', headers)
hms.get('practitioner', headers, 'name.givenName:contain="Silas S9"&sort=-identifier.start')
.then((response) => { console.log('Searching with request URL:', JSON.stringify(response, null, 2)); })
.catch((err) => { console.log('Error :', err); });
} catch(err) {
console.log(err)
}
}
});
ISC