@ls-stack/utils 3.27.2 → 3.29.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.
@@ -10,7 +10,7 @@
10
10
 
11
11
  ### ConcurrentCalls\<R, E\>
12
12
 
13
- Defined in: [packages/utils/src/concurrentCalls.ts:31](https://github.com/lucasols/utils/blob/main/packages/utils/src/concurrentCalls.ts#L31)
13
+ Defined in: [packages/utils/src/concurrentCalls.ts:124](https://github.com/lucasols/utils/blob/main/packages/utils/src/concurrentCalls.ts#L124)
14
14
 
15
15
  #### Type Parameters
16
16
 
@@ -42,7 +42,7 @@ new ConcurrentCalls<R, E>(): ConcurrentCalls<R, E>;
42
42
  add(...calls): this;
43
43
  ```
44
44
 
45
- Defined in: [packages/utils/src/concurrentCalls.ts:35](https://github.com/lucasols/utils/blob/main/packages/utils/src/concurrentCalls.ts#L35)
45
+ Defined in: [packages/utils/src/concurrentCalls.ts:128](https://github.com/lucasols/utils/blob/main/packages/utils/src/concurrentCalls.ts#L128)
46
46
 
47
47
  ###### Parameters
48
48
 
@@ -60,7 +60,7 @@ Defined in: [packages/utils/src/concurrentCalls.ts:35](https://github.com/lucaso
60
60
  resultifyAdd(...calls): this;
61
61
  ```
62
62
 
63
- Defined in: [packages/utils/src/concurrentCalls.ts:41](https://github.com/lucasols/utils/blob/main/packages/utils/src/concurrentCalls.ts#L41)
63
+ Defined in: [packages/utils/src/concurrentCalls.ts:134](https://github.com/lucasols/utils/blob/main/packages/utils/src/concurrentCalls.ts#L134)
64
64
 
65
65
  ###### Parameters
66
66
 
@@ -78,7 +78,7 @@ Defined in: [packages/utils/src/concurrentCalls.ts:41](https://github.com/lucaso
78
78
  runAll(__namedParameters): Promise<Result<R[], E>>;
79
79
  ```
80
80
 
81
- Defined in: [packages/utils/src/concurrentCalls.ts:58](https://github.com/lucasols/utils/blob/main/packages/utils/src/concurrentCalls.ts#L58)
81
+ Defined in: [packages/utils/src/concurrentCalls.ts:151](https://github.com/lucasols/utils/blob/main/packages/utils/src/concurrentCalls.ts#L151)
82
82
 
83
83
  ###### Parameters
84
84
 
@@ -94,7 +94,8 @@ Defined in: [packages/utils/src/concurrentCalls.ts:58](https://github.com/lucaso
94
94
 
95
95
  ```ts
96
96
  runAllSettled(__namedParameters): Promise<{
97
- aggregatedError: null | AggregateError;
97
+ aggregatedError: | null
98
+ | ConcurrentCallsAggregateError;
98
99
  allFailed: boolean;
99
100
  failures: E[];
100
101
  succeeded: R[];
@@ -102,7 +103,7 @@ runAllSettled(__namedParameters): Promise<{
102
103
  }>;
103
104
  ```
104
105
 
105
- Defined in: [packages/utils/src/concurrentCalls.ts:90](https://github.com/lucasols/utils/blob/main/packages/utils/src/concurrentCalls.ts#L90)
106
+ Defined in: [packages/utils/src/concurrentCalls.ts:183](https://github.com/lucasols/utils/blob/main/packages/utils/src/concurrentCalls.ts#L183)
106
107
 
107
108
  ###### Parameters
108
109
 
@@ -113,7 +114,8 @@ Defined in: [packages/utils/src/concurrentCalls.ts:90](https://github.com/lucaso
113
114
  ###### Returns
114
115
 
115
116
  `Promise`\<\{
116
- `aggregatedError`: `null` \| `AggregateError`;
117
+ `aggregatedError`: \| `null`
118
+ \| [`ConcurrentCallsAggregateError`](README.md#concurrentcallsaggregateerror);
117
119
  `allFailed`: `boolean`;
118
120
  `failures`: `E`[];
119
121
  `succeeded`: `R`[];
@@ -124,7 +126,7 @@ Defined in: [packages/utils/src/concurrentCalls.ts:90](https://github.com/lucaso
124
126
 
125
127
  ### ConcurrentCallsWithMetadata\<M, R, E\>
126
128
 
127
- Defined in: [packages/utils/src/concurrentCalls.ts:157](https://github.com/lucasols/utils/blob/main/packages/utils/src/concurrentCalls.ts#L157)
129
+ Defined in: [packages/utils/src/concurrentCalls.ts:250](https://github.com/lucasols/utils/blob/main/packages/utils/src/concurrentCalls.ts#L250)
128
130
 
129
131
  #### Type Parameters
130
132
 
@@ -160,7 +162,7 @@ new ConcurrentCallsWithMetadata<M, R, E>(): ConcurrentCallsWithMetadata<M, R, E>
160
162
  add(...calls): this;
161
163
  ```
162
164
 
163
- Defined in: [packages/utils/src/concurrentCalls.ts:165](https://github.com/lucasols/utils/blob/main/packages/utils/src/concurrentCalls.ts#L165)
165
+ Defined in: [packages/utils/src/concurrentCalls.ts:258](https://github.com/lucasols/utils/blob/main/packages/utils/src/concurrentCalls.ts#L258)
164
166
 
165
167
  ###### Parameters
166
168
 
@@ -178,7 +180,7 @@ Defined in: [packages/utils/src/concurrentCalls.ts:165](https://github.com/lucas
178
180
  resultifyAdd(...items): this;
179
181
  ```
180
182
 
181
- Defined in: [packages/utils/src/concurrentCalls.ts:179](https://github.com/lucasols/utils/blob/main/packages/utils/src/concurrentCalls.ts#L179)
183
+ Defined in: [packages/utils/src/concurrentCalls.ts:272](https://github.com/lucasols/utils/blob/main/packages/utils/src/concurrentCalls.ts#L272)
182
184
 
183
185
  ###### Parameters
184
186
 
@@ -196,7 +198,7 @@ Defined in: [packages/utils/src/concurrentCalls.ts:179](https://github.com/lucas
196
198
  runAll(__namedParameters): Promise<Result<SucceededCall<R, M>[], FailedCall<M, E>>>;
197
199
  ```
198
200
 
199
- Defined in: [packages/utils/src/concurrentCalls.ts:199](https://github.com/lucasols/utils/blob/main/packages/utils/src/concurrentCalls.ts#L199)
201
+ Defined in: [packages/utils/src/concurrentCalls.ts:292](https://github.com/lucasols/utils/blob/main/packages/utils/src/concurrentCalls.ts#L292)
200
202
 
201
203
  ###### Parameters
202
204
 
@@ -212,7 +214,8 @@ Defined in: [packages/utils/src/concurrentCalls.ts:199](https://github.com/lucas
212
214
 
213
215
  ```ts
214
216
  runAllSettled(__namedParameters): Promise<{
215
- aggregatedError: null | AggregateError;
217
+ aggregatedError: | null
218
+ | ConcurrentCallsWithMetadataAggregateError<M>;
216
219
  allFailed: boolean;
217
220
  failed: FailedCall<M, E>[];
218
221
  failures: FailedCall<M, E>[];
@@ -222,7 +225,7 @@ runAllSettled(__namedParameters): Promise<{
222
225
  }>;
223
226
  ```
224
227
 
225
- Defined in: [packages/utils/src/concurrentCalls.ts:246](https://github.com/lucasols/utils/blob/main/packages/utils/src/concurrentCalls.ts#L246)
228
+ Defined in: [packages/utils/src/concurrentCalls.ts:339](https://github.com/lucasols/utils/blob/main/packages/utils/src/concurrentCalls.ts#L339)
226
229
 
227
230
  ###### Parameters
228
231
 
@@ -233,7 +236,8 @@ Defined in: [packages/utils/src/concurrentCalls.ts:246](https://github.com/lucas
233
236
  ###### Returns
234
237
 
235
238
  `Promise`\<\{
236
- `aggregatedError`: `null` \| `AggregateError`;
239
+ `aggregatedError`: \| `null`
240
+ \| [`ConcurrentCallsWithMetadataAggregateError`](README.md#concurrentcallswithmetadataaggregateerror)\<`M`\>;
237
241
  `allFailed`: `boolean`;
238
242
  `failed`: [`FailedCall`](#failedcall)\<`M`, `E`\>[];
239
243
  `failures`: [`FailedCall`](#failedcall)\<`M`, `E`\>[];
@@ -250,7 +254,7 @@ Defined in: [packages/utils/src/concurrentCalls.ts:246](https://github.com/lucas
250
254
  type Action<R, E> = () => Promise<Result<R, E>>;
251
255
  ```
252
256
 
253
- Defined in: [packages/utils/src/concurrentCalls.ts:24](https://github.com/lucasols/utils/blob/main/packages/utils/src/concurrentCalls.ts#L24)
257
+ Defined in: [packages/utils/src/concurrentCalls.ts:117](https://github.com/lucasols/utils/blob/main/packages/utils/src/concurrentCalls.ts#L117)
254
258
 
255
259
  #### Type Parameters
256
260
 
@@ -274,7 +278,7 @@ Defined in: [packages/utils/src/concurrentCalls.ts:24](https://github.com/lucaso
274
278
  type FailedCall<M, E> = object;
275
279
  ```
276
280
 
277
- Defined in: [packages/utils/src/concurrentCalls.ts:19](https://github.com/lucasols/utils/blob/main/packages/utils/src/concurrentCalls.ts#L19)
281
+ Defined in: [packages/utils/src/concurrentCalls.ts:112](https://github.com/lucasols/utils/blob/main/packages/utils/src/concurrentCalls.ts#L112)
278
282
 
279
283
  #### Type Parameters
280
284
 
@@ -294,7 +298,7 @@ Defined in: [packages/utils/src/concurrentCalls.ts:19](https://github.com/lucaso
294
298
  error: E;
295
299
  ```
296
300
 
297
- Defined in: [packages/utils/src/concurrentCalls.ts:21](https://github.com/lucasols/utils/blob/main/packages/utils/src/concurrentCalls.ts#L21)
301
+ Defined in: [packages/utils/src/concurrentCalls.ts:114](https://github.com/lucasols/utils/blob/main/packages/utils/src/concurrentCalls.ts#L114)
298
302
 
299
303
  ##### metadata
300
304
 
@@ -302,7 +306,7 @@ Defined in: [packages/utils/src/concurrentCalls.ts:21](https://github.com/lucaso
302
306
  metadata: M;
303
307
  ```
304
308
 
305
- Defined in: [packages/utils/src/concurrentCalls.ts:20](https://github.com/lucasols/utils/blob/main/packages/utils/src/concurrentCalls.ts#L20)
309
+ Defined in: [packages/utils/src/concurrentCalls.ts:113](https://github.com/lucasols/utils/blob/main/packages/utils/src/concurrentCalls.ts#L113)
306
310
 
307
311
  ***
308
312
 
@@ -312,7 +316,7 @@ Defined in: [packages/utils/src/concurrentCalls.ts:20](https://github.com/lucaso
312
316
  type RunProps = object;
313
317
  ```
314
318
 
315
- Defined in: [packages/utils/src/concurrentCalls.ts:10](https://github.com/lucasols/utils/blob/main/packages/utils/src/concurrentCalls.ts#L10)
319
+ Defined in: [packages/utils/src/concurrentCalls.ts:103](https://github.com/lucasols/utils/blob/main/packages/utils/src/concurrentCalls.ts#L103)
316
320
 
317
321
  #### Properties
318
322
 
@@ -322,7 +326,7 @@ Defined in: [packages/utils/src/concurrentCalls.ts:10](https://github.com/lucaso
322
326
  optional delayStart: (index) => number;
323
327
  ```
324
328
 
325
- Defined in: [packages/utils/src/concurrentCalls.ts:11](https://github.com/lucasols/utils/blob/main/packages/utils/src/concurrentCalls.ts#L11)
329
+ Defined in: [packages/utils/src/concurrentCalls.ts:104](https://github.com/lucasols/utils/blob/main/packages/utils/src/concurrentCalls.ts#L104)
326
330
 
327
331
  ###### Parameters
328
332
 
@@ -353,7 +357,7 @@ type SettledResultWithMetadata<R, M, E> =
353
357
  };
354
358
  ```
355
359
 
356
- Defined in: [packages/utils/src/concurrentCalls.ts:27](https://github.com/lucasols/utils/blob/main/packages/utils/src/concurrentCalls.ts#L27)
360
+ Defined in: [packages/utils/src/concurrentCalls.ts:120](https://github.com/lucasols/utils/blob/main/packages/utils/src/concurrentCalls.ts#L120)
357
361
 
358
362
  #### Type Parameters
359
363
 
@@ -377,7 +381,7 @@ Defined in: [packages/utils/src/concurrentCalls.ts:27](https://github.com/lucaso
377
381
  type SucceededCall<R, M> = object;
378
382
  ```
379
383
 
380
- Defined in: [packages/utils/src/concurrentCalls.ts:15](https://github.com/lucasols/utils/blob/main/packages/utils/src/concurrentCalls.ts#L15)
384
+ Defined in: [packages/utils/src/concurrentCalls.ts:108](https://github.com/lucasols/utils/blob/main/packages/utils/src/concurrentCalls.ts#L108)
381
385
 
382
386
  #### Type Parameters
383
387
 
@@ -397,7 +401,7 @@ Defined in: [packages/utils/src/concurrentCalls.ts:15](https://github.com/lucaso
397
401
  metadata: M;
398
402
  ```
399
403
 
400
- Defined in: [packages/utils/src/concurrentCalls.ts:17](https://github.com/lucasols/utils/blob/main/packages/utils/src/concurrentCalls.ts#L17)
404
+ Defined in: [packages/utils/src/concurrentCalls.ts:110](https://github.com/lucasols/utils/blob/main/packages/utils/src/concurrentCalls.ts#L110)
401
405
 
402
406
  ##### value
403
407
 
@@ -405,7 +409,7 @@ Defined in: [packages/utils/src/concurrentCalls.ts:17](https://github.com/lucaso
405
409
  value: R;
406
410
  ```
407
411
 
408
- Defined in: [packages/utils/src/concurrentCalls.ts:16](https://github.com/lucasols/utils/blob/main/packages/utils/src/concurrentCalls.ts#L16)
412
+ Defined in: [packages/utils/src/concurrentCalls.ts:109](https://github.com/lucasols/utils/blob/main/packages/utils/src/concurrentCalls.ts#L109)
409
413
 
410
414
  ***
411
415
 
@@ -415,4 +419,4 @@ Defined in: [packages/utils/src/concurrentCalls.ts:16](https://github.com/lucaso
415
419
  type ValidMetadata = string | number | boolean | Record<string, unknown>;
416
420
  ```
417
421
 
418
- Defined in: [packages/utils/src/concurrentCalls.ts:8](https://github.com/lucasols/utils/blob/main/packages/utils/src/concurrentCalls.ts#L8)
422
+ Defined in: [packages/utils/src/concurrentCalls.ts:101](https://github.com/lucasols/utils/blob/main/packages/utils/src/concurrentCalls.ts#L101)
@@ -10,6 +10,190 @@
10
10
 
11
11
  - [\<internal\>](-internal-.md)
12
12
 
13
+ ## Classes
14
+
15
+ ### ConcurrentCallsAggregateError
16
+
17
+ Defined in: [packages/utils/src/concurrentCalls.ts:32](https://github.com/lucasols/utils/blob/main/packages/utils/src/concurrentCalls.ts#L32)
18
+
19
+ #### Extends
20
+
21
+ - `AggregateError`
22
+
23
+ #### Constructors
24
+
25
+ ##### Constructor
26
+
27
+ ```ts
28
+ new ConcurrentCallsAggregateError(
29
+ errors,
30
+ total,
31
+ failed): ConcurrentCallsAggregateError;
32
+ ```
33
+
34
+ Defined in: [packages/utils/src/concurrentCalls.ts:37](https://github.com/lucasols/utils/blob/main/packages/utils/src/concurrentCalls.ts#L37)
35
+
36
+ ###### Parameters
37
+
38
+ ###### errors
39
+
40
+ `Error`[]
41
+
42
+ ###### total
43
+
44
+ `number`
45
+
46
+ ###### failed
47
+
48
+ `number`
49
+
50
+ ###### Returns
51
+
52
+ [`ConcurrentCallsAggregateError`](#concurrentcallsaggregateerror)
53
+
54
+ ###### Overrides
55
+
56
+ ```ts
57
+ AggregateError.constructor
58
+ ```
59
+
60
+ #### Properties
61
+
62
+ ##### errors
63
+
64
+ ```ts
65
+ errors: Error[] = [];
66
+ ```
67
+
68
+ Defined in: [packages/utils/src/concurrentCalls.ts:33](https://github.com/lucasols/utils/blob/main/packages/utils/src/concurrentCalls.ts#L33)
69
+
70
+ ###### Overrides
71
+
72
+ ```ts
73
+ AggregateError.errors
74
+ ```
75
+
76
+ ##### failed
77
+
78
+ ```ts
79
+ failed: number = 0;
80
+ ```
81
+
82
+ Defined in: [packages/utils/src/concurrentCalls.ts:35](https://github.com/lucasols/utils/blob/main/packages/utils/src/concurrentCalls.ts#L35)
83
+
84
+ ##### total
85
+
86
+ ```ts
87
+ total: number = 0;
88
+ ```
89
+
90
+ Defined in: [packages/utils/src/concurrentCalls.ts:34](https://github.com/lucasols/utils/blob/main/packages/utils/src/concurrentCalls.ts#L34)
91
+
92
+ ***
93
+
94
+ ### ConcurrentCallsWithMetadataAggregateError\<M\>
95
+
96
+ Defined in: [packages/utils/src/concurrentCalls.ts:50](https://github.com/lucasols/utils/blob/main/packages/utils/src/concurrentCalls.ts#L50)
97
+
98
+ #### Extends
99
+
100
+ - `AggregateError`
101
+
102
+ #### Type Parameters
103
+
104
+ ##### M
105
+
106
+ `M` *extends* [`ValidMetadata`](-internal-.md#validmetadata)
107
+
108
+ #### Constructors
109
+
110
+ ##### Constructor
111
+
112
+ ```ts
113
+ new ConcurrentCallsWithMetadataAggregateError<M>(
114
+ errors,
115
+ total,
116
+ failed): ConcurrentCallsWithMetadataAggregateError<M>;
117
+ ```
118
+
119
+ Defined in: [packages/utils/src/concurrentCalls.ts:58](https://github.com/lucasols/utils/blob/main/packages/utils/src/concurrentCalls.ts#L58)
120
+
121
+ ###### Parameters
122
+
123
+ ###### errors
124
+
125
+ `object`[]
126
+
127
+ ###### total
128
+
129
+ `number`
130
+
131
+ ###### failed
132
+
133
+ `number`
134
+
135
+ ###### Returns
136
+
137
+ [`ConcurrentCallsWithMetadataAggregateError`](#concurrentcallswithmetadataaggregateerror)\<`M`\>
138
+
139
+ ###### Overrides
140
+
141
+ ```ts
142
+ AggregateError.constructor
143
+ ```
144
+
145
+ #### Properties
146
+
147
+ ##### errors
148
+
149
+ ```ts
150
+ errors: Error[] = [];
151
+ ```
152
+
153
+ Defined in: [packages/utils/src/concurrentCalls.ts:53](https://github.com/lucasols/utils/blob/main/packages/utils/src/concurrentCalls.ts#L53)
154
+
155
+ ###### Overrides
156
+
157
+ ```ts
158
+ AggregateError.errors
159
+ ```
160
+
161
+ ##### errorsWithMetadata
162
+
163
+ ```ts
164
+ errorsWithMetadata: object[] = [];
165
+ ```
166
+
167
+ Defined in: [packages/utils/src/concurrentCalls.ts:54](https://github.com/lucasols/utils/blob/main/packages/utils/src/concurrentCalls.ts#L54)
168
+
169
+ ###### error
170
+
171
+ ```ts
172
+ error: Error;
173
+ ```
174
+
175
+ ###### metadata
176
+
177
+ ```ts
178
+ metadata: M;
179
+ ```
180
+
181
+ ##### failed
182
+
183
+ ```ts
184
+ failed: number = 0;
185
+ ```
186
+
187
+ Defined in: [packages/utils/src/concurrentCalls.ts:56](https://github.com/lucasols/utils/blob/main/packages/utils/src/concurrentCalls.ts#L56)
188
+
189
+ ##### total
190
+
191
+ ```ts
192
+ total: number = 0;
193
+ ```
194
+
195
+ Defined in: [packages/utils/src/concurrentCalls.ts:55](https://github.com/lucasols/utils/blob/main/packages/utils/src/concurrentCalls.ts#L55)
196
+
13
197
  ## Functions
14
198
 
15
199
  ### concurrentCalls()
@@ -18,7 +202,7 @@
18
202
  function concurrentCalls<R>(): ConcurrentCalls<R, Error>;
19
203
  ```
20
204
 
21
- Defined in: [packages/utils/src/concurrentCalls.ts:153](https://github.com/lucasols/utils/blob/main/packages/utils/src/concurrentCalls.ts#L153)
205
+ Defined in: [packages/utils/src/concurrentCalls.ts:246](https://github.com/lucasols/utils/blob/main/packages/utils/src/concurrentCalls.ts#L246)
22
206
 
23
207
  Executes multiple asynchronous calls concurrently and collects the results in a easier to use format.
24
208
 
@@ -42,7 +226,7 @@ The type of the result value.
42
226
  function concurrentCallsWithMetadata<M, R>(): ConcurrentCallsWithMetadata<M, R, Error>;
43
227
  ```
44
228
 
45
- Defined in: [packages/utils/src/concurrentCalls.ts:345](https://github.com/lucasols/utils/blob/main/packages/utils/src/concurrentCalls.ts#L345)
229
+ Defined in: [packages/utils/src/concurrentCalls.ts:435](https://github.com/lucasols/utils/blob/main/packages/utils/src/concurrentCalls.ts#L435)
46
230
 
47
231
  Executes multiple asynchronous calls concurrently and collects the results in a easier to use format.
48
232
 
@@ -14,7 +14,7 @@
14
14
  type RetryOptions = object;
15
15
  ```
16
16
 
17
- Defined in: [packages/utils/src/retryOnError.ts:6](https://github.com/lucasols/utils/blob/main/packages/utils/src/retryOnError.ts#L6)
17
+ Defined in: [packages/utils/src/retryOnError.ts:7](https://github.com/lucasols/utils/blob/main/packages/utils/src/retryOnError.ts#L7)
18
18
 
19
19
  Configuration options for retryOnError function.
20
20
 
@@ -26,7 +26,7 @@ Configuration options for retryOnError function.
26
26
  optional debugId: string;
27
27
  ```
28
28
 
29
- Defined in: [packages/utils/src/retryOnError.ts:15](https://github.com/lucasols/utils/blob/main/packages/utils/src/retryOnError.ts#L15)
29
+ Defined in: [packages/utils/src/retryOnError.ts:16](https://github.com/lucasols/utils/blob/main/packages/utils/src/retryOnError.ts#L16)
30
30
 
31
31
  Optional ID for debug logging
32
32
 
@@ -36,17 +36,27 @@ Optional ID for debug logging
36
36
  optional delayBetweenRetriesMs: number | (retry) => number;
37
37
  ```
38
38
 
39
- Defined in: [packages/utils/src/retryOnError.ts:8](https://github.com/lucasols/utils/blob/main/packages/utils/src/retryOnError.ts#L8)
39
+ Defined in: [packages/utils/src/retryOnError.ts:9](https://github.com/lucasols/utils/blob/main/packages/utils/src/retryOnError.ts#L9)
40
40
 
41
41
  Delay between retries in milliseconds or function returning delay
42
42
 
43
+ ##### disableRetries?
44
+
45
+ ```ts
46
+ optional disableRetries: boolean;
47
+ ```
48
+
49
+ Defined in: [packages/utils/src/retryOnError.ts:18](https://github.com/lucasols/utils/blob/main/packages/utils/src/retryOnError.ts#L18)
50
+
51
+ Disable retries
52
+
43
53
  ##### retryCondition()?
44
54
 
45
55
  ```ts
46
56
  optional retryCondition: (error, lastAttempt) => boolean;
47
57
  ```
48
58
 
49
- Defined in: [packages/utils/src/retryOnError.ts:10](https://github.com/lucasols/utils/blob/main/packages/utils/src/retryOnError.ts#L10)
59
+ Defined in: [packages/utils/src/retryOnError.ts:11](https://github.com/lucasols/utils/blob/main/packages/utils/src/retryOnError.ts#L11)
50
60
 
51
61
  Function to determine if retry should happen, receives error and duration of last attempt
52
62
 
@@ -54,7 +64,7 @@ Function to determine if retry should happen, receives error and duration of las
54
64
 
55
65
  ###### error
56
66
 
57
- `unknown`
67
+ `Error`
58
68
 
59
69
  ###### lastAttempt
60
70
 
@@ -23,7 +23,7 @@ function retryOnError<T>(
23
23
  originalMaxRetries): Promise<T>;
24
24
  ```
25
25
 
26
- Defined in: [packages/utils/src/retryOnError.ts:38](https://github.com/lucasols/utils/blob/main/packages/utils/src/retryOnError.ts#L38)
26
+ Defined in: [packages/utils/src/retryOnError.ts:41](https://github.com/lucasols/utils/blob/main/packages/utils/src/retryOnError.ts#L41)
27
27
 
28
28
  Retries a function on error with configurable retry logic.
29
29
 
@@ -57,10 +57,14 @@ Configuration options
57
57
 
58
58
  `number` = `0`
59
59
 
60
+ internal use only
61
+
60
62
  ##### originalMaxRetries
61
63
 
62
64
  `number` = `maxRetries`
63
65
 
66
+ internal use only
67
+
64
68
  #### Returns
65
69
 
66
70
  `Promise`\<`T`\>
@@ -79,3 +83,82 @@ await retryOnError(
79
83
  { delayBetweenRetriesMs: 1000 }
80
84
  );
81
85
  ```
86
+
87
+ ***
88
+
89
+ ### retryResultOnError()
90
+
91
+ ```ts
92
+ function retryResultOnError<T, E>(
93
+ fn,
94
+ maxRetries,
95
+ options,
96
+ __retry,
97
+ __originalMaxRetries): Promise<Result<T, E>>;
98
+ ```
99
+
100
+ Defined in: [packages/utils/src/retryOnError.ts:116](https://github.com/lucasols/utils/blob/main/packages/utils/src/retryOnError.ts#L116)
101
+
102
+ Retries a result function on error with configurable retry logic.
103
+
104
+ #### Type Parameters
105
+
106
+ ##### T
107
+
108
+ `T`
109
+
110
+ ##### E
111
+
112
+ `E` *extends* `ResultValidErrors`
113
+
114
+ #### Parameters
115
+
116
+ ##### fn
117
+
118
+ (`ctx`) => `Promise`\<`Result`\<`T`, `E`\>\>
119
+
120
+ Function to retry that receives context with retry count
121
+
122
+ ##### maxRetries
123
+
124
+ `number`
125
+
126
+ Maximum number of retries
127
+
128
+ ##### options
129
+
130
+ Configuration options
131
+
132
+ ###### debugId?
133
+
134
+ `string`
135
+
136
+ ###### delayBetweenRetriesMs?
137
+
138
+ `number` \| (`retry`) => `number`
139
+
140
+ ###### disableRetries?
141
+
142
+ `boolean`
143
+
144
+ ###### retryCondition?
145
+
146
+ (`error`, `lastAttempt`) => `boolean`
147
+
148
+ ##### \_\_retry
149
+
150
+ `number` = `0`
151
+
152
+ internal use only
153
+
154
+ ##### \_\_originalMaxRetries
155
+
156
+ `number` = `maxRetries`
157
+
158
+ internal use only
159
+
160
+ #### Returns
161
+
162
+ `Promise`\<`Result`\<`T`, `E`\>\>
163
+
164
+ Promise resolving to the function result or rejecting with the final error
@@ -20,6 +20,8 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
20
20
  // src/concurrentCalls.ts
21
21
  var concurrentCalls_exports = {};
22
22
  __export(concurrentCalls_exports, {
23
+ ConcurrentCallsAggregateError: () => ConcurrentCallsAggregateError,
24
+ ConcurrentCallsWithMetadataAggregateError: () => ConcurrentCallsWithMetadataAggregateError,
23
25
  concurrentCalls: () => concurrentCalls,
24
26
  concurrentCallsWithMetadata: () => concurrentCallsWithMetadata
25
27
  });
@@ -70,6 +72,67 @@ function truncateString(str, length, ellipsis = "\u2026") {
70
72
  }
71
73
 
72
74
  // src/concurrentCalls.ts
75
+ function formatErrorMessagesWithCounts(messages, failed, total) {
76
+ const errorMessageCounts = /* @__PURE__ */ new Map();
77
+ for (const message of messages) {
78
+ errorMessageCounts.set(message, (errorMessageCounts.get(message) || 0) + 1);
79
+ }
80
+ const formattedMessages = Array.from(errorMessageCounts.entries()).map(
81
+ ([message, count]) => count > 1 ? `${message} (${count}x)` : message
82
+ );
83
+ return `${failed}/${total} calls failed:
84
+ ${truncateArray(
85
+ formattedMessages,
86
+ 5,
87
+ "..."
88
+ ).join("\n")}`;
89
+ }
90
+ var ConcurrentCallsAggregateError = class extends AggregateError {
91
+ errors = [];
92
+ total = 0;
93
+ failed = 0;
94
+ constructor(errors, total, failed) {
95
+ const messages = errors.map(
96
+ (error) => `- ${truncateString(error.message, 100)}`
97
+ );
98
+ const message = formatErrorMessagesWithCounts(messages, failed, total);
99
+ super(errors, message);
100
+ this.errors = errors;
101
+ this.total = total;
102
+ this.failed = failed;
103
+ }
104
+ };
105
+ var ConcurrentCallsWithMetadataAggregateError = class extends AggregateError {
106
+ errors = [];
107
+ errorsWithMetadata = [];
108
+ total = 0;
109
+ failed = 0;
110
+ constructor(errors, total, failed) {
111
+ const messages = errors.map(({ error, metadata }) => {
112
+ let metadataPrefix = typeof metadata === "string" || typeof metadata === "number" || typeof metadata === "boolean" ? `${metadata}: ` : "";
113
+ try {
114
+ const isArray = Array.isArray(metadata);
115
+ if (isObject(metadata) || isArray) {
116
+ const stringifiedMetadata = JSON.stringify(metadata);
117
+ if (stringifiedMetadata.length < 30) {
118
+ metadataPrefix = `${stringifiedMetadata}: `;
119
+ } else {
120
+ metadataPrefix = `[${isArray ? "Array" : "Object"} metadata]: `;
121
+ }
122
+ }
123
+ } catch (_) {
124
+ }
125
+ return `- ${metadataPrefix}${truncateString(error.message, 100)}`;
126
+ });
127
+ const message = formatErrorMessagesWithCounts(messages, failed, total);
128
+ const errorInstances = errors.map((e) => e.error);
129
+ super(errorInstances, message);
130
+ this.errors = errorInstances;
131
+ this.errorsWithMetadata = errors;
132
+ this.total = total;
133
+ this.failed = failed;
134
+ }
135
+ };
73
136
  var ConcurrentCalls = class {
74
137
  #pendingCalls = [];
75
138
  #alreadyRun = false;
@@ -146,7 +209,7 @@ var ConcurrentCalls = class {
146
209
  }
147
210
  }
148
211
  const allFailed = failed.length === total;
149
- const aggregatedError = failed.length > 0 ? new AggregateError(failed, "One or more calls failed") : null;
212
+ const aggregatedError = failed.length > 0 ? new ConcurrentCallsAggregateError(failed, total, failed.length) : null;
150
213
  this.#pendingCalls = [];
151
214
  return {
152
215
  succeeded,
@@ -272,13 +335,10 @@ var ConcurrentCallsWithMetadata = class {
272
335
  }
273
336
  }
274
337
  const allFailed = failedProcessing.length === total;
275
- const aggregatedError = failedProcessing.length > 0 ? new AggregateError(
276
- failedProcessing.map((f) => f.error),
277
- `${failedProcessing.length}/${total} calls failed: ${truncateArray(
278
- failedProcessing.map((f) => truncateString(f.error.message, 20)),
279
- 5,
280
- "..."
281
- ).join("; ")}`
338
+ const aggregatedError = failedProcessing.length > 0 ? new ConcurrentCallsWithMetadataAggregateError(
339
+ failedProcessing,
340
+ total,
341
+ failedProcessing.length
282
342
  ) : null;
283
343
  return {
284
344
  succeeded: succeededProcessing,
@@ -296,6 +356,8 @@ function concurrentCallsWithMetadata() {
296
356
  }
297
357
  // Annotate the CommonJS export names for ESM import in node:
298
358
  0 && (module.exports = {
359
+ ConcurrentCallsAggregateError,
360
+ ConcurrentCallsWithMetadataAggregateError,
299
361
  concurrentCalls,
300
362
  concurrentCallsWithMetadata
301
363
  });
@@ -1,5 +1,24 @@
1
1
  import { Result } from 't-result';
2
2
 
3
+ declare class ConcurrentCallsAggregateError extends AggregateError {
4
+ errors: Error[];
5
+ total: number;
6
+ failed: number;
7
+ constructor(errors: Error[], total: number, failed: number);
8
+ }
9
+ declare class ConcurrentCallsWithMetadataAggregateError<M extends ValidMetadata> extends AggregateError {
10
+ errors: Error[];
11
+ errorsWithMetadata: {
12
+ error: Error;
13
+ metadata: M;
14
+ }[];
15
+ total: number;
16
+ failed: number;
17
+ constructor(errors: {
18
+ error: Error;
19
+ metadata: M;
20
+ }[], total: number, failed: number);
21
+ }
3
22
  type ValidMetadata = string | number | boolean | Record<string, unknown>;
4
23
  type RunProps = {
5
24
  delayStart?: (index: number) => number;
@@ -33,7 +52,7 @@ declare class ConcurrentCalls<R = unknown, E extends Error = Error> {
33
52
  failures: E[];
34
53
  succeeded: R[];
35
54
  total: number;
36
- aggregatedError: AggregateError | null;
55
+ aggregatedError: ConcurrentCallsAggregateError | null;
37
56
  }>;
38
57
  }
39
58
  /**
@@ -62,7 +81,7 @@ declare class ConcurrentCallsWithMetadata<M extends ValidMetadata, R = unknown,
62
81
  succeeded: SucceededCall<R, M>[];
63
82
  total: number;
64
83
  results: SettledResultWithMetadata<R, M, E>[];
65
- aggregatedError: AggregateError | null;
84
+ aggregatedError: ConcurrentCallsWithMetadataAggregateError<M> | null;
66
85
  }>;
67
86
  }
68
87
  /**
@@ -74,4 +93,4 @@ declare class ConcurrentCallsWithMetadata<M extends ValidMetadata, R = unknown,
74
93
  */
75
94
  declare function concurrentCallsWithMetadata<M extends ValidMetadata, R = unknown>(): ConcurrentCallsWithMetadata<M, R, Error>;
76
95
 
77
- export { concurrentCalls, concurrentCallsWithMetadata };
96
+ export { ConcurrentCallsAggregateError, ConcurrentCallsWithMetadataAggregateError, concurrentCalls, concurrentCallsWithMetadata };
@@ -1,5 +1,24 @@
1
1
  import { Result } from 't-result';
2
2
 
3
+ declare class ConcurrentCallsAggregateError extends AggregateError {
4
+ errors: Error[];
5
+ total: number;
6
+ failed: number;
7
+ constructor(errors: Error[], total: number, failed: number);
8
+ }
9
+ declare class ConcurrentCallsWithMetadataAggregateError<M extends ValidMetadata> extends AggregateError {
10
+ errors: Error[];
11
+ errorsWithMetadata: {
12
+ error: Error;
13
+ metadata: M;
14
+ }[];
15
+ total: number;
16
+ failed: number;
17
+ constructor(errors: {
18
+ error: Error;
19
+ metadata: M;
20
+ }[], total: number, failed: number);
21
+ }
3
22
  type ValidMetadata = string | number | boolean | Record<string, unknown>;
4
23
  type RunProps = {
5
24
  delayStart?: (index: number) => number;
@@ -33,7 +52,7 @@ declare class ConcurrentCalls<R = unknown, E extends Error = Error> {
33
52
  failures: E[];
34
53
  succeeded: R[];
35
54
  total: number;
36
- aggregatedError: AggregateError | null;
55
+ aggregatedError: ConcurrentCallsAggregateError | null;
37
56
  }>;
38
57
  }
39
58
  /**
@@ -62,7 +81,7 @@ declare class ConcurrentCallsWithMetadata<M extends ValidMetadata, R = unknown,
62
81
  succeeded: SucceededCall<R, M>[];
63
82
  total: number;
64
83
  results: SettledResultWithMetadata<R, M, E>[];
65
- aggregatedError: AggregateError | null;
84
+ aggregatedError: ConcurrentCallsWithMetadataAggregateError<M> | null;
66
85
  }>;
67
86
  }
68
87
  /**
@@ -74,4 +93,4 @@ declare class ConcurrentCallsWithMetadata<M extends ValidMetadata, R = unknown,
74
93
  */
75
94
  declare function concurrentCallsWithMetadata<M extends ValidMetadata, R = unknown>(): ConcurrentCallsWithMetadata<M, R, Error>;
76
95
 
77
- export { concurrentCalls, concurrentCallsWithMetadata };
96
+ export { ConcurrentCallsAggregateError, ConcurrentCallsWithMetadataAggregateError, concurrentCalls, concurrentCallsWithMetadata };
@@ -11,11 +11,73 @@ import {
11
11
  invariant
12
12
  } from "./chunk-C2SVCIWE.js";
13
13
  import {
14
+ isObject,
14
15
  isPromise
15
16
  } from "./chunk-JF2MDHOJ.js";
16
17
 
17
18
  // src/concurrentCalls.ts
18
19
  import { Result, resultify, unknownToError } from "t-result";
20
+ function formatErrorMessagesWithCounts(messages, failed, total) {
21
+ const errorMessageCounts = /* @__PURE__ */ new Map();
22
+ for (const message of messages) {
23
+ errorMessageCounts.set(message, (errorMessageCounts.get(message) || 0) + 1);
24
+ }
25
+ const formattedMessages = Array.from(errorMessageCounts.entries()).map(
26
+ ([message, count]) => count > 1 ? `${message} (${count}x)` : message
27
+ );
28
+ return `${failed}/${total} calls failed:
29
+ ${truncateArray(
30
+ formattedMessages,
31
+ 5,
32
+ "..."
33
+ ).join("\n")}`;
34
+ }
35
+ var ConcurrentCallsAggregateError = class extends AggregateError {
36
+ errors = [];
37
+ total = 0;
38
+ failed = 0;
39
+ constructor(errors, total, failed) {
40
+ const messages = errors.map(
41
+ (error) => `- ${truncateString(error.message, 100)}`
42
+ );
43
+ const message = formatErrorMessagesWithCounts(messages, failed, total);
44
+ super(errors, message);
45
+ this.errors = errors;
46
+ this.total = total;
47
+ this.failed = failed;
48
+ }
49
+ };
50
+ var ConcurrentCallsWithMetadataAggregateError = class extends AggregateError {
51
+ errors = [];
52
+ errorsWithMetadata = [];
53
+ total = 0;
54
+ failed = 0;
55
+ constructor(errors, total, failed) {
56
+ const messages = errors.map(({ error, metadata }) => {
57
+ let metadataPrefix = typeof metadata === "string" || typeof metadata === "number" || typeof metadata === "boolean" ? `${metadata}: ` : "";
58
+ try {
59
+ const isArray = Array.isArray(metadata);
60
+ if (isObject(metadata) || isArray) {
61
+ const stringifiedMetadata = JSON.stringify(metadata);
62
+ if (stringifiedMetadata.length < 30) {
63
+ metadataPrefix = `${stringifiedMetadata}: `;
64
+ } else {
65
+ metadataPrefix = `[${isArray ? "Array" : "Object"} metadata]: `;
66
+ }
67
+ }
68
+ } catch (_) {
69
+ }
70
+ return `- ${metadataPrefix}${truncateString(error.message, 100)}`;
71
+ });
72
+ const message = formatErrorMessagesWithCounts(messages, failed, total);
73
+ const errorInstances = errors.map((e) => e.error);
74
+ super(errorInstances, message);
75
+ this.errors = errorInstances;
76
+ this.errorsWithMetadata = errors;
77
+ this.total = total;
78
+ this.failed = failed;
79
+ }
80
+ };
19
81
  var ConcurrentCalls = class {
20
82
  #pendingCalls = [];
21
83
  #alreadyRun = false;
@@ -92,7 +154,7 @@ var ConcurrentCalls = class {
92
154
  }
93
155
  }
94
156
  const allFailed = failed.length === total;
95
- const aggregatedError = failed.length > 0 ? new AggregateError(failed, "One or more calls failed") : null;
157
+ const aggregatedError = failed.length > 0 ? new ConcurrentCallsAggregateError(failed, total, failed.length) : null;
96
158
  this.#pendingCalls = [];
97
159
  return {
98
160
  succeeded,
@@ -218,13 +280,10 @@ var ConcurrentCallsWithMetadata = class {
218
280
  }
219
281
  }
220
282
  const allFailed = failedProcessing.length === total;
221
- const aggregatedError = failedProcessing.length > 0 ? new AggregateError(
222
- failedProcessing.map((f) => f.error),
223
- `${failedProcessing.length}/${total} calls failed: ${truncateArray(
224
- failedProcessing.map((f) => truncateString(f.error.message, 20)),
225
- 5,
226
- "..."
227
- ).join("; ")}`
283
+ const aggregatedError = failedProcessing.length > 0 ? new ConcurrentCallsWithMetadataAggregateError(
284
+ failedProcessing,
285
+ total,
286
+ failedProcessing.length
228
287
  ) : null;
229
288
  return {
230
289
  succeeded: succeededProcessing,
@@ -241,6 +300,8 @@ function concurrentCallsWithMetadata() {
241
300
  return new ConcurrentCallsWithMetadata();
242
301
  }
243
302
  export {
303
+ ConcurrentCallsAggregateError,
304
+ ConcurrentCallsWithMetadataAggregateError,
244
305
  concurrentCalls,
245
306
  concurrentCallsWithMetadata
246
307
  };
@@ -20,9 +20,11 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
20
20
  // src/retryOnError.ts
21
21
  var retryOnError_exports = {};
22
22
  __export(retryOnError_exports, {
23
- retryOnError: () => retryOnError
23
+ retryOnError: () => retryOnError,
24
+ retryResultOnError: () => retryResultOnError
24
25
  });
25
26
  module.exports = __toCommonJS(retryOnError_exports);
27
+ var import_t_result = require("t-result");
26
28
 
27
29
  // src/sleep.ts
28
30
  function sleep(ms) {
@@ -31,7 +33,7 @@ function sleep(ms) {
31
33
 
32
34
  // src/retryOnError.ts
33
35
  async function retryOnError(fn, maxRetries, options = {}, retry = 0, originalMaxRetries = maxRetries) {
34
- const { delayBetweenRetriesMs, retryCondition } = options;
36
+ const { delayBetweenRetriesMs, retryCondition, disableRetries } = options;
35
37
  if (options.debugId) {
36
38
  if (retry > 0) {
37
39
  console.info(
@@ -43,9 +45,12 @@ async function retryOnError(fn, maxRetries, options = {}, retry = 0, originalMax
43
45
  try {
44
46
  return await fn({ retry });
45
47
  } catch (error) {
46
- if (maxRetries > 0) {
48
+ if (maxRetries > 0 && !disableRetries) {
47
49
  const errorDuration = Date.now() - startTime;
48
- const shouldRetry = retryCondition ? retryCondition(error, { duration: errorDuration, retry }) : true;
50
+ const shouldRetry = retryCondition ? retryCondition((0, import_t_result.unknownToError)(error), {
51
+ duration: errorDuration,
52
+ retry
53
+ }) : true;
49
54
  if (!shouldRetry) {
50
55
  throw error;
51
56
  }
@@ -66,7 +71,47 @@ async function retryOnError(fn, maxRetries, options = {}, retry = 0, originalMax
66
71
  }
67
72
  }
68
73
  }
74
+ async function retryResultOnError(fn, maxRetries, options = {}, __retry = 0, __originalMaxRetries = maxRetries) {
75
+ const { delayBetweenRetriesMs, retryCondition } = options;
76
+ if (options.debugId) {
77
+ if (__retry > 0) {
78
+ console.info(
79
+ `Retrying ${options.debugId} (retry ${__retry}/${__originalMaxRetries}) after error`
80
+ );
81
+ }
82
+ }
83
+ const startTime = Date.now();
84
+ const result = await fn({ retry: __retry });
85
+ if (result.ok) {
86
+ return result;
87
+ }
88
+ if (maxRetries > 0 && !options.disableRetries) {
89
+ const errorDuration = Date.now() - startTime;
90
+ const shouldRetry = retryCondition ? retryCondition(result.error, {
91
+ duration: errorDuration,
92
+ retry: __retry
93
+ }) : true;
94
+ if (!shouldRetry) {
95
+ return result;
96
+ }
97
+ if (delayBetweenRetriesMs) {
98
+ await sleep(
99
+ typeof delayBetweenRetriesMs === "function" ? delayBetweenRetriesMs(__retry) : delayBetweenRetriesMs
100
+ );
101
+ }
102
+ return retryResultOnError(
103
+ fn,
104
+ maxRetries - 1,
105
+ options,
106
+ __retry + 1,
107
+ __originalMaxRetries
108
+ );
109
+ } else {
110
+ return result;
111
+ }
112
+ }
69
113
  // Annotate the CommonJS export names for ESM import in node:
70
114
  0 && (module.exports = {
71
- retryOnError
115
+ retryOnError,
116
+ retryResultOnError
72
117
  });
@@ -1,3 +1,5 @@
1
+ import { ResultValidErrors, Result } from 't-result';
2
+
1
3
  /**
2
4
  * Configuration options for retryOnError function.
3
5
  */
@@ -5,12 +7,14 @@ type RetryOptions = {
5
7
  /** Delay between retries in milliseconds or function returning delay */
6
8
  delayBetweenRetriesMs?: number | ((retry: number) => number);
7
9
  /** Function to determine if retry should happen, receives error and duration of last attempt */
8
- retryCondition?: (error: unknown, lastAttempt: {
10
+ retryCondition?: (error: Error, lastAttempt: {
9
11
  duration: number;
10
12
  retry: number;
11
13
  }) => boolean;
12
14
  /** Optional ID for debug logging */
13
15
  debugId?: string;
16
+ /** Disable retries */
17
+ disableRetries?: boolean;
14
18
  };
15
19
  /**
16
20
  * Retries a function on error with configurable retry logic.
@@ -18,8 +22,8 @@ type RetryOptions = {
18
22
  * @param fn - Function to retry that receives context with retry count
19
23
  * @param maxRetries - Maximum number of retries
20
24
  * @param options - Configuration options
21
- * @param retry
22
- * @param originalMaxRetries
25
+ * @param retry - internal use only
26
+ * @param originalMaxRetries - internal use only
23
27
  * @returns Promise resolving to the function result or rejecting with the final error
24
28
  *
25
29
  * @example
@@ -36,5 +40,31 @@ declare function retryOnError<T>(fn: (ctx: {
36
40
  /** Current retry count, (0 for first attempt) */
37
41
  retry: number;
38
42
  }) => Promise<T>, maxRetries: number, options?: RetryOptions, retry?: number, originalMaxRetries?: number): Promise<T>;
43
+ /**
44
+ * Retries a result function on error with configurable retry logic.
45
+ *
46
+ * @param fn - Function to retry that receives context with retry count
47
+ * @param maxRetries - Maximum number of retries
48
+ * @param options - Configuration options
49
+ * @param options.delayBetweenRetriesMs
50
+ * @param options.retryCondition
51
+ * @param options.debugId
52
+ * @param options.disableRetries
53
+ * @param __retry - internal use only
54
+ * @param __originalMaxRetries - internal use only
55
+ * @returns Promise resolving to the function result or rejecting with the final error
56
+ */
57
+ declare function retryResultOnError<T, E extends ResultValidErrors>(fn: (ctx: {
58
+ /** Current retry count, (0 for first attempt) */
59
+ retry: number;
60
+ }) => Promise<Result<T, E>>, maxRetries: number, options?: {
61
+ delayBetweenRetriesMs?: number | ((retry: number) => number);
62
+ retryCondition?: (error: E, lastAttempt: {
63
+ duration: number;
64
+ retry: number;
65
+ }) => boolean;
66
+ debugId?: string;
67
+ disableRetries?: boolean;
68
+ }, __retry?: number, __originalMaxRetries?: number): Promise<Result<T, E>>;
39
69
 
40
- export { retryOnError };
70
+ export { retryOnError, retryResultOnError };
@@ -1,3 +1,5 @@
1
+ import { ResultValidErrors, Result } from 't-result';
2
+
1
3
  /**
2
4
  * Configuration options for retryOnError function.
3
5
  */
@@ -5,12 +7,14 @@ type RetryOptions = {
5
7
  /** Delay between retries in milliseconds or function returning delay */
6
8
  delayBetweenRetriesMs?: number | ((retry: number) => number);
7
9
  /** Function to determine if retry should happen, receives error and duration of last attempt */
8
- retryCondition?: (error: unknown, lastAttempt: {
10
+ retryCondition?: (error: Error, lastAttempt: {
9
11
  duration: number;
10
12
  retry: number;
11
13
  }) => boolean;
12
14
  /** Optional ID for debug logging */
13
15
  debugId?: string;
16
+ /** Disable retries */
17
+ disableRetries?: boolean;
14
18
  };
15
19
  /**
16
20
  * Retries a function on error with configurable retry logic.
@@ -18,8 +22,8 @@ type RetryOptions = {
18
22
  * @param fn - Function to retry that receives context with retry count
19
23
  * @param maxRetries - Maximum number of retries
20
24
  * @param options - Configuration options
21
- * @param retry
22
- * @param originalMaxRetries
25
+ * @param retry - internal use only
26
+ * @param originalMaxRetries - internal use only
23
27
  * @returns Promise resolving to the function result or rejecting with the final error
24
28
  *
25
29
  * @example
@@ -36,5 +40,31 @@ declare function retryOnError<T>(fn: (ctx: {
36
40
  /** Current retry count, (0 for first attempt) */
37
41
  retry: number;
38
42
  }) => Promise<T>, maxRetries: number, options?: RetryOptions, retry?: number, originalMaxRetries?: number): Promise<T>;
43
+ /**
44
+ * Retries a result function on error with configurable retry logic.
45
+ *
46
+ * @param fn - Function to retry that receives context with retry count
47
+ * @param maxRetries - Maximum number of retries
48
+ * @param options - Configuration options
49
+ * @param options.delayBetweenRetriesMs
50
+ * @param options.retryCondition
51
+ * @param options.debugId
52
+ * @param options.disableRetries
53
+ * @param __retry - internal use only
54
+ * @param __originalMaxRetries - internal use only
55
+ * @returns Promise resolving to the function result or rejecting with the final error
56
+ */
57
+ declare function retryResultOnError<T, E extends ResultValidErrors>(fn: (ctx: {
58
+ /** Current retry count, (0 for first attempt) */
59
+ retry: number;
60
+ }) => Promise<Result<T, E>>, maxRetries: number, options?: {
61
+ delayBetweenRetriesMs?: number | ((retry: number) => number);
62
+ retryCondition?: (error: E, lastAttempt: {
63
+ duration: number;
64
+ retry: number;
65
+ }) => boolean;
66
+ debugId?: string;
67
+ disableRetries?: boolean;
68
+ }, __retry?: number, __originalMaxRetries?: number): Promise<Result<T, E>>;
39
69
 
40
- export { retryOnError };
70
+ export { retryOnError, retryResultOnError };
@@ -3,8 +3,9 @@ import {
3
3
  } from "./chunk-5DZT3Z5Z.js";
4
4
 
5
5
  // src/retryOnError.ts
6
+ import { unknownToError } from "t-result";
6
7
  async function retryOnError(fn, maxRetries, options = {}, retry = 0, originalMaxRetries = maxRetries) {
7
- const { delayBetweenRetriesMs, retryCondition } = options;
8
+ const { delayBetweenRetriesMs, retryCondition, disableRetries } = options;
8
9
  if (options.debugId) {
9
10
  if (retry > 0) {
10
11
  console.info(
@@ -16,9 +17,12 @@ async function retryOnError(fn, maxRetries, options = {}, retry = 0, originalMax
16
17
  try {
17
18
  return await fn({ retry });
18
19
  } catch (error) {
19
- if (maxRetries > 0) {
20
+ if (maxRetries > 0 && !disableRetries) {
20
21
  const errorDuration = Date.now() - startTime;
21
- const shouldRetry = retryCondition ? retryCondition(error, { duration: errorDuration, retry }) : true;
22
+ const shouldRetry = retryCondition ? retryCondition(unknownToError(error), {
23
+ duration: errorDuration,
24
+ retry
25
+ }) : true;
22
26
  if (!shouldRetry) {
23
27
  throw error;
24
28
  }
@@ -39,6 +43,46 @@ async function retryOnError(fn, maxRetries, options = {}, retry = 0, originalMax
39
43
  }
40
44
  }
41
45
  }
46
+ async function retryResultOnError(fn, maxRetries, options = {}, __retry = 0, __originalMaxRetries = maxRetries) {
47
+ const { delayBetweenRetriesMs, retryCondition } = options;
48
+ if (options.debugId) {
49
+ if (__retry > 0) {
50
+ console.info(
51
+ `Retrying ${options.debugId} (retry ${__retry}/${__originalMaxRetries}) after error`
52
+ );
53
+ }
54
+ }
55
+ const startTime = Date.now();
56
+ const result = await fn({ retry: __retry });
57
+ if (result.ok) {
58
+ return result;
59
+ }
60
+ if (maxRetries > 0 && !options.disableRetries) {
61
+ const errorDuration = Date.now() - startTime;
62
+ const shouldRetry = retryCondition ? retryCondition(result.error, {
63
+ duration: errorDuration,
64
+ retry: __retry
65
+ }) : true;
66
+ if (!shouldRetry) {
67
+ return result;
68
+ }
69
+ if (delayBetweenRetriesMs) {
70
+ await sleep(
71
+ typeof delayBetweenRetriesMs === "function" ? delayBetweenRetriesMs(__retry) : delayBetweenRetriesMs
72
+ );
73
+ }
74
+ return retryResultOnError(
75
+ fn,
76
+ maxRetries - 1,
77
+ options,
78
+ __retry + 1,
79
+ __originalMaxRetries
80
+ );
81
+ } else {
82
+ return result;
83
+ }
84
+ }
42
85
  export {
43
- retryOnError
86
+ retryOnError,
87
+ retryResultOnError
44
88
  };
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.27.2",
4
+ "version": "3.29.0",
5
5
  "license": "MIT",
6
6
  "files": [
7
7
  "lib",