@naturalcycles/js-lib 14.149.0 → 14.149.2

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.
@@ -1,4 +1,3 @@
1
- import type { RecursiveArray } from '../lodash.types';
2
1
  import type { FalsyValue, Mapper, Predicate, StringMap } from '../types';
3
2
  /**
4
3
  * Creates an array of elements split into groups the length of size. If collection can’t be split evenly, the
@@ -13,24 +12,6 @@ import type { FalsyValue, Mapper, Predicate, StringMap } from '../types';
13
12
  * Based on: https://github.com/you-dont-need/You-Dont-Need-Lodash-Underscore#_chunk
14
13
  */
15
14
  export declare function _chunk<T>(array: readonly T[], size?: number): T[][];
16
- /**
17
- * Polyfill to Array.flat() with depth=1.
18
- * https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/flat
19
- *
20
- * @deprecated prefer native Array.flat(), it's supported since iOS 12
21
- */
22
- export declare function _flatten<T>(arrays: T[][]): T[];
23
- /**
24
- * Recursively flattens a nested array.
25
- *
26
- * @param arr The array to recursively flatten.
27
- * @return Returns the new flattened array.
28
- *
29
- * Based on: https://github.com/you-dont-need/You-Dont-Need-Lodash-Underscore#_flattendeep
30
- *
31
- * @deprecated prefer native Array.flat(), it's supported since iOS 12
32
- */
33
- export declare function _flattenDeep<T>(arr: RecursiveArray<T>): T[];
34
15
  /**
35
16
  * Removes duplicates from given array.
36
17
  */
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports._minByOrUndefined = exports._maxByOrUndefined = exports._minBy = exports._maxBy = exports._max = exports._maxOrUndefined = exports._min = exports._minOrUndefined = exports._lastOrUndefined = exports._last = exports._shuffle = exports._mapToObject = exports._sumBy = exports._sum = exports._difference = exports._intersection = exports._countBy = exports._dropRightWhile = exports._dropWhile = exports._takeRightWhile = exports._takeWhile = exports._findLast = exports._sortBy = exports._groupBy = exports._by = exports._uniqBy = exports._uniq = exports._flattenDeep = exports._flatten = exports._chunk = void 0;
3
+ exports._minByOrUndefined = exports._maxByOrUndefined = exports._minBy = exports._maxBy = exports._max = exports._maxOrUndefined = exports._min = exports._minOrUndefined = exports._lastOrUndefined = exports._last = exports._shuffle = exports._mapToObject = exports._sumBy = exports._sum = exports._difference = exports._intersection = exports._countBy = exports._dropRightWhile = exports._dropWhile = exports._takeRightWhile = exports._takeWhile = exports._findLast = exports._sortBy = exports._groupBy = exports._by = exports._uniqBy = exports._uniq = exports._chunk = void 0;
4
4
  const is_util_1 = require("../is.util");
5
5
  /**
6
6
  * Creates an array of elements split into groups the length of size. If collection can’t be split evenly, the
@@ -20,33 +20,6 @@ function _chunk(array, size = 1) {
20
20
  }, []);
21
21
  }
22
22
  exports._chunk = _chunk;
23
- /**
24
- * Polyfill to Array.flat() with depth=1.
25
- * https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/flat
26
- *
27
- * @deprecated prefer native Array.flat(), it's supported since iOS 12
28
- */
29
- function _flatten(arrays) {
30
- // to flat single level array
31
- return [].concat(...arrays);
32
- }
33
- exports._flatten = _flatten;
34
- /**
35
- * Recursively flattens a nested array.
36
- *
37
- * @param arr The array to recursively flatten.
38
- * @return Returns the new flattened array.
39
- *
40
- * Based on: https://github.com/you-dont-need/You-Dont-Need-Lodash-Underscore#_flattendeep
41
- *
42
- * @deprecated prefer native Array.flat(), it's supported since iOS 12
43
- */
44
- function _flattenDeep(arr) {
45
- return Array.isArray(arr)
46
- ? arr.reduce((a, b) => a.concat(_flattenDeep(b)), [])
47
- : [arr];
48
- }
49
- exports._flattenDeep = _flattenDeep;
50
23
  /**
51
24
  * Removes duplicates from given array.
52
25
  */
