UNPKG

notification-service-sdk

Version:

A Node.js notification service SDK supporting email and SMS providers

119 lines 5.23 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.HuaweiSmsProvider = void 0; const interfaces_1 = require("../interfaces"); const crypto = __importStar(require("crypto")); const util = __importStar(require("util")); const querystring = __importStar(require("querystring")); const axios_1 = __importDefault(require("axios")); class HuaweiSmsProvider { constructor(config) { if (!config.endpoint || !config.accessKeyId || !config.accessKeySecret || !config.signName || !config.sender) { throw new interfaces_1.NotificationError('Invalid Huawei SMS configuration', 'INVALID_CONFIG'); } this.config = config; } buildWsseHeader(appKey, appSecret) { const time = new Date(Date.now()).toISOString().replace(/.[0-9]+\Z/, 'Z'); // Created const nonce = crypto.randomBytes(64).toString('hex'); // Nonce const passwordDigestBase64Str = crypto .createHash('sha256') .update(nonce + time + appSecret) .digest('base64'); // PasswordDigest return util.format('UsernameToken Username="%s",PasswordDigest="%s",Nonce="%s",Created="%s"', appKey, passwordDigestBase64Str, nonce, time); } buildRequestBody(message) { const { phoneNumbers, templateId, templateParams: templateParas, statusCallBack, signature } = message; const phones = Array.isArray(phoneNumbers) ? phoneNumbers.join(',') : phoneNumbers; if (signature && signature.length > 0) { return querystring.stringify({ from: this.config.sender, to: phones, templateId: templateId, templateParas: JSON.stringify(templateParas), statusCallback: statusCallBack, signature: signature, }); } return querystring.stringify({ from: this.config.sender, to: phones, templateId: templateId, templateParas: JSON.stringify(templateParas), statusCallback: statusCallBack, }); } async send(message) { var _a; try { if (!message.phoneNumbers) { throw new interfaces_1.NotificationError('Phone numbers are required', 'MISSING_PHONE_NUMBERS'); } if (!message.templateId) { throw new interfaces_1.NotificationError('Template ID is required', 'MISSING_TEMPLATE_ID'); } const requestBody = this.buildRequestBody(message); const headers = { 'Content-Type': 'application/x-www-form-urlencoded', Authorization: 'WSSE realm="SDP",profile="UsernameToken",type="Appkey"', 'X-WSSE': this.buildWsseHeader(this.config.accessKeyId, this.config.accessKeySecret), }; const response = await axios_1.default.post(this.config.endpoint, requestBody, { headers }); const responseData = response.data; if (response.status !== 200) { return { success: false, error: new interfaces_1.NotificationError(`Failed to send SMS: ${responseData.errorMsg}`, responseData.errorCode), }; } return { success: true, messageId: (_a = responseData.result) === null || _a === void 0 ? void 0 : _a.smsMsgId, }; } catch (error) { return { success: false, error: error instanceof Error ? error : new Error(String(error)), }; } } } exports.HuaweiSmsProvider = HuaweiSmsProvider; //# sourceMappingURL=huaweiSms.js.map