awaitly 1.31.1 → 1.32.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 (169) hide show
  1. package/dist/adapters.cjs +2 -2
  2. package/dist/adapters.cjs.map +1 -1
  3. package/dist/adapters.d.cts +3 -1
  4. package/dist/adapters.d.ts +3 -1
  5. package/dist/adapters.js +2 -2
  6. package/dist/adapters.js.map +1 -1
  7. package/dist/batch.cjs +2 -2
  8. package/dist/batch.cjs.map +1 -1
  9. package/dist/batch.d.cts +3 -1
  10. package/dist/batch.d.ts +3 -1
  11. package/dist/batch.js +2 -2
  12. package/dist/batch.js.map +1 -1
  13. package/dist/circuit-breaker.cjs +2 -2
  14. package/dist/circuit-breaker.cjs.map +1 -1
  15. package/dist/circuit-breaker.d.cts +3 -1
  16. package/dist/circuit-breaker.d.ts +3 -1
  17. package/dist/circuit-breaker.js +2 -2
  18. package/dist/circuit-breaker.js.map +1 -1
  19. package/dist/conditional.d.cts +3 -1
  20. package/dist/conditional.d.ts +3 -1
  21. package/dist/core.cjs +2 -2
  22. package/dist/core.cjs.map +1 -1
  23. package/dist/core.d.cts +2 -1
  24. package/dist/core.d.ts +2 -1
  25. package/dist/core.js +2 -2
  26. package/dist/core.js.map +1 -1
  27. package/dist/diagnostics.cjs +3 -3
  28. package/dist/diagnostics.cjs.map +1 -1
  29. package/dist/diagnostics.d.cts +3 -1
  30. package/dist/diagnostics.d.ts +3 -1
  31. package/dist/diagnostics.js +3 -3
  32. package/dist/diagnostics.js.map +1 -1
  33. package/dist/durable.cjs +3 -3
  34. package/dist/durable.cjs.map +1 -1
  35. package/dist/durable.d.cts +8 -361
  36. package/dist/durable.d.ts +8 -361
  37. package/dist/durable.js +3 -3
  38. package/dist/durable.js.map +1 -1
  39. package/dist/engine.cjs +11 -0
  40. package/dist/engine.cjs.map +1 -0
  41. package/dist/engine.d.cts +114 -0
  42. package/dist/engine.d.ts +114 -0
  43. package/dist/engine.js +11 -0
  44. package/dist/engine.js.map +1 -0
  45. package/dist/errors-entry-CMH73Eym.d.cts +339 -0
  46. package/dist/errors-entry-DOt5UUl4.d.ts +339 -0
  47. package/dist/errors.cjs +1 -1
  48. package/dist/errors.cjs.map +1 -1
  49. package/dist/errors.d.cts +2 -318
  50. package/dist/errors.d.ts +2 -318
  51. package/dist/errors.js +1 -1
  52. package/dist/errors.js.map +1 -1
  53. package/dist/fetch.cjs +2 -2
  54. package/dist/fetch.cjs.map +1 -1
  55. package/dist/fetch.d.cts +3 -1
  56. package/dist/fetch.d.ts +3 -1
  57. package/dist/fetch.js +2 -2
  58. package/dist/fetch.js.map +1 -1
  59. package/dist/functional.cjs +1 -1
  60. package/dist/functional.cjs.map +1 -1
  61. package/dist/functional.d.cts +2 -0
  62. package/dist/functional.d.ts +2 -0
  63. package/dist/functional.js +1 -1
  64. package/dist/functional.js.map +1 -1
  65. package/dist/{guards-PU64_GKv.d.cts → guards-B79mP5Q8.d.cts} +3 -3
  66. package/dist/{guards-B5lgMJq0.d.ts → guards-BUq6NJCM.d.ts} +3 -3
  67. package/dist/{hitl-Dyiy0R1v.d.cts → hitl-BCqkMAHw.d.cts} +2 -2
  68. package/dist/{hitl-BjeSm1sJ.d.ts → hitl-BZtPx0aU.d.ts} +2 -2
  69. package/dist/hitl.cjs +2 -2
  70. package/dist/hitl.cjs.map +1 -1
  71. package/dist/hitl.d.cts +9 -6
  72. package/dist/hitl.d.ts +9 -6
  73. package/dist/hitl.js +2 -2
  74. package/dist/hitl.js.map +1 -1
  75. package/dist/index-BVUAOWGG.d.ts +417 -0
  76. package/dist/index-CQnpmUcC.d.cts +417 -0
  77. package/dist/index.cjs +1 -1
  78. package/dist/index.cjs.map +1 -1
  79. package/dist/index.d.cts +4 -3
  80. package/dist/index.d.ts +4 -3
  81. package/dist/index.js +1 -1
  82. package/dist/index.js.map +1 -1
  83. package/dist/otel.d.cts +3 -1
  84. package/dist/otel.d.ts +3 -1
  85. package/dist/{persistence-entry-B3ukwbb5.d.ts → persistence-entry-ClFhxd2Q.d.ts} +11 -5
  86. package/dist/{persistence-entry-CgVzswbg.d.cts → persistence-entry-jflY61az.d.cts} +11 -5
  87. package/dist/persistence.d.cts +6 -3
  88. package/dist/persistence.d.ts +6 -3
  89. package/dist/policies.d.cts +3 -1
  90. package/dist/policies.d.ts +3 -1
  91. package/dist/ratelimit.cjs +2 -2
  92. package/dist/ratelimit.cjs.map +1 -1
  93. package/dist/ratelimit.d.cts +3 -1
  94. package/dist/ratelimit.d.ts +3 -1
  95. package/dist/ratelimit.js +2 -2
  96. package/dist/ratelimit.js.map +1 -1
  97. package/dist/reliability.cjs +2 -2
  98. package/dist/reliability.cjs.map +1 -1
  99. package/dist/reliability.d.cts +3 -1
  100. package/dist/reliability.d.ts +3 -1
  101. package/dist/reliability.js +2 -2
  102. package/dist/reliability.js.map +1 -1
  103. package/dist/resolver.cjs +2 -2
  104. package/dist/resolver.cjs.map +1 -1
  105. package/dist/resolver.d.cts +5 -3
  106. package/dist/resolver.d.ts +5 -3
  107. package/dist/resolver.js +2 -2
  108. package/dist/resolver.js.map +1 -1
  109. package/dist/resource.cjs +2 -2
  110. package/dist/resource.cjs.map +1 -1
  111. package/dist/resource.d.cts +3 -1
  112. package/dist/resource.d.ts +3 -1
  113. package/dist/resource.js +2 -2
  114. package/dist/resource.js.map +1 -1
  115. package/dist/result/retry.cjs +1 -1
  116. package/dist/result/retry.cjs.map +1 -1
  117. package/dist/result/retry.d.cts +2 -0
  118. package/dist/result/retry.d.ts +2 -0
  119. package/dist/result/retry.js +1 -1
  120. package/dist/result/retry.js.map +1 -1
  121. package/dist/result.cjs +1 -1
  122. package/dist/result.cjs.map +1 -1
  123. package/dist/result.d.cts +8 -23
  124. package/dist/result.d.ts +8 -23
  125. package/dist/result.js +1 -1
  126. package/dist/result.js.map +1 -1
  127. package/dist/{run-entry-C1uFytM6.d.cts → run-entry-rw7zll_G.d.ts} +16 -45
  128. package/dist/{run-entry-C1uFytM6.d.ts → run-entry-yQu63Yj9.d.cts} +16 -45
  129. package/dist/run.cjs +2 -2
  130. package/dist/run.cjs.map +1 -1
  131. package/dist/run.d.cts +3 -1
  132. package/dist/run.d.ts +3 -1
  133. package/dist/run.js +2 -2
  134. package/dist/run.js.map +1 -1
  135. package/dist/saga.cjs +2 -2
  136. package/dist/saga.cjs.map +1 -1
  137. package/dist/saga.d.cts +3 -1
  138. package/dist/saga.d.ts +3 -1
  139. package/dist/saga.js +2 -2
  140. package/dist/saga.js.map +1 -1
  141. package/dist/singleflight.d.cts +3 -1
  142. package/dist/singleflight.d.ts +3 -1
  143. package/dist/streaming.cjs +4 -4
  144. package/dist/streaming.cjs.map +1 -1
  145. package/dist/streaming.d.cts +5 -3
  146. package/dist/streaming.d.ts +5 -3
  147. package/dist/streaming.js +4 -4
  148. package/dist/streaming.js.map +1 -1
  149. package/dist/testing.cjs +8 -4
  150. package/dist/testing.cjs.map +1 -1
  151. package/dist/testing.d.cts +87 -4
  152. package/dist/testing.d.ts +87 -4
  153. package/dist/testing.js +8 -4
  154. package/dist/testing.js.map +1 -1
  155. package/dist/{types-wa_wTOn3.d.ts → types-CuWK5AlK.d.ts} +1 -1
  156. package/dist/{types-DUWrNIJu.d.cts → types-uR3JpwvF.d.cts} +1 -1
  157. package/dist/webhook.cjs +2 -2
  158. package/dist/webhook.cjs.map +1 -1
  159. package/dist/webhook.d.cts +7 -4
  160. package/dist/webhook.d.ts +7 -4
  161. package/dist/webhook.js +2 -2
  162. package/dist/webhook.js.map +1 -1
  163. package/dist/workflow.cjs +3 -3
  164. package/dist/workflow.cjs.map +1 -1
  165. package/dist/workflow.d.cts +47 -11
  166. package/dist/workflow.d.ts +47 -11
  167. package/dist/workflow.js +3 -3
  168. package/dist/workflow.js.map +1 -1
  169. package/package.json +15 -1
