UNPKG

@mirad-work/sms-core

Version:

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

118 lines 4.18 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.MelipayamakDriver = void 0; const base_driver_1 = require("./base-driver"); /** * Melipayamak SMS driver implementation * Implements the Iranian Melipayamak SMS API */ class MelipayamakDriver extends base_driver_1.BaseSmsDriver { constructor(config, httpClient) { super(config, httpClient); this.melipayamakConfig = config; } /** * Override buildUrl to append API key at the end of the URL */ buildUrl(endpoint) { const baseUrl = super.buildUrl(endpoint); return `${baseUrl}/${this.melipayamakConfig.apiKey}`; } /** * 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/shared"); const payload = this.buildVerifyPayload(message); this.log("info", "Sending verification SMS via Melipayamak", { to: message.to, template: message.template, }); const response = await this.handleHttpRequest(() => this.httpClient.post(url, payload, { headers: this.buildHeaders(), })); return this.processMelipayamakResponse(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 args = []; // Handle different token formats if (Array.isArray(message.tokens)) { // Positional tokens const tokens = message.tokens; tokens.forEach((token) => { if (token !== undefined) { args.push(String(token)); } }); } else { // Named tokens - convert to array based on common parameter names const tokens = message.tokens; // Common token names for Melipayamak const paramNames = [ "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", ]; // First try to get tokens in the expected order paramNames.forEach((name) => { if (tokens[name] !== undefined) { args.push(String(tokens[name])); } }); // If no standard names found, use all values if (args.length === 0) { Object.values(tokens).forEach((value) => { if (value !== undefined) { args.push(String(value)); } }); } } return { to: message.to, bodyId: message.template, args, }; } /** * Process Melipayamak API response and convert to standard format */ processMelipayamakResponse(response) { if (response.status === "ارسال موفق بود") { // Melipayamak uses RetStatus 1 for success const messageId = response.recId; // Message ID is in the Value field return this.createSuccessResponse(response, messageId); } else { const errorMessage = response.status || "Unknown error"; return this.createErrorResponse(errorMessage, `MELIPAYAMAK_${response.status}`, response); } } } exports.MelipayamakDriver = MelipayamakDriver; //# sourceMappingURL=melipayamak-driver.js.map