@@ -297,23 +297,24 @@ class Fetcher {
297
297
  if (retryStatus.retryAttempt >= count) {
298
298
  retryStatus.retryStopped = true;
299
299
  }
300
- if (retryStatus.retryStopped)
301
- return;
302
- // Here we know that more retries will be attempted
303
300
  // We don't log "last error", because it will be thrown and logged by consumer,
304
301
  // but we should log all previous errors, otherwise they are lost.
305
- // Here is the right place where we know it's not the "last error"
306
- if (res.err) {
302
+ // Here is the right place where we know it's not the "last error".
303
+ // lastError = retryStatus.retryStopped
304
+ // We need to log the response "anyway" if logResponse is true
305
+ if (res.err && (!retryStatus.retryStopped || res.req.logResponse)) {
307
306
  this.cfg.logger.error([
308
307
  ' <<',
309
308
  res.fetchResponse?.status || 0,
310
309
  res.signature,
311
- `try#${retryStatus.retryAttempt + 1}/${count + 1}`,
310
+ count && `try#${retryStatus.retryAttempt + 1}/${count + 1}`,
312
311
  (0, time_util_1._since)(res.req.started),
313
312
  ]
314
313
  .filter(Boolean)
315
314
  .join(' '), res.err.cause || res.err);
316
315
  }
316
+ if (retryStatus.retryStopped)
317
+ return;
317
318
  retryStatus.retryAttempt++;
318
319
  retryStatus.retryTimeout = (0, number_util_1._clamp)(retryStatus.retryTimeout * timeoutMultiplier, 0, timeoutMax);
319
320
  await (0, pDelay_1.pDelay)(this.getRetryTimeout(res));
@@ -440,21 +441,26 @@ class Fetcher {
440
441
  return norm;
441
442
  }
442
443
  normalizeOptions(opt) {
443
- const { timeoutSeconds, throwHttpErrors, retryPost, retry4xx, retry5xx, retry, mode, jsonReviver, } = this.cfg;
444
444
  const req = {
445
+ ...(0, object_util_1._pick)(this.cfg, [
446
+ 'timeoutSeconds',
447
+ 'throwHttpErrors',
448
+ 'retryPost',
449
+ 'retry4xx',
450
+ 'retry5xx',
451
+ 'mode',
452
+ 'jsonReviver',
453
+ 'logRequest',
454
+ 'logRequestBody',
455
+ 'logResponse',
456
+ 'logResponseBody',
457
+ ]),
445
458
  started: Date.now(),
446
- mode,
447
- timeoutSeconds,
448
- throwHttpErrors,
449
- retryPost,
450
- retry4xx,
451
- retry5xx,
452
- jsonReviver,
453
459
  ...(0, object_util_1._omit)(opt, ['method', 'headers', 'credentials']),
454
460
  inputUrl: opt.url || '',
455
461
  fullUrl: opt.url || '',
456
462
  retry: {
457
- ...retry,
463
+ ...this.cfg.retry,
458
464
  ...(0, object_util_1._filterUndefinedValues)(opt.retry || {}),
459
465
  },
460
466
  init: (0, object_util_1._merge)({
@@ -2,7 +2,7 @@ import type { CommonLogger } from '../log/commonLogger';
2
2
  import type { Promisable } from '../typeFest';
3
3
  import type { Reviver, UnixTimestampMillisNumber } from '../types';
4
4
  import type { HttpMethod, HttpStatusFamily } from './http.model';
5
- export interface FetcherNormalizedCfg extends Required<FetcherCfg>, Omit<FetcherRequest, 'started' | 'fullUrl'> {
5
+ export interface FetcherNormalizedCfg extends Required<FetcherCfg>, Omit<FetcherRequest, 'started' | 'fullUrl' | 'logRequest' | 'logRequestBody' | 'logResponse' | 'logResponseBody'> {
6
6
  logger: CommonLogger;
7
7
  searchParams: Record<string, any>;
8
8
  }
@@ -148,6 +148,10 @@ export interface FetcherOptions {
148
148
  */
149
149
  retry5xx?: boolean;
150
150
  jsonReviver?: Reviver;
151
+ logRequest?: boolean;
152
+ logRequestBody?: boolean;
153
+ logResponse?: boolean;
154
+ logResponseBody?: boolean;
151
155
  }
152
156
  export type RequestInitNormalized = Omit<RequestInit, 'method' | 'headers'> & {
153
157
  method: HttpMethod;
package/dist/types.d.ts CHANGED
@@ -137,10 +137,6 @@ export interface InstanceId {
137
137
  * @example '2019-06-21'
138
138
  */
139
139
  export type IsoDateString = string;
140
- /**
141
- * @deprecated use IsoDateString
142
- */
143
- export type IsoDate = string;
144
140
  /**
145
141
  * Interface explicitly states that the value is an ISO DateTime string (with time).
146
142
  *
@@ -159,10 +155,6 @@ export type UnixTimestampNumber = number;
159
155
  * @example 1628945450000
160
156
  */
161
157
  export type UnixTimestampMillisNumber = number;
162
- /**
163
- * @deprecated use UnixTimestampNumber
164
- */
165
- export type UnixTimestamp = number;
166
158
  export type NumberOfSeconds = number;
167
159
  export type NumberOfMilliseconds = number;
168
160
  /**
@@ -16,31 +16,6 @@ export function _chunk(array, size = 1) {
16
16
  return idx % size === 0 ? [...arr, [item]] : [...arr.slice(0, -1), [...arr.slice(-1)[0], item]];
17
17
  }, []);
18
18
  }
19
- /**
20
- * Polyfill to Array.flat() with depth=1.
21
- * https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/flat
22
- *
23
- * @deprecated prefer native Array.flat(), it's supported since iOS 12
24
- */
25
- export function _flatten(arrays) {
26
- // to flat single level array
27
- return [].concat(...arrays);
28
- }
29
- /**
30
- * Recursively flattens a nested array.
31
- *
32
- * @param arr The array to recursively flatten.
33
- * @return Returns the new flattened array.
34
- *
35
- * Based on: https://github.com/you-dont-need/You-Dont-Need-Lodash-Underscore#_flattendeep
36
- *
37
- * @deprecated prefer native Array.flat(), it's supported since iOS 12
38
- */
39
- export function _flattenDeep(arr) {
40
- return Array.isArray(arr)
41
- ? arr.reduce((a, b) => a.concat(_flattenDeep(b)), [])
42
- : [arr];
43
- }
44
19
  /**
45
20
  * Removes duplicates from given array.
46
21
  */
@@ -3,7 +3,7 @@ import { isServerSide } from '../env';
3
3
  import { _anyToError, _anyToErrorObject, _errorToErrorObject } from '../error/error.util';
4
4
  import { HttpRequestError } from '../error/httpRequestError';
5
5
  import { _clamp } from '../number/number.util';
6
- import { _filterNullishValues, _filterUndefinedValues, _mapKeys, _merge, _omit, } from '../object/object.util';
6
+ import { _filterNullishValues, _filterUndefinedValues, _mapKeys, _merge, _omit, _pick, } from '../object/object.util';
7
7
  import { pDelay } from '../promise/pDelay';
8
8
  import { _jsonParse, _jsonParseIfPossible } from '../string/json.util';
9
9
  import { _since } from '../time/time.util';
@@ -284,23 +284,24 @@ export class Fetcher {
284
284
  if (retryStatus.retryAttempt >= count) {
285
285
  retryStatus.retryStopped = true;
286
286
  }
287
- if (retryStatus.retryStopped)
288
- return;
289
- // Here we know that more retries will be attempted
290
287
  // We don't log "last error", because it will be thrown and logged by consumer,
291
288
  // but we should log all previous errors, otherwise they are lost.
292
- // Here is the right place where we know it's not the "last error"
293
- if (res.err) {
289
+ // Here is the right place where we know it's not the "last error".
290
+ // lastError = retryStatus.retryStopped
291
+ // We need to log the response "anyway" if logResponse is true
292
+ if (res.err && (!retryStatus.retryStopped || res.req.logResponse)) {
294
293
  this.cfg.logger.error([
295
294
  ' <<',
296
295
  ((_a = res.fetchResponse) === null || _a === void 0 ? void 0 : _a.status) || 0,
297
296
  res.signature,
298
- `try#${retryStatus.retryAttempt + 1}/${count + 1}`,
297
+ count && `try#${retryStatus.retryAttempt + 1}/${count + 1}`,
299
298
  _since(res.req.started),
300
299
  ]
301
300
  .filter(Boolean)
302
301
  .join(' '), res.err.cause || res.err);
303
302
  }
303
+ if (retryStatus.retryStopped)
304
+ return;
304
305
  retryStatus.retryAttempt++;
305
306
  retryStatus.retryTimeout = _clamp(retryStatus.retryTimeout * timeoutMultiplier, 0, timeoutMax);
306
307
  await pDelay(this.getRetryTimeout(res));
@@ -431,14 +432,19 @@ export class Fetcher {
431
432
  }
432
433
  normalizeOptions(opt) {
433
434
  var _a, _b;
434
- const { timeoutSeconds, throwHttpErrors, retryPost, retry4xx, retry5xx, retry, mode, jsonReviver, } = this.cfg;
435
- const req = Object.assign(Object.assign({ started: Date.now(), mode,
436
- timeoutSeconds,
437
- throwHttpErrors,
438
- retryPost,
439
- retry4xx,
440
- retry5xx,
441
- jsonReviver }, _omit(opt, ['method', 'headers', 'credentials'])), { inputUrl: opt.url || '', fullUrl: opt.url || '', retry: Object.assign(Object.assign({}, retry), _filterUndefinedValues(opt.retry || {})), init: _merge(Object.assign(Object.assign({}, this.cfg.init), { method: opt.method || this.cfg.init.method, credentials: opt.credentials || this.cfg.init.credentials, redirect: ((_b = (_a = opt.followRedirects) !== null && _a !== void 0 ? _a : this.cfg.followRedirects) !== null && _b !== void 0 ? _b : true) ? 'follow' : 'error' }), {
435
+ const req = Object.assign(Object.assign(Object.assign(Object.assign({}, _pick(this.cfg, [
436
+ 'timeoutSeconds',
437
+ 'throwHttpErrors',
438
+ 'retryPost',
439
+ 'retry4xx',
440
+ 'retry5xx',
441
+ 'mode',
442
+ 'jsonReviver',
443
+ 'logRequest',
444
+ 'logRequestBody',
445
+ 'logResponse',
446
+ 'logResponseBody',
447
+ ])), { started: Date.now() }), _omit(opt, ['method', 'headers', 'credentials'])), { inputUrl: opt.url || '', fullUrl: opt.url || '', retry: Object.assign(Object.assign({}, this.cfg.retry), _filterUndefinedValues(opt.retry || {})), init: _merge(Object.assign(Object.assign({}, this.cfg.init), { method: opt.method || this.cfg.init.method, credentials: opt.credentials || this.cfg.init.credentials, redirect: ((_b = (_a = opt.followRedirects) !== null && _a !== void 0 ? _a : this.cfg.followRedirects) !== null && _b !== void 0 ? _b : true) ? 'follow' : 'error' }), {
442
448
  headers: _mapKeys(opt.headers || {}, k => k.toLowerCase()),
443
449
  }) });
444
450
  // setup url
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@naturalcycles/js-lib",
3
- "version": "14.149.0",
3
+ "version": "14.149.2",
4
4
  "scripts": {
5
5
  "prepare": "husky install",
6
6
  "build-prod": "build-prod-esm-cjs",
@@ -39,6 +39,7 @@
39
39
  "node": ">=18.12.0"
40
40
  },
41
41
  "publishConfig": {
42
+ "provenance": true,
42
43
  "access": "public"
43
44
  },
44
45
  "repository": {
@@ -1,5 +1,4 @@
1
1
  import { _isNotNullish } from '../is.util'
2
- import type { RecursiveArray } from '../lodash.types'
3
2
  import type { FalsyValue, Mapper, Predicate, StringMap } from '../types'
4
3
 
5
4
  /**
@@ -20,33 +19,6 @@ export function _chunk<T>(array: readonly T[], size = 1): T[][] {
20
19
  }, [] as T[][])
21
20
  }
22
21
 
23
- /**
24
- * Polyfill to Array.flat() with depth=1.
25
- * https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/flat
26
- *
27
- * @deprecated prefer native Array.flat(), it's supported since iOS 12
28
- */
29
- export function _flatten<T>(arrays: T[][]): T[] {
30
- // to flat single level array
31
- return ([] as T[]).concat(...arrays)
32
- }
33
-
34
- /**
35
- * Recursively flattens a nested array.
36
- *
37
- * @param arr The array to recursively flatten.
38
- * @return Returns the new flattened array.
39
- *
40
- * Based on: https://github.com/you-dont-need/You-Dont-Need-Lodash-Underscore#_flattendeep
41
- *
42
- * @deprecated prefer native Array.flat(), it's supported since iOS 12
43
- */
44
- export function _flattenDeep<T>(arr: RecursiveArray<T>): T[] {
45
- return Array.isArray(arr)
46
- ? arr.reduce((a: RecursiveArray<T>, b) => a.concat(_flattenDeep(b as RecursiveArray<T>)), [])
47
- : ([arr] as any)
48
- }
49
-
50
22
  /**
51
23
  * Removes duplicates from given array.
52
24
  */
@@ -5,7 +5,10 @@ import type { HttpMethod, HttpStatusFamily } from './http.model'
5
5
 
6
6
  export interface FetcherNormalizedCfg
7
7
  extends Required<FetcherCfg>,
8
- Omit<FetcherRequest, 'started' | 'fullUrl'> {
8
+ Omit<
9
+ FetcherRequest,
10
+ 'started' | 'fullUrl' | 'logRequest' | 'logRequestBody' | 'logResponse' | 'logResponseBody'
11
+ > {
9
12
  logger: CommonLogger
10
13
  searchParams: Record<string, any>
11
14
  }
@@ -181,6 +184,11 @@ export interface FetcherOptions {
181
184
  retry5xx?: boolean
182
185
 
183
186
  jsonReviver?: Reviver
187
+
188
+ logRequest?: boolean
189
+ logRequestBody?: boolean
190
+ logResponse?: boolean
191
+ logResponseBody?: boolean
184
192
  }
185
193
 
186
194
  export type RequestInitNormalized = Omit<RequestInit, 'method' | 'headers'> & {
@@ -11,6 +11,7 @@ import {
11
11
  _mapKeys,
12
12
  _merge,
13
13
  _omit,
14
+ _pick,
14
15
  } from '../object/object.util'
15
16
  import { pDelay } from '../promise/pDelay'
16
17
  import { _jsonParse, _jsonParseIfPossible } from '../string/json.util'
@@ -385,19 +386,18 @@ export class Fetcher {
385
386
  retryStatus.retryStopped = true
386
387
  }
387
388
 
388
- if (retryStatus.retryStopped) return
389
-
390
- // Here we know that more retries will be attempted
391
389
  // We don't log "last error", because it will be thrown and logged by consumer,
392
390
  // but we should log all previous errors, otherwise they are lost.
393
- // Here is the right place where we know it's not the "last error"
394
- if (res.err) {
391
+ // Here is the right place where we know it's not the "last error".
392
+ // lastError = retryStatus.retryStopped
393
+ // We need to log the response "anyway" if logResponse is true
394
+ if (res.err && (!retryStatus.retryStopped || res.req.logResponse)) {
395
395
  this.cfg.logger.error(
396
396
  [
397
397
  ' <<',
398
398
  res.fetchResponse?.status || 0,
399
399
  res.signature,
400
- `try#${retryStatus.retryAttempt + 1}/${count + 1}`,
400
+ count && `try#${retryStatus.retryAttempt + 1}/${count + 1}`,
401
401
  _since(res.req.started),
402
402
  ]
403
403
  .filter(Boolean)
@@ -406,6 +406,8 @@ export class Fetcher {
406
406
  )
407
407
  }
408
408
 
409
+ if (retryStatus.retryStopped) return
410
+
409
411
  retryStatus.retryAttempt++
410
412
  retryStatus.retryTimeout = _clamp(retryStatus.retryTimeout * timeoutMultiplier, 0, timeoutMax)
411
413
 
@@ -545,31 +547,26 @@ export class Fetcher {
545
547
  }
546
548
 
547
549
  private normalizeOptions(opt: FetcherOptions): FetcherRequest {
548
- const {
549
- timeoutSeconds,
550
- throwHttpErrors,
551
- retryPost,
552
- retry4xx,
553
- retry5xx,
554
- retry,
555
- mode,
556
- jsonReviver,
557
- } = this.cfg
558
-
559
550
  const req: FetcherRequest = {
551
+ ..._pick(this.cfg, [
552
+ 'timeoutSeconds',
553
+ 'throwHttpErrors',
554
+ 'retryPost',
555
+ 'retry4xx',
556
+ 'retry5xx',
557
+ 'mode',
558
+ 'jsonReviver',
559
+ 'logRequest',
560
+ 'logRequestBody',
561
+ 'logResponse',
562
+ 'logResponseBody',
563
+ ]),
560
564
  started: Date.now(),
561
- mode,
562
- timeoutSeconds,
563
- throwHttpErrors,
564
- retryPost,
565
- retry4xx,
566
- retry5xx,
567
- jsonReviver,
568
565
  ..._omit(opt, ['method', 'headers', 'credentials']),
569
566
  inputUrl: opt.url || '',
570
567
  fullUrl: opt.url || '',
571
568
  retry: {
572
- ...retry,
569
+ ...this.cfg.retry,
573
570
  ..._filterUndefinedValues(opt.retry || {}),
574
571
  },
575
572
  init: _merge(
@@ -584,7 +581,6 @@ export class Fetcher {
584
581
  } as RequestInit,
585
582
  ),
586
583
  }
587
-
588
584
  // setup url
589
585
  const baseUrl = opt.baseUrl || this.cfg.baseUrl
590
586
  if (baseUrl) {
package/src/types.ts CHANGED
@@ -191,11 +191,6 @@ export interface InstanceId {
191
191
  */
192
192
  export type IsoDateString = string
193
193
 
194
- /**
195
- * @deprecated use IsoDateString
196
- */
197
- export type IsoDate = string
198
-
199
194
  /**
200
195
  * Interface explicitly states that the value is an ISO DateTime string (with time).
201
196
  *
@@ -217,11 +212,6 @@ export type UnixTimestampNumber = number
217
212
  */
218
213
  export type UnixTimestampMillisNumber = number
219
214
 
220
- /**
221
- * @deprecated use UnixTimestampNumber
222
- */
223
- export type UnixTimestamp = number
224
-
225
215
  export type NumberOfSeconds = number
226
216
  export type NumberOfMilliseconds = number
227
217