@jnode/request 2.0.0 → 2.1.0

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.
@@ -0,0 +1,13 @@
1
+ # Contributing to JNode packages
2
+
3
+ Hello there! Welcome to the contributing guildlines!
4
+
5
+ I was a bit surprised to see you here, especially since we’re not as well-known right now, and our packages are quite straightforward, mainly designed for specific projects.
6
+
7
+ If you’re here, perhaps you’ve spotted some bugs, security issues, or have an idea for something new. We’re happy to welcome any contributions!
8
+
9
+ - **Bugs**: If you find any bugs, please let us know.
10
+ - **Security Problems**: If a bug affects the package’s security, please reach out to JustApple via [email](mailto:ja@justapple.tw) or Discord!
11
+ - **Docs and Tutorials**: This is the part I’m most excited about. The docs are mostly generated by Gemini, so they might not be perfectly clear. Feel free to update them while maintaining the Node.js official doc style.
12
+
13
+ For new features, please always create an issue and wait for our response. Also, please remember to keep the package simple and free from third-party dependencies.
package/README.md CHANGED
@@ -45,18 +45,23 @@ request('POST', 'https://example.com/', [
45
45
  - `url` [\<string\>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#string_type) | [\<URL\>](https://nodejs.org/docs/latest/api/url.html#the-whatwg-url-api) The target URL to request, support both `http` and `https`.
46
46
  - `body` [\<string\>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#string_type) | [\<Buffer\>](https://nodejs.org/docs/latest/api/buffer.html#class-buffer) | [\<Uint8Array\>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array) | [\<stream.Readable\>](https://nodejs.org/docs/latest/api/stream.html#class-streamreadable) | [\<URLSearchParams\>](https://nodejs.org/docs/latest/api/url.html#class-urlsearchparams) | [\<request.FormPart[]\>](#class-requestformpart) | [\<Object[]\>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object) The request body. Using [\<URLSearchParams\>](https://nodejs.org/docs/latest/api/url.html#class-urlsearchparams) will send a `application/x-www-form-urlencoded` request; using [\<request.FormPart[]\>](#class-requestformpart) or [\<Object[]\>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object) will send a `multipart/form-body` request.
47
47
  - `headers` [\<Object\>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object) An object containing request headers. `Content-Length` is automatically calculated for [\<string\>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#string_type), [\<Buffer\>](https://nodejs.org/docs/latest/api/buffer.html#class-buffer), [\<Uint8Array\>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array), or [\<URLSearchParams\>](https://nodejs.org/docs/latest/api/url.html#class-urlsearchparams) body. And `Content-Type` will be force replaced while sending a `multipart/form-body` request with [\<FormPart[]\>](#class-requestformpart) or [\<Object[]\>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object) body.
48
- - `options` [\<Object\>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object) `options` in [`http.request()`](https://nodejs.org/docs/latest/api/http.html#httprequestoptions-callback).
48
+ - `options` [\<Object\>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)
49
+ - `multipart` [\<string\>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#string_type) Multipart type. **Default**: `'form-body'`.
50
+ - `options` in [`http.request()`](https://nodejs.org/docs/latest/api/http.html#httprequestoptions-callback).
49
51
  - Returns: [\<request.RequestResponse\>](#class-requestrequestresponse)
50
52
 
51
53
  Make a HTTP(S) request.
52
54
 
53
55
  ## Class: `request.FormPart`
54
56
 
55
- ### `new request.FormPart(name, body[, headers])`
57
+ ### `new request.FormPart(name, body[, headers, options])`
56
58
 
57
59
  - `name` [\<string\>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#string_type) Name of the part.
58
60
  - `body` [\<string\>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#string_type) | [\<Buffer\>](https://nodejs.org/docs/latest/api/buffer.html#class-buffer) | [\<Uint8Array\>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array) | [\<stream.Readable\>](https://nodejs.org/docs/latest/api/stream.html#class-streamreadable) | [\<URLSearchParams\>](https://nodejs.org/docs/latest/api/url.html#class-urlsearchparams) The body of the part. Using [\<URLSearchParams\>](https://nodejs.org/docs/latest/api/url.html#class-urlsearchparams) will create a `application/x-www-form-urlencoded` part.
59
61
  - `headers` [\<Object\>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object) An object containing part headers. There is a special `filename` field, will be added to `Content-Disposition`.
62
+ - `options` [\<Object\>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)
63
+ - `filename` [\<string\>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#string_type) File name.
64
+ - `disposition` [\<string\>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#string_type) Part disposition. **Default**: `'form-data'`.
60
65
 
61
66
  ## Class: `request.RequestResponse`
62
67
 
@@ -84,7 +89,7 @@ The response HTTP headers.
84
89
 
85
90
  - Type: [\<Buffer\>](https://nodejs.org/docs/latest/api/buffer.html#class-buffer)
86
91
 
87
- > **Notice**: You should avoid reading this property directly, use [`requestRespond.buffer()`](#requestrespondbuffer) instead.
92
+ > **Notice**: You should avoid reading this property directly, use [`requestRespond.buffer()`](#requestresponsebuffer) instead.
88
93
 
89
94
  ### `requestResponse.buffer()`
90
95
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jnode/request",
3
- "version": "2.0.0",
3
+ "version": "2.1.0",
4
4
  "description": "Simple HTTP(s) requesting package for Node.js.",
5
5
  "main": "src/index.js",
6
6
  "scripts": {
package/src/index.js CHANGED
@@ -30,7 +30,7 @@ function request(method = 'GET', url = '', body, headers = {}, options = {}) {
30
30
  });
31
31
 
32
32
  // write headers and body
33
- if (typeof body === 'string' || Buffer.isBuffer(body) || body instanceof Uint8Array) { // static body
33
+ if (typeof body === 'string' || body instanceof Uint8Array) { // static body
34
34
  req.setHeader('Content-Length', Buffer.byteLength(body));
35
35
  req.end(body);
36
36
  } else if (body instanceof stream.Readable) { // stream
@@ -38,7 +38,7 @@ function request(method = 'GET', url = '', body, headers = {}, options = {}) {
38
38
  } else if (Array.isArray(body)) { // multipart/form-body
39
39
  // generate boundary
40
40
  const boundary = `----WebKitFormBoundary${Date.now().toString(16)}${crypto.randomUUID()}`;
41
- req.setHeader('Content-Type', `multipart/form-data; boundary=${boundary}`);
41
+ req.setHeader('Content-Type', `multipart/${options.multipart || 'form-data'}; boundary=${boundary}`);
42
42
 
43
43
  // write every parts
44
44
  for (const part of body) {
@@ -48,12 +48,12 @@ function request(method = 'GET', url = '', body, headers = {}, options = {}) {
48
48
  const partHeaders = part.headers || {};
49
49
 
50
50
  // set Content-Disposition header
51
- if (!partHeaders['Content-Disposition']) {
52
- partHeaders['Content-Disposition'] = `form-data; name="${part.name}"${part.filename ? `; filename="${part.filename}"` : ''}`;
51
+ if (!req.getHeader('content-disposition') && (part.name || part.filename || part.disposition)) {
52
+ partHeaders['Content-Disposition'] = (part.disposition || 'form-data') + (part.name ? `; name="${part.name}"` : '') + (part.filename ? `; filename="${part.filename}"` : '');
53
53
  }
54
54
 
55
55
  // write headers and body
56
- if (typeof part.body === 'string' || Buffer.isBuffer(part.body) || part.body instanceof Uint8Array) { // static body
56
+ if (typeof part.body === 'string' || part.body instanceof Uint8Array) { // static body
57
57
  for (const [key, value] of Object.entries(partHeaders)) {
58
58
  req.write(`${key}: ${value}\r\n`);
59
59
  }
@@ -107,10 +107,12 @@ function request(method = 'GET', url = '', body, headers = {}, options = {}) {
107
107
 
108
108
  // form part
109
109
  class FormPart {
110
- constructor(name = '', body, headers = {}) {
110
+ constructor(name = '', body, headers = {}, options = {}) {
111
111
  this.name = name;
112
112
  this.body = body;
113
113
  this.headers = headers;
114
+ this.filename = options.filename;
115
+ this.disposition = options.disposition;
114
116
  }
115
117
  }
116
118