UNPKG

node-easysms

Version:

EasySMS is an SMS sender for Node.js

247 lines (246 loc) 8.41 kB
'use strict'; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.EasySms = void 0; const axios_1 = __importDefault(require("axios")); const OrderStrategy_1 = require("./Strategies/OrderStrategy"); const RandomStrategy_1 = require("./Strategies/RandomStrategy"); const Config_1 = require("./Config"); const Gateway_1 = require("./Gateway"); const Message_1 = require("./Message"); const Messenger_1 = require("./Messenger"); const PhoneNumber_1 = require("./PhoneNumber"); const Utils_1 = require("./Support/Utils"); const TestGateway_1 = require("../Gateways/TestGateway"); const AliyunGateway_1 = require("../Gateways/AliyunGateway"); const TencentGateway_1 = require("../Gateways/TencentGateway"); const BaiduGateway_1 = require("../Gateways/BaiduGateway"); const QiniuGateway_1 = require("../Gateways/QiniuGateway"); const YunpianGateway_1 = require("../Gateways/YunpianGateway"); const JuheGateway_1 = require("../Gateways/JuheGateway"); const UroraGateway_1 = require("../Gateways/UroraGateway"); /** * EasySms */ class EasySms { constructor(config) { this.config = null; this.customCreators = {}; this.createdGateways = {}; this.supportGateways = { test: TestGateway_1.TestGateway, aliyun: AliyunGateway_1.AliyunGateway, tencent: TencentGateway_1.TencentGateway, baidu: BaiduGateway_1.BaiduGateway, qiniu: QiniuGateway_1.QiniuGateway, yunpian: YunpianGateway_1.YunpianGateway, juhe: JuheGateway_1.JuheGateway, urora: UroraGateway_1.UroraGateway, }; this.messenger = null; this.config = new Config_1.Config(config); } /** * 获取请求客户端 * @returns */ getHttpClient() { if (!this.httpClient) { this.httpClient = axios_1.default.create(); } return this.httpClient; } /** * 设置请求客户端 * @param instance * @returns */ setHttpClient(instance) { this.httpClient = instance; return this; } /** * 设置配置 * @param config * @returns */ setConfig(config) { this.config = new Config_1.Config(config); return this; } /** * 设置发送器 * @param messenger * @returns */ setMessenger(messenger) { this.messenger = messenger; this.messenger.setApp(this); return this; } /** * 获取发送器 * @returns */ getMessenger() { if (!this.messenger) { this.messenger = new Messenger_1.Messenger(); this.messenger.setApp(this); } return this.messenger; } /** * 扩展自定义网关 * @param name * @param func * @returns */ extend(name, func) { this.customCreators[name.toLowerCase()] = func; return this; } /** * 获取网关实例 * @param name 网关标识 * @returns */ gateway(name) { if (!this.createdGateways[name]) { this.createdGateways[name] = this.createGateway(name); this.createdGateways[name].setHttpClient(this.getHttpClient()); } return this.createdGateways[name]; } /** * 获取或设置网关策略,并返回可执行方法 * @param strategy * @returns */ strategy(strategy = null) { if (!strategy) { strategy = this.config.get('default.strategy', Messenger_1.Messenger.STRATEGY_ORDER); } else { this.config.set('default.strategy', strategy); } if (typeof strategy === 'function') { return strategy; } else if (strategy === Messenger_1.Messenger.STRATEGY_RANDOM) { return RandomStrategy_1.RandomStrategy; } else { return OrderStrategy_1.OrderStrategy; } } /** * 发送短信 * @param to 电话 * @param message 消息 * @param gateways 网关标识列表,默认去 default.gateways 配置 * @returns */ send(to_1, message_1) { return __awaiter(this, arguments, void 0, function* (to, message, gateways = null) { if (!to) { throw new Error('Empty number.'); } if (!message) { throw new Error('Empty message.'); } to = this.formatPhoneNumber(to); message = this.formatMessage(message); gateways = gateways !== null && gateways !== void 0 ? gateways : message.getGateways(); if (!gateways || gateways.length === 0) { gateways = this.config.get('default.gateways', []); } if (!gateways || gateways.length === 0) { throw new Error('Empty gateways.'); } let formattedGateways = this.formatGateways(gateways); return this.getMessenger().send(to, message, formattedGateways); }); } createGateway(name) { let config = this.config.get(`gateways.${name}`); if (!config) { if (name === 'test') { config = { gateway: 'test', }; } else { throw new Error('Invalid gateway: ' + name); } } if (typeof config.gateway == 'undefined') { config.gateway = name; } if (typeof config.timeout === 'undefined') { config.timeout = this.config.get('timeout', Gateway_1.Gateway.DEFAULT_TIMEOUT); } if (typeof config.gateway == 'string') { let gateway = config.gateway; if (typeof this.customCreators[gateway] != 'undefined') { return this.callCustomCreator(gateway, config); } if (typeof this.supportGateways[gateway] == 'undefined') { throw new Error('Invalid gateway: ' + gateway); } return this.buildGateway(this.supportGateways[gateway], config); } return this.buildGateway(config.gateway, config); } buildGateway(className, config) { return new className(config); } callCustomCreator(name, config) { if ((0, Utils_1.isGatewayConstructable)(this.customCreators[name])) { let className = this.customCreators[name]; return new className(config); } return this.customCreators[name](config); } formatPhoneNumber(number) { if (number instanceof PhoneNumber_1.PhoneNumber) { return number; } return new PhoneNumber_1.PhoneNumber(number.replace(/^\s+/, '').replace(/\s+$/, '')); } formatMessage(message) { if (!(message instanceof Message_1.Message)) { if (typeof message === 'string') { message = { content: message, template: message, }; } message = new Message_1.Message(message); } return message; } formatGateways(gateways) { let formatted = this.strategy().call(null, gateways); let results = []; let globalSettings = this.config.get('gateways', {}); for (let gateway of formatted) { if (gateway !== 'test' && typeof globalSettings[gateway] === 'undefined') continue; results.push(gateway); } return results; } } exports.EasySms = EasySms; ;