@faasjs/request 0.0.3-beta.99 → 0.0.4-beta.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 CHANGED
@@ -54,7 +54,8 @@ ___
54
54
  | `agent?` | `boolean` | - |
55
55
  | `auth?` | `string` | The authentication credentials to use for the request. Format: `username:password` |
56
56
  | `body?` | { `[key: string]`: `any`; } \| `string` | - |
57
- | `downloadStream?` | `NodeJS.WritableStream` | Create a write stream to download a file. ```ts const stream = createWriteStream('filepath') await request('https://example.com', { downloadStream: stream }) ``` |
57
+ | `downloadFile?` | `string` | Path of downloading a file from the server. ```ts await request('https://example.com', { downloadFile: 'filepath' }) ``` |
58
+ | `downloadStream?` | `NodeJS.WritableStream` | Create a write stream to download a file. ```ts import { createWriteStream } from 'fs' const stream = createWriteStream('filepath') await request('https://example.com', { downloadStream: stream }) ``` |
58
59
  | `file?` | `string` | Path of uploading a file to the server. ```ts await request('https://example.com', { file: 'filepath' }) ``` |
59
60
  | `headers?` | `http.OutgoingHttpHeaders` | - |
60
61
  | `logger?` | `Logger` | - |
@@ -111,10 +112,6 @@ ___
111
112
 
112
113
  Request
113
114
 
114
- **`Url`**
115
-
116
- https://faasjs.com/doc/request.html
117
-
118
115
  #### Type parameters
119
116
 
120
117
  | Name | Type |
@@ -132,6 +129,10 @@ https://faasjs.com/doc/request.html
132
129
 
