@felixgeelhaar/govee-api-client
Version:
Enterprise-grade TypeScript client library for the Govee Developer REST API
130 lines • 5.35 kB
JavaScript
import pino from 'pino';
import { GoveeDeviceRepository } from './infrastructure/GoveeDeviceRepository';
import { GoveeControlService } from './services/GoveeControlService';
import { RetryPolicy } from './infrastructure/retry';
export class GoveeClient {
constructor(config) {
this.validateConfig(config);
// Initialize logger (silent by default)
this.logger = config.logger ?? pino({ level: 'silent' });
// Initialize repository
const repositoryConfig = {
apiKey: config.apiKey,
logger: this.logger,
};
if (config.timeout !== undefined) {
repositoryConfig.timeout = config.timeout;
}
const repository = new GoveeDeviceRepository(repositoryConfig);
// Initialize control service
const serviceConfig = {
repository,
logger: this.logger,
};
if (config.rateLimit !== undefined) {
serviceConfig.rateLimit = config.rateLimit;
}
if (config.enableRetries !== undefined) {
serviceConfig.enableRetries = config.enableRetries;
}
if (config.retryPolicy !== undefined) {
serviceConfig.retryPolicy = config.retryPolicy;
}
this.controlService = new GoveeControlService(serviceConfig);
this.logger.info('GoveeClient initialized successfully');
}
validateConfig(config) {
if (!config.apiKey || typeof config.apiKey !== 'string' || config.apiKey.trim().length === 0) {
throw new Error('API key is required and must be a non-empty string');
}
if (config.timeout !== undefined &&
(!Number.isInteger(config.timeout) || config.timeout <= 0)) {
throw new Error('Timeout must be a positive integer');
}
if (config.rateLimit !== undefined &&
(!Number.isInteger(config.rateLimit) || config.rateLimit <= 0)) {
throw new Error('Rate limit must be a positive integer');
}
if (config.enableRetries !== undefined && typeof config.enableRetries !== 'boolean') {
throw new Error('enableRetries must be a boolean');
}
if (config.retryPolicy !== undefined) {
const validPolicies = ['development', 'testing', 'production', 'custom'];
if (!(config.retryPolicy instanceof RetryPolicy) &&
!validPolicies.includes(config.retryPolicy)) {
throw new Error('retryPolicy must be a RetryPolicy instance or one of: development, testing, production, custom');
}
}
}
// Device management methods
async getDevices() {
return this.controlService.getDevices();
}
async getDeviceState(deviceId, model) {
return this.controlService.getDeviceState(deviceId, model);
}
async getControllableDevices() {
return this.controlService.getControllableDevices();
}
async getRetrievableDevices() {
return this.controlService.getRetrievableDevices();
}
async findDeviceByName(deviceName) {
return this.controlService.findDeviceByName(deviceName);
}
async findDevicesByModel(model) {
return this.controlService.findDevicesByModel(model);
}
// Device control methods
async sendCommand(deviceId, model, command) {
return this.controlService.sendCommand(deviceId, model, command);
}
async turnOn(deviceId, model) {
return this.controlService.turnOn(deviceId, model);
}
async turnOff(deviceId, model) {
return this.controlService.turnOff(deviceId, model);
}
async setBrightness(deviceId, model, brightness) {
return this.controlService.setBrightness(deviceId, model, brightness);
}
async setColor(deviceId, model, color) {
return this.controlService.setColor(deviceId, model, color);
}
async setColorTemperature(deviceId, model, colorTemperature) {
return this.controlService.setColorTemperature(deviceId, model, colorTemperature);
}
// Convenience methods
async turnOnWithBrightness(deviceId, model, brightness) {
return this.controlService.turnOnWithBrightness(deviceId, model, brightness);
}
async turnOnWithColor(deviceId, model, color, brightness) {
return this.controlService.turnOnWithColor(deviceId, model, color, brightness);
}
async turnOnWithColorTemperature(deviceId, model, colorTemperature, brightness) {
return this.controlService.turnOnWithColorTemperature(deviceId, model, colorTemperature, brightness);
}
async isDeviceOnline(deviceId, model) {
return this.controlService.isDeviceOnline(deviceId, model);
}
async isDevicePoweredOn(deviceId, model) {
return this.controlService.isDevicePoweredOn(deviceId, model);
}
// Monitoring and debugging methods
getRateLimiterStats() {
return this.controlService.getRateLimiterStats();
}
getRetryMetrics() {
return this.controlService.getRetryMetrics();
}
getServiceStats() {
return this.controlService.getServiceStats();
}
resetRetryMetrics() {
return this.controlService.resetRetryMetrics();
}
isRetryEnabled() {
return this.controlService.isRetryEnabled();
}
}
//# sourceMappingURL=GoveeClient.js.map