@jnode/request 1.0.5 → 1.1.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/README.md +48 -1
- package/package.json +1 -1
- package/src/index.js +81 -3
package/README.md
CHANGED
|
@@ -13,7 +13,7 @@ npm install @jnode/request
|
|
|
13
13
|
### Import JustRequest
|
|
14
14
|
|
|
15
15
|
```javascript
|
|
16
|
-
const { request, generateMultipartBody, RequestResponse } = require('@jnode/request');
|
|
16
|
+
const { request, multipartRequest, generateMultipartBody, RequestResponse } = require('@jnode/request');
|
|
17
17
|
```
|
|
18
18
|
|
|
19
19
|
### Making a request
|
|
@@ -85,6 +85,35 @@ async function uploadFile() {
|
|
|
85
85
|
uploadFile();
|
|
86
86
|
```
|
|
87
87
|
|
|
88
|
+
### Making a multipart request
|
|
89
|
+
```javascript
|
|
90
|
+
const fs = require('fs');
|
|
91
|
+
|
|
92
|
+
async function uploadFile() {
|
|
93
|
+
const fileStream = fs.createReadStream('./example.txt');
|
|
94
|
+
const parts = [{
|
|
95
|
+
disposition: 'form-data; name="file"; filename="example.txt"',
|
|
96
|
+
stream: fileStream
|
|
97
|
+
}, {
|
|
98
|
+
disposition: 'form-data; name="key"',
|
|
99
|
+
data: 'value'
|
|
100
|
+
}];
|
|
101
|
+
|
|
102
|
+
const headers = {
|
|
103
|
+
'X-Custom-Header': 'custom value'
|
|
104
|
+
};
|
|
105
|
+
try {
|
|
106
|
+
const response = await multipartRequest('POST', 'https://example.com/api/upload', headers, parts);
|
|
107
|
+
console.log('Status Code:', response.statusCode);
|
|
108
|
+
console.log('Response:', response.text());
|
|
109
|
+
} catch (error) {
|
|
110
|
+
console.error('Request failed:', error);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
uploadFile();
|
|
115
|
+
```
|
|
116
|
+
|
|
88
117
|
## Functions
|
|
89
118
|
|
|
90
119
|
### `request(method, url, headers = {}, body)`
|
|
@@ -98,6 +127,24 @@ Makes an HTTP(S) request.
|
|
|
98
127
|
|
|
99
128
|
**Returns:** A Promise that resolves to a `RequestResponse` object.
|
|
100
129
|
|
|
130
|
+
### `multipartRequest(method, url, headers, parts, options)`
|
|
131
|
+
|
|
132
|
+
Makes an HTTP(S) multipart/form-data request.
|
|
133
|
+
|
|
134
|
+
- `method`: HTTP method (`POST`, `PUT`, etc.).
|
|
135
|
+
- `url`: The request URL.
|
|
136
|
+
- `headers`: An object containing request headers.
|
|
137
|
+
- `parts`: An array of objects, where each object represents a part of the form data.
|
|
138
|
+
- `disposition` (Optional): Content disposition.
|
|
139
|
+
- `type` (Optional): Content type of the data. Defaults to `application/octet-stream`.
|
|
140
|
+
- `data`: Data (string or Buffer) of this part.
|
|
141
|
+
- `file` (Optional): Path to a local file for streaming.
|
|
142
|
+
- `base64` (Optional): base64 encoded data string.
|
|
143
|
+
- `stream` (Optional): Any readable stream.
|
|
144
|
+
- `options`: Other options.
|
|
145
|
+
|
|
146
|
+
**Returns:** A Promise that resolves to a `RequestResponse` object.
|
|
147
|
+
|
|
101
148
|
### `generateMultipartBody(parts = [])`
|
|
102
149
|
|
|
103
150
|
Generates the body for a `multipart/form-data` request.
|
package/package.json
CHANGED
package/src/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/*
|
|
2
2
|
JustRequest
|
|
3
|
-
v.1.
|
|
3
|
+
v.1.1.0
|
|
4
4
|
|
|
5
5
|
Simple HTTP(s) package for Node.js.
|
|
6
6
|
|
|
@@ -10,6 +10,10 @@ by JustNode Dev Team / JustApple
|
|
|
10
10
|
//load node packages
|
|
11
11
|
const http = require('http');
|
|
12
12
|
const https = require('https');
|
|
13
|
+
const fs = require('fs');
|
|
14
|
+
const { URL } = require('url');
|
|
15
|
+
const { pipeline } = require('stream/promises');
|
|
16
|
+
const crypto = require('crypto');
|
|
13
17
|
|
|
14
18
|
//https request in async/await
|
|
15
19
|
function request(method, url, headers = {}, body) {
|
|
@@ -26,7 +30,7 @@ function request(method, url, headers = {}, body) {
|
|
|
26
30
|
headers: headers
|
|
27
31
|
}, (res) => {
|
|
28
32
|
let chunks = [];
|
|
29
|
-
res.on('data',
|
|
33
|
+
res.on('data', chunks.push);
|
|
30
34
|
res.on('end', () => {
|
|
31
35
|
resolve(new RequestResponse(res, Buffer.concat(chunks)));
|
|
32
36
|
});
|
|
@@ -53,6 +57,80 @@ function generateMultipartBody(parts = []) {
|
|
|
53
57
|
return result;
|
|
54
58
|
}
|
|
55
59
|
|
|
60
|
+
//http multipart/form-data request
|
|
61
|
+
async function multipartRequest(method, url, headers, parts, options) {
|
|
62
|
+
return new Promise(async (resolve, reject) => {
|
|
63
|
+
const parsedUrl = new URL(url); //parse url
|
|
64
|
+
const isHttps = parsedUrl.protocol === 'https:'; //check protocol
|
|
65
|
+
const reqModule = isHttps ? https : http; //select module from http and https
|
|
66
|
+
|
|
67
|
+
const boundary = `----WebKitFormBoundary${Date.now().toString(16)}${crypto.randomUUID()}`; //create a random boundary
|
|
68
|
+
headers['Content-Type'] = `multipart/form-data; boundary=${boundary}`; //set header
|
|
69
|
+
|
|
70
|
+
//set options
|
|
71
|
+
options = {
|
|
72
|
+
method,
|
|
73
|
+
headers,
|
|
74
|
+
...options
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
//make a request
|
|
78
|
+
const req = reqModule.request(url, options, (res) => {
|
|
79
|
+
//save buffers
|
|
80
|
+
let chunks = [];
|
|
81
|
+
res.on('data', chunks.push);
|
|
82
|
+
|
|
83
|
+
res.on('end', () => {
|
|
84
|
+
resolve(new RequestResponse(res, Buffer.concat(chunks)));
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
res.on('error', (err) => {
|
|
88
|
+
reject(err);
|
|
89
|
+
});
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
req.on('error', (err) => {
|
|
93
|
+
reject(err);
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
//send every parts
|
|
97
|
+
for (const part of parts) {
|
|
98
|
+
req.write(`--${boundary}\r\n`); //write boundary
|
|
99
|
+
|
|
100
|
+
//write disposition
|
|
101
|
+
if (part.disposition) {
|
|
102
|
+
req.write(`Content-Disposition: ${part.disposition}\r\n`);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
//write content type
|
|
106
|
+
let contentType = part.type ?? 'application/octet-stream';
|
|
107
|
+
req.write(`Content-Type: ${contentType}\r\n`);
|
|
108
|
+
|
|
109
|
+
//send data
|
|
110
|
+
if (part.file) { //local file, using streams
|
|
111
|
+
req.write('\r\n');
|
|
112
|
+
const fileStream = fs.createReadStream(part.file);
|
|
113
|
+
await pipeline(fileStream, req, { end: false }); //pipe stream but not end
|
|
114
|
+
req.write('\r\n');
|
|
115
|
+
} else if (part.data) { //string or buffer data
|
|
116
|
+
req.write('\r\n');
|
|
117
|
+
req.write(part.data + '\r\n');
|
|
118
|
+
} else if (part.base64) { //base64 data
|
|
119
|
+
req.write('Content-Transfer-Encoding: base64\r\n\r\n'); //using base64 encoding
|
|
120
|
+
req.write(part.base64 + '\r\n');
|
|
121
|
+
} else if (part.stream) { //any readable stream
|
|
122
|
+
req.write('\r\n');
|
|
123
|
+
await pipeline(part.stream, req, { end: false });
|
|
124
|
+
req.write('\r\n');
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
//end request
|
|
129
|
+
req.write(`--${boundary}--\r\n`);
|
|
130
|
+
req.end();
|
|
131
|
+
});
|
|
132
|
+
}
|
|
133
|
+
|
|
56
134
|
//request response
|
|
57
135
|
class RequestResponse {
|
|
58
136
|
constructor(res, body) {
|
|
@@ -79,4 +157,4 @@ class RequestResponse {
|
|
|
79
157
|
}
|
|
80
158
|
|
|
81
159
|
//export
|
|
82
|
-
module.exports = { request, generateMultipartBody, RequestResponse };
|
|
160
|
+
module.exports = { request, multipartRequest, generateMultipartBody, RequestResponse };
|