@jnode/request 1.0.0 → 1.0.4

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