flaks-request 1.0.1 → 1.0.2
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/package.json +1 -1
- package/src/index.js +42 -26
- package/src/types.ts +11 -6
package/package.json
CHANGED
package/src/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
/** @import { RequestHandler, RequestOptions, RequestResponse, SearchParamsObject } from './types' */
|
|
1
|
+
/** @import { JsonValue, RequestHandler, RequestOptions, RequestResponse, SearchParamsObject } from './types' */
|
|
2
2
|
|
|
3
3
|
const { WriteStream } = require("node:fs");
|
|
4
4
|
const { pipeline } = require("node:stream/promises");
|
|
@@ -33,6 +33,18 @@ function buildSearchParams(obj, prefix = "") {
|
|
|
33
33
|
return params;
|
|
34
34
|
}
|
|
35
35
|
|
|
36
|
+
function safeParse(text = "") {
|
|
37
|
+
try {
|
|
38
|
+
/** @type {JsonValue} */
|
|
39
|
+
const body = JSON.parse(text);
|
|
40
|
+
const isJson = true;
|
|
41
|
+
|
|
42
|
+
return { body, isJson };
|
|
43
|
+
} catch {
|
|
44
|
+
return { body: text };
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
36
48
|
/**
|
|
37
49
|
*
|
|
38
50
|
* @param {RequestOptions} requestOptions
|
|
@@ -54,6 +66,19 @@ function resolveUrl(requestOptions) {
|
|
|
54
66
|
return url;
|
|
55
67
|
}
|
|
56
68
|
|
|
69
|
+
/**
|
|
70
|
+
*
|
|
71
|
+
* @param {RequestOptions} requestOptions
|
|
72
|
+
* @param {IncomingMessage} res
|
|
73
|
+
* @param {RequestResponse['body']} [body]
|
|
74
|
+
* @param {RequestResponse['isJson']} [isJson]
|
|
75
|
+
* @param {RequestResponse['verbose']} [verbose]
|
|
76
|
+
* @returns
|
|
77
|
+
*/
|
|
78
|
+
function buildResponse(requestOptions, res, success = true, body, isJson, verbose) {
|
|
79
|
+
return { success, status: res.statusCode ?? -1, headers: res.headers, body, isJson, requestOptions, verbose };
|
|
80
|
+
}
|
|
81
|
+
|
|
57
82
|
/**
|
|
58
83
|
*
|
|
59
84
|
* @param {RequestOptions} requestOptions
|
|
@@ -148,14 +173,17 @@ function handleRequest(requestOptions, responseHandler) {
|
|
|
148
173
|
res.on("data", (chunk) => chunks.push(chunk));
|
|
149
174
|
|
|
150
175
|
res.on("end", () => {
|
|
151
|
-
const
|
|
152
|
-
const
|
|
176
|
+
const text = Buffer.concat(chunks).toString();
|
|
177
|
+
const verbose = [`Status:${status}`];
|
|
178
|
+
|
|
179
|
+
if (redirectUrls.length) verbose.push(`\nRedirects:\n${redirectUrls.join("\n")}`);
|
|
180
|
+
verbose.push(`\nReq:${JSON.stringify(requestOptions, null, 2)}`);
|
|
181
|
+
verbose.push(`\nBody:\n${text}`);
|
|
153
182
|
|
|
154
|
-
|
|
155
|
-
msgs.push(`\nReq:${JSON.stringify(requestOptions, null, 2)}`);
|
|
156
|
-
msgs.push(`\nBody:\n${body}`);
|
|
183
|
+
const msg = verbose.join(" ");
|
|
157
184
|
|
|
158
|
-
const
|
|
185
|
+
const json = safeParse(text);
|
|
186
|
+
resolve(buildResponse(requestOptions, res, false, json.body, json.isJson, verbose));
|
|
159
187
|
reject(new Error(msg));
|
|
160
188
|
});
|
|
161
189
|
|
|
@@ -214,27 +242,13 @@ async function request(requestOptions) {
|
|
|
214
242
|
|
|
215
243
|
res.on("end", () => {
|
|
216
244
|
const text = Buffer.concat(chunks).toString();
|
|
245
|
+
const json = safeParse(text);
|
|
217
246
|
|
|
218
|
-
|
|
219
|
-
let isJson = false;
|
|
220
|
-
|
|
221
|
-
try {
|
|
222
|
-
body = JSON.parse(text);
|
|
223
|
-
isJson = true;
|
|
224
|
-
} catch {
|
|
225
|
-
// keep text body
|
|
226
|
-
}
|
|
227
|
-
|
|
228
|
-
resolve({
|
|
229
|
-
success: true,
|
|
230
|
-
isJson,
|
|
231
|
-
body,
|
|
232
|
-
status: res.statusCode,
|
|
233
|
-
requestOptions,
|
|
234
|
-
});
|
|
247
|
+
resolve(buildResponse(requestOptions, res, true, json.body, json.isJson));
|
|
235
248
|
});
|
|
236
249
|
});
|
|
237
250
|
}
|
|
251
|
+
|
|
238
252
|
/**
|
|
239
253
|
*
|
|
240
254
|
* @param {RequestOptions} requestOptions
|
|
@@ -243,7 +257,7 @@ function requestHeaders(requestOptions) {
|
|
|
243
257
|
return handleRequest(requestOptions, (res, resolve) => {
|
|
244
258
|
res.destroy();
|
|
245
259
|
|
|
246
|
-
resolve(
|
|
260
|
+
resolve(buildResponse(requestOptions, res));
|
|
247
261
|
});
|
|
248
262
|
}
|
|
249
263
|
|
|
@@ -254,7 +268,9 @@ function requestHeaders(requestOptions) {
|
|
|
254
268
|
*/
|
|
255
269
|
function requestFile(requestOptions, stream) {
|
|
256
270
|
return handleRequest(requestOptions, (res, resolve, reject) => {
|
|
257
|
-
pipeline(res, stream)
|
|
271
|
+
pipeline(res, stream)
|
|
272
|
+
.then(() => resolve(buildResponse(requestOptions, res)))
|
|
273
|
+
.catch(reject);
|
|
258
274
|
});
|
|
259
275
|
}
|
|
260
276
|
|
package/src/types.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { IncomingMessage } from "node:http";
|
|
1
|
+
import { IncomingHttpHeaders, IncomingMessage } from "node:http";
|
|
2
2
|
|
|
3
3
|
type SearchParamsValue = string | number | boolean | null | undefined | SearchParamsObject | unknown[];
|
|
4
4
|
|
|
@@ -19,14 +19,19 @@ export interface RequestOptions {
|
|
|
19
19
|
|
|
20
20
|
export type RequestHandler = (
|
|
21
21
|
res: IncomingMessage,
|
|
22
|
-
resolve: (value:
|
|
22
|
+
resolve: (value: RequestResponse) => void,
|
|
23
23
|
reject: (err: Error) => void,
|
|
24
24
|
) => void;
|
|
25
25
|
|
|
26
|
+
export type JsonValue = string | number | boolean | null | JsonValue[] | { [key: string]: JsonValue };
|
|
27
|
+
|
|
26
28
|
export interface RequestResponse {
|
|
27
|
-
success
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
29
|
+
success?: boolean;
|
|
30
|
+
status: number;
|
|
31
|
+
headers: IncomingHttpHeaders;
|
|
32
|
+
body?: JsonValue;
|
|
33
|
+
isJson?: boolean;
|
|
31
34
|
requestOptions: RequestOptions;
|
|
35
|
+
|
|
36
|
+
verbose?: string[];
|
|
32
37
|
}
|