-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathfetch.js
57 lines (48 loc) · 1.37 KB
/
fetch.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
const http = require("http");
const https = require("https");
var URL = require('url').URL;
const getOptions = (url, method = 'GET', body = null) => {
const {hostname, port, pathname:path} = new URL(url);
return {
hostname,
port,
path,
method,
// body,
headers: Object.assign({
'Content-Type': 'application/json',
}, body === null ? {} : {'Content-Length': Buffer.byteLength(body)})
};
};
const parseOutput = (resolve, reject, res) => {
let output = ''
res.setEncoding('utf8')
res.on('data', chunk => output += chunk);
res.on('end', () => {
if (res.statusCode !== 200) reject("Api call failed with response code " + res.statusCode);
else resolve(JSON.parse(output));
});
};
const getService = url => url.indexOf('https://') === 0 ? https : http;
const post = (url, json) => {
return new Promise((resolve, reject) => {
let body = JSON.stringify(json);
const options = getOptions(url, 'POST', body);
let req = getService(url).request(options, res => parseOutput(resolve, reject, res));
req.on('error', err => reject(err.message));
req.write(body)
req.end()
})
};
const get = (url) => {
return new Promise((resolve, reject) => {
const options = getOptions(url, 'GET');
let req = getService(url).request(options, res => parseOutput(resolve, reject, res));
req.on('error', err => reject(err.message));
req.end()
})
};
module.exports = {
get,
post
}