-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
61 lines (48 loc) · 1.7 KB
/
index.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
58
59
60
61
const https = require('https');
/**
* A https.request wrapper function.
* @param {object} httpsOptions
* @param {(string|object)} [payload]
* @returns {Promise.<{ response: object, data: string }, any>}
*/
module.exports = function (httpsOptions, payload) {
return new Promise((resolve, reject) => {
const requestOptions = Object.assign({
headers: {},
method: 'GET',
port: 443
}, httpsOptions);
if (payload != null) {
if (typeof payload === 'object') {
if (!requestOptions.headers['Content-Type']) {
requestOptions.headers['Content-Type'] = 'application/json';
}
}
if (typeof payload !== 'string') {
payload = JSON.stringify(payload);
}
requestOptions.headers['Content-Length'] = Buffer.byteLength(payload);
}
let startDate;
const request = https.request(requestOptions, (response) => {
let data = '';
response.on('data', (chunk) => void (data += chunk));
response.on('end', () => {
if (process.env.SHOW_LOG) {
console.log(`\u001b[36mNODE-HTTPS-REQUEST\u001b[0m completed in \x1b[33m${new Date().getTime() - startDate.getTime()}\x1b[0mms -> \x1b[33m${response.statusCode}\x1b[0m status code -> \x1b[33m${Buffer.byteLength(data)}\x1b[0mB data`);
}
resolve({ response, data });
});
});
request.on('error', (error) => void reject(error));
if (payload != null) {
request.write(payload);
}
if (process.env.SHOW_LOG) {
startDate = new Date();
const { host, method, path, port } = requestOptions;
console.log(`\u001b[36mNODE-HTTPS-REQUEST\u001b[0m ${payload ? `\u001b[33m${Buffer.byteLength(payload)}\u001b[0mB payload -> ` : ''}${method} \u001b[32mhttps://${host}:${port}${path}\u001b[0m`);
}
request.end();
});
};