-
Notifications
You must be signed in to change notification settings - Fork 0
/
Access.JSONPlaceholder.js
39 lines (39 loc) · 1.13 KB
/
Access.JSONPlaceholder.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
var q = require('q');
var request = require('request-promise-any');
var request = request.defaults({ json: true });
var group = require('./group');
module.exports = endpoint => ({
get: ({ id, type, relation }) =>
relation ?
request.get(`${endpoint}/${type}s/${id}/${relation}s`).promise().then(object => [object]) :
id instanceof Array ?
request.get(`${endpoint}/${type}s?${id.map(id => `id=${id}`).join('&')}`).promise() :
request.get(`${endpoint}/${type}s/${id}`).promise().then(object => [object]),
batch: function* (queue) {
var request = group.call(queue, [
request => request.relation || "", [
request => request.type, []
]
]);
for (var relation in request)
for (var type in request[relation])
if (relation)
for (var id in request[relation][type])
yield [
{ id, type, relation },
[request[relation][type][id]]
];
else
yield [
{
id:
request[relation][type].length > 1 ?
request[relation][type].map(request => request.id) :
request[relation][type][0].id,
type,
relation
},
request[relation][type]
];
}
});