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