binhend 2.1.23 → 2.1.24
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/https.js +78 -77
package/package.json
CHANGED
package/src/https.js
CHANGED
|
@@ -1,91 +1,92 @@
|
|
|
1
|
-
// @ts-nocheck
|
|
2
1
|
|
|
3
2
|
const https = require('https');
|
|
4
3
|
|
|
5
4
|
function HTTPS(url) {
|
|
6
5
|
this.url = url;
|
|
7
6
|
this.parameters = [];
|
|
7
|
+
};
|
|
8
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
|
-
res.on('end', () => {
|
|
35
|
-
var response;
|
|
36
|
-
|
|
37
|
-
try {
|
|
38
|
-
response = JSON.parse(data);
|
|
39
|
-
}
|
|
40
|
-
catch(error) {
|
|
41
|
-
response = data;
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
if (callback instanceof Function) {
|
|
45
|
-
callback(response);
|
|
46
|
-
}
|
|
47
|
-
});
|
|
9
|
+
HTTPS.prototype.get = function(callback) {
|
|
10
|
+
return this.request('GET', callback);
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
HTTPS.prototype.post = function(callback) {
|
|
14
|
+
return this.request('POST', callback);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
HTTPS.prototype.request = function(method, callback) {
|
|
18
|
+
|
|
19
|
+
var URL = this.url + this.parameters.join('/') + (this.queries || '');
|
|
20
|
+
|
|
21
|
+
var options = {
|
|
22
|
+
method,
|
|
23
|
+
headers: this.headers
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
var req = https.request(URL, options, function(res) {
|
|
27
|
+
res.setEncoding('utf8');
|
|
28
|
+
|
|
29
|
+
var data = '';
|
|
30
|
+
|
|
31
|
+
res.on('data', (chunk) => {
|
|
32
|
+
data += chunk;
|
|
48
33
|
});
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
34
|
+
|
|
35
|
+
res.on('end', () => {
|
|
36
|
+
var response;
|
|
37
|
+
|
|
38
|
+
try {
|
|
39
|
+
response = JSON.parse(data);
|
|
40
|
+
}
|
|
41
|
+
catch(error) {
|
|
42
|
+
response = data;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
if (callback instanceof Function) {
|
|
46
|
+
callback(response);
|
|
47
|
+
}
|
|
52
48
|
});
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
this.header = function(map) {
|
|
64
|
-
this.headers = map;
|
|
65
|
-
return this;
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
this.body = function(map) {
|
|
69
|
-
this.payload = JSON.stringify(map);
|
|
70
|
-
return this;
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
req.on('error', (err) => {
|
|
52
|
+
console.error(`[BINHEND][HTTPS] Error request: ${method} ${URL}`);
|
|
53
|
+
console.error(`[BINHEND][HTTPS] --- Error message: ${err.message}`);
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
if (this.payload) {
|
|
57
|
+
req.write(JSON.stringify(this.payload));
|
|
71
58
|
}
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
59
|
+
|
|
60
|
+
req.end();
|
|
61
|
+
|
|
62
|
+
return this;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
HTTPS.prototype.header = function(map) {
|
|
66
|
+
this.headers = map;
|
|
67
|
+
return this;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
HTTPS.prototype.body = function(map) {
|
|
71
|
+
this.payload = JSON.stringify(map);
|
|
72
|
+
return this;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
HTTPS.prototype.parameter = function(array) {
|
|
76
|
+
if (array instanceof Array) {
|
|
77
|
+
var lastChar = this.url[this.url.length - 1];
|
|
78
|
+
this.parameters = (lastChar === '/' ? [] : ['']).concat(array);
|
|
79
79
|
}
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
return this;
|
|
80
|
+
return this;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
HTTPS.prototype.query = function(map) {
|
|
84
|
+
var queryString = '';
|
|
85
|
+
for (var key in map) {
|
|
86
|
+
queryString += key + '=' + map[key] + '&';
|
|
88
87
|
}
|
|
89
|
-
|
|
88
|
+
this.queries = '?' + queryString;
|
|
89
|
+
return this;
|
|
90
|
+
}
|
|
90
91
|
|
|
91
92
|
module.exports = { HTTPS };
|