@naturalcycles/js-lib 14.153.3 → 14.154.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,6 +8,7 @@ const httpRequestError_1 = require("../error/httpRequestError");
8
8
  const number_util_1 = require("../number/number.util");
9
9
  const object_util_1 = require("../object/object.util");
10
10
  const pDelay_1 = require("../promise/pDelay");
11
+ const pTimeout_1 = require("../promise/pTimeout");
11
12
  const json_util_1 = require("../string/json.util");
12
13
  const stringifyAny_1 = require("../string/stringifyAny");
13
14
  const time_util_1 = require("../time/time.util");
@@ -98,9 +99,7 @@ class Fetcher {
98
99
  async fetch(opt) {
99
100
  const res = await this.doFetch(opt);
100
101
  if (res.err) {
101
- if (res.req.throwHttpErrors)
102
- throw res.err;
103
- return res;
102
+ throw res.err;
104
103
  }
105
104
  return res.body;
106
105
  }
@@ -119,7 +118,11 @@ class Fetcher {
119
118
  const abortController = new AbortController();
120
119
  req.init.signal = abortController.signal;
121
120
  timeout = setTimeout(() => {
122
- abortController.abort(`timeout of ${timeoutSeconds} sec`);
121
+ // Apparently, providing a `string` reason to abort() causes Undici to throw `invalid_argument` error,
122
+ // so, we're wrapping it in a TimeoutError instance
123
+ abortController.abort(new pTimeout_1.TimeoutError(`request timed out after ${timeoutSeconds} sec`));
124
+ // abortController.abort(`timeout of ${timeoutSeconds} sec`)
125
+ // abortController.abort()
123
126
  }, timeoutSeconds * 1000);
124
127
  }
125
128
  for (const hook of this.cfg.hooks.beforeRequest || []) {
@@ -164,6 +167,7 @@ class Fetcher {
164
167
  res.fetchResponse = undefined;
165
168
  }
166
169
  res.statusFamily = this.getStatusFamily(res);
170
+ res.statusCode = res.fetchResponse?.status;
167
171
  if (res.fetchResponse?.ok) {
168
172
  await this.onOkResponse(res, timeout);
169
173
  }
@@ -246,7 +250,7 @@ class Fetcher {
246
250
  ]
247
251
  .filter(Boolean)
248
252
  .join(' '));
249
- if (this.cfg.logResponseBody) {
253
+ if (this.cfg.logResponseBody && res.body !== undefined) {
250
254
  logger.log(res.body);
251
255
  }
252
256
  }
@@ -428,7 +432,6 @@ class Fetcher {
428
432
  mode: 'void',
429
433
  searchParams: {},
430
434
  timeoutSeconds: 30,
431
- throwHttpErrors: true,
432
435
  retryPost: false,
433
436
  retry3xx: false,
434
437
  retry4xx: false,
@@ -458,7 +461,6 @@ class Fetcher {
458
461
  const req = {
459
462
  ...(0, object_util_1._pick)(this.cfg, [
460
463
  'timeoutSeconds',
461
- 'throwHttpErrors',
462
464
  'retryPost',
463
465
  'retry4xx',
464
466
  'retry5xx',
@@ -89,7 +89,6 @@ export interface FetcherRequest extends Omit<FetcherOptions, 'method' | 'headers
89
89
  fullUrl: string;
90
90
  init: RequestInitNormalized;
91
91
  mode: FetcherMode;
92
- throwHttpErrors: boolean;
93
92
  timeoutSeconds: number;
94
93
  retry: FetcherRetryOptions;
95
94
  retryPost: boolean;
@@ -106,7 +105,6 @@ export interface FetcherOptions {
106
105
  */
107
106
  url?: string;
108
107
  baseUrl?: string;
109
- throwHttpErrors?: boolean;
110
108
  /**
111
109
  * Default: 30.
112
110
  *
@@ -170,6 +168,7 @@ export interface FetcherSuccessResponse<BODY = unknown> {
170
168
  fetchResponse: Response;
171
169
  body: BODY;
172
170
  req: FetcherRequest;
171
+ statusCode: number;
173
172
  statusFamily?: HttpStatusFamily;
174
173
  retryStatus: FetcherRetryStatus;
175
174
  signature: string;
@@ -180,6 +179,7 @@ export interface FetcherErrorResponse<BODY = unknown> {
180
179
  fetchResponse?: Response;
181
180
  body?: BODY;
182
181
  req: FetcherRequest;
182
+ statusCode?: number;
183
183
  statusFamily?: HttpStatusFamily;
184
184
  retryStatus: FetcherRetryStatus;
185
185
  signature: string;
@@ -5,6 +5,7 @@ import { HttpRequestError } from '../error/httpRequestError';
5
5
  import { _clamp } from '../number/number.util';
6
6
  import { _filterNullishValues, _filterUndefinedValues, _mapKeys, _merge, _omit, _pick, } from '../object/object.util';
7
7
  import { pDelay } from '../promise/pDelay';
8
+ import { TimeoutError } from '../promise/pTimeout';
8
9
  import { _jsonParse, _jsonParseIfPossible } from '../string/json.util';
9
10
  import { _stringifyAny } from '../string/stringifyAny';
10
11
  import { _since } from '../time/time.util';
@@ -82,9 +83,7 @@ export class Fetcher {
82
83
  async fetch(opt) {
83
84
  const res = await this.doFetch(opt);
84
85
  if (res.err) {
85
- if (res.req.throwHttpErrors)
86
- throw res.err;
87
- return res;
86
+ throw res.err;
88
87
  }
89
88
  return res.body;
90
89
  }
@@ -94,7 +93,7 @@ export class Fetcher {
94
93
  * Use this method instead of `throwHttpErrors: false` or try-catching.
95
94
  */
96
95
  async doFetch(opt) {
97
- var _a;
96
+ var _a, _b;
98
97
  const req = this.normalizeOptions(opt);
99
98
  const { logger } = this.cfg;
100
99
  const { timeoutSeconds, init: { method }, } = req;
@@ -104,7 +103,11 @@ export class Fetcher {
104
103
  const abortController = new AbortController();
105
104
  req.init.signal = abortController.signal;
106
105
  timeout = setTimeout(() => {
107
- abortController.abort(`timeout of ${timeoutSeconds} sec`);
106
+ // Apparently, providing a `string` reason to abort() causes Undici to throw `invalid_argument` error,
107
+ // so, we're wrapping it in a TimeoutError instance
108
+ abortController.abort(new TimeoutError(`request timed out after ${timeoutSeconds} sec`));
109
+ // abortController.abort(`timeout of ${timeoutSeconds} sec`)
110
+ // abortController.abort()
108
111
  }, timeoutSeconds * 1000);
109
112
  }
110
113
  for (const hook of this.cfg.hooks.beforeRequest || []) {
@@ -149,7 +152,8 @@ export class Fetcher {
149
152
  res.fetchResponse = undefined;
150
153
  }
151
154
  res.statusFamily = this.getStatusFamily(res);
152
- if ((_a = res.fetchResponse) === null || _a === void 0 ? void 0 : _a.ok) {
155
+ res.statusCode = (_a = res.fetchResponse) === null || _a === void 0 ? void 0 : _a.status;
156
+ if ((_b = res.fetchResponse) === null || _b === void 0 ? void 0 : _b.ok) {
153
157
  await this.onOkResponse(res, timeout);
154
158
  }
155
159
  else {
@@ -231,7 +235,7 @@ export class Fetcher {
231
235
  ]
232
236
  .filter(Boolean)
233
237
  .join(' '));
234
- if (this.cfg.logResponseBody) {
238
+ if (this.cfg.logResponseBody && res.body !== undefined) {
235
239
  logger.log(res.body);
236
240
  }
237
241
  }
@@ -418,7 +422,6 @@ export class Fetcher {
418
422
  mode: 'void',
419
423
  searchParams: {},
420
424
  timeoutSeconds: 30,
421
- throwHttpErrors: true,
422
425
  retryPost: false,
423
426
  retry3xx: false,
424
427
  retry4xx: false,
@@ -447,7 +450,6 @@ export class Fetcher {
447
450
  normalizeOptions(opt) {
448
451
  const req = Object.assign(Object.assign(Object.assign(Object.assign({}, _pick(this.cfg, [
449
452
  'timeoutSeconds',
450
- 'throwHttpErrors',
451
453
  'retryPost',
452
454
  'retry4xx',
453
455
  'retry5xx',
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@naturalcycles/js-lib",
3
- "version": "14.153.3",
3
+ "version": "14.154.0",
4
4
  "scripts": {
5
5
  "prepare": "husky install",
6
6
  "build-prod": "build-prod-esm-cjs",
@@ -117,7 +117,6 @@ export interface FetcherRequest
117
117
  fullUrl: string
118
118
  init: RequestInitNormalized
119
119
  mode: FetcherMode
120
- throwHttpErrors: boolean
121
120
  timeoutSeconds: number
122
121
  retry: FetcherRetryOptions
123
122
  retryPost: boolean
@@ -138,7 +137,6 @@ export interface FetcherOptions {
138
137
 
139
138
  baseUrl?: string
140
139
 
141
- throwHttpErrors?: boolean
142
140
  /**
143
141
  * Default: 30.
144
142
  *
@@ -216,6 +214,7 @@ export interface FetcherSuccessResponse<BODY = unknown> {
216
214
  fetchResponse: Response
217
215
  body: BODY
218
216
  req: FetcherRequest
217
+ statusCode: number
219
218
  statusFamily?: HttpStatusFamily
220
219
  retryStatus: FetcherRetryStatus
221
220
  signature: string
@@ -227,6 +226,7 @@ export interface FetcherErrorResponse<BODY = unknown> {
227
226
  fetchResponse?: Response
228
227
  body?: BODY
229
228
  req: FetcherRequest
229
+ statusCode?: number
230
230
  statusFamily?: HttpStatusFamily
231
231
  retryStatus: FetcherRetryStatus
232
232
  signature: string
@@ -14,6 +14,7 @@ import {
14
14
  _pick,
15
15
  } from '../object/object.util'
16
16
  import { pDelay } from '../promise/pDelay'
17
+ import { TimeoutError } from '../promise/pTimeout'
17
18
  import { _jsonParse, _jsonParseIfPossible } from '../string/json.util'
18
19
  import { _stringifyAny } from '../string/stringifyAny'
19
20
  import { _since } from '../time/time.util'
@@ -152,8 +153,7 @@ export class Fetcher {
152
153
  async fetch<T = unknown>(opt: FetcherOptions): Promise<T> {
153
154
  const res = await this.doFetch<T>(opt)
154
155
  if (res.err) {
155
- if (res.req.throwHttpErrors) throw res.err
156
- return res as any
156
+ throw res.err
157
157
  }
158
158
  return res.body
159
159
  }
@@ -177,7 +177,11 @@ export class Fetcher {
177
177
  const abortController = new AbortController()
178
178
  req.init.signal = abortController.signal
179
179
  timeout = setTimeout(() => {
180
- abortController.abort(`timeout of ${timeoutSeconds} sec`)
180
+ // Apparently, providing a `string` reason to abort() causes Undici to throw `invalid_argument` error,
181
+ // so, we're wrapping it in a TimeoutError instance
182
+ abortController.abort(new TimeoutError(`request timed out after ${timeoutSeconds} sec`))
183
+ // abortController.abort(`timeout of ${timeoutSeconds} sec`)
184
+ // abortController.abort()
181
185
  }, timeoutSeconds * 1000) as any as number
182
186
  }
183
187
 
@@ -229,6 +233,7 @@ export class Fetcher {
229
233
  res.fetchResponse = undefined
230
234
  }
231
235
  res.statusFamily = this.getStatusFamily(res)
236
+ res.statusCode = res.fetchResponse?.status
232
237
 
233
238
  if (res.fetchResponse?.ok) {
234
239
  await this.onOkResponse(res as FetcherResponse<T> & { fetchResponse: Response }, timeout)
@@ -319,7 +324,7 @@ export class Fetcher {
319
324
  .join(' '),
320
325
  )
321
326
 
322
- if (this.cfg.logResponseBody) {
327
+ if (this.cfg.logResponseBody && res.body !== undefined) {
323
328
  logger.log(res.body)
324
329
  }
325
330
  }
@@ -529,7 +534,6 @@ export class Fetcher {
529
534
  mode: 'void',
530
535
  searchParams: {},
531
536
  timeoutSeconds: 30,
532
- throwHttpErrors: true,
533
537
  retryPost: false,
534
538
  retry3xx: false,
535
539
  retry4xx: false,
@@ -564,7 +568,6 @@ export class Fetcher {
564
568
  const req: FetcherRequest = {
565
569
  ..._pick(this.cfg, [
566
570
  'timeoutSeconds',
567
- 'throwHttpErrors',
568
571
  'retryPost',
569
572
  'retry4xx',
570
573
  'retry5xx',