@naturalcycles/js-lib 14.76.0 → 14.77.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.
@@ -1,5 +1,10 @@
1
- import { CommonLogger } from '..';
1
+ import { AnyFunction, CommonLogger } from '..';
2
2
  export interface PRetryOptions {
3
+ /**
4
+ * If set - will be included in the error message.
5
+ * Can be used to identify the place in the code that failed.
6
+ */
7
+ name?: string;
3
8
  /**
4
9
  * How many attempts to try.
5
10
  * First attempt is not a retry, but "initial try". It still counts.
@@ -62,4 +67,4 @@ export interface PRetryOptions {
62
67
  * Returns a Function (!), enhanced with retry capabilities.
63
68
  * Implements "Exponential back-off strategy" by multiplying the delay by `delayMultiplier` with each try.
64
69
  */
65
- export declare function pRetry<T extends Function>(fn: T, opt?: PRetryOptions): T;
70
+ export declare function pRetry<T extends AnyFunction>(fn: T, opt?: PRetryOptions): T;
@@ -8,7 +8,7 @@ const __1 = require("..");
8
8
  */
9
9
  // eslint-disable-next-line @typescript-eslint/ban-types
10
10
  function pRetry(fn, opt = {}) {
11
- const { maxAttempts = 4, delay: initialDelay = 1000, delayMultiplier = 2, predicate, logger = console, } = opt;
11
+ const { maxAttempts = 4, delay: initialDelay = 1000, delayMultiplier = 2, predicate, logger = console, name = fn.name, } = opt;
12
12
  let { logFirstAttempt = false, logRetries = true, logFailures = false, logSuccess = false } = opt;
13
13
  if (opt.logAll) {
14
14
  logFirstAttempt = logRetries = logFailures = true;
@@ -16,7 +16,7 @@ function pRetry(fn, opt = {}) {
16
16
  if (opt.logNone) {
17
17
  logSuccess = logFirstAttempt = logRetries = logFailures = false;
18
18
  }
19
- const fname = ['pRetry', fn.name].filter(Boolean).join('.');
19
+ const fname = ['pRetry', name].filter(Boolean).join('.');
20
20
  return async function (...args) {
21
21
  let delay = initialDelay;
22
22
  let attempt = 0;
@@ -36,9 +36,9 @@ function pRetry(fn, opt = {}) {
36
36
  }
37
37
  catch (err) {
38
38
  if (logFailures) {
39
- logger.warn(`${fname} attempt #${attempt} error in ${(0, __1._since)(started)}:\n${(0, __1._stringifyAny)(err, {
39
+ logger.warn(`${fname} attempt #${attempt} error in ${(0, __1._since)(started)}:`, (0, __1._stringifyAny)(err, {
40
40
  includeErrorData: true,
41
- })}`);
41
+ }));
42
42
  }
43
43
  if (attempt >= maxAttempts || (predicate && !predicate(err, attempt, maxAttempts))) {
44
44
  // Give up
@@ -5,7 +5,7 @@ import { _since, _stringifyAny } from '..';
5
5
  */
6
6
  // eslint-disable-next-line @typescript-eslint/ban-types
7
7
  export function pRetry(fn, opt = {}) {
8
- const { maxAttempts = 4, delay: initialDelay = 1000, delayMultiplier = 2, predicate, logger = console, } = opt;
8
+ const { maxAttempts = 4, delay: initialDelay = 1000, delayMultiplier = 2, predicate, logger = console, name = fn.name, } = opt;
9
9
  let { logFirstAttempt = false, logRetries = true, logFailures = false, logSuccess = false } = opt;
10
10
  if (opt.logAll) {
11
11
  logFirstAttempt = logRetries = logFailures = true;
@@ -13,7 +13,7 @@ export function pRetry(fn, opt = {}) {
13
13
  if (opt.logNone) {
14
14
  logSuccess = logFirstAttempt = logRetries = logFailures = false;
15
15
  }
16
- const fname = ['pRetry', fn.name].filter(Boolean).join('.');
16
+ const fname = ['pRetry', name].filter(Boolean).join('.');
17
17
  return async function (...args) {
18
18
  let delay = initialDelay;
19
19
  let attempt = 0;
@@ -33,9 +33,9 @@ export function pRetry(fn, opt = {}) {
33
33
  }
34
34
  catch (err) {
35
35
  if (logFailures) {
36
- logger.warn(`${fname} attempt #${attempt} error in ${_since(started)}:\n${_stringifyAny(err, {
36
+ logger.warn(`${fname} attempt #${attempt} error in ${_since(started)}:`, _stringifyAny(err, {
37
37
  includeErrorData: true,
38
- })}`);
38
+ }));
39
39
  }
40
40
  if (attempt >= maxAttempts || (predicate && !predicate(err, attempt, maxAttempts))) {
41
41
  // Give up
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@naturalcycles/js-lib",
3
- "version": "14.76.0",
3
+ "version": "14.77.0",
4
4
  "scripts": {
5
5
  "prepare": "husky install",
6
6
  "build-prod": "build-prod-esm-cjs",
@@ -1,6 +1,12 @@
1
- import { _since, _stringifyAny, CommonLogger } from '..'
1
+ import { _since, _stringifyAny, AnyFunction, CommonLogger } from '..'
2
2
 
3
3
  export interface PRetryOptions {
4
+ /**
5
+ * If set - will be included in the error message.
6
+ * Can be used to identify the place in the code that failed.
7
+ */
8
+ name?: string
9
+
4
10
  /**
5
11
  * How many attempts to try.
6
12
  * First attempt is not a retry, but "initial try". It still counts.
@@ -75,13 +81,14 @@ export interface PRetryOptions {
75
81
  * Implements "Exponential back-off strategy" by multiplying the delay by `delayMultiplier` with each try.
76
82
  */
77
83
  // eslint-disable-next-line @typescript-eslint/ban-types
78
- export function pRetry<T extends Function>(fn: T, opt: PRetryOptions = {}): T {
84
+ export function pRetry<T extends AnyFunction>(fn: T, opt: PRetryOptions = {}): T {
79
85
  const {
80
86
  maxAttempts = 4,
81
87
  delay: initialDelay = 1000,
82
88
  delayMultiplier = 2,
83
89
  predicate,
84
90
  logger = console,
91
+ name = fn.name,
85
92
  } = opt
86
93
 
87
94
  let { logFirstAttempt = false, logRetries = true, logFailures = false, logSuccess = false } = opt
@@ -93,7 +100,7 @@ export function pRetry<T extends Function>(fn: T, opt: PRetryOptions = {}): T {
93
100
  logSuccess = logFirstAttempt = logRetries = logFailures = false
94
101
  }
95
102
 
96
- const fname = ['pRetry', fn.name].filter(Boolean).join('.')
103
+ const fname = ['pRetry', name].filter(Boolean).join('.')
97
104
 
98
105
  return async function (this: any, ...args: any[]) {
99
106
  let delay = initialDelay
@@ -118,9 +125,10 @@ export function pRetry<T extends Function>(fn: T, opt: PRetryOptions = {}): T {
118
125
  } catch (err) {
119
126
  if (logFailures) {
120
127
  logger.warn(
121
- `${fname} attempt #${attempt} error in ${_since(started)}:\n${_stringifyAny(err, {
128
+ `${fname} attempt #${attempt} error in ${_since(started)}:`,
129
+ _stringifyAny(err, {
122
130
  includeErrorData: true,
123
- })}`,
131
+ }),
124
132
  )
125
133
  }
126
134