@k03mad/request 5.2.0 → 5.4.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.
@@ -25,6 +25,9 @@ jobs:
25
25
  with:
26
26
  node-version-file: '.nvmrc'
27
27
 
28
+ - name: Install PNPM
29
+ run: npm i pnpm -g
30
+
28
31
  - name: Run setup
29
32
  run: npm run setup
30
33
 
package/README.md CHANGED
@@ -1 +1 @@
1
- Adds queues and logging to the `Got` library
1
+ "Got" with queues and debug logs
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.4.0",
4
4
  "description": "Request library",
5
5
  "maintainers": [
6
6
  "Kirill Molchanov <k03.mad@gmail.com"
@@ -17,21 +17,22 @@
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.14.0",
24
25
  "eslint": "8.56.0",
25
26
  "husky": "8.0.3"
26
27
  },
27
28
  "scripts": {
28
- "lint": "npm run lint:eslint",
29
+ "lint": "pnpm run lint:eslint",
29
30
  "lint:eslint": "eslint ./ --cache",
30
- "clean": "npm run clean:modules && npm run clean:eslint:cache",
31
+ "clean": "pnpm run clean:modules && pnpm run clean:eslint:cache",
31
32
  "clean:modules": "rm -rf ./node_modules || true",
32
33
  "clean:eslint:cache": "rm -rf .eslintcache || true",
33
- "setup": "npm run clean && npm i",
34
- "setup:prod": "npm run clean && npm i --omit=dev",
34
+ "setup": "pnpm run clean && pnpm i",
35
+ "setup:prod": "pnpm run clean && pnpm i --omit=dev",
35
36
  "prepare": "husky install || true"
36
37
  }
37
38
  }