This repository has been archived by the owner on Nov 16, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 26
Fetch embedded resources #29
Merged
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -126,4 +126,30 @@ 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 | ||
* @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) { | ||
if (!entityJSON || !entityJSON.hasOwnProperty('_embedded')) { | ||
return Promise.reject('This is probably not HAL+JSON'); | ||
} | ||
|
||
let links = []; | ||
|
||
Object.keys(entityJSON._embedded).forEach(key => { | ||
entityJSON._embedded[key].forEach(ref => { | ||
links.push(ref._links.self.href.split(this.base)[1]); | ||
}); | ||
}); | ||
|
||
links = Array.from(new Set(links)); | ||
|
||
return Promise.all([Promise.resolve(entityJSON)].concat(links.map(link => this.issueRequest(methods.get, link)))); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm assuming your doing this so that the parent entity response is provided adjacent to the embedded ones? A comment could help make the intent here clear. |
||
} | ||
|
||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
{ | ||
"_links": { | ||
"self": { | ||
"href": "http://foo.dev/page/bruce-wayne?_format=hal_json" | ||
}, | ||
"type": { | ||
"href": "http://foo.dev/rest/type/node/page" | ||
}, | ||
"http://foo.dev/rest/relation/node/page/uid": [{ | ||
"href": "http://foo.dev/user/1?_format=hal_json", | ||
"lang": "en" | ||
}], | ||
"http://foo.dev/rest/relation/node/page/revision_uid": [{ | ||
"href": "http://foo.dev/user/1?_format=hal_json" | ||
}], | ||
"http://foo.dev/rest/relation/node/page/field_actor": [{ | ||
"href": "http://foo.dev/node/6?_format=hal_json" | ||
}, { | ||
"href": "http://foo.dev/node/8?_format=hal_json" | ||
}] | ||
}, | ||
"nid": [{ | ||
"value": "7" | ||
}], | ||
"uuid": [{ | ||
"value": "284365c0-fa63-43ac-ae68-d60600873c34" | ||
}], | ||
"vid": [{ | ||
"value": "7" | ||
}], | ||
"langcode": [{ | ||
"value": "en", | ||
"lang": "en" | ||
}], | ||
"type": [{ | ||
"target_id": "page" | ||
}], | ||
"title": [{ | ||
"value": "Bruce Wayne", | ||
"lang": "en" | ||
}], | ||
"_embedded": { | ||
"http://foo.dev/rest/relation/node/page/uid": [{ | ||
"_links": { | ||
"self": { | ||
"href": "http://foo.dev/user/1?_format=hal_json" | ||
}, | ||
"type": { | ||
"href": "http://foo.dev/rest/type/user/user" | ||
} | ||
}, | ||
"uuid": [{ | ||
"value": "b578592a-a751-4993-9060-53f488597e59" | ||
}], | ||
"lang": "en" | ||
}], | ||
"http://foo.dev/rest/relation/node/page/revision_uid": [{ | ||
"_links": { | ||
"self": { | ||
"href": "http://foo.dev/user/1?_format=hal_json" | ||
}, | ||
"type": { | ||
"href": "http://foo.dev/rest/type/user/user" | ||
} | ||
}, | ||
"uuid": [{ | ||
"value": "b578592a-a751-4993-9060-53f488597e59" | ||
}] | ||
}], | ||
"http://foo.dev/rest/relation/node/page/field_actor": [{ | ||
"_links": { | ||
"self": { | ||
"href": "http://foo.dev/node/6?_format=hal_json" | ||
}, | ||
"type": { | ||
"href": "http://foo.dev/rest/type/node/actor" | ||
} | ||
}, | ||
"uuid": [{ | ||
"value": "b5c619cd-1dfe-4c9d-afea-760a8eaf27c9" | ||
}] | ||
}, { | ||
"_links": { | ||
"self": { | ||
"href": "http://foo.dev/node/8?_format=hal_json" | ||
}, | ||
"type": { | ||
"href": "http://foo.dev/rest/type/node/actor" | ||
} | ||
}, | ||
"uuid": [{ | ||
"value": "ac1ad040-c05c-45ac-82d7-5b3eda40e451" | ||
}] | ||
}] | ||
}, | ||
"status": [{ | ||
"value": "1", | ||
"lang": "en" | ||
}], | ||
"created": [{ | ||
"value": "1470156620", | ||
"lang": "en" | ||
}], | ||
"changed": [{ | ||
"value": "1470170146", | ||
"lang": "en" | ||
}], | ||
"promote": [{ | ||
"value": "0" | ||
}], | ||
"sticky": [{ | ||
"value": "0", | ||
"lang": "en" | ||
}], | ||
"revision_timestamp": [{ | ||
"value": "1470156635" | ||
}], | ||
"revision_translation_affected": [{ | ||
"value": "1", | ||
"lang": "en" | ||
}], | ||
"default_langcode": [{ | ||
"value": "1", | ||
"lang": "en" | ||
}], | ||
"body": [{ | ||
"value": "some body", | ||
"format": "basic_html", | ||
"summary": "", | ||
"lang": "en" | ||
}], | ||
"comment": [{ | ||
"status": "2", | ||
"cid": "0", | ||
"last_comment_timestamp": "1470156635", | ||
"last_comment_name": null, | ||
"last_comment_uid": "1", | ||
"comment_count": "0", | ||
"lang": "en" | ||
}] | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A comment about not wanting to fetch the same resource multiple times hence the use of
Set
could help future contributors. Also a note aboutPromise.all
needing an array. ??