@zap-studio/retry 0.2.0 → 0.3.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.
Files changed (42) hide show
  1. package/CHANGELOG.md +18 -0
  2. package/README.md +16 -4
  3. package/dist/abort.d.mts +30 -0
  4. package/dist/abort.d.mts.map +1 -0
  5. package/dist/abort.mjs +61 -0
  6. package/dist/abort.mjs.map +1 -0
  7. package/dist/errors-fWo_KyVO.d.mts +76 -0
  8. package/dist/errors-fWo_KyVO.d.mts.map +1 -0
  9. package/dist/errors.d.mts +2 -0
  10. package/dist/{error.mjs → errors.mjs} +21 -3
  11. package/dist/errors.mjs.map +1 -0
  12. package/dist/exponential-backoff.d.mts +18 -0
  13. package/dist/exponential-backoff.d.mts.map +1 -1
  14. package/dist/exponential-backoff.mjs +9 -0
  15. package/dist/exponential-backoff.mjs.map +1 -1
  16. package/dist/fixed-delay.d.mts +12 -0
  17. package/dist/fixed-delay.d.mts.map +1 -1
  18. package/dist/fixed-delay.mjs +6 -0
  19. package/dist/fixed-delay.mjs.map +1 -1
  20. package/dist/index.d.mts +3 -32
  21. package/dist/index.d.mts.map +1 -1
  22. package/dist/index.mjs +9 -172
  23. package/dist/index.mjs.map +1 -1
  24. package/dist/result-mode.d.mts +24 -0
  25. package/dist/result-mode.d.mts.map +1 -0
  26. package/dist/result-mode.mjs +145 -0
  27. package/dist/result-mode.mjs.map +1 -0
  28. package/dist/sleep.d.mts +17 -0
  29. package/dist/sleep.d.mts.map +1 -0
  30. package/dist/sleep.mjs +21 -0
  31. package/dist/sleep.mjs.map +1 -0
  32. package/dist/throw-mode.d.mts +26 -0
  33. package/dist/throw-mode.d.mts.map +1 -0
  34. package/dist/throw-mode.mjs +50 -0
  35. package/dist/throw-mode.mjs.map +1 -0
  36. package/dist/types.d.mts +58 -3
  37. package/dist/types.d.mts.map +1 -1
  38. package/package.json +6 -2
  39. package/dist/error-CVW4I654.d.mts +0 -44
  40. package/dist/error-CVW4I654.d.mts.map +0 -1
  41. package/dist/error.d.mts +0 -2
  42. package/dist/error.mjs.map +0 -1
package/dist/types.d.mts CHANGED
@@ -1,4 +1,4 @@
1
- import { t as RetryError } from "./error-CVW4I654.mjs";
1
+ import { r as RetryError, t as AbortError } from "./errors-fWo_KyVO.mjs";
2
2
 
3
3
  //#region src/types.d.ts
4
4
  /**
@@ -28,25 +28,59 @@ interface RetryPolicy<TError = unknown, TData = unknown> {
28
28
  * Decision returned by a retry policy for a specific attempt.
29
29
  */
30
30
  interface RetryDecision {
31
+ /**
32
+ * When `true`, the runner may schedule another attempt (subject to
33
+ * `delayMs` and the runner's abort rules).
34
+ */
31
35
  readonly shouldRetry: boolean;
36
+ /**
37
+ * Milliseconds to wait before the next attempt when `shouldRetry` is `true`.
38
+ */
32
39
  readonly delayMs: number;
40
+ /**
41
+ * Optional machine-readable reason for the decision.
42
+ */
33
43
  readonly reason?: "retry" | "max-attempts-reached" | "policy-declined";
34
44
  }
35
45
  /**
36
46
  * Input passed to `RetryPolicy.next(...)` for each failed attempt.
37
47
  */
38
48
  interface RetryDecisionInput<TError = unknown, TData = unknown> {
49
+ /**
50
+ * One-based attempt number for the current failure.
51
+ */
39
52
  readonly attempt: number;
53
+ /**
54
+ * Optional policy-level maximum attempts, when a policy wants to pass it
55
+ * through to `next`.
56
+ */
40
57
  readonly maxAttempts?: number;
58
+ /**
59
+ * Error raised by the most recent `execute(attempt)` call, when a failure
60
+ * occurred.
61
+ */
41
62
  readonly error?: TError;
63
+ /**
64
+ * Optional data captured alongside the failure, when a policy populates
65
+ * it.
66
+ */
42
67
  readonly data?: TData;
43
68
  }
