@k03mad/request 1.3.2 → 2.0.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.
@@ -10,28 +10,23 @@ const debug = _debug('mad:request');
10
10
 
11
11
  /**
12
12
  * @param {object} opts
13
- * @returns {object}
14
13
  */
15
14
  const prepareRequestOpts = (opts = {}) => {
16
15
  const UA = 'curl/8.1.2';
17
16
 
18
- const preparedOpts = {...opts};
19
-
20
- if (preparedOpts.dnsCache === undefined) {
21
- preparedOpts.dnsCache = true;
17
+ if (opts.dnsCache === undefined) {
18
+ opts.dnsCache = true;
22
19
  }
23
20
 
24
- if (!preparedOpts.timeout) {
25
- preparedOpts.timeout = {request: 10_000};
21
+ if (!opts.timeout) {
22
+ opts.timeout = {request: 15_000};
26
23
  }
27
24
 
28
- if (!preparedOpts.headers) {
29
- preparedOpts.headers = {'user-agent': UA};
30
- } else if (!preparedOpts.headers['user-agent']) {
31
- preparedOpts.headers['user-agent'] = UA;
25
+ if (!opts.headers) {
26
+ opts.headers = {'user-agent': UA};
27
+ } else if (!opts.headers['user-agent']) {
28
+ opts.headers['user-agent'] = UA;
32
29
  }
33
-
34
- return preparedOpts;
35
30
  };
36
31
 
37
32
  /**
@@ -41,21 +36,21 @@ const prepareRequestOpts = (opts = {}) => {
41
36
  * @returns {object}
42
37
  */
43
38
  const sendRequest = async (url, opts) => {
44
- const preparedOpts = prepareRequestOpts(opts);
39
+ prepareRequestOpts(opts);
45
40
 
46
41
  try {
47
- const response = await got(url, preparedOpts);
42
+ const response = await got(url, opts);
48
43
 
49
- if (!preparedOpts.responseType) {
44
+ if (!opts.responseType) {
50
45
  try {
51
46
  response.body = JSON.parse(response.body);
52
47
  } catch {}
53
48
  }
54
49
 
55
- debug(getCurl(url, preparedOpts, response));
50
+ debug(getCurl(url, opts, response));
56
51
  return response;
57
52
  } catch (err) {
58
- debug(getCurl(url, preparedOpts, err));
53
+ debug(getCurl(url, opts, err));
59
54
 
60
55
  err.__pretty = {};
61
56
 
@@ -66,9 +61,7 @@ const sendRequest = async (url, opts) => {
66
61
  err?.response?.ip ? `(${err.response.ip})` : '',
67
62
  ].join(' ');
68
63
 
69
- if (Object.keys(opts).length > 0) {
70
- err.__pretty.opts = opts;
71
- }
64
+ err.__pretty.opts = opts;
72
65
 
73
66
  if (err?.response?.body) {
74
67
  try {
@@ -117,7 +110,7 @@ export const requestCache = (url, opts = {}, {cacheBy, expire = 43_200} = {}) =>
117
110
  const queue = getQueue(new URL(url).host, opts.method);
118
111
 
119
112
  return queue.add(async () => {
120
- const preparedOpts = prepareRequestOpts(opts);
113
+ prepareRequestOpts(opts);
121
114
 
122
115
  const cacheGotResponseKeys = [
123
116
  'body',
@@ -128,7 +121,7 @@ export const requestCache = (url, opts = {}, {cacheBy, expire = 43_200} = {}) =>
128
121
  'timings',
129
122
  ];
130
123
 
131
- const cacheKey = `${url}::${JSON.stringify(cacheBy || preparedOpts)}`;
124
+ const cacheKey = `${url}::${JSON.stringify(cacheBy || opts)}`;
132
125
  const log = `${blue(url)}\n${dim(cacheKey)}`;
133
126
 
134
127
  try {
@@ -151,7 +144,7 @@ export const requestCache = (url, opts = {}, {cacheBy, expire = 43_200} = {}) =>
151
144
  debug(`${red('CACHE ERROR')} :: ${dim(err)} :: ${log}`);
152
145
  }
153
146
 
154
- const res = await request(url, preparedOpts, {skipQueue: true});
147
+ const res = await request(url, opts, {skipQueue: true});
155
148
 
156
149
  const cachedResponse = {};
157
150
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@k03mad/request",
3
- "version": "1.3.2",
3
+ "version": "2.0.0",
4
4
  "description": "Request library",
5
5
  "maintainers": [
6
6
  "Kirill Molchanov <k03.mad@gmail.com"
@@ -1,44 +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: Git status
31
- run: git status
32
-
33
- - name: Update version
34
- run: npm version patch
35
-
36
- - name: Publish version
37
- uses: JS-DevTools/npm-publish@v2
38
- with:
39
- token: ${{ secrets.NPM_TOKEN }}
40
-
41
- - name: Create PR
42
- uses: peter-evans/create-pull-request@v5
43
- with:
44
- title: Patch package version