@digilogiclabs/platform-core 1.20.3 → 1.21.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.
- package/dist/{ConsoleEmail-CO0OM4UT.d.ts → ConsoleEmail-BbdpINKx.d.ts} +4 -46
- package/dist/{ConsoleEmail-DDB28vyS.d.mts → ConsoleEmail-DpxhsG9C.d.mts} +4 -46
- package/dist/{IAI-D8wA_i8N.d.mts → IAI-CWd11dzL.d.mts} +1 -105
- package/dist/{IAI-D8wA_i8N.d.ts → IAI-CWd11dzL.d.ts} +1 -105
- package/dist/IEmail-CsOcaQcb.d.mts +45 -0
- package/dist/IEmail-CsOcaQcb.d.ts +45 -0
- package/dist/ILogger-DLDBZaH4.d.mts +105 -0
- package/dist/ILogger-DLDBZaH4.d.ts +105 -0
- package/dist/{agents-Cc65YUoW.d.ts → agents-C42rGwRl.d.ts} +2 -1
- package/dist/{agents-DGciJI27.d.mts → agents-CES-Aq1K.d.mts} +2 -1
- package/dist/agents.d.mts +3 -2
- package/dist/agents.d.ts +3 -2
- package/dist/auth.d.mts +238 -1
- package/dist/auth.d.ts +238 -1
- package/dist/auth.js +198 -0
- package/dist/auth.js.map +1 -1
- package/dist/auth.mjs +183 -0
- package/dist/auth.mjs.map +1 -1
- package/dist/circuit-breaker-CP6kw0P5.d.ts +351 -0
- package/dist/circuit-breaker-CsQivd57.d.mts +351 -0
- package/dist/email-templates.d.mts +106 -1
- package/dist/email-templates.d.ts +106 -1
- package/dist/email-templates.js +608 -0
- package/dist/email-templates.js.map +1 -1
- package/dist/email-templates.mjs +596 -0
- package/dist/email-templates.mjs.map +1 -1
- package/dist/index.d.mts +12 -355
- package/dist/index.d.ts +12 -355
- package/dist/index.js +10 -7
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +10 -7
- package/dist/index.mjs.map +1 -1
- package/dist/migrate.js +0 -0
- package/dist/testing.d.mts +5 -3
- package/dist/testing.d.ts +5 -3
- package/package.json +11 -11
|
@@ -0,0 +1,351 @@
|
|
|
1
|
+
import { I as ILogger } from './ILogger-DLDBZaH4.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Retry Pattern Implementation
|
|
5
|
+
*
|
|
6
|
+
* Provides configurable retry logic with exponential backoff and jitter.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Options for retry behavior
|
|
11
|
+
*/
|
|
12
|
+
interface RetryOptions {
|
|
13
|
+
/**
|
|
14
|
+
* Maximum number of retry attempts
|
|
15
|
+
* @default 3
|
|
16
|
+
*/
|
|
17
|
+
maxAttempts: number;
|
|
18
|
+
/**
|
|
19
|
+
* Initial delay between retries in milliseconds
|
|
20
|
+
* @default 100
|
|
21
|
+
*/
|
|
22
|
+
baseDelay: number;
|
|
23
|
+
/**
|
|
24
|
+
* Maximum delay between retries in milliseconds
|
|
25
|
+
* @default 10000
|
|
26
|
+
*/
|
|
27
|
+
maxDelay: number;
|
|
28
|
+
/**
|
|
29
|
+
* Multiplier for exponential backoff
|
|
30
|
+
* @default 2
|
|
31
|
+
*/
|
|
32
|
+
backoffMultiplier: number;
|
|
33
|
+
/**
|
|
34
|
+
* Whether to add random jitter to delays
|
|
35
|
+
* @default true
|
|
36
|
+
*/
|
|
37
|
+
jitter: boolean;
|
|
38
|
+
/**
|
|
39
|
+
* Maximum jitter in milliseconds
|
|
40
|
+
* @default 100
|
|
41
|
+
*/
|
|
42
|
+
maxJitter: number;
|
|
43
|
+
/**
|
|
44
|
+
* Function to determine if error is retryable
|
|
45
|
+
* @default All errors are retryable
|
|
46
|
+
*/
|
|
47
|
+
retryIf?: (error: Error) => boolean;
|
|
48
|
+
/**
|
|
49
|
+
* Function to determine if error is non-retryable
|
|
50
|
+
* Takes precedence over retryIf
|
|
51
|
+
*/
|
|
52
|
+
abortIf?: (error: Error) => boolean;
|
|
53
|
+
/**
|
|
54
|
+
* Callback on each retry attempt
|
|
55
|
+
*/
|
|
56
|
+
onRetry?: (error: Error, attempt: number, delay: number) => void;
|
|
57
|
+
/**
|
|
58
|
+
* Logger for retry operations
|
|
59
|
+
*/
|
|
60
|
+
logger?: ILogger;
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Default retry options
|
|
64
|
+
*/
|
|
65
|
+
declare const DEFAULT_RETRY_OPTIONS: RetryOptions;
|
|
66
|
+
/**
|
|
67
|
+
* Result of a retry operation
|
|
68
|
+
*/
|
|
69
|
+
interface RetryResult<T> {
|
|
70
|
+
/** Whether the operation succeeded */
|
|
71
|
+
success: boolean;
|
|
72
|
+
/** The result if successful */
|
|
73
|
+
result?: T;
|
|
74
|
+
/** The final error if all attempts failed */
|
|
75
|
+
error?: Error;
|
|
76
|
+
/** Number of attempts made */
|
|
77
|
+
attempts: number;
|
|
78
|
+
/** Total time spent in milliseconds */
|
|
79
|
+
totalTime: number;
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Execute a function with retry logic
|
|
83
|
+
*/
|
|
84
|
+
declare function withRetry<T>(fn: () => Promise<T>, options?: Partial<RetryOptions>): Promise<T>;
|
|
85
|
+
/**
|
|
86
|
+
* Execute with retry and return detailed result
|
|
87
|
+
*/
|
|
88
|
+
declare function withRetryResult<T>(fn: () => Promise<T>, options?: Partial<RetryOptions>): Promise<RetryResult<T>>;
|
|
89
|
+
/**
|
|
90
|
+
* Create a retryable version of a function
|
|
91
|
+
*/
|
|
92
|
+
declare function retryable<TArgs extends unknown[], TResult>(fn: (...args: TArgs) => Promise<TResult>, options?: Partial<RetryOptions>): (...args: TArgs) => Promise<TResult>;
|
|
93
|
+
/**
|
|
94
|
+
* Common retry predicates
|
|
95
|
+
*/
|
|
96
|
+
declare const RetryPredicates: {
|
|
97
|
+
/**
|
|
98
|
+
* Retry on network errors
|
|
99
|
+
*/
|
|
100
|
+
networkErrors: (error: Error) => boolean;
|
|
101
|
+
/**
|
|
102
|
+
* Retry on HTTP 5xx errors
|
|
103
|
+
*/
|
|
104
|
+
serverErrors: (error: Error) => boolean;
|
|
105
|
+
/**
|
|
106
|
+
* Retry on HTTP 429 (rate limit) errors
|
|
107
|
+
*/
|
|
108
|
+
rateLimitErrors: (error: Error) => boolean;
|
|
109
|
+
/**
|
|
110
|
+
* Retry on database deadlock errors
|
|
111
|
+
*/
|
|
112
|
+
deadlockErrors: (error: Error) => boolean;
|
|
113
|
+
/**
|
|
114
|
+
* Never retry on authentication errors
|
|
115
|
+
*/
|
|
116
|
+
authenticationErrors: (error: Error) => boolean;
|
|
117
|
+
/**
|
|
118
|
+
* Never retry on validation errors
|
|
119
|
+
*/
|
|
120
|
+
validationErrors: (error: Error) => boolean;
|
|
121
|
+
};
|
|
122
|
+
/**
|
|
123
|
+
* Common retry configurations
|
|
124
|
+
*/
|
|
125
|
+
declare const RetryConfigs: {
|
|
126
|
+
/**
|
|
127
|
+
* Fast retry for quick failures
|
|
128
|
+
*/
|
|
129
|
+
fast: Partial<RetryOptions>;
|
|
130
|
+
/**
|
|
131
|
+
* Standard retry configuration
|
|
132
|
+
*/
|
|
133
|
+
standard: Partial<RetryOptions>;
|
|
134
|
+
/**
|
|
135
|
+
* Aggressive retry for critical operations
|
|
136
|
+
*/
|
|
137
|
+
aggressive: Partial<RetryOptions>;
|
|
138
|
+
/**
|
|
139
|
+
* Conservative retry for rate-limited APIs
|
|
140
|
+
*/
|
|
141
|
+
rateLimited: Partial<RetryOptions>;
|
|
142
|
+
/**
|
|
143
|
+
* Database retry configuration
|
|
144
|
+
*/
|
|
145
|
+
database: Partial<RetryOptions>;
|
|
146
|
+
};
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* Circuit Breaker Pattern Implementation
|
|
150
|
+
*
|
|
151
|
+
* Prevents cascading failures by temporarily blocking calls to a failing service.
|
|
152
|
+
*/
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* Circuit breaker states
|
|
156
|
+
*/
|
|
157
|
+
type CircuitState = "closed" | "open" | "half-open";
|
|
158
|
+
/**
|
|
159
|
+
* Options for circuit breaker behavior
|
|
160
|
+
*/
|
|
161
|
+
interface CircuitBreakerOptions {
|
|
162
|
+
/**
|
|
163
|
+
* Name of the circuit (for logging)
|
|
164
|
+
*/
|
|
165
|
+
name: string;
|
|
166
|
+
/**
|
|
167
|
+
* Number of failures before opening the circuit
|
|
168
|
+
* @default 5
|
|
169
|
+
*/
|
|
170
|
+
failureThreshold: number;
|
|
171
|
+
/**
|
|
172
|
+
* Time in milliseconds before attempting to close the circuit
|
|
173
|
+
* @default 30000
|
|
174
|
+
*/
|
|
175
|
+
resetTimeout: number;
|
|
176
|
+
/**
|
|
177
|
+
* Number of successful requests needed in half-open state to close the circuit
|
|
178
|
+
* @default 3
|
|
179
|
+
*/
|
|
180
|
+
successThreshold: number;
|
|
181
|
+
/**
|
|
182
|
+
* Time window in milliseconds for counting failures
|
|
183
|
+
* @default 60000
|
|
184
|
+
*/
|
|
185
|
+
failureWindow: number;
|
|
186
|
+
/**
|
|
187
|
+
* Function to determine if error should trip the circuit
|
|
188
|
+
* @default All errors trip the circuit
|
|
189
|
+
*/
|
|
190
|
+
shouldTrip?: (error: Error) => boolean;
|
|
191
|
+
/**
|
|
192
|
+
* Callback when circuit state changes
|
|
193
|
+
*/
|
|
194
|
+
onStateChange?: (from: CircuitState, to: CircuitState) => void;
|
|
195
|
+
/**
|
|
196
|
+
* Callback when circuit opens
|
|
197
|
+
*/
|
|
198
|
+
onOpen?: (failures: number, lastError: Error) => void;
|
|
199
|
+
/**
|
|
200
|
+
* Callback when circuit closes
|
|
201
|
+
*/
|
|
202
|
+
onClose?: () => void;
|
|
203
|
+
/**
|
|
204
|
+
* Callback when request is rejected due to open circuit
|
|
205
|
+
*/
|
|
206
|
+
onReject?: (state: CircuitState) => void;
|
|
207
|
+
/**
|
|
208
|
+
* Logger for circuit breaker events
|
|
209
|
+
*/
|
|
210
|
+
logger?: ILogger;
|
|
211
|
+
}
|
|
212
|
+
/**
|
|
213
|
+
* Default circuit breaker options
|
|
214
|
+
*/
|
|
215
|
+
declare const DEFAULT_CIRCUIT_BREAKER_OPTIONS: Omit<CircuitBreakerOptions, "name">;
|
|
216
|
+
/**
|
|
217
|
+
* Error thrown when circuit is open
|
|
218
|
+
*/
|
|
219
|
+
declare class CircuitOpenError extends Error {
|
|
220
|
+
readonly name = "CircuitOpenError";
|
|
221
|
+
readonly circuitName: string;
|
|
222
|
+
readonly state: CircuitState;
|
|
223
|
+
readonly nextAttemptAt: Date;
|
|
224
|
+
constructor(circuitName: string, state: CircuitState, resetTimeout: number);
|
|
225
|
+
}
|
|
226
|
+
/**
|
|
227
|
+
* Circuit breaker statistics
|
|
228
|
+
*/
|
|
229
|
+
interface CircuitBreakerStats {
|
|
230
|
+
state: CircuitState;
|
|
231
|
+
failures: number;
|
|
232
|
+
successes: number;
|
|
233
|
+
lastFailure?: Date;
|
|
234
|
+
lastSuccess?: Date;
|
|
235
|
+
lastStateChange?: Date;
|
|
236
|
+
totalRequests: number;
|
|
237
|
+
rejectedRequests: number;
|
|
238
|
+
}
|
|
239
|
+
/**
|
|
240
|
+
* Circuit breaker implementation
|
|
241
|
+
*/
|
|
242
|
+
declare class CircuitBreaker {
|
|
243
|
+
private state;
|
|
244
|
+
private failures;
|
|
245
|
+
private successes;
|
|
246
|
+
private failureTimestamps;
|
|
247
|
+
private lastFailure?;
|
|
248
|
+
private lastSuccess?;
|
|
249
|
+
private lastStateChange?;
|
|
250
|
+
private lastOpenedAt?;
|
|
251
|
+
private totalRequests;
|
|
252
|
+
private rejectedRequests;
|
|
253
|
+
private lastError?;
|
|
254
|
+
private readonly options;
|
|
255
|
+
private readonly logger;
|
|
256
|
+
constructor(options: CircuitBreakerOptions);
|
|
257
|
+
/**
|
|
258
|
+
* Execute a function with circuit breaker protection
|
|
259
|
+
*/
|
|
260
|
+
execute<T>(fn: () => Promise<T>): Promise<T>;
|
|
261
|
+
/**
|
|
262
|
+
* Check if circuit allows execution
|
|
263
|
+
*/
|
|
264
|
+
canExecute(): boolean;
|
|
265
|
+
/**
|
|
266
|
+
* Record a successful execution
|
|
267
|
+
*/
|
|
268
|
+
private onSuccess;
|
|
269
|
+
/**
|
|
270
|
+
* Record a failed execution
|
|
271
|
+
*/
|
|
272
|
+
private onFailure;
|
|
273
|
+
/**
|
|
274
|
+
* Transition to a new state
|
|
275
|
+
*/
|
|
276
|
+
private transitionTo;
|
|
277
|
+
/**
|
|
278
|
+
* Check if reset timeout has passed
|
|
279
|
+
*/
|
|
280
|
+
private shouldAttemptReset;
|
|
281
|
+
/**
|
|
282
|
+
* Get remaining time until reset attempt
|
|
283
|
+
*/
|
|
284
|
+
private getRemainingTimeout;
|
|
285
|
+
/**
|
|
286
|
+
* Get current circuit state
|
|
287
|
+
*/
|
|
288
|
+
getState(): CircuitState;
|
|
289
|
+
/**
|
|
290
|
+
* Get circuit breaker statistics
|
|
291
|
+
*/
|
|
292
|
+
getStats(): CircuitBreakerStats;
|
|
293
|
+
/**
|
|
294
|
+
* Manually reset the circuit to closed state
|
|
295
|
+
*/
|
|
296
|
+
reset(): void;
|
|
297
|
+
/**
|
|
298
|
+
* Manually trip the circuit to open state
|
|
299
|
+
*/
|
|
300
|
+
trip(): void;
|
|
301
|
+
/**
|
|
302
|
+
* Check if circuit is open
|
|
303
|
+
*/
|
|
304
|
+
isOpen(): boolean;
|
|
305
|
+
/**
|
|
306
|
+
* Check if circuit is closed
|
|
307
|
+
*/
|
|
308
|
+
isClosed(): boolean;
|
|
309
|
+
/**
|
|
310
|
+
* Check if circuit is half-open
|
|
311
|
+
*/
|
|
312
|
+
isHalfOpen(): boolean;
|
|
313
|
+
}
|
|
314
|
+
/**
|
|
315
|
+
* Create a circuit breaker
|
|
316
|
+
*/
|
|
317
|
+
declare function createCircuitBreaker(options: CircuitBreakerOptions): CircuitBreaker;
|
|
318
|
+
/**
|
|
319
|
+
* Circuit breaker registry for managing multiple circuits
|
|
320
|
+
*/
|
|
321
|
+
declare class CircuitBreakerRegistry {
|
|
322
|
+
private circuits;
|
|
323
|
+
private readonly logger;
|
|
324
|
+
private readonly defaultOptions;
|
|
325
|
+
constructor(options?: {
|
|
326
|
+
logger?: ILogger;
|
|
327
|
+
defaults?: Partial<CircuitBreakerOptions>;
|
|
328
|
+
});
|
|
329
|
+
/**
|
|
330
|
+
* Get or create a circuit breaker
|
|
331
|
+
*/
|
|
332
|
+
get(name: string, options?: Partial<CircuitBreakerOptions>): CircuitBreaker;
|
|
333
|
+
/**
|
|
334
|
+
* Execute with a named circuit breaker
|
|
335
|
+
*/
|
|
336
|
+
execute<T>(name: string, fn: () => Promise<T>, options?: Partial<CircuitBreakerOptions>): Promise<T>;
|
|
337
|
+
/**
|
|
338
|
+
* Get all circuit statistics
|
|
339
|
+
*/
|
|
340
|
+
getAllStats(): Record<string, CircuitBreakerStats>;
|
|
341
|
+
/**
|
|
342
|
+
* Reset all circuits
|
|
343
|
+
*/
|
|
344
|
+
resetAll(): void;
|
|
345
|
+
/**
|
|
346
|
+
* Clear all circuits
|
|
347
|
+
*/
|
|
348
|
+
clear(): void;
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
export { type CircuitBreakerOptions as C, DEFAULT_RETRY_OPTIONS as D, type RetryOptions as R, type RetryResult as a, withRetryResult as b, RetryPredicates as c, RetryConfigs as d, type CircuitState as e, DEFAULT_CIRCUIT_BREAKER_OPTIONS as f, CircuitOpenError as g, type CircuitBreakerStats as h, CircuitBreaker as i, createCircuitBreaker as j, CircuitBreakerRegistry as k, retryable as r, withRetry as w };
|
|
@@ -0,0 +1,351 @@
|
|
|
1
|
+
import { I as ILogger } from './ILogger-DLDBZaH4.mjs';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Retry Pattern Implementation
|
|
5
|
+
*
|
|
6
|
+
* Provides configurable retry logic with exponential backoff and jitter.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Options for retry behavior
|
|
11
|
+
*/
|
|
12
|
+
interface RetryOptions {
|
|
13
|
+
/**
|
|
14
|
+
* Maximum number of retry attempts
|
|
15
|
+
* @default 3
|
|
16
|
+
*/
|
|
17
|
+
maxAttempts: number;
|
|
18
|
+
/**
|
|
19
|
+
* Initial delay between retries in milliseconds
|
|
20
|
+
* @default 100
|
|
21
|
+
*/
|
|
22
|
+
baseDelay: number;
|
|
23
|
+
/**
|
|
24
|
+
* Maximum delay between retries in milliseconds
|
|
25
|
+
* @default 10000
|
|
26
|
+
*/
|
|
27
|
+
maxDelay: number;
|
|
28
|
+
/**
|
|
29
|
+
* Multiplier for exponential backoff
|
|
30
|
+
* @default 2
|
|
31
|
+
*/
|
|
32
|
+
backoffMultiplier: number;
|
|
33
|
+
/**
|
|
34
|
+
* Whether to add random jitter to delays
|
|
35
|
+
* @default true
|
|
36
|
+
*/
|
|
37
|
+
jitter: boolean;
|
|
38
|
+
/**
|
|
39
|
+
* Maximum jitter in milliseconds
|
|
40
|
+
* @default 100
|
|
41
|
+
*/
|
|
42
|
+
maxJitter: number;
|
|
43
|
+
/**
|
|
44
|
+
* Function to determine if error is retryable
|
|
45
|
+
* @default All errors are retryable
|
|
46
|
+
*/
|
|
47
|
+
retryIf?: (error: Error) => boolean;
|
|
48
|
+
/**
|
|
49
|
+
* Function to determine if error is non-retryable
|
|
50
|
+
* Takes precedence over retryIf
|
|
51
|
+
*/
|
|
52
|
+
abortIf?: (error: Error) => boolean;
|
|
53
|
+
/**
|
|
54
|
+
* Callback on each retry attempt
|
|
55
|
+
*/
|
|
56
|
+
onRetry?: (error: Error, attempt: number, delay: number) => void;
|
|
57
|
+
/**
|
|
58
|
+
* Logger for retry operations
|
|
59
|
+
*/
|
|
60
|
+
logger?: ILogger;
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Default retry options
|
|
64
|
+
*/
|
|
65
|
+
declare const DEFAULT_RETRY_OPTIONS: RetryOptions;
|
|
66
|
+
/**
|
|
67
|
+
* Result of a retry operation
|
|
68
|
+
*/
|
|
69
|
+
interface RetryResult<T> {
|
|
70
|
+
/** Whether the operation succeeded */
|
|
71
|
+
success: boolean;
|
|
72
|
+
/** The result if successful */
|
|
73
|
+
result?: T;
|
|
74
|
+
/** The final error if all attempts failed */
|
|
75
|
+
error?: Error;
|
|
76
|
+
/** Number of attempts made */
|
|
77
|
+
attempts: number;
|
|
78
|
+
/** Total time spent in milliseconds */
|
|
79
|
+
totalTime: number;
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Execute a function with retry logic
|
|
83
|
+
*/
|
|
84
|
+
declare function withRetry<T>(fn: () => Promise<T>, options?: Partial<RetryOptions>): Promise<T>;
|
|
85
|
+
/**
|
|
86
|
+
* Execute with retry and return detailed result
|
|
87
|
+
*/
|
|
88
|
+
declare function withRetryResult<T>(fn: () => Promise<T>, options?: Partial<RetryOptions>): Promise<RetryResult<T>>;
|
|
89
|
+
/**
|
|
90
|
+
* Create a retryable version of a function
|
|
91
|
+
*/
|
|
92
|
+
declare function retryable<TArgs extends unknown[], TResult>(fn: (...args: TArgs) => Promise<TResult>, options?: Partial<RetryOptions>): (...args: TArgs) => Promise<TResult>;
|
|
93
|
+
/**
|
|
94
|
+
* Common retry predicates
|
|
95
|
+
*/
|
|
96
|
+
declare const RetryPredicates: {
|
|
97
|
+
/**
|
|
98
|
+
* Retry on network errors
|
|
99
|
+
*/
|
|
100
|
+
networkErrors: (error: Error) => boolean;
|
|
101
|
+
/**
|
|
102
|
+
* Retry on HTTP 5xx errors
|
|
103
|
+
*/
|
|
104
|
+
serverErrors: (error: Error) => boolean;
|
|
105
|
+
/**
|
|
106
|
+
* Retry on HTTP 429 (rate limit) errors
|
|
107
|
+
*/
|
|
108
|
+
rateLimitErrors: (error: Error) => boolean;
|
|
109
|
+
/**
|
|
110
|
+
* Retry on database deadlock errors
|
|
111
|
+
*/
|
|
112
|
+
deadlockErrors: (error: Error) => boolean;
|
|
113
|
+
/**
|
|
114
|
+
* Never retry on authentication errors
|
|
115
|
+
*/
|
|
116
|
+
authenticationErrors: (error: Error) => boolean;
|
|
117
|
+
/**
|
|
118
|
+
* Never retry on validation errors
|
|
119
|
+
*/
|
|
120
|
+
validationErrors: (error: Error) => boolean;
|
|
121
|
+
};
|
|
122
|
+
/**
|
|
123
|
+
* Common retry configurations
|
|
124
|
+
*/
|
|
125
|
+
declare const RetryConfigs: {
|
|
126
|
+
/**
|
|
127
|
+
* Fast retry for quick failures
|
|
128
|
+
*/
|
|
129
|
+
fast: Partial<RetryOptions>;
|
|
130
|
+
/**
|
|
131
|
+
* Standard retry configuration
|
|
132
|
+
*/
|
|
133
|
+
standard: Partial<RetryOptions>;
|
|
134
|
+
/**
|
|
135
|
+
* Aggressive retry for critical operations
|
|
136
|
+
*/
|
|
137
|
+
aggressive: Partial<RetryOptions>;
|
|
138
|
+
/**
|
|
139
|
+
* Conservative retry for rate-limited APIs
|
|
140
|
+
*/
|
|
141
|
+
rateLimited: Partial<RetryOptions>;
|
|
142
|
+
/**
|
|
143
|
+
* Database retry configuration
|
|
144
|
+
*/
|
|
145
|
+
database: Partial<RetryOptions>;
|
|
146
|
+
};
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* Circuit Breaker Pattern Implementation
|
|
150
|
+
*
|
|
151
|
+
* Prevents cascading failures by temporarily blocking calls to a failing service.
|
|
152
|
+
*/
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* Circuit breaker states
|
|
156
|
+
*/
|
|
157
|
+
type CircuitState = "closed" | "open" | "half-open";
|
|
158
|
+
/**
|
|
159
|
+
* Options for circuit breaker behavior
|
|
160
|
+
*/
|
|
161
|
+
interface CircuitBreakerOptions {
|
|
162
|
+
/**
|
|
163
|
+
* Name of the circuit (for logging)
|
|
164
|
+
*/
|
|
165
|
+
name: string;
|
|
166
|
+
/**
|
|
167
|
+
* Number of failures before opening the circuit
|
|
168
|
+
* @default 5
|
|
169
|
+
*/
|
|
170
|
+
failureThreshold: number;
|
|
171
|
+
/**
|
|
172
|
+
* Time in milliseconds before attempting to close the circuit
|
|
173
|
+
* @default 30000
|
|
174
|
+
*/
|
|
175
|
+
resetTimeout: number;
|
|
176
|
+
/**
|
|
177
|
+
* Number of successful requests needed in half-open state to close the circuit
|
|
178
|
+
* @default 3
|
|
179
|
+
*/
|
|
180
|
+
successThreshold: number;
|
|
181
|
+
/**
|
|
182
|
+
* Time window in milliseconds for counting failures
|
|
183
|
+
* @default 60000
|
|
184
|
+
*/
|
|
185
|
+
failureWindow: number;
|
|
186
|
+
/**
|
|
187
|
+
* Function to determine if error should trip the circuit
|
|
188
|
+
* @default All errors trip the circuit
|
|
189
|
+
*/
|
|
190
|
+
shouldTrip?: (error: Error) => boolean;
|
|
191
|
+
/**
|
|
192
|
+
* Callback when circuit state changes
|
|
193
|
+
*/
|
|
194
|
+
onStateChange?: (from: CircuitState, to: CircuitState) => void;
|
|
195
|
+
/**
|
|
196
|
+
* Callback when circuit opens
|
|
197
|
+
*/
|
|
198
|
+
onOpen?: (failures: number, lastError: Error) => void;
|
|
199
|
+
/**
|
|
200
|
+
* Callback when circuit closes
|
|
201
|
+
*/
|
|
202
|
+
onClose?: () => void;
|
|
203
|
+
/**
|
|
204
|
+
* Callback when request is rejected due to open circuit
|
|
205
|
+
*/
|
|
206
|
+
onReject?: (state: CircuitState) => void;
|
|
207
|
+
/**
|
|
208
|
+
* Logger for circuit breaker events
|
|
209
|
+
*/
|
|
210
|
+
logger?: ILogger;
|
|
211
|
+
}
|
|
212
|
+
/**
|
|
213
|
+
* Default circuit breaker options
|
|
214
|
+
*/
|
|
215
|
+
declare const DEFAULT_CIRCUIT_BREAKER_OPTIONS: Omit<CircuitBreakerOptions, "name">;
|
|
216
|
+
/**
|
|
217
|
+
* Error thrown when circuit is open
|
|
218
|
+
*/
|
|
219
|
+
declare class CircuitOpenError extends Error {
|
|
220
|
+
readonly name = "CircuitOpenError";
|
|
221
|
+
readonly circuitName: string;
|
|
222
|
+
readonly state: CircuitState;
|
|
223
|
+
readonly nextAttemptAt: Date;
|
|
224
|
+
constructor(circuitName: string, state: CircuitState, resetTimeout: number);
|
|
225
|
+
}
|
|
226
|
+
/**
|
|
227
|
+
* Circuit breaker statistics
|
|
228
|
+
*/
|
|
229
|
+
interface CircuitBreakerStats {
|
|
230
|
+
state: CircuitState;
|
|
231
|
+
failures: number;
|
|
232
|
+
successes: number;
|
|
233
|
+
lastFailure?: Date;
|
|
234
|
+
lastSuccess?: Date;
|
|
235
|
+
lastStateChange?: Date;
|
|
236
|
+
totalRequests: number;
|
|
237
|
+
rejectedRequests: number;
|
|
238
|
+
}
|
|
239
|
+
/**
|
|
240
|
+
* Circuit breaker implementation
|
|
241
|
+
*/
|
|
242
|
+
declare class CircuitBreaker {
|
|
243
|
+
private state;
|
|
244
|
+
private failures;
|
|
245
|
+
private successes;
|
|
246
|
+
private failureTimestamps;
|
|
247
|
+
private lastFailure?;
|
|
248
|
+
private lastSuccess?;
|
|
249
|
+
private lastStateChange?;
|
|
250
|
+
private lastOpenedAt?;
|
|
251
|
+
private totalRequests;
|
|
252
|
+
private rejectedRequests;
|
|
253
|
+
private lastError?;
|
|
254
|
+
private readonly options;
|
|
255
|
+
private readonly logger;
|
|
256
|
+
constructor(options: CircuitBreakerOptions);
|
|
257
|
+
/**
|
|
258
|
+
* Execute a function with circuit breaker protection
|
|
259
|
+
*/
|
|
260
|
+
execute<T>(fn: () => Promise<T>): Promise<T>;
|
|
261
|
+
/**
|
|
262
|
+
* Check if circuit allows execution
|
|
263
|
+
*/
|
|
264
|
+
canExecute(): boolean;
|
|
265
|
+
/**
|
|
266
|
+
* Record a successful execution
|
|
267
|
+
*/
|
|
268
|
+
private onSuccess;
|
|
269
|
+
/**
|
|
270
|
+
* Record a failed execution
|
|
271
|
+
*/
|
|
272
|
+
private onFailure;
|
|
273
|
+
/**
|
|
274
|
+
* Transition to a new state
|
|
275
|
+
*/
|
|
276
|
+
private transitionTo;
|
|
277
|
+
/**
|
|
278
|
+
* Check if reset timeout has passed
|
|
279
|
+
*/
|
|
280
|
+
private shouldAttemptReset;
|
|
281
|
+
/**
|
|
282
|
+
* Get remaining time until reset attempt
|
|
283
|
+
*/
|
|
284
|
+
private getRemainingTimeout;
|
|
285
|
+
/**
|
|
286
|
+
* Get current circuit state
|
|
287
|
+
*/
|
|
288
|
+
getState(): CircuitState;
|
|
289
|
+
/**
|
|
290
|
+
* Get circuit breaker statistics
|
|
291
|
+
*/
|
|
292
|
+
getStats(): CircuitBreakerStats;
|
|
293
|
+
/**
|
|
294
|
+
* Manually reset the circuit to closed state
|
|
295
|
+
*/
|
|
296
|
+
reset(): void;
|
|
297
|
+
/**
|
|
298
|
+
* Manually trip the circuit to open state
|
|
299
|
+
*/
|
|
300
|
+
trip(): void;
|
|
301
|
+
/**
|
|
302
|
+
* Check if circuit is open
|
|
303
|
+
*/
|
|
304
|
+
isOpen(): boolean;
|
|
305
|
+
/**
|
|
306
|
+
* Check if circuit is closed
|
|
307
|
+
*/
|
|
308
|
+
isClosed(): boolean;
|
|
309
|
+
/**
|
|
310
|
+
* Check if circuit is half-open
|
|
311
|
+
*/
|
|
312
|
+
isHalfOpen(): boolean;
|
|
313
|
+
}
|
|
314
|
+
/**
|
|
315
|
+
* Create a circuit breaker
|
|
316
|
+
*/
|
|
317
|
+
declare function createCircuitBreaker(options: CircuitBreakerOptions): CircuitBreaker;
|
|
318
|
+
/**
|
|
319
|
+
* Circuit breaker registry for managing multiple circuits
|
|
320
|
+
*/
|
|
321
|
+
declare class CircuitBreakerRegistry {
|
|
322
|
+
private circuits;
|
|
323
|
+
private readonly logger;
|
|
324
|
+
private readonly defaultOptions;
|
|
325
|
+
constructor(options?: {
|
|
326
|
+
logger?: ILogger;
|
|
327
|
+
defaults?: Partial<CircuitBreakerOptions>;
|
|
328
|
+
});
|
|
329
|
+
/**
|
|
330
|
+
* Get or create a circuit breaker
|
|
331
|
+
*/
|
|
332
|
+
get(name: string, options?: Partial<CircuitBreakerOptions>): CircuitBreaker;
|
|
333
|
+
/**
|
|
334
|
+
* Execute with a named circuit breaker
|
|
335
|
+
*/
|
|
336
|
+
execute<T>(name: string, fn: () => Promise<T>, options?: Partial<CircuitBreakerOptions>): Promise<T>;
|
|
337
|
+
/**
|
|
338
|
+
* Get all circuit statistics
|
|
339
|
+
*/
|
|
340
|
+
getAllStats(): Record<string, CircuitBreakerStats>;
|
|
341
|
+
/**
|
|
342
|
+
* Reset all circuits
|
|
343
|
+
*/
|
|
344
|
+
resetAll(): void;
|
|
345
|
+
/**
|
|
346
|
+
* Clear all circuits
|
|
347
|
+
*/
|
|
348
|
+
clear(): void;
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
export { type CircuitBreakerOptions as C, DEFAULT_RETRY_OPTIONS as D, type RetryOptions as R, type RetryResult as a, withRetryResult as b, RetryPredicates as c, RetryConfigs as d, type CircuitState as e, DEFAULT_CIRCUIT_BREAKER_OPTIONS as f, CircuitOpenError as g, type CircuitBreakerStats as h, CircuitBreaker as i, createCircuitBreaker as j, CircuitBreakerRegistry as k, retryable as r, withRetry as w };
|