@k03mad/request 5.1.1 → 5.3.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.
- package/app/lib/curl.js +73 -35
- package/app/lib/request.js +15 -17
- package/package.json +4 -3
package/app/lib/curl.js
CHANGED
|
@@ -1,81 +1,119 @@
|
|
|
1
1
|
import chalk from 'chalk';
|
|
2
2
|
import prettyBytes from 'pretty-bytes';
|
|
3
3
|
|
|
4
|
-
const {
|
|
4
|
+
const {
|
|
5
|
+
bgGreen,
|
|
6
|
+
bgRed,
|
|
7
|
+
bgWhite,
|
|
8
|
+
bgYellow,
|
|
9
|
+
black,
|
|
10
|
+
blue,
|
|
11
|
+
bold,
|
|
12
|
+
cyan,
|
|
13
|
+
dim,
|
|
14
|
+
gray,
|
|
15
|
+
green,
|
|
16
|
+
magenta,
|
|
17
|
+
yellow,
|
|
18
|
+
} = chalk;
|
|
19
|
+
|
|
20
|
+
const MIN_RESPONSE_SYM_LENGTH_TO_PRINT = 2000;
|
|
21
|
+
|
|
22
|
+
const SKIP_HEADERS = new Set(['accept-encoding: gzip, deflate, br']);
|
|
5
23
|
|
|
6
24
|
/**
|
|
7
|
-
* @param {
|
|
25
|
+
* @param {object} res
|
|
8
26
|
* @param {object} [opts]
|
|
9
|
-
* @param {string} [opts.method]
|
|
10
|
-
* @param {object} [opts.headers]
|
|
11
27
|
* @param {string} [opts.body]
|
|
12
28
|
* @param {object} [opts.json]
|
|
13
29
|
* @param {object} [opts.form]
|
|
14
|
-
* @param {object} [
|
|
15
|
-
* @param {
|
|
16
|
-
* @param {string} [opts.password]
|
|
17
|
-
* @param {object} res
|
|
30
|
+
* @param {object} [params]
|
|
31
|
+
* @param {boolean} [params.skipResponse]
|
|
18
32
|
* @returns {string}
|
|
19
33
|
*/
|
|
20
|
-
export default (
|
|
21
|
-
body, form,
|
|
22
|
-
headers = {}, json, method = 'GET', password,
|
|
23
|
-
searchParams, username,
|
|
24
|
-
}, res) => {
|
|
34
|
+
export default (res, {body, form, json}, {skipResponse} = {}) => {
|
|
25
35
|
const msg = [];
|
|
26
36
|
|
|
27
|
-
const fullUrl = searchParams
|
|
28
|
-
? `${url}?${new URLSearchParams(searchParams).toString()}`
|
|
29
|
-
: url;
|
|
30
|
-
|
|
31
37
|
const response = res?.response || res;
|
|
38
|
+
const reqOptions = response.request?.options;
|
|
32
39
|
|
|
40
|
+
// some info before curl
|
|
33
41
|
if (response.statusCode) {
|
|
34
|
-
|
|
42
|
+
const statusCodeStringify = String(response.statusCode);
|
|
43
|
+
let bgColor = bgWhite;
|
|
44
|
+
|
|
45
|
+
if ((/^(1|2)/).test(statusCodeStringify)) {
|
|
46
|
+
bgColor = bgGreen;
|
|
47
|
+
} else if (statusCodeStringify.startsWith('3')) {
|
|
48
|
+
bgColor = bgYellow;
|
|
49
|
+
} else if ((/^(4|5)/).test(statusCodeStringify)) {
|
|
50
|
+
bgColor = bgRed;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
msg.push(bgColor(black(bold(response.statusCode))));
|
|
35
54
|
}
|
|
36
55
|
|
|
37
|
-
if (response.timings) {
|
|
38
|
-
msg.push(`[${response.timings.phases.total} ms]`);
|
|
56
|
+
if (response.timings?.phases?.total) {
|
|
57
|
+
msg.push(gray(`[response: ${response.timings.phases.total} ms]`));
|
|
39
58
|
}
|
|
40
59
|
|
|
41
60
|
if (response.headers?.['content-length']) {
|
|
42
|
-
msg.push(`[${prettyBytes(Number(response.headers['content-length']))}]`);
|
|
61
|
+
msg.push(gray(`[content: ${prettyBytes(Number(response.headers['content-length']))}]`));
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
if (response.ip) {
|
|
65
|
+
msg.push(gray(`[host: ${response.ip}]`));
|
|
43
66
|
}
|
|
44
67
|
|
|
45
|
-
|
|
68
|
+
// begin verbose curl with method and url
|
|
69
|
+
msg.push('\ncurl -v');
|
|
70
|
+
|
|
71
|
+
if (reqOptions?.method) {
|
|
72
|
+
msg.push(cyan(`-X ${reqOptions.method}`));
|
|
73
|
+
}
|
|
46
74
|
|
|
47
|
-
if (
|
|
48
|
-
msg.push(
|
|
75
|
+
if (reqOptions?.url) {
|
|
76
|
+
msg.push(blue(reqOptions.url));
|
|
49
77
|
}
|
|
50
78
|
|
|
51
|
-
|
|
79
|
+
// headers
|
|
80
|
+
const headersParams = Object.entries(response.request?.options?.headers || {});
|
|
81
|
+
|
|
82
|
+
if (headersParams.length > 0) {
|
|
83
|
+
const headersStringify = headersParams
|
|
84
|
+
.map(([key, value]) => {
|
|
85
|
+
const keyValue = `${key}: ${value}`;
|
|
86
|
+
|
|
87
|
+
if (!SKIP_HEADERS.has(keyValue)) {
|
|
88
|
+
return `-H "${keyValue}"`;
|
|
89
|
+
}
|
|
90
|
+
})
|
|
91
|
+
.filter(Boolean)
|
|
92
|
+
.join(' ');
|
|
93
|
+
|
|
94
|
+
msg.push(dim(magenta(headersStringify)));
|
|
95
|
+
}
|
|
52
96
|
|
|
97
|
+
// body flags and formatted data
|
|
53
98
|
let bodyParams;
|
|
54
99
|
|
|
55
100
|
if (json) {
|
|
56
|
-
headers['content-type'] = 'application/json';
|
|
57
101
|
bodyParams = `-d '${JSON.stringify(json)}'`;
|
|
58
102
|
} else if (form) {
|
|
59
|
-
headers['content-type'] = 'application/x-www-form-urlencoded';
|
|
60
103
|
bodyParams = `-d '${new URLSearchParams(form).toString()}'`;
|
|
61
104
|
} else if (body) {
|
|
62
105
|
bodyParams = `-d '${body}'`;
|
|
63
106
|
}
|
|
64
107
|
|
|
65
|
-
const headersParams = Object.entries(headers);
|
|
66
|
-
|
|
67
|
-
if (headersParams.length > 0) {
|
|
68
|
-
msg.push(dim(magenta(headersParams.map(([key, value]) => `-H "${key}: ${value}"`).join(' '))));
|
|
69
|
-
}
|
|
70
|
-
|
|
71
108
|
if (bodyParams) {
|
|
72
109
|
msg.push(dim(yellow(bodyParams)));
|
|
73
110
|
}
|
|
74
111
|
|
|
75
|
-
|
|
112
|
+
// response if any and small length
|
|
113
|
+
if (!skipResponse && response) {
|
|
76
114
|
const message = JSON.stringify(response.body || response.message);
|
|
77
115
|
|
|
78
|
-
if (message?.length <
|
|
116
|
+
if (message?.length < MIN_RESPONSE_SYM_LENGTH_TO_PRINT) {
|
|
79
117
|
msg.push(`\n${dim(green(message))}`);
|
|
80
118
|
}
|
|
81
119
|
}
|
package/app/lib/request.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import chalk from 'chalk';
|
|
2
2
|
import _debug from 'debug';
|
|
3
3
|
import got from 'got';
|
|
4
|
+
import stripAnsi from 'strip-ansi';
|
|
4
5
|
|
|
5
6
|
import getCurl from './curl.js';
|
|
6
7
|
import getQueue from './queue.js';
|
|
@@ -40,36 +41,33 @@ const sendRequest = async (url, opts) => {
|
|
|
40
41
|
try {
|
|
41
42
|
const response = await gotDefaultOpts(url, opts);
|
|
42
43
|
|
|
43
|
-
if (!opts.responseType) {
|
|
44
|
+
if (!opts.responseType && response.body) {
|
|
44
45
|
try {
|
|
45
46
|
response.body = JSON.parse(response.body);
|
|
46
47
|
} catch {}
|
|
47
48
|
}
|
|
48
49
|
|
|
49
|
-
debug(getCurl(
|
|
50
|
+
debug(getCurl(response, opts));
|
|
50
51
|
return response;
|
|
51
52
|
} catch (err) {
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
err?.options?.method,
|
|
57
|
-
url,
|
|
58
|
-
].join(' ').trim();
|
|
59
|
-
|
|
60
|
-
if (err?.response?.ip) {
|
|
61
|
-
err.__ip = err.response.ip;
|
|
53
|
+
if (!opts.responseType && err.response?.body) {
|
|
54
|
+
try {
|
|
55
|
+
err.response.body = JSON.parse(err.response.body);
|
|
56
|
+
} catch {}
|
|
62
57
|
}
|
|
63
58
|
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
59
|
+
const curl = getCurl(err, opts, {skipResponse: true});
|
|
60
|
+
debug(curl);
|
|
61
|
+
|
|
62
|
+
err.__ = {
|
|
63
|
+
debug: stripAnsi(curl).split(' \n'),
|
|
64
|
+
};
|
|
67
65
|
|
|
68
66
|
if (err?.response?.body) {
|
|
69
67
|
try {
|
|
70
|
-
err.
|
|
68
|
+
err.__.response = JSON.parse(err.response.body);
|
|
71
69
|
} catch {
|
|
72
|
-
err.
|
|
70
|
+
err.__.response = err.response.body;
|
|
73
71
|
}
|
|
74
72
|
}
|
|
75
73
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@k03mad/request",
|
|
3
|
-
"version": "5.
|
|
3
|
+
"version": "5.3.0",
|
|
4
4
|
"description": "Request library",
|
|
5
5
|
"maintainers": [
|
|
6
6
|
"Kirill Molchanov <k03.mad@gmail.com"
|
|
@@ -17,10 +17,11 @@
|
|
|
17
17
|
"debug": "4.3.4",
|
|
18
18
|
"got": "14.0.0",
|
|
19
19
|
"p-queue": "8.0.1",
|
|
20
|
-
"pretty-bytes": "6.1.1"
|
|
20
|
+
"pretty-bytes": "6.1.1",
|
|
21
|
+
"strip-ansi": "7.1.0"
|
|
21
22
|
},
|
|
22
23
|
"devDependencies": {
|
|
23
|
-
"@k03mad/eslint-config": "17.
|
|
24
|
+
"@k03mad/eslint-config": "17.13.0",
|
|
24
25
|
"eslint": "8.56.0",
|
|
25
26
|
"husky": "8.0.3"
|
|
26
27
|
},
|