binhend 2.1.19 → 2.1.21
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 +76 -79
package/package.json
CHANGED
package/src/https.js
CHANGED
|
@@ -5,90 +5,87 @@ const https = require('https');
|
|
|
5
5
|
function HTTPS(url) {
|
|
6
6
|
this.url = url;
|
|
7
7
|
this.parameters = [];
|
|
8
|
-
};
|
|
9
|
-
|
|
10
|
-
var prototype = HTTPS.prototype;
|
|
11
|
-
|
|
12
|
-
prototype.get = function(callback) {
|
|
13
|
-
return this.request('GET', callback);
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
prototype.post = function(callback) {
|
|
17
|
-
return this.request('POST', callback);
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
prototype.request = function(method, callback) {
|
|
21
|
-
|
|
22
|
-
var URL = this.url + this.parameters.join('/') + (this.queries || '');
|
|
23
8
|
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
9
|
+
this.get = function(callback) {
|
|
10
|
+
return this.request('GET', callback);
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
this.post = function(callback) {
|
|
14
|
+
return this.request('POST', callback);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
this.request = function(method, callback) {
|
|
18
|
+
var URL = this.url + this.parameters.join('/') + (this.queries || '');
|
|
19
|
+
|
|
20
|
+
var options = {
|
|
21
|
+
method,
|
|
22
|
+
headers: this.headers
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
var req = https.request(URL, options, function(res) {
|
|
26
|
+
res.setEncoding('utf8');
|
|
27
|
+
|
|
28
|
+
var data = '';
|
|
29
|
+
|
|
30
|
+
res.on('data', (chunk) => {
|
|
31
|
+
data += chunk;
|
|
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
|
+
});
|
|
36
48
|
});
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
try {
|
|
42
|
-
response = JSON.parse(data);
|
|
43
|
-
}
|
|
44
|
-
catch(error) {
|
|
45
|
-
response = data;
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
if (callback instanceof Function) {
|
|
49
|
-
callback(response);
|
|
50
|
-
}
|
|
49
|
+
|
|
50
|
+
req.on("error", (err) => {
|
|
51
|
+
console.log("HTTPS request encounters error:" + err.message);
|
|
51
52
|
});
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
53
|
+
|
|
54
|
+
if (this.payload) {
|
|
55
|
+
req.write(JSON.stringify(this.payload));
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
req.end();
|
|
59
|
+
|
|
60
|
+
return this;
|
|
60
61
|
}
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
prototype.header = function(map) {
|
|
68
|
-
this.headers = map;
|
|
69
|
-
return this;
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
prototype.body = function(map) {
|
|
73
|
-
this.payload = JSON.stringify(map);
|
|
74
|
-
return this;
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
prototype.parameter = function(array) {
|
|
78
|
-
if (array instanceof Array) {
|
|
79
|
-
var last_char = this.url[this.url.length - 1];
|
|
80
|
-
this.parameters = (last_char === '/' ? [] : ['']).concat(array);
|
|
62
|
+
|
|
63
|
+
this.header = function(map) {
|
|
64
|
+
this.headers = map;
|
|
65
|
+
return this;
|
|
81
66
|
}
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
var query_string = '';
|
|
87
|
-
for (var key in map) {
|
|
88
|
-
query_string += key + '=' + map[key] + '&';
|
|
67
|
+
|
|
68
|
+
this.body = function(map) {
|
|
69
|
+
this.payload = JSON.stringify(map);
|
|
70
|
+
return this;
|
|
89
71
|
}
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
72
|
+
|
|
73
|
+
this.parameter = function(array) {
|
|
74
|
+
if (array instanceof Array) {
|
|
75
|
+
var last_char = this.url[this.url.length - 1];
|
|
76
|
+
this.parameters = (last_char === '/' ? [] : ['']).concat(array);
|
|
77
|
+
}
|
|
78
|
+
return this;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
this.query = function(map) {
|
|
82
|
+
var query_string = '';
|
|
83
|
+
for (var key in map) {
|
|
84
|
+
query_string += key + '=' + map[key] + '&';
|
|
85
|
+
}
|
|
86
|
+
this.queries = '?' + query_string;
|
|
87
|
+
return this;
|
|
88
|
+
}
|
|
89
|
+
};
|
|
93
90
|
|
|
94
91
|
module.exports = { HTTPS };
|