Skip to content
Permalink
master
Switch branches/tags
Go to file
Catch up to latest versions of core infrastructure. Also polish up certain bits of ES5 code to better use ES6+ options.
1 contributor

Users who have contributed to this file

module.exports = function follow(api, rootPath, relArray) {
const root = api({
method: 'GET',
path: rootPath
});
return relArray.reduce(function(root, arrayItem) {
const rel = typeof arrayItem === 'string' ? arrayItem : arrayItem.rel;
return traverseNext(root, rel, arrayItem);
}, root);
function traverseNext (root, rel, arrayItem) {
return root.then(function (response) {
if (hasEmbeddedRel(response.entity, rel)) {
return response.entity._embedded[rel];
}
if(!response.entity._links) {
return [];
}
if (typeof arrayItem === 'string') {
return api({
method: 'GET',
path: response.entity._links[rel].href
});
} else {
return api({
method: 'GET',
path: response.entity._links[rel].href,
params: arrayItem.params
});
}
});
}
function hasEmbeddedRel (entity, rel) {
return entity._embedded && entity._embedded.hasOwnProperty(rel);
}
};