@k03mad/request 5.2.0 → 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 CHANGED
@@ -27,9 +27,11 @@ const SKIP_HEADERS = new Set(['accept-encoding: gzip, deflate, br']);
27
27
  * @param {string} [opts.body]
28
28
  * @param {object} [opts.json]
29
29
  * @param {object} [opts.form]
30
+ * @param {object} [params]
31
+ * @param {boolean} [params.skipResponse]
30
32
  * @returns {string}
31
33
  */
32
- export default (res, {body, form, json}) => {
34
+ export default (res, {body, form, json}, {skipResponse} = {}) => {
33
35
  const msg = [];
34
36
 
35
37
  const response = res?.response || res;
@@ -52,15 +54,19 @@ export default (res, {body, form, json}) => {
52
54
  }
53
55
 
54
56
  if (response.timings?.phases?.total) {
55
- msg.push(gray(`[${response.timings.phases.total} ms]`));
57
+ msg.push(gray(`[response: ${response.timings.phases.total} ms]`));
56
58
  }
57
59
 
58
60
  if (response.headers?.['content-length']) {
59
- msg.push(gray(`[${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}]`));
60
66
  }
61
67
 
62
68
  // begin verbose curl with method and url
63
- msg.push('curl -v');
69
+ msg.push('\ncurl -v');
64
70
 
65
71
  if (reqOptions?.method) {
66
72
  msg.push(cyan(`-X ${reqOptions.method}`));
@@ -104,7 +110,7 @@ export default (res, {body, form, json}) => {
104
110
  }
105
111
 
106
112
  // response if any and small length
107
- if (response) {
113
+ if (!skipResponse && response) {
108
114
  const message = JSON.stringify(response.body || response.message);
109
115
 
110
116
  if (message?.length < MIN_RESPONSE_SYM_LENGTH_TO_PRINT) {
@@ -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,7 +41,7 @@ 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 {}
@@ -49,27 +50,24 @@ const sendRequest = async (url, opts) => {
49
50
  debug(getCurl(response, opts));
50
51
  return response;
51
52
  } catch (err) {
52
- debug(getCurl(err, opts));
53
-
54
- err.__req = [
55
- err?.response?.statusCode || err?.code,
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
- if (Object.keys(opts).length > 0) {
65
- err.__opts = opts;
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.__res = JSON.parse(err.response.body);
68
+ err.__.response = JSON.parse(err.response.body);
71
69
  } catch {
72
- err.__res = err.response.body;
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.2.0",
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.12.5",
24
+ "@k03mad/eslint-config": "17.13.0",
24
25
  "eslint": "8.56.0",
25
26
  "husky": "8.0.3"
26
27
  },