133
130
  `Promise`<[`Response`](#response)<`T`\>\>
134
131
 
132
+ **`Url`**
133
+
134
+ https://faasjs.com/doc/request.html
135
+
135
136
  ___
136
137
 
137
138
  ### setMock
@@ -140,12 +141,6 @@ ___
140
141
 
141
142
  Mock requests
142
143
 
143
- **`Example`**
144
-
145
- ```ts
146
- setMock(async (url, options) => Promise.resolve({ headers: {}, statusCode: 200, body: { data: 'ok' } }))
147
- ```
148
-
149
144
  #### Parameters
150
145
 
151
146
  | Name | Type | Description |
@@ -155,3 +150,9 @@ setMock(async (url, options) => Promise.resolve({ headers: {}, statusCode: 200,
155
150
  #### Returns
156
151
 
157
152
  `void`
153
+
154
+ **`Example`**
155
+
156
+ ```ts
157
+ setMock(async (url, options) => Promise.resolve({ headers: {}, statusCode: 200, body: { data: 'ok' } }))
158
+ ```
package/dist/index.d.mts CHANGED
@@ -49,11 +49,21 @@ type RequestOptions = {
49
49
  * Create a write stream to download a file.
50
50
  *
51
51
  * ```ts
52
+ * import { createWriteStream } from 'fs'
53
+ *
52
54
  * const stream = createWriteStream('filepath')
53
55
  * await request('https://example.com', { downloadStream: stream })
54
56
  * ```
55
57
  */
56
58
  downloadStream?: NodeJS.WritableStream;
59
+ /**
60
+ * Path of downloading a file from the server.
61
+ *
62
+ * ```ts
63
+ * await request('https://example.com', { downloadFile: 'filepath' })
64
+ * ```
65
+ */
66
+ downloadFile?: string;
57
67
  pfx?: Buffer;
58
68
  passphrase?: string;
59
69
  agent?: boolean;
@@ -83,6 +93,7 @@ declare function querystringify(obj: any): string;
83
93
  * @param {string=} options.auth Auth, format: user:password
84
94
  * @param {string=} options.file Upload file path
85
95
  * @param {WritableStream=} options.downloadStream Download stream
96
+ * @param {string=} options.downloadFile Download to file
86
97
  * @param {Buffer=} options.pfx pfx
87
98
  * @param {string=} options.passphrase passphrase
88
99
  * @param {boolean=} options.agent agent
@@ -91,6 +102,6 @@ declare function querystringify(obj: any): string;
91
102
  * @returns {promise}
92
103
  * @url https://faasjs.com/doc/request.html
93
104
  */
94
- declare function request<T = any>(url: string, { headers, method, query, body, timeout, auth, file, downloadStream, pfx, passphrase, agent, parse, logger, }?: RequestOptions): Promise<Response<T>>;
105
+ declare function request<T = any>(url: string, { headers, method, query, body, timeout, auth, file, downloadStream, downloadFile, pfx, passphrase, agent, parse, logger, }?: RequestOptions): Promise<Response<T>>;
95
106
 
96
107
  export { Request, RequestOptions, Response, querystringify, request, setMock };
package/dist/index.d.ts CHANGED
@@ -49,11 +49,21 @@ type RequestOptions = {
49
49
  * Create a write stream to download a file.
50
50
  *
51
51
  * ```ts
52
+ * import { createWriteStream } from 'fs'
53
+ *
52
54
  * const stream = createWriteStream('filepath')
53
55
  * await request('https://example.com', { downloadStream: stream })
54
56
  * ```
55
57
  */
56
58
  downloadStream?: NodeJS.WritableStream;
59
+ /**
60
+ * Path of downloading a file from the server.
61
+ *
62
+ * ```ts
63
+ * await request('https://example.com', { downloadFile: 'filepath' })
64
+ * ```
65
+ */
66
+ downloadFile?: string;
57
67
  pfx?: Buffer;
58
68
  passphrase?: string;
59
69
  agent?: boolean;
@@ -83,6 +93,7 @@ declare function querystringify(obj: any): string;
83
93
  * @param {string=} options.auth Auth, format: user:password
84
94
  * @param {string=} options.file Upload file path
85
95
  * @param {WritableStream=} options.downloadStream Download stream
96
+ * @param {string=} options.downloadFile Download to file
86
97
  * @param {Buffer=} options.pfx pfx
87
98
  * @param {string=} options.passphrase passphrase
88
99
  * @param {boolean=} options.agent agent
@@ -91,6 +102,6 @@ declare function querystringify(obj: any): string;
91
102
  * @returns {promise}
92
103
  * @url https://faasjs.com/doc/request.html
93
104
  */
94
- declare function request<T = any>(url: string, { headers, method, query, body, timeout, auth, file, downloadStream, pfx, passphrase, agent, parse, logger, }?: RequestOptions): Promise<Response<T>>;
105
+ declare function request<T = any>(url: string, { headers, method, query, body, timeout, auth, file, downloadStream, downloadFile, pfx, passphrase, agent, parse, logger, }?: RequestOptions): Promise<Response<T>>;
95
106
 
96
107
  export { Request, RequestOptions, Response, querystringify, request, setMock };
package/dist/index.js CHANGED
@@ -47,7 +47,7 @@ function querystringify(obj) {
47
47
  value = encodeURIComponent(value);
48
48
  if (key === null || value === null)
49
49
  continue;
50
- pairs.push(key + "=" + value);
50
+ pairs.push(`${key}=${value}`);
51
51
  }
52
52
  }
53
53
  return pairs.length ? pairs.join("&") : "";
@@ -61,12 +61,14 @@ async function request(url$1, {
61
61
  auth,
62
62
  file,
63
63
  downloadStream,
64
+ downloadFile,
64
65
  pfx,
65
66
  passphrase,
66
67
  agent,
67
68
  parse,
68
69
  logger: logger$1
69
70
  } = { headers: {} }) {
71
+ var _a;
70
72
  if (!logger$1)
71
73
  logger$1 = new logger.Logger("request");
72
74
  if (mock) {
@@ -99,7 +101,7 @@ async function request(url$1, {
99
101
  host: uri.host ? uri.host.replace(/:[0-9]+$/, "") : uri.host,
100
102
  method: method ? method.toUpperCase() : "GET",
101
103
  path: uri.pathname + uri.search,
102
- port: uri.port,
104
+ port: uri.port || (uri.protocol === "https:" ? "443" : "80"),
103
105
  timeout,
104
106
  auth,
105
107
  pfx,
@@ -110,7 +112,7 @@ async function request(url$1, {
110
112
  if (typeof headers[key] !== "undefined" && headers[key] !== null)
111
113
  options.headers[key] = headers[key];
112
114
  if (body && typeof body !== "string")
113
- if (options.headers["Content-Type"] && options.headers["Content-Type"].toString().includes("application/x-www-form-urlencoded"))
115
+ if ((_a = options.headers["Content-Type"]) == null ? void 0 : _a.toString().includes("application/x-www-form-urlencoded"))
114
116
  body = querystringify(body);
115
117
  else
116
118
  body = JSON.stringify(body);
@@ -124,9 +126,11 @@ async function request(url$1, {
124
126
  const req = protocol.request(options, function(res) {
125
127
  if (downloadStream) {
126
128
  res.pipe(downloadStream);
127
- downloadStream.on("finish", function() {
128
- resolve(void 0);
129
- });
129
+ downloadStream.on("finish", () => resolve(void 0));
130
+ } else if (downloadFile) {
131
+ const stream = fs.createWriteStream(downloadFile);
132
+ res.pipe(stream);
133
+ stream.on("finish", () => resolve(void 0));
130
134
  } else {
131
135
  const raw = [];
132
136
  res.on("data", (chunk) => {
@@ -134,7 +138,13 @@ async function request(url$1, {
134
138
  });
135
139
  res.on("end", () => {
136
140
  const data = Buffer.concat(raw).toString();
137
- logger$1.timeEnd(url$1, "response %s %s %s", res.statusCode, res.headers["content-type"], data);
141
+ logger$1.timeEnd(
142
+ url$1,
143
+ "response %s %s %s",
144
+ res.statusCode,
145
+ res.headers["content-type"],
146
+ data
147
+ );
138
148
  const response = res.statusCode >= 200 && res.statusCode < 400 ? /* @__PURE__ */ Object.create(null) : new Error();
139
149
  response.request = options;
140
150
  response.request.body = body;
@@ -166,13 +176,17 @@ async function request(url$1, {
166
176
  const crlf = "\r\n";
167
177
  const boundary = `--${Math.random().toString(16)}`;
168
178
  const delimiter = `${crlf}--${boundary}`;
169
- const headers2 = [`Content-Disposition: form-data; name="file"; filename="${path.basename(file)}"${crlf}`];
179
+ const headers2 = [
180
+ `Content-Disposition: form-data; name="file"; filename="${path.basename(
181
+ file
182
+ )}"${crlf}`
183
+ ];
170
184
  const multipartBody = Buffer.concat([
171
185
  Buffer.from(delimiter + crlf + headers2.join("") + crlf),
172
186
  fs.readFileSync(file),
173
187
  Buffer.from(`${delimiter}--`)
174
188
  ]);
175
- req.setHeader("Content-Type", "multipart/form-data; boundary=" + boundary);
189
+ req.setHeader("Content-Type", `multipart/form-data; boundary=${boundary}`);
176
190
  req.setHeader("Content-Length", multipartBody.length);
177
191
  req.write(multipartBody);
178
192
  }
package/dist/index.mjs CHANGED
@@ -1,7 +1,7 @@
1
1
  import * as http from 'http';
2
2
  import * as https from 'https';
3
3
  import { URL } from 'url';
4
- import { readFileSync } from 'fs';
4
+ import { createWriteStream, readFileSync } from 'fs';
5
5
  import { basename } from 'path';
6
6
  import { Logger } from '@faasjs/logger';
7
7
 
@@ -24,7 +24,7 @@ function querystringify(obj) {
24
24
  value = encodeURIComponent(value);
25
25
  if (key === null || value === null)
26
26
  continue;
27
- pairs.push(key + "=" + value);
27
+ pairs.push(`${key}=${value}`);
28
28
  }
29
29
  }
30
30
  return pairs.length ? pairs.join("&") : "";
@@ -38,12 +38,14 @@ async function request(url, {
38
38
  auth,
39
39
  file,
40
40
  downloadStream,
41
+ downloadFile,
41
42
  pfx,
42
43
  passphrase,
43
44
  agent,
44
45
  parse,
45
46
  logger
46
47
  } = { headers: {} }) {
48
+ var _a;
47
49
  if (!logger)
48
50
  logger = new Logger("request");
49
51
  if (mock) {
@@ -76,7 +78,7 @@ async function request(url, {
76
78
  host: uri.host ? uri.host.replace(/:[0-9]+$/, "") : uri.host,
77
79
  method: method ? method.toUpperCase() : "GET",
78
80
  path: uri.pathname + uri.search,
79
- port: uri.port,
81
+ port: uri.port || (uri.protocol === "https:" ? "443" : "80"),
80
82
  timeout,
81
83
  auth,
82
84
  pfx,
@@ -87,7 +89,7 @@ async function request(url, {
87
89
  if (typeof headers[key] !== "undefined" && headers[key] !== null)
88
90
  options.headers[key] = headers[key];
89
91
  if (body && typeof body !== "string")
90
- if (options.headers["Content-Type"] && options.headers["Content-Type"].toString().includes("application/x-www-form-urlencoded"))
92
+ if ((_a = options.headers["Content-Type"]) == null ? void 0 : _a.toString().includes("application/x-www-form-urlencoded"))
91
93
  body = querystringify(body);
92
94
  else
93
95
  body = JSON.stringify(body);
@@ -101,9 +103,11 @@ async function request(url, {
101
103
  const req = protocol.request(options, function(res) {
102
104
  if (downloadStream) {
103
105
  res.pipe(downloadStream);
104
- downloadStream.on("finish", function() {
105
- resolve(void 0);
106
- });
106
+ downloadStream.on("finish", () => resolve(void 0));
107
+ } else if (downloadFile) {
108
+ const stream = createWriteStream(downloadFile);
109
+ res.pipe(stream);
110
+ stream.on("finish", () => resolve(void 0));
107
111
  } else {
108
112
  const raw = [];
109
113
  res.on("data", (chunk) => {
@@ -111,7 +115,13 @@ async function request(url, {
111
115
  });
112
116
  res.on("end", () => {
113
117
  const data = Buffer.concat(raw).toString();
114
- logger.timeEnd(url, "response %s %s %s", res.statusCode, res.headers["content-type"], data);
118
+ logger.timeEnd(
119
+ url,
120
+ "response %s %s %s",
121
+ res.statusCode,
122
+ res.headers["content-type"],
123
+ data
124
+ );
115
125
  const response = res.statusCode >= 200 && res.statusCode < 400 ? /* @__PURE__ */ Object.create(null) : new Error();
116
126
  response.request = options;
117
127
  response.request.body = body;
@@ -143,13 +153,17 @@ async function request(url, {
143
153
  const crlf = "\r\n";
144
154
  const boundary = `--${Math.random().toString(16)}`;
145
155
  const delimiter = `${crlf}--${boundary}`;
146
- const headers2 = [`Content-Disposition: form-data; name="file"; filename="${basename(file)}"${crlf}`];
156
+ const headers2 = [
157
+ `Content-Disposition: form-data; name="file"; filename="${basename(
158
+ file
159
+ )}"${crlf}`
160
+ ];
147
161
  const multipartBody = Buffer.concat([
148
162
  Buffer.from(delimiter + crlf + headers2.join("") + crlf),
149
163
  readFileSync(file),
150
164
  Buffer.from(`${delimiter}--`)
151
165
  ]);
152
- req.setHeader("Content-Type", "multipart/form-data; boundary=" + boundary);
166
+ req.setHeader("Content-Type", `multipart/form-data; boundary=${boundary}`);
153
167
  req.setHeader("Content-Length", multipartBody.length);
154
168
  req.write(multipartBody);
155
169
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@faasjs/request",
3
- "version": "0.0.3-beta.99",
3
+ "version": "0.0.4-beta.1",
4
4
  "license": "MIT",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -20,11 +20,14 @@
20
20
  "files": [
21
21
  "dist"
22
22
  ],
23
- "dependencies": {
24
- "@faasjs/logger": "0.0.3-beta.99"
23
+ "peerDependencies": {
24
+ "@faasjs/logger": "0.0.3-beta.109"
25
+ },
26
+ "devDependencies": {
27
+ "@faasjs/logger": "0.0.4-beta.1"
25
28
  },
26
29
  "engines": {
27
- "npm": ">=8.0.0",
28
- "node": ">=16.0.0"
30
+ "npm": ">=9.0.0",
31
+ "node": ">=18.0.0"
29
32
  }
30
33
  }