@wowistudio/grit 0.0.2 → 0.0.3

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.
package/README.md CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  > ⚠️ **Work in Progress**: This library is currently under active development. The API may change and some features may be incomplete.
4
4
 
5
- A fluent API for configurable retries with error filtering and backoff strategies (fixed, exponential, random jitter)
5
+ Grit is a library for retries with error filtering and backoff strategies (fixed, exponential, random jitter)
6
6
 
7
7
  ## Installation
8
8
 
@@ -87,7 +87,7 @@ await Grit.retry(3)
87
87
 
88
88
  ### Timeout
89
89
 
90
- > ⚠️ **Note**: JavaScript timeouts don't actually stop the execution of the underlying operation. The timeout will reject/throw an error after the specified duration, but the original operation may continue running in the background.
90
+ > ⚠️ **Note**: the execution of operation is not stopped after timeout. The process will stop waiting for the code execution finish after the specified duration, but the original operation may continue running in the background.
91
91
 
92
92
  ```typescript
93
93
  import { Grit } from '@wowistudio/grit';
@@ -96,6 +96,19 @@ import { Grit } from '@wowistudio/grit';
96
96
  await Grit.retry(3)
97
97
  .withTimeout(5000)
98
98
  .attempt(apiCall);
99
+
100
+ // Custom timeout error message
101
+ await Grit.retry(3)
102
+ .withTimeout({ timeout: 5000, message: 'Request timed out after 5 seconds' })
103
+ .attempt(apiCall);
104
+
105
+ // With abort controller signal
106
+ const controller = new AbortController();
107
+ Grit.retry(3)
108
+ .withTimeout({ timeout: 5000, signal: controller.signal })
109
+ .attempt(apiCall);
110
+
111
+ controller.abort() // abort somewhere else to timeout immediately and not retry
99
112
  ```
100
113
 
101
114
  ### Fallback
@@ -121,13 +134,23 @@ const result = await Grit.retry(0)
121
134
  })
122
135
  .attempt(fastCacheLookup);
123
136
 
124
- // Attempt return type is typesafe and will be a union of return type .fallback & .attempt
125
- const result: number | string = await Grit.retry(0)
137
+ // Attempt return type is typesafe when adding a fallback
138
+ const result = await Grit.retry(0) // will be of type (number | string)
126
139
  .withTimeout(100)
127
140
  .withFallback(() => 1)
128
141
  .attempt(() => 'a')
129
142
  ```
143
+ ### Safe Attempt
144
+
145
+ ```typescript
146
+ // Attempt and return result & error
147
+ const answer = await Grit.retry(1)
148
+ .safeAttempt(() => {
149
+ throw new Error('whoops')
150
+ });
130
151
 
