UNPKG

@mirad-work/sms-core

Version:

A framework-agnostic TypeScript SMS service core with provider abstraction for Iranian SMS providers

107 lines 3.75 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.SmsService = void 0; const sms_driver_factory_1 = require("./sms-driver-factory"); const sms_exceptions_1 = require("../exceptions/sms-exceptions"); /** * Main SMS service class - provides a high-level interface for SMS operations * This is the primary class that applications should use */ class SmsService { constructor(config, driverFactory) { this.validateConfig(config); this.config = config; this.driverFactory = driverFactory || new sms_driver_factory_1.SmsDriverFactory(config); } /** * Send a template-based SMS message (OTP, verification codes, etc.) */ async verify(message) { this.validateMessage(message); if (!message.template) { throw new sms_exceptions_1.MessageValidationException("Template is required for verify operation"); } if (!message.tokens) { throw new sms_exceptions_1.MessageValidationException("Tokens are required for verify operation"); } try { const driver = this.driverFactory.createDriver(message.driver); return await driver.verify(message); } catch (error) { throw this.handleError(error, "Send verification SMS"); } } /** * Get list of available drivers */ getAvailableDrivers() { return this.driverFactory.getAvailableDrivers(); } /** * Check if a specific driver is available */ isDriverAvailable(driverType) { return this.driverFactory.isDriverAvailable(driverType); } /** * Get the default driver type */ getDefaultDriver() { return this.config.defaultDriver; } /** * Create a template-based SMS message object */ createVerificationMessage(to, template, tokens, options = {}) { return { to, template, tokens, driver: options.driver, }; } /** * Validate SMS message */ validateMessage(message) { if (!message) { throw new sms_exceptions_1.MessageValidationException("Message is required"); } if (!message.to) { throw new sms_exceptions_1.MessageValidationException("Recipient phone number is required"); } if (!message.content && !message.template) { throw new sms_exceptions_1.MessageValidationException("Either content or template must be provided"); } if (message.template && !message.tokens) { throw new sms_exceptions_1.MessageValidationException("Tokens are required when using templates"); } } /** * Validate service configuration */ validateConfig(config) { if (!config) { throw new sms_exceptions_1.MessageValidationException("SMS configuration is required"); } if (!config.defaultDriver) { throw new sms_exceptions_1.MessageValidationException("Default driver is required"); } if (!config.drivers || Object.keys(config.drivers).length === 0) { throw new sms_exceptions_1.MessageValidationException("At least one driver configuration is required"); } } /** * Handle and standardize errors */ handleError(error, context) { if (error instanceof sms_exceptions_1.SmsException) { return error; } const errorMessage = error instanceof Error ? error.message : String(error); return new sms_exceptions_1.SmsException(`${context} failed: ${errorMessage}`, error, "SERVICE_ERROR"); } } exports.SmsService = SmsService; //# sourceMappingURL=sms-service.js.map