-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxy-factory.js
More file actions
35 lines (31 loc) · 883 Bytes
/
proxy-factory.js
File metadata and controls
35 lines (31 loc) · 883 Bytes
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
const rp = require('request-promise');
const utils = require('./utils');
const makeRequest = (key, qs = {}, headers = {}, acc = []) => {
const options = {
method: 'GET',
json: true,
url: `https://public-api.capterra.com/v1/${key}`,
qs,
headers,
};
return rp(options).then(body => {
const data = utils.cleanData(body.data, qs);
if (!data.length) {
return {
data: acc,
total: body.total
};
} else {
return makeRequest(key, { ...qs, scroll_id: body.scroll_id }, headers, [...acc, ...data]);
}
});
};
const proxyFactory = (key) => {
return function (req, res, next) {
const headers = { 'Authorization': req.headers['authorization'] };
makeRequest(key, req.query, headers)
.then((response) => res.json(response))
.catch((error) => next(error));
};
};
module.exports = proxyFactory;