forked from dev-drprasad/delete-older-releases
-
Notifications
You must be signed in to change notification settings - Fork 1
/
fetch.js
36 lines (30 loc) · 808 Bytes
/
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
const https = require("https");
module.exports = function fetch(options, data) {
return new Promise(function (resolve, reject) {
const req = https.request(options, function (res) {
let data = "";
res.on("data", function (chunk) {
data += chunk;
});
res.on("end", function () {
let body = undefined;
try {
body = data ? JSON.parse(data) : undefined;
} catch (e) {
return reject(e);
}
if (res.statusCode < 200 || res.statusCode >= 300) {
return reject(body ? new Error(body.message) : res.statusMessage);
}
return resolve(body);
});
});
req.on("error", function (err) {
reject(err);
});
if (data) {
req.write(data);
}
req.end();
});
};