44
69
  /**
45
70
  * Input passed to `RetryPolicy.onExhausted(...)` when retries stop.
46
71
  */
47
72
  interface RetryExhaustedInput<TError = unknown, TData = unknown> {
73
+ /**
74
+ * Count of completed attempts that led to stopping retries.
75
+ */
48
76
  readonly attempts: number;
77
+ /**
78
+ * Last execution error, when available.
79
+ */
49
80
  readonly error?: TError;
81
+ /**
82
+ * Last captured data, when a policy or runner supplies it.
83
+ */
50
84
  readonly data?: TData;
51
85
  }
52
86
  /**
@@ -70,19 +104,40 @@ interface RetryRunOptions {
70
104
  *
71
105
  * When `false`, the runner returns a `RetryRunResult` discriminated union.
72
106
  *
73
- * @default false
107
+ * @default true
74
108
  */
75
109
  readonly throwOnExhausted?: boolean;
76
110
  }
77
111
  /**
78
112
  * Result union returned by non-throw runner mode.
113
+ *
114
+ * - Success: `ok: true` with the resolved `value`.
115
+ * - Failure: `ok: false` with terminal `error` and completed `attempts` count
116
+ * (exhaustion or abort).
79
117
  */
80
118
  type RetryRunResult<T> = {
119
+ /**
120
+ * Discriminator for a successful run.
121
+ */
81
122
  ok: true;
123
+ /**
124
+ * Successful return value from the final attempt.
125
+ */
82
126
  value: T;
83
127
  } | {
128
+ /**
129
+ * Discriminator for a failed or aborted run.
130
+ */
84
131
  ok: false;
85
- error: RetryError;
132
+ /**
133
+ * Terminal error: `RetryError` when retries are exhausted, or
134
+ * `AbortError` when the run is canceled (non-throw path uses the same
135
+ * instances as throw mode, not wrapped).
136
+ */
137
+ error: RetryError | AbortError;
138
+ /**
139
+ * Number of attempts that completed before the terminal outcome.
140
+ */
86
141
  attempts: number;
87
142
  };
88
143
  //#endregion
