UNPKG

plivo

Version:

A Node.js SDK to make voice calls and send SMS using Plivo and to generate Plivo XML

282 lines (235 loc) 11.2 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); exports.HostedMessagingNumberInterface = exports.HostedMessagingNumber = exports.ListHostedMessagingNumberResponse = exports.CreateHostedMessagingNumberResponse = exports.HostedMessagingNumberResponse = undefined; var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); var _base = require('../base'); var _common = require('../utils/common.js'); function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; } function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } var clientKey = Symbol(); var action = 'HostedMessagingNumber/'; var idField = 'hosted_messaging_number_id'; var HostedMessagingNumberResponse = exports.HostedMessagingNumberResponse = function HostedMessagingNumberResponse(params) { _classCallCheck(this, HostedMessagingNumberResponse); params = params || {}; this.apiId = params.apiId; this.alias = params.alias; this.application = params.application; this.failureReason = params.failureReason; this.file = params.file; this.hostedMessagingNumberId = params.hostedMessagingNumberId; this.loaId = params.loaId; this.number = params.number; this.hostedStatus = params.hostedStatus; this.linkedNumbers = params.linkedNumbers; this.resourceUri = params.resourceUri; this.createdAt = params.createdAt; }; var CreateHostedMessagingNumberResponse = exports.CreateHostedMessagingNumberResponse = function CreateHostedMessagingNumberResponse(params) { _classCallCheck(this, CreateHostedMessagingNumberResponse); params = params || {}; this.apiId = params.apiId; this.alias = params.alias; this.application = params.application; this.failureReason = params.failureReason; this.file = params.file; this.hostedMessagingNumberId = params.hostedMessagingNumberId; this.loaId = params.loaId; this.number = params.number; this.hostedStatus = params.hostedStatus; this.linkedNumbers = params.linkedNumbers; this.resourceUri = params.resourceUri; this.message = params.message; this.createdAt = params.createdAt; }; var ListHostedMessagingNumberResponse = exports.ListHostedMessagingNumberResponse = function ListHostedMessagingNumberResponse(params) { _classCallCheck(this, ListHostedMessagingNumberResponse); params = params || {}; this.apiId = params.apiId; this.meta = params.metaResponse; this.objects = params.objects; }; var HostedMessagingNumber = exports.HostedMessagingNumber = function (_PlivoResource) { _inherits(HostedMessagingNumber, _PlivoResource); function HostedMessagingNumber(client) { var data = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; _classCallCheck(this, HostedMessagingNumber); var _this = _possibleConstructorReturn(this, (HostedMessagingNumber.__proto__ || Object.getPrototypeOf(HostedMessagingNumber)).call(this, action, HostedMessagingNumber, idField, client)); if (idField in data) { _this.id = data[idField]; } _this[clientKey] = client; (0, _common.extend)(_this, data); return _this; } /** * get HostedMessagingNumber by given id * @method * @param {string} id - id of the HostedMessagingNumber * @promise {object} return {@link HostedMessagingNumber} object * @fail {Error} return Error */ _createClass(HostedMessagingNumber, [{ key: 'get', value: function get(id) { var _this2 = this; var client = this[clientKey]; return new Promise(function (resolve, reject) { if (action !== '' && !id) { reject(new Error(_this2[idKey] + ' must be set')); } client('GET', action + (id ? id + '/' : '')).then(function (response) { var object = new HostedMessagingNumberResponse(response.body, client); Object.keys(object).forEach(function (key) { return object[key] === undefined && delete object[key]; }); resolve(object); }).catch(function (error) { reject(error); }); }); } /** * list all HostedMessagingNumber * @method * @param {object} params - params containing options to list HostedMessagingNumber by. * @param {string} [params.alias] - Alias * @param {string} [params.hostedStatus] - Hosted Status * @param {string} [params.number] - Phone Number * @param {string} [params.loaId] - LOA ID * @fail {Error} return Error */ }, { key: 'list', value: function list() { var params = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; var client = this[clientKey]; return new Promise(function (resolve, reject) { client('GET', action, params).then(function (response) { resolve(new ListHostedMessagingNumberResponse(response.body, idField)); }).catch(function (error) { reject(error); }); }); } /** * Create a HostedMessagingNumber * @method * @param {object} params * @param {string} [params.alias] - Alias * @param {string} [params.applicationId] - Application ID * @param {string} [params.number] - Phone Number * @param {string} [params.applicationId] - LOA ID * @fail {Error} return Error */ }, { key: 'create', value: function create() { var params = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; var client = this[clientKey]; return new Promise(function (resolve, reject) { client('POST', action, params).then(function (response) { var object = new CreateHostedMessagingNumberResponse(response.body, idField); Object.keys(object).forEach(function (key) { return object[key] === undefined && delete object[key]; }); resolve(object); }).catch(function (error) { reject(error); }); }); } }]); return HostedMessagingNumber; }(_base.PlivoResource); /** * Represents a HostedMessagingNumber interface * @constructor * @param {function} client - make api call * @param {object} [data] - data of call */ var HostedMessagingNumberInterface = exports.HostedMessagingNumberInterface = function (_PlivoResourceInterfa) { _inherits(HostedMessagingNumberInterface, _PlivoResourceInterfa); function HostedMessagingNumberInterface(client) { var data = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; _classCallCheck(this, HostedMessagingNumberInterface); var _this3 = _possibleConstructorReturn(this, (HostedMessagingNumberInterface.__proto__ || Object.getPrototypeOf(HostedMessagingNumberInterface)).call(this, action, HostedMessagingNumber, idField, client)); (0, _common.extend)(_this3, data); _this3[clientKey] = client; return _this3; } /** * get HostedMessagingNumber by given id * @method * @param {string} id - id of the HostedMessagingNumber * @promise {object} return {@link HostedMessagingNumber} object * @fail {Error} return Error */ _createClass(HostedMessagingNumberInterface, [{ key: 'get', value: function get(id) { var errors = (0, _common.validate)([{ field: 'id', value: id, validators: ['isRequired', 'isString'] }]); if (errors) { return errors; } return new HostedMessagingNumber(this[clientKey], { id: idField }).get(id); } /** * list all HostedMessagingNumber * @method * @param {object} params - params containing options to list HostedMessagingNumber by. * @param {string} [params.alias] - Alias * @param {string} [params.hostedStatus] - Hosted Status * @param {string} [params.number] - Phone Number * @param {string} [params.loaId] - LOA ID * @fail {Error} return Error */ }, { key: 'list', value: function list() { var params = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; var validations = []; if (params.hasOwnProperty("alias")) { validations.push({ field: 'alias', value: params.alias, validators: ['isString'] }); } if (params.hasOwnProperty("hostedStatus")) { validations.push({ field: 'hostedStatus', value: params.hostedStatus, validators: ['isString'] }); } if (params.hasOwnProperty("number")) { validations.push({ field: 'number', value: params.number, validators: ['isString'] }); } if (params.hasOwnProperty("loaId")) { validations.push({ field: 'loaId', value: params.loaId, validators: ['isString'] }); } var errors = (0, _common.validate)(validations); if (errors) { return errors; } return new HostedMessagingNumber(this[clientKey], { id: idField }).list(params); } /** * Create a HostedMessagingNumber * @method * @param {object} params * @param {string} [params.alias] - Alias * @param {string} [params.applicationId] - Application ID * @param {string} [params.number] - Phone Number * @param {string} [params.applicationId] - LOA ID * @fail {Error} return Error */ }, { key: 'create', value: function create() { var params = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; var errors = (0, _common.validate)([{ field: 'alias', value: params.alias, validators: ['isRequired', 'isString'] }, { field: 'applicationId', value: params.applicationId, validators: ['isRequired', 'isString'] }, { field: 'loaId', value: params.loaId, validators: ['isRequired', 'isString'] }, { field: 'number', value: params.number, validators: ['isRequired', 'isString'] }]); if (errors) { return errors; } return new HostedMessagingNumber(this[clientKey], { id: idField }).create(params); } }]); return HostedMessagingNumberInterface; }(_base.PlivoResourceInterface);