UNPKG

@felixgeelhaar/govee-api-client

Version:

Enterprise-grade TypeScript client library for the Govee Developer REST API

142 lines 4.79 kB
import { RetryExecutorFactory, } from './RetryableRequest'; /** * Repository wrapper that adds retry functionality to any IGoveeDeviceRepository implementation */ export class RetryableRepository { constructor(config) { this.requestIdCounter = 0; this.repository = config.repository; this.retryExecutor = config.retryExecutor || RetryExecutorFactory.createForGoveeApi(config.logger); this.logger = config.logger; this.enableRequestIds = config.enableRequestIds ?? true; } /** * Generates a unique request ID */ generateRequestId(operation) { if (!this.enableRequestIds) { return `${operation}-${Date.now()}`; } return `${operation}-${++this.requestIdCounter}-${Date.now()}`; } /** * Finds all devices with retry logic */ async findAll() { const request = { id: this.generateRequestId('findAll'), description: 'Fetch all Govee devices', execute: () => this.repository.findAll(), context: { operation: 'findAll', timestamp: new Date().toISOString(), }, }; return this.retryExecutor.execute(request); } /** * Finds device state with retry logic */ async findState(deviceId, sku) { const request = { id: this.generateRequestId('findState'), description: `Fetch state for device ${deviceId} (${sku})`, execute: () => this.repository.findState(deviceId, sku), context: { operation: 'findState', deviceId, sku, timestamp: new Date().toISOString(), }, }; return this.retryExecutor.execute(request); } /** * Sends command with retry logic */ async sendCommand(deviceId, sku, command) { const request = { id: this.generateRequestId('sendCommand'), description: `Send ${command.toObject().name} command to device ${deviceId} (${sku})`, execute: () => this.repository.sendCommand(deviceId, sku, command), context: { operation: 'sendCommand', deviceId, sku, command: command.toObject(), timestamp: new Date().toISOString(), }, }; return this.retryExecutor.execute(request); } /** * Gets detailed retry result for findAll operation */ async findAllWithRetryResult() { const request = { id: this.generateRequestId('findAll'), description: 'Fetch all Govee devices (with retry details)', execute: () => this.repository.findAll(), context: { operation: 'findAll', timestamp: new Date().toISOString(), }, }; return this.retryExecutor.executeWithResult(request); } /** * Gets detailed retry result for findState operation */ async findStateWithRetryResult(deviceId, sku) { const request = { id: this.generateRequestId('findState'), description: `Fetch state for device ${deviceId} (${sku}) with retry details`, execute: () => this.repository.findState(deviceId, sku), context: { operation: 'findState', deviceId, sku, timestamp: new Date().toISOString(), }, }; return this.retryExecutor.executeWithResult(request); } /** * Gets detailed retry result for sendCommand operation */ async sendCommandWithRetryResult(deviceId, sku, command) { const request = { id: this.generateRequestId('sendCommand'), description: `Send ${command.toObject().name} command to device ${deviceId} (${sku}) with retry details`, execute: () => this.repository.sendCommand(deviceId, sku, command), context: { operation: 'sendCommand', deviceId, sku, command: command.toObject(), timestamp: new Date().toISOString(), }, }; return this.retryExecutor.executeWithResult(request); } /** * Gets current retry metrics */ getRetryMetrics() { return this.retryExecutor.getMetrics(); } /** * Resets retry metrics */ resetRetryMetrics() { this.retryExecutor.resetMetrics(); } /** * Gets the underlying repository (for direct access when needed) */ getUnderlyingRepository() { return this.repository; } } //# sourceMappingURL=RetryableRepository.js.map