Skip to content
This repository was archived by the owner on Nov 16, 2018. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions lib/jsonapi.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,4 +95,49 @@ module.exports = class JSONAPI {
);
}

/**
* Send unlimited subrequests to Drupal
* @param {array} [requests=[]]
* An array of subrequests to make.
* @return {promise}
* A resolved promise with the processed multipart response.
*/
subRequests(requests = []) {
const requestURL = `${this.request.options.base}/subrequests?_format=json&query=${JSON.stringify(
requests.map(request => Object.assign({
requestId: `waterwheel-${Math.random().toString(36).substr(2, 10)}`,
action: 'view',
headers: {
'Accept': 'application/json'
}}, typeof request === 'string' || request instanceof String ?
{uri: request} :
request.hasOwnProperty('options') && Object.keys(request.options).length && request.options.constructor === Object ?
Object.assign(request, {uri: `${request.uri}?${qs.stringify(request.options, {indices: false})}`}) :
request
))
)}`;
return this.request.axios.get(requestURL)
.then(({data}) => Object.keys(data).map(key =>({[key]: JSON.parse(data[key].body)})));
}

merge({data, included}) {
return new Promise((resolve, reject) => {

return resolve(
data.map((item) => {
const relationships = Object.keys(item.relationships);
relationships.forEach((relationship) => {
let relationshipToAttach = included.filter(include => (
item.relationships[relationship].data &&
item.relationships[relationship].data.id === include.id &&
item.relationships[relationship].data.type === include.type
));
item.relationships[relationship] = Object.assign(item.relationships[relationship], relationshipToAttach[0]);
});
return item;
})
);
});
}

};
1 change: 0 additions & 1 deletion test/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ test.cb('Waterwheel Browser', t => {
t.end();
})
.catch(err => {
console.log(err);
t.end();
});
});
Expand Down
1 change: 0 additions & 1 deletion test/entity.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ test('GET', t => {
}, new t.context.oauth(t.context.baseURL, t.context.oauthOptions));

const Entity = rs.require('../lib/entity');
console.log(new Entity(t.context.options, request).options.methods.hasOwnProperty('get'));
return new Entity(t.context.options, request).get(1)
.then(res => {
t.deepEqual({
Expand Down