UNPKG

messageway

Version:
137 lines (136 loc) 4.64 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.MessageWay = exports.isMessageWayError = void 0; const got_1 = require("got"); const types_1 = require("./types"); const MESSAGE_WAY_HOST = 'https://api.msgway.com'; /** * Check whether an error is a MessageWay standard error or not<br> * These objects have two fields: `code` and `message` * @param error The error object to check * @returns `error` is a standard MessageWay error */ function isMessageWayError(error) { return !!(error && error.code && error.message && error.constructor === Object); } exports.isMessageWayError = isMessageWayError; class MessageWay { /** * @param apiKey Your API key in MsgWay.com. * @param manual Generate OTP code automatically or you gonna to enter it manually. <br>Default is `false`. * @param language Target language for showing error messages. */ constructor(apiKey, manual = false, language = 'fa-IR') { this.apiKey = apiKey; this.language = language; this.sendSMS = this.sendSMS.bind(this); this.sendIVR = this.sendIVR.bind(this); this.sendGapMessage = this.sendGapMessage.bind(this); this.verify = this.verify.bind(this); this.getStatus = this.getStatus.bind(this); this.getBalance = this.getBalance.bind(this); } request(path, body) { let language = this.language; if ('language' in body) { language = body.language; delete body.language; } return (0, got_1.default)(MESSAGE_WAY_HOST + path, { headers: { 'accept-language': language, 'content-type': 'application/json', apiKey: this.apiKey }, method: 'post', responseType: 'text', body: JSON.stringify(body), }) .then(response => { let result; try { result = JSON.parse(response.body); } catch (err) { return Promise.reject(err); } if (result.error) { return Promise.reject(result); } delete result.status; delete result.error; return result; }) .catch((error) => { if (error.response) { try { error = JSON.parse(error.response.body); } catch (error) { } } if (error.status === 'error' && isMessageWayError(error.error)) { return Promise.reject(error.error); } return Promise.reject(error); }); } send(method, options) { const body = Object.assign({ method }, options); return this.request('/send', body) .then(result => result.referenceID); } /** * Send OTP Code via SMS * @param options Send options * @returns Reference ID */ sendSMS(options) { return this.send('sms', options); } /** * Send OTP Code via IVR (Interactive voice response) * @param options Send options * @returns Reference ID */ sendIVR(options) { return this.send('ivr', options); } /** * Send OTP Code via Gap Messenger ([gap.im](https://gap.im)) * @returns Reference ID */ sendGapMessage(options) { return this.send('messenger', Object.assign(Object.assign({}, options), { provider: types_1.MessengerProvider.Gap })); } /** * Send OTP Code via WhatsApp Messenger ([whatsapp.com](https://www.whatsapp.com)) * @returns Reference ID */ sendWhatsAppMessage(options) { return this.send('messenger', Object.assign(Object.assign({}, options), { provider: types_1.MessengerProvider.WhatsApp })); } /** * Verify the user entered code. * @returns If the code is correct the promise resolves. */ verify(options) { return this.request('/otp/verify', options) .then(() => { }); } /** * Get status of the sent OTP. * @param referenceID */ getStatus(options) { return this.request('/status', options); } /** * Get account balance. * @returns A promise that resolves to the account balance as a number. * @author amirmm4d */ getBalance() { return this.request('/balance/get', {}); } } exports.MessageWay = MessageWay;