@ls-stack/utils 3.19.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.
@@ -17,7 +17,7 @@
17
17
  - [conversions](conversions.md)
18
18
  - [createThrottleController](createThrottleController/README.md)
19
19
  - [debounce](debounce.md)
20
- - [dedent](dedent.md)
20
+ - [dedent](dedent/README.md)
21
21
  - [deepEqual](deepEqual.md)
22
22
  - [enhancedMap](enhancedMap.md)
23
23
  - [exhaustiveMatch](exhaustiveMatch/README.md)
@@ -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,17 @@
1
+ [**@ls-stack/utils**](../README.md)
2
+
3
+ ***
4
+
5
+ [@ls-stack/utils](../modules.md) / [dedent](README.md) / \<internal\>
6
+
7
+ # \<internal\>
8
+
9
+ ## Type Aliases
10
+
11
+ ### InterpolationValue
12
+
13
+ ```ts
14
+ type InterpolationValue = string | number | boolean | null | undefined;
15
+ ```
16
+
17
+ Defined in: [packages/utils/src/dedent.ts:30](https://github.com/lucasols/utils/blob/main/packages/utils/src/dedent.ts#L30)
@@ -0,0 +1,204 @@
1
+ [**@ls-stack/utils**](../README.md)
2
+
3
+ ***
4
+
5
+ [@ls-stack/utils](../modules.md) / dedent
6
+
7
+ # dedent
8
+
9
+ ## Modules
10
+
11
+ - [\<internal\>](-internal-.md)
12
+
13
+ ## Interfaces
14
+
15
+ ### Dedent()
16
+
17
+ Defined in: [packages/utils/src/dedent.ts:35](https://github.com/lucasols/utils/blob/main/packages/utils/src/dedent.ts#L35)
18
+
19
+ Dedent function interface that can be used both as a template tag and a regular function
20
+
21
+ #### Call Signature
22
+
23
+ ```ts
24
+ Dedent(literals): string;
25
+ ```
26
+
27
+ Defined in: [packages/utils/src/dedent.ts:39](https://github.com/lucasols/utils/blob/main/packages/utils/src/dedent.ts#L39)
28
+
29
+ Dedent function interface that can be used both as a template tag and a regular function
30
+
31
+ ##### Parameters
32
+
33
+ ###### literals
34
+
35
+ `string`
36
+
37
+ ##### Returns
38
+
39
+ `string`
40
+
41
+ #### Call Signature
42
+
43
+ ```ts
44
+ Dedent(strings, ...values): string;
45
+ ```
46
+
47
+ Defined in: [packages/utils/src/dedent.ts:43](https://github.com/lucasols/utils/blob/main/packages/utils/src/dedent.ts#L43)
48
+
49
+ Dedent function interface that can be used both as a template tag and a regular function
50
+
51
+ ##### Parameters
52
+
53
+ ###### strings
54
+
55
+ `TemplateStringsArray`
56
+
57
+ ###### values
58
+
59
+ ...[`InterpolationValue`](-internal-.md#interpolationvalue)[]
60
+
61
+ ##### Returns
62
+
63
+ `string`
64
+
65
+ #### Properties
66
+
67
+ ##### withOptions
68
+
69
+ ```ts
70
+ withOptions: CreateDedent;
71
+ ```
72
+
73
+ Defined in: [packages/utils/src/dedent.ts:47](https://github.com/lucasols/utils/blob/main/packages/utils/src/dedent.ts#L47)
74
+
75
+ Create a new dedent function with custom options
76
+
77
+ ***
78
+
79
+ ### DedentOptions
80
+
81
+ Defined in: [packages/utils/src/dedent.ts:4](https://github.com/lucasols/utils/blob/main/packages/utils/src/dedent.ts#L4)
82
+
83
+ Configuration options for the dedent function behavior
84
+
85
+ #### Properties
86
+
87
+ ##### escapeSpecialCharacters?
88
+
89
+ ```ts
90
+ optional escapeSpecialCharacters: boolean;
91
+ ```
92
+
93
+ Defined in: [packages/utils/src/dedent.ts:10](https://github.com/lucasols/utils/blob/main/packages/utils/src/dedent.ts#L10)
94
+
95
+ Whether to process escape sequences like \n, `, \$, and {
96
+ When true, allows using escaped characters in template literals
97
+
98
+ ###### Default
99
+
100
+ ```ts
101
+ true for template literals, false for plain strings
102
+ ```
103
+
104
+ ##### identInterpolations?
105
+
106
+ ```ts
107
+ optional identInterpolations: boolean;
108
+ ```
109
+
110
+ Defined in: [packages/utils/src/dedent.ts:21](https://github.com/lucasols/utils/blob/main/packages/utils/src/dedent.ts#L21)
111
+
112
+ Whether to preserve relative indentation of interpolated multi-line values
113
+ When true, multi-line interpolations are re-indented to match the surrounding context
114
+
115
+ ###### Default
116
+
117
+ ```ts
118
+ true
119
+ ```
120
+
121
+ ##### showNullishOrFalseValues?
122
+
123
+ ```ts
124
+ optional showNullishOrFalseValues: boolean;
125
+ ```
126
+
127
+ Defined in: [packages/utils/src/dedent.ts:27](https://github.com/lucasols/utils/blob/main/packages/utils/src/dedent.ts#L27)
128
+
129
+ Whether to display nullish or false values (false, null, undefined) in interpolations
130
+ When false, nullish or false values are skipped entirely
131
+
132
+ ###### Default
133
+
134
+ ```ts
135
+ false
136
+ ```
137
+
138
+ ##### trimWhitespace?
139
+
140
+ ```ts
141
+ optional trimWhitespace: boolean;
142
+ ```
143
+
144
+ Defined in: [packages/utils/src/dedent.ts:15](https://github.com/lucasols/utils/blob/main/packages/utils/src/dedent.ts#L15)
145
+
146
+ Whether to trim leading and trailing whitespace from the final result
147
+
148
+ ###### Default
149
+
150
+ ```ts
151
+ true
152
+ ```
153
+
154
+ ## Type Aliases
155
+
156
+ ### CreateDedent()
157
+
158
+ ```ts
159
+ type CreateDedent = (options) => Dedent;
160
+ ```
161
+
162
+ Defined in: [packages/utils/src/dedent.ts:53](https://github.com/lucasols/utils/blob/main/packages/utils/src/dedent.ts#L53)
163
+
164
+ Factory function type for creating dedent functions with custom options
165
+
166
+ #### Parameters
167
+
168
+ ##### options
169
+
170
+ [`DedentOptions`](#dedentoptions)
171
+
172
+ #### Returns
173
+
174
+ [`Dedent`](#dedent)
175
+
176
+ ## Variables
177
+
178
+ ### dedent
179
+
180
+ ```ts
181
+ const dedent: Dedent;
182
+ ```
183
+
184
+ Defined in: [packages/utils/src/dedent.ts:75](https://github.com/lucasols/utils/blob/main/packages/utils/src/dedent.ts#L75)
185
+
186
+ Remove common leading indentation from multi-line strings while preserving relative indentation.
187
+ Can be used as a tagged template literal or called with a plain string.
188
+
189
+ By default, it will dedent interpolated multi-line strings to match the surrounding context.
190
+ And it will not show falsy values.
191
+
192
+ #### Example
193
+
194
+ ```typescript
195
+ const text = dedent`
196
+ function hello() {
197
+ console.log('world');
198
+ }
199
+ `;
200
+ // Result:
201
+ "function hello() {
202
+ console.log('world');
203
+ }"
204
+ ```
package/docs/modules.md CHANGED
@@ -17,7 +17,7 @@
17
17
  - [conversions](conversions.md)
18
18
  - [createThrottleController](createThrottleController/README.md)
19
19
  - [debounce](debounce.md)
20
- - [dedent](dedent.md)
20
+ - [dedent](dedent/README.md)
21
21
  - [deepEqual](deepEqual.md)
22
22
  - [enhancedMap](enhancedMap.md)
23
23
  - [exhaustiveMatch](exhaustiveMatch/README.md)
@@ -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
+ ```
package/lib/dedent.cjs CHANGED
@@ -23,7 +23,9 @@ __export(dedent_exports, {
23
23
  dedent: () => dedent
24
24
  });
25
25
  module.exports = __toCommonJS(dedent_exports);
26
- var dedent = createDedent({});
26
+ var dedent = createDedent({
27
+ identInterpolations: true
28
+ });
27
29
  function createDedent(options) {
28
30
  d.withOptions = (newOptions) => createDedent({ ...options, ...newOptions });
29
31
  return d;
@@ -31,7 +33,9 @@ function createDedent(options) {
31
33
  const raw = typeof strings === "string" ? [strings] : strings.raw;
32
34
  const {
33
35
  escapeSpecialCharacters = Array.isArray(strings),
34
- trimWhitespace = true
36
+ trimWhitespace = true,
37
+ identInterpolations = true,
38
+ showNullishOrFalseValues = false
35
39
  } = options;
36
40
  let result = "";
37
41
  for (let i = 0; i < raw.length; i++) {
@@ -41,7 +45,24 @@ function createDedent(options) {
41
45
  }
42
46
  result += next;
43
47
  if (i < values.length) {
44
- result += values[i];
48
+ let val = values[i];
49
+ if (!showNullishOrFalseValues && (val === false || val === null || val === void 0)) {
50
+ continue;
51
+ }
52
+ val = String(val);
53
+ if (identInterpolations && val.includes("\n")) {
54
+ let withIdent = val;
55
+ const currentIndent = getCurrentIndent(result);
56
+ if (currentIndent && withIdent) {
57
+ const lines2 = withIdent.split("\n");
58
+ withIdent = lines2.map((line, index) => {
59
+ return index === 0 || line === "" ? line : currentIndent + line;
60
+ }).join("\n");
61
+ }
62
+ result += withIdent;
63
+ } else {
64
+ result += val;
65
+ }
45
66
  }
46
67
  }
47
68
  const lines = result.split("\n");
@@ -70,6 +91,13 @@ function createDedent(options) {
70
91
  return result;
71
92
  }
72
93
  }
94
+ function getCurrentIndent(str) {
95
+ const lines = str.split("\n");
96
+ const lastLine = lines[lines.length - 1];
97
+ if (!lastLine) return "";
98
+ const match = lastLine.match(/^(\s*)/);
99
+ return match ? match[1] : "";
100
+ }
73
101
  // Annotate the CommonJS export names for ESM import in node:
74
102
  0 && (module.exports = {
75
103
  dedent
package/lib/dedent.d.cts CHANGED
@@ -1,13 +1,73 @@
1
+ /**
2
+ * Configuration options for the dedent function behavior
3
+ */
1
4
  interface DedentOptions {
5
+ /**
6
+ * Whether to process escape sequences like \n, \`, \$, and \{
7
+ * When true, allows using escaped characters in template literals
8
+ * @default true for template literals, false for plain strings
9
+ */
2
10
  escapeSpecialCharacters?: boolean;
11
+ /**
12
+ * Whether to trim leading and trailing whitespace from the final result
13
+ * @default true
14
+ */
3
15
  trimWhitespace?: boolean;
16
+ /**
17
+ * Whether to preserve relative indentation of interpolated multi-line values
18
+ * When true, multi-line interpolations are re-indented to match the surrounding context
19
+ * @default true
20
+ */
21
+ identInterpolations?: boolean;
22
+ /**
23
+ * Whether to display nullish or false values (false, null, undefined) in interpolations
24
+ * When false, nullish or false values are skipped entirely
25
+ * @default false
26
+ */
27
+ showNullishOrFalseValues?: boolean;
4
28
  }
29
+ type InterpolationValue = string | number | boolean | null | undefined;
30
+ /**
31
+ * Dedent function interface that can be used both as a template tag and a regular function
32
+ */
5
33
  interface Dedent {
34
+ /**
35
+ * Process a plain string to remove common indentation
36
+ */
6
37
  (literals: string): string;
7
- (strings: TemplateStringsArray, ...values: unknown[]): string;
38
+ /**
39
+ * Process a template literal to remove common indentation while handling interpolations
40
+ */
41
+ (strings: TemplateStringsArray, ...values: InterpolationValue[]): string;
42
+ /**
43
+ * Create a new dedent function with custom options
44
+ */
8
45
  withOptions: CreateDedent;
9
46
  }
47
+ /**
48
+ * Factory function type for creating dedent functions with custom options
49
+ */
10
50
  type CreateDedent = (options: DedentOptions) => Dedent;
51
+ /**
52
+ * Remove common leading indentation from multi-line strings while preserving relative indentation.
53
+ * Can be used as a tagged template literal or called with a plain string.
54
+ *
55
+ * By default, it will dedent interpolated multi-line strings to match the surrounding context.
56
+ * And it will not show falsy values.
57
+ *
58
+ * @example
59
+ * ```typescript
60
+ * const text = dedent`
61
+ * function hello() {
62
+ * console.log('world');
63
+ * }
64
+ * `;
65
+ * // Result:
66
+ * "function hello() {
67
+ * console.log('world');
68
+ * }"
69
+ * ```
70
+ */
11
71
  declare const dedent: Dedent;
12
72
 
13
73
  export { type CreateDedent, type Dedent, type DedentOptions, dedent };
package/lib/dedent.d.ts CHANGED
@@ -1,13 +1,73 @@
1
+ /**
2
+ * Configuration options for the dedent function behavior
3
+ */
1
4
  interface DedentOptions {
5
+ /**
6
+ * Whether to process escape sequences like \n, \`, \$, and \{
7
+ * When true, allows using escaped characters in template literals
8
+ * @default true for template literals, false for plain strings
9
+ */
2
10
  escapeSpecialCharacters?: boolean;
11
+ /**
12
+ * Whether to trim leading and trailing whitespace from the final result
13
+ * @default true
14
+ */
3
15
  trimWhitespace?: boolean;
16
+ /**
17
+ * Whether to preserve relative indentation of interpolated multi-line values
18
+ * When true, multi-line interpolations are re-indented to match the surrounding context
19
+ * @default true
20
+ */
21
+ identInterpolations?: boolean;
22
+ /**
23
+ * Whether to display nullish or false values (false, null, undefined) in interpolations
24
+ * When false, nullish or false values are skipped entirely
25
+ * @default false
26
+ */
27
+ showNullishOrFalseValues?: boolean;
4
28
  }
29
+ type InterpolationValue = string | number | boolean | null | undefined;
30
+ /**
31
+ * Dedent function interface that can be used both as a template tag and a regular function
32
+ */
5
33
  interface Dedent {
34
+ /**
35
+ * Process a plain string to remove common indentation
36
+ */
6
37
  (literals: string): string;
7
- (strings: TemplateStringsArray, ...values: unknown[]): string;
38
+ /**
39
+ * Process a template literal to remove common indentation while handling interpolations
40
+ */
41
+ (strings: TemplateStringsArray, ...values: InterpolationValue[]): string;
42
+ /**
43
+ * Create a new dedent function with custom options
44
+ */
8
45
  withOptions: CreateDedent;
9
46
  }
47
+ /**
48
+ * Factory function type for creating dedent functions with custom options
49
+ */
10
50
  type CreateDedent = (options: DedentOptions) => Dedent;
51
+ /**
52
+ * Remove common leading indentation from multi-line strings while preserving relative indentation.
53
+ * Can be used as a tagged template literal or called with a plain string.
54
+ *
55
+ * By default, it will dedent interpolated multi-line strings to match the surrounding context.
56
+ * And it will not show falsy values.
57
+ *
58
+ * @example
59
+ * ```typescript
60
+ * const text = dedent`
61
+ * function hello() {
62
+ * console.log('world');
63
+ * }
64
+ * `;
65
+ * // Result:
66
+ * "function hello() {
67
+ * console.log('world');
68
+ * }"
69
+ * ```
70
+ */
11
71
  declare const dedent: Dedent;
12
72
 
13
73
  export { type CreateDedent, type Dedent, type DedentOptions, dedent };
package/lib/dedent.js CHANGED
@@ -1,5 +1,7 @@
1
1
  // src/dedent.ts
2
- var dedent = createDedent({});
2
+ var dedent = createDedent({
3
+ identInterpolations: true
4
+ });
3
5
  function createDedent(options) {
4
6
  d.withOptions = (newOptions) => createDedent({ ...options, ...newOptions });
5
7
  return d;
@@ -7,7 +9,9 @@ function createDedent(options) {
7
9
  const raw = typeof strings === "string" ? [strings] : strings.raw;
8
10
  const {
9
11
  escapeSpecialCharacters = Array.isArray(strings),
10
- trimWhitespace = true
12
+ trimWhitespace = true,
13
+ identInterpolations = true,
14
+ showNullishOrFalseValues = false
11
15
  } = options;
12
16
  let result = "";
13
17
  for (let i = 0; i < raw.length; i++) {
@@ -17,7 +21,24 @@ function createDedent(options) {
17
21
  }
18
22
  result += next;
19
23
  if (i < values.length) {
20
- result += values[i];
24
+ let val = values[i];
25
+ if (!showNullishOrFalseValues && (val === false || val === null || val === void 0)) {
26
+ continue;
27
+ }
28
+ val = String(val);
29
+ if (identInterpolations && val.includes("\n")) {
30
+ let withIdent = val;
31
+ const currentIndent = getCurrentIndent(result);
32
+ if (currentIndent && withIdent) {
33
+ const lines2 = withIdent.split("\n");
34
+ withIdent = lines2.map((line, index) => {
35
+ return index === 0 || line === "" ? line : currentIndent + line;
36
+ }).join("\n");
37
+ }
38
+ result += withIdent;
39
+ } else {
40
+ result += val;
41
+ }
21
42
  }
22
43
  }
23
44
  const lines = result.split("\n");
@@ -46,6 +67,13 @@ function createDedent(options) {
46
67
  return result;
47
68
  }
48
69
  }
70
+ function getCurrentIndent(str) {
71
+ const lines = str.split("\n");
72
+ const lastLine = lines[lines.length - 1];
73
+ if (!lastLine) return "";
74
+ const match = lastLine.match(/^(\s*)/);
75
+ return match ? match[1] : "";
76
+ }
49
77
  export {
50
78
  dedent
51
79
  };
@@ -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.19.0",
4
+ "version": "3.21.0",
5
5
  "license": "MIT",
6
6
  "files": [
7
7
  "lib",
package/docs/dedent.md DELETED
@@ -1,117 +0,0 @@
1
- [**@ls-stack/utils**](README.md)
2
-
3
- ***
4
-
5
- [@ls-stack/utils](modules.md) / dedent
6
-
7
- # dedent
8
-
9
- ## Interfaces
10
-
11
- ### Dedent()
12
-
13
- Defined in: [packages/utils/src/dedent.ts:6](https://github.com/lucasols/utils/blob/main/packages/utils/src/dedent.ts#L6)
14
-
15
- #### Call Signature
16
-
17
- ```ts
18
- Dedent(literals): string;
19
- ```
20
-
21
- Defined in: [packages/utils/src/dedent.ts:7](https://github.com/lucasols/utils/blob/main/packages/utils/src/dedent.ts#L7)
22
-
23
- ##### Parameters
24
-
25
- ###### literals
26
-
27
- `string`
28
-
29
- ##### Returns
30
-
31
- `string`
32
-
33
- #### Call Signature
34
-
35
- ```ts
36
- Dedent(strings, ...values): string;
37
- ```
38
-
39
- Defined in: [packages/utils/src/dedent.ts:8](https://github.com/lucasols/utils/blob/main/packages/utils/src/dedent.ts#L8)
40
-
41
- ##### Parameters
42
-
43
- ###### strings
44
-
45
- `TemplateStringsArray`
46
-
47
- ###### values
48
-
49
- ...`unknown`[]
50
-
51
- ##### Returns
52
-
53
- `string`
54
-
55
- #### Properties
56
-
57
- ##### withOptions
58
-
59
- ```ts
60
- withOptions: CreateDedent;
61
- ```
62
-
63
- Defined in: [packages/utils/src/dedent.ts:9](https://github.com/lucasols/utils/blob/main/packages/utils/src/dedent.ts#L9)
64
-
65
- ***
66
-
67
- ### DedentOptions
68
-
69
- Defined in: [packages/utils/src/dedent.ts:1](https://github.com/lucasols/utils/blob/main/packages/utils/src/dedent.ts#L1)
70
-
71
- #### Properties
72
-
73
- ##### escapeSpecialCharacters?
74
-
75
- ```ts
76
- optional escapeSpecialCharacters: boolean;
77
- ```
78
-
79
- Defined in: [packages/utils/src/dedent.ts:2](https://github.com/lucasols/utils/blob/main/packages/utils/src/dedent.ts#L2)
80
-
81
- ##### trimWhitespace?
82
-
83
- ```ts
84
- optional trimWhitespace: boolean;
85
- ```
86
-
87
- Defined in: [packages/utils/src/dedent.ts:3](https://github.com/lucasols/utils/blob/main/packages/utils/src/dedent.ts#L3)
88
-
89
- ## Type Aliases
90
-
91
- ### CreateDedent()
92
-
93
- ```ts
94
- type CreateDedent = (options) => Dedent;
95
- ```
96
-
97
- Defined in: [packages/utils/src/dedent.ts:12](https://github.com/lucasols/utils/blob/main/packages/utils/src/dedent.ts#L12)
98
-
99
- #### Parameters
100
-
101
- ##### options
102
-
103
- [`DedentOptions`](#dedentoptions)
104
-
105
- #### Returns
106
-
107
- [`Dedent`](#dedent)
108
-
109
- ## Variables
110
-
111
- ### dedent
112
-
113
- ```ts
114
- const dedent: Dedent;
115
- ```
116
-
117
- Defined in: [packages/utils/src/dedent.ts:14](https://github.com/lucasols/utils/blob/main/packages/utils/src/dedent.ts#L14)
@@ -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`\>