@contrail/util 1.1.1-0 → 1.1.1-1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  export interface RetryConfig {
2
2
  maxRetries?: number;
3
3
  initialDelay?: number;
4
- onRetry?: (error: Error, attempt: number) => void;
4
+ onRetryCallback?: (error: Error, attempt: number) => Promise<void> | void;
5
5
  }
6
6
  export declare function retry<T>(fn: () => Promise<T>, config?: RetryConfig): Promise<T>;
@@ -16,7 +16,7 @@ const DEFAULT_CONFIG = {
16
16
  };
17
17
  function retry(fn, config = DEFAULT_CONFIG) {
18
18
  return __awaiter(this, void 0, void 0, function* () {
19
- const { maxRetries = 3, initialDelay = 1000, onRetry } = config;
19
+ const { maxRetries, initialDelay, onRetryCallback } = Object.assign(Object.assign({}, DEFAULT_CONFIG), config);
20
20
  if (maxRetries < 1) {
21
21
  throw new Error('maxRetries must be at least 1');
22
22
  }
@@ -27,15 +27,13 @@ function retry(fn, config = DEFAULT_CONFIG) {
27
27
  }
28
28
  catch (error) {
29
29
  attempt++;
30
- if (onRetry)
31
- onRetry(error, attempt);
32
30
  if (attempt === maxRetries) {
33
- console.error(`Max retries reached for function: ${fn.name}`);
34
- throw new Error(`Max retries reached: ${error.message}`);
31
+ throw error;
35
32
  }
36
- const delay = initialDelay * attempt;
37
- console.log(`Attempt ${attempt} failed, retrying in ${delay}ms...`);
38
- yield new Promise((resolve) => setTimeout(resolve, delay));
33
+ if (onRetryCallback) {
34
+ yield onRetryCallback(error, attempt);
35
+ }
36
+ yield new Promise((resolve) => setTimeout(resolve, initialDelay * attempt));
39
37
  }
40
38
  }
41
39
  throw new Error('Max retries reached');
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@contrail/util",
3
- "version": "1.1.1-0",
3
+ "version": "1.1.1-1",
4
4
  "description": "General javascript utilities",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",