@k03mad/request 1.3.3 → 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.
@@ -8,31 +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
- * @returns {object}
14
- */
15
- const prepareRequestOpts = (opts = {}) => {
16
- const UA = 'curl/8.1.2';
17
-
18
- const preparedOpts = {...opts};
19
-
20
- if (preparedOpts.dnsCache === undefined) {
21
- preparedOpts.dnsCache = true;
22
- }
23
-
24
- if (!preparedOpts.timeout) {
25
- preparedOpts.timeout = {request: 15_000};
26
- }
27
-
28
- if (!preparedOpts.headers) {
29
- preparedOpts.headers = {'user-agent': UA};
30
- } else if (!preparedOpts.headers['user-agent']) {
31
- preparedOpts.headers['user-agent'] = UA;
32
- }
33
-
34
- return preparedOpts;
35
- };
11
+ const gotDefault = got.extend({
12
+ dnsCache: true,
13
+ timeout: {request: 15_000},
14
+ headers: {'user-agent': 'curl/8.1.2'},
15
+ });
36
16
 
37
17
  /**
38
18
  * Отправить запрос
@@ -41,40 +21,39 @@ const prepareRequestOpts = (opts = {}) => {
41
21
  * @returns {object}
42
22
  */
43
23
  const sendRequest = async (url, opts) => {
44
- const preparedOpts = prepareRequestOpts(opts);
45
-
46
24
  try {
47
- const response = await got(url, preparedOpts);
25
+ const response = await gotDefault(url, opts);
48
26
 
49
- if (!preparedOpts.responseType) {
27
+ if (!opts.responseType) {
50
28
  try {
51
29
  response.body = JSON.parse(response.body);
52
30
  } catch {}
53
31
  }
54
32
 
55
- debug(getCurl(url, preparedOpts, response));
33
+ debug(getCurl(url, opts, response));
56
34
  return response;
57
35
  } catch (err) {
58
- debug(getCurl(url, preparedOpts, err));
36
+ debug(getCurl(url, opts, err));
59
37
 
60
- err.__pretty = {};
61
-
62
- err.__pretty.req = [
63
- err?.response?.statusCode,
38
+ err.__req = [
39
+ err?.response?.statusCode || err?.code,
64
40
  err?.options?.method,
65
41
  url,
66
- err?.response?.ip ? `(${err.response.ip})` : '',
67
- ].join(' ');
42
+ ].join(' ').trim();
43
+
44
+ if (err?.response?.ip) {
45
+ err.__ip = err.response.ip;
46
+ }
68
47
 
69
48
  if (Object.keys(opts).length > 0) {
70
- err.__pretty.opts = opts;
49
+ err.__opts = opts;
71
50
  }
72
51
 
73
52
  if (err?.response?.body) {
74
53
  try {
75
- err.__pretty.body = JSON.parse(err.response.body);
54
+ err.__res = JSON.parse(err.response.body);
76
55
  } catch {
77
- err.__pretty.body = err.response.body;
56
+ err.__res = err.response.body;
78
57
  }
79
58
  }
80
59
 
@@ -117,8 +96,6 @@ export const requestCache = (url, opts = {}, {cacheBy, expire = 43_200} = {}) =>
117
96
  const queue = getQueue(new URL(url).host, opts.method);
118
97
 
119
98
  return queue.add(async () => {
120
- const preparedOpts = prepareRequestOpts(opts);
121
-
122
99
  const cacheGotResponseKeys = [
123
100
  'body',
124
101
  'headers',
@@ -128,7 +105,7 @@ export const requestCache = (url, opts = {}, {cacheBy, expire = 43_200} = {}) =>
128
105
  'timings',
129
106
  ];
130
107
 
131
- const cacheKey = `${url}::${JSON.stringify(cacheBy || preparedOpts)}`;
108
+ const cacheKey = `${url}::${JSON.stringify(cacheBy || opts)}`;
132
109
  const log = `${blue(url)}\n${dim(cacheKey)}`;
133
110
 
134
111
  try {
@@ -151,7 +128,7 @@ export const requestCache = (url, opts = {}, {cacheBy, expire = 43_200} = {}) =>
151
128
  debug(`${red('CACHE ERROR')} :: ${dim(err)} :: ${log}`);
152
129
  }
153
130
 
154
- const res = await request(url, preparedOpts, {skipQueue: true});
131
+ const res = await request(url, opts, {skipQueue: true});
155
132
 
156
133
  const cachedResponse = {};
157
134
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@k03mad/request",
3
- "version": "1.3.3",
3
+ "version": "2.1.0",
4
4
  "description": "Request library",
5
5
  "maintainers": [
6
6
  "Kirill Molchanov <k03.mad@gmail.com"
@@ -1,41 +0,0 @@
1
- name: Publish
2
-
3
- on:
4
- push:
5
- branches:
6
- - master
7
- paths:
8
- - '**.js'
9
-
10
- jobs:
11
- eslint:
12
- name: Publish
13
- environment: npm
14
- runs-on: ubuntu-latest
15
- steps:
16
- - name: Checkout
17
- uses: actions/checkout@v3
18
-
19
- - name: Install NodeJS
20
- uses: actions/setup-node@v3
21
- with:
22
- node-version-file: '.nvmrc'
23
-
24
- - name: Install dependencies
25
- run: npm ci
26
-
27
- - name: Set credentials
28
- run: git config user.email "auto@action.autoaction_1" && git config user.name "Action"
29
-
30
- - name: Update version
31
- run: npm version patch
32
-
33
- - name: Publish version
34
- uses: JS-DevTools/npm-publish@v2
35
- with:
36
- token: ${{ secrets.NPM_TOKEN }}
37
-
38
- - name: Create PR
39
- uses: peter-evans/create-pull-request@v5
40
- with:
41
- title: Patch package version