@faasjs/request 0.0.3-beta.8 → 0.0.3-beta.81
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 +3 -3
- package/dist/index.d.ts +9 -0
- package/dist/index.js +11 -3
- package/dist/index.mjs +11 -3
- package/package.json +3 -4
package/README.md
CHANGED
|
@@ -52,10 +52,10 @@ ___
|
|
|
52
52
|
| Name | Type | Description |
|
|
53
53
|
| :------ | :------ | :------ |
|
|
54
54
|
| `agent?` | `boolean` | - |
|
|
55
|
-
| `auth?` | `string` | The authentication credentials to use for the request.
|
|
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. |
|
|
58
|
-
| `file?` | `string` | Path of uploading a file to the server. |
|
|
57
|
+
| `downloadStream?` | `NodeJS.WritableStream` | Create a write stream to download a file. ```ts const stream = createWriteStream('filepath') await request('https://example.com', { downloadStream: stream }) ``` |
|
|
58
|
+
| `file?` | `string` | Path of uploading a file to the server. ```ts await request('https://example.com', { file: 'filepath' }) ``` |
|
|
59
59
|
| `headers?` | `http.OutgoingHttpHeaders` | - |
|
|
60
60
|
| `logger?` | `Logger` | - |
|
|
61
61
|
| `method?` | `string` | The HTTP method to use when making the request. Defaults to GET. |
|
package/dist/index.d.ts
CHANGED
|
@@ -39,10 +39,19 @@ type RequestOptions = {
|
|
|
39
39
|
auth?: string;
|
|
40
40
|
/**
|
|
41
41
|
* Path of uploading a file to the server.
|
|
42
|
+
*
|
|
43
|
+
* ```ts
|
|
44
|
+
* await request('https://example.com', { file: 'filepath' })
|
|
45
|
+
* ```
|
|
42
46
|
*/
|
|
43
47
|
file?: string;
|
|
44
48
|
/**
|
|
45
49
|
* Create a write stream to download a file.
|
|
50
|
+
*
|
|
51
|
+
* ```ts
|
|
52
|
+
* const stream = createWriteStream('filepath')
|
|
53
|
+
* await request('https://example.com', { downloadStream: stream })
|
|
54
|
+
* ```
|
|
46
55
|
*/
|
|
47
56
|
downloadStream?: NodeJS.WritableStream;
|
|
48
57
|
pfx?: Buffer;
|
package/dist/index.js
CHANGED
|
@@ -77,13 +77,20 @@ async function request(url, {
|
|
|
77
77
|
} = { headers: {} }) {
|
|
78
78
|
if (!logger)
|
|
79
79
|
logger = new import_logger.Logger("request");
|
|
80
|
-
if (mock)
|
|
80
|
+
if (mock) {
|
|
81
|
+
logger.debug("mock %s %j", url, {
|
|
82
|
+
headers,
|
|
83
|
+
method,
|
|
84
|
+
query,
|
|
85
|
+
body
|
|
86
|
+
});
|
|
81
87
|
return mock(url, {
|
|
82
88
|
headers,
|
|
83
89
|
method,
|
|
84
90
|
query,
|
|
85
91
|
body
|
|
86
92
|
});
|
|
93
|
+
}
|
|
87
94
|
if (query) {
|
|
88
95
|
if (!url.includes("?"))
|
|
89
96
|
url += "?";
|
|
@@ -136,7 +143,7 @@ async function request(url, {
|
|
|
136
143
|
res.on("end", () => {
|
|
137
144
|
const data = Buffer.concat(raw).toString();
|
|
138
145
|
logger.timeEnd(url, "response %s %s %s", res.statusCode, res.headers["content-type"], data);
|
|
139
|
-
const response = /* @__PURE__ */ Object.create(null);
|
|
146
|
+
const response = res.statusCode >= 200 && res.statusCode < 400 ? /* @__PURE__ */ Object.create(null) : new Error();
|
|
140
147
|
response.request = options;
|
|
141
148
|
response.request.body = body;
|
|
142
149
|
response.statusCode = res.statusCode;
|
|
@@ -145,7 +152,7 @@ async function request(url, {
|
|
|
145
152
|
response.body = data;
|
|
146
153
|
if (response.body && response.headers["content-type"] && response.headers["content-type"].includes("application/json"))
|
|
147
154
|
try {
|
|
148
|
-
response.body = parse
|
|
155
|
+
response.body = (parse || JSON.parse)(response.body);
|
|
149
156
|
logger.debug("response.parse JSON");
|
|
150
157
|
} catch (error) {
|
|
151
158
|
console.warn("response plain body", response.body);
|
|
@@ -155,6 +162,7 @@ async function request(url, {
|
|
|
155
162
|
resolve(response);
|
|
156
163
|
else {
|
|
157
164
|
logger.debug("response.error %j", response);
|
|
165
|
+
response.message = `${res.statusCode} ${res.statusMessage} ${url}`;
|
|
158
166
|
reject(response);
|
|
159
167
|
}
|
|
160
168
|
});
|
package/dist/index.mjs
CHANGED
|
@@ -45,13 +45,20 @@ async function request(url, {
|
|
|
45
45
|
} = { headers: {} }) {
|
|
46
46
|
if (!logger)
|
|
47
47
|
logger = new Logger("request");
|
|
48
|
-
if (mock)
|
|
48
|
+
if (mock) {
|
|
49
|
+
logger.debug("mock %s %j", url, {
|
|
50
|
+
headers,
|
|
51
|
+
method,
|
|
52
|
+
query,
|
|
53
|
+
body
|
|
54
|
+
});
|
|
49
55
|
return mock(url, {
|
|
50
56
|
headers,
|
|
51
57
|
method,
|
|
52
58
|
query,
|
|
53
59
|
body
|
|
54
60
|
});
|
|
61
|
+
}
|
|
55
62
|
if (query) {
|
|
56
63
|
if (!url.includes("?"))
|
|
57
64
|
url += "?";
|
|
@@ -104,7 +111,7 @@ async function request(url, {
|
|
|
104
111
|
res.on("end", () => {
|
|
105
112
|
const data = Buffer.concat(raw).toString();
|
|
106
113
|
logger.timeEnd(url, "response %s %s %s", res.statusCode, res.headers["content-type"], data);
|
|
107
|
-
const response = /* @__PURE__ */ Object.create(null);
|
|
114
|
+
const response = res.statusCode >= 200 && res.statusCode < 400 ? /* @__PURE__ */ Object.create(null) : new Error();
|
|
108
115
|
response.request = options;
|
|
109
116
|
response.request.body = body;
|
|
110
117
|
response.statusCode = res.statusCode;
|
|
@@ -113,7 +120,7 @@ async function request(url, {
|
|
|
113
120
|
response.body = data;
|
|
114
121
|
if (response.body && response.headers["content-type"] && response.headers["content-type"].includes("application/json"))
|
|
115
122
|
try {
|
|
116
|
-
response.body = parse
|
|
123
|
+
response.body = (parse || JSON.parse)(response.body);
|
|
117
124
|
logger.debug("response.parse JSON");
|
|
118
125
|
} catch (error) {
|
|
119
126
|
console.warn("response plain body", response.body);
|
|
@@ -123,6 +130,7 @@ async function request(url, {
|
|
|
123
130
|
resolve(response);
|
|
124
131
|
else {
|
|
125
132
|
logger.debug("response.error %j", response);
|
|
133
|
+
response.message = `${res.statusCode} ${res.statusMessage} ${url}`;
|
|
126
134
|
reject(response);
|
|
127
135
|
}
|
|
128
136
|
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@faasjs/request",
|
|
3
|
-
"version": "0.0.3-beta.
|
|
3
|
+
"version": "0.0.3-beta.81",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -15,14 +15,13 @@
|
|
|
15
15
|
},
|
|
16
16
|
"funding": "https://github.com/sponsors/faasjs",
|
|
17
17
|
"scripts": {
|
|
18
|
-
"build": "tsup-node src/index.ts --format esm,cjs"
|
|
19
|
-
"build:types": "tsup-node src/index.ts --dts-only"
|
|
18
|
+
"build": "tsup-node src/index.ts --format esm,cjs --dts --clean"
|
|
20
19
|
},
|
|
21
20
|
"files": [
|
|
22
21
|
"dist"
|
|
23
22
|
],
|
|
24
23
|
"dependencies": {
|
|
25
|
-
"@faasjs/logger": "^0.0.3-beta.
|
|
24
|
+
"@faasjs/logger": "^0.0.3-beta.81"
|
|
26
25
|
},
|
|
27
26
|
"engines": {
|
|
28
27
|
"npm": ">=8.0.0",
|