/** * A Circuit Breaker pattern implementation to prevent cascading failures * @class * @param {Object} options - Configuration options * @param {number} options.failureThreshold - Number of failures before opening the circuit * @param {number} options.resetTimeout - Timeout in ms before attempting to reset the circuit * @param {string} [options.state='closed'] - Initial state of the circuit breaker * @throws {Error} If failureThreshold or resetTimeout is not a positive number */ export class CircuitBreaker { /** * Creates an instance of CircuitBreaker * @constructor * @param {Object} options - Configuration options * @param {number} options.failureThreshold - Number of failures before opening the circuit * @param {number} options.resetTimeout - Timeout in ms before attempting to reset the circuit * @param {string} [options.state='closed'] - Initial state of the circuit breaker * @throws {Error} If failureThreshold or reset,Timeout is not a positive number */ constructor(options: { failureThreshold: number; resetTimeout: number; state?: string; }); failureThreshold: number; resetTimeout: number; state: string; failureCount: number; lastFailureTime: number; /** * Calls the provided function and manages circuit state * @param {Function} fn - Function to call * @returns {Promise} Result of the function call * @throws {Error} If circuit is open or if the function throws an error * @example * const breaker = new CircuitBreaker({ failureThreshold: 3, resetTimeout: 5000 }); * const result = await breaker.call(() => fetch('https://api.example.com/data')); * console.log(result); */ call(fn: Function): Promise; /** * Checks if the circuit can be closed based on the reset timeout * @returns {boolean} True if circuit can be closed, false otherwise */ canClose(): boolean; /** * Manually closes the circuit * @returns {void} */ close(): void; /** * Returns the current state of the circuit * @returns {string} Current state ('closed', 'open', or 'half-open') */ getState(): string; /** * Returns the number of failures recorded * @returns {number} Failure count */ getFailureCount(): number; }