@@ -0,0 +1,339 @@
1
+ import { TaggedErrorConstructor } from './tagged-error.cjs';
2
+
3
+ /**
4
+ * awaitly/errors
5
+ *
6
+ * Pre-built error types for common failure scenarios.
7
+ * Uses TaggedError for type-safe exhaustive matching.
8
+ *
9
+ * @example
10
+ * ```typescript
11
+ * import { TimeoutError, RetryExhaustedError, RateLimitError, CircuitOpenError } from 'awaitly/errors';
12
+ *
13
+ * // Create errors
14
+ * const timeout = new TimeoutError({ operation: 'fetchUser', ms: 5000 });
15
+ * const retryFailed = new RetryExhaustedError({ operation: 'sendEmail', attempts: 3 });
16
+ *
17
+ * // Pattern match
18
+ * TaggedError.match(error, {
19
+ * TimeoutError: (e) => `${e.operation} timed out after ${e.ms}ms`,
20
+ * RetryExhaustedError: (e) => `${e.operation} failed after ${e.attempts} attempts`,
21
+ * RateLimitError: (e) => `Rate limit exceeded, retry after ${e.retryAfterMs}ms`,
22
+ * CircuitOpenError: (e) => `Circuit ${e.circuitName} is open`,
23
+ * });
24
+ * ```
25
+ */
26
+ /**
27
+ * Factory function to create tagged error classes with default values.
28
+ *
29
+ * This is a convenience wrapper around TaggedError that allows specifying
30
+ * default property values for error types.
31
+ *
32
+ * @example
33
+ * ```typescript
34
+ * // Define custom error with defaults
35
+ * const NetworkError = makeError('NetworkError', {
36
+ * defaults: { retryable: true },
37
+ * message: (p) => `Network error: ${p.reason}`,
38
+ * });
39
+ *
40
+ * class MyNetworkError extends NetworkError<{ reason: string; code?: number }> {}
41
+ *
42
+ * const error = new MyNetworkError({ reason: 'Connection refused' });
43
+ * // error.retryable === true (from defaults)
44
+ * ```
45
+ */
46
+ declare function makeError<Tag extends string>(tag: Tag, options?: {
47
+ message?: (props: Record<string, unknown>) => string;
48
+ defaults?: Record<string, unknown>;
49
+ }): {
50
+ new (props?: Record<string, unknown>): {
51
+ readonly [x: string]: unknown;
52
+ readonly _tag: Tag;
53
+ name: string;
54
+ message: string;
55
+ stack?: string;
56
+ cause?: unknown;
57
+ };
58
+ };
59
+ declare const TimeoutError_base: TaggedErrorConstructor<"TimeoutError", {
60
+ /** Name of the operation that timed out */
61
+ operation?: string;
62
+ /** Timeout duration in milliseconds */
63
+ ms: number;
64
+ }>;
65
+ /**
66
+ * Error thrown when an operation times out.
67
+ *
68
+ * @example
69
+ * ```typescript
70
+ * const error = new TimeoutError({
71
+ * operation: 'fetchUser',
72
+ * ms: 5000,
73
+ * });
74
+ * console.log(error.message); // "TimeoutError: fetchUser timed out after 5000ms"
75
+ * ```
76
+ */
77
+ declare class TimeoutError extends TimeoutError_base {
78
+ }
79
+ declare const RetryExhaustedError_base: TaggedErrorConstructor<"RetryExhaustedError", {
80
+ /** Name of the operation that failed */
81
+ operation?: string;
82
+ /** Total number of retry attempts made */
83
+ attempts: number;
84
+ /** The last error encountered before giving up */
85
+ lastError?: unknown;
86
+ }>;
87
+ /**
88
+ * Error thrown when all retry attempts are exhausted.
89
+ *
90
+ * @example
91
+ * ```typescript
92
+ * const error = new RetryExhaustedError({
93
+ * operation: 'sendEmail',
94
+ * attempts: 3,
95
+ * lastError: originalError,
96
+ * });
97
+ * console.log(error.message); // "RetryExhaustedError: sendEmail failed after 3 attempts"
98
+ * ```
99
+ */
100
+ declare class RetryExhaustedError extends RetryExhaustedError_base {
101
+ }
102
+ declare const RateLimitError_base: TaggedErrorConstructor<"RateLimitError", {
103
+ /** Name of the rate limiter that was exceeded */
104
+ limiterName?: string;
105
+ /** Time in milliseconds until the rate limit resets */
106
+ retryAfterMs?: number;
107
+ }>;
108
+ /**
109
+ * Error thrown when a rate limit is exceeded.
110
+ *
111
+ * @example
112
+ * ```typescript
113
+ * const error = new RateLimitError({
114
+ * limiterName: 'api-calls',
115
+ * retryAfterMs: 1000,
116
+ * });
117
+ * console.log(error.message); // "RateLimitError: Rate limit exceeded for api-calls"
118
+ * ```
119
+ */
120
+ declare class RateLimitError extends RateLimitError_base {
121
+ }
122
+ declare const CircuitBreakerOpenError_base: TaggedErrorConstructor<"CircuitBreakerOpenError", {
123
+ /** Name of the circuit breaker */
124
+ circuitName: string;
125
+ /** Current state of the circuit */
126
+ state?: "OPEN" | "HALF_OPEN";
127
+ /** Time in milliseconds until the circuit may close */
128
+ retryAfterMs?: number;
129
+ }>;
130
+ /**
131
+ * Error thrown when a circuit breaker is open.
132
+ *
133
+ * @example
134
+ * ```typescript
135
+ * const error = new CircuitBreakerOpenError({
136
+ * circuitName: 'payment-api',
137
+ * state: 'OPEN',
138
+ * retryAfterMs: 30000,
139
+ * });
140
+ * console.log(error.message); // "CircuitBreakerOpenError: Circuit payment-api is OPEN"
141
+ * ```
142
+ */
143
+ declare class CircuitBreakerOpenError extends CircuitBreakerOpenError_base {
144
+ }
145
+ declare const ValidationError_base: TaggedErrorConstructor<"ValidationError", {
146
+ /** Field that failed validation */
147
+ field: string;
148
+ /** Reason for validation failure */
149
+ reason: string;
150
+ /** Raw value that failed validation */
151
+ value?: unknown;
152
+ }>;
153
+ /**
154
+ * Error thrown when validation fails.
155
+ *
156
+ * @example
157
+ * ```typescript
158
+ * const error = new ValidationError({
159
+ * field: 'email',
160
+ * reason: 'Invalid email format',
161
+ * });
162
+ * console.log(error.message); // "ValidationError: Invalid email - Invalid email format"
163
+ * ```
164
+ */
165
+ declare class ValidationError extends ValidationError_base {
166
+ }
167
+ declare const NotFoundError_base: TaggedErrorConstructor<"NotFoundError", {
168
+ /** Type of resource that was not found */
169
+ resource: string;
170
+ /** Identifier of the missing resource */
171
+ id?: string;
172
+ }>;
173
+ /**
174
+ * Error thrown when a resource is not found.
175
+ *
176
+ * @example
177
+ * ```typescript
178
+ * const error = new NotFoundError({
179
+ * resource: 'User',
180
+ * id: '123',
181
+ * });
182
+ * console.log(error.message); // "NotFoundError: User with id 123 not found"
183
+ * ```
184
+ */
185
+ declare class NotFoundError extends NotFoundError_base {
186
+ }
187
+ declare const UnauthorizedError_base: TaggedErrorConstructor<"UnauthorizedError", {
188
+ /** Action that was attempted */
189
+ action?: string;
190
+ /** Resource that was being accessed */
191
+ resource?: string;
192
+ /** Reason for denial */
193
+ reason?: string;
194
+ }>;
195
+ /**
196
+ * Error thrown when access is denied.
197
+ *
198
+ * @example
199
+ * ```typescript
200
+ * const error = new UnauthorizedError({
201
+ * action: 'delete',
202
+ * resource: 'User',
203
+ * });
204
+ * console.log(error.message); // "UnauthorizedError: Not authorized to delete User"
205
+ * ```
206
+ */
207
+ declare class UnauthorizedError extends UnauthorizedError_base {
208
+ }
209
+ declare const NetworkError_base: TaggedErrorConstructor<"NetworkError", {
210
+ /** URL that was being accessed */
211
+ url?: string;
212
+ /** Reason for the network failure */
213
+ reason: string;
214
+ /** Whether this error is retryable */
215
+ retryable?: boolean;
216
+ /** HTTP status code if applicable */
217
+ statusCode?: number;
218
+ }>;
219
+ /**
220
+ * Error thrown for network-related failures.
221
+ *
222
+ * @example
223
+ * ```typescript
224
+ * const error = new NetworkError({
225
+ * url: 'https://api.example.com/users',
226
+ * reason: 'Connection refused',
227
+ * retryable: true,
228
+ * });
229
+ * ```
230
+ */
231
+ declare class NetworkError extends NetworkError_base {
232
+ }
233
+ declare const CompensationError_base: TaggedErrorConstructor<"CompensationError", {
234
+ /** Step that triggered compensation */
235
+ step: string;
236
+ /** The original error that caused compensation */
237
+ originalError?: unknown;
238
+ /** Error that occurred during compensation */
239
+ compensationError?: unknown;
240
+ }>;
241
+ /**
242
+ * Error thrown when a saga compensation fails.
243
+ *
244
+ * @example
245
+ * ```typescript
246
+ * const error = new CompensationError({
247
+ * step: 'chargeCard',
248
+ * originalError: paymentError,
249
+ * compensationError: refundError,
250
+ * });
251
+ * ```
252
+ */
253
+ declare class CompensationError extends CompensationError_base {
254
+ }
255
+ declare const UnexpectedError_base: TaggedErrorConstructor<"UnexpectedError", {
256
+ /** The original thrown value or cancellation error */
257
+ cause?: unknown;
258
+ }>;
259
+ /**
260
+ * Default error type for uncaught exceptions and cancellation in workflows.
261
+ * This is the default `U` type when `catchUnexpected` is not provided.
262
+ *
263
+ * @example
264
+ * ```typescript
265
+ * // Automatically used as the default — no need to pass catchUnexpected:
266
+ * const workflow = createWorkflow("checkout", { chargeCard, sendEmail });
267
+ *
268
+ * // Equivalent to:
269
+ * const workflow = createWorkflow("checkout", { chargeCard, sendEmail }, {
270
+ * catchUnexpected: (cause) => new UnexpectedError({ cause }),
271
+ * });
272
+ * ```
273
+ */
274
+ declare class UnexpectedError extends UnexpectedError_base {
275
+ }
276
+ /**
277
+ * Union of all pre-built error types.
278
+ * Useful for exhaustive pattern matching.
279
+ *
280
+ * @example
281
+ * ```typescript
282
+ * function handleError(error: AwaitlyError): string {
283
+ * return TaggedError.match(error, {
284
+ * TimeoutError: (e) => `Timeout: ${e.ms}ms`,
285
+ * RetryExhaustedError: (e) => `Retries: ${e.attempts}`,
286
+ * RateLimitError: (e) => `Rate limited`,
287
+ * CircuitBreakerOpenError: (e) => `Circuit open: ${e.circuitName}`,
288
+ * ValidationError: (e) => `Invalid: ${e.field}`,
289
+ * NotFoundError: (e) => `Not found: ${e.resource}`,
290
+ * UnauthorizedError: (e) => `Unauthorized`,
291
+ * NetworkError: (e) => `Network: ${e.reason}`,
292
+ * CompensationError: (e) => `Compensation failed: ${e.step}`,
293
+ * });
294
+ * }
295
+ * ```
296
+ */
297
+ type AwaitlyError = TimeoutError | RetryExhaustedError | RateLimitError | CircuitBreakerOpenError | ValidationError | NotFoundError | UnauthorizedError | NetworkError | CompensationError;
298
+ /**
299
+ * Check if an error is a TimeoutError.
300
+ */
301
+ declare function isTimeoutError(error: unknown): error is TimeoutError;
302
+ /**
303
+ * Check if an error is a RetryExhaustedError.
304
+ */
305
+ declare function isRetryExhaustedError(error: unknown): error is RetryExhaustedError;
306
+ /**
307
+ * Check if an error is a RateLimitError.
308
+ */
309
+ declare function isRateLimitError(error: unknown): error is RateLimitError;
310
+ /**
311
+ * Check if an error is a CircuitBreakerOpenError.
312
+ */
313
+ declare function isCircuitBreakerOpenError(error: unknown): error is CircuitBreakerOpenError;
314
+ /**
315
+ * Check if an error is a ValidationError.
316
+ */
317
+ declare function isValidationError(error: unknown): error is ValidationError;
318
+ /**
319
+ * Check if an error is a NotFoundError.
320
+ */
321
+ declare function isNotFoundError(error: unknown): error is NotFoundError;
322
+ /**
323
+ * Check if an error is an UnauthorizedError.
324
+ */
325
+ declare function isUnauthorizedError(error: unknown): error is UnauthorizedError;
326
+ /**
327
+ * Check if an error is a NetworkError.
328
+ */
329
+ declare function isNetworkError(error: unknown): error is NetworkError;
330
+ /**
331
+ * Check if an error is a CompensationError.
332
+ */
333
+ declare function isCompensationError(error: unknown): error is CompensationError;
334
+ /**
335
+ * Check if an error is any AwaitlyError.
336
+ */
337
+ declare function isAwaitlyError(error: unknown): error is AwaitlyError;
338
+
339
+ export { type AwaitlyError as A, CircuitBreakerOpenError as C, NetworkError as N, RateLimitError as R, TimeoutError as T, UnexpectedError as U, ValidationError as V, CompensationError as a, NotFoundError as b, RetryExhaustedError as c, UnauthorizedError as d, isCircuitBreakerOpenError as e, isCompensationError as f, isNetworkError as g, isNotFoundError as h, isAwaitlyError as i, isRateLimitError as j, isRetryExhaustedError as k, isTimeoutError as l, isUnauthorizedError as m, isValidationError as n, makeError as o };
@@ -0,0 +1,339 @@
1
+ import { TaggedErrorConstructor } from './tagged-error.js';
2
+
3
+ /**
4
+ * awaitly/errors
5
+ *
6
+ * Pre-built error types for common failure scenarios.
7
+ * Uses TaggedError for type-safe exhaustive matching.
8
+ *
9
+ * @example
10
+ * ```typescript
11
+ * import { TimeoutError, RetryExhaustedError, RateLimitError, CircuitOpenError } from 'awaitly/errors';
12
+ *
13
+ * // Create errors
14
+ * const timeout = new TimeoutError({ operation: 'fetchUser', ms: 5000 });
15
+ * const retryFailed = new RetryExhaustedError({ operation: 'sendEmail', attempts: 3 });
16
+ *
17
+ * // Pattern match
18
+ * TaggedError.match(error, {
19
+ * TimeoutError: (e) => `${e.operation} timed out after ${e.ms}ms`,
20
+ * RetryExhaustedError: (e) => `${e.operation} failed after ${e.attempts} attempts`,
21
+ * RateLimitError: (e) => `Rate limit exceeded, retry after ${e.retryAfterMs}ms`,
22
+ * CircuitOpenError: (e) => `Circuit ${e.circuitName} is open`,
23
+ * });
24
+ * ```
25
+ */
26
+ /**
27
+ * Factory function to create tagged error classes with default values.
28
+ *
29
+ * This is a convenience wrapper around TaggedError that allows specifying
30
+ * default property values for error types.
31
+ *
32
+ * @example
33
+ * ```typescript
34
+ * // Define custom error with defaults
35
+ * const NetworkError = makeError('NetworkError', {
36
+ * defaults: { retryable: true },
37
+ * message: (p) => `Network error: ${p.reason}`,
38
+ * });
39
+ *
40
+ * class MyNetworkError extends NetworkError<{ reason: string; code?: number }> {}
41
+ *
42
+ * const error = new MyNetworkError({ reason: 'Connection refused' });
43
+ * // error.retryable === true (from defaults)
44
+ * ```
45
+ */
46
+ declare function makeError<Tag extends string>(tag: Tag, options?: {
47
+ message?: (props: Record<string, unknown>) => string;
48
+ defaults?: Record<string, unknown>;
49
+ }): {
50
+ new (props?: Record<string, unknown>): {
51
+ readonly [x: string]: unknown;
52
+ readonly _tag: Tag;
53
+ name: string;
54
+ message: string;
55
+ stack?: string;
56
+ cause?: unknown;
57
+ };
58
+ };
59
+ declare const TimeoutError_base: TaggedErrorConstructor<"TimeoutError", {
60
+ /** Name of the operation that timed out */
61
+ operation?: string;
62
+ /** Timeout duration in milliseconds */
63
+ ms: number;
64
+ }>;
65
+ /**
66
+ * Error thrown when an operation times out.
67
+ *
68
+ * @example
69
+ * ```typescript
70
+ * const error = new TimeoutError({
71
+ * operation: 'fetchUser',
72
+ * ms: 5000,
73
+ * });
74
+ * console.log(error.message); // "TimeoutError: fetchUser timed out after 5000ms"
75
+ * ```
76
+ */
77
+ declare class TimeoutError extends TimeoutError_base {
78
+ }
79
+ declare const RetryExhaustedError_base: TaggedErrorConstructor<"RetryExhaustedError", {
80
+ /** Name of the operation that failed */
81
+ operation?: string;
82
+ /** Total number of retry attempts made */
83
+ attempts: number;
84
+ /** The last error encountered before giving up */
85
+ lastError?: unknown;
86
+ }>;
87
+ /**
88
+ * Error thrown when all retry attempts are exhausted.
89
+ *
90
+ * @example
91
+ * ```typescript
92
+ * const error = new RetryExhaustedError({
93
+ * operation: 'sendEmail',
94
+ * attempts: 3,
95
+ * lastError: originalError,
96
+ * });
97
+ * console.log(error.message); // "RetryExhaustedError: sendEmail failed after 3 attempts"
98
+ * ```
99
+ */
100
+ declare class RetryExhaustedError extends RetryExhaustedError_base {
101
+ }
102
+ declare const RateLimitError_base: TaggedErrorConstructor<"RateLimitError", {
103
+ /** Name of the rate limiter that was exceeded */
104
+ limiterName?: string;
105
+ /** Time in milliseconds until the rate limit resets */
106
+ retryAfterMs?: number;
107
+ }>;
108
+ /**
109
+ * Error thrown when a rate limit is exceeded.
110
+ *
111
+ * @example
112
+ * ```typescript
113
+ * const error = new RateLimitError({
114
+ * limiterName: 'api-calls',
115
+ * retryAfterMs: 1000,
116
+ * });
117
+ * console.log(error.message); // "RateLimitError: Rate limit exceeded for api-calls"
118
+ * ```
119
+ */
120
+ declare class RateLimitError extends RateLimitError_base {
121
+ }
122
+ declare const CircuitBreakerOpenError_base: TaggedErrorConstructor<"CircuitBreakerOpenError", {
123
+ /** Name of the circuit breaker */
124
+ circuitName: string;
125
+ /** Current state of the circuit */
126
+ state?: "OPEN" | "HALF_OPEN";
127
+ /** Time in milliseconds until the circuit may close */
128
+ retryAfterMs?: number;
129
+ }>;
130
+ /**
131
+ * Error thrown when a circuit breaker is open.
132
+ *
133
+ * @example
134
+ * ```typescript
135
+ * const error = new CircuitBreakerOpenError({
136
+ * circuitName: 'payment-api',
137
+ * state: 'OPEN',
138
+ * retryAfterMs: 30000,
139
+ * });
140
+ * console.log(error.message); // "CircuitBreakerOpenError: Circuit payment-api is OPEN"
141
+ * ```
142
+ */
143
+ declare class CircuitBreakerOpenError extends CircuitBreakerOpenError_base {
144
+ }
145
+ declare const ValidationError_base: TaggedErrorConstructor<"ValidationError", {
146
+ /** Field that failed validation */
147
+ field: string;
148
+ /** Reason for validation failure */
149
+ reason: string;
150
+ /** Raw value that failed validation */
151
+ value?: unknown;
152
+ }>;
153
+ /**
154
+ * Error thrown when validation fails.
155
+ *
156
+ * @example
157
+ * ```typescript
158
+ * const error = new ValidationError({
159
+ * field: 'email',
160
+ * reason: 'Invalid email format',
161
+ * });
162
+ * console.log(error.message); // "ValidationError: Invalid email - Invalid email format"
163
+ * ```
164
+ */
165
+ declare class ValidationError extends ValidationError_base {
166
+ }
167
+ declare const NotFoundError_base: TaggedErrorConstructor<"NotFoundError", {
168
+ /** Type of resource that was not found */
169
+ resource: string;
170
+ /** Identifier of the missing resource */
171
+ id?: string;
172
+ }>;
173
+ /**
174
+ * Error thrown when a resource is not found.
175
+ *
176
+ * @example
177
+ * ```typescript
178
+ * const error = new NotFoundError({
179
+ * resource: 'User',
180
+ * id: '123',
181
+ * });
182
+ * console.log(error.message); // "NotFoundError: User with id 123 not found"
183
+ * ```
184
+ */
185
+ declare class NotFoundError extends NotFoundError_base {
186
+ }
187
+ declare const UnauthorizedError_base: TaggedErrorConstructor<"UnauthorizedError", {
188
+ /** Action that was attempted */
189
+ action?: string;
190
+ /** Resource that was being accessed */
191
+ resource?: string;
192
+ /** Reason for denial */
193
+ reason?: string;
194
+ }>;
195
+ /**
196
+ * Error thrown when access is denied.
197
+ *
198
+ * @example
199
+ * ```typescript
200
+ * const error = new UnauthorizedError({
201
+ * action: 'delete',
202
+ * resource: 'User',
203
+ * });
204
+ * console.log(error.message); // "UnauthorizedError: Not authorized to delete User"
205
+ * ```
206
+ */
207
+ declare class UnauthorizedError extends UnauthorizedError_base {
208
+ }
209
+ declare const NetworkError_base: TaggedErrorConstructor<"NetworkError", {
210
+ /** URL that was being accessed */
211
+ url?: string;
212
+ /** Reason for the network failure */
213
+ reason: string;
214
+ /** Whether this error is retryable */
215
+ retryable?: boolean;
216
+ /** HTTP status code if applicable */
217
+ statusCode?: number;
218
+ }>;
219
+ /**
220
+ * Error thrown for network-related failures.
221
+ *
222
+ * @example
223
+ * ```typescript
224
+ * const error = new NetworkError({
225
+ * url: 'https://api.example.com/users',
226
+ * reason: 'Connection refused',
227
+ * retryable: true,
228
+ * });
229
+ * ```
230
+ */
231
+ declare class NetworkError extends NetworkError_base {
232
+ }
233
+ declare const CompensationError_base: TaggedErrorConstructor<"CompensationError", {
234
+ /** Step that triggered compensation */
235
+ step: string;
236
+ /** The original error that caused compensation */
237
+ originalError?: unknown;
238
+ /** Error that occurred during compensation */
239
+ compensationError?: unknown;
240
+ }>;
241
+ /**
242
+ * Error thrown when a saga compensation fails.
243
+ *
244
+ * @example
245
+ * ```typescript
246
+ * const error = new CompensationError({
247
+ * step: 'chargeCard',
248
+ * originalError: paymentError,
249
+ * compensationError: refundError,
250
+ * });
251
+ * ```
252
+ */
253
+ declare class CompensationError extends CompensationError_base {
254
+ }
255
+ declare const UnexpectedError_base: TaggedErrorConstructor<"UnexpectedError", {
256
+ /** The original thrown value or cancellation error */
257
+ cause?: unknown;
258
+ }>;
259
+ /**
260
+ * Default error type for uncaught exceptions and cancellation in workflows.
261
+ * This is the default `U` type when `catchUnexpected` is not provided.
262
+ *
263
+ * @example
264
+ * ```typescript
265
+ * // Automatically used as the default — no need to pass catchUnexpected:
266
+ * const workflow = createWorkflow("checkout", { chargeCard, sendEmail });
267
+ *
268
+ * // Equivalent to:
269
+ * const workflow = createWorkflow("checkout", { chargeCard, sendEmail }, {
270
+ * catchUnexpected: (cause) => new UnexpectedError({ cause }),
271
+ * });
272
+ * ```
273
+ */
274
+ declare class UnexpectedError extends UnexpectedError_base {
275
+ }
276
+ /**
277
+ * Union of all pre-built error types.
278
+ * Useful for exhaustive pattern matching.
279
+ *
280
+ * @example
281
+ * ```typescript
282
+ * function handleError(error: AwaitlyError): string {
283
+ * return TaggedError.match(error, {
284
+ * TimeoutError: (e) => `Timeout: ${e.ms}ms`,
285
+ * RetryExhaustedError: (e) => `Retries: ${e.attempts}`,
286
+ * RateLimitError: (e) => `Rate limited`,
287
+ * CircuitBreakerOpenError: (e) => `Circuit open: ${e.circuitName}`,
288
+ * ValidationError: (e) => `Invalid: ${e.field}`,
289
+ * NotFoundError: (e) => `Not found: ${e.resource}`,
290
+ * UnauthorizedError: (e) => `Unauthorized`,
291
+ * NetworkError: (e) => `Network: ${e.reason}`,
292
+ * CompensationError: (e) => `Compensation failed: ${e.step}`,
293
+ * });
294
+ * }
295
+ * ```
296
+ */
297
+ type AwaitlyError = TimeoutError | RetryExhaustedError | RateLimitError | CircuitBreakerOpenError | ValidationError | NotFoundError | UnauthorizedError | NetworkError | CompensationError;
298
+ /**
299
+ * Check if an error is a TimeoutError.
300
+ */
301
+ declare function isTimeoutError(error: unknown): error is TimeoutError;
302
+ /**
303
+ * Check if an error is a RetryExhaustedError.
304
+ */
305
+ declare function isRetryExhaustedError(error: unknown): error is RetryExhaustedError;
306
+ /**
307
+ * Check if an error is a RateLimitError.
308
+ */
309
+ declare function isRateLimitError(error: unknown): error is RateLimitError;
310
+ /**
311
+ * Check if an error is a CircuitBreakerOpenError.
312
+ */
313
+ declare function isCircuitBreakerOpenError(error: unknown): error is CircuitBreakerOpenError;
314
+ /**
315
+ * Check if an error is a ValidationError.
316
+ */
317
+ declare function isValidationError(error: unknown): error is ValidationError;
318
+ /**
319
+ * Check if an error is a NotFoundError.
320
+ */
321
+ declare function isNotFoundError(error: unknown): error is NotFoundError;
322
+ /**
323
+ * Check if an error is an UnauthorizedError.
324
+ */
325
+ declare function isUnauthorizedError(error: unknown): error is UnauthorizedError;
326
+ /**
327
+ * Check if an error is a NetworkError.
328
+ */
329
+ declare function isNetworkError(error: unknown): error is NetworkError;
330
+ /**
331
+ * Check if an error is a CompensationError.
332
+ */
333
+ declare function isCompensationError(error: unknown): error is CompensationError;
334
+ /**
335
+ * Check if an error is any AwaitlyError.
336
+ */
337
+ declare function isAwaitlyError(error: unknown): error is AwaitlyError;
338
+
339
+ export { type AwaitlyError as A, CircuitBreakerOpenError as C, NetworkError as N, RateLimitError as R, TimeoutError as T, UnexpectedError as U, ValidationError as V, CompensationError as a, NotFoundError as b, RetryExhaustedError as c, UnauthorizedError as d, isCircuitBreakerOpenError as e, isCompensationError as f, isNetworkError as g, isNotFoundError as h, isAwaitlyError as i, isRateLimitError as j, isRetryExhaustedError as k, isTimeoutError as l, isUnauthorizedError as m, isValidationError as n, makeError as o };