UNPKG

@mirad-work/sms-core

Version:

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

97 lines 3.38 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.SmsIrDriver = void 0; const base_driver_1 = require("./base-driver"); /** * SMS.ir SMS driver implementation * Implements the Iranian SMS.ir API */ class SmsIrDriver extends base_driver_1.BaseSmsDriver { constructor(config, httpClient) { super(config, httpClient); this.smsIrConfig = config; } /** * Send a template-based SMS message (OTP/verification) */ async verify(message) { this.validateMessage(message); if (!message.template) { return this.createErrorResponse("Template is required for verify operation", "MISSING_TEMPLATE"); } if (!message.tokens) { return this.createErrorResponse("Tokens are required for verify operation", "MISSING_TOKENS"); } try { const url = this.buildUrl("send/verify"); const payload = this.buildVerifyPayload(message); this.log("info", "Sending verification SMS via SMS.ir", { to: message.to, template: message.template, }); const response = await this.handleHttpRequest(() => this.httpClient.post(url, payload, { headers: this.buildHeaders({ "X-API-KEY": this.smsIrConfig.apiKey, }), })); return this.processSmsIrResponse(response.data); } catch (error) { const err = error; this.log("error", "SMS verify failed", { error: err.message }); return this.createErrorResponse(err.message, "VERIFY_FAILED"); } } /** * Build payload for verify/OTP requests */ buildVerifyPayload(message) { const parameters = []; // Handle different token formats if (Array.isArray(message.tokens)) { // Positional tokens const tokens = message.tokens; tokens.forEach((token, index) => { if (token !== undefined) { parameters.push({ name: `Parameter${index + 1}`, value: String(token), }); } }); } else { // Named tokens const tokens = message.tokens; Object.entries(tokens).forEach(([name, value]) => { if (value !== undefined) { parameters.push({ name, value: String(value), }); } }); } return { mobile: message.to, templateId: message.template, parameters, }; } /** * Process SMS.ir API response and convert to standard format */ processSmsIrResponse(response) { if (response.status === 1) { // SMS.ir uses status 1 for success const messageId = response.data?.messageId; return this.createSuccessResponse(response, messageId); } else { const errorMessage = response.message || "Unknown error"; return this.createErrorResponse(errorMessage, `SMSIR_${response.status}`, response); } } } exports.SmsIrDriver = SmsIrDriver; //# sourceMappingURL=smsir-driver.js.map