@ls-stack/utils 3.20.0 → 3.21.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.
@@ -32,7 +32,7 @@
32
32
  - [objUtils](objUtils.md)
33
33
  - [parallelAsyncCalls](parallelAsyncCalls/README.md)
34
34
  - [promiseUtils](promiseUtils/README.md)
35
- - [retryOnError](retryOnError.md)
35
+ - [retryOnError](retryOnError/README.md)
36
36
  - [runShellCmd](runShellCmd/README.md)
37
37
  - [safeJson](safeJson.md)
38
38
  - [saferTyping](saferTyping.md)
package/docs/modules.md CHANGED
@@ -32,7 +32,7 @@
32
32
  - [objUtils](objUtils.md)
33
33
  - [parallelAsyncCalls](parallelAsyncCalls/README.md)
34
34
  - [promiseUtils](promiseUtils/README.md)
35
- - [retryOnError](retryOnError.md)
35
+ - [retryOnError](retryOnError/README.md)
36
36
  - [runShellCmd](runShellCmd/README.md)
37
37
  - [safeJson](safeJson.md)
38
38
  - [saferTyping](saferTyping.md)
@@ -0,0 +1,71 @@
1
+ [**@ls-stack/utils**](../README.md)
2
+
3
+ ***
4
+
5
+ [@ls-stack/utils](../modules.md) / [retryOnError](README.md) / \<internal\>
6
+
7
+ # \<internal\>
8
+
9
+ ## Type Aliases
10
+
11
+ ### RetryOptions
12
+
13
+ ```ts
14
+ type RetryOptions = object;
15
+ ```
16
+
17
+ Defined in: [packages/utils/src/retryOnError.ts:6](https://github.com/lucasols/utils/blob/main/packages/utils/src/retryOnError.ts#L6)
18
+
19
+ Configuration options for retryOnError function.
20
+
21
+ #### Properties
22
+
23
+ ##### debugId?
24
+
25
+ ```ts
26
+ optional debugId: string;
27
+ ```
28
+
29
+ Defined in: [packages/utils/src/retryOnError.ts:15](https://github.com/lucasols/utils/blob/main/packages/utils/src/retryOnError.ts#L15)
30
+
31
+ Optional ID for debug logging
32
+
33
+ ##### delayBetweenRetriesMs?
34
+
35
+ ```ts
36
+ optional delayBetweenRetriesMs: number | (retry) => number;
37
+ ```
38
+
39
+ Defined in: [packages/utils/src/retryOnError.ts:8](https://github.com/lucasols/utils/blob/main/packages/utils/src/retryOnError.ts#L8)
40
+
41
+ Delay between retries in milliseconds or function returning delay
42
+
43
+ ##### retryCondition()?
44
+
45
+ ```ts
46
+ optional retryCondition: (error, lastAttempt) => boolean;
47
+ ```
48
+
49
+ Defined in: [packages/utils/src/retryOnError.ts:10](https://github.com/lucasols/utils/blob/main/packages/utils/src/retryOnError.ts#L10)
50
+
51
+ Function to determine if retry should happen, receives error and duration of last attempt
52
+
53
+ ###### Parameters
54
+
55
+ ###### error
56
+
57
+ `unknown`
58
+
59
+ ###### lastAttempt
60
+
61
+ ###### duration
62
+
63
+ `number`
64
+
65
+ ###### retry
66
+
67
+ `number`
68
+
69
+ ###### Returns
70
+
71
+ `boolean`
@@ -0,0 +1,85 @@
1
+ [**@ls-stack/utils**](../README.md)
2
+
3
+ ***
4
+
5
+ [@ls-stack/utils](../modules.md) / retryOnError
6
+
7
+ # retryOnError
8
+
9
+ ## Modules
10
+
11
+ - [\<internal\>](-internal-.md)
12
+
13
+ ## Functions
14
+
15
+ ### retryOnError()
16
+
17
+ ```ts
18
+ function retryOnError<T>(
19
+ fn,
20
+ maxRetries,
21
+ options,
22
+ retry,
23
+ originalMaxRetries): Promise<T>;
24
+ ```
25
+
26
+ Defined in: [packages/utils/src/retryOnError.ts:38](https://github.com/lucasols/utils/blob/main/packages/utils/src/retryOnError.ts#L38)
27
+
28
+ Retries a function on error with configurable retry logic.
29
+
30
+ #### Type Parameters
31
+
32
+ ##### T
33
+
34
+ `T`
35
+
36
+ #### Parameters
37
+
38
+ ##### fn
39
+
40
+ (`ctx`) => `Promise`\<`T`\>
41
+
42
+ Function to retry that receives context with retry count
43
+
44
+ ##### maxRetries
45
+
46
+ `number`
47
+
48
+ Maximum number of retries
49
+
50
+ ##### options
51
+
52
+ [`RetryOptions`](-internal-.md#retryoptions) = `{}`
53
+
54
+ Configuration options
55
+
56
+ ##### retry
57
+
58
+ `number` = `0`
59
+
60
+ Internal parameter tracking current retry count
61
+
62
+ ##### originalMaxRetries
63
+
64
+ `number` = `maxRetries`
65
+
66
+ Internal parameter tracking original max retries for logging
67
+
68
+ #### Returns
69
+
70
+ `Promise`\<`T`\>
71
+
72
+ Promise resolving to the function result or rejecting with the final error
73
+
74
+ #### Example
75
+
76
+ ```ts
77
+ await retryOnError(
78
+ async (ctx) => {
79
+ console.log(`Attempt ${ctx.retry + 1}`);
80
+ return await fetchData();
81
+ },
82
+ 3,
83
+ { delayBetweenRetriesMs: 1000 }
84
+ );
85
+ ```
@@ -30,33 +30,23 @@ function sleep(ms) {
30
30
  }
31
31
 
32
32
  // src/retryOnError.ts
33
- async function retryOnError(fn, maxRetries, options = {}, retry = 0) {
34
- const {
35
- delayBetweenRetriesMs,
36
- retryCondition,
37
- maxErrorDurationMs = 400
38
- } = options;
33
+ async function retryOnError(fn, maxRetries, options = {}, retry = 0, originalMaxRetries = maxRetries) {
34
+ const { delayBetweenRetriesMs, retryCondition } = options;
39
35
  if (options.debugId) {
40
36
  if (retry > 0) {
41
37
  console.info(
42
- `Retrying ${options.debugId} (retry ${retry}/${maxRetries}) after error`
38
+ `Retrying ${options.debugId} (retry ${retry}/${originalMaxRetries}) after error`
43
39
  );
44
40
  }
45
41
  }
46
42
  const startTime = Date.now();
47
43
  try {
48
- return await fn();
44
+ return await fn({ retry });
49
45
  } catch (error) {
50
46
  if (maxRetries > 0) {
51
47
  const errorDuration = Date.now() - startTime;
52
- const shouldRetry = retryCondition ? retryCondition(error) : true;
53
- let maxErrorDurationMsToUse = maxErrorDurationMs;
54
- if (typeof shouldRetry === "boolean") {
55
- if (!shouldRetry) throw error;
56
- } else {
57
- maxErrorDurationMsToUse = shouldRetry.maxErrorDurationMs;
58
- }
59
- if (errorDuration > maxErrorDurationMsToUse) {
48
+ const shouldRetry = retryCondition ? retryCondition(error, { duration: errorDuration, retry }) : true;
49
+ if (!shouldRetry) {
60
50
  throw error;
61
51
  }
62
52
  if (delayBetweenRetriesMs) {
@@ -64,7 +54,7 @@ async function retryOnError(fn, maxRetries, options = {}, retry = 0) {
64
54
  typeof delayBetweenRetriesMs === "function" ? delayBetweenRetriesMs(retry) : delayBetweenRetriesMs
65
55
  );
66
56
  }
67
- return retryOnError(fn, maxRetries - 1, options, retry + 1);
57
+ return retryOnError(fn, maxRetries - 1, options, retry + 1, originalMaxRetries);
68
58
  } else {
69
59
  throw error;
70
60
  }
@@ -1,10 +1,39 @@
1
- declare function retryOnError<T>(fn: () => Promise<T>, maxRetries: number, options?: {
1
+ /**
2
+ * Configuration options for retryOnError function.
3
+ */
4
+ type RetryOptions = {
5
+ /** Delay between retries in milliseconds or function returning delay */
2
6
  delayBetweenRetriesMs?: number | ((retry: number) => number);
3
- retryCondition?: (error: unknown) => boolean | {
4
- maxErrorDurationMs: number;
5
- };
6
- maxErrorDurationMs?: number;
7
+ /** Function to determine if retry should happen, receives error and duration of last attempt */
8
+ retryCondition?: (error: unknown, lastAttempt: {
9
+ duration: number;
10
+ retry: number;
11
+ }) => boolean;
12
+ /** Optional ID for debug logging */
7
13
  debugId?: string;
8
- }, retry?: number): Promise<T>;
14
+ };
15
+ /**
16
+ * Retries a function on error with configurable retry logic.
17
+ *
18
+ * @param fn - Function to retry that receives context with retry count
19
+ * @param maxRetries - Maximum number of retries
20
+ * @param options - Configuration options
21
+ * @param retry - Internal parameter tracking current retry count
22
+ * @param originalMaxRetries - Internal parameter tracking original max retries for logging
23
+ * @returns Promise resolving to the function result or rejecting with the final error
24
+ *
25
+ * @example
26
+ * await retryOnError(
27
+ * async (ctx) => {
28
+ * console.log(`Attempt ${ctx.retry + 1}`);
29
+ * return await fetchData();
30
+ * },
31
+ * 3,
32
+ * { delayBetweenRetriesMs: 1000 }
33
+ * );
34
+ */
35
+ declare function retryOnError<T>(fn: (ctx: {
36
+ retry: number;
37
+ }) => Promise<T>, maxRetries: number, options?: RetryOptions, retry?: number, originalMaxRetries?: number): Promise<T>;
9
38
 
10
39
  export { retryOnError };
@@ -1,10 +1,39 @@
1
- declare function retryOnError<T>(fn: () => Promise<T>, maxRetries: number, options?: {
1
+ /**
2
+ * Configuration options for retryOnError function.
3
+ */
4
+ type RetryOptions = {
5
+ /** Delay between retries in milliseconds or function returning delay */
2
6
  delayBetweenRetriesMs?: number | ((retry: number) => number);
3
- retryCondition?: (error: unknown) => boolean | {
4
- maxErrorDurationMs: number;
5
- };
6
- maxErrorDurationMs?: number;
7
+ /** Function to determine if retry should happen, receives error and duration of last attempt */
8
+ retryCondition?: (error: unknown, lastAttempt: {
9
+ duration: number;
10
+ retry: number;
11
+ }) => boolean;
12
+ /** Optional ID for debug logging */
7
13
  debugId?: string;
8
- }, retry?: number): Promise<T>;
14
+ };
15
+ /**
16
+ * Retries a function on error with configurable retry logic.
17
+ *
18
+ * @param fn - Function to retry that receives context with retry count
19
+ * @param maxRetries - Maximum number of retries
20
+ * @param options - Configuration options
21
+ * @param retry - Internal parameter tracking current retry count
22
+ * @param originalMaxRetries - Internal parameter tracking original max retries for logging
23
+ * @returns Promise resolving to the function result or rejecting with the final error
24
+ *
25
+ * @example
26
+ * await retryOnError(
27
+ * async (ctx) => {
28
+ * console.log(`Attempt ${ctx.retry + 1}`);
29
+ * return await fetchData();
30
+ * },
31
+ * 3,
32
+ * { delayBetweenRetriesMs: 1000 }
33
+ * );
34
+ */
35
+ declare function retryOnError<T>(fn: (ctx: {
36
+ retry: number;
37
+ }) => Promise<T>, maxRetries: number, options?: RetryOptions, retry?: number, originalMaxRetries?: number): Promise<T>;
9
38
 
10
39
  export { retryOnError };
@@ -3,33 +3,23 @@ import {
3
3
  } from "./chunk-5DZT3Z5Z.js";
4
4
 
5
5
  // src/retryOnError.ts
6
- async function retryOnError(fn, maxRetries, options = {}, retry = 0) {
7
- const {
8
- delayBetweenRetriesMs,
9
- retryCondition,
10
- maxErrorDurationMs = 400
11
- } = options;
6
+ async function retryOnError(fn, maxRetries, options = {}, retry = 0, originalMaxRetries = maxRetries) {
7
+ const { delayBetweenRetriesMs, retryCondition } = options;
12
8
  if (options.debugId) {
13
9
  if (retry > 0) {
14
10
  console.info(
15
- `Retrying ${options.debugId} (retry ${retry}/${maxRetries}) after error`
11
+ `Retrying ${options.debugId} (retry ${retry}/${originalMaxRetries}) after error`
16
12
  );
17
13
  }
18
14
  }
19
15
  const startTime = Date.now();
20
16
  try {
21
- return await fn();
17
+ return await fn({ retry });
22
18
  } catch (error) {
23
19
  if (maxRetries > 0) {
24
20
  const errorDuration = Date.now() - startTime;
25
- const shouldRetry = retryCondition ? retryCondition(error) : true;
26
- let maxErrorDurationMsToUse = maxErrorDurationMs;
27
- if (typeof shouldRetry === "boolean") {
28
- if (!shouldRetry) throw error;
29
- } else {
30
- maxErrorDurationMsToUse = shouldRetry.maxErrorDurationMs;
31
- }
32
- if (errorDuration > maxErrorDurationMsToUse) {
21
+ const shouldRetry = retryCondition ? retryCondition(error, { duration: errorDuration, retry }) : true;
22
+ if (!shouldRetry) {
33
23
  throw error;
34
24
  }
35
25
  if (delayBetweenRetriesMs) {
@@ -37,7 +27,7 @@ async function retryOnError(fn, maxRetries, options = {}, retry = 0) {
37
27
  typeof delayBetweenRetriesMs === "function" ? delayBetweenRetriesMs(retry) : delayBetweenRetriesMs
38
28
  );
39
29
  }
40
- return retryOnError(fn, maxRetries - 1, options, retry + 1);
30
+ return retryOnError(fn, maxRetries - 1, options, retry + 1, originalMaxRetries);
41
31
  } else {
42
32
  throw error;
43
33
  }
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.20.0",
4
+ "version": "3.21.0",
5
5
  "license": "MIT",
6
6
  "files": [
7
7
  "lib",
@@ -1,67 +0,0 @@
1
- [**@ls-stack/utils**](README.md)
2
-
3
- ***
4
-
5
- [@ls-stack/utils](modules.md) / retryOnError
6
-
7
- # retryOnError
8
-
9
- ## Functions
10
-
11
- ### retryOnError()
12
-
13
- ```ts
14
- function retryOnError<T>(
15
- fn,
16
- maxRetries,
17
- options,
18
- retry): Promise<T>;
19
- ```
20
-
21
- Defined in: [packages/utils/src/retryOnError.ts:3](https://github.com/lucasols/utils/blob/main/packages/utils/src/retryOnError.ts#L3)
22
-
23
- #### Type Parameters
24
-
25
- ##### T
26
-
27
- `T`
28
-
29
- #### Parameters
30
-
31
- ##### fn
32
-
33
- () => `Promise`\<`T`\>
34
-
35
- ##### maxRetries
36
-
37
- `number`
38
-
39
- ##### options
40
-
41
- ###### debugId?
42
-
43
- `string`
44
-
45
- ###### delayBetweenRetriesMs?
46
-
47
- `number` \| (`retry`) => `number`
48
-
49
- ###### maxErrorDurationMs?
50
-
51
- `number`
52
-
53
- ###### retryCondition?
54
-
55
- (`error`) =>
56
- \| `boolean`
57
- \| \{
58
- `maxErrorDurationMs`: `number`;
59
- \}
60
-
61
- ##### retry
62
-
63
- `number` = `0`
64
-
65
- #### Returns
66
-
67
- `Promise`\<`T`\>