Skip to content
This repository has been archived by the owner on Nov 16, 2018. It is now read-only.

Commit

Permalink
Ability to filter embedded requests by field name
Browse files Browse the repository at this point in the history
  • Loading branch information
Matthew Grill committed Aug 3, 2016
1 parent 5032fa5 commit 81822e2
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 7 deletions.
14 changes: 12 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
20 changes: 15 additions & 5 deletions lib/waterwheel.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
28 changes: 28 additions & 0 deletions test/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'});
});
});

0 comments on commit 81822e2

Please sign in to comment.