Skip to content

Commit

Permalink
support resolveReferences for arrays of refs
Browse files Browse the repository at this point in the history
  • Loading branch information
vlad-ignatov committed Jun 8, 2019
1 parent 82e8d71 commit 7637151
Show file tree
Hide file tree
Showing 10 changed files with 851 additions and 640 deletions.
164 changes: 81 additions & 83 deletions build/fhir-client.js
Expand Up @@ -8126,89 +8126,87 @@ function () {
function () {
var _ref = _asyncToGenerator(
/*#__PURE__*/
regeneratorRuntime.mark(function _callee2(data) {
var resolve;
return regeneratorRuntime.wrap(function _callee2$(_context2) {
regeneratorRuntime.mark(function _callee(data) {
var getRef, resolve;
return regeneratorRuntime.wrap(function _callee$(_context) {
while (1) {
switch (_context2.prev = _context2.next) {
switch (_context.prev = _context.next) {
case 0:
resolve =
/*#__PURE__*/
function () {
var _ref2 = _asyncToGenerator(
/*#__PURE__*/
regeneratorRuntime.mark(function _callee(obj) {
return regeneratorRuntime.wrap(function _callee$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0:
_context.next = 2;
return Promise.all(fhirOptions.resolveReferences.map(function (path) {
return new Promise(function (resolve2, reject) {
var ref = _getPath(obj, path + ".reference");

if (ref) {
var sub = _resolvedRefs[ref];

if (!sub) {
return _this2.request(ref).then(function (sub) {
_resolvedRefs[ref] = sub;

if (fhirOptions.graph) {
setPath(obj, path, sub);
}
}).then(resolve2, reject);
}

if (fhirOptions.graph) {
setPath(obj, path, sub);
}
}

resolve2();
});
}));

case 2:
case "end":
return _context.stop();
/**
* Gets single reference by id. Caches the result in _resolvedRefs
* @param {String} refId
*/
getRef = function getRef(refId) {
var sub = _resolvedRefs[refId];

if (!sub) {
return _this2.request(refId).then(function (sub) {
_resolvedRefs[refId] = sub;
return sub;
});
}

return sub;
};
/**
* Resolve all refs (specified in fhirOptions.resolveReferences)
* in the given resource.
* @param {Object} obj FHIR Resource
*/


resolve = function resolve(obj) {
return Promise.all(fhirOptions.resolveReferences.map(function (path) {
var node = _getPath(obj, path);

if (node) {
var isArray = Array.isArray(node);
return Promise.all(makeArray(node).map(function (item, i) {
var ref = item.reference;

if (ref) {
return getRef(ref).then(function (sub) {
if (fhirOptions.graph) {
if (isArray) {
setPath(obj, "".concat(path, ".").concat(i), sub);
} else {
setPath(obj, path, sub);
}
}
});
}
}
}, _callee);
}));
}
}));

return function resolve(_x2) {
return _ref2.apply(this, arguments);
};
}();
};

if (!(data && data.resourceType == "Bundle")) {
_context2.next = 6;
_context.next = 7;
break;
}

_context2.next = 4;
_context.next = 5;
return Promise.all((data.entry || []).map(function (item) {
return resolve(item.resource);
}));

case 4:
_context2.next = 8;
case 5:
_context.next = 9;
break;

case 6:
_context2.next = 8;
case 7:
_context.next = 9;
return resolve(data);

case 8:
return _context2.abrupt("return", data);

case 9:
return _context.abrupt("return", data);

case 10:
case "end":
return _context2.stop();
return _context.stop();
}
}
}, _callee2);
}, _callee);
}));

return function (_x) {
Expand All @@ -8218,30 +8216,30 @@ function () {
.then(
/*#__PURE__*/
function () {
var _ref3 = _asyncToGenerator(
var _ref2 = _asyncToGenerator(
/*#__PURE__*/
regeneratorRuntime.mark(function _callee3(data) {
regeneratorRuntime.mark(function _callee2(data) {
var links, next, nextPage;
return regeneratorRuntime.wrap(function _callee3$(_context3) {
return regeneratorRuntime.wrap(function _callee2$(_context2) {
while (1) {
switch (_context3.prev = _context3.next) {
switch (_context2.prev = _context2.next) {
case 0:
if (!(data && data.resourceType == "Bundle")) {
_context3.next = 18;
_context2.next = 18;
break;
}

if (!hasPageCallback) {
_context3.next = 4;
_context2.next = 4;
break;
}

_context3.next = 4;
_context2.next = 4;
return fhirOptions.onPage(data, _objectSpread({}, _resolvedRefs));

case 4:
if (! --fhirOptions.pageLimit) {
_context3.next = 18;
_context2.next = 18;
break;
}

Expand All @@ -8252,49 +8250,49 @@ function () {
data = makeArray(data); // console.log("===>", data);

if (!(next && next.url)) {
_context3.next = 18;
_context2.next = 18;
break;
}

_context3.next = 11;
_context2.next = 11;
return _this2.request(next.url, fhirOptions, _resolvedRefs);

case 11:
nextPage = _context3.sent;
nextPage = _context2.sent;

if (!hasPageCallback) {
_context3.next = 14;
_context2.next = 14;
break;
}

return _context3.abrupt("return", null);
return _context2.abrupt("return", null);

case 14:
if (!fhirOptions.resolveReferences.length) {
_context3.next = 17;
_context2.next = 17;
break;
}

Object.assign(_resolvedRefs, nextPage.references); // console.log("===>", nextPage);

return _context3.abrupt("return", data.concat(makeArray(nextPage.data || nextPage)));
return _context2.abrupt("return", data.concat(makeArray(nextPage.data || nextPage)));

case 17:
return _context3.abrupt("return", data.concat(makeArray(nextPage)));
return _context2.abrupt("return", data.concat(makeArray(nextPage)));

case 18:
return _context3.abrupt("return", data);
return _context2.abrupt("return", data);

case 19:
case "end":
return _context3.stop();
return _context2.stop();
}
}
}, _callee3);
}, _callee2);
}));

return function (_x3) {
return _ref3.apply(this, arguments);
return function (_x2) {
return _ref2.apply(this, arguments);
};
}()) // Finalize --------------------------------------------------------
.then(function (data) {
Expand Down
2 changes: 1 addition & 1 deletion build/fhir-client.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion build/fhir-client.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion build/fhir-client.min.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion build/report.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/README.md
Expand Up @@ -5,7 +5,7 @@ SMART on FHIR JavaScript Library
This is a JavaScript library for connecting SMART apps to Fhir servers.
It works both in browsers (IE10+) and on the server (NodeJS).

> This is the documentation for the new version. If you want to migrate from older versions, make sure you check out **[what's new in v2](http://docs.smarthealthit.org/client-js/v2.html)**. For older versions see http://docs.smarthealthit.org/clients/javascript/
> This is the documentation for version 2.0.0.+. If you want to migrate from older versions, make sure you check out **[what's new in v2](http://docs.smarthealthit.org/client-js/v2.html)**. For older versions see [http://docs.smarthealthit.org/clients/javascript/](http://docs.smarthealthit.org/clients/javascript/).

[![CircleCI](https://circleci.com/gh/smart-on-fhir/client-js/tree/master.svg?style=svg)](https://circleci.com/gh/smart-on-fhir/client-js/tree/master) [![Coverage Status](https://coveralls.io/repos/github/smart-on-fhir/client-js/badge.svg?branch=master)](https://coveralls.io/github/smart-on-fhir/client-js?branch=master) [![npm version](https://badge.fury.io/js/fhirclient.svg)](https://badge.fury.io/js/fhirclient)
Expand Down
55 changes: 37 additions & 18 deletions src/Client.js
Expand Up @@ -417,27 +417,46 @@ class FhirClient
// Resolve References ----------------------------------------------
.then(async (data) => {

const resolve = async obj => {
await Promise.all(fhirOptions.resolveReferences.map(path => {
return new Promise((resolve2, reject) => {
const ref = getPath(obj, path + ".reference");
if (ref) {
let sub = _resolvedRefs[ref];
if (!sub) {
return this.request(ref).then(sub => {
_resolvedRefs[ref] = sub;
/**
* Gets single reference by id. Caches the result in _resolvedRefs
* @param {String} refId
*/
const getRef = refId => {
let sub = _resolvedRefs[refId];
if (!sub) {
return this.request(refId).then(sub => {
_resolvedRefs[refId] = sub;
return sub;
});
}
return sub;
};

/**
* Resolve all refs (specified in fhirOptions.resolveReferences)
* in the given resource.
* @param {Object} obj FHIR Resource
*/
const resolve = obj => {
return Promise.all(fhirOptions.resolveReferences.map(path => {
const node = getPath(obj, path);
if (node) {
const isArray = Array.isArray(node);
return Promise.all(makeArray(node).map((item, i) => {
const ref = item.reference;
if (ref) {
return getRef(ref).then(sub => {
if (fhirOptions.graph) {
setPath(obj, path, sub);
if (isArray) {
setPath(obj, `${path}.${i}`, sub);
} else {
setPath(obj, path, sub);
}
}
}).then(resolve2, reject);
});
}

if (fhirOptions.graph) {
setPath(obj, path, sub);
}
}
resolve2();
});
}));
}
}));
};

Expand Down
49 changes: 47 additions & 2 deletions test/Client.test.js
Expand Up @@ -1030,8 +1030,53 @@ describe("FHIR.client", () => {
]);
});
});
// it ("can refresh");
// it ("can not refresh if useRefreshToken is false");

describe ("resolve all refs if it points to an array", () => {
crossPlatformTest(async (env) => {
const client = new Client(env, mockUrl);

// Main page
mockServer.mock({
headers: { "content-type": "application/json" },
status: 200,
body: {
resourceType: "Patient",
id: "id",
ref1: [
{ reference: "whatever-1" },
{ reference: "whatever-2" }
]
}
});

// Referenced page 1
mockServer.mock({
headers: { "content-type": "application/json" },
status: 200,
body: { resourceType: "Ref", id: "Ref-id-1" }
});

// Referenced page 2
mockServer.mock({
headers: { "content-type": "application/json" },
status: 200,
body: { resourceType: "Ref", id: "Ref-id-2" }
});

const result = await client.request(
"/Patient/id",
{ resolveReferences: "ref1" }
);
expect(result).to.equal({
resourceType: "Patient",
id: "id",
ref1: [
{ resourceType: "Ref", id: "Ref-id-1" },
{ resourceType: "Ref", id: "Ref-id-2" }
]
});
});
});
});

describe ("client.user", () => {
Expand Down

0 comments on commit 7637151

Please sign in to comment.