@k03mad/request 2.0.0 → 2.1.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.
Files changed (2) hide show
  1. package/app/lib/request.js +18 -34
  2. package/package.json +1 -1
@@ -8,26 +8,11 @@ import getQueue from './queue.js';
8
8
  const {blue, cyan, dim, green, red, yellow} = chalk;
9
9
  const debug = _debug('mad:request');
10
10
 
11
- /**
12
- * @param {object} opts
13
- */
14
- const prepareRequestOpts = (opts = {}) => {
15
- const UA = 'curl/8.1.2';
16
-
17
- if (opts.dnsCache === undefined) {
18
- opts.dnsCache = true;
19
- }
20
-
21
- if (!opts.timeout) {
22
- opts.timeout = {request: 15_000};
23
- }
24
-
25
- if (!opts.headers) {
26
- opts.headers = {'user-agent': UA};
27
- } else if (!opts.headers['user-agent']) {
28
- opts.headers['user-agent'] = UA;
29
- }
30
- };
11
+ const gotDefault = got.extend({
12
+ dnsCache: true,
13
+ timeout: {request: 15_000},
14
+ headers: {'user-agent': 'curl/8.1.2'},
15
+ });
31
16
 
32
17
  /**
33
18
  * Отправить запрос
@@ -36,10 +21,8 @@ const prepareRequestOpts = (opts = {}) => {
36
21
  * @returns {object}
37
22
  */
38
23
  const sendRequest = async (url, opts) => {
39
- prepareRequestOpts(opts);
40
-
41
24
  try {
42
- const response = await got(url, opts);
25
+ const response = await gotDefault(url, opts);
43
26
 
44
27
  if (!opts.responseType) {
45
28
  try {
@@ -52,22 +35,25 @@ const sendRequest = async (url, opts) => {
52
35
  } catch (err) {
53
36
  debug(getCurl(url, opts, err));
54
37
 
55
- err.__pretty = {};
56
-
57
- err.__pretty.req = [
58
- err?.response?.statusCode,
38
+ err.__req = [
39
+ err?.response?.statusCode || err?.code,
59
40
  err?.options?.method,
60
41
  url,
61
- err?.response?.ip ? `(${err.response.ip})` : '',
62
- ].join(' ');
42
+ ].join(' ').trim();
63
43
 
64
- err.__pretty.opts = opts;
44
+ if (err?.response?.ip) {
45
+ err.__ip = err.response.ip;
46
+ }
47
+
48
+ if (Object.keys(opts).length > 0) {
49
+ err.__opts = opts;
50
+ }
65
51
 
66
52
  if (err?.response?.body) {
67
53
  try {
68
- err.__pretty.body = JSON.parse(err.response.body);
54
+ err.__res = JSON.parse(err.response.body);
69
55
  } catch {
70
- err.__pretty.body = err.response.body;
56
+ err.__res = err.response.body;
71
57
  }
72
58
  }
73
59
 
@@ -110,8 +96,6 @@ export const requestCache = (url, opts = {}, {cacheBy, expire = 43_200} = {}) =>
110
96
  const queue = getQueue(new URL(url).host, opts.method);
111
97
 
112
98
  return queue.add(async () => {
113
- prepareRequestOpts(opts);
114
-
115
99
  const cacheGotResponseKeys = [
116
100
  'body',
117
101
  'headers',
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@k03mad/request",
3
- "version": "2.0.0",
3
+ "version": "2.1.0",
4
4
  "description": "Request library",
5
5
  "maintainers": [
6
6
  "Kirill Molchanov <k03.mad@gmail.com"