-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrequest.js
More file actions
26 lines (20 loc) · 734 Bytes
/
request.js
File metadata and controls
26 lines (20 loc) · 734 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
const https = require('https');
module.exports = async (url, options = null) => {
return new Promise((resolve, reject) => {
const req = https.get(url, options, res => {
if (res.statusCode === 403) {
return reject(new Error(`Invalid token`));
}
if (res.statusCode < 200 || res.statusCode >= 300) {
return reject(new Error(`Status Code: ${res.statusCode}`));
}
const data = [];
res.on('data', chunk => {
data.push(chunk);
});
res.on('end', () => resolve(JSON.parse(Buffer.concat(data).toString())));
});
req.on('error', reject);
req.end();
});
};