From 81822e28bc86b91bc180831177cce4ae43514af4 Mon Sep 17 00:00:00 2001 From: Matthew Grill Date: Wed, 3 Aug 2016 14:07:24 -0700 Subject: [PATCH] Ability to filter embedded requests by field name --- README.md | 14 ++++++++++++-- lib/waterwheel.js | 20 +++++++++++++++----- test/base.js | 28 ++++++++++++++++++++++++++++ 3 files changed, 55 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index d79a839..3a12953 100644 --- a/README.md +++ b/README.md @@ -253,11 +253,21 @@ waterwheel.api.node.page.get(1, 'hal_json') .catch(err => { // err }); + +waterwheel.api.node.page.get(1, 'hal_json') + .then(res => waterwheel.fetchEmbedded(res, ['my_field'])) + .then(res => { + // res + }) + .catch(err => { + // err + }); ``` -`.fetchEmbedded()` accepts 1 argument +`.fetchEmbedded()` accepts 2 arguments - `entityJSON`: This should be a HAL+JSON structured object containing an `_embedded` key at the root. + - `includedFields`: Optionally provide a single field as a `string`, or an `array` of `strings` to filter the embedded request by. -When requesting embedded resources duplicates are removed to prevent extra HTTP requests. An array is returned with your original response and any embedded resources. If any of the subsequent requests fail, the promise is rejected. +When requesting embedded resources duplicates are removed to prevent extra HTTP requests. An array is returned with your original response and any embedded resources. If any of the subsequent requests fail, the promise is rejected. ### Entity Query diff --git a/lib/waterwheel.js b/lib/waterwheel.js index de8e75e..9ecda10 100644 --- a/lib/waterwheel.js +++ b/lib/waterwheel.js @@ -130,22 +130,32 @@ module.exports = class Waterwheel extends Request { * Fetch embedded resources from HAL+JSON documents * @param {object} entityJSON * An object, usually returned from Drupal, containing _embedded information + * @param {string|array} [includedFields] + * If specified, a series of embedded resources to fetch. * @return {Promise} * If no _embedded key is found, a rejection is returned, else a resolved * promise with all the embedded resources requests completed. */ - fetchEmbedded(entityJSON) { + fetchEmbedded(entityJSON, includedFields) { if (!entityJSON || !entityJSON.hasOwnProperty('_embedded')) { return Promise.reject('This is probably not HAL+JSON'); } + const fieldsToFilterBy = includedFields ? + (Array.isArray(includedFields) ? includedFields : [includedFields]) : false; + + const embeddedResources = entityJSON._embedded; + const embeddedResourcesKeys = Object.keys(embeddedResources); + let links = []; - Object.keys(entityJSON._embedded).forEach(key => { - entityJSON._embedded[key].forEach(ref => { - links.push(ref._links.self.href.split(this.base)[1]); + (fieldsToFilterBy ? + embeddedResourcesKeys.filter(key => fieldsToFilterBy.indexOf(key.split('/').pop()) !== -1) : + embeddedResourcesKeys).forEach(key => { + embeddedResources[key].forEach(ref => { + links.push(ref._links.self.href.split(this.base)[1]); + }); }); - }); // Create a Set from the possibly-duplicate links array. // Get an array from that set. diff --git a/test/base.js b/test/base.js index e0b0074..6a56376 100644 --- a/test/base.js +++ b/test/base.js @@ -188,3 +188,31 @@ test('Fetch Embedded', t => { t.deepEqual(res[1], {halExample: 'Some HAL+JSON'}); }); }); + +test('Fetch Embedded - Single Field', t => { + requireSubvert.subvert('axios', () => ( + Promise.resolve({data: {halExample: 'Some HAL+JSON'}}) + )); + + const waterwheel = new t.context.Waterwheel(t.context.base, t.context.credentials); + const halJSON = require('./sample/hal.example.json'); + return waterwheel.fetchEmbedded(halJSON, 'field_actor') + .then(res =>{ + t.is(res.length, 3); + t.deepEqual(res[1], {halExample: 'Some HAL+JSON'}); + }); +}); + +test('Fetch Embedded - Multiple Fields', t => { + requireSubvert.subvert('axios', () => ( + Promise.resolve({data: {halExample: 'Some HAL+JSON'}}) + )); + + const waterwheel = new t.context.Waterwheel(t.context.base, t.context.credentials); + const halJSON = require('./sample/hal.example.json'); + return waterwheel.fetchEmbedded(halJSON, ['field_actor', 'revision_uid']) + .then(res =>{ + t.is(res.length, 4); + t.deepEqual(res[1], {halExample: 'Some HAL+JSON'}); + }); +});