plivo
Version:
A Node.js SDK to make voice calls and send SMS using Plivo and to generate Plivo XML
519 lines (435 loc) • 21.7 kB
JavaScript
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.NumberInterface = exports.NumberResource = exports.PhoneNumberInterface = exports.PhoneNumber = exports.UpdateNumberResponse = exports.SearchNumberResponse = exports.BuyNumberResponse = undefined;
var _get = function get(object, property, receiver) { if (object === null) object = Function.prototype; var desc = Object.getOwnPropertyDescriptor(object, property); if (desc === undefined) { var parent = Object.getPrototypeOf(object); if (parent === null) { return undefined; } else { return get(parent, property, receiver); } } else if ("value" in desc) { return desc.value; } else { var getter = desc.get; if (getter === undefined) { return undefined; } return getter.call(receiver); } };
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 = 'Number/';
var idField = 'number';
var BuyNumberResponse = exports.BuyNumberResponse = function BuyNumberResponse(params, statusCode) {
_classCallCheck(this, BuyNumberResponse);
params = params || {};
this.apiId = params.apiId;
this.numbers = params.numbers;
this.status = params.status;
this.message = params.message;
if (params.fallbackNumber !== undefined) {
this.fallbackNumber = params.fallbackNumber;
}
this.statusCode = statusCode;
};
var SearchNumberResponse = exports.SearchNumberResponse = function SearchNumberResponse(params) {
_classCallCheck(this, SearchNumberResponse);
params = params || {};
this.number = params.number;
this.prefix = params.prefix;
this.city = params.city;
this.country = params.country;
this.region = params.region;
this.rate_center = params.rate_center;
this.lata = params.lata;
this.type = params.type;
this.sub_type = params.sub_type;
this.setup_rate = params.setup_rate;
this.monthly_rental_rate = params.monthly_rental_rate;
this.sms_enabled = params.sms_enabled;
this.sms_rate = params.sms_rate;
this.voice_enabled = params.voice_enabled;
this.voice_rate = params.voice_rate;
this.restriction = params.restriction;
this.restriction_text = params.restriction_text;
this.resource_uri = params.resource_uri;
};
var UpdateNumberResponse = exports.UpdateNumberResponse = function UpdateNumberResponse(params) {
_classCallCheck(this, UpdateNumberResponse);
params = params || {};
this.apiId = params.apiId;
this.message = params.message;
};
/**
* Represents a PhoneNumber
* @constructor
* @param {function} client - make api call
* @param {object} [data] - data of call
*/
var PhoneNumber = exports.PhoneNumber = function (_PlivoResource) {
_inherits(PhoneNumber, _PlivoResource);
function PhoneNumber(client) {
var data = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
_classCallCheck(this, PhoneNumber);
var _this = _possibleConstructorReturn(this, (PhoneNumber.__proto__ || Object.getPrototypeOf(PhoneNumber)).call(this, 'PhoneNumber/', PhoneNumber, idField, client));
if (idField in data) {
_this.id = data[idField];
}
(0, _common.extend)(_this, data);
_this[clientKey] = client;
return _this;
}
/**
* Buy Phone Number
* @method
* @param {string} appId - app id
* @param {string} cnamLookup - cnam lookup
* @param {boolean} haEnable - enable HA Number
* @param {string} complianceApplicationId - approved regulatory compliance application id to link to the number at purchase (required for regulated numbers such as India)
* @promise {@link PlivoGenericResponse} return PlivoGenericResponse Object if success
* @fail {Error} return Error
*/
_createClass(PhoneNumber, [{
key: 'buy',
value: function buy(number, appId, cnamLookup, haEnable, complianceApplicationId) {
return new PhoneNumberInterface(this[clientKey], {
id: this.id
}).buy(number, appId, cnamLookup, haEnable, complianceApplicationId);
}
}]);
return PhoneNumber;
}(_base.PlivoResource);
/**
* Represents a PhoneNumbers Interface
* @constructor
* @param {function} client - make api call
* @param {object} [data] - data of call
* @param {string} [data.test] - test data
*/
var PhoneNumberInterface = exports.PhoneNumberInterface = function (_PlivoResourceInterfa) {
_inherits(PhoneNumberInterface, _PlivoResourceInterfa);
function PhoneNumberInterface(client) {
var data = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
_classCallCheck(this, PhoneNumberInterface);
var _this2 = _possibleConstructorReturn(this, (PhoneNumberInterface.__proto__ || Object.getPrototypeOf(PhoneNumberInterface)).call(this, 'PhoneNumber/', PhoneNumber, idField, client));
(0, _common.extend)(_this2, data);
_this2[clientKey] = client;
return _this2;
}
/**
* Buy Phone Number
* @method
* @param {string} appId - app id
* @param {string} cnamLookup - cnam lookup
* @param {boolean} haEnable - enable HA Number
* @param {string} complianceApplicationId - approved regulatory compliance application id to link to the number at purchase (required for regulated numbers such as India)
* @promise {@link PlivoGenericResponse} return PlivoGenericResponse Object if success
* @fail {Error} return Error
*/
_createClass(PhoneNumberInterface, [{
key: 'buy',
value: function buy(number, appId, cnamLookup, haEnable, complianceApplicationId) {
var params = {};
if (appId) {
params.app_id = appId;
}
if (cnamLookup) {
params.cnam_lookup = cnamLookup;
}
if (haEnable !== undefined) {
params.ha_enable = haEnable;
}
if (complianceApplicationId) {
params.compliance_application_id = complianceApplicationId;
}
var client = this[clientKey];
return new Promise(function (resolve, reject) {
client('POST', 'PhoneNumber/' + number + '/', params).then(function (response) {
resolve(new BuyNumberResponse(response.body, response.response.status, idField));
}).catch(function (error) {
reject(error);
});
});
}
}]);
return PhoneNumberInterface;
}(_base.PlivoResourceInterface);
/**
* Represents a Number
* @constructor
* @param {function} client - make api call
* @param {object} [data] - data of call
*/
var NumberResource = exports.NumberResource = function (_PlivoResource2) {
_inherits(NumberResource, _PlivoResource2);
function NumberResource(client) {
var data = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
_classCallCheck(this, NumberResource);
var _this3 = _possibleConstructorReturn(this, (NumberResource.__proto__ || Object.getPrototypeOf(NumberResource)).call(this, action, NumberResource, idField, client));
if (idField in data) {
_this3.id = data[idField];
}
(0, _common.extend)(_this3, data);
_this3[clientKey] = client;
return _this3;
}
/**
* Unrent Number
* @method
* @promise {boolean} return true if success
* @fail {Error} return Error
*/
_createClass(NumberResource, [{
key: 'unrent',
value: function unrent(number) {
var client = this[clientKey];
var action = 'Number/';
return new Promise(function (resolve, reject) {
client('DELETE', action + number + '/').then(function () {
resolve(true);
}).catch(function (error) {
reject(error);
});
});
}
/**
* Update Number
* @method
* @param {object} params
* @param {string} [params.appId] - app id linked to the number
* @param {string} [params.subAccount] - auth_id of subaccount the number is transferred to
* @param {string} [params.alias] - textual name of the number
* @param {string} [params.complianceApplicationId] - approved regulatory compliance application id to link to the number (note: the public docs show this as `compliance_id` but the backend wire param is `compliance_application_id`)
* @param {string} [params.cnamLookup] - 'enabled' or 'disabled' (US numbers only)
* @param {string} [params.cnam] - caller ID name to use for outbound calls
* @param {string} [params.cnamCallbackUrl] - URL to notify when CNAM registration changes
* @param {string} [params.cnamCallbackMethod] - 'GET' or 'POST' for the CNAM callback
* @param {string} [params.callerReputation] - 'enabled' or 'disabled'
* @param {string} [params.profileUuid] - business profile uuid to associate with the number
* @param {string} [params.callerReputationCallbackUrl] - URL to notify on caller reputation status changes
* @param {string} [params.callerReputationCallbackMethod] - 'GET' or 'POST' for the caller reputation callback
* @promise {@link NumberResource} return NumberResource Object if success
* @fail {Error} return Error
*/
}, {
key: 'update',
value: function update(number) {
var params = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
var client = this[clientKey];
var action = 'Number/';
var that = this;
// lodash snakeCase would convert subAccount -> sub_account, but the API expects "subaccount".
var apiParams = Object.assign({}, params);
if (apiParams.subAccount) {
apiParams.subaccount = apiParams.subAccount;
delete apiParams.subAccount;
}
return new Promise(function (resolve, reject) {
client('POST', action + number + '/', apiParams).then(function (response) {
(0, _common.extend)(that, response.body);
if (params.hasOwnProperty('isVoiceRequest')) {
delete params.isVoiceRequest;
}
(0, _common.extend)(that, params);
resolve(new UpdateNumberResponse(that));
}).catch(function (error) {
reject(error);
});
});
}
}]);
return NumberResource;
}(_base.PlivoResource);
/**
* Represents a Numbers
* @constructor
* @param {function} client - make api call
* @param {object} [data] - data of call
*/
var NumberInterface = exports.NumberInterface = function (_PlivoResourceInterfa2) {
_inherits(NumberInterface, _PlivoResourceInterfa2);
function NumberInterface(client) {
_classCallCheck(this, NumberInterface);
var _this4 = _possibleConstructorReturn(this, (NumberInterface.__proto__ || Object.getPrototypeOf(NumberInterface)).call(this, action, NumberResource, idField, client));
_this4[clientKey] = client;
return _this4;
}
/**
* Buy Phone Number
* @method
* @param {string} number - number to buy
* @param {string} appId - app id
* @param {string} cnamLookup - cnam lookup
* @param {boolean} haEnable - enable HA Number
* @param {string} complianceApplicationId - approved regulatory compliance application id to link to the number at purchase (required for regulated numbers such as India)
* @promise {@link PlivoGenericResponse} return PlivoGenericResponse Object if success
* @fail {Error} return Error
*/
_createClass(NumberInterface, [{
key: 'buy',
value: function buy(number, appId, cnamLookup, haEnable, complianceApplicationId) {
var errors = (0, _common.validate)([{
field: 'number',
value: number,
validators: ['isRequired']
}]);
if (errors) {
return errors;
}
return new PhoneNumber(this[clientKey], {
id: number
}).buy(number, appId, cnamLookup, haEnable, complianceApplicationId);
}
/**
* Add own number from carrier
* @method
* @param {string} numbers - A comma separated list of numbers that need to be added for the carrier.
* @param {string} carrier - The carrier_id of the IncomingCarrier that the number is associated with.
* @param {string} region - region that is associated with the Number
* @param {string} optionaParams - optional params
* @promise {@link PlivoGenericResponse} return PlivoGenericResponse Object if success
* @fail {Error} return Error
*/
}, {
key: 'addOwnNumber',
value: function addOwnNumber(numbers, carrier, region, optionalParams) {
var errors = (0, _common.validate)([{
field: 'numbers',
value: numbers,
validators: ['isRequired']
}, {
field: 'carrier',
value: carrier,
validators: ['isRequired']
}, {
field: 'region',
value: region,
validators: ['isRequired']
}]);
if (errors) {
return errors;
}
var params = optionalParams || {};
params.numbers = numbers;
params.carrier = carrier;
params.region = region;
var client = this[clientKey];
return new Promise(function (resolve, reject) {
client('POST', action, params).then(function (response) {
resolve(new _base.PlivoGenericResponse(response.body, idField));
}).catch(function (error) {
reject(error);
});
});
}
/**
* Add own number from carrier
* @method
* @param {string} countryISO - The ISO code A2 of the country
* @param {string} optionaParams - optional params
* @promise {@link PhoneNumberInterface} return PhoneNumbers Object if success
* @fail {Error} return Error
*/
}, {
key: 'search',
value: function search(countryISO, optionalParams) {
var errors = (0, _common.validate)([{
field: 'country_iso',
value: countryISO,
validators: ['isRequired']
}]);
if (errors) {
return errors;
}
var params = optionalParams || {};
params.country_iso = countryISO;
return new PhoneNumberInterface(this[clientKey]).list(params);
}
/**
* List rented Account Phone Numbers.
* @method
* @param {object} [params]
* @param {string} [params.type] - filter by number category: 'local', 'mobile', 'fixed', 'national', 'tollfree'
* @param {string} [params.numberStartswith] - filter numbers whose digits begin with this prefix
* @param {string} [params.subAccount] - auth_id of subaccount to filter by
* @param {string} [params.alias] - exact alias match
* @param {string} [params.services] - filter by capability: 'voice', 'sms', 'mms' (comma-separated combinations allowed)
* @param {string} [params.cnamLookup] - 'enabled' or 'disabled'
* @param {string} [params.renewal_date] - filter by exact renewal date (YYYY-MM-DD)
* @param {string} [params.renewal_date__lt] - filter numbers with renewal_date before this date
* @param {string} [params.renewal_date__lte] - filter numbers with renewal_date on or before this date
* @param {string} [params.renewal_date__gt] - filter numbers with renewal_date after this date
* @param {string} [params.renewal_date__gte] - filter numbers with renewal_date on or after this date
* @param {string} [params.tendlcRegistrationStatus] - 'UNREGISTERED', 'PROCESSING', or 'COMPLETED'
* @param {string} [params.tendlcCampaignId] - filter by linked 10DLC campaign id
* @param {string} [params.tollFreeSmsVerification] - 'UNVERIFIED', 'PENDING_VERIFICATION', or 'VERIFIED'
* @param {number} [params.limit] - results per page (max 20)
* @param {number} [params.offset] - pagination offset
* @promise {@link NumberResource}[] return list of NumberResource Objects if success
* @fail {Error} return Error
*/
}, {
key: 'list',
value: function list(params) {
var apiParams = Object.assign({}, params);
// lodash snakeCase would convert subAccount -> sub_account, but the API expects "subaccount".
if (apiParams.subAccount) {
apiParams.subaccount = apiParams.subAccount;
delete apiParams.subAccount;
}
return _get(NumberInterface.prototype.__proto__ || Object.getPrototypeOf(NumberInterface.prototype), 'list', this).call(this, apiParams);
}
/**
* Update Number
* @method
* @param {string} number - number to update
* @param {object} params
* @param {string} [params.appId] - app id linked to the number
* @param {string} [params.subAccount] - auth_id of subaccount the number is transferred to
* @param {string} [params.alias] - textual name of the number
* @param {string} [params.complianceApplicationId] - approved regulatory compliance application id to link to the number (note: the public docs show this as `compliance_id` but the backend wire param is `compliance_application_id`)
* @param {string} [params.cnamLookup] - 'enabled' or 'disabled' (US numbers only)
* @param {string} [params.cnam] - caller ID name to use for outbound calls
* @param {string} [params.cnamCallbackUrl] - URL to notify when CNAM registration changes
* @param {string} [params.cnamCallbackMethod] - 'GET' or 'POST' for the CNAM callback
* @param {string} [params.callerReputation] - 'enabled' or 'disabled'
* @param {string} [params.profileUuid] - business profile uuid to associate with the number
* @param {string} [params.callerReputationCallbackUrl] - URL to notify on caller reputation status changes
* @param {string} [params.callerReputationCallbackMethod] - 'GET' or 'POST' for the caller reputation callback
* @promise {@link NumberResource} return NumberResource Object if success
* @fail {Error} return Error
*/
}, {
key: 'update',
value: function update(number) {
var params = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
var errors = (0, _common.validate)([{
field: 'number',
value: number,
validators: ['isRequired']
}]);
if (errors) {
return errors;
}
return new NumberResource(this[clientKey], {
id: number
}).update(number, params);
}
/**
* Unrent Number
* @method
* @param {string} number - number to unrent
* @promise {boolean} return true if success
* @fail {Error} return Error
*/
}, {
key: 'unrent',
value: function unrent(number) {
var errors = (0, _common.validate)([{
field: 'number',
value: number,
validators: ['isRequired']
}]);
if (errors) {
return errors;
}
return new NumberResource(this[clientKey], {
id: number
}).unrent(number);
}
}]);
return NumberInterface;
}(_base.PlivoResourceInterface);