152
+ console.log(answer) // { result: null, error: Error('whoops') }
153
+ ```
131
154
 
132
155
  ## Reusable Builder Instances
133
156
 
@@ -141,9 +164,6 @@ const grit = Grit.retry(3)
141
164
  .withDelay(2000)
142
165
  .withTimeout(5000)
143
166
  .onlyErrors([NetworkError])
144
- .beforeRetry((retryCount) => {
145
- console.log(`Retrying (attempt ${retryCount})...`);
146
- });
147
167
 
148
168
  // Use the same builder for multiple different operations
149
169
  const userData = await grit.attempt(fetchUserData);
package/dist/builder.d.ts CHANGED
@@ -9,7 +9,6 @@ declare class GritBuilder<FallbackType = never> {
9
9
  $delay: DelayConfig | undefined;
10
10
  $timeout: TimeoutConfig | undefined;
11
11
  $fallback: FallbackFunction<FallbackType> | undefined;
12
- beforeRetryFn?: (retryCount: number) => void;
13
12
  logging: boolean;
14
13
  constructor(retryCount: number);
15
14
  onlyErrors(errors: (typeof GritError)[]): this;
@@ -17,8 +16,12 @@ declare class GritBuilder<FallbackType = never> {
17
16
  withLogging(enabled?: boolean): this;
18
17
  withDelay(config: DelayConfig): this;
19
18
  withTimeout(timeout: TimeoutConfig): this;
20
- beforeRetry(fn: (retryCount: number) => void): this;
21
19
  withFallback<T>(fallback: FallbackFunction<T>): GritBuilder<T>;
22
20
  attempt<T = FallbackType>(fn: FunctionToExecute<T>): Promise<FallbackType extends never ? T : T | FallbackType>;
21
+ safeAttempt<T = FallbackType>(fn: FunctionToExecute<T>): Promise<{
22
+ result: (FallbackType extends never ? T : T | FallbackType) | null;
23
+ error: unknown | null;
24
+ }>;
23
25
  }
24
26
  export { GritBuilder };
27
+ //# sourceMappingURL=builder.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"builder.d.ts","sourceRoot":"","sources":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AAM,EAAE,CAAC,YAAY,SAAS,KAAK,GAAG,CAAC,GAAG,CAAC,GAAG,YAAY,CAAC,GAAG,IAAI,CAAC;QAAC,KAAK,EAAE,OAAO,GAAG,IAAI,CAAA;KAAE,CAAC;CAK5G;AAED,OAAO,EAAE,WAAW,EAAE,CAAC"}
package/dist/builder.js CHANGED
@@ -34,10 +34,6 @@ class GritBuilder {
34
34
  this.$timeout = timeout;
35
35
  return this;
36
36
  }
37
- beforeRetry(fn) {
38
- this.beforeRetryFn = fn;
39
- return this;
40
- }
41
37
  withFallback(fallback) {
42
38
  const newBuilder = this;
43
39
  newBuilder.$fallback = fallback;
@@ -46,6 +42,9 @@ class GritBuilder {
46
42
  attempt(fn) {
47
43
  return __classPrivateFieldGet(this, _GritBuilder_instances, "m", _GritBuilder_build).call(this).attempt(fn);
48
44
  }
45
+ safeAttempt(fn) {
46
+ return __classPrivateFieldGet(this, _GritBuilder_instances, "m", _GritBuilder_build).call(this).safeAttempt(fn);
47
+ }
49
48
  }
50
49
  _GritBuilder_instances = new WeakSet(), _GritBuilder_build = function _GritBuilder_build() {
51
50
  return new Grit({
@@ -54,9 +53,9 @@ _GritBuilder_instances = new WeakSet(), _GritBuilder_build = function _GritBuild
54
53
  skipErrors: this._skipErrors,
55
54
  delay: this.$delay,
56
55
  timeout: this.$timeout,
57
- beforeRetryFn: this.beforeRetryFn,
58
56
  logging: this.logging,
59
57
  fallback: this.$fallback,
60
58
  });
61
59
  };
62
60
  export { GritBuilder };
61
+ //# sourceMappingURL=builder.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"builder.js","sourceRoot":"","sources":["../src/builder.ts"],"names":[],"mappings":";;;;;;AACA,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AAGjC,MAAM,WAAW;IAWb,YAAY,UAAkB;;QAT9B,gBAAW,GAAyB,EAAE,CAAC;QACvC,gBAAW,GAAyB,EAAE,CAAC;QACvC,OAAE,GAAuC,SAAS,CAAC;QAInD,YAAO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AAI,CAAC,OAAO;QACrB,QAAQ,EAAE,IAAI,CAAC,SAAS;KAC3B,CAAC,CAAC;AACP,CAAC;AAyBL,OAAO,EAAE,WAAW,EAAE,CAAC"}
@@ -9,3 +9,4 @@ export declare class TimeoutError extends Error {
9
9
  constructor(message: string);
10
10
  }
11
11
  export { GritError, MaxRetriesError };
12
+ //# sourceMappingURL=exceptions.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"exceptions.d.ts","sourceRoot":"","sources":["../src/exceptions.ts"],"names":[],"mappings":"AAAA,cAAM,SAAU,SAAQ,KAAK;gBACb,OAAO,EAAE,MAAM;CAI9B;AAED,cAAM,eAAgB,SAAQ,KAAK;gBACnB,OAAO,EAAE,MAAM;CAI9B;AAED,qBAAa,YAAa,SAAQ,KAAK;IACnC,IAAI,SAAkB;gBAEV,OAAO,EAAE,MAAM;CAI9B;AAGD,OAAO,EAAE,SAAS,EAAE,eAAe,EAAE,CAAC"}
@@ -18,3 +18,4 @@ export class TimeoutError extends Error {
18
18
  }
19
19
  }
20
20
  export { GritError, MaxRetriesError };
21
+ //# sourceMappingURL=exceptions.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"exceptions.js","sourceRoot":"","sources":["../src/exceptions.ts"],"names":[],"mappings":"AAAA,MAAM,SAAU,SAAQ,KAAK;IACzB,YAAY,OAAe;QACvB,KAAK,CAAC,OAAO,CAAC,CAAC;QACf,IAAI,CAAC,IAAI,GAAG,WAAW,CAAC;IAC5B,CAAC;CACJ;AAED,MAAM,eAAgB,SAAQ,KAAK;IAC/B,YAAY,OAAe;QACvB,KAAK,CAAC,OAAO,CAAC,CAAC;QACf,IAAI,CAAC,IAAI,GAAG,iBAAiB,CAAC;IAClC,CAAC;CACJ;AAED,MAAM,OAAO,YAAa,SAAQ,KAAK;IAGnC,YAAY,OAAe;QACvB,KAAK,CAAC,OAAO,CAAC,CAAC;QAHnB,SAAI,GAAG,cAAc,CAAC;QAIlB,IAAI,CAAC,IAAI,GAAG,cAAc,CAAC;IAC/B,CAAC;CACJ;AAGD,OAAO,EAAE,SAAS,EAAE,eAAe,EAAE,CAAC"}
package/dist/grit.d.ts CHANGED
@@ -1,19 +1,23 @@
1
1
  import { GritBuilder } from "./builder.js";
2
- import type { FunctionToExecute, GritProps } from "./types.js";
2
+ import type { DelayConfig, FunctionToExecute, GritProps, TimeoutConfig } from "./types.js";
3
3
  declare class Grit<T> {
4
4
  #private;
5
- private retryCount?;
5
+ readonly retryCount?: number;
6
6
  private attempts;
7
7
  private retries;
8
8
  private onlyErrors;
9
9
  private skipErrors;
10
- private delay;
10
+ readonly delay: DelayConfig | undefined;
11
11
  private fallback;
12
- private timeout;
13
- private beforeRetryFn?;
12
+ readonly timeout: TimeoutConfig | undefined;
14
13
  private logging;
15
14
  constructor(props: GritProps);
16
15
  attempt(fn: FunctionToExecute<T>): Promise<T>;
16
+ safeAttempt(fn: FunctionToExecute<T>): Promise<{
17
+ result: T | null;
18
+ error: unknown | null;
19
+ }>;
17
20
  static retry(retryCount: number): GritBuilder<never>;
18
21
  }
19
22
  export { Grit };
23
+ //# sourceMappingURL=grit.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"grit.d.ts","sourceRoot":"","sources":["../src/grit.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,cAAc,CAAC;AAE3C,OAAO,KAAK,EAAE,WAAW,EAAoB,iBAAiB,EAAE,SAAS,EAAE,aAAa,EAAE,MAAM,YAAY,CAAC;AAM7G,cAAM,IAAI,CAAC,CAAC;;IACR,QAAQ,CAAC,UAAU,CAAC,EAAE,MAAM,CAAC;IAC7B,OAAO,CAAC,QAAQ,CAAa;IAC7B,OAAO,CAAC,OAAO,CAAa;IAC5B,OAAO,CAAC,UAAU,CAAmB;IACrC,OAAO,CAAC,UAAU,CAAmB;IACrC,QAAQ,CAAC,KAAK,EAAE,WAAW,GAAG,SAAS,CAAC;IACxC,OAAO,CAAC,QAAQ,CAAkC;IAClD,QAAQ,CAAC,OAAO,EAAE,aAAa,GAAG,SAAS,CAAC;IAC5C,OAAO,CAAC,OAAO,CAAkB;gBAGrB,KAAK,EAAE,SAAS;IA0GtB,OAAO,CAAC,EAAE,EAAE,iBAAiB,CAAC,CAAC,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC;IAI7C,WAAW,CAAC,EAAE,EAAE,iBAAiB,CAAC,CAAC,CAAC,GAAG,OAAO,CAAC;QAAE,MAAM,EAAE,CAAC,GAAG,IAAI,CAAC;QAAC,KAAK,EAAE,OAAO,GAAG,IAAI,CAAA;KAAE,CAAC;IASjG,MAAM,CAAC,KAAK,CAAC,UAAU,EAAE,MAAM;CAGlC;AAED,OAAO,EAAE,IAAI,EAAE,CAAC"}
package/dist/grit.js CHANGED
@@ -1,42 +1,40 @@
1
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
- return new (P || (P = Promise))(function (resolve, reject) {
4
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
- step((generator = generator.apply(thisArg, _arguments || [])).next());
8
- });
9
- };
10
1
  var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
11
2
  if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
12
3
  if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
13
4
  return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
14
5
  };
15
- var _Grit_instances, _Grit_currentDelay_get, _Grit_handleBeforeRetry, _Grit_backoff, _Grit_withTimeout, _Grit_processError, _Grit_execute;
6
+ var _Grit_instances, _Grit_currentDelay_get, _Grit_backoff, _Grit_withTimeout, _Grit_execute;
16
7
  import { GritBuilder } from "./builder.js";
17
8
  import { GritError, TimeoutError } from "./exceptions.js";
18
- import { isObject, validateGritConfig } from "./utils.js";
9
+ import { getTimeoutConfig, isObject, validateGritConfig } from "./utils.js";
10
+ const getAbortedReason = (signal) => signal.reason ?? new DOMException('This operation was aborted.', 'AbortError');
19
11
  class Grit {
20
12
  constructor(props) {
21
13
  _Grit_instances.add(this);
22
14
  this.attempts = 1;
23
15
  this.retries = 0;
24
16
  this.logging = false;
25
- const { retryCount, fn, onlyErrors, skipErrors, delay, timeout, beforeRetryFn, logging, fallback } = props;
17
+ const { retryCount, onlyErrors, skipErrors, delay, timeout, logging, fallback } = props;
26
18
  this.retryCount = retryCount;
27
19
  this.onlyErrors = onlyErrors || [];
28
20
  this.skipErrors = skipErrors || [];
29
21
  this.delay = delay;
30
22
  this.fallback = fallback;
31
23
  this.timeout = timeout;
32
- this.beforeRetryFn = beforeRetryFn;
33
24
  this.logging = logging || false;
34
- validateGritConfig(delay, retryCount);
25
+ validateGritConfig(this);
35
26
  }
36
- attempt(fn) {
37
- return __awaiter(this, void 0, void 0, function* () {
38
- return __classPrivateFieldGet(this, _Grit_instances, "m", _Grit_execute).call(this, fn);
39
- });
27
+ async attempt(fn) {
28
+ return __classPrivateFieldGet(this, _Grit_instances, "m", _Grit_execute).call(this, fn);
29
+ }
30
+ async safeAttempt(fn) {
31
+ try {
32
+ const result = await __classPrivateFieldGet(this, _Grit_instances, "m", _Grit_execute).call(this, fn);
33
+ return { result, error: null };
34
+ }
35
+ catch (error) {
36
+ return { error, result: null };
37
+ }
40
38
  }
41
39
  static retry(retryCount) {
42
40
  return new GritBuilder(retryCount);
@@ -64,64 +62,59 @@ _Grit_instances = new WeakSet(), _Grit_currentDelay_get = function _Grit_current
64
62
  if (this.logging)
65
63
  console.debug("Delaying for", delay, "ms");
66
64
  return delay;
67
- }, _Grit_handleBeforeRetry = function _Grit_handleBeforeRetry() {
68
- if (this.retries > 0 && this.beforeRetryFn)
69
- this.beforeRetryFn(this.retries);
70
- }, _Grit_backoff = function _Grit_backoff() {
71
- return __awaiter(this, void 0, void 0, function* () {
72
- return new Promise((resolve) => setTimeout(resolve, __classPrivateFieldGet(this, _Grit_instances, "a", _Grit_currentDelay_get)));
73
- });
74
- }, _Grit_withTimeout = function _Grit_withTimeout(promise) {
65
+ }, _Grit_backoff = async function _Grit_backoff() {
66
+ return new Promise((resolve) => setTimeout(resolve, __classPrivateFieldGet(this, _Grit_instances, "a", _Grit_currentDelay_get)));
67
+ }, _Grit_withTimeout = function _Grit_withTimeout(fn) {
75
68
  let timer = undefined;
76
- const wrappedPromise = new Promise((resolve, reject) => {
77
- // promise.then(resolve, (error) => {
78
- // clearTimeout(timer);
79
- // timer = undefined;
80
- // reject(error);
81
- // })
82
- promise.then(resolve, reject);
83
- if (this.timeout === Number.POSITIVE_INFINITY)
69
+ let abortHandler = () => { };
70
+ const { timeout, message, signal } = getTimeoutConfig(this.timeout);
71
+ const timeoutController = new AbortController();
72
+ let wrappedPromise = new Promise((resolve, reject) => {
73
+ if (signal?.aborted) {
74
+ reject(getAbortedReason(signal));
84
75
  return;
85
- const message = `Promise timed out after ${this.timeout} milliseconds`;
76
+ }
77
+ if (signal) {
78
+ abortHandler = () => {
79
+ reject(getAbortedReason(signal));
80
+ };
81
+ signal.addEventListener('abort', abortHandler, { once: true });
82
+ }
83
+ Promise.resolve(fn(this.attempts)).then(resolve, reject);
86
84
  const timeoutError = new TimeoutError(message);
87
- timer = setTimeout(() => {
88
- console.log('timeout joe joe joe');
89
- reject(timeoutError);
90
- }, this.timeout);
85
+ timeoutController.signal.addEventListener('abort', () => reject(timeoutError));
86
+ timer = setTimeout(() => timeoutController.abort(), timeout);
91
87
  });
92
- const cancelablePromise = wrappedPromise.finally(() => {
88
+ wrappedPromise = wrappedPromise.finally(() => {
93
89
  clearTimeout(timer);
94
90
  timer = undefined;
95
91
  });
96
- return cancelablePromise;
97
- }, _Grit_processError = function _Grit_processError(error) {
98
- if (error && typeof error === 'object' && 'constructor' in error) {
99
- if (this.skipErrors.length > 0 && this.skipErrors.includes(error.constructor))
100
- throw error;
101
- if (this.onlyErrors.length > 0 && !this.onlyErrors.includes(error.constructor))
102
- throw error;
92
+ wrappedPromise.abort = timeoutController.abort;
93
+ return wrappedPromise;
94
+ }, _Grit_execute = async function _Grit_execute(fn) {
95
+ try {
96
+ if (this.timeout)
97
+ return await __classPrivateFieldGet(this, _Grit_instances, "m", _Grit_withTimeout).call(this, fn);
98
+ return await fn(this.attempts);
103
99
  }
104
- this.attempts++;
105
- this.retries++;
106
- }, _Grit_execute = function _Grit_execute(fn) {
107
- return __awaiter(this, void 0, void 0, function* () {
108
- try {
109
- __classPrivateFieldGet(this, _Grit_instances, "m", _Grit_handleBeforeRetry).call(this);
110
- if (this.timeout)
111
- return yield __classPrivateFieldGet(this, _Grit_instances, "m", _Grit_withTimeout).call(this, Promise.resolve(fn(this.attempts)));
112
- return yield fn(this.attempts);
100
+ catch (error) {
101
+ if (this.retries >= this.retryCount) {
102
+ if (this.fallback)
103
+ return this.fallback(error, this.attempts);
104
+ throw error;
113
105
  }
114
- catch (error) {
115
- if (this.retries >= this.retryCount) {
116
- if (this.fallback)
117
- return this.fallback(error, this.attempts);
106
+ if (error && typeof error === 'object' && 'constructor' in error) {
107
+ if (this.skipErrors.length > 0 && this.skipErrors.includes(error.constructor))
108
+ throw error;
109
+ if (this.onlyErrors.length > 0 && !this.onlyErrors.includes(error.constructor))
118
110
  throw error;
119
- }
120
- __classPrivateFieldGet(this, _Grit_instances, "m", _Grit_processError).call(this, error);
121
- if (this.delay)
122
- return __classPrivateFieldGet(this, _Grit_instances, "m", _Grit_backoff).call(this).then(() => __classPrivateFieldGet(this, _Grit_instances, "m", _Grit_execute).call(this, fn));
123
- return yield __classPrivateFieldGet(this, _Grit_instances, "m", _Grit_execute).call(this, fn);
124
111
  }
125
- });
112
+ this.attempts++;
113
+ this.retries++;
114
+ if (this.delay)
115
+ return __classPrivateFieldGet(this, _Grit_instances, "m", _Grit_backoff).call(this).then(() => __classPrivateFieldGet(this, _Grit_instances, "m", _Grit_execute).call(this, fn));
116
+ return await __classPrivateFieldGet(this, _Grit_instances, "m", _Grit_execute).call(this, fn);
117
+ }
126
118
  };
127
119
  export { Grit };
120
+ //# sourceMappingURL=grit.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"grit.js","sourceRoot":"","sources":["../src/grit.ts"],"names":[],"mappings":";;;;;;AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,cAAc,CAAC;AAC3C,OAAO,EAAE,SAAS,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAC;AAE1D,OAAO,EAAE,gBAAgB,EAAE,QAAQ,EAAE,kBAAkB,EAAE,MAAM,YAAY,CAAC;AAE5E,MAAM,gBAAgB,GAAG,CAAC,MAAmB,EAAE,EAAE,CAAC,MAAM,CAAC,MAAM,IAAI,IAAI,YAAY,CAAC,6BAA6B,EAAE,YAAY,CAAC,CAAC;AAGjI,MAAM,IAAI;IAYN,YAAY,KAAgB;;QAVpB,aAAQ,GAAW,CAAC,CAAC;QACrB,YAAO,GAAW,CAAC,CAAC;QAMpB,YAAO,GAAY,KAAK,CAAC;QAI7B,MAAM,EAAE,UAAU,EAAE,UAAU,EAAE,UAAU,EAAE,KAAK,EAAE,OAAO,EAAE,OAAO,EAAE,QAAQ,EAAE,GAAG,KAAK,CAAC;QACxF,IAAI,CAAC,UAAU,GAAG,UAAU,CAAC;QAC7B,IAAI,CAAC,UAAU,GAAG,UAAU,IAAI,EAAE,CAAC;QACnC,IAAI,CAAC,UAAU,GAAG,UAAU,IAAI,EAAE,CAAC;QACnC,IAAI,CAAC,KAAK,GAAG,KAAK,CAAC;QACnB,IAAI,CAAC,QAAQ,GAAG,QAAQ,CAAC;QACzB,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC;QACvB,IAAI,CAAC,OAAO,GAAG,OAAO,IAAI,KAAK,CAAC;QAEhC,kBAAkB,CAAC,IAAI,CA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C;YACnC,IAAI,IAAI,CAAC,QAAQ;gBACb,OAAO,IAAI,CAAC,QAAQ,CAAC,KAAK,EAAE,IAAI,CAAC,QAAQ,CAAC,CAAC;YAC/C,MAAM,KAAK,CAAC;QAChB,CAAC;QAED,IAAI,KAAK,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,aAAa,IAAI,KAAK,EAAE,CAAC;YAC/D,IAAI,IAAI,CAAC,UAAU,CAAC,MAAM,GAAG,CAAC,IAAI,IAAI,CAAC,UAAU,CAAC,QAAQ,CAAC,KAAK,CAAC,WAA2B,CAAC;gBACzF,MAAM,KAAK,CAAC;YAEhB,IAAI,IAAI,CAAC,UAAU,CAAC,MAAM,GAAG,CAAC,IAAI,CAAC,IAAI,CAAC,UAAU,CAAC,QAAQ,CAAC,KAAK,CAAC,WAA2B,CAAC;gBAC1F,MAAM,KAAK,CAAC;QACpB,CAAC;QAED,IAAI,CAAC,QAAQ,EAAE,CAAC;QAChB,IAAI,CAAC,OAAO,EAAE,CAAC;QAEf,IAAI,IAAI,CAAC,KAAK;YACV,OAAO,uBAAA,IAAI,sCAAS,MAAb,IAAI,CAAW,CAAC,IAAI,CAAC,GAAG,EAAE,CAAC,uBAAA,IAAI,sCAAS,MAAb,IAAI,EAAU,EAAE,CAAC,CAAC,CAAC;QACzD,OAAO,MAAM,uBAAA,IAAI,sCAAS,MAAb,IAAI,EAAU,EAAE,CAAC,CAAC;IACnC,CAAC;AACL,CAAC;AAoBL,OAAO,EAAE,IAAI,EAAE,CAAC"}
package/dist/index.d.ts CHANGED
@@ -1,3 +1,4 @@
1
1
  export { GritBuilder } from "./builder.js";
2
2
  export { Grit } from "./grit.js";
3
3
  export type { DelayConfig, FunctionToExecute } from "./types.js";
4
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,cAAc,CAAC;AAC3C,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AACjC,YAAY,EAAE,WAAW,EAAE,iBAAiB,EAAE,MAAM,YAAY,CAAC"}
package/dist/index.js CHANGED
@@ -1,2 +1,3 @@
1
1
  export { GritBuilder } from "./builder.js";
2
2
  export { Grit } from "./grit.js";
3
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,cAAc,CAAC;AAC3C,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC"}
package/dist/types.d.ts CHANGED
@@ -6,10 +6,13 @@ export type GritProps = {
6
6
  skipErrors?: (typeof Error)[];
7
7
  delay?: DelayConfig;
8
8
  timeout?: TimeoutConfig;
9
- beforeRetryFn?: (retryCount: number) => void;
10
9
  logging?: boolean;
11
10
  };
12
- export type TimeoutConfig = number;
11
+ export type TimeoutConfig = number | {
12
+ timeout: number;
13
+ message?: string;
14
+ signal?: AbortSignal;
15
+ };
13
16
  export type DelayConfig = number | number[] | {
14
17
  factor?: number;
15
18
  delay: number;
@@ -19,4 +22,5 @@ export type DelayConfig = number | number[] | {
19
22
  maxDelay: number;
20
23
  };
21
24
  export type FunctionToExecute<T> = (attempts: number) => (Promise<T> | T);
22
- export type FallbackFunction<T> = (error: Error, attempts: number) => (Promise<T> | T);
25
+ export type FallbackFunction<T> = (error: unknown, attempts: number) => (Promise<T> | T);
26
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,SAAS,GAAG;IACpB,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,EAAE,CAAC,EAAE,iBAAiB,CAAC,GAAG,CAAC,CAAC;IAC5B,QAAQ,CAAC,EAAE,gBAAgB,CAAC,GAAG,CAAC,CAAC;IACjC,UAAU,CAAC,EAAE,CAAC,OAAO,KAAK,CAAC,EAAE,CAAC;IAC9B,UAAU,CAAC,EAAE,CAAC,OAAO,KAAK,CAAC,EAAE,CAAC;IAC9B,KAAK,CAAC,EAAE,WAAW,CAAC;IACpB,OAAO,CAAC,EAAE,aAAa,CAAC;IACxB,OAAO,CAAC,EAAE,OAAO,CAAC;CACrB,CAAA;AAED,MAAM,MAAM,aAAa,GACnB,MAAM,GACN;IAAE,OAAO,EAAE,MAAM,CAAC;IAAC,OAAO,CAAC,EAAE,MAAM,CAAC;IAAC,MAAM,CAAC,EAAE,WAAW,CAAC;CAAE,CAAA;AAElE,MAAM,MAAM,WAAW,GACjB,MAAM,GACN,MAAM,EAAE,GACR;IAAE,MAAM,CAAC,EAAE,MAAM,CAAC;IAAC,KAAK,EAAE,MAAM,CAAC;CAAE,GACnC;IACE,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,QAAQ,EAAE,MAAM,CAAC;IACjB,QAAQ,EAAE,MAAM,CAAC;CACpB,CAAA;AAEL,MAAM,MAAM,iBAAiB,CAAC,CAAC,IAAI,CAAC,QAAQ,EAAE,MAAM,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAA;AACzE,MAAM,MAAM,gBAAgB,CAAC,CAAC,IAAI,CAAC,KAAK,EAAE,OAAO,EAAE,QAAQ,EAAE,MAAM,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAA"}
package/dist/types.js CHANGED
@@ -1 +1,2 @@
1
1
  export {};
2
+ //# sourceMappingURL=types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":""}
package/dist/utils.d.ts CHANGED
@@ -1,4 +1,11 @@
1
- import type { DelayConfig } from "./types.js";
1
+ import { Grit } from "./grit.js";
2
+ import type { TimeoutConfig } from "./types.js";
2
3
  export declare const isObject: (o: any) => o is Record<string, any>;
3
4
  export declare const isPromise: <T>(value: any) => value is Promise<T>;
4
- export declare const validateGritConfig: (config: DelayConfig | undefined, retryCount: number | undefined) => void;
5
+ export declare const validateGritConfig: (grit: Grit<any>) => void;
6
+ export declare const getTimeoutConfig: (config: TimeoutConfig | undefined) => {
7
+ timeout: number;
8
+ message: string;
9
+ signal?: AbortSignal;
10
+ };
11
+ //# sourceMappingURL=utils.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AACjC,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,YAAY,CAAC;AAEhD,eAAO,MAAM,QAAQ,GAAI,GAAG,GAAG,KAAG,CAAC,IAAI,MAAM,CAAC,MAAM,EAAE,GAAG,CAExD,CAAA;AAED,eAAO,MAAM,SAAS,GAAI,CAAC,EAAE,OAAO,GAAG,KAAG,KAAK,IAAI,OAAO,CAAC,CAAC,CAE3D,CAAA;AAED,eAAO,MAAM,kBAAkB,GAAI,MAAM,IAAI,CAAC,GAAG,CAAC,SAuEjD,CAAA;AAED,eAAO,MAAM,gBAAgB,GAAI,QAAQ,aAAa,GAAG,SAAS,KAAG;IAAE,OAAO,EAAE,MAAM,CAAC;IAAC,OAAO,EAAE,MAAM,CAAC;IAAC,MAAM,CAAC,EAAE,WAAW,CAAC;CAgB7H,CAAA"}
package/dist/utils.js CHANGED
@@ -5,46 +5,85 @@ export const isObject = (o) => {
5
5
  export const isPromise = (value) => {
6
6
  return value !== null && typeof value === "object" && typeof value.then === "function";
7
7
  };
8
- export const validateGritConfig = (config, retryCount) => {
8
+ export const validateGritConfig = (grit) => {
9
+ const { retryCount, delay: delayConfig, timeout: timeoutConfig } = grit;
9
10
  if (retryCount !== 0 && !retryCount)
10
11
  throw new GritError("Missing retry config (Grit.retry(<count>))");
11
- if (typeof config === "number") {
12
- if (config < 0)
13
- throw new GritError("delay must be greater than 0");
14
- return;
15
- }
16
- if (!config)
17
- return;
18
- // validate array config
19
- if (Array.isArray(config)) {
20
- if (config.length !== retryCount)
21
- throw new GritError("Delay array length must be equal to retry count");
22
- for (const delay of config) {
23
- if (delay <= 0)
12
+ if (delayConfig) {
13
+ if (typeof delayConfig === "number") {
14
+ if (delayConfig < 0)
15
+ throw new GritError("delay must be greater than 0");
16
+ return;
17
+ }
18
+ // validate array config
19
+ if (Array.isArray(delayConfig)) {
20
+ if (delayConfig.length !== retryCount)
21
+ throw new GritError("Delay array length must be equal to retry count");
22
+ for (const delay of delayConfig) {
23
+ if (delay <= 0)
24
+ throw new GritError("delay must be greater than 0");
25
+ }
26
+ return;
27
+ }
28
+ if (!isObject(delayConfig))
29
+ throw new GritError("Invalid backoff config");
30
+ // validate delay config
31
+ if ("delay" in delayConfig) {
32
+ if (typeof delayConfig.delay !== "number")
33
+ throw new GritError("delay must be a number");
34
+ if (delayConfig.delay <= 0)
24
35
  throw new GritError("delay must be greater than 0");
36
+ return;
25
37
  }
26
- return;
38
+ // validate random delay config
39
+ const { minDelay, maxDelay } = delayConfig;
40
+ if (!minDelay || typeof minDelay !== "number")
41
+ throw new GritError("minDelay must be a number");
42
+ if (!maxDelay || typeof maxDelay !== "number")
43
+ throw new GritError("maxDelay must be a number");
44
+ if (minDelay >= maxDelay)
45
+ throw new GritError("minDelay must be less than maxDelay");
46
+ if (minDelay <= 0)
47
+ throw new GritError("minDelay must be greater than 0");
48
+ if (maxDelay <= 0)
49
+ throw new GritError("maxDelay must be greater than 0");
50
+ }
51
+ if (timeoutConfig) {
52
+ if (typeof timeoutConfig === "number") {
53
+ if (timeoutConfig <= 0)
54
+ throw new GritError("timeout must be greater than 0");
55
+ if (timeoutConfig === Number.POSITIVE_INFINITY)
56
+ throw new GritError("timeout cannot be infinity");
57
+ return;
58
+ }
59
+ if (!isObject(timeoutConfig))
60
+ throw new GritError("Invalid timeout config");
61
+ if ("timeout" in timeoutConfig) {
62
+ if (typeof timeoutConfig.timeout !== "number")
63
+ throw new GritError("timeout must be a number");
64
+ if (timeoutConfig.timeout === Number.POSITIVE_INFINITY)
65
+ throw new GritError("timeout cannot be infinity");
66
+ if (timeoutConfig.timeout <= 0)
67
+ throw new GritError("timeout must be greater than 0");
68
+ }
69
+ }
70
+ };
71
+ export const getTimeoutConfig = (config) => {
72
+ if (typeof config === "number") {
73
+ return {
74
+ timeout: config,
75
+ signal: undefined,
76
+ message: "Promise timed out after " + config + " milliseconds"
77
+ };
27
78
  }
28
- if (!isObject(config))
29
- throw new GritError("Invalid backoff config");
30
- // validate delay config
31
- if ("delay" in config) {
32
- if (typeof config.delay !== "number")
33
- throw new GritError("delay must be a number");
34
- if (config.delay <= 0)
35
- throw new GritError("delay must be greater than 0");
36
- return;
79
+ else if (typeof config === "object") {
80
+ const { timeout, signal, message } = config;
81
+ return {
82
+ timeout,
83
+ signal,
84
+ message: message || "Promise timed out after " + timeout + " milliseconds"
85
+ };
37
86
  }
38
- // validate random delay config
39
- const { minDelay, maxDelay } = config;
40
- if (!minDelay || typeof minDelay !== "number")
41
- throw new GritError("minDelay must be a number");
42
- if (!maxDelay || typeof maxDelay !== "number")
43
- throw new GritError("maxDelay must be a number");
44
- if (minDelay >= maxDelay)
45
- throw new GritError("minDelay must be less than maxDelay");
46
- if (minDelay <= 0)
47
- throw new GritError("minDelay must be greater than 0");
48
- if (maxDelay <= 0)
49
- throw new GritError("maxDelay must be greater than 0");
87
+ throw new GritError("Invalid timeout config");
50
88
  };
89
+ //# sourceMappingURL=utils.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"utils.js","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAI5C,MAAM,CAAC,MAAM,QAAQ,GAAG,CAAC,CAAM,EAA4B,EAAE;IACzD,OAAO,OAAO,CAAC,KAAK,QAAQ,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CA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IAAI,aAAa,CAAC,OAAO,KAAK,MAAM,CAAC,iBAAiB;gBAClD,MAAM,IAAI,SAAS,CAAC,4BAA4B,CAAC,CAAC;YACtD,IAAI,aAAa,CAAC,OAAO,IAAI,CAAC;gBAC1B,MAAM,IAAI,SAAS,CAAC,gCAAgC,CAAC,CAAC;QAC9D,CAAC;IACL,CAAC;AACL,CAAC,CAAA;AAED,MAAM,CAAC,MAAM,gBAAgB,GAAG,CAAC,MAAiC,EAA+D,EAAE;IAC/H,IAAI,OAAO,MAAM,KAAK,QAAQ,EAAE,CAAC;QAC7B,OAAO;YACH,OAAO,EAAE,MAAM;YACf,MAAM,EAAE,SAAS;YACjB,OAAO,EAAE,0BAA0B,GAAG,MAAM,GAAG,eAAe;SACjE,CAAC;IACN,CAAC;SAAM,IAAI,OAAO,MAAM,KAAK,QAAQ,EAAE,CAAC;QACpC,MAAM,EAAE,OAAO,EAAE,MAAM,EAAE,OAAO,EAAE,GAAG,MAAM,CAAC;QAC5C,OAAO;YACH,OAAO;YACP,MAAM;YACN,OAAO,EAAE,OAAO,IAAI,0BAA0B,GAAG,OAAO,GAAG,eAAe;SAC7E,CAAC;IACN,CAAC;IACD,MAAM,IAAI,SAAS,CAAC,wBAAwB,CAAC,CAAC;AAClD,CAAC,CAAA"}
package/package.json CHANGED
@@ -1,27 +1,30 @@
1
1
  {
2
2
  "name": "@wowistudio/grit",
3
3
  "description": "A library for retry.",
4
- "main": "dist/index.js",
5
- "types": "dist/index.d.ts",
6
- "files": [
7
- "/dist"
8
- ],
9
- "version": "0.0.2",
4
+ "version": "0.0.3",
10
5
  "license": "MIT",
6
+ "type": "module",
7
+ "main": "./dist/index.js",
8
+ "module": "./dist/index.js",
9
+ "types": "./dist/index.d.ts",
11
10
  "exports": {
12
11
  ".": {
13
- "types": "./src/index.ts",
14
- "default": "./src/index.ts"
12
+ "types": "./dist/index.d.ts",
13
+ "import": "./dist/index.js"
15
14
  }
16
15
  },
17
- "type": "module",
16
+ "files": [
17
+ "dist"
18
+ ],
18
19
  "scripts": {
20
+ "build": "tsc",
21
+ "login": "npm adduser",
22
+ "publish": "npm publish --access=public",
19
23
  "test": "vitest run",
20
24
  "test:watch": "vitest"
21
25
  },
22
- "dependencies": {},
23
26
  "devDependencies": {
24
- "vitest": "^2.1.8",
25
- "@types/node": "^20.9.0"
27
+ "@types/node": "^20.9.0",
28
+ "vitest": "^2.1.8"
26
29
  }
27
30
  }