@jnode/request 1.0.1 → 1.0.5
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 +124 -0
- package/package.json +1 -1
- package/src/index.js +10 -3
package/README.md
CHANGED
|
@@ -1,2 +1,126 @@
|
|
|
1
1
|
# JustRequest
|
|
2
2
|
|
|
3
|
+
Simple HTTP(s) package for Node.js.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```shell
|
|
8
|
+
npm install @jnode/request
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
### Import JustRequest
|
|
14
|
+
|
|
15
|
+
```javascript
|
|
16
|
+
const { request, generateMultipartBody, RequestResponse } = require('@jnode/request');
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
### Making a request
|
|
20
|
+
|
|
21
|
+
```javascript
|
|
22
|
+
async function fetchData() {
|
|
23
|
+
try {
|
|
24
|
+
const response = await request('GET', 'https://example.com/api/data');
|
|
25
|
+
console.log('Status Code:', response.statusCode);
|
|
26
|
+
console.log('Body:', response.text()); // Use response.json() if expecting a JSON response
|
|
27
|
+
} catch (error) {
|
|
28
|
+
console.error('Request failed:', error);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
fetchData();
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
### Sending data with the request
|
|
36
|
+
|
|
37
|
+
```javascript
|
|
38
|
+
async function postData() {
|
|
39
|
+
try {
|
|
40
|
+
const headers = {
|
|
41
|
+
'Content-Type': 'application/json'
|
|
42
|
+
};
|
|
43
|
+
const body = JSON.stringify({ key: 'value' });
|
|
44
|
+
const response = await request('POST', 'https://example.com/api/post', headers, body);
|
|
45
|
+
console.log('Status Code:', response.statusCode);
|
|
46
|
+
console.log('Response:', response.text());
|
|
47
|
+
} catch (error) {
|
|
48
|
+
console.error('Request failed:', error);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
postData();
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
### Generating a `multipart/form-data` body
|
|
56
|
+
|
|
57
|
+
```javascript
|
|
58
|
+
const fs = require('fs');
|
|
59
|
+
|
|
60
|
+
async function uploadFile() {
|
|
61
|
+
const fileData = await fs.promises.readFile('./example.txt');
|
|
62
|
+
const parts = [{
|
|
63
|
+
disposition: 'form-data; name="file"; filename="example.txt"',
|
|
64
|
+
contentType: 'text/plain',
|
|
65
|
+
data: fileData
|
|
66
|
+
}, {
|
|
67
|
+
disposition: 'form-data; name="key"',
|
|
68
|
+
data: 'value'
|
|
69
|
+
}];
|
|
70
|
+
const body = generateMultipartBody(parts);
|
|
71
|
+
|
|
72
|
+
const headers = {
|
|
73
|
+
'Content-Type': 'multipart/form-data; boundary=----JustNodeFormBoundary'
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
try {
|
|
77
|
+
const response = await request('POST', 'https://example.com/api/upload', headers, body);
|
|
78
|
+
console.log('Status Code:', response.statusCode);
|
|
79
|
+
console.log('Response:', response.text());
|
|
80
|
+
} catch (error) {
|
|
81
|
+
console.error('Request failed:', error);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
uploadFile();
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
## Functions
|
|
89
|
+
|
|
90
|
+
### `request(method, url, headers = {}, body)`
|
|
91
|
+
|
|
92
|
+
Makes an HTTP(S) request.
|
|
93
|
+
|
|
94
|
+
- `method`: HTTP method (`GET`, `POST`, `PUT`, `DELETE`, etc.).
|
|
95
|
+
- `url`: The request URL.
|
|
96
|
+
- `headers`: An object containing request headers. Defaults to an empty object.
|
|
97
|
+
- `body`: The request body (string or Buffer).
|
|
98
|
+
|
|
99
|
+
**Returns:** A Promise that resolves to a `RequestResponse` object.
|
|
100
|
+
|
|
101
|
+
### `generateMultipartBody(parts = [])`
|
|
102
|
+
|
|
103
|
+
Generates the body for a `multipart/form-data` request.
|
|
104
|
+
|
|
105
|
+
- `parts`: An array of objects, where each object represents a part of the form data.
|
|
106
|
+
- `disposition` (Optional): Content disposition.
|
|
107
|
+
- `contentType` (Optional): Content type of the data. Defaults to `application/octet-stream`.
|
|
108
|
+
- `data`: Data (string or Buffer) of this part.
|
|
109
|
+
- `encoded` (Optional): If provided, the data is already base64 encoded, you may skip the default base64 encoding.
|
|
110
|
+
|
|
111
|
+
**Returns:** A string representing the multipart body.
|
|
112
|
+
|
|
113
|
+
## Class `RequestResponse`
|
|
114
|
+
|
|
115
|
+
A class representing the response from an HTTP(S) request.
|
|
116
|
+
|
|
117
|
+
### Properties
|
|
118
|
+
|
|
119
|
+
- `statusCode`: The HTTP status code of the response.
|
|
120
|
+
- `headers`: An object containing the response headers.
|
|
121
|
+
- `body`: The raw response body (Buffer).
|
|
122
|
+
|
|
123
|
+
### Methods
|
|
124
|
+
|
|
125
|
+
- `text(encoding = 'utf8')`: Returns the response body as a string with optional encoding.
|
|
126
|
+
- `json()`: Attempts to parse the response body as JSON. Returns a JSON object or `undefined` if parsing fails.
|
package/package.json
CHANGED
package/src/index.js
CHANGED
|
@@ -7,14 +7,21 @@ Simple HTTP(s) package for Node.js.
|
|
|
7
7
|
by JustNode Dev Team / JustApple
|
|
8
8
|
*/
|
|
9
9
|
|
|
10
|
+
//load node packages
|
|
11
|
+
const http = require('http');
|
|
12
|
+
const https = require('https');
|
|
13
|
+
|
|
10
14
|
//https request in async/await
|
|
11
15
|
function request(method, url, headers = {}, body) {
|
|
12
16
|
return new Promise((resolve, reject) => {
|
|
13
17
|
//auto provide `Content-Length` header
|
|
14
18
|
headers['Content-Length'] = headers['Content-Length'] ?? (body ? Buffer.byteLength(body) : 0);
|
|
15
19
|
|
|
20
|
+
//support http and https
|
|
21
|
+
const protocol = url.startsWith('https://') ? https : http;
|
|
22
|
+
|
|
16
23
|
//make request
|
|
17
|
-
|
|
24
|
+
protocol.request(url, {
|
|
18
25
|
method: method,
|
|
19
26
|
headers: headers
|
|
20
27
|
}, (res) => {
|
|
@@ -58,13 +65,13 @@ class RequestResponse {
|
|
|
58
65
|
|
|
59
66
|
//to string
|
|
60
67
|
text(encoding) {
|
|
61
|
-
return this.body.toString(encoding);
|
|
68
|
+
return this._text ?? (this._text = this.body.toString(encoding));
|
|
62
69
|
}
|
|
63
70
|
|
|
64
71
|
//to json
|
|
65
72
|
json(encoding) {
|
|
66
73
|
try {
|
|
67
|
-
return JSON.parse(this.body);
|
|
74
|
+
return this._json ?? (this._json = JSON.parse(this.body));
|
|
68
75
|
} catch (err) {
|
|
69
76
|
return undefined;
|
|
70
77
|
}
|