This repository has been archived by the owner on Sep 5, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.js
68 lines (58 loc) · 1.64 KB
/
utils.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
const {logExpression} = require('@cisl/zepto-logger');
const appSettings = require('./appSettings');
const fetch = require('node-fetch');
function constructUrl(parts) {
let url = `${parts.protocol ? parts.protocol : 'http'}://${parts.host ? parts.host : 'localhost'}`;
if (parts.port) {
url += `:${parts.port}`;
}
return url;
}
module.exports.postToService = async (service, route, data) => {
logExpression(`post to ${service} on ${route}`, 2);
logExpression(data, 2);
if (!appSettings.serviceMap[service]) {
throw new Error(`Unrecognized service: ${service}`)
}
const serviceUrl = constructUrl(appSettings.serviceMap[service]);
if (route[0] !== '/') {
route = '/' + route;
}
const res = await fetch(`${serviceUrl}${route}`, {
method: 'post',
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify(data)
});
const body = await res.text();
try {
return JSON.parse(body);
}
catch (exc) {
logExpression('Could not parse response');
logExpression(body);
//throw exc;
return {}
}
};
module.exports.getFromService = async (service, route) => {
logExpression(`get from ${service} on ${route}`, 2);
if (!appSettings.serviceMap[service]) {
throw new Error(`Unrecognized service: ${service}`);
}
const serviceUrl = constructUrl(appSettings.serviceMap[service]);
if (route[0] !== '/') {
route = '/' + route;
}
const res = await fetch(`${serviceUrl}${route}`);
const body = await res.text();
try {
return JSON.parse(body);
}
catch (exc) {
logExpression('Could not parse response', 2);
logExpression(body, 2);
throw exc;
}
}