@ls-stack/utils 3.29.0 → 3.30.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.
@@ -50,6 +50,36 @@ Defined in: [packages/utils/src/retryOnError.ts:18](https://github.com/lucasols/
50
50
 
51
51
  Disable retries
52
52
 
53
+ ##### onRetry()?
54
+
55
+ ```ts
56
+ optional onRetry: (error, lastAttempt) => void;
57
+ ```
58
+
59
+ Defined in: [packages/utils/src/retryOnError.ts:20](https://github.com/lucasols/utils/blob/main/packages/utils/src/retryOnError.ts#L20)
60
+
61
+ Function to call when retry happens
62
+
63
+ ###### Parameters
64
+
65
+ ###### error
66
+
67
+ `Error`
68
+
69
+ ###### lastAttempt
70
+
71
+ ###### duration
72
+
73
+ `number`
74
+
75
+ ###### retry
76
+
77
+ `number`
78
+
79
+ ###### Returns
80
+
81
+ `void`
82
+
53
83
  ##### retryCondition()?
54
84
 
55
85
  ```ts
@@ -23,7 +23,7 @@ function retryOnError<T>(
23
23
  originalMaxRetries): Promise<T>;
24
24
  ```
25
25
 
26
- Defined in: [packages/utils/src/retryOnError.ts:41](https://github.com/lucasols/utils/blob/main/packages/utils/src/retryOnError.ts#L41)
26
+ Defined in: [packages/utils/src/retryOnError.ts:46](https://github.com/lucasols/utils/blob/main/packages/utils/src/retryOnError.ts#L46)
27
27
 
28
28
  Retries a function on error with configurable retry logic.
29
29
 
@@ -97,7 +97,7 @@ function retryResultOnError<T, E>(
97
97
  __originalMaxRetries): Promise<Result<T, E>>;
98
98
  ```
99
99
 
100
- Defined in: [packages/utils/src/retryOnError.ts:116](https://github.com/lucasols/utils/blob/main/packages/utils/src/retryOnError.ts#L116)
100
+ Defined in: [packages/utils/src/retryOnError.ts:132](https://github.com/lucasols/utils/blob/main/packages/utils/src/retryOnError.ts#L132)
101
101
 
102
102
  Retries a result function on error with configurable retry logic.
103
103
 
@@ -141,6 +141,10 @@ Configuration options
141
141
 
142
142
  `boolean`
143
143
 
144
+ ###### onRetry?
145
+
146
+ (`error`, `lastAttempt`) => `void`
147
+
144
148
  ###### retryCondition?
145
149
 
146
150
  (`error`, `lastAttempt`) => `boolean`
@@ -33,7 +33,7 @@ function sleep(ms) {
33
33
 
34
34
  // src/retryOnError.ts
35
35
  async function retryOnError(fn, maxRetries, options = {}, retry = 0, originalMaxRetries = maxRetries) {
36
- const { delayBetweenRetriesMs, retryCondition, disableRetries } = options;
36
+ const { delayBetweenRetriesMs, retryCondition, disableRetries, onRetry } = options;
37
37
  if (options.debugId) {
38
38
  if (retry > 0) {
39
39
  console.info(
@@ -47,12 +47,19 @@ async function retryOnError(fn, maxRetries, options = {}, retry = 0, originalMax
47
47
  } catch (error) {
48
48
  if (maxRetries > 0 && !disableRetries) {
49
49
  const errorDuration = Date.now() - startTime;
50
- const shouldRetry = retryCondition ? retryCondition((0, import_t_result.unknownToError)(error), {
50
+ const normalizedError = (0, import_t_result.unknownToError)(error);
51
+ const shouldRetry = retryCondition ? retryCondition(normalizedError, {
51
52
  duration: errorDuration,
52
53
  retry
53
54
  }) : true;
54
55
  if (!shouldRetry) {
55
- throw error;
56
+ throw normalizedError;
57
+ }
58
+ if (onRetry) {
59
+ onRetry(normalizedError, {
60
+ duration: errorDuration,
61
+ retry
62
+ });
56
63
  }
57
64
  if (delayBetweenRetriesMs) {
58
65
  await sleep(
@@ -72,7 +79,7 @@ async function retryOnError(fn, maxRetries, options = {}, retry = 0, originalMax
72
79
  }
73
80
  }
74
81
  async function retryResultOnError(fn, maxRetries, options = {}, __retry = 0, __originalMaxRetries = maxRetries) {
75
- const { delayBetweenRetriesMs, retryCondition } = options;
82
+ const { delayBetweenRetriesMs, retryCondition, onRetry } = options;
76
83
  if (options.debugId) {
77
84
  if (__retry > 0) {
78
85
  console.info(
@@ -94,6 +101,12 @@ async function retryResultOnError(fn, maxRetries, options = {}, __retry = 0, __o
94
101
  if (!shouldRetry) {
95
102
  return result;
96
103
  }
104
+ if (onRetry) {
105
+ onRetry(result.error, {
106
+ duration: errorDuration,
107
+ retry: __retry
108
+ });
109
+ }
97
110
  if (delayBetweenRetriesMs) {
98
111
  await sleep(
99
112
  typeof delayBetweenRetriesMs === "function" ? delayBetweenRetriesMs(__retry) : delayBetweenRetriesMs
@@ -15,6 +15,11 @@ type RetryOptions = {
15
15
  debugId?: string;
16
16
  /** Disable retries */
17
17
  disableRetries?: boolean;
18
+ /** Function to call when retry happens */
19
+ onRetry?: (error: Error, lastAttempt: {
20
+ duration: number;
21
+ retry: number;
22
+ }) => void;
18
23
  };
19
24
  /**
20
25
  * Retries a function on error with configurable retry logic.
@@ -50,6 +55,7 @@ declare function retryOnError<T>(fn: (ctx: {
50
55
  * @param options.retryCondition
51
56
  * @param options.debugId
52
57
  * @param options.disableRetries
58
+ * @param options.onRetry
53
59
  * @param __retry - internal use only
54
60
  * @param __originalMaxRetries - internal use only
55
61
  * @returns Promise resolving to the function result or rejecting with the final error
@@ -65,6 +71,10 @@ declare function retryResultOnError<T, E extends ResultValidErrors>(fn: (ctx: {
65
71
  }) => boolean;
66
72
  debugId?: string;
67
73
  disableRetries?: boolean;
74
+ onRetry?: (error: E, lastAttempt: {
75
+ duration: number;
76
+ retry: number;
77
+ }) => void;
68
78
  }, __retry?: number, __originalMaxRetries?: number): Promise<Result<T, E>>;
69
79
 
70
80
  export { retryOnError, retryResultOnError };
@@ -15,6 +15,11 @@ type RetryOptions = {
15
15
  debugId?: string;
16
16
  /** Disable retries */
17
17
  disableRetries?: boolean;
18
+ /** Function to call when retry happens */
19
+ onRetry?: (error: Error, lastAttempt: {
20
+ duration: number;
21
+ retry: number;
22
+ }) => void;
18
23
  };
19
24
  /**
20
25
  * Retries a function on error with configurable retry logic.
@@ -50,6 +55,7 @@ declare function retryOnError<T>(fn: (ctx: {
50
55
  * @param options.retryCondition
51
56
  * @param options.debugId
52
57
  * @param options.disableRetries
58
+ * @param options.onRetry
53
59
  * @param __retry - internal use only
54
60
  * @param __originalMaxRetries - internal use only
55
61
  * @returns Promise resolving to the function result or rejecting with the final error
@@ -65,6 +71,10 @@ declare function retryResultOnError<T, E extends ResultValidErrors>(fn: (ctx: {
65
71
  }) => boolean;
66
72
  debugId?: string;
67
73
  disableRetries?: boolean;
74
+ onRetry?: (error: E, lastAttempt: {
75
+ duration: number;
76
+ retry: number;
77
+ }) => void;
68
78
  }, __retry?: number, __originalMaxRetries?: number): Promise<Result<T, E>>;
69
79
 
70
80
  export { retryOnError, retryResultOnError };
@@ -5,7 +5,7 @@ import {
5
5
  // src/retryOnError.ts
6
6
  import { unknownToError } from "t-result";
7
7
  async function retryOnError(fn, maxRetries, options = {}, retry = 0, originalMaxRetries = maxRetries) {
8
- const { delayBetweenRetriesMs, retryCondition, disableRetries } = options;
8
+ const { delayBetweenRetriesMs, retryCondition, disableRetries, onRetry } = options;
9
9
  if (options.debugId) {
10
10
  if (retry > 0) {
11
11
  console.info(
@@ -19,12 +19,19 @@ async function retryOnError(fn, maxRetries, options = {}, retry = 0, originalMax
19
19
  } catch (error) {
20
20
  if (maxRetries > 0 && !disableRetries) {
21
21
  const errorDuration = Date.now() - startTime;
22
- const shouldRetry = retryCondition ? retryCondition(unknownToError(error), {
22
+ const normalizedError = unknownToError(error);
23
+ const shouldRetry = retryCondition ? retryCondition(normalizedError, {
23
24
  duration: errorDuration,
24
25
  retry
25
26
  }) : true;
26
27
  if (!shouldRetry) {
27
- throw error;
28
+ throw normalizedError;
29
+ }
30
+ if (onRetry) {
31
+ onRetry(normalizedError, {
32
+ duration: errorDuration,
33
+ retry
34
+ });
28
35
  }
29
36
  if (delayBetweenRetriesMs) {
30
37
  await sleep(
@@ -44,7 +51,7 @@ async function retryOnError(fn, maxRetries, options = {}, retry = 0, originalMax
44
51
  }
45
52
  }
46
53
  async function retryResultOnError(fn, maxRetries, options = {}, __retry = 0, __originalMaxRetries = maxRetries) {
47
- const { delayBetweenRetriesMs, retryCondition } = options;
54
+ const { delayBetweenRetriesMs, retryCondition, onRetry } = options;
48
55
  if (options.debugId) {
49
56
  if (__retry > 0) {
50
57
  console.info(
@@ -66,6 +73,12 @@ async function retryResultOnError(fn, maxRetries, options = {}, __retry = 0, __o
66
73
  if (!shouldRetry) {
67
74
  return result;
68
75
  }
76
+ if (onRetry) {
77
+ onRetry(result.error, {
78
+ duration: errorDuration,
79
+ retry: __retry
80
+ });
81
+ }
69
82
  if (delayBetweenRetriesMs) {
70
83
  await sleep(
71
84
  typeof delayBetweenRetriesMs === "function" ? delayBetweenRetriesMs(__retry) : delayBetweenRetriesMs
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@ls-stack/utils",
3
3
  "description": "Universal TypeScript utilities for browser and Node.js",
4
- "version": "3.29.0",
4
+ "version": "3.30.0",
5
5
  "license": "MIT",
6
6
  "files": [
7
7
  "lib",