-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathrequest.js
38 lines (36 loc) · 1.09 KB
/
request.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
// Copyright © 2020 Jan Keromnes.
// The following code is covered by the MIT license.
const http = require('http');
const https = require('https');
module.exports = async (method, url, data) => {
const protocols = { http, https };
const client = url.includes('://') ? protocols[url.split('://')[0]] : http;
if (!client) {
throw new Error(`Unsupported protocol: ${url}`);
}
return new Promise((resolve, reject) => {
const req = client.request(url, { method }, res => {
if (res.statusCode >= 400) {
reject(new Error(`Couldn't ${method} ${url} - Response status: ${res.statusCode}`));
return;
}
let body = '';
res.on('data', chunk => { body += chunk; });
res.on('error', error => { reject(error); });
res.on('end', () => {
try {
resolve(JSON.parse(body));
} catch (_) {
resolve(body);
}
});
}).on('error', error => {
reject(error);
});
req.on('error', error => { reject(error); });
if (data) {
req.write(JSON.stringify(data, null, 2));
}
req.end();
});
};