Skip to content

Commit

Permalink
Update most remaining sample apps to latest client
Browse files Browse the repository at this point in the history
  • Loading branch information
nschwertner committed Mar 6, 2022
1 parent ce72abf commit 2144a74
Show file tree
Hide file tree
Showing 16 changed files with 99 additions and 83 deletions.
50 changes: 23 additions & 27 deletions authorize-client/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ function refreshApp() {

function initialize(settings) {
setSettings({
client_id : settings.client_id,
secret : settings.secret,
scope : settings.scope + " launch",
launch_id : urlParam("launch"),
api_server_uri: urlParam("iss")
clientId : settings.clientId,
clientSecret : settings.clientSecret,
scope : settings.scope,
launch : urlParam("launch"),
iss : urlParam("iss")
});
clearAuthToken();
refreshApp();
Expand Down Expand Up @@ -60,38 +60,38 @@ function setSettings(data) {
}

function hasAuthToken() {
return sessionStorage.tokenResponse !== undefined;
return sessionStorage.SMART_KEY !== undefined;
}

function clearAuthToken() {
delete sessionStorage.tokenResponse;
let key = sessionStorage.SMART_KEY;
if (key) delete sessionStorage[key.substring(1,key.length-1)];
delete sessionStorage.SMART_KEY;
}

function getHumanName(name) {
return name.given.join(" ") + " " + name.family;
return name.map((name) => name.given.join(" ") + " " + name.family).join(" / ");
}

function authorize() {
var settings = getSettings();

FHIR.oauth2.authorize({
"client": {
"client_id": settings.client_id,
"scope" : settings.scope,
"launch" : settings.launch_id
},
"server": settings.api_server_uri
"clientId" : settings.clientId,
"scope" : settings.scope,
"clientSecret" : settings.clientSecret,
"launch" : settings.launch,
"iss" : settings.iss
});
}

function getPatientName() {
var ret = $.Deferred();

FHIR.oauth2.ready(function(smart) {
var patient = smart.patient;
patient.read().then(function(pt) {
ret.resolve(getHumanName(pt.name[0]));
}).fail(function() {
FHIR.oauth2.ready(function(client) {
client.patient.read().then(function(pt) {
ret.resolve(getHumanName(pt.name));
}, function() {
ret.reject("Could not fetch patient name");
});
});
Expand All @@ -102,18 +102,15 @@ function getPatientName() {
function getUserName() {
var ret = $.Deferred();

FHIR.oauth2.ready(function(smart){
var user = smart.user;

// smart.userId = "Patient/" + smart.userId
$.when(user.read())
FHIR.oauth2.ready(function(client){
client.user.read()
.then(function(pt) {
if (pt) {
if (pt.resourceType === "Practitioner" ||
pt.resourceType === "RelatedPerson" ||
pt.resourceType === "Patient")
{
ret.resolve(getHumanName(pt.name[0]));
ret.resolve(getHumanName(pt.name));
}
else {
ret.reject("Could not fetch user name");
Expand All @@ -122,8 +119,7 @@ function getUserName() {
else {
ret.resolve(pt);
}
})
.fail(function(error) {
}, function(error) {
window.SMART = smart
console.log(smart)
console.log(error)
Expand Down
2 changes: 1 addition & 1 deletion authorize-client/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<head>
<title>User Profile</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="node_modules/fhirclient/fhir-client.js"></script>
<script src="node_modules/fhirclient/build/fhir-client.js"></script>
<script src="app.js"></script>
</head>
<body>
Expand Down
2 changes: 1 addition & 1 deletion authorize-client/launch.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<script src="app.js"></script>
<script>
initialize({
client_id: "my_web_app",
clientId: "my_web_app",
scope: "patient/*.read openid profile"
});
</script>
Expand Down
4 changes: 2 additions & 2 deletions authorize-client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"start": "http-server -p 9090 -c-1"
},
"dependencies": {
"fhirclient": "^0.1.8",
"http-server": "^0.10.0"
"fhirclient": "^2.4.0",
"http-server": "^14.1.0"
}
}
37 changes: 33 additions & 4 deletions medications-standalone/index.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<!DOCTYPE html>
<html>
<head>
<script src="node_modules/fhirclient/fhir-client.js"></script>
<script src="node_modules/fhirclient/build/fhir-client.js"></script>
</head>
<body>
<h1>Medications for <span id="name">Loading...</span></h1>
Expand Down Expand Up @@ -37,11 +37,11 @@ <h1>Medications for <span id="name">Loading...</span></h1>
med_list.innerHTML += "<li> " + getMedicationName(medCodings) + "</li>";
}

FHIR.oauth2.ready(function(smart){
smart.patient.read().then(function(pt) {
FHIR.oauth2.ready(function(client){
client.patient.read().then(function(pt) {
displayPatient (pt);
});
smart.patient.api.fetchAllWithReferences({type: "MedicationRequest"},["MedicationRequest.medicationReference"]).then(function(results, refs) {
client.patient.api.fetchAllWithReferences({type: "MedicationRequest"},["MedicationRequest.medicationReference"]).then(function(results, refs) {
if (results.length) {
med_list.innerHTML = ""
results.forEach(function(prescription){
Expand All @@ -59,6 +59,35 @@ <h1>Medications for <span id="name">Loading...</span></h1>
});
});

FHIR.oauth2.ready(function(client){
client.patient.read().then(function(pt) {
displayPatient (pt);

client.patient.request(
"MedicationRequest",
{
'pageLimit': 0,
'flat': true,
'resolveReferences': 'medicationReference'
}
).then(function(results) {
if (results.length) {
med_list.innerHTML = "";
results.forEach(function(prescription){
if (prescription.medicationCodeableConcept) {
displayMedication(prescription.medicationCodeableConcept.coding || []);
} else if (prescription.medicationReference) {
displayMedication(prescription.medicationReference.code.coding || []);
}
});
}
else {
med_list.innerHTML = "No medications found for the selected patient";
}
});
});
});

</script>
</body>
</html>
10 changes: 4 additions & 6 deletions medications-standalone/launch.html
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
<!DOCTYPE html>
<html>
<head>
<script src="node_modules/fhirclient/fhir-client.js"></script>
<script src="node_modules/fhirclient/build/fhir-client.js"></script>
<script>
FHIR.oauth2.authorize({
"client": {
"client_id": "my_web_app",
"scope": "patient/*.read launch/patient"
},
"server": "https://launch.smarthealthit.org/v/r3/sim/eyJoIjoiMSIsImoiOiIxIn0/fhir"
"clientId": "my_web_app",
"scope": "patient/*.read launch/patient",
"iss": "https://launch.smarthealthit.org/v/r4/sim/eyJoIjoiMSIsImoiOiIxIn0/fhir"
});
</script>
</head>
Expand Down
4 changes: 2 additions & 2 deletions medications-standalone/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"start": "http-server -p 9090 -c-1"
},
"dependencies": {
"fhirclient": "0.1.12",
"http-server": "^0.10.0"
"fhirclient": "^2.4.0",
"http-server": "^14.1.0"
}
}
32 changes: 13 additions & 19 deletions medications/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<html>
<head>
<title>Medications List</title>
<script src="node_modules/fhirclient/fhir-client.js"></script>
<script src="node_modules/fhirclient/build/fhir-client.js"></script>
</head>
<body>
<h1>Medications for <span id="name">Loading...</span></h1>
Expand Down Expand Up @@ -37,31 +37,25 @@ <h1>Medications for <span id="name">Loading...</span></h1>
med_list.innerHTML += "<li> " + getMedicationName(medCodings) + "</li>";
}

FHIR.oauth2.ready(function(smart){
smart.patient.read().then(function(pt) {
FHIR.oauth2.ready(function(client){
client.patient.read().then(function(pt) {
displayPatient (pt);

// smart.api.patient.fetcAllWithReferences(...) does not work
// with fhir-client.js. MedicationRequest is not listed in
// https://github.com/FHIR/fhir.js/blob/master/src/middlewares/patient.js#L5
// and we have to explicitly search with patient id
smart.api.fetchAllWithReferences(
{
type: "MedicationRequest",
query: {
patient: pt.id
}
},
[ "MedicationRequest.medicationReference" ]
).then(function(results, refs) {
client.patient.request(
"MedicationRequest",
{
'pageLimit': 0,
'flat': true,
'resolveReferences': 'medicationReference'
}
).then(function(results) {
if (results.length) {
med_list.innerHTML = "";
results.forEach(function(prescription){
if (prescription.medicationCodeableConcept) {
displayMedication(prescription.medicationCodeableConcept.coding);
displayMedication(prescription.medicationCodeableConcept.coding || []);
} else if (prescription.medicationReference) {
var med = refs(prescription, prescription.medicationReference);
displayMedication(med && med.code.coding || []);
displayMedication(prescription.medicationReference.code.coding || []);
}
});
}
Expand Down
4 changes: 2 additions & 2 deletions medications/launch.html
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<!DOCTYPE html>
<html>
<head>
<script src="node_modules/fhirclient/fhir-client.js"></script>
<script src="node_modules/fhirclient/build/fhir-client.js"></script>
<script>
FHIR.oauth2.authorize({
"client_id": "my_web_app",
"clientId" : "my_web_app",
"scope" : "patient/*.read"
});
</script>
Expand Down
4 changes: 2 additions & 2 deletions medications/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"start": "http-server -p 9090 -c-1"
},
"dependencies": {
"fhirclient": "0.1.12",
"http-server": "^0.10.0"
"fhirclient": "^2.4.0",
"http-server": "^14.1.0"
}
}
6 changes: 3 additions & 3 deletions tokens/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<head>
<title>Sample app</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="node_modules/fhirclient/fhir-client.js"></script>
<script src="node_modules/fhirclient/build/fhir-client.js"></script>
</head>
<body>
<pre></pre>
Expand All @@ -13,10 +13,10 @@
}

FHIR.oauth2.ready(function(client) {
$.when(client.user.read()).then(
client.user.read().then(
function(user){
log("User:\n" + JSON.stringify(user, null, 4) + "\n");
log("Token Response:\n" + JSON.stringify(client.tokenResponse, null, 4));
log("Token Response:\n" + JSON.stringify(client.state.tokenResponse, null, 4));
},
function(error){
log("Error:\n" + error);
Expand Down
4 changes: 2 additions & 2 deletions tokens/launch.html
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<!DOCTYPE html>
<html>
<head>
<script src="node_modules/fhirclient/fhir-client.js"></script>
<script src="node_modules/fhirclient/build/fhir-client.js"></script>
<script>
FHIR.oauth2.authorize({
"client_id": "my_web_app",
"clientId" : "my_web_app",
"scope" : "openid profile"
});
</script>
Expand Down
4 changes: 2 additions & 2 deletions tokens/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"start": "http-server -p 9090 -c-1"
},
"dependencies": {
"fhirclient": "^0.1.11",
"http-server": "^0.10.0"
"fhirclient": "^2.4.0",
"http-server": "^14.1.0"
}
}
11 changes: 5 additions & 6 deletions write/index.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<!DOCTYPE html>
<html>
<head>
<script src="node_modules/fhirclient/fhir-client.js"></script>
<script src="node_modules/fhirclient/build/fhir-client.js"></script>
<style>
pre {
margin: 0;
Expand All @@ -14,7 +14,7 @@
(function () {
"use strict";

FHIR.oauth2.ready(function(smart){
FHIR.oauth2.ready(function(client){
var resource = {
"resourceType": "Patient",
"text": {
Expand Down Expand Up @@ -55,15 +55,14 @@
};

// Create the patient and then update its active flag to "true"
smart.api.create({resource: resource}).done(function(r) {
client.create(resource).then(function(patient) {

// NOTE that the patient will now have new "id" assigned by the
// server. The next request will be PUT (update) and that id will
// be required...
var patient = r.data;
patient["active"] = true;
smart.api.update({resource: patient}).done(function(r) {
var out = JSON.stringify(r.data, null, " ");
client.update(patient).then(function(r) {
var out = JSON.stringify(r, null, " ");
document.getElementsByTagName("pre")[0].innerText = "Now " +
"we have the following patient in the FHIR server:\n\n" +
out;
Expand Down
4 changes: 2 additions & 2 deletions write/launch.html
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<!DOCTYPE html>
<html>
<head>
<script src="node_modules/fhirclient/fhir-client.js"></script>
<script src="node_modules/fhirclient/build/fhir-client.js"></script>
<script>
FHIR.oauth2.authorize({
"client_id": "my_web_app",
"clientId": "my_web_app",
"scope": "user/*.*"
});
</script>
Expand Down
Loading

0 comments on commit 2144a74

Please sign in to comment.