-
Notifications
You must be signed in to change notification settings - Fork 0
/
apiCall.js
50 lines (44 loc) · 947 Bytes
/
apiCall.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
'use strict'
const {
readConfig,
} = require('./config');
exports.apiCall = async (path, method = 'GET', jsonObject) => {
const {
promisify: p
} = require('util');
const request = require('request');
const request_p = p(request);
const {
accessToken
} = await readConfig();
const url = /^https?/.test(path) ? path : `https://idobata.io/api${path}`;
const headers = {
'X-API-Token': accessToken,
'User-Agent': 'idbt',
}
const options = {
url,
method,
headers,
}
if (method === 'POST') {
headers['Content-Type'] = 'application/json'
options.body = JSON.stringify(jsonObject);
}
try {
const res = await request_p(options)
const {
statusCode,
statusMessage,
body
} = res;
if (statusCode >= 400) {
throw new Error(statusMessage);
}
const json = JSON.parse(body);
return json
} catch (e) {
console.log(e);
throw e
}
}