@jnode/request 1.0.0 → 1.0.1
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/index.js +68 -1
package/package.json
CHANGED
package/src/index.js
CHANGED
|
@@ -5,4 +5,71 @@ v.1.0.0
|
|
|
5
5
|
Simple HTTP(s) package for Node.js.
|
|
6
6
|
|
|
7
7
|
by JustNode Dev Team / JustApple
|
|
8
|
-
*/
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
//https request in async/await
|
|
11
|
+
function request(method, url, headers = {}, body) {
|
|
12
|
+
return new Promise((resolve, reject) => {
|
|
13
|
+
//auto provide `Content-Length` header
|
|
14
|
+
headers['Content-Length'] = headers['Content-Length'] ?? (body ? Buffer.byteLength(body) : 0);
|
|
15
|
+
|
|
16
|
+
//make request
|
|
17
|
+
https.request(url, {
|
|
18
|
+
method: method,
|
|
19
|
+
headers: headers
|
|
20
|
+
}, (res) => {
|
|
21
|
+
let chunks = [];
|
|
22
|
+
res.on('data', (chunk) => chunks.push(chunk));
|
|
23
|
+
res.on('end', () => {
|
|
24
|
+
resolve(new RequestResponse(res, Buffer.concat(chunks)));
|
|
25
|
+
});
|
|
26
|
+
res.on('error', reject);
|
|
27
|
+
}).end(body).on('error', reject);
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
//generate `multipart/form-data` type request body
|
|
32
|
+
function generateMultipartBody(parts = []) {
|
|
33
|
+
//we use `----JustNodeFormBoundary` as boundary, and will encode data with base64
|
|
34
|
+
//every part of `parts` is an object with item `disposition`, `contentType` and `data` (Buffer)
|
|
35
|
+
|
|
36
|
+
let result = '';
|
|
37
|
+
for (let i of parts) { //add every part
|
|
38
|
+
result += '------JustNodeFormBoundary\r\n' +
|
|
39
|
+
'Content-Disposition: form-data' + (i.disposition ? '; ' + i.disposition : '') + '\r\n' +
|
|
40
|
+
'Content-Type: ' + (i.contentType ?? 'application/octet-stream') + '\r\n' +
|
|
41
|
+
'Content-Transfer-Encoding: base64\r\n\r\n' +
|
|
42
|
+
(i.encoded ?? ((typeof i.data === 'string') ? Buffer.from(i.data) : i.data).toString('base64')) + '\r\n';
|
|
43
|
+
}
|
|
44
|
+
result += '------JustNodeFormBoundary--'; //end
|
|
45
|
+
|
|
46
|
+
return result;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
//request response
|
|
50
|
+
class RequestResponse {
|
|
51
|
+
constructor(res, body) {
|
|
52
|
+
this.res = res;
|
|
53
|
+
this.body = body;
|
|
54
|
+
|
|
55
|
+
this.statusCode = res.statusCode;
|
|
56
|
+
this.headers = res.headers;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
//to string
|
|
60
|
+
text(encoding) {
|
|
61
|
+
return this.body.toString(encoding);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
//to json
|
|
65
|
+
json(encoding) {
|
|
66
|
+
try {
|
|
67
|
+
return JSON.parse(this.body);
|
|
68
|
+
} catch (err) {
|
|
69
|
+
return undefined;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
//export
|
|
75
|
+
module.exports = { request, generateMultipartBody, RequestResponse };
|