@faasjs/request 2.8.1 → 3.0.0-canary.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/dist/index.d.mts +8 -10
- package/dist/index.d.ts +8 -10
- package/dist/index.js +87 -86
- package/dist/index.mjs +87 -86
- package/package.json +5 -5
package/dist/index.d.mts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { OutgoingHttpHeaders } from 'node:http';
|
|
2
|
+
import https from 'node:https';
|
|
2
3
|
import { Logger } from '@faasjs/logger';
|
|
3
4
|
|
|
4
5
|
/**
|
|
@@ -16,11 +17,11 @@ import { Logger } from '@faasjs/logger';
|
|
|
16
17
|
*/
|
|
17
18
|
|
|
18
19
|
type Request = {
|
|
19
|
-
headers?:
|
|
20
|
+
headers?: OutgoingHttpHeaders;
|
|
20
21
|
method?: string;
|
|
21
22
|
host?: string;
|
|
22
23
|
path?: string;
|
|
23
|
-
query?:
|
|
24
|
+
query?: OutgoingHttpHeaders;
|
|
24
25
|
body?: {
|
|
25
26
|
[key: string]: any;
|
|
26
27
|
};
|
|
@@ -29,11 +30,11 @@ type Response<T = any> = {
|
|
|
29
30
|
request?: Request;
|
|
30
31
|
statusCode?: number;
|
|
31
32
|
statusMessage?: string;
|
|
32
|
-
headers:
|
|
33
|
+
headers: OutgoingHttpHeaders;
|
|
33
34
|
body: T;
|
|
34
35
|
};
|
|
35
36
|
type RequestOptions = {
|
|
36
|
-
headers?:
|
|
37
|
+
headers?: OutgoingHttpHeaders;
|
|
37
38
|
/**
|
|
38
39
|
* The HTTP method to use when making the request. Defaults to GET.
|
|
39
40
|
*/
|
|
@@ -79,15 +80,12 @@ type RequestOptions = {
|
|
|
79
80
|
* ```
|
|
80
81
|
*/
|
|
81
82
|
downloadFile?: string;
|
|
82
|
-
pfx?: Buffer;
|
|
83
|
-
passphrase?: string;
|
|
84
|
-
agent?: boolean;
|
|
85
83
|
/**
|
|
86
84
|
* Body parser. Defaults to `JSON.parse`.
|
|
87
85
|
*/
|
|
88
86
|
parse?: (body: string) => any;
|
|
89
87
|
logger?: Logger;
|
|
90
|
-
}
|
|
88
|
+
} & Pick<https.RequestOptions, 'pfx' | 'passphrase' | 'agent'>;
|
|
91
89
|
type Mock = (url: string, options: RequestOptions) => Promise<Response>;
|
|
92
90
|
/**
|
|
93
91
|
* Mock requests
|
|
@@ -104,7 +102,7 @@ declare class ResponseError extends Error {
|
|
|
104
102
|
request: Request;
|
|
105
103
|
statusCode: number;
|
|
106
104
|
statusMessage: string;
|
|
107
|
-
headers:
|
|
105
|
+
headers: OutgoingHttpHeaders;
|
|
108
106
|
body: any;
|
|
109
107
|
constructor(message: string, response: Response<any>);
|
|
110
108
|
}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { OutgoingHttpHeaders } from 'node:http';
|
|
2
|
+
import https from 'node:https';
|
|
2
3
|
import { Logger } from '@faasjs/logger';
|
|
3
4
|
|
|
4
5
|
/**
|
|
@@ -16,11 +17,11 @@ import { Logger } from '@faasjs/logger';
|
|
|
16
17
|
*/
|
|
17
18
|
|
|
18
19
|
type Request = {
|
|
19
|
-
headers?:
|
|
20
|
+
headers?: OutgoingHttpHeaders;
|
|
20
21
|
method?: string;
|
|
21
22
|
host?: string;
|
|
22
23
|
path?: string;
|
|
23
|
-
query?:
|
|
24
|
+
query?: OutgoingHttpHeaders;
|
|
24
25
|
body?: {
|
|
25
26
|
[key: string]: any;
|
|
26
27
|
};
|
|
@@ -29,11 +30,11 @@ type Response<T = any> = {
|
|
|
29
30
|
request?: Request;
|
|
30
31
|
statusCode?: number;
|
|
31
32
|
statusMessage?: string;
|
|
32
|
-
headers:
|
|
33
|
+
headers: OutgoingHttpHeaders;
|
|
33
34
|
body: T;
|
|
34
35
|
};
|
|
35
36
|
type RequestOptions = {
|
|
36
|
-
headers?:
|
|
37
|
+
headers?: OutgoingHttpHeaders;
|
|
37
38
|
/**
|
|
38
39
|
* The HTTP method to use when making the request. Defaults to GET.
|
|
39
40
|
*/
|
|
@@ -79,15 +80,12 @@ type RequestOptions = {
|
|
|
79
80
|
* ```
|
|
80
81
|
*/
|
|
81
82
|
downloadFile?: string;
|
|
82
|
-
pfx?: Buffer;
|
|
83
|
-
passphrase?: string;
|
|
84
|
-
agent?: boolean;
|
|
85
83
|
/**
|
|
86
84
|
* Body parser. Defaults to `JSON.parse`.
|
|
87
85
|
*/
|
|
88
86
|
parse?: (body: string) => any;
|
|
89
87
|
logger?: Logger;
|
|
90
|
-
}
|
|
88
|
+
} & Pick<https.RequestOptions, 'pfx' | 'passphrase' | 'agent'>;
|
|
91
89
|
type Mock = (url: string, options: RequestOptions) => Promise<Response>;
|
|
92
90
|
/**
|
|
93
91
|
* Mock requests
|
|
@@ -104,7 +102,7 @@ declare class ResponseError extends Error {
|
|
|
104
102
|
request: Request;
|
|
105
103
|
statusCode: number;
|
|
106
104
|
statusMessage: string;
|
|
107
|
-
headers:
|
|
105
|
+
headers: OutgoingHttpHeaders;
|
|
108
106
|
body: any;
|
|
109
107
|
constructor(message: string, response: Response<any>);
|
|
110
108
|
}
|
package/dist/index.js
CHANGED
|
@@ -38,6 +38,12 @@ function querystringify(obj) {
|
|
|
38
38
|
return pairs.length ? pairs.join("&") : "";
|
|
39
39
|
}
|
|
40
40
|
var ResponseError = class extends Error {
|
|
41
|
+
response;
|
|
42
|
+
request;
|
|
43
|
+
statusCode;
|
|
44
|
+
statusMessage;
|
|
45
|
+
headers;
|
|
46
|
+
body;
|
|
41
47
|
constructor(message, response) {
|
|
42
48
|
super(message);
|
|
43
49
|
this.response = response;
|
|
@@ -49,7 +55,6 @@ var ResponseError = class extends Error {
|
|
|
49
55
|
}
|
|
50
56
|
};
|
|
51
57
|
async function request(url$1, options = { headers: {} }) {
|
|
52
|
-
var _a;
|
|
53
58
|
const requestId = crypto.randomUUID();
|
|
54
59
|
const logger$1 = options.logger || new logger.Logger(`request][${requestId}`);
|
|
55
60
|
if (mock) {
|
|
@@ -84,7 +89,7 @@ async function request(url$1, options = { headers: {} }) {
|
|
|
84
89
|
requestOptions.headers[key] = options.headers[key];
|
|
85
90
|
let body = options.body;
|
|
86
91
|
if (body && typeof body !== "string")
|
|
87
|
-
if (
|
|
92
|
+
if (options.headers["Content-Type"]?.toString().includes("application/x-www-form-urlencoded"))
|
|
88
93
|
body = querystringify(body);
|
|
89
94
|
else body = JSON.stringify(body);
|
|
90
95
|
if (body && !options.headers["Content-Length"])
|
|
@@ -94,96 +99,92 @@ async function request(url$1, options = { headers: {} }) {
|
|
|
94
99
|
...options,
|
|
95
100
|
body
|
|
96
101
|
});
|
|
97
|
-
const req = protocol.request(
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
reject(error);
|
|
122
|
-
}).on("finish", () => {
|
|
123
|
-
logger$1.timeEnd(
|
|
124
|
-
requestId,
|
|
125
|
-
"response %s %s %s",
|
|
126
|
-
res.statusCode,
|
|
127
|
-
res.headers["content-type"],
|
|
128
|
-
stream2.bytesWritten
|
|
129
|
-
);
|
|
130
|
-
resolve(void 0);
|
|
131
|
-
});
|
|
132
|
-
res.pipe(stream2, { end: true });
|
|
133
|
-
return;
|
|
134
|
-
}
|
|
135
|
-
let stream = res;
|
|
136
|
-
switch (res.headers["content-encoding"]) {
|
|
137
|
-
case "br":
|
|
138
|
-
stream = res.pipe(zlib.createBrotliDecompress());
|
|
139
|
-
break;
|
|
140
|
-
case "gzip":
|
|
141
|
-
stream = res.pipe(zlib.createGunzip());
|
|
142
|
-
break;
|
|
143
|
-
}
|
|
144
|
-
const raw = [];
|
|
145
|
-
stream.on("error", (e) => {
|
|
146
|
-
logger$1.timeEnd(requestId, "response.error %j", e);
|
|
147
|
-
reject(e);
|
|
148
|
-
}).on("end", () => {
|
|
149
|
-
const data = Buffer.concat(raw).toString();
|
|
102
|
+
const req = protocol.request(requestOptions, (res) => {
|
|
103
|
+
if (options.downloadStream) {
|
|
104
|
+
options.downloadStream.on("error", (error) => {
|
|
105
|
+
logger$1.timeEnd(requestId, "response.error %j", error);
|
|
106
|
+
reject(error);
|
|
107
|
+
}).on("finish", () => {
|
|
108
|
+
logger$1.timeEnd(
|
|
109
|
+
requestId,
|
|
110
|
+
"response %s %s",
|
|
111
|
+
res.statusCode,
|
|
112
|
+
res.headers["content-type"]
|
|
113
|
+
);
|
|
114
|
+
options.downloadStream.end();
|
|
115
|
+
resolve(void 0);
|
|
116
|
+
});
|
|
117
|
+
res.pipe(options.downloadStream, { end: true });
|
|
118
|
+
return;
|
|
119
|
+
}
|
|
120
|
+
if (options.downloadFile) {
|
|
121
|
+
const stream2 = fs.createWriteStream(options.downloadFile).on("error", (error) => {
|
|
122
|
+
logger$1.timeEnd(requestId, "response.error %j", error);
|
|
123
|
+
stream2.destroy();
|
|
124
|
+
reject(error);
|
|
125
|
+
}).on("finish", () => {
|
|
150
126
|
logger$1.timeEnd(
|
|
151
127
|
requestId,
|
|
152
|
-
"response %s %s %s
|
|
128
|
+
"response %s %s %s",
|
|
153
129
|
res.statusCode,
|
|
154
130
|
res.headers["content-type"],
|
|
155
|
-
|
|
156
|
-
data
|
|
131
|
+
stream2.bytesWritten
|
|
157
132
|
);
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
response.statusMessage = res.statusMessage;
|
|
163
|
-
response.headers = res.headers;
|
|
164
|
-
response.body = data;
|
|
165
|
-
if (response.body && response.headers["content-type"] && response.headers["content-type"].includes("application/json") && typeof response.body === "string" && (response.body.startsWith("{") || response.body.startsWith("[")))
|
|
166
|
-
try {
|
|
167
|
-
response.body = (options.parse || JSON.parse)(response.body);
|
|
168
|
-
logger$1.debug("response.parse JSON");
|
|
169
|
-
} catch (error) {
|
|
170
|
-
logger$1.warn("response plain body", response.body);
|
|
171
|
-
logger$1.error(error);
|
|
172
|
-
}
|
|
173
|
-
if (response.statusCode >= 200 && response.statusCode < 400)
|
|
174
|
-
resolve(response);
|
|
175
|
-
else {
|
|
176
|
-
logger$1.debug("response.error %j", response);
|
|
177
|
-
reject(
|
|
178
|
-
new ResponseError(
|
|
179
|
-
`${res.statusMessage || res.statusCode} ${requestOptions.host}${requestOptions.path}`,
|
|
180
|
-
response
|
|
181
|
-
)
|
|
182
|
-
);
|
|
183
|
-
}
|
|
184
|
-
}).on("data", (chunk) => raw.push(chunk));
|
|
133
|
+
resolve(void 0);
|
|
134
|
+
});
|
|
135
|
+
res.pipe(stream2, { end: true });
|
|
136
|
+
return;
|
|
185
137
|
}
|
|
186
|
-
|
|
138
|
+
let stream = res;
|
|
139
|
+
switch (res.headers["content-encoding"]) {
|
|
140
|
+
case "br":
|
|
141
|
+
stream = res.pipe(zlib.createBrotliDecompress());
|
|
142
|
+
break;
|
|
143
|
+
case "gzip":
|
|
144
|
+
stream = res.pipe(zlib.createGunzip());
|
|
145
|
+
break;
|
|
146
|
+
}
|
|
147
|
+
let raw = "";
|
|
148
|
+
stream.on("data", (chunk) => raw += chunk).on("error", (e) => {
|
|
149
|
+
logger$1.timeEnd(requestId, "response.error %j", e);
|
|
150
|
+
reject(e);
|
|
151
|
+
}).on("end", () => {
|
|
152
|
+
logger$1.timeEnd(
|
|
153
|
+
requestId,
|
|
154
|
+
"response %s %s %s %j",
|
|
155
|
+
res.statusCode,
|
|
156
|
+
res.headers["content-type"],
|
|
157
|
+
res.headers["content-encoding"],
|
|
158
|
+
raw
|
|
159
|
+
);
|
|
160
|
+
const response = /* @__PURE__ */ Object.create(null);
|
|
161
|
+
response.request = requestOptions;
|
|
162
|
+
response.request.body = body;
|
|
163
|
+
response.statusCode = res.statusCode;
|
|
164
|
+
response.statusMessage = res.statusMessage;
|
|
165
|
+
response.headers = res.headers;
|
|
166
|
+
response.body = raw;
|
|
167
|
+
if (response.body && response.headers["content-type"] && response.headers["content-type"].includes("application/json") && typeof response.body === "string" && (response.body.startsWith("{") || response.body.startsWith("[")))
|
|
168
|
+
try {
|
|
169
|
+
response.body = (options.parse || JSON.parse)(response.body);
|
|
170
|
+
logger$1.debug("response.parse JSON");
|
|
171
|
+
} catch (error) {
|
|
172
|
+
logger$1.warn("response plain body", response.body);
|
|
173
|
+
logger$1.error(error);
|
|
174
|
+
}
|
|
175
|
+
if (response.statusCode >= 200 && response.statusCode < 400)
|
|
176
|
+
resolve(response);
|
|
177
|
+
else {
|
|
178
|
+
logger$1.debug("response.error %j", response);
|
|
179
|
+
reject(
|
|
180
|
+
new ResponseError(
|
|
181
|
+
`${res.statusMessage || res.statusCode} ${requestOptions.host}${requestOptions.path}`,
|
|
182
|
+
response
|
|
183
|
+
)
|
|
184
|
+
);
|
|
185
|
+
}
|
|
186
|
+
});
|
|
187
|
+
});
|
|
187
188
|
if (body) req.write(body);
|
|
188
189
|
if (options.file) {
|
|
189
190
|
const crlf = "\r\n";
|
package/dist/index.mjs
CHANGED
|
@@ -31,6 +31,12 @@ function querystringify(obj) {
|
|
|
31
31
|
return pairs.length ? pairs.join("&") : "";
|
|
32
32
|
}
|
|
33
33
|
var ResponseError = class extends Error {
|
|
34
|
+
response;
|
|
35
|
+
request;
|
|
36
|
+
statusCode;
|
|
37
|
+
statusMessage;
|
|
38
|
+
headers;
|
|
39
|
+
body;
|
|
34
40
|
constructor(message, response) {
|
|
35
41
|
super(message);
|
|
36
42
|
this.response = response;
|
|
@@ -42,7 +48,6 @@ var ResponseError = class extends Error {
|
|
|
42
48
|
}
|
|
43
49
|
};
|
|
44
50
|
async function request(url, options = { headers: {} }) {
|
|
45
|
-
var _a;
|
|
46
51
|
const requestId = randomUUID();
|
|
47
52
|
const logger = options.logger || new Logger(`request][${requestId}`);
|
|
48
53
|
if (mock) {
|
|
@@ -77,7 +82,7 @@ async function request(url, options = { headers: {} }) {
|
|
|
77
82
|
requestOptions.headers[key] = options.headers[key];
|
|
78
83
|
let body = options.body;
|
|
79
84
|
if (body && typeof body !== "string")
|
|
80
|
-
if (
|
|
85
|
+
if (options.headers["Content-Type"]?.toString().includes("application/x-www-form-urlencoded"))
|
|
81
86
|
body = querystringify(body);
|
|
82
87
|
else body = JSON.stringify(body);
|
|
83
88
|
if (body && !options.headers["Content-Length"])
|
|
@@ -87,96 +92,92 @@ async function request(url, options = { headers: {} }) {
|
|
|
87
92
|
...options,
|
|
88
93
|
body
|
|
89
94
|
});
|
|
90
|
-
const req = protocol.request(
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
reject(error);
|
|
115
|
-
}).on("finish", () => {
|
|
116
|
-
logger.timeEnd(
|
|
117
|
-
requestId,
|
|
118
|
-
"response %s %s %s",
|
|
119
|
-
res.statusCode,
|
|
120
|
-
res.headers["content-type"],
|
|
121
|
-
stream2.bytesWritten
|
|
122
|
-
);
|
|
123
|
-
resolve(void 0);
|
|
124
|
-
});
|
|
125
|
-
res.pipe(stream2, { end: true });
|
|
126
|
-
return;
|
|
127
|
-
}
|
|
128
|
-
let stream = res;
|
|
129
|
-
switch (res.headers["content-encoding"]) {
|
|
130
|
-
case "br":
|
|
131
|
-
stream = res.pipe(createBrotliDecompress());
|
|
132
|
-
break;
|
|
133
|
-
case "gzip":
|
|
134
|
-
stream = res.pipe(createGunzip());
|
|
135
|
-
break;
|
|
136
|
-
}
|
|
137
|
-
const raw = [];
|
|
138
|
-
stream.on("error", (e) => {
|
|
139
|
-
logger.timeEnd(requestId, "response.error %j", e);
|
|
140
|
-
reject(e);
|
|
141
|
-
}).on("end", () => {
|
|
142
|
-
const data = Buffer.concat(raw).toString();
|
|
95
|
+
const req = protocol.request(requestOptions, (res) => {
|
|
96
|
+
if (options.downloadStream) {
|
|
97
|
+
options.downloadStream.on("error", (error) => {
|
|
98
|
+
logger.timeEnd(requestId, "response.error %j", error);
|
|
99
|
+
reject(error);
|
|
100
|
+
}).on("finish", () => {
|
|
101
|
+
logger.timeEnd(
|
|
102
|
+
requestId,
|
|
103
|
+
"response %s %s",
|
|
104
|
+
res.statusCode,
|
|
105
|
+
res.headers["content-type"]
|
|
106
|
+
);
|
|
107
|
+
options.downloadStream.end();
|
|
108
|
+
resolve(void 0);
|
|
109
|
+
});
|
|
110
|
+
res.pipe(options.downloadStream, { end: true });
|
|
111
|
+
return;
|
|
112
|
+
}
|
|
113
|
+
if (options.downloadFile) {
|
|
114
|
+
const stream2 = createWriteStream(options.downloadFile).on("error", (error) => {
|
|
115
|
+
logger.timeEnd(requestId, "response.error %j", error);
|
|
116
|
+
stream2.destroy();
|
|
117
|
+
reject(error);
|
|
118
|
+
}).on("finish", () => {
|
|
143
119
|
logger.timeEnd(
|
|
144
120
|
requestId,
|
|
145
|
-
"response %s %s %s
|
|
121
|
+
"response %s %s %s",
|
|
146
122
|
res.statusCode,
|
|
147
123
|
res.headers["content-type"],
|
|
148
|
-
|
|
149
|
-
data
|
|
124
|
+
stream2.bytesWritten
|
|
150
125
|
);
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
response.statusMessage = res.statusMessage;
|
|
156
|
-
response.headers = res.headers;
|
|
157
|
-
response.body = data;
|
|
158
|
-
if (response.body && response.headers["content-type"] && response.headers["content-type"].includes("application/json") && typeof response.body === "string" && (response.body.startsWith("{") || response.body.startsWith("[")))
|
|
159
|
-
try {
|
|
160
|
-
response.body = (options.parse || JSON.parse)(response.body);
|
|
161
|
-
logger.debug("response.parse JSON");
|
|
162
|
-
} catch (error) {
|
|
163
|
-
logger.warn("response plain body", response.body);
|
|
164
|
-
logger.error(error);
|
|
165
|
-
}
|
|
166
|
-
if (response.statusCode >= 200 && response.statusCode < 400)
|
|
167
|
-
resolve(response);
|
|
168
|
-
else {
|
|
169
|
-
logger.debug("response.error %j", response);
|
|
170
|
-
reject(
|
|
171
|
-
new ResponseError(
|
|
172
|
-
`${res.statusMessage || res.statusCode} ${requestOptions.host}${requestOptions.path}`,
|
|
173
|
-
response
|
|
174
|
-
)
|
|
175
|
-
);
|
|
176
|
-
}
|
|
177
|
-
}).on("data", (chunk) => raw.push(chunk));
|
|
126
|
+
resolve(void 0);
|
|
127
|
+
});
|
|
128
|
+
res.pipe(stream2, { end: true });
|
|
129
|
+
return;
|
|
178
130
|
}
|
|
179
|
-
|
|
131
|
+
let stream = res;
|
|
132
|
+
switch (res.headers["content-encoding"]) {
|
|
133
|
+
case "br":
|
|
134
|
+
stream = res.pipe(createBrotliDecompress());
|
|
135
|
+
break;
|
|
136
|
+
case "gzip":
|
|
137
|
+
stream = res.pipe(createGunzip());
|
|
138
|
+
break;
|
|
139
|
+
}
|
|
140
|
+
let raw = "";
|
|
141
|
+
stream.on("data", (chunk) => raw += chunk).on("error", (e) => {
|
|
142
|
+
logger.timeEnd(requestId, "response.error %j", e);
|
|
143
|
+
reject(e);
|
|
144
|
+
}).on("end", () => {
|
|
145
|
+
logger.timeEnd(
|
|
146
|
+
requestId,
|
|
147
|
+
"response %s %s %s %j",
|
|
148
|
+
res.statusCode,
|
|
149
|
+
res.headers["content-type"],
|
|
150
|
+
res.headers["content-encoding"],
|
|
151
|
+
raw
|
|
152
|
+
);
|
|
153
|
+
const response = /* @__PURE__ */ Object.create(null);
|
|
154
|
+
response.request = requestOptions;
|
|
155
|
+
response.request.body = body;
|
|
156
|
+
response.statusCode = res.statusCode;
|
|
157
|
+
response.statusMessage = res.statusMessage;
|
|
158
|
+
response.headers = res.headers;
|
|
159
|
+
response.body = raw;
|
|
160
|
+
if (response.body && response.headers["content-type"] && response.headers["content-type"].includes("application/json") && typeof response.body === "string" && (response.body.startsWith("{") || response.body.startsWith("[")))
|
|
161
|
+
try {
|
|
162
|
+
response.body = (options.parse || JSON.parse)(response.body);
|
|
163
|
+
logger.debug("response.parse JSON");
|
|
164
|
+
} catch (error) {
|
|
165
|
+
logger.warn("response plain body", response.body);
|
|
166
|
+
logger.error(error);
|
|
167
|
+
}
|
|
168
|
+
if (response.statusCode >= 200 && response.statusCode < 400)
|
|
169
|
+
resolve(response);
|
|
170
|
+
else {
|
|
171
|
+
logger.debug("response.error %j", response);
|
|
172
|
+
reject(
|
|
173
|
+
new ResponseError(
|
|
174
|
+
`${res.statusMessage || res.statusCode} ${requestOptions.host}${requestOptions.path}`,
|
|
175
|
+
response
|
|
176
|
+
)
|
|
177
|
+
);
|
|
178
|
+
}
|
|
179
|
+
});
|
|
180
|
+
});
|
|
180
181
|
if (body) req.write(body);
|
|
181
182
|
if (options.file) {
|
|
182
183
|
const crlf = "\r\n";
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@faasjs/request",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "3.0.0-canary.1",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"module": "dist/index.mjs",
|
|
@@ -34,13 +34,13 @@
|
|
|
34
34
|
"dist"
|
|
35
35
|
],
|
|
36
36
|
"peerDependencies": {
|
|
37
|
-
"@faasjs/logger": "
|
|
37
|
+
"@faasjs/logger": "3.0.0-canary.1"
|
|
38
38
|
},
|
|
39
39
|
"devDependencies": {
|
|
40
|
-
"@faasjs/logger": "
|
|
40
|
+
"@faasjs/logger": "3.0.0-canary.1"
|
|
41
41
|
},
|
|
42
42
|
"engines": {
|
|
43
|
-
"
|
|
44
|
-
"
|
|
43
|
+
"node": ">=22.0.0",
|
|
44
|
+
"npm": ">=10.0.0"
|
|
45
45
|
}
|
|
46
46
|
}
|