@@ -1 +1 @@
1
- {"version":3,"file":"types.d.mts","names":[],"sources":["../src/types.ts"],"mappings":";;;;;;;;;;;;UAiBiB,WAAA;;;;;;EAMf,IAAA,CAAK,KAAA,EAAO,kBAAA,CAAmB,MAAA,EAAQ,KAAA,IAAS,aAAA;;;;;;EAMhD,WAAA,CAAY,KAAA,EAAO,mBAAA,CAAoB,MAAA,EAAQ,KAAA,IAAS,UAAA;AAAA;AAM1D;;;AAAA,UAAiB,aAAA;EAAA,SACN,WAAA;EAAA,SACA,OAAA;EAAA,SACA,MAAA;AAAA;;AAMX;;UAAiB,kBAAA;EAAA,SACN,OAAA;EAAA,SACA,WAAA;EAAA,SACA,KAAA,GAAQ,MAAA;EAAA,SACR,I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CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zap-studio/retry",
3
- "version": "0.2.0",
3
+ "version": "0.3.0",
4
4
  "private": false,
5
5
  "description": "Composable retry policies for resilient async operations.",
6
6
  "keywords": [
@@ -30,9 +30,13 @@
30
30
  "types": "./dist/index.d.mts",
31
31
  "exports": {
32
32
  ".": "./dist/index.mjs",
33
- "./error": "./dist/error.mjs",
33
+ "./abort": "./dist/abort.mjs",
34
+ "./errors": "./dist/errors.mjs",
34
35
  "./exponential-backoff": "./dist/exponential-backoff.mjs",
35
36
  "./fixed-delay": "./dist/fixed-delay.mjs",
37
+ "./result-mode": "./dist/result-mode.mjs",
38
+ "./sleep": "./dist/sleep.mjs",
39
+ "./throw-mode": "./dist/throw-mode.mjs",
36
40
  "./types": "./dist/types.mjs",
37
41
  "./package.json": "./package.json"
38
42
  },
@@ -1,44 +0,0 @@
1
- //#region src/error.d.ts
2
- /**
3
- * Terminal error types used by retry policies and runners.
4
- *
5
- * @module @zap-studio/retry/error
6
- */
7
- /**
8
- * Context payload attached to `RetryError`.
9
- */
10
- interface RetryErrorContext {
11
- readonly attempts: number;
12
- readonly lastError?: unknown;
13
- readonly lastData?: unknown;
14
- }
15
- /**
16
- * Error thrown when retries are exhausted.
17
- *
18
- * @example
19
- * throw new RetryError("Retry exhausted", {
20
- * attempts: 3,
21
- * lastError: new Error("network"),
22
- * });
23
- */
24
- declare class RetryError extends Error {
25
- /**
26
- * Total attempts performed before exhaustion.
27
- */
28
- readonly attempts: number;
29
- /**
30
- * Last captured error from execution.
31
- */
32
- readonly lastError?: unknown;
33
- /**
34
- * Last captured data value, when available.
35
- */
36
- readonly lastData?: unknown;
37
- /**
38
- * Creates a RetryError with structured terminal context.
39
- */
40
- constructor(message: string, context: RetryErrorContext);
41
- }
42
- //#endregion
43
- export { RetryErrorContext as n, RetryError as t };
44
- //# sourceMappingURL=error-CVW4I654.d.mts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"error-CVW4I654.d.mts","names":[],"sources":["../src/error.ts"],"mappings":";;AASA;;;;;;;UAAiB,iBAAA;EAAA,SACN,QAAA;EAAA,SACA,SAAA;EAAA,SACA,QAAA;AAAA;;;;;;;;;;cAYE,UAAA,SAAmB,KAAA;EAiBQ;;;EAAA,SAbtB,QAAA;;;;WAIA,SAAA;;;;WAIA,QAAA;;;;EAKhB,WAAA,CAAY,OAAA,UAAiB,OAAA,EAAS,iBAAA;AAAA"}
package/dist/error.d.mts DELETED
@@ -1,2 +0,0 @@
1
- import { n as RetryErrorContext, t as RetryError } from "./error-CVW4I654.mjs";
2
- export { RetryError, RetryErrorContext };
@@ -1 +0,0 @@
1
- {"version":3,"file":"error.mjs","names":[],"sources":["../src/error.ts"],"sourcesContent":["/**\n * Terminal error types used by retry policies and runners.\n *\n * @module @zap-studio/retry/error\n */\n\n/**\n * Context payload attached to `RetryError`.\n */\nexport interface RetryErrorContext {\n readonly attempts: number;\n readonly lastError?: unknown;\n readonly lastData?: unknown;\n}\n\n/**\n * Error thrown when retries are exhausted.\n *\n * @example\n * throw new RetryError(\"Retry exhausted\", {\n * attempts: 3,\n * lastError: new Error(\"network\"),\n * });\n */\nexport class RetryError extends Error {\n /**\n * Total attempts performed before exhaustion.\n */\n public readonly attempts: number;\n /**\n * Last captured error from execution.\n */\n public readonly lastError?: unknown;\n /**\n * Last captured data value, when available.\n */\n public readonly lastData?: unknown;\n\n /**\n * Creates a RetryError with structured terminal context.\n */\n constructor(message: string, context: RetryErrorContext) {\n super(message);\n this.name = \"RetryError\";\n this.attempts = context.attempts;\n this.lastError = context.lastError;\n this.lastData = context.lastData;\n }\n}\n"],"mappings":";;;;;;;;;;AAwBA,IAAa,aAAb,cAAgC,MAAM;;;;CAIpC;;;;CAIA;;;;CAIA;;;;CAKA,YAAY,SAAiB,SAA4B;AACvD,QAAM,QAAQ;AACd,OAAK,OAAO;AACZ,OAAK,WAAW,QAAQ;AACxB,OAAK,YAAY,QAAQ;AACzB,OAAK,WAAW,QAAQ"}