diff --git a/lib/base.js b/lib/base.js index 98d0f49..8049673 100644 --- a/lib/base.js +++ b/lib/base.js @@ -1,4 +1,6 @@ -import { extend } from './utils/common.js'; +import { + extend +} from './utils/common.js'; let actionKey = Symbol('api action'); let klassKey = Symbol('constructor'); @@ -7,182 +9,187 @@ let clientKey = Symbol('make api call'); export class PlivoGenericResponse { constructor(params, idString) { - params = params || {}; - if (typeof idString !== 'undefined' && (idString in params)) { - this.id = params[idString]; - } else if ('request_uuid' in params) { - this.id = params.request_uuid; - } - extend(this, params); + params = params || {}; + if (typeof idString !== 'undefined' && (idString in params)) { + this.id = params[idString]; + } else if ('request_uuid' in params) { + this.id = params.request_uuid; + } + extend(this, params); } } export class PlivoResource { constructor(action, klass, idField, request) { - this[actionKey] = action; - this[klassKey] = klass; - this[idKey] = idField; - this[clientKey] = request; + this[actionKey] = action; + this[klassKey] = klass; + this[idKey] = idField; + this[clientKey] = request; } update(params, id) { - let client = this[clientKey]; - let action = this[actionKey]; - let that = this; - id = typeof id !== 'undefined' ? id : that.id; + let client = this[clientKey]; + let action = this[actionKey]; + let that = this; + id = typeof id !== 'undefined' ? id : that.id; - return new Promise((resolve, reject) => { - client('POST', action + id + '/', params) - .then(response => { - extend(that, response.body); - if (params.hasOwnProperty('isVoiceRequest')){ - delete params.isVoiceRequest; - } - extend(that, params); - resolve(that); - }) - .catch(error => { - reject(error); - }); - }); + return new Promise((resolve, reject) => { + client('POST', action + id + '/', params) + .then(response => { + extend(that, response.body); + if (params.hasOwnProperty('isVoiceRequest')) { + delete params.isVoiceRequest; + } + extend(that, params); + resolve(that); + }) + .catch(error => { + reject(error); + }); + }); } delete(params) { - let client = this[clientKey]; - let action = this[actionKey]; - let id = this.id; + let client = this[clientKey]; + let action = this[actionKey]; + let id = this.id; - return new Promise((resolve, reject) => { - client('DELETE', action + id + '/', params) - .then(() => { - resolve(true); - }) - .catch(error => { - reject(error); - }); - }); + return new Promise((resolve, reject) => { + client('DELETE', action + id + '/', params) + .then(() => { + resolve(true); + }) + .catch(error => { + reject(error); + }); + }); } executeAction(task = '', method = 'GET', params = {}, action) { - let client = this[clientKey]; - action = action == null ? this[actionKey] : action; - let idField = this[idKey]; + let client = this[clientKey]; + action = action == null ? this[actionKey] : action; + let idField = this[idKey]; - return new Promise((resolve, reject) => { - client(method, action + task, params) - .then(response => { - resolve(new PlivoGenericResponse(response.body, idField)); - }) - .catch(error => { - reject(error); - }); - }); + return new Promise((resolve, reject) => { + client(method, action + task, params) + .then(response => { + resolve(new PlivoGenericResponse(response.body, idField)); + }) + .catch(error => { + reject(error); + }); + }); } + + customexecuteAction(url, method = 'GET', params = {}) { - let client = this[clientKey]; - let idField = this[idKey]; - return new Promise((resolve, reject) => { - client(method, url, params) - .then(response => { - resolve(new PlivoGenericResponse(response.body, idField)); - }) - .catch(error => { - reject(error); - }); - }); + let client = this[clientKey]; + let idField = this[idKey]; + return new Promise((resolve, reject) => { + client(method, url, params) + .then(response => { + resolve(new PlivoGenericResponse(response.body, idField)); + }) + .catch(error => { + reject(error); + }); + }); } customexecuteGetNumberAction(url, method = 'GET', params = {}) { - let client = this[clientKey]; - let idField = this[idKey]; - let number = ""; - let promise = client(method, url, params).then(response => { - number = response.body.objects[0].number; - return number; - }).catch(error => { - console.log(error); - return error; - }); - return promise; + let client = this[clientKey]; + let idField = this[idKey]; + let number = ""; + let promise = client(method, url, params).then(response => { + number = response.body.objects[0].number; + return number; + }).catch(error => { + console.log(error); + return error; + }); + return promise; } getMetaResponse(url, method = 'GET', params = {}) { - let client = this[clientKey]; - let idField = this[idKey]; - let count = 0; - return new Promise((resolve, reject) => { - client(method, url, params) - .then(response => { - count = response.body.meta.totalCount; - resolve(count); - }) - .catch(error => { - reject(error); - }); - }); + let client = this[clientKey]; + let idField = this[idKey]; + let count = 0; + return new Promise((resolve, reject) => { + client(method, url, params) + .then(response => { + count = response.body.meta.totalCount; + resolve(count); + }) + .catch(error => { + reject(error); + }); + }); } } export class PlivoResourceInterface { constructor(action, klass, idField, request) { - this[actionKey] = action; - this[klassKey] = klass; - this[idKey] = idField; - this[clientKey] = request; + this[actionKey] = action; + this[klassKey] = klass; + this[idKey] = idField; + this[clientKey] = request; } get(id, params = {}) { - let client = this[clientKey]; - let action = this[actionKey]; - let Klass = this[klassKey]; + let client = this[clientKey]; + let action = this[actionKey]; + let Klass = this[klassKey]; - return new Promise((resolve, reject) => { - if (action !== '' && !id) { - reject(new Error(this[idKey] + ' must be set')); - } + return new Promise((resolve, reject) => { + if (action !== '' && !id) { + reject(new Error(this[idKey] + ' must be set')); + } - client('GET', action + (id ? id + '/' : ''), params) - .then(response => { - resolve(new Klass(client, response.body)); - }) - .catch(error => { - reject(error); - }); - }); + client('GET', action + (id ? id + '/' : ''), params) + .then(response => { + resolve(new Klass(client, response.body)); + }) + .catch(error => { + reject(error); + }); + }); } list(params) { - let client = this[clientKey]; - let action = this[actionKey]; - let Klass = this[klassKey]; + let client = this[clientKey]; + let action = this[actionKey]; + let Klass = this[klassKey]; - return new Promise((resolve, reject) => { - client('GET', action, params) - .then(response => { - let objects = []; - Object.defineProperty(objects, 'meta', { value: response.body.meta, enumerable: true }); - response.body.objects.forEach(item => { - objects.push(new Klass(client, item)); - }); - resolve(objects); - }) - .catch(error => { - reject(error); - }); - }); + return new Promise((resolve, reject) => { + client('GET', action, params) + .then(response => { + let objects = []; + Object.defineProperty(objects, 'meta', { + value: response.body.meta, + enumerable: true + }); + response.body.objects.forEach(item => { + objects.push(new Klass(client, item)); + }); + resolve(objects); + }) + .catch(error => { + reject(error); + }); + }); } create(params) { - let client = this[clientKey]; - let idField = this[idKey]; - let action = this[actionKey] + (this.id ? this.id + '/' : ''); + let client = this[clientKey]; + let idField = this[idKey]; + let action = this[actionKey] + (this.id ? this.id + '/' : ''); - return new Promise((resolve, reject) => { - client('POST', action, params) - .then(response => { - resolve(new PlivoGenericResponse(response.body, idField)); - }) - .catch(error => { - reject(error); - }); - }); + return new Promise((resolve, reject) => { + client('POST', action, params) + .then(response => { + resolve(new PlivoGenericResponse(response.body, idField)); + }) + .catch(error => { + reject(error); + }); + }); } -} +} \ No newline at end of file diff --git a/lib/resources/accounts.js b/lib/resources/accounts.js index c40b9e7..da8a22c 100644 --- a/lib/resources/accounts.js +++ b/lib/resources/accounts.js @@ -1,8 +1,10 @@ -import {extend, validate} from '../utils/common.js'; import {PlivoResource, PlivoResourceInterface} from '../base'; +import {extend, validate} from '../utils/common.js'; + +import { PlivoGenericResponse } from '../../dist/base'; const clientKey = Symbol(); -const action = ''; +const action = 'Subaccount/'; const idField = 'authId'; /** @@ -11,10 +13,77 @@ const idField = 'authId'; * @param {function} client - make api call * @param {object} [data] - data of call */ + +export class GetAccountDetails +{ + constructor(params) { + params = params || {}; + this.accountType= params.accountType; + this.address = params.address; + this.apiId = params.apiId; + this.autoRecharge = params.autoRecharge; + this.billingMode = params.billingMode; + this.cashCredits = params.cashCredits; + this.city = params.city; + this.name = params.name; + this.resourceUri = params.resourceUri; + this.state = params.state; + this.timezone = params.timezone; + } + +} + +export class CreateSubAccountResponse +{ + constructor(params) { + params = params || {}; + this.apiId= params.apiId; + this.authId = params.authId; + this.authToken = params.authToken; + this.message = params.message; + } + +} + +export class UpdateSubAccountDetails +{ + constructor(params) { + params = params || {}; + this.apiId= params.apiId; + this.message = params.message; + } + +} + +export class UpdateAccountDetailsResponse +{ + constructor(params) { + params = params || {}; + this.apiId= params.apiId; + this.message = params.message; + } + +} + +export class GetSubAccountDetails { + constructor(params) { + params = params || {}; + this.account= params.account; + this.apiId = params.apiId; + this.authId = params.authId; + this.authToken = params.authToken; + this.created = params.created; + this.enabled = params.enabled; + this.modified = params.modified; + this.name = params.name; + this.resourceUri = params.resourceUri; + + } +} export class Subaccount extends PlivoResource { constructor(client, data = {}) { super('Subaccount/', Subaccount, idField, client); - + this[clientKey] = client; if (idField in data) { this.id = data[idField]; } @@ -47,7 +116,22 @@ export class Subaccount extends PlivoResource { params.enabled = enabled.toString(); } - return super.update(params); + let client = this[clientKey]; + let that = this; + return new Promise((resolve, reject) => { + client('POST', action + that.id + '/', params) + .then(response => { + extend(that, response.body); + if (params.hasOwnProperty('isVoiceRequest')){ + delete params.isVoiceRequest; + } + extend(that, params); + resolve(new UpdateSubAccountDetails(that)); + }) + .catch(error => { + reject(error); + }); + }); } /** @@ -57,18 +141,29 @@ export class Subaccount extends PlivoResource { * @promise {boolean} return true if subaccount deleted * @fail {Error} return Error */ - delete(cascade) { + delete(cascade) + { let params = {}; if (typeof cascade === 'boolean') { params.cascade = cascade.toString(); } - - return super.delete(params); + let client = this[clientKey]; + let id = this.id; + + return new Promise((resolve, reject) => { + client('DELETE', action + id + '/', params) + .then(() => { + resolve(true); + }) + .catch(error => { + reject(error); + }); + }); } - } + /** * Represents a Subaccount Interface * @constructor @@ -98,7 +193,20 @@ export class SubaccountInterface extends PlivoResourceInterface { if (errors) { return errors; } - return super.get(id); + let client = this[clientKey]; + + return new Promise((resolve, reject) => { + if (action !== '' && !id) { + reject(new Error(this[idKey] + ' must be set')); + } + client('GET', action + (id ? id + '/' : '')) + .then(response => { + resolve(new GetSubAccountDetails(response.body,client)); + }) + .catch(error => { + reject(error); + }); + }); } /** @@ -125,8 +233,16 @@ export class SubaccountInterface extends PlivoResourceInterface { if (typeof enabled === 'boolean') { params.enabled = enabled.toString(); } - - return super.create(params); + let client = this[clientKey]; + return new Promise((resolve, reject) => { + client('POST', action + (this.id ? this.id + '/' : ''), params) + .then(response => { + resolve(new CreateSubAccountResponse(response.body, idField)); + }) + .catch(error => { + reject(error); + }); + }) } /** @@ -197,10 +313,12 @@ export class Account extends PlivoResource { * @promise {PlivoGenericResponse} return PlivoGenericResponse object * @fail {Error} return Error */ - get() { - return new AccountInterface(this[clientKey]) - .get(); - } +get() { + return new AccountInterface(this[clientKey]) + .get(); +} + + /** * update account detail @@ -213,7 +331,23 @@ export class Account extends PlivoResource { * @fail {Error} return Error */ update(params) { - return super.update(params, ''); + + let client = this[clientKey]; + let that = this; + return new Promise((resolve, reject) => { + client('POST', '/', params) + .then(response => { + extend(that, response.body); + if (params.hasOwnProperty('isVoiceRequest')){ + delete params.isVoiceRequest; + } + extend(that, params); + resolve(new UpdateAccountDetailsResponse(that)); + }) + .catch(error => { + reject(error); + }); + }); } } @@ -238,8 +372,17 @@ export class AccountInterface extends PlivoResourceInterface { * @fail {Error} return Error */ get() { - return super.get(); - } + let client = this[clientKey]; + return new Promise((resolve, reject) => { + client('GET', '/') + .then(response => { + resolve(new GetAccountDetails(response.body, client)); + }) + .catch(error => { + reject(error); + }); + }); +} /** * update account detail diff --git a/lib/resources/applications.js b/lib/resources/applications.js index a06355a..d8895dd 100644 --- a/lib/resources/applications.js +++ b/lib/resources/applications.js @@ -1,5 +1,11 @@ -import {extend, validate} from '../utils/common.js'; -import {PlivoResource, PlivoResourceInterface} from '../base'; +import { + PlivoResource, + PlivoResourceInterface +} from '../base'; +import { + extend, + validate +} from '../utils/common.js'; const clientKey = Symbol(); const action = 'Application/'; @@ -11,60 +17,144 @@ const idField = 'appId'; * @param {function} client - make api call * @param {object} [data] - data of call */ -export class Application extends PlivoResource { - constructor(client, data = {}) { - super(action, Application, idField, client); - if (idField in data) { - this.id = data[idField]; + +export class UpdateApplicationResponse { + constructor(params) { + params = params || {}; + this.apiId = params.apiId; + this.message = params.message; } - - extend(this, data); - } - -/** - * update application - * @method - * @param {object} params - to update application - * @param {string} [params.answerUrl] The URL invoked by Plivo when a call executes this application. - * @param {string} [params.answerMethod] The method used to call the answer_url. Defaults to POST. - * @param {string} [params.hangupUrl] The URL that is notified by Plivo when the call hangs up. - * @param {string} [params.hangupMethod] The method used to call the hangup_url. Defaults to POST - * @param {string} [params.fallbackAnswerUrl] Invoked by Plivo only if answer_url is unavailable or the XML response is invalid. Should contain a XML response. - * @param {string} [params.fallbackMethod] The method used to call the fallback_answer_url. Defaults to POST. - * @param {string} [params.messageUrl] The URL that is notified by Plivo when an inbound message is received. Defaults not set. - * @param {string} [params.messageMethod] The method used to call the message_url. Defaults to POST. - * @param {boolean} [params.defaultNumberApp] If set to true, associates all newly created Plivo numbers that have not specified an app_id, to this application. - * @param {boolean} [params.defaultEndpointApp] If set to true, associates all newly created Plivo endpoints that have not specified an app_id, to this application. - * @param {string} [params.subaccount] Id of the subaccount, in case only subaccount applications are needed. - * @param {boolean} [params.logIncomingMessages] flag to control incoming message logs. - - * @promise {object} return {@link Application} object - * @fail {Error} return Error - */ - update(params) { - params.isVoiceRequest = 'true'; - return super.update(params); - } - -/** - * delete application - * @method - * @param {object} params - params to delete application - * @param {boolean} [params.cascade] - delete associated endpoints - * @param {string} [params.newEndpointApplication] - link associated endpoints with app - * @promise {object} return true on success - * @fail {Error} return Error - */ - delete(params) { - if (typeof params.cascade === 'boolean') { - params.cascade = params.cascade.toString(); - } - params.isVoiceRequest = 'true'; - return super.delete(params); - } - } + +export class CreateApplicationResponse { + constructor(params) { + params = params || {}; + this.apiId = params.apiId; + this.appId = params.appId; + this.message = params.message; + } +} + +export class RetrieveApplicationResponse { + constructor(params) { + params = params || {}; + this.answerMethod = params.answerMethod; + this.answerUrl = params.answerUrl; + this.apiId = params.apiId; + this.appId = params.appId; + this.appName = params.appName; + this.applicationType = params.applicationType; + this.defaultApp = params.defaultApp; + this.defaultEndpointApp = params.defaultEndpointApp; + this.enabled = params.enabled; + this.fallbackAnswerUrl = params.fallbackAnswerUrl; + this.fallbackMethod = params.fallbackMethod; + this.hangupMethod = params.hangupMethod; + this.logIncomingMessage = params.logIncomingMessage; + this.messageMethod = params.messageMethod; + this.resourceUri = params.resourceUri; + this.sipUri = params.sipUri; + this.subAccount = params.subAccount; + } +} +export class ListAllApplicationResponse { + constructor(params) { + params = params || {}; + this.answerMethod = params.answerMethod; + this.answerUrl = params.answerUrl; + this.apiId = params.apiId; + this.appId = params.appId; + this.appName = params.appName; + this.applicationType = params.applicationType; + this.defaultApp = params.defaultApp; + this.defaultEndpointApp = params.defaultEndpointApp; + this.enabled = params.enabled; + this.fallbackAnswerUrl = params.fallbackAnswerUrl; + this.fallbackMethod = params.fallbackMethod; + this.hangupMethod = params.hangupMethod; + this.logIncomingMessage = params.logIncomingMessage; + this.messageMethod = params.messageMethod; + this.resourceUri = params.resourceUri; + this.sipUri = params.sipUri; + this.subAccount = params.subAccount; + } +} + +export class Application extends PlivoResource { + constructor(client, data = {}) { + super(action, Application, idField, client); + if (idField in data) { + this.id = data[idField]; + } + this[clientKey] = client; + extend(this, data); + } + + /** + * update application + * @method + * @param {object} params - to update application + * @param {string} [params.answerUrl] The URL invoked by Plivo when a call executes this application. + * @param {string} [params.answerMethod] The method used to call the answer_url. Defaults to POST. + * @param {string} [params.hangupUrl] The URL that is notified by Plivo when the call hangs up. + * @param {string} [params.hangupMethod] The method used to call the hangup_url. Defaults to POST + * @param {string} [params.fallbackAnswerUrl] Invoked by Plivo only if answer_url is unavailable or the XML response is invalid. Should contain a XML response. + * @param {string} [params.fallbackMethod] The method used to call the fallback_answer_url. Defaults to POST. + * @param {string} [params.messageUrl] The URL that is notified by Plivo when an inbound message is received. Defaults not set. + * @param {string} [params.messageMethod] The method used to call the message_url. Defaults to POST. + * @param {boolean} [params.defaultNumberApp] If set to true, associates all newly created Plivo numbers that have not specified an app_id, to this application. + * @param {boolean} [params.defaultEndpointApp] If set to true, associates all newly created Plivo endpoints that have not specified an app_id, to this application. + * @param {string} [params.subaccount] Id of the subaccount, in case only subaccount applications are needed. + * @param {boolean} [params.logIncomingMessages] flag to control incoming message logs. + + * @promise {object} return {@link Application} object + * @fail {Error} return Error + */ + update(params) { + params.isVoiceRequest = 'true'; + let client = this[clientKey]; + let that = this; + return new Promise((resolve, reject) => { + client('POST', action + that.id + '/', params) + .then(response => { + extend(that, response.body); + if (params.hasOwnProperty('isVoiceRequest')) { + delete params.isVoiceRequest; + } + extend(that, params); + resolve(new UpdateApplicationResponse(that)); + }) + .catch(error => { + reject(error); + }); + }); + + } + + /** + * delete application + * @method + * @param {object} params - params to delete application + * @param {boolean} [params.cascade] - delete associated endpoints + * @param {string} [params.newEndpointApplication] - link associated endpoints with app + * @promise {object} return true on success + * @fail {Error} return Error + */ + delete(params, id) { + let client = this[clientKey]; + return new Promise((resolve, reject) => { + client('DELETE', action + id + '/', params) + .then(() => { + resolve(true); + }) + .catch(error => { + reject(error); + }); + }); + } +} + /** * Represents a Application interface * @constructor @@ -73,126 +163,165 @@ export class Application extends PlivoResource { */ export class ApplicationInterface extends PlivoResourceInterface { - constructor(client, data = {}) { - super(action, Application, idField, client); - extend(this, data); - - this[clientKey] = client; - } - -/** - * get application by given id - * @method - * @param {string} id - id of application - * @promise {object} return {@link Application} object - * @fail {Error} return Error - */ - get(id) { - let params = {} - params.isVoiceRequest = 'true' - return super.get(id, params); - } - - /** - * list applications - * @method - * @param {object} params - params to list applications - * @param {string} [params.subaccount] - ID of the subaccount if present - * @param {integer} [params.limit] - To display no of results per page - * @param {integer} [params.offset] - No of value items by which results should be offset - */ - list(params= {}) { - params.isVoiceRequest = 'true'; - return super.list(params); - } - -/** - * create Application - * @method - * @param {string} appName - name of application - * @param {object} params - params to create application - * @param {string} [params.answerUrl] - answer url - * @param {string} [params.appName] The name of your application - * @param {string} [params.answerUrl] The URL invoked by Plivo when a call executes this application. - * @param {string} [params.answerMethod] The method used to call the answer_url. Defaults to POST. - * @param {string} [params.hangupUrl] The URL that is notified by Plivo when the call hangs up. - * @param {string} [params.hangupMethod] The method used to call the hangup_url. Defaults to POST - * @param {string} [params.fallbackAnswerUrl] Invoked by Plivo only if answer_url is unavailable or the XML response is invalid. Should contain a XML response. - * @param {string} [params.fallbackMethod] The method used to call the fallback_answer_url. Defaults to POST. - * @param {string} [params.messageUrl] The URL that is notified by Plivo when an inbound message is received. Defaults not set. - * @param {string} [params.messageMethod] The method used to call the message_url. Defaults to POST. - * @param {boolean} [params.defaultNumberApp] If set to true, associates all newly created Plivo numbers that have not specified an app_id, to this application. - * @param {boolean} [params.defaultEndpointApp] If set to true, associates all newly created Plivo endpoints that have not specified an app_id, to this application. - * @param {string} [params.subaccount] Id of the subaccount, in case only subaccount applications are needed. - * @param {boolean} [params.logIncomingMessages] flag to control incoming message logs. - * @promise {object} return {@link PlivoGenericResponse} object - * @fail {Error} return Error - */ - create(appName, params = {}) { - - let errors = validate([ - {field: 'app_name', value: appName, validators: ['isRequired', 'isString']} - ]); - - if (errors) { - return errors; + constructor(client, data = {}) { + super(action, Application, idField, client); + extend(this, data); + this[clientKey] = client; } - params.app_name = appName; - params.isVoiceRequest = 'true'; - return super.create(params); - } + /** + * get application by given id + * @method + * @param {string} id - id of application + * @promise {object} return {@link Application} object + * @fail {Error} return Error + */ + get(id) { + let params = {} + params.isVoiceRequest = 'true' + let client = this[clientKey]; -/** - * update Application - * @method - * @param {string} id - id of application - * @param {object} params - to update application - * @param {string} [params.answerUrl] The URL invoked by Plivo when a call executes this application. - * @param {string} [params.answerMethod] The method used to call the answer_url. Defaults to POST. - * @param {string} [params.hangupUrl] The URL that is notified by Plivo when the call hangs up. - * @param {string} [params.hangupMethod] The method used to call the hangup_url. Defaults to POST - * @param {string} [params.fallbackAnswerUrl] Invoked by Plivo only if answer_url is unavailable or the XML response is invalid. Should contain a XML response. - * @param {string} [params.fallbackMethod] The method used to call the fallback_answer_url. Defaults to POST. - * @param {string} [params.messageUrl] The URL that is notified by Plivo when an inbound message is received. Defaults not set. - * @param {string} [params.messageMethod] The method used to call the message_url. Defaults to POST. - * @param {boolean} [params.defaultNumberApp] If set to true, associates all newly created Plivo numbers that have not specified an app_id, to this application. - * @param {boolean} [params.defaultEndpointApp] If set to true, associates all newly created Plivo endpoints that have not specified an app_id, to this application. - * @param {string} [params.subaccount] Id of the subaccount, in case only subaccount applications are needed. - * @param {boolean} [params.logIncomingMessages] flag to control incoming message logs. - * @promise {object} return {@link Application} object - * @fail {Error} return Error - */ - update(id, params) { - let errors = validate([ - {field: 'id', value: id, validators: ['isRequired']} - ]); - - if (errors) { - return errors; - } - return new Application(this[clientKey], { - id: id - }).update(params); - } - -/** - * delete Application - * @method - * @param {string} id - id of application - * @param {object} params - params to delete application - * @param {boolean} [params.cascade] - delete associated endpoints - * @param {string} [params.newEndpointApplication] - link associated endpoints with app - * @promise {object} return true on success - * @fail {Error} return Error - */ - delete(id, params = {}) { - if (typeof params.cascade === 'boolean') { - params.cascade = params.cascade.toString(); + return new Promise((resolve, reject) => { + if (action !== '' && !id) { + reject(new Error(this[idKey] + ' must be set')); + } + client('GET', action + (id ? id + '/' : ''), params) + .then(response => { + resolve(new RetrieveApplicationResponse(response.body, client)); + }) + .catch(error => { + reject(error); + }); + }); } - return new Application(this[clientKey], { - id: id - }).delete(params); - } -} + /** + * list applications + * @method + * @param {object} params - params to list applications + * @param {string} [params.subaccount] - ID of the subaccount if present + * @param {integer} [params.limit] - To display no of results per page + * @param {integer} [params.offset] - No of value items by which results should be offset + */ + list(params = {}) { + let client = this[clientKey]; + return new Promise((resolve, reject) => { + client('GET', action, params) + .then(response => { + let objects = []; + Object.defineProperty(objects, 'meta', { + value: response.body.meta, + enumerable: true + }); + response.body.objects.forEach(item => { + objects.push(new ListAllApplicationResponse(item, client)); + }); + resolve(objects); + }) + .catch(error => { + reject(error); + }); + }); + } + + /** + * create Application + * @method + * @param {string} appName - name of application + * @param {object} params - params to create application + * @param {string} [params.answerUrl] - answer url + * @param {string} [params.appName] The name of your application + * @param {string} [params.answerUrl] The URL invoked by Plivo when a call executes this application. + * @param {string} [params.answerMethod] The method used to call the answer_url. Defaults to POST. + * @param {string} [params.hangupUrl] The URL that is notified by Plivo when the call hangs up. + * @param {string} [params.hangupMethod] The method used to call the hangup_url. Defaults to POST + * @param {string} [params.fallbackAnswerUrl] Invoked by Plivo only if answer_url is unavailable or the XML response is invalid. Should contain a XML response. + * @param {string} [params.fallbackMethod] The method used to call the fallback_answer_url. Defaults to POST. + * @param {string} [params.messageUrl] The URL that is notified by Plivo when an inbound message is received. Defaults not set. + * @param {string} [params.messageMethod] The method used to call the message_url. Defaults to POST. + * @param {boolean} [params.defaultNumberApp] If set to true, associates all newly created Plivo numbers that have not specified an app_id, to this application. + * @param {boolean} [params.defaultEndpointApp] If set to true, associates all newly created Plivo endpoints that have not specified an app_id, to this application. + * @param {string} [params.subaccount] Id of the subaccount, in case only subaccount applications are needed. + * @param {boolean} [params.logIncomingMessages] flag to control incoming message logs. + * @promise {object} return {@link PlivoGenericResponse} object + * @fail {Error} return Error + */ + create(appName, params = {}) { + + let errors = validate([{ + field: 'app_name', + value: appName, + validators: ['isRequired', 'isString'] + }]); + + if (errors) { + return errors; + } + params.app_name = appName; + let client = this[clientKey]; + return new Promise((resolve, reject) => { + console.log(action, params) + client('POST', action, params) + .then(response => { + resolve(new CreateApplicationResponse(response.body, idField)); + }) + .catch(error => { + reject(error); + }); + }) + } + + /** + * update Application + * @method + * @param {string} id - id of application + * @param {object} params - to update application + * @param {string} [params.answerUrl] The URL invoked by Plivo when a call executes this application. + * @param {string} [params.answerMethod] The method used to call the answer_url. Defaults to POST. + * @param {string} [params.hangupUrl] The URL that is notified by Plivo when the call hangs up. + * @param {string} [params.hangupMethod] The method used to call the hangup_url. Defaults to POST + * @param {string} [params.fallbackAnswerUrl] Invoked by Plivo only if answer_url is unavailable or the XML response is invalid. Should contain a XML response. + * @param {string} [params.fallbackMethod] The method used to call the fallback_answer_url. Defaults to POST. + * @param {string} [params.messageUrl] The URL that is notified by Plivo when an inbound message is received. Defaults not set. + * @param {string} [params.messageMethod] The method used to call the message_url. Defaults to POST. + * @param {boolean} [params.defaultNumberApp] If set to true, associates all newly created Plivo numbers that have not specified an app_id, to this application. + * @param {boolean} [params.defaultEndpointApp] If set to true, associates all newly created Plivo endpoints that have not specified an app_id, to this application. + * @param {string} [params.subaccount] Id of the subaccount, in case only subaccount applications are needed. + * @param {boolean} [params.logIncomingMessages] flag to control incoming message logs. + * @promise {object} return {@link Application} object + * @fail {Error} return Error + */ + update(id, params) { + let errors = validate([{ + field: 'id', + value: id, + validators: ['isRequired'] + }]); + + if (errors) { + return errors; + } + return new Application(this[clientKey], { + id: id + }).update(params); + } + + /** + * delete Application + * @method + * @param {string} id - id of application + * @param {object} params - params to delete application + * @param {boolean} [params.cascade] - delete associated endpoints + * @param {string} [params.newEndpointApplication] - link associated endpoints with app + * @promise {object} return true on success + * @fail {Error} return Error + */ + delete(id, params = {}) { + if (typeof params.cascade === 'boolean') { + params.cascade = params.cascade.toString(); + } + return new Application(this[clientKey], { + id: id + }).delete(params, id); + } +} \ No newline at end of file diff --git a/lib/resources/call.js b/lib/resources/call.js index 655c0ac..2b25bfe 100644 --- a/lib/resources/call.js +++ b/lib/resources/call.js @@ -1,12 +1,167 @@ - -import {extend, validate} from '../utils/common.js'; -import {PlivoResource, PlivoResourceInterface} from '../base'; import * as _ from "lodash"; +import { + PlivoResource, + PlivoResourceInterface +} from '../base'; +import { + extend, + validate +} from '../utils/common.js'; + const clientKey = Symbol(); const action = 'Call/'; const idField = 'callUuid'; +export class CallTransferResponse { + constructor(params) { + params = params || {}; + this.apiId = params.apiId; + this.callUuids = params.callUuids; + this.message = params.message; + + } +} +export class ListAllQueuedCalls { + constructor(params) { + params = params || {}; + this.apiId = params.id; + this.calls = params.calls; + } +} +export class ListAllLiveCallResponse { + constructor(params) { + params = params || {}; + this.apiId = params.id; + this.callUuid = params.callUuid; + } +} + +export class CreateCallResponse { + constructor(params) { + params = params || {}; + this.apiId = params.apiId; + this.message = params.message; + this.requestUuid = params.requestUuid; + + } +} + + +export class GetQueuedCallResponse { + constructor(params) { + params = params || {}; + this.apiId = params.apiId; + this.direction = params.direction; + this.from = params.from; + this.callStatus = params.callStatus; + this.to = params.to; + this.callerName = params.callerName; + this.callUuid = params.callUuid; + this.requestUuid = params.requestUuid; + } +} + + +export class GetLiveCallResponse { + constructor(params) { + params = params || {}; + this.apiId = params.apiId; + this.callStatus = params.callStatus; + this.callUuid = params.callUuid; + this.callerName = params.callerName; + this.direction = params.direction; + this.from = params.from; + this.requestUuid = params.requestUuid; + this.sessionStart = params.sessionStart; + this.to = params.to; + } +} +export class RetrieveCallResponse { + constructor(params) { + params = params || {}; + this.apiId = params.apiId; + this.answerTime = params.answerTime; + this.billDuration = params.billDuration; + this.billedDuration = params.billedDuration; + this.callDirection = params.callDirection; + this.callDuration = params.callDuration; + this.callState = params.callState; + this.callUuid = params.callUuid; + this.conferenceUuid = params.conferenceUuid; + this.endTime = params.endTime; + this.fromNumber = params.fromNumber; + this.hangupCauseCode = params.hangupCauseCode; + this.hangupCauseName = params.hangupCauseName; + this.hangupSource = params.hangupSource; + this.initiationTime = params.initiationTime; + this.parentCallUuid = params.parentCallUuid; + this.resourceUri = params.resourceUri; + this.toNumber = params.toNumber; + this.totalAmount = params.totalAmount; + this.totalRate = params.totalRate; + } +} + +export class ListAllCallsResponse { + constructor(params) { + params = params || {}; + this.answerTime = params.answerTime; + this.billDuration = params.billDuration; + this.billedDuration = params.billedDuration; + this.callDirection = params.callDirection; + this.callDuration = params.callDuration; + this.callState = params.callState; + this.callUuid = params.callUuid; + this.conferenceUuid = params.conferenceUuid; + this.endTime = params.endTime; + this.fromNumber = params.fromNumber; + this.hangupCauseCode = params.hangupCauseCode; + this.hangupCauseName = params.hangupCauseName; + this.hangupSource = params.hangupSource; + this.initiationTime = params.initiationTime; + this.parentCallUuid = params.parentCallUuid; + this.resourceUri = params.resourceUri; + this.toNumber = params.toNumber; + this.totalAmount = params.totalAmount; + this.totalRate = params.totalRate; + } +} + +export class StartPlayingMusicResponse { + constructor(params) { + params = params || {}; + this.apiId = params.apiId; + this.message = params.message; + } +} + +export class StartSpeakingTextResponse { + constructor(params) { + params = params || {}; + this.apiId = params.apiId; + this.message = params.message; + } +} + +export class SendDigitsResponse { + constructor(params) { + params = params || {}; + this.apiId = params.apiId; + this.message = params.message; + } +} + +export class RecordCallResponse { + constructor(params) { + params = params || {}; + this.apiId = params.apiId; + this.message = params.message; + this.recordingId = params.recordingId; + this.url = params.url; + } +} + /** * Represents a Call * @constructor @@ -14,203 +169,268 @@ const idField = 'callUuid'; * @param {object} [data] - data of call */ export class Call extends PlivoResource { - constructor(client, data = {}) { - super(action, Call, idField, client); + constructor(client, data = {}) { + super(action, Call, idField, client); - if (idField in data) { - this.id = data[idField]; + if (idField in data) { + this.id = data[idField]; + } + + extend(this, data); + this[clientKey] = client; } - extend(this, data); - this[clientKey] = client; - } - -/** - * hangup call - * @method - * @promise {Boolean} return true if call hung up - * @fail {Error} return Error - */ - hangup() { - let params = {} - params.isVoiceRequest = 'true'; - return super.delete(params); - } - -/** - * transfer call - * @method - * @param {object} params - optional params to transfer a call - * @param {string} [params.legs] aleg, bleg or both Defaults to aleg. aleg will transfer call_uuid ; bleg will transfer the bridged leg (if found) of call_uuid ; both will transfer call_uuid and bridged leg of call_uuid - * @param {string} [params.alegUrl] URL to transfer for aleg, if legs is aleg or both, then aleg_url has to be specified. - * @param {string} [params.alegMethod] HTTP method to invoke aleg_url. Defaults to POST. - * @param {string} [params.blegUrl] URL to transfer for bridged leg, if legs is bleg or both, then bleg_url has to be specified. - * @param {string} [params.blegMethod] HTTP method to invoke bleg_url. Defaults to POST. - * @promise {object} return call object - * @fail {Error} return Error - */ - transfer(params) { - params.isVoiceRequest = 'true'; - return super.update(params); - } -/** - * record call - * @method - * @param {object} params - to record call - * @promise {object} return PlivoGenericResponse Object - * @fail {Error} return Error - */ - record(params) { - return this.startRecording(params); - } - -/** - * record call - * @method - * @param {object} params - to record call - * @promise {object} return PlivoGenericResponse Object - * @fail {Error} return Error - */ - startRecording(params) { - params.isVoiceRequest = 'true'; - return super.executeAction(this.id + '/Record/', 'POST', params); - } -/** - * stop recording call - * @method - * @param {object} params - to stop recording call - * @promise {object} return PlivoGenericResponse Object - * @fail {Error} return Error - */ - stopRecording(params) { - params.isVoiceRequest = 'true'; - return super.executeAction(this.id + '/Record/', 'DELETE', params); - } - -/** - * play music for call - * @method - * @param {string} url - url which contains audio to play for call - * @param {object} optionalParams - to stop recording call - * @promise {object} returns PlivoGenericResponse Object - * @fail {Error} returns Error - */ - playMusic(url, optionalParams) { - return this.startPlayingMusic(url, optionalParams); - } -/** - * play music for call - * @method - * @param {string} url - url which contains audio to play for call - * @param {object} optionalParams - to stop recording call - * @promise {object} returns PlivoGenericResponse Object - * @fail {Error} returns Error - */ - startPlayingMusic(urls, optionalParams) { - let params = optionalParams || {}; - params.urls = urls; - params.isVoiceRequest = 'true'; - - let errors = validate([ - {field: 'urls', value: urls, validators: ['isRequired', 'isString']} - ]); - - if (errors) { - return errors; - } - return super.executeAction(this.id + '/Play/', 'POST', params); - } - -/** - * stop playing music for call - * @method - * @promise {object} returns PlivoGenericResponse Object - * @fail {Error} returns Error - */ - stopPlayingMusic() { - let params = {} - params.isVoiceRequest = 'true' - return super.executeAction(this.id + '/Play/', 'DELETE', params); - } - -/** - * speak text for call - * @method - * @param {string} text - text to speak for call - * @param {object} optionalParams - to speak for call - * @promise {object} returns PlivoGenericResponse Object - * @fail {Error} returns Error - */ - speakText(text, optionalParams) { - return this.startSpeakingText(text, optionalParams); - } - -/** - * speak text for call - * @method - * @param {string} text - text to speak for call - * @param {object} optionalParams - to speak for call - * @promise {object} returns PlivoGenericResponse Object - * @fail {Error} returns Error - */ - startSpeakingText(text, optionalParams) { - let errors = validate([{field: 'text', value: text, validators: ['isRequired', 'isString']}]); - - if (errors) { - return errors; + /** + * hangup call + * @method + * @promise {Boolean} return true if call hung up + * @fail {Error} return Error + */ + hangup() { + let params = {} + params.isVoiceRequest = 'true'; + return super.delete(params); } - let params = optionalParams || {}; - params.text = text; - params.isVoiceRequest = 'true'; + /** + * transfer call + * @method + * @param {object} params - optional params to transfer a call + * @param {string} [params.legs] aleg, bleg or both Defaults to aleg. aleg will transfer call_uuid ; bleg will transfer the bridged leg (if found) of call_uuid ; both will transfer call_uuid and bridged leg of call_uuid + * @param {string} [params.alegUrl] URL to transfer for aleg, if legs is aleg or both, then aleg_url has to be specified. + * @param {string} [params.alegMethod] HTTP method to invoke aleg_url. Defaults to POST. + * @param {string} [params.blegUrl] URL to transfer for bridged leg, if legs is bleg or both, then bleg_url has to be specified. + * @param {string} [params.blegMethod] HTTP method to invoke bleg_url. Defaults to POST. + * @promise {object} return call object + * @fail {Error} return Error + */ + transfer(params, callUUID) { + params.isVoiceRequest = 'true'; + let client = this[clientKey]; + let that = this; + callUUID = typeof callUUID !== 'undefined' ? callUUID : that.callUUID; - return super.executeAction(this.id + '/Speak/', 'POST', params); - } - -/** - * stop speaking text for call - * @method - * @promise {object} returns PlivoGenericResponse Object - * @fail {Error} returns Error - */ - stopSpeakingText() { - let params = {} - params.isVoiceRequest = 'true'; - return super.executeAction(this.id + '/Speak/', 'DELETE', params); - } - -/** - * Send digits on a call - * @method - * @param {number} digits - digits to be send - * @param {object} optionalParams - to send digits for call - * @promise {object} returns PlivoGenericResponse Object - * @fail {Error} returns Error - */ - sendDigits(digits, optionalParams) { - let errors = validate([{field: 'digits', value: digits, validators: ['isRequired']}]); - - if (errors) { - return errors; + return new Promise((resolve, reject) => { + client('POST', action + callUUID + '/', params) + .then(response => { + extend(that, response.body); + if (params.hasOwnProperty('isVoiceRequest')) { + delete params.isVoiceRequest; + } + extend(that, params); + resolve(new CallTransferResponse(that)); + }) + .catch(error => { + reject(error); + }); + }); + } + /** + * record call + * @method + * @param {object} params - to record call + * @promise {object} return PlivoGenericResponse Object + * @fail {Error} return Error + */ + record(params) { + return this.startRecording(params); } - let params = optionalParams || {}; - params.digits = digits; - params.isVoiceRequest = 'true'; - return super.executeAction(this.id + '/DTMF/', 'POST', params); - } + /** + * record call + * @method + * @param {object} params - to record call + * @promise {object} return PlivoGenericResponse Object + * @fail {Error} return Error + */ + startRecording(params) { + let client = this[clientKey]; + return new Promise((resolve, reject) => { + client('POST', action + this.id + '/Record/', params) + .then(response => { + resolve(new RecordCallResponse(response.body, idField)); + }) + .catch(error => { + reject(error); + }); + }); + } + /** + * stop recording call + * @method + * @param {object} params - to stop recording call + * @promise {object} return PlivoGenericResponse Object + * @fail {Error} return Error + */ + stopRecording(params) { + params.isVoiceRequest = 'true'; + return super.executeAction(this.id + '/Record/', 'DELETE', params); + } -/** - * Hangup a Call Request - * @method - * @promise {object} returns PlivoGenericResponse Object - * @fail {Error} returns Error - */ - cancel() { - let params = {}; - params.isVoiceRequest = 'true'; - return super.executeAction('Request/' + this.id + '/', 'DELETE', params, ''); - } + /** + * play music for call + * @method + * @param {string} url - url which contains audio to play for call + * @param {object} optionalParams - to stop recording call + * @promise {object} returns PlivoGenericResponse Object + * @fail {Error} returns Error + */ + playMusic(url, optionalParams) { + return this.startPlayingMusic(url, optionalParams); + } + /** + * play music for call + * @method + * @param {string} url - url which contains audio to play for call + * @param {object} optionalParams - to stop recording call + * @promise {object} returns PlivoGenericResponse Object + * @fail {Error} returns Error + */ + startPlayingMusic(urls, optionalParams) { + let params = optionalParams || {}; + params.urls = urls; + params.isVoiceRequest = 'true'; + + let errors = validate([{ + field: 'urls', + value: urls, + validators: ['isRequired', 'isString'] + }]); + + if (errors) { + return errors; + } + let client = this[clientKey]; + return new Promise((resolve, reject) => { + client('POST', action + this.id + '/Play/', params) + .then(response => { + resolve(new StartPlayingMusicResponse(response.body, idField)); + }) + .catch(error => { + reject(error); + }); + }); + } + + /** + * stop playing music for call + * @method + * @promise {object} returns PlivoGenericResponse Object + * @fail {Error} returns Error + */ + stopPlayingMusic() { + let params = {} + params.isVoiceRequest = 'true' + return super.executeAction(this.id + '/Play/', 'DELETE', params); + } + + /** + * speak text for call + * @method + * @param {string} text - text to speak for call + * @param {object} optionalParams - to speak for call + * @promise {object} returns PlivoGenericResponse Object + * @fail {Error} returns Error + */ + speakText(text, optionalParams) { + return this.startSpeakingText(text, optionalParams); + } + + /** + * speak text for call + * @method + * @param {string} text - text to speak for call + * @param {object} optionalParams - to speak for call + * @promise {object} returns PlivoGenericResponse Object + * @fail {Error} returns Error + */ + startSpeakingText(text, optionalParams) { + let errors = validate([{ + field: 'text', + value: text, + validators: ['isRequired', 'isString'] + }]); + + if (errors) { + return errors; + } + + let params = optionalParams || {}; + params.text = text; + params.isVoiceRequest = 'true'; + + let client = this[clientKey]; + return new Promise((resolve, reject) => { + client('POST', action + this.id + '/Speak/', params) + .then(response => { + resolve(new StartSpeakingTextResponse(response.body, idField)); + }) + .catch(error => { + reject(error); + }); + }); + } + + /** + * stop speaking text for call + * @method + * @promise {object} returns PlivoGenericResponse Object + * @fail {Error} returns Error + */ + stopSpeakingText() { + let params = {} + params.isVoiceRequest = 'true'; + return super.executeAction(this.id + '/Speak/', 'DELETE', params); + } + + /** + * Send digits on a call + * @method + * @param {number} digits - digits to be send + * @param {object} optionalParams - to send digits for call + * @promise {object} returns PlivoGenericResponse Object + * @fail {Error} returns Error + */ + sendDigits(digits, optionalParams) { + let errors = validate([{ + field: 'digits', + value: digits, + validators: ['isRequired'] + }]); + + if (errors) { + return errors; + } + + let params = optionalParams || {}; + params.digits = digits; + params.isVoiceRequest = 'true'; + + let client = this[clientKey]; + return new Promise((resolve, reject) => { + client('POST', action + this.id + '/DTMF/', params) + .then(response => { + resolve(new SendDigitsResponse(response.body, idField)); + }) + .catch(error => { + reject(error); + }); + }); + + + } + + /** + * Hangup a Call Request + * @method + * @promise {object} returns PlivoGenericResponse Object + * @fail {Error} returns Error + */ + cancel() { + let params = {}; + params.isVoiceRequest = 'true'; + return super.executeAction('Request/' + this.id + '/', 'DELETE', params, ''); + } } const liveCallInterfaceKey = Symbol('liveCallInterface'); @@ -224,350 +444,440 @@ const queuedCallInterfaceKey = Symbol('queuedCallInterface'); */ export class CallInterface extends PlivoResourceInterface { - constructor(client, data = {}) { - super(action, Call, idField, client); - extend(this, data); + constructor(client, data = {}) { + super(action, Call, idField, client); + extend(this, data); - this[clientKey] = client; - this[liveCallInterfaceKey] = new LiveCallInterface(client); - this[queuedCallInterfaceKey] = new QueuedCallInterface(client); - } - -/** - * Get A Call Detail - * @method - * @param {string} id - call uuid to get information of. - * @promise {object} returns Call Object - * @fail {Error} returns Error - */ - get(id) { - let errors = validate([{field: 'id', value: id, validators: ['isRequired']}]); - - if (errors) { - return errors; + this[clientKey] = client; + this[liveCallInterfaceKey] = new LiveCallInterface(client); + this[queuedCallInterfaceKey] = new QueuedCallInterface(client); } - let params = {} - params.isVoiceRequest = 'true'; - return super.get(id, params); - } -/** - * Get All Call Detail - * @method - * @param {object} params - params to get all call details. - * @promise {object[]} returns list of Call Object - * @fail {Error} returns Error - */ - list(params) { - params.isVoiceRequest = 'true'; - return super.list(params); - } + /** + * Get A Call Detail + * @method + * @param {string} id - call uuid to get information of. + * @promise {object} returns Call Object + * @fail {Error} returns Error + */ + get(id) { + let errors = validate([{ + field: 'id', + value: id, + validators: ['isRequired'] + }]); -/** - * Create a call - * @method - * @param {string} from - The phone number to be used as the caller id (with the country code).For e.g, a USA caller id number could be, 15677654321, with '1' for the country code. - * @param {string} to - The regular number(s) or sip endpoint(s) to call. Regular number must be prefixed with country code but without the + sign). For e.g, to dial a number in the USA, the number could be, 15677654321, with '1' for the country code. Multiple numbers can be sent by using a delimiter. For e.g. 15677654321<12077657621<12047657621. Sip endpoints must be prefixed with sip: E.g., sip:john1234@phone.plivo.com. To make bulk calls, the delimiter < is used. For example, 15677654321<15673464321 0(in seconds). - * @param {number} [params.hangupOnRing] Schedules the call for hangup at a specified time after the call starts ringing. Value should be an integer >= 0 (in seconds). - * @param {string} [params.machineDetection] Used to detect if the call has been answered by a machine. The valid values are true and hangup. - * @param {number} [params.machineDetectionTime] Time allotted to analyze if the call has been answered by a machine. It should be an integer >= 2000 and <= 10000 and the unit is ms. The default value is 5000 ms. - * @param {string} [params.machineDetectionUrl] A URL where machine detection parameters will be sent by Plivo. This parameter should be used to make machine detection asynchronous - * @param {string} [params.machineDetectionMethod] The HTTP method which will be used by Plivo to request the machine_detection_url. Defaults to POST. - * @param {string} [params.sipHeaders] List of SIP headers in the form of 'key=value' pairs, separated by commas. - * @param {number} [params.ringTimeout] Determines the time in seconds the call should ring. If the call is not answered within the ring_timeout value or the default value of 120s, it is canceled. - * @param {string} [params.parentCallUuid] The call_uuid of the first leg in an ongoing conference call. It is recommended to use this parameter in scenarios where a member who is already present in the conference intends to add new members by initiating outbound API calls. - * @param {boolean} [params.errorIfParentNotFound] if set to true and the parent_call_uuid cannot be found, the API request would return an error. If set to false, the outbound call API request will be executed even if the parent_call_uuid is not found. Defaults to false. - * @promise {object} returns PlivoGenericResponse Object - * @fail {Error} returns Error - */ - create(from, to, answerUrl, params = {}) { - let errors = validate([ - {field: 'from', value: from, validators: ['isRequired']}, - {field: 'to', value: to, validators: ['isRequired']}, - {field: 'answer_url', value: answerUrl, validators: ['isRequired']} - ]); + if (errors) { + return errors; + } + let params = {} + params.isVoiceRequest = 'true'; + let client = this[clientKey]; + return new Promise((resolve, reject) => { + if (action !== '' && !id) { + reject(new Error(this[idKey] + ' must be set')); + } + client('GET', action + (id ? id + '/' : ''), params) + .then(response => { + resolve(new RetrieveCallResponse(response.body, client)); + }) + .catch(error => { + reject(error); + }); + }); - if (errors) { - return errors; } - params.from = from; - params.to = _.isArray(to) ? _.join(to, '<') : to; - params.answer_url = answerUrl; - params.isVoiceRequest = 'true'; - return super.create(params); - } + /** + * Get All Call Detail + * @method + * @param {object} params - params to get all call details. + * @promise {object[]} returns list of Call Object + * @fail {Error} returns Error + */ + list(params) { + params.isVoiceRequest = 'true'; + let client = this[clientKey]; -/** - * Hangup A Specific Call - * @method - * @param {string} callUUID - call uuid to hangup call - * @promise {object} returns PlivoGenericResponse Object - * @fail {Error} returns Error - */ - hangup(callUUID) { - let errors = validate([ - {field: 'call_uuid', value: callUUID, validators: ['isRequired']} - ]); - - if (errors) { - return errors; + return new Promise((resolve, reject) => { + client('GET', action, params) + .then(response => { + let objects = []; + Object.defineProperty(objects, 'meta', { + value: response.body.meta, + enumerable: true + }); + response.body.objects.forEach(item => { + objects.push(new ListAllCallsResponse(item, client)); + }); + resolve(objects); + }) + .catch(error => { + reject(error); + }); + }); } - return new Call(this[clientKey], { - id: callUUID - }).hangup(); - } -/** - * Transfer a Call - * @method - * @param {string} callUUID - call uuid to transfer call - * @param {object} params - optional params to transfer a call - * @param {string} [params.legs] aleg, bleg or both Defaults to aleg. aleg will transfer call_uuid ; bleg will transfer the bridged leg (if found) of call_uuid ; both will transfer call_uuid and bridged leg of call_uuid - * @param {string} [params.alegUrl] URL to transfer for aleg, if legs is aleg or both, then aleg_url has to be specified. - * @param {string} [params.alegMethod] HTTP method to invoke aleg_url. Defaults to POST. - * @param {string} [params.blegUrl] URL to transfer for bridged leg, if legs is bleg or both, then bleg_url has to be specified. - * @param {string} [params.blegMethod] HTTP method to invoke bleg_url. Defaults to POST. - * @promise {object} returns PlivoGenericResponse Object - * @fail {Error} returns Error - */ - transfer(callUUID, params) { - let errors = validate([ - {field: 'call_uuid', value: callUUID, validators: ['isRequired']} - ]); - if (errors) { - return errors; + /** + * Create a call + * @method + * @param {string} from - The phone number to be used as the caller id (with the country code).For e.g, a USA caller id number could be, 15677654321, with '1' for the country code. + * @param {string} to - The regular number(s) or sip endpoint(s) to call. Regular number must be prefixed with country code but without the + sign). For e.g, to dial a number in the USA, the number could be, 15677654321, with '1' for the country code. Multiple numbers can be sent by using a delimiter. For e.g. 15677654321<12077657621<12047657621. Sip endpoints must be prefixed with sip: E.g., sip:john1234@phone.plivo.com. To make bulk calls, the delimiter < is used. For example, 15677654321<15673464321 0(in seconds). + * @param {number} [params.hangupOnRing] Schedules the call for hangup at a specified time after the call starts ringing. Value should be an integer >= 0 (in seconds). + * @param {string} [params.machineDetection] Used to detect if the call has been answered by a machine. The valid values are true and hangup. + * @param {number} [params.machineDetectionTime] Time allotted to analyze if the call has been answered by a machine. It should be an integer >= 2000 and <= 10000 and the unit is ms. The default value is 5000 ms. + * @param {string} [params.machineDetectionUrl] A URL where machine detection parameters will be sent by Plivo. This parameter should be used to make machine detection asynchronous + * @param {string} [params.machineDetectionMethod] The HTTP method which will be used by Plivo to request the machine_detection_url. Defaults to POST. + * @param {string} [params.sipHeaders] List of SIP headers in the form of 'key=value' pairs, separated by commas. + * @param {number} [params.ringTimeout] Determines the time in seconds the call should ring. If the call is not answered within the ring_timeout value or the default value of 120s, it is canceled. + * @param {string} [params.parentCallUuid] The call_uuid of the first leg in an ongoing conference call. It is recommended to use this parameter in scenarios where a member who is already present in the conference intends to add new members by initiating outbound API calls. + * @param {boolean} [params.errorIfParentNotFound] if set to true and the parent_call_uuid cannot be found, the API request would return an error. If set to false, the outbound call API request will be executed even if the parent_call_uuid is not found. Defaults to false. + * @promise {object} returns PlivoGenericResponse Object + * @fail {Error} returns Error + */ + create(from, to, answerUrl, params = {}) { + let errors = validate([{ + field: 'from', + value: from, + validators: ['isRequired'] + }, + { + field: 'to', + value: to, + validators: ['isRequired'] + }, + { + field: 'answer_url', + value: answerUrl, + validators: ['isRequired'] + } + ]); + + if (errors) { + return errors; + } + params.from = from; + params.to = _.isArray(to) ? _.join(to, '<') : to; + params.answer_url = answerUrl; + params.isVoiceRequest = 'true'; + + let client = this[clientKey]; + return new Promise((resolve, reject) => { + client('POST', action, params) + .then(response => { + resolve(new CreateCallResponse(response.body, idField)); + }) + .catch(error => { + reject(error); + }); + }); } - return new Call(this[clientKey], { - id: callUUID - }).transfer(params); - } -/** - * Record a Call - * @method - * @param {string} callUUID - call uuid to record call - * @param {object} optionalParams - optional params to record a call - * @promise {object} returns PlivoGenericResponse Object - * @fail {Error} returns Error - */ - record(callUUID, optionalParams) { - let errors = validate([ - {field: 'call_uuid', value: callUUID, validators: ['isRequired']} - ]); + /** + * Hangup A Specific Call + * @method + * @param {string} callUUID - call uuid to hangup call + * @promise {object} returns PlivoGenericResponse Object + * @fail {Error} returns Error + */ + hangup(callUUID) { + let errors = validate([{ + field: 'call_uuid', + value: callUUID, + validators: ['isRequired'] + }]); - if (errors) { - return errors; + if (errors) { + return errors; + } + return new Call(this[clientKey], { + id: callUUID + }).hangup(); } - return new Call(this[clientKey], { - id: callUUID - }).record(optionalParams); - } + /** + * Transfer a Call + * @method + * @param {string} callUUID - call uuid to transfer call + * @param {object} params - optional params to transfer a call + * @param {string} [params.legs] aleg, bleg or both Defaults to aleg. aleg will transfer call_uuid ; bleg will transfer the bridged leg (if found) of call_uuid ; both will transfer call_uuid and bridged leg of call_uuid + * @param {string} [params.alegUrl] URL to transfer for aleg, if legs is aleg or both, then aleg_url has to be specified. + * @param {string} [params.alegMethod] HTTP method to invoke aleg_url. Defaults to POST. + * @param {string} [params.blegUrl] URL to transfer for bridged leg, if legs is bleg or both, then bleg_url has to be specified. + * @param {string} [params.blegMethod] HTTP method to invoke bleg_url. Defaults to POST. + * @promise {object} returns PlivoGenericResponse Object + * @fail {Error} returns Error + */ + transfer(callUUID, params) { + let errors = validate([{ + field: 'call_uuid', + value: callUUID, + validators: ['isRequired'] + }]); -/** - * Stop Recording a Call - * @method - * @param {string} callUUID - call uuid to stop recording a call - * @param {object} optionalParams - optional params to stop recording a call - * @promise {object} returns PlivoGenericResponse Object - * @fail {Error} returns Error - */ - stopRecording(callUUID, optionalParams) { - let errors = validate([ - {field: 'call_uuid', value: callUUID, validators: ['isRequired']} - ]); - - if (errors) { - return errors; + if (errors) { + return errors; + } + return new Call(this[clientKey], { + id: callUUID + }).transfer(params, callUUID); } - return new Call(this[clientKey], { - id: callUUID - }).stopRecording(optionalParams); - } -/** - * Play a music file - * @method - * @param {string} callUUID - call uuid to play music file - * @param {string} url - A single URL or a list of comma separated URLs linking to an mp3 or wav file. - * @param {object} optionalParams - optional params to play music file. - * @promise {object} returns PlivoGenericResponse Object - * @fail {Error} returns Error - */ - playMusic(callUUID, urls, optionalParams) { - let errors = validate([ - {field: 'call_uuid', value: callUUID, validators: ['isRequired']}, - {field: 'urls', value: urls, validators: ['isRequired', 'isString']} - ]); + /** + * Record a Call + * @method + * @param {string} callUUID - call uuid to record call + * @param {object} optionalParams - optional params to record a call + * @promise {object} returns PlivoGenericResponse Object + * @fail {Error} returns Error + */ + record(callUUID, optionalParams) { + let errors = validate([{ + field: 'call_uuid', + value: callUUID, + validators: ['isRequired'] + }]); - if (errors) { - return errors; + if (errors) { + return errors; + } + return new Call(this[clientKey], { + id: callUUID + }).record(optionalParams); } - return new Call(this[clientKey], { - id: callUUID - }).playMusic(urls, optionalParams); - } -/** - * Stop Playing a music file - * @method - * @param {string} callUUID - call uuid to stop plaing music file - * @promise {object} returns PlivoGenericResponse Object - * @fail {Error} returns Error - */ - stopPlayingMusic(callUUID) { - let errors = validate([ - {field: 'call_uuid', value: callUUID, validators: ['isRequired']} - ]); + /** + * Stop Recording a Call + * @method + * @param {string} callUUID - call uuid to stop recording a call + * @param {object} optionalParams - optional params to stop recording a call + * @promise {object} returns PlivoGenericResponse Object + * @fail {Error} returns Error + */ + stopRecording(callUUID, optionalParams) { + let errors = validate([{ + field: 'call_uuid', + value: callUUID, + validators: ['isRequired'] + }]); - if (errors) { - return errors; + if (errors) { + return errors; + } + return new Call(this[clientKey], { + id: callUUID + }).stopRecording(optionalParams); } - return new Call(this[clientKey], { - id: callUUID - }).stopPlayingMusic(); - } -/** - * Speak text during a call - * @method - * @param {string} callUUID - call uuid to speak text during a call - * @param {string} text - text to be played. - * @param {object} optionalParams - optional params to speak text during a call - * @promise {object} returns PlivoGenericResponse Object - * @fail {Error} returns Error - */ - speakText(callUUID, text, optionalParams) { - let errors = validate([ - {field: 'call_uuid', value: callUUID, validators: ['isRequired']}, - {field: 'text', value: text, validators: ['isRequired', 'isString']} - ]); + /** + * Play a music file + * @method + * @param {string} callUUID - call uuid to play music file + * @param {string} url - A single URL or a list of comma separated URLs linking to an mp3 or wav file. + * @param {object} optionalParams - optional params to play music file. + * @promise {object} returns PlivoGenericResponse Object + * @fail {Error} returns Error + */ + playMusic(callUUID, urls, optionalParams) { + let errors = validate([{ + field: 'call_uuid', + value: callUUID, + validators: ['isRequired'] + }, + { + field: 'urls', + value: urls, + validators: ['isRequired', 'isString'] + } + ]); - if (errors) { - return errors; + if (errors) { + return errors; + } + return new Call(this[clientKey], { + id: callUUID + }).playMusic(urls, optionalParams); } - return new Call(this[clientKey], { - id: callUUID - }).speakText(text, optionalParams); - } -/** - * Stop Speaking text during a call - * @method - * @param {string} callUUID - call uuid to stop speaking text during a call - * @param {object} optionalParams - optional params to stop speaking text during a call - * @promise {object} returns PlivoGenericResponse Object - * @fail {Error} returns Error - */ - stopSpeakingText(callUUID) { - let errors = validate([ - {field: 'call_uuid', value: callUUID, validators: ['isRequired']} - ]); + /** + * Stop Playing a music file + * @method + * @param {string} callUUID - call uuid to stop plaing music file + * @promise {object} returns PlivoGenericResponse Object + * @fail {Error} returns Error + */ + stopPlayingMusic(callUUID) { + let errors = validate([{ + field: 'call_uuid', + value: callUUID, + validators: ['isRequired'] + }]); - if (errors) { - return errors; + if (errors) { + return errors; + } + return new Call(this[clientKey], { + id: callUUID + }).stopPlayingMusic(); } - return new Call(this[clientKey], { - id: callUUID - }).stopSpeakingText(); - } -/** - * Send digits on a call - * @method - * @param {string} callUUID - call uuid to send digits on a call - * @param {number} digits - digits to be send - * @param {object} optionalParams - optional params to send digits - * @promise {object} returns PlivoGenericResponse Object - * @fail {Error} returns Error - */ - sendDigits(callUUID, digits, optionalParams) { - let errors = validate([ - {field: 'call_uuid', value: callUUID, validators: ['isRequired']}, - {field: 'digits', value: digits, validators: ['isRequired']} - ]); + /** + * Speak text during a call + * @method + * @param {string} callUUID - call uuid to speak text during a call + * @param {string} text - text to be played. + * @param {object} optionalParams - optional params to speak text during a call + * @promise {object} returns PlivoGenericResponse Object + * @fail {Error} returns Error + */ + speakText(callUUID, text, optionalParams) { + let errors = validate([{ + field: 'call_uuid', + value: callUUID, + validators: ['isRequired'] + }, + { + field: 'text', + value: text, + validators: ['isRequired', 'isString'] + } + ]); - if (errors) { - return errors; + if (errors) { + return errors; + } + return new Call(this[clientKey], { + id: callUUID + }).speakText(text, optionalParams); } - return new Call(this[clientKey], { - id: callUUID - }).sendDigits(digits, optionalParams); - } -/** - * Hangup a call request - * @method - * @param {string} callUUID - call uuid to send digits on a call - * @promise {object} returns PlivoGenericResponse Object - * @fail {Error} returns Error - */ - cancel(id) { - let errors = validate([ - {field: 'call_uuid', value: id, validators: ['isRequired']} - ]); + /** + * Stop Speaking text during a call + * @method + * @param {string} callUUID - call uuid to stop speaking text during a call + * @param {object} optionalParams - optional params to stop speaking text during a call + * @promise {object} returns PlivoGenericResponse Object + * @fail {Error} returns Error + */ + stopSpeakingText(callUUID) { + let errors = validate([{ + field: 'call_uuid', + value: callUUID, + validators: ['isRequired'] + }]); - if (errors) { - return errors; + if (errors) { + return errors; + } + return new Call(this[clientKey], { + id: callUUID + }).stopSpeakingText(); } - return new Call(this[clientKey], { - id: id - }).cancel(); - } - listLiveCalls(params) { - return this[liveCallInterfaceKey].list(params); - } + /** + * Send digits on a call + * @method + * @param {string} callUUID - call uuid to send digits on a call + * @param {number} digits - digits to be send + * @param {object} optionalParams - optional params to send digits + * @promise {object} returns PlivoGenericResponse Object + * @fail {Error} returns Error + */ + sendDigits(callUUID, digits, optionalParams) { + let errors = validate([{ + field: 'call_uuid', + value: callUUID, + validators: ['isRequired'] + }, + { + field: 'digits', + value: digits, + validators: ['isRequired'] + } + ]); - getLiveCall(id) { - return this[liveCallInterfaceKey].get(id); - } + if (errors) { + return errors; + } + return new Call(this[clientKey], { + id: callUUID + }).sendDigits(digits, optionalParams); + } - listQueuedCalls() { - return this[queuedCallInterfaceKey].list(); - } + /** + * Hangup a call request + * @method + * @param {string} callUUID - call uuid to send digits on a call + * @promise {object} returns PlivoGenericResponse Object + * @fail {Error} returns Error + */ + cancel(id) { + let errors = validate([{ + field: 'call_uuid', + value: id, + validators: ['isRequired'] + }]); - getQueuedCall(id) { - return this[queuedCallInterfaceKey].get(id); - } + if (errors) { + return errors; + } + return new Call(this[clientKey], { + id: id + }).cancel(); + } + + listLiveCalls(params) { + return this[liveCallInterfaceKey].list(params); + } + + getLiveCall(id) { + return this[liveCallInterfaceKey].get(id); + } + + listQueuedCalls() { + return this[queuedCallInterfaceKey].list(); + } + + getQueuedCall(id) { + return this[queuedCallInterfaceKey].get(id); + } } export class LiveCallResource extends PlivoResource { - constructor(client, data = {}) { - super(action, LiveCallResource, idField, client); + constructor(client, data = {}) { + super(action, LiveCallResource, idField, client); - if (idField in data) { - this.id = data[idField]; + if (idField in data) { + this.id = data[idField]; + } + + extend(this, data); + this[clientKey] = client; } - - extend(this, data); - this[clientKey] = client; - } } export class QueuedCallResource extends PlivoResource { - constructor(client, data = {}) { - super(action, QueuedCallResource, idField, client); + constructor(client, data = {}) { + super(action, QueuedCallResource, idField, client); - if (idField in data) { - this.id = data[idField]; + if (idField in data) { + this.id = data[idField]; + } + + extend(this, data); + this[clientKey] = client; } - - extend(this, data); - this[clientKey] = client; - } } /** @@ -577,55 +887,72 @@ export class QueuedCallResource extends PlivoResource { * @param {object} [data] - data of call */ class LiveCallInterface extends PlivoResourceInterface { - constructor(client, data = {}) { - super(action, LiveCallResource, idField, client); - extend(this, data); + constructor(client, data = {}) { + super(action, LiveCallResource, idField, client); + extend(this, data); - this[clientKey] = client; - } - - /** - * Get A Live Call Detail - * @method - * @param {string} id - call uuid to get information of. - * @promise {object} returns LiveCallResource Object - * @fail {Error} returns Error - */ - get(id) { - let errors = validate([{field: 'id', value: id, validators: ['isRequired']}]); - - if (errors) { - return errors; + this[clientKey] = client; } - return super.get(id, { - status: 'live', - isVoiceRequest: 'true' - }); - } - list(params) { - let client = this[clientKey]; - if (params === undefined){ - params = {} - } - params.status = 'live' - params.isVoiceRequest = 'true' - return new Promise((resolve, reject) => { - client('GET', action, params) - .then(response => { - let calls = []; - response.body.calls.forEach(callUuid => { - calls.push(new LiveCallResource(client, { - callUuid: callUuid - })); - }); - resolve(calls); - }) - .catch(error => { - reject(error); + /** + * Get A Live Call Detail + * @method + * @param {string} id - call uuid to get information of. + * @promise {object} returns LiveCallResource Object + * @fail {Error} returns Error + */ + get(id) { + let errors = validate([{ + field: 'id', + value: id, + validators: ['isRequired'] + }]); + + if (errors) { + return errors; + } + let client = this[clientKey]; + return new Promise((resolve, reject) => { + if (action !== '' && !id) { + reject(new Error(this[idKey] + ' must be set')); + } + client('GET', action + (id ? id + '/' : ''), { + status: 'live', + isVoiceRequest: 'true' + }) + .then(response => { + resolve(new GetLiveCallResponse(response.body, client)); + }) + .catch(error => { + reject(error); + }); }); - }); - } + } + + //List all Live calls + list(params) { + let client = this[clientKey]; + if (params === undefined) { + params = {} + } + params.status = 'live' + params.isVoiceRequest = 'true' + return new Promise((resolve, reject) => { + client('GET', action, params) + .then(response => { + let calls = []; + response.body.calls.forEach(callUuid => { + calls.push(new LiveCallResource(client, { + callUuid: callUuid + })); + }); + resolve(new ListAllLiveCallResponse(calls[0])); + }) + .catch(error => { + reject(error); + }); + }); + } } @@ -636,49 +963,72 @@ class LiveCallInterface extends PlivoResourceInterface { * @param {object} [data] - data of call */ class QueuedCallInterface extends PlivoResourceInterface { - constructor(client, data = {}) { - super(action, QueuedCallResource, idField, client); - extend(this, data); + constructor(client, data = {}) { + super(action, QueuedCallResource, idField, client); + extend(this, data); - this[clientKey] = client; - } - - /** - * Get A Queued Call Detail - * @method - * @param {string} id - call uuid to get information of. - * @promise {object} returns QueuedCallResource Object - * @fail {Error} returns Error - */ - get(id) { - let errors = validate([{field: 'id', value: id, validators: ['isRequired']}]); - - if (errors) { - return errors; + this[clientKey] = client; } - return super.get(id, { - status: 'queued', - isVoiceRequest: 'true' - }); - } - list() { - let client = this[clientKey]; + /** + * Get A Queued Call Detail + * @method + * @param {string} id - call uuid to get information of. + * @promise {object} returns QueuedCallResource Object + * @fail {Error} returns Error + */ + get(id) { + let errors = validate([{ + field: 'id', + value: id, + validators: ['isRequired'] + }]); - return new Promise((resolve, reject) => { - client('GET', action, {status: 'queued', isVoiceRequest: 'true'}) - .then(response => { - let calls = []; - response.body.calls.forEach(callUuid => { - calls.push(new QueuedCallResource(client, { - callUuid: callUuid - })); - }); - resolve(calls); - }) - .catch(error => { - reject(error); + if (errors) { + return errors; + } + let client = this[clientKey]; + + return new Promise((resolve, reject) => { + if (action !== '' && !id) { + reject(new Error(this[idKey] + ' must be set')); + } + + client('GET', action + (id ? id + '/' : ''), { + status: 'queued', + isVoiceRequest: 'true' + }) + .then(response => { + resolve(new GetQueuedCallResponse(response.body, client)); + }) + .catch(error => { + reject(error); + }); }); - }); - } -} + + + } + + list() { + let client = this[clientKey]; + + return new Promise((resolve, reject) => { + client('GET', action, { + status: 'queued', + isVoiceRequest: 'true' + }) + .then(response => { + let calls = []; + response.body.calls.forEach(callUuid => { + calls.push(new QueuedCallResource(client, { + callUuid: callUuid + })); + }); + resolve(new ListAllQueuedCalls(calls[0])); + }) + .catch(error => { + reject(error); + }); + }); + } +} \ No newline at end of file diff --git a/lib/resources/callFeedback.js b/lib/resources/callFeedback.js index 0bfa535..1037dd0 100644 --- a/lib/resources/callFeedback.js +++ b/lib/resources/callFeedback.js @@ -1,7 +1,24 @@ -import {extend, validate} from '../utils/common.js'; -import {PlivoResource, PlivoResourceInterface} from '../base'; import * as _ from "lodash"; +import { + PlivoResource, + PlivoResourceInterface +} from '../base'; +import { + extend, + validate +} from '../utils/common.js'; + +export class CallFeedbackResponse { + constructor(params) { + params = params || {}; + this.apiId = params.apiId; + this.message = params.message; + this.status = params.status; + + } +} + const clientKey = Symbol(); const action = 'Call/'; const idField = 'callUuid'; @@ -10,14 +27,14 @@ const CALLINSIGHTS_BASE_URL = 'https://stats.plivo.com/' export class CallFeedback extends PlivoResource { constructor(client, data = {}) { super(action, Call, idField, client); - + if (idField in data) { - this.id = data[idField]; + this.id = data[idField]; } - + extend(this, data); this[clientKey] = client; - } + } } /** @@ -30,15 +47,22 @@ export class CallFeedbackInterface extends PlivoResourceInterface { constructor(client, data = {}) { super(action, CallFeedback, idField, client); extend(this, data); - + this[clientKey] = client; - } - - create(callUUID, rating, issues=[], notes="") { - let errors = validate([ - {field: 'callUUId', value: callUUID, validators: ['isRequired']}, - {field: 'rating', value: rating, validators: ['isRequired']} - ]); + } + + create(callUUID, rating, issues = [], notes = "") { + let errors = validate([{ + field: 'callUUId', + value: callUUID, + validators: ['isRequired'] + }, + { + field: 'rating', + value: rating, + validators: ['isRequired'] + } + ]); if (errors) { return errors; @@ -55,7 +79,17 @@ export class CallFeedbackInterface extends PlivoResourceInterface { params.isCallInsightsRequest = ""; params.CallInsightsBaseUrl = CALLINSIGHTS_BASE_URL; params.CallInsightsRequestPath = `v1/Call/${callUUID}/Feedback/`; - return super.create(params); + + let client = this[clientKey]; + return new Promise((resolve, reject) => { + client('POST', action, params) + .then(response => { + resolve(new CallFeedbackResponse(response.body, idField)); + }) + .catch(error => { + reject(error); + }); + }); } - + } \ No newline at end of file diff --git a/lib/resources/conferences.js b/lib/resources/conferences.js index 1073ba7..f5bfff7 100644 --- a/lib/resources/conferences.js +++ b/lib/resources/conferences.js @@ -1,5 +1,11 @@ -import {extend, validate} from '../utils/common.js'; -import {PlivoResource, PlivoResourceInterface} from '../base'; +import { + PlivoResource, + PlivoResourceInterface +} from '../base'; +import { + extend, + validate +} from '../utils/common.js'; const clientKey = Symbol(); const action = 'Conference/'; @@ -11,288 +17,434 @@ const idField = 'conferenceName'; * @param {function} client - make api call * @param {object} [data] - data of call */ + +export class MuteMemberResponse { + constructor(params) { + params = params || {}; + this.apiId = params.apiId; + this.memberId = params.memberId; + this.message = params.message; + + } +} + +export class StartRecordingConferenceResponse { + constructor(params) { + params = params || {}; + this.apiId = params.apiId; + this.message = params.message; + this.recordingId = params.recordingId; + this.url = params.url; + } +} + +export class RetrieveConferenceResponse { + constructor(params) { + params = params || {}; + this.apiId = params.apiId; + this.conferenceMemberCount = params.conferenceMemberCount; + this.conferenceName = params.conferenceName; + this.conferenceRunTime = params.conferenceRunTime; + this.members = params.members; + } +} + +export class ListAllConferenceResponse { + constructor(params) { + params = params || {}; + this.apiId = params.apiId; + this.conferences = params.conferences; + } +} +export class SpeakMemberResponse { + constructor(params) { + params = params || {}; + this.apiId = params.apiId; + this.memberId = params.memberId; + this.message = params.message; + + } +} + +export class PlayAudioMemberResponse { + constructor(params) { + params = params || {}; + this.apiId = params.apiId; + this.memberId = params.memberId; + this.message = params.message; + + } +} + +export class DeafMemberResponse { + constructor(params) { + params = params || {}; + this.apiId = params.apiId; + this.memberId = params.memberId; + this.message = params.message; + + } +} + export class Conference extends PlivoResource { - constructor(client, data = {}) { - super(action, Conference, idField, client); + constructor(client, data = {}) { + super(action, Conference, idField, client); - if (idField in data) { - this.id = data[idField]; + if (idField in data) { + this.id = data[idField]; + } + + extend(this, data); + this[clientKey] = client; } - extend(this, data); - this[clientKey] = client; - } - -/** - * hangup conference - * @method - * @promise {Boolean} return true if call hung up - * @fail {Error} return Error - */ - hangup() { - let params = {} - params.isVoiceRequest = 'true' - return super.delete(params); - } - -/** - * hangup member from conference - * @method - * @param {string} memberId - id of member to be hangup - * @promise {PlivoGenericResponse} return PlivoGenericResponse if success - * @fail {Error} return Error - */ - hangupMember(memberId) { - let errors = validate([ - {field: 'member_id', value: memberId, validators: ['isRequired']} - ]); - - if (errors) { - return errors; + /** + * hangup conference + * @method + * @promise {Boolean} return true if call hung up + * @fail {Error} return Error + */ + hangup() { + let params = {} + params.isVoiceRequest = 'true' + return super.delete(params); } - let params = {} - params.isVoiceRequest = 'true' - return super.executeAction(this.id + '/Member/' + memberId + '/', 'DELETE', params); - } -/** - * kick member from conference - * @method - * @param {string} memberId - id of member - * @promise {PlivoGenericResponse} return PlivoGenericResponse if success - * @fail {Error} return Error - */ - kickMember(memberId) { - let errors = validate([ - {field: 'member_id', value: memberId, validators: ['isRequired']} - ]); + /** + * hangup member from conference + * @method + * @param {string} memberId - id of member to be hangup + * @promise {PlivoGenericResponse} return PlivoGenericResponse if success + * @fail {Error} return Error + */ + hangupMember(memberId) { + let errors = validate([{ + field: 'member_id', + value: memberId, + validators: ['isRequired'] + }]); - if (errors) { - return errors; + if (errors) { + return errors; + } + let params = {} + params.isVoiceRequest = 'true' + return super.executeAction(this.id + '/Member/' + memberId + '/', 'DELETE', params); } - let params = {} - params.isVoiceRequest = 'true' - return super.executeAction(this.id + '/Member/' + memberId + '/Kick/', 'POST',params); - } -/** - * mute member from conference - * @method - * @param {string} memberId - id of member - * @promise {PlivoGenericResponse} return PlivoGenericResponse if success - * @fail {Error} return Error - */ - muteMember(memberId) { - let errors = validate([ - {field: 'member_id', value: memberId, validators: ['isRequired']} - ]); + /** + * kick member from conference + * @method + * @param {string} memberId - id of member + * @promise {PlivoGenericResponse} return PlivoGenericResponse if success + * @fail {Error} return Error + */ + kickMember(memberId) { + let errors = validate([{ + field: 'member_id', + value: memberId, + validators: ['isRequired'] + }]); - if (errors) { - return errors; + if (errors) { + return errors; + } + let params = {} + params.isVoiceRequest = 'true' + return super.executeAction(this.id + '/Member/' + memberId + '/Kick/', 'POST', params); } - let params = {} - params.isVoiceRequest = 'true' - return super.executeAction(this.id + '/Member/' + memberId + '/Mute/', 'POST',params); - } -/** - * unmute member from conference - * @method - * @param {string} memberId - id of member - * @promise {PlivoGenericResponse} return PlivoGenericResponse if success - * @fail {Error} return Error - */ - unmuteMember(memberId) { - let errors = validate([ - {field: 'member_id', value: memberId, validators: ['isRequired']} - ]); + /** + * mute member from conference + * @method + * @param {string} memberId - id of member + * @promise {PlivoGenericResponse} return PlivoGenericResponse if success + * @fail {Error} return Error + */ + muteMember(memberId) { + let errors = validate([{ + field: 'member_id', + value: memberId, + validators: ['isRequired'] + }]); - if (errors) { - return errors; + if (errors) { + return errors; + } + let params = {} + params.isVoiceRequest = 'true' + let client = this[clientKey]; + return new Promise((resolve, reject) => { + client('POST', action + this.id + '/Member/' + memberId + '/Mute/', params) + .then(response => { + resolve(new MuteMemberResponse(response.body, idField)); + }) + .catch(error => { + reject(error); + }); + }); } - let params = {} - params.isVoiceRequest = 'true' - return super.executeAction(this.id + '/Member/' + memberId + '/Mute/', 'DELETE', params); - } -/** - * deaf member from conference - * @method - * @param {string} memberId - id of member - * @promise {PlivoGenericResponse} return PlivoGenericResponse if success - * @fail {Error} return Error - */ - deafMember(memberId) { - let errors = validate([ - {field: 'member_id', value: memberId, validators: ['isRequired']} - ]); + /** + * unmute member from conference + * @method + * @param {string} memberId - id of member + * @promise {PlivoGenericResponse} return PlivoGenericResponse if success + * @fail {Error} return Error + */ + unmuteMember(memberId) { + let errors = validate([{ + field: 'member_id', + value: memberId, + validators: ['isRequired'] + }]); - if (errors) { - return errors; + if (errors) { + return errors; + } + let params = {} + params.isVoiceRequest = 'true' + return super.executeAction(this.id + '/Member/' + memberId + '/Mute/', 'DELETE', params); } - let params = {} - params.isVoiceRequest = 'true' - return super.executeAction(this.id + '/Member/' + memberId + '/Deaf/', 'POST', params); - } -/** - * undeaf member from conference - * @method - * @param {string} memberId - id of member - * @promise {PlivoGenericResponse} return PlivoGenericResponse if success - * @fail {Error} return Error - */ - undeafMember(memberId) { - let errors = validate([ - {field: 'member_id', value: memberId, validators: ['isRequired']} - ]); + /** + * deaf member from conference + * @method + * @param {string} memberId - id of member + * @promise {PlivoGenericResponse} return PlivoGenericResponse if success + * @fail {Error} return Error + */ + deafMember(memberId) { + let errors = validate([{ + field: 'member_id', + value: memberId, + validators: ['isRequired'] + }]); - if (errors) { - return errors; + if (errors) { + return errors; + } + let params = {} + params.isVoiceRequest = 'true' + let client = this[clientKey]; + return new Promise((resolve, reject) => { + client('POST', action + this.id + '/Member/' + memberId + '/Deaf/', params) + .then(response => { + resolve(new DeafMemberResponse(response.body, idField)); + }) + .catch(error => { + reject(error); + }); + }); } - let params = {} - params.isVoiceRequest = 'true' - return super.executeAction(this.id + '/Member/' + memberId + '/Deaf/', 'DELETE', params); - } -/** - * play audio to member - * @method - * @param {string} memberId - id of member - * @param {string} url - url for audio - * @promise {PlivoGenericResponse} return PlivoGenericResponse if success - * @fail {Error} return Error - */ - playAudioToMember(memberId, url) { - let errors = validate([ - {field: 'member_id', value: memberId, validators: ['isRequired']}, - {field: 'url', value: url, validators: ['isRequired']} - ]); + /** + * undeaf member from conference + * @method + * @param {string} memberId - id of member + * @promise {PlivoGenericResponse} return PlivoGenericResponse if success + * @fail {Error} return Error + */ + undeafMember(memberId) { + let errors = validate([{ + field: 'member_id', + value: memberId, + validators: ['isRequired'] + }]); - if (errors) { - return errors; + if (errors) { + return errors; + } + let params = {} + params.isVoiceRequest = 'true' + return super.executeAction(this.id + '/Member/' + memberId + '/Deaf/', 'DELETE', params); } - let params = {url: url}; - params.isVoiceRequest = 'true'; - return super.executeAction(this.id + '/Member/' + memberId + '/Play/', 'POST', params); - } -/** - * stop playing audio to member - * @method - * @param {string} memberId - id of member - * @promise {PlivoGenericResponse} return PlivoGenericResponse if success - * @fail {Error} return Error - */ - stopPlayingAudioToMember(memberId) { - let errors = validate([ - {field: 'member_id', value: memberId, validators: ['isRequired']} - ]); + /** + * play audio to member + * @method + * @param {string} memberId - id of member + * @param {string} url - url for audio + * @promise {PlivoGenericResponse} return PlivoGenericResponse if success + * @fail {Error} return Error + */ + playAudioToMember(memberId, url) { + let errors = validate([{ + field: 'member_id', + value: memberId, + validators: ['isRequired'] + }, + { + field: 'url', + value: url, + validators: ['isRequired'] + } + ]); - if (errors) { - return errors; + if (errors) { + return errors; + } + let params = { + url: url + }; + params.isVoiceRequest = 'true'; + let client = this[clientKey]; + return new Promise((resolve, reject) => { + client('POST', action + this.id + '/Member/' + memberId + '/Play/', params) + .then(response => { + resolve(new PlayAudioMemberResponse(response.body, idField)); + }) + .catch(error => { + reject(error); + }); + }); } - let params = {}; - params.isVoiceRequest = 'true'; - return super.executeAction(this.id + '/Member/' + memberId + '/Play/', 'DELETE', params); - } -/** - * speak text to member - * @method - * @param {string} memberId - id of member - * @param {string} text - text to be speak to member - * @param {object} optionalParams - optionalPrams to speak text - * @param {string} [optionalParams.voice] The voice to be used. Can be MAN or WOMAN. Defaults to WOMAN. - * @param {string} [optionalParams.language] The language to be used. Defaults to en-US. - * @promise {PlivoGenericResponse} return PlivoGenericResponse if success - * @fail {Error} return Error - */ - speakTextToMember(memberId, text, optionalParams) { - let errors = validate([ - {field: 'member_id', value: memberId, validators: ['isRequired']}, - {field: 'text', value: text, validators: ['isRequired']} - ]); + /** + * stop playing audio to member + * @method + * @param {string} memberId - id of member + * @promise {PlivoGenericResponse} return PlivoGenericResponse if success + * @fail {Error} return Error + */ + stopPlayingAudioToMember(memberId) { + let errors = validate([{ + field: 'member_id', + value: memberId, + validators: ['isRequired'] + }]); - if (errors) { - return errors; + if (errors) { + return errors; + } + let params = {}; + params.isVoiceRequest = 'true'; + return super.executeAction(this.id + '/Member/' + memberId + '/Play/', 'DELETE', params); } - let params = optionalParams || {}; - params.text = text; - params.isVoiceRequest = 'true'; - return super.executeAction(this.id + '/Member/' + memberId + '/Speak/', 'POST', params); - } -/** - * stop speaking text to member - * @method - * @param {string} memberId - id of member - * @promise {PlivoGenericResponse} return PlivoGenericResponse if success - * @fail {Error} return Error - */ - stopSpeakingTextToMember(memberId) { - let errors = validate([ - {field: 'member_id', value: memberId, validators: ['isRequired']} - ]); + /** + * speak text to member + * @method + * @param {string} memberId - id of member + * @param {string} text - text to be speak to member + * @param {object} optionalParams - optionalPrams to speak text + * @param {string} [optionalParams.voice] The voice to be used. Can be MAN or WOMAN. Defaults to WOMAN. + * @param {string} [optionalParams.language] The language to be used. Defaults to en-US. + * @promise {PlivoGenericResponse} return PlivoGenericResponse if success + * @fail {Error} return Error + */ + speakTextToMember(memberId, text, optionalParams) { + let errors = validate([{ + field: 'member_id', + value: memberId, + validators: ['isRequired'] + }, + { + field: 'text', + value: text, + validators: ['isRequired'] + } + ]); - if (errors) { - return errors; + if (errors) { + return errors; + } + let params = optionalParams || {}; + params.text = text; + params.isVoiceRequest = 'true'; + let client = this[clientKey]; + return new Promise((resolve, reject) => { + client('POST', action + this.id + '/Member/' + memberId + '/Speak/', params) + .then(response => { + resolve(new SpeakMemberResponse(response.body, idField)); + }) + .catch(error => { + reject(error); + }); + }); } - let params = {}; - params.isVoiceRequest = 'true'; - return super.executeAction(this.id + '/Member/' + memberId + '/Speak/', 'DELETE'); - } - /** - * Record conference - * @method - * @param {object} params - optional params to record conference - * @param {string} [params.fileFormat] The file format of the record can be of mp3 or wav format. Defaults to mp3 format. - * @param {string} [params.transcriptionType] The type of transcription required. The following values are allowed: - * - auto - This is the default value. Transcription is completely automated; turnaround time is about 5 minutes. - * - hybrid - Transcription is a combination of automated and human verification processes; turnaround time is about 10-15 minutes. - * @param {string} [params.transcriptionUrl] The URL where the transcription is available. - * @param {string} [params.transcriptionMethod] The method used to invoke the transcription_url. Defaults to POST. - * @param {string} [params.callbackUrl] The URL invoked by the API when the recording ends. - * @param {string} [params.callbackMethod] The method which is used to invoke the callback_url URL. Defaults to POST. - * @promise {PlivoGenericResponse} return PlivoGenericResponse if success - * @fail {Error} return Error - */ - record(params) { - return this.startRecording(params); - } + /** + * stop speaking text to member + * @method + * @param {string} memberId - id of member + * @promise {PlivoGenericResponse} return PlivoGenericResponse if success + * @fail {Error} return Error + */ + stopSpeakingTextToMember(memberId) { + let errors = validate([{ + field: 'member_id', + value: memberId, + validators: ['isRequired'] + }]); - /** - * Record conference - * @method - * @param {object} params - optional params to record conference - * @param {string} [params.fileFormat] The file format of the record can be of mp3 or wav format. Defaults to mp3 format. - * @param {string} [params.transcriptionType] The type of transcription required. The following values are allowed: - * - auto - This is the default value. Transcription is completely automated; turnaround time is about 5 minutes. - * - hybrid - Transcription is a combination of automated and human verification processes; turnaround time is about 10-15 minutes. - * @param {string} [params.transcriptionUrl] The URL where the transcription is available. - * @param {string} [params.transcriptionMethod] The method used to invoke the transcription_url. Defaults to POST. - * @param {string} [params.callbackUrl] The URL invoked by the API when the recording ends. - * @param {string} [params.callbackMethod] The method which is used to invoke the callback_url URL. Defaults to POST. - * @promise {PlivoGenericResponse} return PlivoGenericResponse if success - * @fail {Error} return Error - */ - startRecording(params={}) { - params.isVoiceRequest = 'true'; - return super.executeAction(this.id + '/Record/', 'POST', params); - } + if (errors) { + return errors; + } + let params = {}; + params.isVoiceRequest = 'true'; + return super.executeAction(this.id + '/Member/' + memberId + '/Speak/', 'DELETE'); + } -/** - * stop recording conference - * @method - * @promise {PlivoGenericResponse} return PlivoGenericResponse if success - * @fail {Error} return Error - */ - stopRecording() { - let params = {}; - params.isVoiceRequest = 'true'; - return super.executeAction(this.id + '/Record/', 'DELETE', params); - } + /** + * Record conference + * @method + * @param {object} params - optional params to record conference + * @param {string} [params.fileFormat] The file format of the record can be of mp3 or wav format. Defaults to mp3 format. + * @param {string} [params.transcriptionType] The type of transcription required. The following values are allowed: + * - auto - This is the default value. Transcription is completely automated; turnaround time is about 5 minutes. + * - hybrid - Transcription is a combination of automated and human verification processes; turnaround time is about 10-15 minutes. + * @param {string} [params.transcriptionUrl] The URL where the transcription is available. + * @param {string} [params.transcriptionMethod] The method used to invoke the transcription_url. Defaults to POST. + * @param {string} [params.callbackUrl] The URL invoked by the API when the recording ends. + * @param {string} [params.callbackMethod] The method which is used to invoke the callback_url URL. Defaults to POST. + * @promise {PlivoGenericResponse} return PlivoGenericResponse if success + * @fail {Error} return Error + */ + record(params) { + return this.startRecording(params); + } + + /** + * Record conference + * @method + * @param {object} params - optional params to record conference + * @param {string} [params.fileFormat] The file format of the record can be of mp3 or wav format. Defaults to mp3 format. + * @param {string} [params.transcriptionType] The type of transcription required. The following values are allowed: + * - auto - This is the default value. Transcription is completely automated; turnaround time is about 5 minutes. + * - hybrid - Transcription is a combination of automated and human verification processes; turnaround time is about 10-15 minutes. + * @param {string} [params.transcriptionUrl] The URL where the transcription is available. + * @param {string} [params.transcriptionMethod] The method used to invoke the transcription_url. Defaults to POST. + * @param {string} [params.callbackUrl] The URL invoked by the API when the recording ends. + * @param {string} [params.callbackMethod] The method which is used to invoke the callback_url URL. Defaults to POST. + * @promise {PlivoGenericResponse} return PlivoGenericResponse if success + * @fail {Error} return Error + */ + startRecording(params = {}) { + params.isVoiceRequest = 'true'; + let client = this[clientKey]; + return new Promise((resolve, reject) => { + client('POST', action + this.id + '/Record/', params) + .then(response => { + resolve(new StartRecordingConferenceResponse(response.body, idField)); + }) + .catch(error => { + reject(error); + }); + }); + } + + /** + * stop recording conference + * @method + * @promise {PlivoGenericResponse} return PlivoGenericResponse if success + * @fail {Error} return Error + */ + stopRecording() { + let params = {}; + params.isVoiceRequest = 'true'; + return super.executeAction(this.id + '/Record/', 'DELETE', params); + } } /** @@ -303,387 +455,486 @@ export class Conference extends PlivoResource { */ export class ConferenceInterface extends PlivoResourceInterface { - constructor(client, data = {}) { - super(action, Conference, idField, client); - extend(this, data); + constructor(client, data = {}) { + super(action, Conference, idField, client); + extend(this, data); - this[clientKey] = client; - } - -/** - * get conference by id - * @method - * @param {string} id - id of conference - * @promise {@link Conference} return {@link Conference} object if success - * @fail {Error} return Error - */ - get(id) { - let errors = validate([ - {field: 'id', value: id, validators: ['isRequired']} - ]); - - if (errors) { - return errors; + this[clientKey] = client; } - let params = {}; - params.isVoiceRequest = 'true'; - return super.get(id, params); - } -/** - * get all conferences. returns name of all conferences - * @method - * @promise {@link [Conference]} returns list of {@link Conference} objects if success - * @fail {Error} return Error - */ - list() { - let client = this[clientKey]; - let params = {} - params.isVoiceRequest = 'true'; - return new Promise((resolve, reject) => { - client('GET', action, params) - .then(response => { - let conferences = []; - response.body.conferences.forEach(conference => { - conferences.push(new Conference(client, { - name: conference - })); - }); - resolve(response.body); - }) - .catch(error => { - reject(error); + /** + * get conference by id + * @method + * @param {string} id - id of conference + * @promise {@link Conference} return {@link Conference} object if success + * @fail {Error} return Error + */ + get(id) { + let errors = validate([{ + field: 'id', + value: id, + validators: ['isRequired'] + }]); + + if (errors) { + return errors; + } + let params = {}; + params.isVoiceRequest = 'true'; + //return super.get(id, params); + let client = this[clientKey]; + return new Promise((resolve, reject) => { + if (action !== '' && !id) { + reject(new Error(this[idKey] + ' must be set')); + } + client('GET', action + (id ? id + '/' : ''), params) + .then(response => { + resolve(new RetrieveConferenceResponse(response.body, client)); + }) + .catch(error => { + reject(error); + }); }); - }); - } - -/** - * hangup conference - * @method - * @param {string} conferenceName - name of conference - * @promise {@link Conference} return {@link Conference} object if success - * @fail {Error} return Error - */ - hangup(conferenceName) { - let errors = validate([ - {field: 'conference_name', value: conferenceName, validators: ['isRequired']} - ]); - - if (errors) { - return errors; - } - let params = {}; - params.isVoiceRequest = 'true'; - return new Conference(this[clientKey], { - id: conferenceName - }).delete(params); - } - -/** - * hangup all - * @method - * @promise {@link PlivoGenericResponse} returns object of PlivoGenericResponse if success - * @fail {Error} return Error - */ - hangupAll() { - let params = {}; - params.isVoiceRequest = 'true'; - return new Conference(this[clientKey]) - .executeAction('', 'DELETE', params); - } - -/** - * hangup member from conference - * @method - * @param {string} id - id of conference - * @param {string} memberId - id of member to be hangup - * @promise {PlivoGenericResponse} return PlivoGenericResponse if success - * @fail {Error} return Error - */ - hangupMember(id, memberId) { - let errors = validate([ - {field: 'id', value: id, validators: ['isRequired']}, - {field: 'memberId', value: memberId, validators: ['isRequired']} - ]); - - if (errors) { - return errors; - } - return new Conference(this[clientKey], { - id: id - }).hangupMember(memberId); - } - -/** - * kick member from conference - * @method - * @param {string} id - id of conference - * @param {string} memberId - id of member - * @promise {PlivoGenericResponse} return PlivoGenericResponse if success - * @fail {Error} return Error - */ - kickMember(id, memberId) { - let errors = validate([ - {field: 'id', value: id, validators: ['isRequired']}, - {field: 'memberId', value: memberId, validators: ['isRequired']} - ]); - - if (errors) { - return errors; - } - return new Conference(this[clientKey], { - id: id - }).kickMember(memberId); - } - -/** - * mute member - * @method - * @param {string} id - id of conference - * @param {string} memberId - id of member - * @promise {PlivoGenericResponse} return PlivoGenericResponse if success - * @fail {Error} return Error - */ - muteMember(id, memberId) { - let errors = validate([ - {field: 'id', value: id, validators: ['isRequired']}, - {field: 'memberId', value: memberId, validators: ['isRequired']} - ]); - - if (errors) { - return errors; - } - return new Conference(this[clientKey], { - id: id - }).muteMember(memberId); - } - -/** - * unmute member - * @method - * @param {string} id - id of conference - * @param {string} memberId - id of member - * @promise {PlivoGenericResponse} return PlivoGenericResponse if success - * @fail {Error} return Error - */ - unmuteMember(id, memberId) { - let errors = validate([ - {field: 'id', value: id, validators: ['isRequired']}, - {field: 'memberId', value: memberId, validators: ['isRequired']} - ]); - - if (errors) { - return errors; - } - return new Conference(this[clientKey], { - id: id - }).unmuteMember(memberId); - } - -/** - * deaf member - * @method - * @param {string} id - id of conference - * @param {string} memberId - id of member - * @promise {PlivoGenericResponse} return PlivoGenericResponse if success - * @fail {Error} return Error - */ - deafMember(id, memberId) { - let errors = validate([ - {field: 'id', value: id, validators: ['isRequired']}, - {field: 'memberId', value: memberId, validators: ['isRequired']} - ]); - - if (errors) { - return errors; - } - return new Conference(this[clientKey], { - id: id - }).deafMember(memberId); - } - -/** - * undeaf member - * @method - * @param {string} id - id of conference - * @param {string} memberId - id of member - * @promise {PlivoGenericResponse} return PlivoGenericResponse if success - * @fail {Error} return Error - */ - undeafMember(id, memberId) { - let errors = validate([ - {field: 'id', value: id, validators: ['isRequired']}, - {field: 'memberId', value: memberId, validators: ['isRequired']} - ]); - - if (errors) { - return errors; - } - return new Conference(this[clientKey], { - id: id - }).undeafMember(memberId); - } -/** - * play audio to member - * @method - * @param {string} id - id of conference - * @param {string} memberId - id of member - * @param {string} url - urls for audio - * @promise {PlivoGenericResponse} return PlivoGenericResponse if success - * @fail {Error} return Error - */ - - playAudioToMember(id, memberId, url) { - let errors = validate([ - {field: 'id', value: id, validators: ['isRequired']}, - {field: 'memberId', value: memberId, validators: ['isRequired']}, - {field: 'url', value: url, validators: ['isRequired']} - ]); - - if (errors) { - return errors; - } - return new Conference(this[clientKey], { - id: id - }).playAudioToMember(memberId, url); - } - -/** - * stop playing audio to member - * @method - * @param {string} id - id of conference - * @param {string} memberId - id of member - * @promise {PlivoGenericResponse} return PlivoGenericResponse if success - * @fail {Error} return Error - */ - stopPlayingAudioToMember(id, memberId) { - let errors = validate([ - {field: 'id', value: id, validators: ['isRequired']}, - {field: 'memberId', value: memberId, validators: ['isRequired']} - ]); - - if (errors) { - return errors; - } - return new Conference(this[clientKey], { - id: id - }).stopPlayingAudioToMember(memberId); - } - -/** - * speak text to member - * @method - * @param {string} id - id of conference - * @param {string} memberId - id of member - * @param {string} text - text to speak - * @param {object} optionalParams - optional params - * @promise {PlivoGenericResponse} return PlivoGenericResponse if success - * @fail {Error} return Error - */ - speakTextToMember(id, memberId, text, optionalParams) { - let errors = validate([ - {field: 'id', value: id, validators: ['isRequired']}, - {field: 'memberId', value: memberId, validators: ['isRequired']}, - {field: 'text', value: text, validators: ['isRequired']} - ]); - - if (errors) { - return errors; - } - return new Conference(this[clientKey], { - id: id - }).speakTextToMember(memberId, text, optionalParams); - } - -/** - * stop speaking text to member - * @method - * @param {string} id - id of conference - * @param {string} memberId - id of member - * @promise {PlivoGenericResponse} return PlivoGenericResponse if success - * @fail {Error} return Error - */ - stopSpeakingTextToMember(id, memberId) { - let errors = validate([ - {field: 'id', value: id, validators: ['isRequired']}, - {field: 'memberId', value: memberId, validators: ['isRequired']} - ]); - - if (errors) { - return errors; - } - return new Conference(this[clientKey], { - id: id - }).stopSpeakingTextToMember(memberId); - } - -/** - * record conference - * @method - * @param {string} id - id of conference - * @param {object} params - optional params to record conference - * @param {string} [params.fileFormat] The file format of the record can be of mp3 or wav format. Defaults to mp3 format. - * @param {string} [params.transcriptionType] The type of transcription required. The following values are allowed: - * - auto - This is the default value. Transcription is completely automated; turnaround time is about 5 minutes. - * - hybrid - Transcription is a combination of automated and human verification processes; turnaround time is about 10-15 minutes. - * @param {string} [params.transcriptionUrl] The URL where the transcription is available. - * @param {string} [params.transcriptionMethod] The method used to invoke the transcription_url. Defaults to POST. - * @param {string} [params.callbackUrl] The URL invoked by the API when the recording ends. - * @param {string} [params.callbackMethod] The method which is used to invoke the callback_url URL. Defaults to POST. - * @promise {PlivoGenericResponse} return PlivoGenericResponse if success - * @fail {Error} return Error - */ - record(id, params) { - return this.startRecording(id, params); - } - -/** - * record conference - * @method - * @param {string} id - id of conference - * @param {object} params - optional params to record conference - * @param {string} [params.fileFormat] The file format of the record can be of mp3 or wav format. Defaults to mp3 format. - * @param {string} [params.transcriptionType] The type of transcription required. The following values are allowed: - * - auto - This is the default value. Transcription is completely automated; turnaround time is about 5 minutes. - * - hybrid - Transcription is a combination of automated and human verification processes; turnaround time is about 10-15 minutes. - * @param {string} [params.transcriptionUrl] The URL where the transcription is available. - * @param {string} [params.transcriptionMethod] The method used to invoke the transcription_url. Defaults to POST. - * @param {string} [params.callbackUrl] The URL invoked by the API when the recording ends. - * @param {string} [params.callbackMethod] The method which is used to invoke the callback_url URL. Defaults to POST. - * @promise {PlivoGenericResponse} return PlivoGenericResponse if success - * @fail {Error} return Error - */ - startRecording(id, params) { - let errors = validate([ - {field: 'id', value: id, validators: ['isRequired']} - ]); - - if (errors) { - return errors; } - return new Conference(this[clientKey], { - id: id - }).startRecording(params); - } - -/** - * stop recording - * @method - * @param {string} id - id of conference - * @promise {PlivoGenericResponse} return PlivoGenericResponse if success - * @fail {Error} return Error - */ - stopRecording(id) { - let errors = validate([ - {field: 'id', value: id, validators: ['isRequired']} - ]); - - if (errors) { - return errors; + /** + * get all conferences. returns name of all conferences + * @method + * @promise {@link [Conference]} returns list of {@link Conference} objects if success + * @fail {Error} return Error + */ + list() { + let client = this[clientKey]; + let params = {} + params.isVoiceRequest = 'true'; + return new Promise((resolve, reject) => { + client('GET', action, params) + .then(response => { + let conferences = []; + response.body.conferences.forEach(conference => { + conferences.push(new Conference(client, { + name: conference + })); + }); + resolve(new ListAllConferenceResponse(response.body)); + }) + .catch(error => { + reject(error); + }); + }); } - return new Conference(this[clientKey], { - id: id - }).stopRecording(); - } -} + + /** + * hangup conference + * @method + * @param {string} conferenceName - name of conference + * @promise {@link Conference} return {@link Conference} object if success + * @fail {Error} return Error + */ + hangup(conferenceName) { + let errors = validate([{ + field: 'conference_name', + value: conferenceName, + validators: ['isRequired'] + }]); + + if (errors) { + return errors; + } + let params = {}; + params.isVoiceRequest = 'true'; + return new Conference(this[clientKey], { + id: conferenceName + }).delete(params); + } + + /** + * hangup all + * @method + * @promise {@link PlivoGenericResponse} returns object of PlivoGenericResponse if success + * @fail {Error} return Error + */ + hangupAll() { + let params = {}; + params.isVoiceRequest = 'true'; + return new Conference(this[clientKey]) + .executeAction('', 'DELETE', params); + } + + /** + * hangup member from conference + * @method + * @param {string} id - id of conference + * @param {string} memberId - id of member to be hangup + * @promise {PlivoGenericResponse} return PlivoGenericResponse if success + * @fail {Error} return Error + */ + hangupMember(id, memberId) { + let errors = validate([{ + field: 'id', + value: id, + validators: ['isRequired'] + }, + { + field: 'memberId', + value: memberId, + validators: ['isRequired'] + } + ]); + + if (errors) { + return errors; + } + return new Conference(this[clientKey], { + id: id + }).hangupMember(memberId); + } + + /** + * kick member from conference + * @method + * @param {string} id - id of conference + * @param {string} memberId - id of member + * @promise {PlivoGenericResponse} return PlivoGenericResponse if success + * @fail {Error} return Error + */ + kickMember(id, memberId) { + let errors = validate([{ + field: 'id', + value: id, + validators: ['isRequired'] + }, + { + field: 'memberId', + value: memberId, + validators: ['isRequired'] + } + ]); + + if (errors) { + return errors; + } + return new Conference(this[clientKey], { + id: id + }).kickMember(memberId); + } + + /** + * mute member + * @method + * @param {string} id - id of conference + * @param {string} memberId - id of member + * @promise {PlivoGenericResponse} return PlivoGenericResponse if success + * @fail {Error} return Error + */ + muteMember(id, memberId) { + let errors = validate([{ + field: 'id', + value: id, + validators: ['isRequired'] + }, + { + field: 'memberId', + value: memberId, + validators: ['isRequired'] + } + ]); + + if (errors) { + return errors; + } + return new Conference(this[clientKey], { + id: id + }).muteMember(memberId); + } + + /** + * unmute member + * @method + * @param {string} id - id of conference + * @param {string} memberId - id of member + * @promise {PlivoGenericResponse} return PlivoGenericResponse if success + * @fail {Error} return Error + */ + unmuteMember(id, memberId) { + let errors = validate([{ + field: 'id', + value: id, + validators: ['isRequired'] + }, + { + field: 'memberId', + value: memberId, + validators: ['isRequired'] + } + ]); + + if (errors) { + return errors; + } + return new Conference(this[clientKey], { + id: id + }).unmuteMember(memberId); + } + + /** + * deaf member + * @method + * @param {string} id - id of conference + * @param {string} memberId - id of member + * @promise {PlivoGenericResponse} return PlivoGenericResponse if success + * @fail {Error} return Error + */ + deafMember(id, memberId) { + let errors = validate([{ + field: 'id', + value: id, + validators: ['isRequired'] + }, + { + field: 'memberId', + value: memberId, + validators: ['isRequired'] + } + ]); + + if (errors) { + return errors; + } + return new Conference(this[clientKey], { + id: id + }).deafMember(memberId); + } + + /** + * undeaf member + * @method + * @param {string} id - id of conference + * @param {string} memberId - id of member + * @promise {PlivoGenericResponse} return PlivoGenericResponse if success + * @fail {Error} return Error + */ + undeafMember(id, memberId) { + let errors = validate([{ + field: 'id', + value: id, + validators: ['isRequired'] + }, + { + field: 'memberId', + value: memberId, + validators: ['isRequired'] + } + ]); + + if (errors) { + return errors; + } + return new Conference(this[clientKey], { + id: id + }).undeafMember(memberId); + } + /** + * play audio to member + * @method + * @param {string} id - id of conference + * @param {string} memberId - id of member + * @param {string} url - urls for audio + * @promise {PlivoGenericResponse} return PlivoGenericResponse if success + * @fail {Error} return Error + */ + + playAudioToMember(id, memberId, url) { + let errors = validate([{ + field: 'id', + value: id, + validators: ['isRequired'] + }, + { + field: 'memberId', + value: memberId, + validators: ['isRequired'] + }, + { + field: 'url', + value: url, + validators: ['isRequired'] + } + ]); + + if (errors) { + return errors; + } + return new Conference(this[clientKey], { + id: id + }).playAudioToMember(memberId, url); + } + + /** + * stop playing audio to member + * @method + * @param {string} id - id of conference + * @param {string} memberId - id of member + * @promise {PlivoGenericResponse} return PlivoGenericResponse if success + * @fail {Error} return Error + */ + stopPlayingAudioToMember(id, memberId) { + let errors = validate([{ + field: 'id', + value: id, + validators: ['isRequired'] + }, + { + field: 'memberId', + value: memberId, + validators: ['isRequired'] + } + ]); + + if (errors) { + return errors; + } + return new Conference(this[clientKey], { + id: id + }).stopPlayingAudioToMember(memberId); + } + + /** + * speak text to member + * @method + * @param {string} id - id of conference + * @param {string} memberId - id of member + * @param {string} text - text to speak + * @param {object} optionalParams - optional params + * @promise {PlivoGenericResponse} return PlivoGenericResponse if success + * @fail {Error} return Error + */ + speakTextToMember(id, memberId, text, optionalParams) { + let errors = validate([{ + field: 'id', + value: id, + validators: ['isRequired'] + }, + { + field: 'memberId', + value: memberId, + validators: ['isRequired'] + }, + { + field: 'text', + value: text, + validators: ['isRequired'] + } + ]); + + if (errors) { + return errors; + } + return new Conference(this[clientKey], { + id: id + }).speakTextToMember(memberId, text, optionalParams); + } + + /** + * stop speaking text to member + * @method + * @param {string} id - id of conference + * @param {string} memberId - id of member + * @promise {PlivoGenericResponse} return PlivoGenericResponse if success + * @fail {Error} return Error + */ + stopSpeakingTextToMember(id, memberId) { + let errors = validate([{ + field: 'id', + value: id, + validators: ['isRequired'] + }, + { + field: 'memberId', + value: memberId, + validators: ['isRequired'] + } + ]); + + if (errors) { + return errors; + } + return new Conference(this[clientKey], { + id: id + }).stopSpeakingTextToMember(memberId); + } + + /** + * record conference + * @method + * @param {string} id - id of conference + * @param {object} params - optional params to record conference + * @param {string} [params.fileFormat] The file format of the record can be of mp3 or wav format. Defaults to mp3 format. + * @param {string} [params.transcriptionType] The type of transcription required. The following values are allowed: + * - auto - This is the default value. Transcription is completely automated; turnaround time is about 5 minutes. + * - hybrid - Transcription is a combination of automated and human verification processes; turnaround time is about 10-15 minutes. + * @param {string} [params.transcriptionUrl] The URL where the transcription is available. + * @param {string} [params.transcriptionMethod] The method used to invoke the transcription_url. Defaults to POST. + * @param {string} [params.callbackUrl] The URL invoked by the API when the recording ends. + * @param {string} [params.callbackMethod] The method which is used to invoke the callback_url URL. Defaults to POST. + * @promise {PlivoGenericResponse} return PlivoGenericResponse if success + * @fail {Error} return Error + */ + record(id, params) { + return this.startRecording(id, params); + } + + /** + * record conference + * @method + * @param {string} id - id of conference + * @param {object} params - optional params to record conference + * @param {string} [params.fileFormat] The file format of the record can be of mp3 or wav format. Defaults to mp3 format. + * @param {string} [params.transcriptionType] The type of transcription required. The following values are allowed: + * - auto - This is the default value. Transcription is completely automated; turnaround time is about 5 minutes. + * - hybrid - Transcription is a combination of automated and human verification processes; turnaround time is about 10-15 minutes. + * @param {string} [params.transcriptionUrl] The URL where the transcription is available. + * @param {string} [params.transcriptionMethod] The method used to invoke the transcription_url. Defaults to POST. + * @param {string} [params.callbackUrl] The URL invoked by the API when the recording ends. + * @param {string} [params.callbackMethod] The method which is used to invoke the callback_url URL. Defaults to POST. + * @promise {PlivoGenericResponse} return PlivoGenericResponse if success + * @fail {Error} return Error + */ + startRecording(id, params) { + let errors = validate([{ + field: 'id', + value: id, + validators: ['isRequired'] + }]); + + if (errors) { + return errors; + } + + return new Conference(this[clientKey], { + id: id + }).startRecording(params); + } + + /** + * stop recording + * @method + * @param {string} id - id of conference + * @promise {PlivoGenericResponse} return PlivoGenericResponse if success + * @fail {Error} return Error + */ + stopRecording(id) { + let errors = validate([{ + field: 'id', + value: id, + validators: ['isRequired'] + }]); + + if (errors) { + return errors; + } + return new Conference(this[clientKey], { + id: id + }).stopRecording(); + } +} \ No newline at end of file diff --git a/lib/resources/endpoints.js b/lib/resources/endpoints.js index 13e371b..ed71eb8 100644 --- a/lib/resources/endpoints.js +++ b/lib/resources/endpoints.js @@ -1,168 +1,314 @@ -import {extend, validate} from '../utils/common.js'; -import {PlivoResource, PlivoResourceInterface} from '../base'; +import { + PlivoResource, + PlivoResourceInterface +} from '../base'; +import { + extend, + validate +} from '../utils/common.js'; const clientKey = Symbol(); const action = 'Endpoint/'; const idField = 'endpointId'; + +export class UpdateEndpointResponse { + constructor(params) { + params = params || {}; + this.apiId = params.apiId; + this.message = params.message; + this.alias = params.alias; + + } +} +export class RetrieveEndpointResponse { + constructor(params) { + params = params || {}; + this.apiId = params.apiId; + this.alias = params.alias; + this.application = params.application; + this.endpointId = params.endpointId; + this.password = params.password; + this.resourceUri = params.resourceUri; + this.sipRegistered = params.sipRegistered; + this.sipUri = params.sipUri; + this.subAccount = params.subAccount; + this.username = params.username; + } +} + +export class ListAllEndpointResponse { + constructor(params) { + params = params || {}; + this.alias = params.alias; + this.application = params.application; + this.endpointId = params.endpointId; + this.password = params.password; + this.resourceUri = params.resourceUri; + this.sipRegistered = params.sipRegistered; + this.sipUri = params.sipUri; + this.subAccount = params.subAccount; + this.username = params.username; + } +} + +export class CreateEndpointResponse { + constructor(params) { + params = params || {}; + this.alias = params.alias; + this.apiId = params.apiId; + this.endpointId = params.endpointId; + this.message = params.message; + this.username = params.username; + } +} + /** - * Represents a Endpoint - * @constructor - * @param {function} client - make api call - * @param {object} [data] - data of call - */ +* Represents a Endpoint +* @constructor +* @param {function} client - make api call +* @param {object} [data] - data of call +*/ export class Endpoint extends PlivoResource { constructor(client, data = {}) { - super(action, Endpoint, idField, client); + super(action, Endpoint, idField, client); - if (idField in data) { - this.id = data[idField]; - } + if (idField in data) { + this.id = data[idField]; + } - extend(this, data); - this[clientKey] = client; + extend(this, data); + this[clientKey] = client; } -/** - * update Endpoint - * @method - * @param {object} params - * @param {string} [params.username] - username to update - * @param {string} [params.password] - password to update - * @param {string} [params.alias] - alias to update - * @param {string} [params.appId] - app id to update - * @promise {object} return {@link Endpoint} object if success - * @fail {Error} return Error - */ - update(params) { - params.isVoiceRequest = 'true'; - return super.update(params); + /** + * update Endpoint + * @method + * @param {object} params + * @param {string} [params.username] - username to update + * @param {string} [params.password] - password to update + * @param {string} [params.alias] - alias to update + * @param {string} [params.appId] - app id to update + * @promise {object} return {@link Endpoint} object if success + * @fail {Error} return Error + */ + update(params, id) { + params.isVoiceRequest = 'true'; + let client = this[clientKey]; + let that = this; + return new Promise((resolve, reject) => { + client('POST', action + id + '/', params) + .then(response => { + extend(that, response.body); + if (params.hasOwnProperty('isVoiceRequest')) { + delete params.isVoiceRequest; + } + extend(that, params); + resolve(new UpdateEndpointResponse(that)); + }) + .catch(error => { + reject(error); + }); + }); } -/** - * delete Endpoint - * @method - * @promise {boolean} return true if success - * @fail {Error} return Error - */ + /** + * delete Endpoint + * @method + * @promise {boolean} return true if success + * @fail {Error} return Error + */ delete() { - let params = {}; - params.isVoiceRequest = 'true'; - return super.delete(params); + let params = {}; + params.isVoiceRequest = 'true'; + + let client = this[clientKey]; + let id = this.id; + + return new Promise((resolve, reject) => { + client('DELETE', action + id + '/', params) + .then(() => { + resolve(true); + }) + .catch(error => { + reject(error); + }); + }); + } } /** - * Represents a Endpoint Interface - * @constructor - * @param {function} client - make api call - * @param {object} [data] - data of call - */ +* Represents a Endpoint Interface +* @constructor +* @param {function} client - make api call +* @param {object} [data] - data of call +*/ export class EndpointInterface extends PlivoResourceInterface { constructor(client, data = {}) { - super(action, Endpoint, idField, client); - extend(this, data); + super(action, Endpoint, idField, client); + extend(this, data); - this[clientKey] = client; + this[clientKey] = client; } -/** - * Get Endpoint by given id - * @method - * @param {string} id - id of endpoint - * @promise {object} return {@link Endpoint} object if success - * @fail {Error} return Error - */ + /** + * Get Endpoint by given id + * @method + * @param {string} id - id of endpoint + * @promise {object} return {@link Endpoint} object if success + * @fail {Error} return Error + */ get(id) { - let params = {}; - params.isVoiceRequest = 'true'; - return super.get(id, params); + let params = {}; + params.isVoiceRequest = 'true'; + let client = this[clientKey]; + + return new Promise((resolve, reject) => { + if (action !== '' && !id) { + reject(new Error(this[idKey] + ' must be set')); + } + client('GET', action + (id ? id + '/' : ''), params) + .then(response => { + resolve(new RetrieveEndpointResponse(response.body, client)); + }) + .catch(error => { + reject(error); + }); + }); } list() { - let params = {}; - params.isVoiceRequest = 'true'; - return super.list(params); + let params = {}; + params.isVoiceRequest = 'true'; + + let client = this[clientKey]; + + return new Promise((resolve, reject) => { + client('GET', action, params) + .then(response => { + let objects = []; + Object.defineProperty(objects, 'meta', { + value: response.body.meta, + enumerable: true + }); + response.body.objects.forEach(item => { + objects.push(new ListAllEndpointResponse(item, client)); + }); + console.log(objects) + resolve(objects); + }) + .catch(error => { + reject(error); + }); + }); } -/** - * Create Endpoint - * @method - * @param {string} username - username to create - * @param {string} passwowrd - password to create - * @param {string} alias - alias to create - * @param {string} appId - app id to create - * @promise {object} return {@link PlivoGenericResponse} object if success - * @fail {Error} return Error - */ + /** + * Create Endpoint + * @method + * @param {string} username - username to create + * @param {string} passwowrd - password to create + * @param {string} alias - alias to create + * @param {string} appId - app id to create + * @promise {object} return {@link PlivoGenericResponse} object if success + * @fail {Error} return Error + */ create(username, password, alias, appId) { - let params = {}; + let params = {}; - let errors = validate([ - {field: 'username', value: username, validators: ['isRequired']}, - {field: 'password', value: password, validators: ['isRequired']}, - {field: 'alias', value: alias, validators: ['isRequired']} - ]); + let errors = validate([{ + field: 'username', + value: username, + validators: ['isRequired'] + }, + { + field: 'password', + value: password, + validators: ['isRequired'] + }, + { + field: 'alias', + value: alias, + validators: ['isRequired'] + } + ]); + + if (errors) { + return errors; + } + + params.username = username; + params.password = password; + params.alias = alias; + if (appId) { + params.app_id = appId; + } + params.isVoiceRequest = 'true'; + let client = this[clientKey]; + + return new Promise((resolve, reject) => { + client('POST', action, params) + .then(response => { + resolve(new CreateEndpointResponse(response.body, idField)); + }) + .catch(error => { + reject(error); + }); + }); - if (errors) { - return errors; - } - params.username = username; - params.password = password; - params.alias = alias; - if (appId) { - params.app_id = appId; - } - params.isVoiceRequest = 'true'; - return super.create(params); } -/** - * update Endpoint - * @method - * @param {string} id - id to update - * @param {object} params - * @param {string} [params.username] - username to update - * @param {string} [params.password] - password to update - * @param {string} [params.alias] - alias to update - * @param {string} [params.appId] - app id to update - * @promise {object} return {@link Endpoint} object if success - * @fail {Error} return Error - */ + /** + * update Endpoint + * @method + * @param {string} id - id to update + * @param {object} params + * @param {string} [params.username] - username to update + * @param {string} [params.password] - password to update + * @param {string} [params.alias] - alias to update + * @param {string} [params.appId] - app id to update + * @promise {object} return {@link Endpoint} object if success + * @fail {Error} return Error + */ update(id, params) { - let errors = validate([ - {field: 'id', value: id, validators: ['isRequired']} - ]); + let errors = validate([{ + field: 'id', + value: id, + validators: ['isRequired'] + }]); - if (errors) { - return errors; - } - return new Endpoint(this[clientKey], { - id: id - }).update(params); + if (errors) { + return errors; + } + return new Endpoint(this[clientKey], { + id: id + }).update(params, id); } -/** - * delete Endpoint - * @method - * @param {string} id - id to delete - * @promise {boolean} return true if success - * @fail {Error} return Error - */ + /** + * delete Endpoint + * @method + * @param {string} id - id to delete + * @promise {boolean} return true if success + * @fail {Error} return Error + */ delete(id) { - let errors = validate([ - {field: 'id', value: id, validators: ['isRequired']} - ]); + let errors = validate([{ + field: 'id', + value: id, + validators: ['isRequired'] + }]); - if (errors) { - return errors; - } - return new Endpoint(this[clientKey], { - id: id - }).delete(); + if (errors) { + return errors; + } + return new Endpoint(this[clientKey], { + id: id + }).delete(); } -} +} \ No newline at end of file diff --git a/lib/resources/lookup.js b/lib/resources/lookup.js index 8d0139f..080328d 100644 --- a/lib/resources/lookup.js +++ b/lib/resources/lookup.js @@ -1,20 +1,32 @@ -import { - extend, - validate -} from '../utils/common.js'; +import * as _ from "lodash"; import { PlivoResource, PlivoResourceInterface } from '../base'; - -import * as _ from "lodash"; +import { + extend, + validate +} from '../utils/common.js'; const clientKey = Symbol(); const action = 'Number/'; // unused as it is overridden, only for unit tests const idField = 'OVERRIDDEN'; const LOOKUP_API_BASE_URL = 'https://lookup.plivo.com/v1/Number' +export class LookupResponse { + constructor(params) { + params = params || {}; + this.apiId = params.apiId; + this.phoneNumber = params.phoneNumber; + this.country = params.country; + this.format = params.format; + this.carrier = params.carrier; + this.resourceUri = params.resourceUri; + + } +} + export class Number extends PlivoResource { constructor(client, data = {}) { super(action, Number, idField, client); @@ -46,6 +58,15 @@ export class LookupInterface extends PlivoResourceInterface { overrideUrl: `${LOOKUP_API_BASE_URL}/${number}`, }; - return super.get(number, params); + let client = this[clientKey]; + return new Promise((resolve, reject) => { + client('GET', action + '/', params) + .then(response => { + resolve(new LookupResponse(response.body, client)); + }) + .catch(error => { + reject(error); + }); + }); } -} +} \ No newline at end of file diff --git a/lib/resources/media.js b/lib/resources/media.js index a628b92..ad027a7 100644 --- a/lib/resources/media.js +++ b/lib/resources/media.js @@ -1,18 +1,52 @@ -import { - extend, - validate -} from '../utils/common.js'; -import { - PlivoResource, - PlivoResourceInterface -} from '../base'; import * as _ from 'lodash'; + +import { + PlivoGenericResponse, + PlivoResource, + PlivoResourceInterface +} from '../base'; +import { + extend, + validate +} from '../utils/common.js'; + var fs = require('fs'); const clientKey = Symbol(); const action = 'Media/'; const idField = 'media_id'; +export class UploadMediaResponse { + constructor(params) { + params = params || {}; + this.apiId = params.apiId; + this.objects = params.objects; + } +} + +export class RetrieveMediaResponse { + constructor(params) { + params = params || {}; + this.apiId = params.apiId; + this.contentType = params.contentType; + this.fileName = params.fileName; + this.mediaId = params.mediaId; + this.mediaUrl = params.mediaUrl; + this.size = params.size; + this.uploadTime = params.uploadTime; + } +} + +export class ListMediaResponse { + constructor(params) { + params = params || {}; + this.apiId = params.apiId; + this.meta = params.meta; + this.objects = params.objects; + + } +} + /** * Represents a Message * @constructor @@ -20,15 +54,15 @@ const idField = 'media_id'; * @param {object} [data] - data of call */ export class Media extends PlivoResource { - constructor(client, data = {}) { - super(action, Media, idField, client); + constructor(client, data = {}) { + super(action, Media, idField, client); - if (idField in data) { - this.id = data[idField]; + if (idField in data) { + this.id = data[idField]; + } + + extend(this, data); } - - extend(this, data); - } } /** * Represents a Media Interface @@ -38,61 +72,106 @@ export class Media extends PlivoResource { */ export class MediaInterface extends PlivoResourceInterface { - constructor(client, data = {}) { - super(action, Media, idField, client); - extend(this, data); - this[clientKey] = client; - } - - /** - * Upload Media - * @method - * @fail {Error} return Error - */ - upload(files) { - let errors = validate([{ - field: 'files', - value: files, - validators: ['isRequired'] - }]); - - if (errors) { - return errors; - } - let params = {} - params.file = files - return super.create(params); - } - - /** - * Get Media by given id - * @method - * @param {string} media_id - id of media - * @promise {object} return {@link Media} object if success - * @fail {Error} return Error - */ - get(media_id) { - let errors = validate([{ - field: 'media_id', - value: media_id, - validators: ['isRequired'] - }]); - - if (errors) { - return errors; + constructor(client, data = {}) { + super(action, Media, idField, client); + extend(this, data); + this[clientKey] = client; } - return super.get(media_id); - } + /** + * Upload Media + * @method + * @fail {Error} return Error + */ + upload(files) { + let errors = validate([{ + field: 'files', + value: files, + validators: ['isRequired'] + }]); - /** - * Get All Media Detail - * @method - * @param {object} params - params to get all media details. - * @promise {object[]} returns list of Media Object - * @fail {Error} returns Error - */ - list(params) { - return super.list(params); - } -} + if (errors) { + return errors; + } + let params = {} + params.file = files + + let client = this[clientKey]; + + return new Promise((resolve, reject) => { + client('POST', action, params) + .then(response => { + resolve(new UploadMediaResponse(response.body, idField)); + }) + .catch(error => { + reject(error); + }); + }); + + } + + /** + * Get Media by given id + * @method + * @param {string} media_id - id of media + * @promise {object} return {@link Media} object if success + * @fail {Error} return Error + */ + get(media_id) { + let errors = validate([{ + field: 'media_id', + value: media_id, + validators: ['isRequired'] + }]); + + if (errors) { + return errors; + } + + let client = this[clientKey]; + + return new Promise((resolve, reject) => { + if (action !== '' && !media_id) { + reject(new Error(this[idKey] + ' must be set')); + } + client('GET', action + (media_id ? media_id + '/' : '')) + .then(response => { + resolve(new RetrieveMediaResponse(response.body, client)); + }) + .catch(error => { + reject(error); + }); + }); + + } + + /** + * Get All Media Detail + * @method + * @param {object} params - params to get all media details. + * @promise {object[]} returns list of Media Object + * @fail {Error} returns Error + */ + list(params) { + //return super.list(params); + + let client = this[clientKey]; + return new Promise((resolve, reject) => { + client('GET', action, params) + .then(response => { + let objects = []; + Object.defineProperty(objects, 'meta', { + value: response.body.meta, + enumerable: true + }); + response.body.objects.forEach(item => { + objects.push(new PlivoGenericResponse(item, client)); + }); + resolve(objects); + }) + .catch(error => { + reject(error); + }); + }); + } +} \ No newline at end of file diff --git a/lib/resources/messages.js b/lib/resources/messages.js index a05d788..58799b7 100644 --- a/lib/resources/messages.js +++ b/lib/resources/messages.js @@ -1,16 +1,15 @@ import * as _ from "lodash"; import { - PlivoGenericResponse, - PlivoResource, - PlivoResourceInterface + PlivoGenericResponse, + PlivoResource, + PlivoResourceInterface } from '../base'; import { - extend, - validate + extend, + validate } from '../utils/common.js'; - const action = 'Message/'; const idField = 'messageUuid'; let actionKey = Symbol('api action'); @@ -19,14 +18,78 @@ let idKey = Symbol('id filed'); let clientKey = Symbol('make api call'); export class MessageResponse { - constructor(params) { - params = params || {}; - this.apiId= params.apiId; - this.message = params.message; - this.messageUuid = params.messageUuid; + constructor(params) { + params = params || {}; + this.apiId = params.apiId; + this.message = params.message; + this.messageUuid = params.messageUuid; - } + } } + +export class MessageGetResponse { + constructor(params) { + params = params || {}; + this.apiId = params.apiId; + this.errorCode = params.errorCode; + this.fromNumber = params.fromNumber; + this.messageDirection = params.messageDirection; + this.messageState = params.messageState; + this.messageTime = params.messageTime; + this.messageType = params.messageType; + this.messageUuid = params.messageUuid; + this.resourceUri = params.resourceUri; + this.toNumber = params.toNumber; + this.totalAmount = params.totalAmount; + this.totalRate = params.totalRate; + this.units = params.units; + } +} + +export class MessageListResponse { + constructor(params) { + params = params || {}; + this.errorCode = params.errorCode; + this.fromNumber = params.fromNumber; + this.messageDirection = params.messageDirection; + this.messageState = params.messageState; + this.messageTime = params.messageTime; + this.messageType = params.messageType; + this.messageUuid = params.messageUuid; + this.resourceUri = params.resourceUri; + this.toNumber = params.toNumber; + this.totalAmount = params.totalAmount; + this.totalRate = params.totalRate; + this.units = params.units; + } +} + +export class MMSMediaResponse { + + constructor(params) { + params = params || {}; + this.apiId = params.apiId; + let MMSMediaList = [] + params.objects.forEach(item => { + MMSMediaList.push(new MMSMedia(item)); + }); + this.objects = MMSMediaList; + } +} + +export class MMSMedia { + constructor(params) { + params = params || {}; + this.contentType = params.contentType; + this.fileName = params.fileName; + this.mediaId = params.mediaId; + this.mediaUrl = params.mediaUrl; + this.messageUuid = params.messageUuid; + this.size = params.size; + this.uploadTime = params.uploadTime; + } +} + /** * Represents a Message * @constructor @@ -34,19 +97,31 @@ export class MessageResponse { * @param {object} [data] - data of call */ export class Message extends PlivoResource { - constructor(client, data = {}) { - super(action, Message, idField, client); + constructor(client, data = {}) { + super(action, Message, idField, client); + this[actionKey] = action; + this[clientKey] = client; + if (idField in data) { + this.id = data[idField]; + }; - if (idField in data) { - this.id = data[idField]; - }; + extend(this, data); + } - extend(this, data); - } - - listMedia() { - return super.executeAction(this.id + '/Media/', 'Get', {}); - } + listMedia() { + //return super.executeAction(this.id + '/Media/', 'Get', {}); + let client = this[clientKey]; + let idField = this[idKey]; + return new Promise((resolve, reject) => { + client('Get', this[actionKey] + this.id + '/Media/', {}) + .then(response => { + resolve(new MMSMediaResponse(response.body, idField)); + }) + .catch(error => { + reject(error); + }); + }); + } } /** @@ -58,125 +133,161 @@ export class Message extends PlivoResource { export class MessageInterface extends PlivoResourceInterface { - constructor(client, data = {}) { - super(action, Message, idField, client); - extend(this, data); - this[clientKey] = client; - this[actionKey] = action; - this[klassKey] = Message; - this[idKey] = idField; + constructor(client, data = {}) { + super(action, Message, idField, client); + extend(this, data); + this[clientKey] = client; + this[actionKey] = action; + this[klassKey] = Message; + this[idKey] = idField; - } - - /** - * Send Message - * @method - * @param {string} src - source number - * @param {string} dst - destination number - * @param {string} text - text to send - * @param {object} optionalParams - Optional Params to send message - * @param {string} [optionalParams.type] - The type of message. Should be `sms` or `mms`. Defaults to `sms`. - * @param {string} [optionalParams.url] The URL to which with the status of the message is sent. - * @param {string} [optionalParams.method] The method used to call the url. Defaults to POST. - * @param {list} [optionalParams.media_urls] For sending mms, specify the media urls in list of string - * @param {boolean} [optionalParams.log] If set to false, the content of this message will not be logged on the Plivo infrastructure and the dst value will be masked (e.g., 141XXXXX528). Default is set to true. - * @promise {object} return {@link PlivoGenericMessage} object if success - * @fail {Error} return Error - */ - send(src, dst, text, optionalParams) { - return this.create(src, dst, text, optionalParams); - } - - /** - * Create Message - * @method - * @param {string} src - source number - * @param {string} dst - destination number - * @param {string} text - text to send - * @param {object} optionalParams - Optional Params to send message - * @param {string} [optionalParams.type] - The type of message. Should be `sms` or `mms`. Defaults to `sms`. - * @param {string} [optionalParams.url] The URL to which with the status of the message is sent. - * @param {string} [optionalParams.method] The method used to call the url. Defaults to POST. - * @param {boolean} [optionalParams.log] If set to false, the content of this message will not be logged on the Plivo infrastructure and the dst value will be masked (e.g., 141XXXXX528). Default is set to true. - * @param {Array} [optionalParams.media_urls] For sending mms, specify the media urls in list of string - * @promise {object} return {@link MessageResponse} object if success - * @fail {Error} return Error - */ - createtest(src, dst, text, optionalParams, powerpackUUID) { - let errors = validate([{ - field: 'dst', - value: dst, - validators: ['isRequired'] - }]); - - if (errors) { - return errors; } - if (!src && !powerpackUUID) { - let errorText = 'Neither of src or powerpack uuid present, either one is required' - return new Promise(function (resolve, reject) { - reject(new Error(errorText)); - }); + /** + * Send Message + * @method + * @param {string} src - source number + * @param {string} dst - destination number + * @param {string} text - text to send + * @param {object} optionalParams - Optional Params to send message + * @param {string} [optionalParams.type] - The type of message. Should be `sms` or `mms`. Defaults to `sms`. + * @param {string} [optionalParams.url] The URL to which with the status of the message is sent. + * @param {string} [optionalParams.method] The method used to call the url. Defaults to POST. + * @param {list} [optionalParams.media_urls] For sending mms, specify the media urls in list of string + * @param {boolean} [optionalParams.log] If set to false, the content of this message will not be logged on the Plivo infrastructure and the dst value will be masked (e.g., 141XXXXX528). Default is set to true. + * @promise {object} return {@link PlivoGenericMessage} object if success + * @fail {Error} return Error + */ + send(src, dst, text, optionalParams) { + return this.create(src, dst, text, optionalParams); } - if (src && powerpackUUID) { - let errorText = 'Either of src or powerpack uuid, both of them are present' - return new Promise(function (resolve, reject) { - reject(new Error(errorText)); - }) - } + /** + * Create Message + * @method + * @param {string} src - source number + * @param {string} dst - destination number + * @param {string} text - text to send + * @param {object} optionalParams - Optional Params to send message + * @param {string} [optionalParams.type] - The type of message. Should be `sms` or `mms`. Defaults to `sms`. + * @param {string} [optionalParams.url] The URL to which with the status of the message is sent. + * @param {string} [optionalParams.method] The method used to call the url. Defaults to POST. + * @param {boolean} [optionalParams.log] If set to false, the content of this message will not be logged on the Plivo infrastructure and the dst value will be masked (e.g., 141XXXXX528). Default is set to true. + * @param {Array} [optionalParams.media_urls] For sending mms, specify the media urls in list of string + * @promise {object} return {@link MessageResponse} object if success + * @fail {Error} return Error + */ + create(src, dst, text, optionalParams, powerpackUUID) { + let errors = validate([{ + field: 'dst', + value: dst, + validators: ['isRequired'] + }]); - let params = optionalParams || {}; - if (src) { - params.src = src; - } - params.dst = _.isArray(dst) ? _.join(dst, '<') : dst; - params.text = text; - if (powerpackUUID) { - params.powerpackUUID = powerpackUUID; - } + if (errors) { + return errors; + } - let client = this[clientKey]; - let idField = this[idKey]; - let action = this[actionKey] + (this.id ? this.id + '/' : ''); + if (!src && !powerpackUUID) { + let errorText = 'Neither of src or powerpack uuid present, either one is required' + return new Promise(function(resolve, reject) { + reject(new Error(errorText)); + }); + } - return new Promise((resolve, reject) => { - client('POST', action, params) - .then(response => { - resolve(new MessageResponse(response.body, idField)); + if (src && powerpackUUID) { + let errorText = 'Either of src or powerpack uuid, both of them are present' + return new Promise(function(resolve, reject) { + reject(new Error(errorText)); + }) + } + + let params = optionalParams || {}; + if (src) { + params.src = src; + } + params.dst = _.isArray(dst) ? _.join(dst, '<') : dst; + params.text = text; + if (powerpackUUID) { + params.powerpackUUID = powerpackUUID; + } + + let client = this[clientKey]; + let idField = this[idKey]; + let action = this[actionKey] + (this.id ? this.id + '/' : ''); + + return new Promise((resolve, reject) => { + client('POST', action, params) + .then(response => { + resolve(new MessageResponse(response.body, idField)); + }) + .catch(error => { + reject(error); + }); }) - .catch(error => { - reject(error); - }); - }) - } - - /** - * Get Message by given id - * @method - * @param {string} id - id of message - * @promise {object} return {@link Message} object if success - * @fail {Error} return Error - */ - get(id) { - let errors = validate([{ - field: 'id', - value: id, - validators: ['isRequired'] - }]); - - if (errors) { - return errors; } - return super.get(id); - } + /** + * Get Message by given id + * @method + * @param {string} id - id of message + * @promise {object} return {@link Message} object if success + * @fail {Error} return Error + */ + get(id) { + let errors = validate([{ + field: 'id', + value: id, + validators: ['isRequired'] + }]); - listMedia(messageUUID) { - return new Message(this[clientKey], { - id: messageUUID - }).listMedia(); - } + if (errors) { + return errors; + } -} + let client = this[clientKey]; + let action = this[actionKey]; + + return new Promise((resolve, reject) => { + if (action !== '' && !id) { + reject(new Error(this[idKey] + ' must be set')); + } + client('GET', action + (id ? id + '/' : '')) + .then(response => { + resolve(new MessageGetResponse(response.body, client)); + }) + .catch(error => { + reject(error); + }); + }); + } + + list(params) { + let client = this[clientKey]; + let action = this[actionKey]; + return new Promise((resolve, reject) => { + client('GET', action, params) + .then(response => { + let objects = []; + Object.defineProperty(objects, 'meta', { + value: response.body.meta, + enumerable: true + }); + response.body.objects.forEach(item => { + objects.push(new MessageListResponse(item, client)); + }); + resolve(objects); + }) + .catch(error => { + reject(error); + }); + }); + } + + listMedia(messageUUID) { + return new Message(this[clientKey], { + id: messageUUID + }).listMedia(); + } + +} \ No newline at end of file diff --git a/lib/resources/numbers.js b/lib/resources/numbers.js index f927bf7..14c8152 100644 --- a/lib/resources/numbers.js +++ b/lib/resources/numbers.js @@ -1,10 +1,35 @@ -import {extend, validate} from '../utils/common.js'; -import {PlivoResource, PlivoResourceInterface} from '../base'; +import { + PlivoResource, + PlivoResourceInterface +} from '../base'; +import { + extend, + validate +} from '../utils/common.js'; const clientKey = Symbol(); const action = 'Number/'; const idField = 'number'; + +export class BuyNumberResponse { + constructor(params) { + params = params || {}; + this.apiId = params.apiId; + this.numbers = params.numbers; + this.status = params.status; + + } +} + +export class UpdateNumberResponse { + constructor(params) { + params = params || {}; + this.apiId = params.apiId; + this.message = params.message; + } +} + /** * Represents a PhoneNumber * @constructor @@ -12,29 +37,29 @@ const idField = 'number'; * @param {object} [data] - data of call */ export class PhoneNumber extends PlivoResource { - constructor(client, data = {}) { - super('PhoneNumber/', PhoneNumber, idField, client); + constructor(client, data = {}) { + super('PhoneNumber/', PhoneNumber, idField, client); - if (idField in data) { - this.id = data[idField]; + if (idField in data) { + this.id = data[idField]; + } + + extend(this, data); + this[clientKey] = client; } - extend(this, data); - this[clientKey] = client; - } - -/** - * Buy Phone Number - * @method - * @param {string} appId - app id - * @promise {@link PlivoGenericResponse} return PlivoGenericResponse Object if success - * @fail {Error} return Error - */ - buy(appId) { - return new PhoneNumberInterface(this[clientKey], { - id: this.id - }).buy(appId); - } + /** + * Buy Phone Number + * @method + * @param {string} appId - app id + * @promise {@link PlivoGenericResponse} return PlivoGenericResponse Object if success + * @fail {Error} return Error + */ + buy(appId) { + return new PhoneNumberInterface(this[clientKey], { + id: this.id + }).buy(appId); + } } /** @@ -45,27 +70,37 @@ export class PhoneNumber extends PlivoResource { * @param {string} [data.test] - test data */ export class PhoneNumberInterface extends PlivoResourceInterface { - constructor(client, data = {}) { - super('PhoneNumber/', PhoneNumber, idField, client); + constructor(client, data = {}) { + super('PhoneNumber/', PhoneNumber, idField, client); - extend(this, data); - this[clientKey] = client; - } - -/** - * Buy Phone Number - * @method - * @param {string} appId - app id - * @promise {@link PlivoGenericResponse} return PlivoGenericResponse Object if success - * @fail {Error} return Error - */ - buy(appId) { - let params = {}; - if (appId) { - params.app_id = appId; + extend(this, data); + this[clientKey] = client; + } + + /** + * Buy Phone Number + * @method + * @param {string} appId - app id + * @promise {@link PlivoGenericResponse} return PlivoGenericResponse Object if success + * @fail {Error} return Error + */ + buy(number, appId) { + let params = {}; + if (appId) { + params.app_id = appId; + } + let client = this[clientKey]; + + return new Promise((resolve, reject) => { + client('POST', action + number + '/', params) + .then(response => { + resolve(new BuyNumberResponse(response.body, idField)); + }) + .catch(error => { + reject(error); + }); + }); } - return super.create(params); - } } /** @@ -75,38 +110,68 @@ export class PhoneNumberInterface extends PlivoResourceInterface { * @param {object} [data] - data of call */ export class NumberResource extends PlivoResource { - constructor(client, data = {}) { - super(action, NumberResource, idField, client); + constructor(client, data = {}) { + super(action, NumberResource, idField, client); - if (idField in data) { - this.id = data[idField]; + if (idField in data) { + this.id = data[idField]; + } + extend(this, data); + this[clientKey] = client; } - extend(this, data); - } -/** - * Unrent Number - * @method - * @promise {boolean} return true if success - * @fail {Error} return Error - */ - unrent() { - return super.delete(); - } + /** + * Unrent Number + * @method + * @promise {boolean} return true if success + * @fail {Error} return Error + */ + unrent(number) { + let client = this[clientKey]; + let action = 'Number/'; + return new Promise((resolve, reject) => { + client('DELETE', action + number + '/') + .then(() => { + resolve(true); + }) + .catch(error => { + reject(error); + }); + }); + } + + /** + * Update Number + * @method + * @param {object} params + * @param {string} [params.appId] - app id + * @param {string} [params.subAccount] - auth_id of subaccount + * @param {string} [params.alias] - textual name of number + * @promise {@link NumberResource} return NumberResource Object if success + * @fail {Error} return Error + */ + update(number, params) { + let client = this[clientKey]; + let action = 'Number/'; + let that = this; + + return new Promise((resolve, reject) => { + client('POST', action + number + '/', params) + .then(response => { + extend(that, response.body); + if (params.hasOwnProperty('isVoiceRequest')) { + delete params.isVoiceRequest; + } + extend(that, params); + resolve(new UpdateNumberResponse(that)); + }) + .catch(error => { + reject(error); + }); + }); + + } -/** - * Update Number - * @method - * @param {object} params - * @param {string} [params.appId] - app id - * @param {string} [params.subAccount] - auth_id of subaccount - * @param {string} [params.alias] - textual name of number - * @promise {@link NumberResource} return NumberResource Object if success - * @fail {Error} return Error - */ - update(params) { - return super.update(params); - } } /** @@ -117,125 +182,143 @@ export class NumberResource extends PlivoResource { */ export class NumberInterface extends PlivoResourceInterface { - constructor(client) { - super(action, NumberResource, idField, client); - this[clientKey] = client; - } - -/** - * Buy Phone Number - * @method - * @param {string} number - number to buy - * @param {string} appId - app id - * @promise {@link PlivoGenericResponse} return PlivoGenericResponse Object if success - * @fail {Error} return Error - */ - buy(number, appId) { - let errors = validate([ - {field: 'number', value: number, validators: ['isRequired']} - ]); - - if (errors) { - return errors; - } - return new PhoneNumber(this[clientKey], { - id: number - }).buy(appId); - } - -/** - * 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 - */ - addOwnNumber(numbers, carrier, region, optionalParams) { - let errors = validate([ - {field: 'numbers', value: numbers, validators: ['isRequired']}, - {field: 'carrier', value: carrier, validators: ['isRequired']}, - {field: 'region', value: region, validators: ['isRequired']} - ]); - - if (errors) { - return errors; - } - let params = optionalParams || {}; - - params.numbers = numbers; - params.carrier = carrier; - params.region = region; - - return super.create(params); - } - -/** - * 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 - */ - search(countryISO, optionalParams) { - let errors = validate([ - {field: 'country_iso', value: countryISO, validators: ['isRequired']} - ]); - - if (errors) { - return errors; + constructor(client) { + super(action, NumberResource, idField, client); + this[clientKey] = client; } - let params = optionalParams || {}; - params.country_iso = countryISO; - return new PhoneNumberInterface(this[clientKey]) - .list(params); - } + /** + * Buy Phone Number + * @method + * @param {string} number - number to buy + * @param {string} appId - app id + * @promise {@link PlivoGenericResponse} return PlivoGenericResponse Object if success + * @fail {Error} return Error + */ + buy(number, appId) { + let errors = validate([{ + field: 'number', + value: number, + validators: ['isRequired'] + }]); -/** - * Update Number - * @method - * @param {string} number - number to update - * @param {object} params - * @param {string} [params.appId] - app id - * @param {string} [params.subAccount] - auth_id of subaccount - * @param {string} [params.alias] - textual name of number - * @promise {@link NumberResource} return NumberResource Object if success - * @fail {Error} return Error - */ - update(number, params) { - let errors = validate([ - {field: 'number', value: number, validators: ['isRequired']} - ]); - - if (errors) { - return errors; + if (errors) { + return errors; + } + return new PhoneNumber(this[clientKey], { + id: number + }).buy(number, appId); } - return new NumberResource(this[clientKey], { - id: number - }).update(params); - } -/** - * Unrent Number - * @method - * @param {string} number - number to unrent - * @promise {boolean} return true if success - * @fail {Error} return Error - */ - unrent(number) { - let errors = validate([ - {field: 'number', value: number, validators: ['isRequired']} - ]); + /** + * 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 + */ + addOwnNumber(numbers, carrier, region, optionalParams) { + let errors = validate([{ + field: 'numbers', + value: numbers, + validators: ['isRequired'] + }, + { + field: 'carrier', + value: carrier, + validators: ['isRequired'] + }, + { + field: 'region', + value: region, + validators: ['isRequired'] + } + ]); - if (errors) { - return errors; + if (errors) { + return errors; + } + let params = optionalParams || {}; + + params.numbers = numbers; + params.carrier = carrier; + params.region = region; + + return super.create(params); } - return new NumberResource(this[clientKey], { - id: number - }).unrent(); - } -} + + /** + * 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 + */ + search(countryISO, optionalParams) { + let errors = validate([{ + field: 'country_iso', + value: countryISO, + validators: ['isRequired'] + }]); + + if (errors) { + return errors; + } + + let params = optionalParams || {}; + params.country_iso = countryISO; + return new PhoneNumberInterface(this[clientKey]).list(countryISO, params); + } + + /** + * Update Number + * @method + * @param {string} number - number to update + * @param {object} params + * @param {string} [params.appId] - app id + * @param {string} [params.subAccount] - auth_id of subaccount + * @param {string} [params.alias] - textual name of number + * @promise {@link NumberResource} return NumberResource Object if success + * @fail {Error} return Error + */ + update(number, params) { + let errors = 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 + */ + unrent(number) { + let errors = validate([{ + field: 'number', + value: number, + validators: ['isRequired'] + }]); + + if (errors) { + return errors; + } + return new NumberResource(this[clientKey], { + id: number + }).unrent(number); + } +} \ No newline at end of file diff --git a/lib/resources/phlo.js b/lib/resources/phlo.js index e8000df..43d05a4 100644 --- a/lib/resources/phlo.js +++ b/lib/resources/phlo.js @@ -1,12 +1,44 @@ -import { extend, validate } from '../utils/common.js'; -import { PlivoResource, PlivoResourceInterface } from '../base'; -import { PhloMultiPartyCall, PhloMultiPartyCallInterface } from "../resources/phloMultipartyCall"; import * as _ from "lodash"; +import { + PhloMultiPartyCall, + PhloMultiPartyCallInterface +} from "../resources/phloMultipartyCall"; +import { + PlivoResource, + PlivoResourceInterface +} from '../base'; +import { + extend, + validate +} from '../utils/common.js'; + const clientKey = Symbol(); const action = 'phlo/'; const idField = 'phloUuid'; + +export class RunPHLOResponse { + constructor(params) { + params = params || {}; + this.apiId = params.apiId; + this.phloId = params.phloId; + this.message = params.message; + + } +} + +export class RetrievePHLOResponse { + constructor(params) { + params = params || {}; + this.apiId = params.apiId; + this.phloId = params.phloId; + this.name = params.name; + this.createdOn = params.createdOn; + + } +} + /** * Represents a Phlo * @constructor @@ -14,39 +46,58 @@ const idField = 'phloUuid'; * @param {object} [data] - data of phlo */ export class Phlo extends PlivoResource { - constructor(client, data = {}) { - super(action, Phlo, idField, client); - extend(this, data); + constructor(client, data = {}) { + super(action, Phlo, idField, client); + extend(this, data); + this.client = client; - this.client = client; + // Define multiparty call getters + let item = this; + this.multiPartyCall = function(nodeId) { + let dd = new PhloMultiPartyCall(client, { + phloId: item.phloId, + nodeId: nodeId + }); + return dd; + }; - // Define multiparty call getters - let item = this; - this.multiPartyCall = function (nodeId) { - let dd = new PhloMultiPartyCall(client, { phloId: item.phloId, nodeId: nodeId }); - return dd; - }; + this.multiPartyCall.get = function(nodeId) { + let dd = new PhloMultiPartyCallInterface(client, { + phloId: item.phloId, + nodeId: nodeId + }); + return dd.get(item.phloId, nodeId); + } + + this[clientKey] = client; - this.multiPartyCall.get = function (nodeId) { - let dd = new PhloMultiPartyCallInterface(client, { phloId: item.phloId, nodeId: nodeId }); - return dd.get(item.phloId, nodeId); } - } + /** + * run phlo + * @method + * @promise {Boolean} return true if phlo is complete + * @fail {Error} return Error + */ + run(params) { - /** - * run phlo - * @method - * @promise {Boolean} return true if phlo is complete - * @fail {Error} return Error - */ - run(params) { + //Url for phlo running + // https://phlorunner.plivo.com/v1/account/{AUTH_ID}/phlo/{PHLO_ID} + let action = 'account/' + this.authId + '/phlo/' + this.phloId; + let client = this[clientKey]; + action = action == null ? this[actionKey] : action; - //Url for phlo running - // https://phlorunner.plivo.com/v1/account/{AUTH_ID}/phlo/{PHLO_ID} - let action = 'account/' + this.authId + '/phlo/' + this.phloId; - return super.executeAction(action, 'POST', params, ''); - } + return new Promise((resolve, reject) => { + client('POST', action, params) + .then(response => { + resolve(new RunPHLOResponse(response.body, idField)); + }) + .catch(error => { + reject(error); + }); + }); + + } } @@ -58,37 +109,52 @@ export class Phlo extends PlivoResource { */ export class PhloInterface extends PlivoResourceInterface { - constructor(client, data = {}) { - super(action, Phlo, idField, client); - extend(this, data); - } - - /** - * Get A Phlo Detail - * @method - * @param {string} id - phlo uuid to get information of. - * @promise {object} returns Phlo Object - * @fail {Error} returns Error - */ - get(id) { - - //Validate id first - let errors = validate([{ - field: 'id', - value: id, - validators: ['isRequired'] - }]); - - if (errors) { - return errors; + constructor(client, data = {}) { + super(action, Phlo, idField, client); + extend(this, data); + this[clientKey] = client; } - let params = { - phlo_id: id - }; + /** + * Get A Phlo Detail + * @method + * @param {string} id - phlo uuid to get information of. + * @promise {object} returns Phlo Object + * @fail {Error} returns Error + */ + get(id) { - // Url pattern for getting phlo resource by id - // https://phlorunner.plivo.com/v1/phlo/{phlo_id} - return super.get(id, params); - } -} + //Validate id first + let errors = validate([{ + field: 'id', + value: id, + validators: ['isRequired'] + }]); + + if (errors) { + return errors; + } + + let params = { + phlo_id: id + }; + + // Url pattern for getting phlo resource by id + // https://phlorunner.plivo.com/v1/phlo/{phlo_id} + let client = this[clientKey]; + + return new Promise((resolve, reject) => { + if (action !== '' && !id) { + reject(new Error(this[idKey] + ' must be set')); + } + + client('GET', action + (id ? id + '/' : ''), params) + .then(response => { + resolve(new RetrievePHLOResponse(response.body, client)); + }) + .catch(error => { + reject(error); + }); + }); + } +} \ No newline at end of file diff --git a/lib/resources/phloMultiPartyCallMember.js b/lib/resources/phloMultiPartyCallMember.js index b1c469e..58b9399 100644 --- a/lib/resources/phloMultiPartyCallMember.js +++ b/lib/resources/phloMultiPartyCallMember.js @@ -1,5 +1,11 @@ -import { extend, validate } from '../utils/common.js'; -import { PlivoResource, PlivoResourceInterface } from '../base'; +import { + PlivoResource, + PlivoResourceInterface +} from '../base'; +import { + extend, + validate +} from '../utils/common.js'; const clientKey = Symbol(); const action = 'Phlo/'; @@ -11,6 +17,14 @@ const idField = 'phloUuid'; * @param {function} client - make api call * @param {object} [data] - data of phlo */ + +export class UpdateMemberResponse { + constructor(params) { + params = params || {}; + this.apiId = params.apiId; + this.error = params.error; + } +} export class PhloMultiPartyCallMember extends PlivoResource { constructor(client, data = {}) { @@ -51,7 +65,16 @@ export class PhloMultiPartyCallMember extends PlivoResource { // https://phlorunner.plivo.com/v1/phlo/{PHLO_ID}/multi_party_call/{NODE_ID}/members/{MemberAddress} let task = this.action + this.memberAddress; - return super.executeAction(task, 'POST', params, ''); + let client = this[clientKey]; + return new Promise((resolve, reject) => { + client('POST', task, params) + .then(response => { + resolve(new UpdateMemberResponse(response.body, idField)); + }) + .catch(error => { + reject(error); + }); + }); } } @@ -79,7 +102,11 @@ export class PhloMultiPartyCallMemberInterface extends PlivoResourceInterface { return errors; } - return new PhloMultiPartyCallMember(this.client, { phloId: phloId, nodeId: nodeId, memberAddress: memberAddress }); + return new PhloMultiPartyCallMember(this.client, { + phloId: phloId, + nodeId: nodeId, + memberAddress: memberAddress + }); } -} +} \ No newline at end of file diff --git a/lib/resources/phloMultipartyCall.js b/lib/resources/phloMultipartyCall.js index 9e6ac68..c8dd65b 100644 --- a/lib/resources/phloMultipartyCall.js +++ b/lib/resources/phloMultipartyCall.js @@ -1,10 +1,40 @@ -import { extend, validate } from '../utils/common.js'; -import { PlivoResource, PlivoResourceInterface } from '../base'; -import { PhloMultiPartyCallMemberInterface, PhloMultiPartyCallMember } from './phloMultiPartyCallMember'; +import { + PhloMultiPartyCallMember, + PhloMultiPartyCallMemberInterface +} from './phloMultiPartyCallMember'; +import { + PlivoResource, + PlivoResourceInterface +} from '../base'; +import { + extend, + validate +} from '../utils/common.js'; const clientKey = Symbol(); const idField = 'nodeId'; + +export class UpdateMultipartyCallResponse { + constructor(params) { + params = params || {}; + this.apiId = params.apiId; + this.error = params.error; + } +} + +export class RetrieveMultipartyCallResponse { + constructor(params) { + params = params || {}; + this.apiId = params.apiId; + this.nodeId = params.nodeId; + this.phloId = params.phloId; + this.name = params.name; + this.nodeType = params.nodeType; + this.createdOn = params.createdOn; + } +} + export class PhloMultiPartyCall extends PlivoResource { constructor(client, data = {}) { let action = 'phlo/' + data.phloId + '/multi_party_call/'; @@ -12,16 +42,25 @@ export class PhloMultiPartyCall extends PlivoResource { extend(this, data); this.action = action; this.client = client; + this[clientKey] = client; // Define member getters let item = this; - this.member = function (memberAddress) { - let dd = new PhloMultiPartyCallMember(client, { phloId: item.phloId, nodeId: item.nodeId, memberAddress: memberAddress }); + this.member = function(memberAddress) { + let dd = new PhloMultiPartyCallMember(client, { + phloId: item.phloId, + nodeId: item.nodeId, + memberAddress: memberAddress + }); return dd; }; - this.member.get = function (memberAddress) { - let dd = new PhloMultiPartyCallMemberInterface(client, { phloId: item.phloId, nodeId: item.nodeId, memberAddress: memberAddress }); + this.member.get = function(memberAddress) { + let dd = new PhloMultiPartyCallMemberInterface(client, { + phloId: item.phloId, + nodeId: item.nodeId, + memberAddress: memberAddress + }); return dd.get(item.phloId, item.nodeId, memberAddress); } @@ -53,7 +92,6 @@ export class PhloMultiPartyCall extends PlivoResource { action: action }; - // Url pattern for mp call update // https://phlorunnner.plivo.com/v1/phlo/{phlo_id}/{node_type}/{node_id} let task = this.action + this.nodeId; @@ -65,8 +103,16 @@ export class PhloMultiPartyCall extends PlivoResource { params.trigger_source = triggerSource; } - return super.executeAction(task, 'POST', params, ''); - + let client = this[clientKey]; + return new Promise((resolve, reject) => { + client('POST', task, params) + .then(response => { + resolve(new UpdateMultipartyCallResponse(response.body, idField)); + }) + .catch(error => { + reject(error); + }); + }); } } @@ -78,15 +124,16 @@ export class PhloMultiPartyCallInterface extends PlivoResourceInterface { let action = 'phlo/' + data.phloId + '/multi_party_call/'; super(action, PhloMultiPartyCall, idField, client); extend(this, data); + this[clientKey] = client; } /** - * Get A Phlo Detail - * @method - * @param {string} id - phlo uuid to get information of. - * @promise {object} returns Phlo Object - * @fail {Error} returns Error - */ + * Get A Phlo Detail + * @method + * @param {string} id - phlo uuid to get information of. + * @promise {object} returns Phlo Object + * @fail {Error} returns Error + */ get(phloId, id) { //Validate id first @@ -109,8 +156,18 @@ export class PhloMultiPartyCallInterface extends PlivoResourceInterface { // Url pattern for getting phlo resource by id // https://phlorunner.plivo.com/v1/phlo/{phlo_id} - // console.log('get multi party call with ', id, params); - return super.get(id, params); - + let client = this[clientKey]; + return new Promise((resolve, reject) => { + if (action !== '' && !id) { + reject(new Error(this[idKey] + ' must be set')); + } + client('GET', action + (id ? id + '/' : ''), params) + .then(response => { + resolve(new RetrieveMultipartyCallResponse(client, response.body)); + }) + .catch(error => { + reject(error); + }); + }); } -} +} \ No newline at end of file diff --git a/lib/resources/powerpacks.js b/lib/resources/powerpacks.js index f10c34e..0941181 100644 --- a/lib/resources/powerpacks.js +++ b/lib/resources/powerpacks.js @@ -1,12 +1,165 @@ -import { extend, validate } from '../utils/common.js'; -import { PlivoResource, PlivoResourceInterface } from '../base'; import * as _ from 'lodash'; +import { + PlivoResource, + PlivoResourceInterface +} from '../base'; +import { + extend, + validate +} from '../utils/common.js'; + const action = 'Powerpack/'; const idField = 'uuid'; const numberpoolIdField = 'numberPool'; const clientKey = Symbol(); + +export class ListAllNumbersResponse { + constructor(params) { + params = params || {}; + this.apiId = params.apiId; + this.meta = params.meta; + this.objects = params.objects; + } +} + +export class CreatePowerpackResponse { + constructor(params) { + params = params || {}; + this.apiId = params.apiId; + this.applicationId = params.applicationId; + this.applicationType = params.applicationType; + this.createdOn = params.createdOn; + this.localConnect = params.localConnect; + this.name = params.name; + this.numberPool = params.numberPool; + this.numberPriority = params.numberPriority; + this.stickySender = params.stickySender; + this.uuid = params.uuid; + } +} + +export class UpdatePowerpackResponse { + constructor(params) { + params = params || {}; + this.apiId = params.apiId; + this.applicationId = params.applicationId; + this.applicationType = params.applicationType; + this.createdOn = params.createdOn; + this.localConnect = params.localConnect; + this.name = params.name; + this.numberPool = params.numberPool; + this.stickySender = params.stickySender; + this.uuid = params.uuid; + } +} + +export class ListShortCodeResponse { + constructor(params) { + params = params || {}; + this.apiId = params.apiId; + this.meta = params.meta; + this.objects = params.objects; + } +} +export class ListTollFreeResponse { + constructor(params) { + params = params || {}; + this.apiId = params.apiId; + this.meta = params.meta; + this.objects = params.objects; + } +} + +export class AddNumberResponse { + constructor(params) { + params = params || {}; + this.apiId = params.apiId; + this.accountPhoneNumberResource = params.accountPhoneNumberResource; + this.addedOn = params.addedOn; + this.countryIso2 = params.countryIso2; + this.number = params.number; + this.numberPoolUuid = params.numberPoolUuid; + this.type = params.type; + this.service = params.service; + } +} + +export class RemoveNumberResponse { + constructor(params) { + params = params || {}; + this.apiId = params.apiId; + this.response = params.response; + } +} + +export class RemoveTollFreeNumberResponse { + constructor(params) { + params = params || {}; + this.apiId = params.apiId; + this.response = params.response; + } +} + +export class RemoveShortCodeResponse { + constructor(params) { + params = params || {}; + this.apiId = params.apiId; + this.response = params.response; + } +} +export class AddTollFreeNumberresponse { + constructor(params) { + params = params || {}; + this.apiId = params.apiId; + this.accountPhoneNumberResource = params.accountPhoneNumberResource; + this.addedOn = params.addedOn; + this.countryIso2 = params.countryIso2; + this.number = params.number; + this.numberPoolUuid = params.numberPoolUuid; + this.type = params.type; + this.service = params.service; + } +} + +export class RetrieveNumberResponse { + constructor(params) { + params = params || {}; + this.apiId = params.apiId; + this.accountPhoneNumberResource = params.accountPhoneNumberResource; + this.addedOn = params.addedOn; + this.countryIso2 = params.countryIso2; + this.number = params.number; + this.numberPoolUuid = params.numberPoolUuid; + this.type = params.type; + } +} + + +export class RetrieveTollFreeResponse { + constructor(params) { + params = params || {}; + this.apiId = params.apiId; + this.accountPhoneNumberResource = params.accountPhoneNumberResource; + this.addedOn = params.addedOn; + this.countryIso2 = params.countryIso2; + this.number = params.number; + this.numberPoolUuid = params.numberPoolUuid; + this.type = params.type; + } +} +export class RetrieveShortCodeResponse { + constructor(params) { + params = params || {}; + this.apiId = params.apiId; + this.addedOn = params.addedOn; + this.countryIso2 = params.countryIso2; + this.shortCode = params.shortCode; + this.numberPoolUuid = params.numberPoolUuid; + } +} + /** * Represents a Powerpack * @constructor @@ -14,180 +167,286 @@ const clientKey = Symbol(); * @param {object} [data] - data of call */ export class Powerpack extends PlivoResource { - constructor(client, data = {}) { - super(action, Powerpack, idField, client); + constructor(client, data = {}) { + super(action, Powerpack, idField, client); - if (idField in data) { - this.uuid = data[idField]; - } - if (numberpoolIdField in data) { - this.number_pool_id = data[numberpoolIdField].split('/')[5]; - } - this.number_pool = new NumberPool(client, { - number_pool_id: this.number_pool_id - }); - extend(this, data); - } + if (idField in data) { + this.uuid = data[idField]; + } + if (numberpoolIdField in data) { + this.number_pool_id = data[numberpoolIdField].split('/')[5]; + } + this.number_pool = new NumberPool(client, { + number_pool_id: this.number_pool_id + }); + extend(this, data); + this[clientKey] = client; + } - list_numbers(params) { - let query = this.search_query(params); - var queryparams = {}; - queryparams['search'] = 'hack'; - let path = 'NumberPool/' + this.number_pool_id + '/Number/?' + query; - return super.customexecuteAction(path.toString().trim(), 'GET', queryparams); - } + list_numbers(params) { + let query = this.search_query(params); + var queryparams = {}; + queryparams['search'] = 'hack'; + let path = 'NumberPool/' + this.number_pool_id + '/Number/?' + query; + let client = this[clientKey]; + return new Promise((resolve, reject) => { + client('GET', path.toString().trim(), queryparams) + .then(response => { + resolve(new ListAllNumbersResponse(response.body, idField)); + }) + .catch(error => { + reject(error); + }); + }); + } - search_query(params) { - if (params === undefined) { - params = {}; - } - var query = ''; - if (params.type != undefined) { - query = 'type=' + params.type; - } - if (params.starts_with != undefined) { - if (query == '') { - query = 'starts_with=' + params.starts_with; - } else { - query += '&starts_with=' + params.starts_with; - } - } - // return params; - if (params.country_iso2 != undefined) { - if (query == '') { - query = 'country_iso2=' + params.country_iso2; - } else { - query += '&country_iso2=' + params.country_iso2; - } - } - if (params.limit != undefined) { - if (query == '') { - query = 'limit=' + params.limit; - } else { - query += '&limit=' + params.limit; - } - } - if (params.offset != undefined) { - if (query == '') { - query = 'offset=' + params.offset; - } else { - query += '&offset=' + params.offset; - } - } + search_query(params) { + if (params === undefined) { + params = {}; + } + var query = ''; + if (params.type != undefined) { + query = 'type=' + params.type; + } + if (params.starts_with != undefined) { + if (query == '') { + query = 'starts_with=' + params.starts_with; + } else { + query += '&starts_with=' + params.starts_with; + } + } + // return params; + if (params.country_iso2 != undefined) { + if (query == '') { + query = 'country_iso2=' + params.country_iso2; + } else { + query += '&country_iso2=' + params.country_iso2; + } + } + if (params.limit != undefined) { + if (query == '') { + query = 'limit=' + params.limit; + } else { + query += '&limit=' + params.limit; + } + } + if (params.offset != undefined) { + if (query == '') { + query = 'offset=' + params.offset; + } else { + query += '&offset=' + params.offset; + } + } - if (params.service != undefined) { - if (query == '') { - query = 'service=' + params.service; - } else { - query += '&service=' + params.service; - } - } + if (params.service != undefined) { + if (query == '') { + query = 'service=' + params.service; + } else { + query += '&service=' + params.service; + } + } - query = query + '&'; + query = query + '&'; - return query; - } + return query; + } - count_numbers(params) { - let query = this.search_query(params); - var queryparams = {}; - queryparams['search'] = 'hack'; - let path = 'NumberPool/' + this.number_pool_id + '/Number/?' + query; - return super.getMetaResponse(path.toString().trim(), 'GET', queryparams); - } + count_numbers(params) { + let query = this.search_query(params); + var queryparams = {}; + queryparams['search'] = 'hack'; + let path = 'NumberPool/' + this.number_pool_id + '/Number/?' + query; + return super.getMetaResponse(path.toString().trim(), 'GET', queryparams); + } - find_number(number) { - let path = 'NumberPool/' + this.number_pool_id + '/Number/' + number + '/'; - return super.customexecuteAction(path.toString().trim(), 'GET'); - } - add_number(number, service = '') { - var params = {}; - params['rent'] = 'false'; - if (service != '') { - params['service'] = service - } - let path = 'NumberPool/' + this.number_pool_id + '/Number/' + number + '/'; - return super.customexecuteAction(path.toString().trim(), 'POST', params); - } - add_tollfree(tollfree, service = '') { - var params = {}; - params['rent'] = 'false'; - if (service != '') { - params['service'] = service - } - let path = 'NumberPool/' + this.number_pool_id + '/Tollfree/' + tollfree + '/'; - return super.customexecuteAction(path.toString().trim(), 'POST', params); - } - remove_number(number, unrent = false) { - var params = {}; - params['unrent'] = unrent.toString(); - let path = 'NumberPool/' + this.number_pool_id + '/Number/' + number + '/'; - return super.customexecuteAction(path.toString().trim(), 'DELETE', params); - } - remove_tollfree(tollfree, unrent = false) { - var params = {}; - params['unrent'] = unrent.toString(); - let path = 'NumberPool/' + this.number_pool_id + '/Tollfree/' + tollfree + '/'; - return super.customexecuteAction(path.toString().trim(), 'DELETE', params); - } - remove_shortcode(shortcode) { - let path = 'NumberPool/' + this.number_pool_id + '/Shortcode/' + shortcode + '/'; - return super.customexecuteAction(path.toString().trim(), 'DELETE'); - } - list_shortcodes(params) { - if (params === undefined) { - params = {}; - } - let path = 'NumberPool/' + this.number_pool_id + '/Shortcode/'; - return super.customexecuteAction(path.toString().trim(), 'GET', params); - } - list_tollfree(params) { - if (params === undefined) { - params = {}; - } - let path = 'NumberPool/' + this.number_pool_id + '/Tollfree/'; - return super.customexecuteAction(path.toString().trim(), 'GET', params); - } - find_shortcode(shortcode, service = '') { - let path = 'NumberPool/' + this.number_pool_id + '/Shortcode/' + shortcode + '/'; - if (service != '') { - path = path + '&service=' + service - } - return super.customexecuteAction(path.toString().trim(), 'GET'); - } - find_tollfree(tollfree, service = '') { - let path = 'NumberPool/' + this.number_pool_id + '/Tollfree/' + tollfree + '/'; - if (service != '') { - path = path + '&service=' + service - } - return super.customexecuteAction(path.toString().trim(), 'GET'); - } - buy_add_number(params) { - var number = params.number; - var rentparam = {}; - rentparam['rent'] = 'true'; - if (params.number == undefined) { - try { - if (params.country_iso2 != undefined) { - params['country_iso'] = params.country_iso2; - } - if (params.service != undefined) { - params['service'] = params.service; - } - var test = super.customexecuteGetNumberAction('PhoneNumber/', 'GET', params); - return test.then((val) => { - let path = 'NumberPool/' + this.number_pool_id + '/Number/' + val + '/'; - return super.customexecuteAction(path.toString().trim(), 'POST', rentparam); - }); - } catch (err) { - return err.message; - } - } - let path = 'NumberPool/' + this.number_pool_id + '/Number/' + number + '/'; - return super.customexecuteAction(path.toString().trim(), 'POST', rentparam); - } + find_number(number) { + let path = 'NumberPool/' + this.number_pool_id + '/Number/' + number + '/'; + let client = this[clientKey]; + return new Promise((resolve, reject) => { + client('GET', path.toString().trim()) + .then(response => { + resolve(new RetrieveNumberResponse(response.body, idField)); + }) + .catch(error => { + reject(error); + }); + }); + } + add_number(number, service = '') { + var params = {}; + params['rent'] = 'false'; + if (service != '') { + params['service'] = service + } + let client = this[clientKey]; + let path = 'NumberPool/' + this.number_pool_id + '/Number/' + number + '/'; + return new Promise((resolve, reject) => { + client('POST', path.toString().trim(), params) + .then(response => { + resolve(new AddNumberResponse(response.body, idField)); + }) + .catch(error => { + reject(error); + }); + }); + } - /** + add_tollfree(tollfree, service = '') { + var params = {}; + params['rent'] = 'false'; + if (service != '') { + params['service'] = service + } + let path = 'NumberPool/' + this.number_pool_id + '/Tollfree/' + tollfree + '/'; + let client = this[clientKey]; + return new Promise((resolve, reject) => { + client('POST', path.toString().trim(), params) + .then(response => { + resolve(new AddTollFreeNumberresponse(response.body, idField)); + }) + .catch(error => { + reject(error); + }); + }); + } + + remove_number(number, unrent = false) { + var params = {}; + params['unrent'] = unrent.toString(); + let path = 'NumberPool/' + this.number_pool_id + '/Number/' + number + '/'; + let client = this[clientKey]; + return new Promise((resolve, reject) => { + client('DELETE', path.toString().trim(), params) + .then(response => { + resolve(new RemoveNumberResponse(response.body, idField)); + }) + .catch(error => { + reject(error); + }); + }); + } + remove_tollfree(tollfree, unrent = false) { + var params = {}; + params['unrent'] = unrent.toString(); + let path = 'NumberPool/' + this.number_pool_id + '/Tollfree/' + tollfree + '/'; + let client = this[clientKey]; + return new Promise((resolve, reject) => { + client('DELETE', path.toString().trim(), params) + .then(response => { + resolve(new RemoveTollFreeNumberResponse(response.body, idField)); + }) + .catch(error => { + reject(error); + }); + }); + } + remove_shortcode(shortcode) { + let path = 'NumberPool/' + this.number_pool_id + '/Shortcode/' + shortcode + '/'; + let client = this[clientKey]; + return new Promise((resolve, reject) => { + client('DELETE', path.toString().trim()) + .then(response => { + resolve(new RemoveShortCodeResponse(response.body, idField)); + }) + .catch(error => { + reject(error); + }); + }); + } + + list_shortcodes(params) { + if (params === undefined) { + params = {}; + } + let path = 'NumberPool/' + this.number_pool_id + '/Shortcode/'; + let client = this[clientKey]; + return new Promise((resolve, reject) => { + client('GET', path.toString().trim(), params) + .then(response => { + resolve(new ListShortCodeResponse(response.body, idField)); + }) + .catch(error => { + reject(error); + }); + }); + } + list_tollfree(params) { + if (params === undefined) { + params = {}; + } + let path = 'NumberPool/' + this.number_pool_id + '/Tollfree/'; + let client = this[clientKey]; + return new Promise((resolve, reject) => { + client('GET', path.toString().trim(), params) + .then(response => { + resolve(new ListTollFreeResponse(response.body, idField)); + }) + .catch(error => { + reject(error); + }); + }); + } + + find_shortcode(shortcode, service = '') { + let path = 'NumberPool/' + this.number_pool_id + '/Shortcode/' + shortcode + '/'; + if (service != '') { + path = path + '&service=' + service + } + let client = this[clientKey]; + return new Promise((resolve, reject) => { + client('GET', path.toString().trim()) + .then(response => { + resolve(new RetrieveShortCodeResponse(response.body, idField)); + }) + .catch(error => { + reject(error); + }); + }); + } + + find_tollfree(tollfree, service = '') { + let path = 'NumberPool/' + this.number_pool_id + '/Tollfree/' + tollfree + '/'; + if (service != '') { + path = path + '&service=' + service + } + let client = this[clientKey]; + return new Promise((resolve, reject) => { + client('GET', path.toString().trim()) + .then(response => { + resolve(new RetrieveTollFreeResponse(response.body, idField)); + }) + .catch(error => { + reject(error); + }); + }); + } + + buy_add_number(params) { + var number = params.number; + var rentparam = {}; + rentparam['rent'] = 'true'; + if (params.number == undefined) { + try { + if (params.country_iso2 != undefined) { + params['country_iso'] = params.country_iso2; + } + if (params.service != undefined) { + params['service'] = params.service; + } + var test = super.customexecuteGetNumberAction('PhoneNumber/', 'GET', params); + return test.then((val) => { + let path = 'NumberPool/' + this.number_pool_id + '/Number/' + val + '/'; + return super.customexecuteAction(path.toString().trim(), 'POST', rentparam); + }); + } catch (err) { + return err.message; + } + } + let path = 'NumberPool/' + this.number_pool_id + '/Number/' + number + '/'; + return super.customexecuteAction(path.toString().trim(), 'POST', rentparam); + } + + /** * update powerpack * @method * @param {object} params - to update Powerpack @@ -200,210 +459,220 @@ export class Powerpack extends PlivoResource { * @promise {object} return {@link Powerpack} object * @fail {Error} return Error */ - update(params) { - let path = 'Powerpack/' + this.uuid + '/'; - return super.customexecuteAction(path.toString().trim(), 'POST', params); - } + update(params) { + let path = 'Powerpack/' + this.uuid + '/'; + //return super.customexecuteAction(path.toString().trim(), 'POST', params); + let client = this[clientKey]; + return new Promise((resolve, reject) => { + client('GET', path.toString().trim(), params) + .then(response => { + resolve(new UpdatePowerpackResponse(response.body, idField)); + }) + .catch(error => { + reject(error); + }); + }); + } - /** - * delete Powerpack - * @method - * @promise {object} return true on success - * @fail {Error} return Error - */ - delete(unrent_numbers = false) { - let params = {}; - if (typeof unrent_numbers === 'boolean') { - params.unrent_numbers = unrent_numbers.toString(); - } - let path = 'Powerpack/' + this.uuid + '/'; - return super.customexecuteAction(path.toString().trim(), 'DELETE', params); - } + /** + * delete Powerpack + * @method + * @promise {object} return true on success + * @fail {Error} return Error + */ + delete(unrent_numbers = false) { + let params = {}; + if (typeof unrent_numbers === 'boolean') { + params.unrent_numbers = unrent_numbers.toString(); + } + let path = 'Powerpack/' + this.uuid + '/'; + return super.customexecuteAction(path.toString().trim(), 'DELETE', params); + } } const numberPoolField = 'number_pool_id'; export class NumberPool extends PlivoResource { - constructor(client, data = {}) { - super(action, NumberPool, numberPoolField, client); - this.numbers = new Numbers(client, { - number_pool_id: data.number_pool_id - }); - this.shortcodes = new Shortcode(client, { - number_pool_id: data.number_pool_id - }); - this.tollfree = new Tollfree(client, { - number_pool_id: data.number_pool_id - }); + constructor(client, data = {}) { + super(action, NumberPool, numberPoolField, client); + this.numbers = new Numbers(client, { + number_pool_id: data.number_pool_id + }); + this.shortcodes = new Shortcode(client, { + number_pool_id: data.number_pool_id + }); + this.tollfree = new Tollfree(client, { + number_pool_id: data.number_pool_id + }); - extend(this, data); - } + extend(this, data); + } } export class Numbers extends PlivoResource { - constructor(client, data = {}) { - super(action, Numbers, numberPoolField, client); - extend(this, data); - } - buy_add_number(params) { - var number = params.number; - var rentparam = {}; - rentparam['rent'] = 'true'; - if (params.number == undefined) { - try { - if (params.country_iso2 != undefined) { - params['country_iso'] = params.country_iso2; - } - if (params.service != undefined) { - params['service'] = params.service; - } - var test = super.customexecuteGetNumberAction('PhoneNumber/', 'GET', params); - return test.then((val) => { - let path = 'NumberPool/' + this.number_pool_id + '/Number/' + val + '/'; - return super.customexecuteAction(path.toString().trim(), 'POST', rentparam); - }); - } catch (err) { - return err.message; - } - } - let path = 'NumberPool/' + this.number_pool_id + '/Number/' + number + '/'; - return super.customexecuteAction(path.toString().trim(), 'POST', rentparam); - } + constructor(client, data = {}) { + super(action, Numbers, numberPoolField, client); + extend(this, data); + } + buy_add_number(params) { + var number = params.number; + var rentparam = {}; + rentparam['rent'] = 'true'; + if (params.number == undefined) { + try { + if (params.country_iso2 != undefined) { + params['country_iso'] = params.country_iso2; + } + if (params.service != undefined) { + params['service'] = params.service; + } + var test = super.customexecuteGetNumberAction('PhoneNumber/', 'GET', params); + return test.then((val) => { + let path = 'NumberPool/' + this.number_pool_id + '/Number/' + val + '/'; + return super.customexecuteAction(path.toString().trim(), 'POST', rentparam); + }); + } catch (err) { + return err.message; + } + } + let path = 'NumberPool/' + this.number_pool_id + '/Number/' + number + '/'; + return super.customexecuteAction(path.toString().trim(), 'POST', rentparam); + } - list(params) { - let query = this.search_query(params); - var queryparams = {}; - queryparams['search'] = 'hack'; - let path = 'NumberPool/' + this.number_pool_id + '/Number//?' + query; - return super.customexecuteAction(path.toString().trim(), 'GET', queryparams); - } - count(params) { - let query = this.search_query(params); - var queryparams = {}; - queryparams['search'] = 'hack'; - let path = 'NumberPool/' + this.number_pool_id + '/Number//?' + query; - return super.getMetaResponse(path.toString().trim(), 'GET', queryparams); - } + list(params) { + let query = this.search_query(params); + var queryparams = {}; + queryparams['search'] = 'hack'; + let path = 'NumberPool/' + this.number_pool_id + '/Number//?' + query; + return super.customexecuteAction(path.toString().trim(), 'GET', queryparams); + } + count(params) { + let query = this.search_query(params); + var queryparams = {}; + queryparams['search'] = 'hack'; + let path = 'NumberPool/' + this.number_pool_id + '/Number//?' + query; + return super.getMetaResponse(path.toString().trim(), 'GET', queryparams); + } - search_query(params) { - if (params === undefined) { - params = {}; - } - var query = ''; - if (params.type != undefined) { - query = 'type=' + params.type; - } - if (params.starts_with != undefined) { - if (query == '') { - query = 'starts_with=' + params.starts_with; - } else { - query += '&starts_with=' + params.starts_with; - } - } - // return params; - if (params.country_iso2 != undefined) { - if (query == '') { - query = 'country_iso2=' + params.country_iso2; - } else { - query += '&country_iso2=' + params.country_iso2; - } - } - if (params.limit != undefined) { - if (query == '') { - query = 'limit=' + params.limit; - } else { - query += '&limit=' + params.limit; - } - } - if (params.offset != undefined) { - if (query == '') { - query = 'offset=' + params.offset; - } else { - query += '&offset=' + params.offset; - } - } - if (params.service != undefined) { - if (query == '') { - query = 'service=' + params.service; - } else { - query += '&service=' + params.service; - } - } + search_query(params) { + if (params === undefined) { + params = {}; + } + var query = ''; + if (params.type != undefined) { + query = 'type=' + params.type; + } + if (params.starts_with != undefined) { + if (query == '') { + query = 'starts_with=' + params.starts_with; + } else { + query += '&starts_with=' + params.starts_with; + } + } + // return params; + if (params.country_iso2 != undefined) { + if (query == '') { + query = 'country_iso2=' + params.country_iso2; + } else { + query += '&country_iso2=' + params.country_iso2; + } + } + if (params.limit != undefined) { + if (query == '') { + query = 'limit=' + params.limit; + } else { + query += '&limit=' + params.limit; + } + } + if (params.offset != undefined) { + if (query == '') { + query = 'offset=' + params.offset; + } else { + query += '&offset=' + params.offset; + } + } + if (params.service != undefined) { + if (query == '') { + query = 'service=' + params.service; + } else { + query += '&service=' + params.service; + } + } - query = query + '&'; + query = query + '&'; - return query; - } + return query; + } - find(number) { - let path = 'NumberPool/' + this.number_pool_id + '/Number/' + number + '/'; - return super.customexecuteAction(path.toString().trim(), 'GET'); - } - add(number, service='') { - var params = {}; - params['rent'] = 'false'; - if (service != '') { - params['service'] = service - } - let path = 'NumberPool/' + this.number_pool_id + '/Number/' + number + '/'; - return super.customexecuteAction(path.toString().trim(), 'POST', params); - } - remove(number, unrent = false) { - var params = {}; - params['unrent'] = unrent.toString(); - let path = 'NumberPool/' + this.number_pool_id + '/Number/' + number + '/'; - return super.customexecuteAction(path.toString().trim(), 'DELETE', params); - } + find(number) { + let path = 'NumberPool/' + this.number_pool_id + '/Number/' + number + '/'; + return super.customexecuteAction(path.toString().trim(), 'GET'); + } + add(number, service = '') { + var params = {}; + params['rent'] = 'false'; + if (service != '') { + params['service'] = service + } + let path = 'NumberPool/' + this.number_pool_id + '/Number/' + number + '/'; + return super.customexecuteAction(path.toString().trim(), 'POST', params); + } + remove(number, unrent = false) { + var params = {}; + params['unrent'] = unrent.toString(); + let path = 'NumberPool/' + this.number_pool_id + '/Number/' + number + '/'; + return super.customexecuteAction(path.toString().trim(), 'DELETE', params); + } } export class Shortcode extends PlivoResource { - constructor(client, data = {}) { - super(action, Shortcode, numberPoolField, client); - extend(this, data); - this.number_pool_id = data.number_pool_id; - } - list(params) { - if (params === undefined) { - params = {}; - } - let path = 'NumberPool/' + this.number_pool_id + '/Shortcode/'; - return super.customexecuteAction(path.toString().trim(), 'GET', params); - } - find(shortcode) { - let path = 'NumberPool/' + this.number_pool_id + '/Shortcode/' + shortcode + '/'; - return super.customexecuteAction(path.toString().trim(), 'GET'); - } - remove(shortcode) { - let path = 'NumberPool/' + this.number_pool_id + '/Shortcode/' + shortcode + '/'; - return super.customexecuteAction(path.toString().trim(), 'DELETE'); - } + constructor(client, data = {}) { + super(action, Shortcode, numberPoolField, client); + extend(this, data); + this.number_pool_id = data.number_pool_id; + } + list(params) { + if (params === undefined) { + params = {}; + } + let path = 'NumberPool/' + this.number_pool_id + '/Shortcode/'; + return super.customexecuteAction(path.toString().trim(), 'GET', params); + } + find(shortcode) { + let path = 'NumberPool/' + this.number_pool_id + '/Shortcode/' + shortcode + '/'; + return super.customexecuteAction(path.toString().trim(), 'GET'); + } + remove(shortcode) { + let path = 'NumberPool/' + this.number_pool_id + '/Shortcode/' + shortcode + '/'; + return super.customexecuteAction(path.toString().trim(), 'DELETE'); + } } export class Tollfree extends PlivoResource { - constructor(client, data = {}) { - super(action, Tollfree, numberPoolField, client); - extend(this, data); - this.number_pool_id = data.number_pool_id; - } - add(tollfree) { - var params = {}; - params['rent'] = 'false'; - let path = 'NumberPool/' + this.number_pool_id + '/Tollfree/' + tollfree + '/'; - return super.customexecuteAction(path.toString().trim(), 'POST', params); - } - remove(tollfree, unrent = false) { - var params = {}; - params['unrent'] = unrent.toString(); - let path = 'NumberPool/' + this.number_pool_id + '/Tollfree/' + tollfree + '/'; - return super.customexecuteAction(path.toString().trim(), 'DELETE', params); - } - list(params) { - if (params === undefined) { - params = {}; - } - let path = 'NumberPool/' + this.number_pool_id + '/Tollfree/'; - return super.customexecuteAction(path.toString().trim(), 'GET', params); - } - find(tollfree) { - let path = 'NumberPool/' + this.number_pool_id + '/Tollfree/' + tollfree + '/'; - return super.customexecuteAction(path.toString().trim(), 'GET'); - } + constructor(client, data = {}) { + super(action, Tollfree, numberPoolField, client); + extend(this, data); + this.number_pool_id = data.number_pool_id; + } + add(tollfree) { + var params = {}; + params['rent'] = 'false'; + let path = 'NumberPool/' + this.number_pool_id + '/Tollfree/' + tollfree + '/'; + return super.customexecuteAction(path.toString().trim(), 'POST', params); + } + remove(tollfree, unrent = false) { + var params = {}; + params['unrent'] = unrent.toString(); + let path = 'NumberPool/' + this.number_pool_id + '/Tollfree/' + tollfree + '/'; + return super.customexecuteAction(path.toString().trim(), 'DELETE', params); + } + list(params) { + if (params === undefined) { + params = {}; + } + let path = 'NumberPool/' + this.number_pool_id + '/Tollfree/'; + return super.customexecuteAction(path.toString().trim(), 'GET', params); + } + find(tollfree) { + let path = 'NumberPool/' + this.number_pool_id + '/Tollfree/' + tollfree + '/'; + return super.customexecuteAction(path.toString().trim(), 'GET'); + } } /** * Represents a Powerpack interface @@ -412,90 +681,97 @@ export class Tollfree extends PlivoResource { * @param {object} [data] - data of call */ export class PowerpackInterface extends PlivoResourceInterface { - constructor(client, data = {}) { - super(action, Powerpack, idField, client); - extend(this, data); - } + constructor(client, data = {}) { + super(action, Powerpack, idField, client); + extend(this, data); + this[clientKey] = client; + } - /** - * get Powerpack by given id - * @method - * @param {string} uuid - id of Powerpack - * @promise {object} return {@link Powerpack} object - * @fail {Error} return Error - */ - get(uuid) { - return super.get(uuid); - } + /** + * get Powerpack by given id + * @method + * @param {string} uuid - id of Powerpack + * @promise {object} return {@link Powerpack} object + * @fail {Error} return Error + */ + get(uuid) { + return super.get(uuid); + } - /** - * create Powerpack - * @method - * @param {string} name - name of Powerpack - * @param {object} params - params to create Powerpack - * @param {string} [params.sticky_sender] - - * @param {string} [params.local_connect] - * @param {string} [params.application_type] - * @param {string} [params.application_id] - * @promise {object} return {@link PlivoGenericResponse} object - * @fail {Error} return Error - */ - create(name, params = {}) { - let errors = validate([ - { - field: 'name', - value: name, - validators: [ 'isRequired', 'isString' ] - } - ]); + /** + * create Powerpack + * @method + * @param {string} name - name of Powerpack + * @param {object} params - params to create Powerpack + * @param {string} [params.sticky_sender] - + * @param {string} [params.local_connect] + * @param {string} [params.application_type] + * @param {string} [params.application_id] + * @promise {object} return {@link PlivoGenericResponse} object + * @fail {Error} return Error + */ + create(name, params = {}) { + let errors = validate([{ + field: 'name', + value: name, + validators: ['isRequired', 'isString'] + }]); - if (errors) { - return errors; - } + if (errors) { + return errors; + } - params.name = name; + params.name = name; - return super.create(params); - } + let client = this[clientKey]; + return new Promise((resolve, reject) => { + client('POST', action, params) + .then(response => { + resolve(new CreatePowerpackResponse(response.body, idField)); + }) + .catch(error => { + reject(error); + }); + }); - /** - * update Powerpack - * @method - * @param {string} uuid - id of Powerpack - * @param {object} params - to update Powerpack - * @param {string} [params.name] - * @param {string} [params.sticky_sender] - * @param {string} [params.local_connect] - * @param {string} [params.application_type] - * @param {string} [params.application_id] - * @promise {object} return {@link Powerpack} object - * @fail {Error} return Error - */ - update(uuid, params) { - let errors = validate([ - { - field: 'uuid', - value: uuid, - validators: [ 'isRequired' ] - } - ]); + } - if (errors) { - return errors; - } - return new Powerpack(this[clientKey], { - uuid: uuid - }).update(params); - } + /** + * update Powerpack + * @method + * @param {string} uuid - id of Powerpack + * @param {object} params - to update Powerpack + * @param {string} [params.name] + * @param {string} [params.sticky_sender] + * @param {string} [params.local_connect] + * @param {string} [params.application_type] + * @param {string} [params.application_id] + * @promise {object} return {@link Powerpack} object + * @fail {Error} return Error + */ + update(uuid, params) { + let errors = validate([{ + field: 'uuid', + value: uuid, + validators: ['isRequired'] + }]); - /** - * Get All Call Detail - * @method - * @param {object} params - params to get all call details. - * @promise {object[]} returns list of Call Object - * @fail {Error} returns Error - */ - list(params) { - return super.list(params); - } -} + if (errors) { + return errors; + } + return new Powerpack(this[clientKey], { + uuid: uuid + }).update(params); + } + + /** + * Get All Call Detail + * @method + * @param {object} params - params to get all call details. + * @promise {object[]} returns list of Call Object + * @fail {Error} returns Error + */ + list(params) { + return super.list(params); + } +} \ No newline at end of file diff --git a/lib/resources/pricings.js b/lib/resources/pricings.js index 8bf13bc..3874e82 100644 --- a/lib/resources/pricings.js +++ b/lib/resources/pricings.js @@ -1,66 +1,102 @@ -import {extend, validate} from '../utils/common.js'; -import {PlivoResource, PlivoResourceInterface} from '../base'; +import { + PlivoResource, + PlivoResourceInterface +} from '../base'; +import { + extend, + validate +} from '../utils/common.js'; const clientKey = Symbol(); const action = 'Pricing/'; const idField = 'countryIso'; /** - * Represents a Pricing - * @constructor - * @param {function} client - make api call - * @param {object} [data] - data of call - */ +* Represents a Pricing +* @constructor +* @param {function} client - make api call +* @param {object} [data] - data of call +*/ + +export class PricingResponse { + constructor(params) { + params = params || {}; + this.apiId = params.apiId; + this.country = params.country; + this.countryCode = params.countryCode; + this.countryIso = params.countryIso; + this.message = params.message; + this.mms = params.mms; + this.phoneNumbers = params.phoneNumbers; + this.voice = params.voice; + + } +} export class Pricing extends PlivoResource { constructor(client, data = {}) { - super(action, Pricing, idField, client); - extend(this, data); + super(action, Pricing, idField, client); + extend(this, data); + this[clientKey] = client } -/** - * Get pricings by country - * @method - * @promise {object} return {@link PlivoGenericResponse} object - * @fail {Error} return Error - */ + + /** + * Get pricings by country + * @method + * @promise {object} return {@link PlivoGenericResponse} object + * @fail {Error} return Error + */ get() { - let params = { - country_iso: this.id - }; - return super.executeAction('', 'GET', params); + let params = { + country_iso: this.id + }; + let client = this[clientKey]; + + return new Promise((resolve, reject) => { + client('GET', action, params) + .then(response => { + resolve(new PricingResponse(response.body, idField)); + }) + .catch(error => { + reject(error); + }); + }); + } } /** - * Represents a Pricing Interface - * @constructor - * @param {function} client - make api call - * @param {object} [data] - data of call - */ +* Represents a Pricing Interface +* @constructor +* @param {function} client - make api call +* @param {object} [data] - data of call +*/ export class PricingInterface extends PlivoResourceInterface { constructor(client, data = {}) { - super(action, Pricing, idField, client); - extend(this, data); + super(action, Pricing, idField, client); + extend(this, data); - this[clientKey] = client; + this[clientKey] = client; } -/** - * Get pricings by country - * @method - * @param {string} countryISO - country iso to get pricings - * @promise {object} return {@link PlivoGenericResponse} object - * @fail {Error} return Error - */ + /** + * Get pricings by country + * @method + * @param {string} countryISO - country iso to get pricings + * @promise {object} return {@link PlivoGenericResponse} object + * @fail {Error} return Error + */ get(countryISO) { - let errors = validate([ - {field: 'country_iso', value: countryISO, validators: ['isRequired']} - ]); + let errors = validate([{ + field: 'country_iso', + value: countryISO, + validators: ['isRequired'] + }]); - if (errors) { - return errors; - } - return new Pricing(this[clientKey], { - id: countryISO - }).get(); + if (errors) { + return errors; + } + return new Pricing(this[clientKey], { + id: countryISO + }).get(); } -} +} \ No newline at end of file diff --git a/lib/resources/recordings.js b/lib/resources/recordings.js index d5299b4..81dc158 100644 --- a/lib/resources/recordings.js +++ b/lib/resources/recordings.js @@ -1,73 +1,141 @@ -import {extend, validate} from '../utils/common.js'; -import {PlivoResource, PlivoResourceInterface} from '../base'; +import { + PlivoResource, + PlivoResourceInterface +} from '../base'; +import { + extend, + validate +} from '../utils/common.js'; const clientKey = Symbol(); const action = 'Recording/'; const idField = 'recordingId'; + +export class RetrieveRecordingResponse { + constructor(params) { + params = params || {}; + this.addTime = params.addTime; + this.apiId = params.apiId; + this.callUuid = params.callUuid; + this.conferenceName = params.conferenceName; + this.recordingDurationMs = params.recordingDurationMs; + this.recordingEndMs = params.recordingEndMs; + this.recordingFormat = params.recordingFormat; + this.recordingId = params.recordingId; + this.recordingStartMs = params.recordingStartMs; + this.recordingType = params.recordingType; + this.recordingUrl = params.recordingUrl; + this.resourceUri = params.resourceUri; + } +} + +export class ListRecordingResponse { + constructor(params) { + params = params || {}; + this.addTime = params.addTime; + this.apiId = params.apiId; + this.callUuid = params.callUuid; + this.conferenceName = params.conferenceName; + this.recordingDurationMs = params.recordingDurationMs; + this.recordingEndMs = params.recordingEndMs; + this.recordingFormat = params.recordingFormat; + this.recordingId = params.recordingId; + this.recordingStartMs = params.recordingStartMs; + this.recordingType = params.recordingType; + this.recordingUrl = params.recordingUrl; + this.resourceUri = params.resourceUri; + } +} + /** - * Represents a Recording - * @constructor - * @param {function} client - make api call - * @param {object} [data] - data of call - */ +* Represents a Recording +* @constructor +* @param {function} client - make api call +* @param {object} [data] - data of call +*/ export class Recording extends PlivoResource { constructor(client, data = {}) { - super(action, Recording, idField, client); - if (idField in data) { - this.id = data[idField]; - } - extend(this, data); + super(action, Recording, idField, client); + this[clientKey] = client + + if (idField in data) { + this.id = data[idField]; + } + extend(this, data); } -/** - * Delete recording - * @method - * @promise {boolean} return true if success - * @fail {Error} return Error - */ - delete() { - let params = {}; - params.isVoiceRequest = 'true'; - return super.delete(params); + /** + * Delete recording + * @method + * @promise {boolean} return true if success + * @fail {Error} return Error + */ + delete(id) { + let client = this[clientKey]; + return new Promise((resolve, reject) => { + client('DELETE', action + id + '/') + .then(() => { + resolve(true); + }) + .catch(error => { + reject(error); + }); + }); } } /** - * Represents a Recording Interface - * @constructor - * @param {function} client - make api call - * @param {object} [data] - data of call - */ +* Represents a Recording Interface +* @constructor +* @param {function} client - make api call +* @param {object} [data] - data of call +*/ export class RecordingInterface extends PlivoResourceInterface { constructor(client, data = {}) { - super(action, Recording, idField, client); - extend(this, data); + super(action, Recording, idField, client); + extend(this, data); - this[clientKey] = client; + this[clientKey] = client; } -/** - * Get recording by id - * @method - * @param {string} id - id to get recording information - * @promise {object} return {@link Pricing} object - * @fail {Error} return Error - */ + /** + * Get recording by id + * @method + * @param {string} id - id to get recording information + * @promise {object} return {@link Pricing} object + * @fail {Error} return Error + */ get(id) { - let errors = validate([ - {field: 'id', value: id, validators: ['isRequired']} - ]); + let errors = validate([{ + field: 'id', + value: id, + validators: ['isRequired'] + }]); - if (errors) { - return errors; - } - let params = {}; - params.isVoiceRequest = 'true'; - return super.get(id, params); + if (errors) { + return errors; + } + let params = {}; + params.isVoiceRequest = 'true'; + + let client = this[clientKey]; + + return new Promise((resolve, reject) => { + if (action !== '' && !id) { + reject(new Error(this[idKey] + ' must be set')); + } + client('GET', action + (id ? id + '/' : ''), params) + .then(response => { + resolve(new RetrieveRecordingResponse(response.body, client)); + }) + .catch(error => { + reject(error); + }); + }); } /** @@ -80,28 +148,47 @@ export class RecordingInterface extends PlivoResourceInterface { * @param {string} [params.limit] - Display no of results per page * @param {string} [params.offset] - No of value items by which results should be offset */ - list(params= {}) { - params.isVoiceRequest = 'true'; - return super.list(params); + list(params = {}) { + params.isVoiceRequest = 'true'; + let client = this[clientKey]; + return new Promise((resolve, reject) => { + client('GET', action, params) + .then(response => { + let objects = []; + Object.defineProperty(objects, 'meta', { + value: response.body.meta, + enumerable: true + }); + response.body.objects.forEach(item => { + objects.push(new ListRecordingResponse(item, client)); + }); + resolve(objects); + }) + .catch(error => { + reject(error); + }); + }); } -/** - * Delete recording by id - * @method - * @param {string} id - id to delete recording - * @promise {boolean} return true if success - * @fail {Error} return Error - */ + /** + * Delete recording by id + * @method + * @param {string} id - id to delete recording + * @promise {boolean} return true if success + * @fail {Error} return Error + */ delete(id) { - let errors = validate([ - {field: 'id', value: id, validators: ['isRequired']} - ]); + let errors = validate([{ + field: 'id', + value: id, + validators: ['isRequired'] + }]); - if (errors) { - return errors; - } - return new Recording(this[clientKey], { - id: id - }).delete(); + if (errors) { + return errors; + } + return new Recording(this[clientKey], { + id: id + }).delete(id); } -} +} \ No newline at end of file diff --git a/lib/utils/jwt.js b/lib/utils/jwt.js index 1dd6070..5ce78d6 100644 --- a/lib/utils/jwt.js +++ b/lib/utils/jwt.js @@ -38,8 +38,8 @@ export function AccessToken(authId, authToken, username, validityOptions = {}, u this.uid = uid || this.username + "-" + (new Date()).getTime(); } -AccessToken.prototype = { - addVoiceGrants: function(incoming = false, outgoing = false) { + +AccessToken.prototype.addVoiceGrants= function(incoming = false, outgoing = false) { this.grants = { voice: { incoming_allow: incoming, @@ -47,7 +47,8 @@ AccessToken.prototype = { } }; - }, + } + AccessToken.prototype = { toJwt: function() { let payload = { jti: this.uid, diff --git a/lib/utils/plivoxml.js b/lib/utils/plivoxml.js index 79ec786..002d668 100644 --- a/lib/utils/plivoxml.js +++ b/lib/utils/plivoxml.js @@ -2,6 +2,7 @@ var qs = require('querystring'); var xmlBuilder = require('xmlbuilder'); var util = require('util'); var plivoUtils = require('./../rest/utils'); + import * as Exceptions from './exceptions'; var jsonStringifier = require('./jsonStrinfigier'); diff --git a/package.json b/package.json index 0a3bee6..a3ef871 100644 --- a/package.json +++ b/package.json @@ -58,6 +58,7 @@ "test": "gulp" }, "dependencies": { + "@types/node": "^14.14.14", "base-64": "^0.1.0", "build-url": "^1.0.10", "jsonwebtoken": "^8.5.1", diff --git a/types/resources/accounts.d.ts b/types/resources/accounts.d.ts index f109892..a907f4e 100644 --- a/types/resources/accounts.d.ts +++ b/types/resources/accounts.d.ts @@ -4,9 +4,70 @@ * @param {function} client - make api call * @param {object} [data] - data of call */ +export class GetAccountDetails { + constructor(params: object); + accountType: string; + address: string; + apiId: string; + autoRecharge: string; + billingMode: string; + cashCredits: string; + city: string; + name: string; + resourceUri: string; + state: string; + timezone: string; +} +export class CreateSubAccountResponse { + constructor(params: object); + apiId: string; + authId: string; + authToken: string; + message: string; +} +export class UpdateSubAccountDetails { + constructor(params: object); + apiId: stringy; + message: string; +} +export class UpdateAccountDetailsResponse { + constructor(params: object); + apiId: string; + message: string; +} +export class GetSubAccountDetails { + constructor(params: object); + account: string; + apiId: string; + authId: string; + authToken: string; + created: string; + enabled: string; + modified: string; + name: string; + resourceUri: string; +} export class Subaccount extends PlivoResource { constructor(client: any, data?: {}); id: any; + /** + * update subaccount + * @method + * @param {string} name - name of subaccount + * @param {boolean} enabled - make account enable or disable + * @promise {Subaccount} return object of subaccount + * @fail {Error} return Error + */ + update(name: string, enabled: boolean): Promise; + /** + * delete subaccount + * @method + * @param {boolean} cascade - delete associated applications, phonenumbers & endpoints + * @promise {boolean} return true if subaccount deleted + * @fail {Error} return Error + */ + delete(cascade: any): Promise; + [clientKey]: any; } /** * Represents a Subaccount Interface @@ -16,6 +77,23 @@ export class Subaccount extends PlivoResource { */ export class SubaccountInterface extends PlivoResourceInterface { constructor(client: any, data?: {}); + /** + * get subaccount by id + * @method + * @param {string} id - id of subaccount + * @promise {Subaccount} return object of subaccount + * @fail {Error} return Error + */ + get(id: string): Promise; + /** + * create subaccount + * @method + * @param {string} name - name of subaccount + * @param {boolean} enabled - enable or disable subaccount + * @promise {PlivoGenericResponse} return object of PlivoGenericObject + * @fail {Error} return Error + */ + create(name: string, enabled: boolean): Promise; /** * update subaccount * @method @@ -53,6 +131,7 @@ export class Account extends PlivoResource { * @fail {Error} return Error */ get(): Promise; + update(params: any): Promise; [clientKey]: any; } /** @@ -63,6 +142,13 @@ export class Account extends PlivoResource { */ export class AccountInterface extends PlivoResourceInterface { constructor(client: any, data?: {}); + /** + * get account detail + * @method + * @promise {PlivoGenericResponse} return PlivoGenericResponse object + * @fail {Error} return Error + */ + get(): Promise; /** * update account detail * @method @@ -80,7 +166,7 @@ export class AccountInterface extends PlivoResourceInterface { }): Promise; [clientKey]: any; } -import { PlivoResource } from "../base.js"; -import { PlivoResourceInterface } from "../base.js"; +import { PlivoResource } from "../base"; declare const clientKey: unique symbol; +import { PlivoResourceInterface } from "../base"; export {}; diff --git a/types/resources/applications.d.ts b/types/resources/applications.d.ts index 5afe15c..26245a8 100644 --- a/types/resources/applications.d.ts +++ b/types/resources/applications.d.ts @@ -1,21 +1,112 @@ /** - * Represents a Application - * @constructor - * @param {function} client - make api call - * @param {object} [data] - data of call - */ +* Represents a Application +* @constructor +* @param {function} client - make api call +* @param {object} [data] - data of call +*/ +export class UpdateApplicationResponse { + constructor(params: object); + apiId: string; + message: string; +} +export class CreateApplicationResponse { + constructor(params: object); + apiId: string; + appId: string; + message: string; +} +export class RetrieveApplicationResponse { + constructor(params: object); + answerMethod: string; + answerUrl: string; + apiId: string; + appId: string; + appName: string; + applicationType: string; + defaultApp: string; + defaultEndpointApp: string; + enabled: string; + fallbackAnswerUrl: string; + fallbackMethod: string; + hangupMethod: string; + logIncomingMessage: string; + messageMethod: string; + resourceUri: string; + sipUri: string; + subAccount: string; +} +export class ListAllApplicationResponse { + constructor(params: object); + answerMethod: string; + answerUrl: string; + appId: string; + appName: string; + applicationType: string; + defaultApp: string; + defaultEndpointApp: string; + enabled: string; + fallbackAnswerUrl: string; + fallbackMethod: string; + hangupMethod: string; + logIncomingMessage: string; + messageMethod: string; + resourceUri: string; + sipUri: string; + subAccount: string; +} export class Application extends PlivoResource { constructor(client: any, data?: {}); id: any; + [clientKey]: any; } /** - * Represents a Application interface - * @constructor - * @param {function} client - make api call - * @param {object} [data] - data of call - */ +* Represents a Application interface +* @constructor +* @param {function} client - make api call +* @param {object} [data] - data of call +*/ export class ApplicationInterface extends PlivoResourceInterface { constructor(client: any, data?: {}); + /** + * get application by given id + * @method + * @param {string} id - id of application + * @promise {object} return {@link Application} object + * @fail {Error} return Error + */ + get(id: string): Promise; + /** + * list applications + * @method + * @param {object} params - params to list applications + * @param {string} [params.subaccount] - ID of the subaccount if present + * @param {integer} [params.limit] - To display no of results per page + * @param {integer} [params.offset] - No of value items by which results should be offset + */ + list(params?: {}): Promise; + /** + * create Application + * @method + * @param {string} appName - name of application + * @param {object} params - params to create application + * @param {string} [params.answerUrl] - answer url + * @param {string} [params.appName] The name of your application + * @param {string} [params.answerUrl] The URL invoked by Plivo when a call executes this application. + * @param {string} [params.answerMethod] The method used to call the answer_url. Defaults to POST. + * @param {string} [params.hangupUrl] The URL that is notified by Plivo when the call hangs up. + * @param {string} [params.hangupMethod] The method used to call the hangup_url. Defaults to POST + * @param {string} [params.fallbackAnswerUrl] Invoked by Plivo only if answer_url is unavailable or the XML response is invalid. Should contain a XML response. + * @param {string} [params.fallbackMethod] The method used to call the fallback_answer_url. Defaults to POST. + * @param {string} [params.messageUrl] The URL that is notified by Plivo when an inbound message is received. Defaults not set. + * @param {string} [params.messageMethod] The method used to call the message_url. Defaults to POST. + * @param {boolean} [params.defaultNumberApp] If set to true, associates all newly created Plivo numbers that have not specified an app_id, to this application. + * @param {boolean} [params.defaultEndpointApp] If set to true, associates all newly created Plivo endpoints that have not specified an app_id, to this application. + * @param {string} [params.subaccount] Id of the subaccount, in case only subaccount applications are needed. + * @param {boolean} [params.logIncomingMessages] flag to control incoming message logs. + * @promise {object} return {@link PlivoGenericResponse} object + * @fail {Error} return Error + */ + create(appName: string, params?: {}): Promise; /** * update Application * @method @@ -66,7 +157,7 @@ export class ApplicationInterface extends PlivoResourceInterface { }): Promise; [clientKey]: any; } -import { PlivoResource } from "../base.js"; -import { PlivoResourceInterface } from "../base.js"; +import { PlivoResource } from "../base"; declare const clientKey: unique symbol; +import { PlivoResourceInterface } from "../base"; export {}; diff --git a/types/resources/call.d.ts b/types/resources/call.d.ts index e7efab2..2a9d67d 100644 --- a/types/resources/call.d.ts +++ b/types/resources/call.d.ts @@ -1,3 +1,116 @@ +export class CallTransferResponse { + constructor(params: object); + apiId: string; + callUuids: object; + message: string; +} +export class ListAllQueuedCalls { + constructor(params: object); + apiId: string; + calls: object; +} +export class ListAllLiveCallResponse { + constructor(params: object); + apiId: string; + callUuid: object; +} +export class CreateCallResponse { + constructor(params: object); + apiId: string; + message: string; + requestUuid: string; +} +export class GetQueuedCallResponse { + constructor(params: object); + apiId: string; + direction: string; + from: string; + callStatus: string; + to: string; + callerName: string; + callUuid: string; + requestUuid: string; +} +export class GetLiveCallResponse { + constructor(params: any); + apiId: string; + callStatus: string; + callUuid: string; + callerName: string; + direction: string; + from: string; + requestUuid: string; + sessionStart: string; + to: string; +} +export class RetrieveCallResponse { + constructor(params: object); + apiId: string; + answerTime: string; + billDuration: string; + billedDuration: string; + callDirection: string; + callDuration: string; + callState: string; + callUuid: string; + conferenceUuid: string; + endTime: string; + fromNumber: string; + hangupCauseCode: string; + hangupCauseName: string; + hangupSource: string; + initiationTime: string; + parentCallUuid: string; + resourceUri: string; + toNumber: string; + totalAmount: string; + totalRate: string; +} +export class ListAllCallsResponse { + constructor(params: object); + apiId: string; + answerTime: string; + billDuration: string; + billedDuration: string; + callDirection: string; + callDuration: string; + callState: string; + callUuid: string; + conferenceUuid: string; + endTime: string; + fromNumber: string; + hangupCauseCode: string; + hangupCauseName: string; + hangupSource: string; + initiationTime: string; + parentCallUuid: string; + resourceUri: string; + toNumber: string; + totalAmount: string; + totalRate: string; +} +export class StartPlayingMusicResponse { + constructor(params: object); + apiId: string; + message: string; +} +export class StartSpeakingTextResponse { + constructor(params: object); + apiId: string; + message: string; +} +export class SendDigitsResponse { + constructor(params: object); + apiId: string; + message: string; +} +export class RecordCallResponse { + constructor(params: object); + apiId: string; + message: string; + recordingId: string; + url: string; +} /** * Represents a Call * @constructor @@ -5,125 +118,125 @@ * @param {object} [data] - data of call */ export class Call extends PlivoResource { - constructor(client: any, data?: {}); - id: any; - /** - * hangup call - * @method - * @promise {Boolean} return true if call hung up - * @fail {Error} return Error - */ - hangup(): Promise; - /** - * transfer call - * @method - * @param {object} params - optional params to transfer a call - * @param {string} [params.legs] aleg, bleg or both Defaults to aleg. aleg will transfer call_uuid ; bleg will transfer the bridged leg (if found) of call_uuid ; both will transfer call_uuid and bridged leg of call_uuid - * @param {string} [params.alegUrl] URL to transfer for aleg, if legs is aleg or both, then aleg_url has to be specified. - * @param {string} [params.alegMethod] HTTP method to invoke aleg_url. Defaults to POST. - * @param {string} [params.blegUrl] URL to transfer for bridged leg, if legs is bleg or both, then bleg_url has to be specified. - * @param {string} [params.blegMethod] HTTP method to invoke bleg_url. Defaults to POST. - * @promise {object} return call object - * @fail {Error} return Error - */ - transfer(params: { - legs: string; - alegUrl: string; - alegMethod: string; - blegUrl: string; - blegMethod: string; - }): Promise; - /** - * record call - * @method - * @param {object} params - to record call - * @promise {object} return PlivoGenericResponse Object - * @fail {Error} return Error - */ - record(params: object): Promise; - /** - * record call - * @method - * @param {object} params - to record call - * @promise {object} return PlivoGenericResponse Object - * @fail {Error} return Error - */ - startRecording(params: object): Promise; - /** - * stop recording call - * @method - * @param {object} params - to stop recording call - * @promise {object} return PlivoGenericResponse Object - * @fail {Error} return Error - */ - stopRecording(params: object): Promise; - /** - * play music for call - * @method - * @param {string} url - url which contains audio to play for call - * @param {object} optionalParams - to stop recording call - * @promise {object} returns PlivoGenericResponse Object - * @fail {Error} returns Error - */ - playMusic(url: string, optionalParams: object): Promise; - /** - * play music for call - * @method - * @param {string} url - url which contains audio to play for call - * @param {object} optionalParams - to stop recording call - * @promise {object} returns PlivoGenericResponse Object - * @fail {Error} returns Error - */ - startPlayingMusic(urls: any, optionalParams: object): Promise; - /** - * stop playing music for call - * @method - * @promise {object} returns PlivoGenericResponse Object - * @fail {Error} returns Error - */ - stopPlayingMusic(): Promise; - /** - * speak text for call - * @method - * @param {string} text - text to speak for call - * @param {object} optionalParams - to speak for call - * @promise {object} returns PlivoGenericResponse Object - * @fail {Error} returns Error - */ - speakText(text: string, optionalParams: object): Promise; - /** - * speak text for call - * @method - * @param {string} text - text to speak for call - * @param {object} optionalParams - to speak for call - * @promise {object} returns PlivoGenericResponse Object - * @fail {Error} returns Error - */ - startSpeakingText(text: string, optionalParams: object): Promise; - /** - * stop speaking text for call - * @method - * @promise {object} returns PlivoGenericResponse Object - * @fail {Error} returns Error - */ - stopSpeakingText(): Promise; - /** - * Send digits on a call - * @method - * @param {number} digits - digits to be send - * @param {object} optionalParams - to send digits for call - * @promise {object} returns PlivoGenericResponse Object - * @fail {Error} returns Error - */ - sendDigits(digits: number, optionalParams: object): Promise; - /** - * Hangup a Call Request - * @method - * @promise {object} returns PlivoGenericResponse Object - * @fail {Error} returns Error - */ - cancel(): Promise; - [clientKey]: any; + constructor(client: any, data ? : {}); + id: any; + /** + * hangup call + * @method + * @promise {Boolean} return true if call hung up + * @fail {Error} return Error + */ + hangup(): Promise < any > ; + /** + * transfer call + * @method + * @param {object} params - optional params to transfer a call + * @param {string} [params.legs] aleg, bleg or both Defaults to aleg. aleg will transfer call_uuid ; bleg will transfer the bridged leg (if found) of call_uuid ; both will transfer call_uuid and bridged leg of call_uuid + * @param {string} [params.alegUrl] URL to transfer for aleg, if legs is aleg or both, then aleg_url has to be specified. + * @param {string} [params.alegMethod] HTTP method to invoke aleg_url. Defaults to POST. + * @param {string} [params.blegUrl] URL to transfer for bridged leg, if legs is bleg or both, then bleg_url has to be specified. + * @param {string} [params.blegMethod] HTTP method to invoke bleg_url. Defaults to POST. + * @promise {object} return call object + * @fail {Error} return Error + */ + transfer(params: { + legs: string; + alegUrl: string; + alegMethod: string; + blegUrl: string; + blegMethod: string; + }, callUUID: any): Promise < CallTransferResponse > ; + /** + * record call + * @method + * @param {object} params - to record call + * @promise {object} return PlivoGenericResponse Object + * @fail {Error} return Error + */ + record(params: object): Promise < any > ; + /** + * record call + * @method + * @param {object} params - to record call + * @promise {object} return PlivoGenericResponse Object + * @fail {Error} return Error + */ + startRecording(params: object): Promise < RecordCallResponse > ; + /** + * stop recording call + * @method + * @param {object} params - to stop recording call + * @promise {object} return PlivoGenericResponse Object + * @fail {Error} return Error + */ + stopRecording(params: object): Promise < any > ; + /** + * play music for call + * @method + * @param {string} url - url which contains audio to play for call + * @param {object} optionalParams - to stop recording call + * @promise {object} returns PlivoGenericResponse Object + * @fail {Error} returns Error + */ + playMusic(url: string, optionalParams: object): Promise < any > ; + /** + * play music for call + * @method + * @param {string} url - url which contains audio to play for call + * @param {object} optionalParams - to stop recording call + * @promise {object} returns PlivoGenericResponse Object + * @fail {Error} returns Error + */ + startPlayingMusic(urls: any, optionalParams: object): Promise < StartPlayingMusicResponse > ; + /** + * stop playing music for call + * @method + * @promise {object} returns PlivoGenericResponse Object + * @fail {Error} returns Error + */ + stopPlayingMusic(): Promise < any > ; + /** + * speak text for call + * @method + * @param {string} text - text to speak for call + * @param {object} optionalParams - to speak for call + * @promise {object} returns PlivoGenericResponse Object + * @fail {Error} returns Error + */ + speakText(text: string, optionalParams: object): Promise < any > ; + /** + * speak text for call + * @method + * @param {string} text - text to speak for call + * @param {object} optionalParams - to speak for call + * @promise {object} returns PlivoGenericResponse Object + * @fail {Error} returns Error + */ + startSpeakingText(text: string, optionalParams: object): Promise < StartSpeakingTextResponse > ; + /** + * stop speaking text for call + * @method + * @promise {object} returns PlivoGenericResponse Object + * @fail {Error} returns Error + */ + stopSpeakingText(): Promise < any > ; + /** + * Send digits on a call + * @method + * @param {number} digits - digits to be send + * @param {object} optionalParams - to send digits for call + * @promise {object} returns PlivoGenericResponse Object + * @fail {Error} returns Error + */ + sendDigits(digits: number, optionalParams: object): Promise < SendDigitsResponse > ; + /** + * Hangup a Call Request + * @method + * @promise {object} returns PlivoGenericResponse Object + * @fail {Error} returns Error + */ + cancel(): Promise < any > ; + [clientKey]: any; } /** * Represents a Call Interface @@ -132,129 +245,179 @@ export class Call extends PlivoResource { * @param {object} [data] - data of call */ export class CallInterface extends PlivoResourceInterface { - constructor(client: any, data?: {}); - /** - * Hangup A Specific Call - * @method - * @param {string} callUUID - call uuid to hangup call - * @promise {object} returns PlivoGenericResponse Object - * @fail {Error} returns Error - */ - hangup(callUUID: string): Promise; - /** - * Transfer a Call - * @method - * @param {string} callUUID - call uuid to transfer call - * @param {object} params - optional params to transfer a call - * @param {string} [params.legs] aleg, bleg or both Defaults to aleg. aleg will transfer call_uuid ; bleg will transfer the bridged leg (if found) of call_uuid ; both will transfer call_uuid and bridged leg of call_uuid - * @param {string} [params.alegUrl] URL to transfer for aleg, if legs is aleg or both, then aleg_url has to be specified. - * @param {string} [params.alegMethod] HTTP method to invoke aleg_url. Defaults to POST. - * @param {string} [params.blegUrl] URL to transfer for bridged leg, if legs is bleg or both, then bleg_url has to be specified. - * @param {string} [params.blegMethod] HTTP method to invoke bleg_url. Defaults to POST. - * @promise {object} returns PlivoGenericResponse Object - * @fail {Error} returns Error - */ - transfer(callUUID: string, params: { - legs: string; - alegUrl: string; - alegMethod: string; - blegUrl: string; - blegMethod: string; - }): Promise; - /** - * Record a Call - * @method - * @param {string} callUUID - call uuid to record call - * @param {object} optionalParams - optional params to record a call - * @promise {object} returns PlivoGenericResponse Object - * @fail {Error} returns Error - */ - record(callUUID: string, optionalParams: object): Promise; - /** - * Stop Recording a Call - * @method - * @param {string} callUUID - call uuid to stop recording a call - * @param {object} optionalParams - optional params to stop recording a call - * @promise {object} returns PlivoGenericResponse Object - * @fail {Error} returns Error - */ - stopRecording(callUUID: string, optionalParams: object): Promise; - /** - * Play a music file - * @method - * @param {string} callUUID - call uuid to play music file - * @param {string} url - A single URL or a list of comma separated URLs linking to an mp3 or wav file. - * @param {object} optionalParams - optional params to play music file. - * @promise {object} returns PlivoGenericResponse Object - * @fail {Error} returns Error - */ - playMusic(callUUID: string, urls: any, optionalParams: object): Promise; - /** - * Stop Playing a music file - * @method - * @param {string} callUUID - call uuid to stop plaing music file - * @promise {object} returns PlivoGenericResponse Object - * @fail {Error} returns Error - */ - stopPlayingMusic(callUUID: string): Promise; - /** - * Speak text during a call - * @method - * @param {string} callUUID - call uuid to speak text during a call - * @param {string} text - text to be played. - * @param {object} optionalParams - optional params to speak text during a call - * @promise {object} returns PlivoGenericResponse Object - * @fail {Error} returns Error - */ - speakText(callUUID: string, text: string, optionalParams: object): Promise; - /** - * Stop Speaking text during a call - * @method - * @param {string} callUUID - call uuid to stop speaking text during a call - * @param {object} optionalParams - optional params to stop speaking text during a call - * @promise {object} returns PlivoGenericResponse Object - * @fail {Error} returns Error - */ - stopSpeakingText(callUUID: string): Promise; - /** - * Send digits on a call - * @method - * @param {string} callUUID - call uuid to send digits on a call - * @param {number} digits - digits to be send - * @param {object} optionalParams - optional params to send digits - * @promise {object} returns PlivoGenericResponse Object - * @fail {Error} returns Error - */ - sendDigits(callUUID: string, digits: number, optionalParams: object): Promise; - /** - * Hangup a call request - * @method - * @param {string} callUUID - call uuid to send digits on a call - * @promise {object} returns PlivoGenericResponse Object - * @fail {Error} returns Error - */ - cancel(id: any): Promise; - listLiveCalls(params: any): Promise; - getLiveCall(id: any): Promise; - listQueuedCalls(): Promise; - getQueuedCall(id: any): Promise; - [clientKey]: any; - [liveCallInterfaceKey]: LiveCallInterface; - [queuedCallInterfaceKey]: QueuedCallInterface; + constructor(client: any, data ? : {}); + /** + * Get A Call Detail + * @method + * @param {string} id - call uuid to get information of. + * @promise {object} returns Call Object + * @fail {Error} returns Error + */ + get(id: string): Promise < RetrieveCallResponse > ; + /** + * Get All Call Detail + * @method + * @param {object} params - params to get all call details. + * @promise {object[]} returns list of Call Object + * @fail {Error} returns Error + */ + list(params: object): Promise < ListAllCallsResponse > ; + /** + * Create a call + * @method + * @param {string} from - The phone number to be used as the caller id (with the country code).For e.g, a USA caller id number could be, 15677654321, with '1' for the country code. + * @param {string} to - The regular number(s) or sip endpoint(s) to call. Regular number must be prefixed with country code but without the + sign). For e.g, to dial a number in the USA, the number could be, 15677654321, with '1' for the country code. Multiple numbers can be sent by using a delimiter. For e.g. 15677654321<12077657621<12047657621. Sip endpoints must be prefixed with sip: E.g., sip:john1234@phone.plivo.com. To make bulk calls, the delimiter < is used. For example, 15677654321<15673464321 0(in seconds). + * @param {number} [params.hangupOnRing] Schedules the call for hangup at a specified time after the call starts ringing. Value should be an integer >= 0 (in seconds). + * @param {string} [params.machineDetection] Used to detect if the call has been answered by a machine. The valid values are true and hangup. + * @param {number} [params.machineDetectionTime] Time allotted to analyze if the call has been answered by a machine. It should be an integer >= 2000 and <= 10000 and the unit is ms. The default value is 5000 ms. + * @param {string} [params.machineDetectionUrl] A URL where machine detection parameters will be sent by Plivo. This parameter should be used to make machine detection asynchronous + * @param {string} [params.machineDetectionMethod] The HTTP method which will be used by Plivo to request the machine_detection_url. Defaults to POST. + * @param {string} [params.sipHeaders] List of SIP headers in the form of 'key=value' pairs, separated by commas. + * @param {number} [params.ringTimeout] Determines the time in seconds the call should ring. If the call is not answered within the ring_timeout value or the default value of 120s, it is canceled. + * @param {string} [params.parentCallUuid] The call_uuid of the first leg in an ongoing conference call. It is recommended to use this parameter in scenarios where a member who is already present in the conference intends to add new members by initiating outbound API calls. + * @param {boolean} [params.errorIfParentNotFound] if set to true and the parent_call_uuid cannot be found, the API request would return an error. If set to false, the outbound call API request will be executed even if the parent_call_uuid is not found. Defaults to false. + * @promise {object} returns PlivoGenericResponse Object + * @fail {Error} returns Error + */ + create(from: string, to: string, answerUrl: string, params ? : {}): Promise < CreateCallResponse > ; + /** + * Hangup A Specific Call + * @method + * @param {string} callUUID - call uuid to hangup call + * @promise {object} returns PlivoGenericResponse Object + * @fail {Error} returns Error + */ + hangup(callUUID: string): Promise < any > ; + /** + * Transfer a Call + * @method + * @param {string} callUUID - call uuid to transfer call + * @param {object} params - optional params to transfer a call + * @param {string} [params.legs] aleg, bleg or both Defaults to aleg. aleg will transfer call_uuid ; bleg will transfer the bridged leg (if found) of call_uuid ; both will transfer call_uuid and bridged leg of call_uuid + * @param {string} [params.alegUrl] URL to transfer for aleg, if legs is aleg or both, then aleg_url has to be specified. + * @param {string} [params.alegMethod] HTTP method to invoke aleg_url. Defaults to POST. + * @param {string} [params.blegUrl] URL to transfer for bridged leg, if legs is bleg or both, then bleg_url has to be specified. + * @param {string} [params.blegMethod] HTTP method to invoke bleg_url. Defaults to POST. + * @promise {object} returns PlivoGenericResponse Object + * @fail {Error} returns Error + */ + transfer(callUUID: string, params: { + legs: string; + alegUrl: string; + alegMethod: string; + blegUrl: string; + blegMethod: string; + }): Promise < any > ; + /** + * Record a Call + * @method + * @param {string} callUUID - call uuid to record call + * @param {object} optionalParams - optional params to record a call + * @promise {object} returns PlivoGenericResponse Object + * @fail {Error} returns Error + */ + record(callUUID: string, optionalParams: object): Promise < any > ; + /** + * Stop Recording a Call + * @method + * @param {string} callUUID - call uuid to stop recording a call + * @param {object} optionalParams - optional params to stop recording a call + * @promise {object} returns PlivoGenericResponse Object + * @fail {Error} returns Error + */ + stopRecording(callUUID: string, optionalParams: object): Promise < any > ; + /** + * Play a music file + * @method + * @param {string} callUUID - call uuid to play music file + * @param {string} url - A single URL or a list of comma separated URLs linking to an mp3 or wav file. + * @param {object} optionalParams - optional params to play music file. + * @promise {object} returns PlivoGenericResponse Object + * @fail {Error} returns Error + */ + playMusic(callUUID: string, urls: any, optionalParams: object): Promise < any > ; + /** + * Stop Playing a music file + * @method + * @param {string} callUUID - call uuid to stop plaing music file + * @promise {object} returns PlivoGenericResponse Object + * @fail {Error} returns Error + */ + stopPlayingMusic(callUUID: string): Promise < any > ; + /** + * Speak text during a call + * @method + * @param {string} callUUID - call uuid to speak text during a call + * @param {string} text - text to be played. + * @param {object} optionalParams - optional params to speak text during a call + * @promise {object} returns PlivoGenericResponse Object + * @fail {Error} returns Error + */ + speakText(callUUID: string, text: string, optionalParams: object): Promise < any > ; + /** + * Stop Speaking text during a call + * @method + * @param {string} callUUID - call uuid to stop speaking text during a call + * @param {object} optionalParams - optional params to stop speaking text during a call + * @promise {object} returns PlivoGenericResponse Object + * @fail {Error} returns Error + */ + stopSpeakingText(callUUID: string): Promise < any > ; + /** + * Send digits on a call + * @method + * @param {string} callUUID - call uuid to send digits on a call + * @param {number} digits - digits to be send + * @param {object} optionalParams - optional params to send digits + * @promise {object} returns PlivoGenericResponse Object + * @fail {Error} returns Error + */ + sendDigits(callUUID: string, digits: number, optionalParams: object): Promise < any > ; + /** + * Hangup a call request + * @method + * @param {string} callUUID - call uuid to send digits on a call + * @promise {object} returns PlivoGenericResponse Object + * @fail {Error} returns Error + */ + cancel(id: any): Promise < any > ; + listLiveCalls(params: any): Promise < any > ; + getLiveCall(id: any): Promise < any > ; + listQueuedCalls(): Promise < any > ; + getQueuedCall(id: any): Promise < any > ; + [clientKey]: any; + [liveCallInterfaceKey]: LiveCallInterface; + [queuedCallInterfaceKey]: QueuedCallInterface; } export class LiveCallResource extends PlivoResource { - constructor(client: any, data?: {}); - id: any; - [clientKey]: any; + constructor(client: any, data ? : {}); + id: any; + [clientKey]: any; } export class QueuedCallResource extends PlivoResource { - constructor(client: any, data?: {}); - id: any; - [clientKey]: any; + constructor(client: any, data ? : {}); + id: any; + [clientKey]: any; } -import { PlivoResource } from "../base.js"; +import { + PlivoResource +} from "../base"; declare const clientKey: unique symbol; -import { PlivoResourceInterface } from "../base.js"; +import { + PlivoResourceInterface +} from "../base"; declare const liveCallInterfaceKey: unique symbol; /** * Represents a LiveCall interface @@ -263,8 +426,8 @@ declare const liveCallInterfaceKey: unique symbol; * @param {object} [data] - data of call */ declare class LiveCallInterface extends PlivoResourceInterface { - constructor(client: any, data?: {}); - [clientKey]: any; + constructor(client: any, data ? : {}); + [clientKey]: any; } declare const queuedCallInterfaceKey: unique symbol; /** @@ -273,8 +436,11 @@ declare const queuedCallInterfaceKey: unique symbol; * @param {function} client - make api call * @param {object} [data] - data of call */ + declare class QueuedCallInterface extends PlivoResourceInterface { - constructor(client: any, data?: {}); - [clientKey]: any; + constructor(client: any, data ? : {}); + get(id: any): Promise < GetQueuedCallResponse > ; + list(): Promise < ListAllQueuedCalls > ; + [clientKey]: any; } -export {}; +export {}; \ No newline at end of file diff --git a/types/resources/callFeedback.d.ts b/types/resources/callFeedback.d.ts index a11b890..e2635d7 100644 --- a/types/resources/callFeedback.d.ts +++ b/types/resources/callFeedback.d.ts @@ -1,3 +1,9 @@ +export class CallFeedbackResponse { + constructor(params: object); + apiId: string; + message: string; + status: string; +} export class CallFeedback extends PlivoResource { constructor(client: any, data?: {}); id: any; @@ -11,9 +17,10 @@ export class CallFeedback extends PlivoResource { */ export class CallFeedbackInterface extends PlivoResourceInterface { constructor(client: any, data?: {}); + create(callUUID: string, rating: string, issues?: never[], notes?: string): Promise; [clientKey]: any; } -import { PlivoResource } from "../base.js"; +import { PlivoResource } from "../base"; declare const clientKey: unique symbol; -import { PlivoResourceInterface } from "../base.js"; +import { PlivoResourceInterface } from "../base"; export {}; diff --git a/types/resources/conferences.d.ts b/types/resources/conferences.d.ts index afcdde0..1eb9f1d 100644 --- a/types/resources/conferences.d.ts +++ b/types/resources/conferences.d.ts @@ -1,9 +1,53 @@ /** - * Represents a Conference - * @constructor - * @param {function} client - make api call - * @param {object} [data] - data of call - */ +* Represents a Conference +* @constructor +* @param {function} client - make api call +* @param {object} [data] - data of call +*/ +export class MuteMemberResponse { + constructor(params: object); + apiId: string; + memberId: string; + message: string; +} +export class StartRecordingConferenceResponse { + constructor(params: object); + apiId: string; + message: string; + recordingId: string; + url: string; +} +export class RetrieveConferenceResponse { + constructor(params: object); + apiId: string; + conferenceMemberCount: string; + conferenceName: string; + conferenceRunTime: string; + members: string; +} +export class ListAllConferenceResponse { + constructor(params: object); + apiId: string; + conferences: string; +} +export class SpeakMemberResponse { + constructor(params: object); + apiId: string; + memberId: string; + message: string; +} +export class PlayAudioMemberResponse { + constructor(params: object); + apiId: string; + memberId: string; + message: string; +} +export class DeafMemberResponse { + constructor(params: string); + apiId: string; + memberId: string; + message: string; +} export class Conference extends PlivoResource { constructor(client: any, data?: {}); id: any; @@ -37,7 +81,7 @@ export class Conference extends PlivoResource { * @promise {PlivoGenericResponse} return PlivoGenericResponse if success * @fail {Error} return Error */ - muteMember(memberId: string): Promise; + muteMember(memberId: string): Promise; /** * unmute member from conference * @method @@ -53,7 +97,7 @@ export class Conference extends PlivoResource { * @promise {PlivoGenericResponse} return PlivoGenericResponse if success * @fail {Error} return Error */ - deafMember(memberId: string): Promise; + deafMember(memberId: string): Promise; /** * undeaf member from conference * @method @@ -70,7 +114,7 @@ export class Conference extends PlivoResource { * @promise {PlivoGenericResponse} return PlivoGenericResponse if success * @fail {Error} return Error */ - playAudioToMember(memberId: string, url: string): Promise; + playAudioToMember(memberId: string, url: string): Promise; /** * stop playing audio to member * @method @@ -93,7 +137,7 @@ export class Conference extends PlivoResource { speakTextToMember(memberId: string, text: string, optionalParams: { voice: string; language: string; - }): Promise; + }): Promise; /** * stop speaking text to member * @method @@ -147,7 +191,7 @@ export class Conference extends PlivoResource { transcriptionMethod: string; callbackUrl: string; callbackMethod: string; - }): Promise; + }): Promise; /** * stop recording conference * @method @@ -158,13 +202,29 @@ export class Conference extends PlivoResource { [clientKey]: any; } /** - * Represents a Conference Interface - * @constructor - * @param {function} client - make api call - * @param {object} [data] - data of call - */ +* Represents a Conference Interface +* @constructor +* @param {function} client - make api call +* @param {object} [data] - data of call +*/ export class ConferenceInterface extends PlivoResourceInterface { constructor(client: any, data?: {}); + + /** + * get conference by id + * @method + * @param {string} id - id of conference + * @promise {@link Conference} return {@link Conference} object if success + * @fail {Error} return Error + */ + get(id: string): Promise; + /** + * get all conferences. returns name of all conferences + * @method + * @promise {@link [Conference]} returns list of {@link Conference} objects if success + * @fail {Error} return Error + */ + list(): Promise; /** * hangup conference * @method @@ -331,7 +391,7 @@ export class ConferenceInterface extends PlivoResourceInterface { stopRecording(id: string): Promise; [clientKey]: any; } -import { PlivoResource } from "../base.js"; +import { PlivoResource } from "../base"; declare const clientKey: unique symbol; -import { PlivoResourceInterface } from "../base.js"; +import { PlivoResourceInterface } from "../base"; export {}; diff --git a/types/resources/endpoints.d.ts b/types/resources/endpoints.d.ts index e1850ef..d40fd8c 100644 --- a/types/resources/endpoints.d.ts +++ b/types/resources/endpoints.d.ts @@ -1,22 +1,101 @@ +export class UpdateEndpointResponse { + constructor(params: object); + apiId: string; + message: string; + alias: string; +} +export class RetrieveEndpointResponse { + constructor(params: object); + apiId: string; + alias: string; + application: string; + endpointId: string; + password: string; + resourceUri: string; + sipRegistered: string; + sipUri: string; + subAccount: string; + username: string; +} +export class ListAllEndpointResponse { + constructor(params: any); + apiId: string; + alias: string; + application: string; + endpointId: string; + password: string; + resourceUri: string; + sipRegistered: string; + sipUri: string; + subAccount: string; + username: string; +} +export class CreateEndpointResponse { + constructor(params: object); + alias: string; + apiId: string; + endpointId: string; + message: string; + username: string; +} /** - * Represents a Endpoint - * @constructor - * @param {function} client - make api call - * @param {object} [data] - data of call - */ +* Represents a Endpoint +* @constructor +* @param {function} client - make api call +* @param {object} [data] - data of call +*/ export class Endpoint extends PlivoResource { constructor(client: any, data?: {}); id: any; + /** + * update Endpoint + * @method + * @param {object} params + * @param {string} [params.username] - username to update + * @param {string} [params.password] - password to update + * @param {string} [params.alias] - alias to update + * @param {string} [params.appId] - app id to update + * @promise {object} return {@link Endpoint} object if success + * @fail {Error} return Error + */ + update(params: object, id: string): Promise; + /** + * delete Endpoint + * @method + * @promise {boolean} return true if success + * @fail {Error} return Error + */ + delete(): Promise; [clientKey]: any; } /** - * Represents a Endpoint Interface - * @constructor - * @param {function} client - make api call - * @param {object} [data] - data of call - */ +* Represents a Endpoint Interface +* @constructor +* @param {function} client - make api call +* @param {object} [data] - data of call +*/ export class EndpointInterface extends PlivoResourceInterface { constructor(client: any, data?: {}); + /** + * Get Endpoint by given id + * @method + * @param {string} id - id of endpoint + * @promise {object} return {@link Endpoint} object if success + * @fail {Error} return Error + */ + get(id: string): Promise; + list(): Promise; + /** + * Create Endpoint + * @method + * @param {string} username - username to create + * @param {string} passwowrd - password to create + * @param {string} alias - alias to create + * @param {string} appId - app id to create + * @promise {object} return {@link PlivoGenericResponse} object if success + * @fail {Error} return Error + */ + create(username: string, password: string, alias: string, appId: string): Promise; /** * update Endpoint * @method @@ -29,12 +108,7 @@ export class EndpointInterface extends PlivoResourceInterface { * @promise {object} return {@link Endpoint} object if success * @fail {Error} return Error */ - update(id: string, params: { - username: string; - password: string; - alias: string; - appId: string; - }): Promise; + update(id: string, params: object): any; /** * delete Endpoint * @method @@ -42,10 +116,10 @@ export class EndpointInterface extends PlivoResourceInterface { * @promise {boolean} return true if success * @fail {Error} return Error */ - delete(id: string): Promise; + delete(id: any): any; [clientKey]: any; } -import { PlivoResource } from "../base.js"; +import { PlivoResource } from "../base"; declare const clientKey: unique symbol; -import { PlivoResourceInterface } from "../base.js"; +import { PlivoResourceInterface } from "../base"; export {}; diff --git a/types/resources/lookup.d.ts b/types/resources/lookup.d.ts new file mode 100644 index 0000000..04c6610 --- /dev/null +++ b/types/resources/lookup.d.ts @@ -0,0 +1,22 @@ +export class LookupResponse { + constructor(params: object); + apiId: string; + phoneNumber: string; + country: object; + format: object; + carrier: object; + resourceUri: string; +} +export class Number extends PlivoResource { + constructor(client: any, data?: {}); + [clientKey]: any; +} +export class LookupInterface extends PlivoResourceInterface { + constructor(client: any, data?: {}); + get(number: string, type?: string): Promise; + [clientKey]: any; +} +import { PlivoResource } from "../base"; +declare const clientKey: unique symbol; +import { PlivoResourceInterface } from "../base"; +export {}; diff --git a/types/resources/media.d.ts b/types/resources/media.d.ts index 52e514a..da86885 100644 --- a/types/resources/media.d.ts +++ b/types/resources/media.d.ts @@ -1,3 +1,24 @@ +export class UploadMediaResponse { + constructor(params: object); + apiId: string; + objects: object; +} +export class RetrieveMediaResponse { + constructor(params: object); + apiId: string; + contentType: string; + fileName: string; + mediaId: string; + mediaUrl: string; + size: string; + uploadTime: string; +} +export class ListMediaResponse { + constructor(params: object); + apiId: string; + meta: string; + objects: string; +} /** * Represents a Message * @constructor @@ -5,8 +26,8 @@ * @param {object} [data] - data of call */ export class Media extends PlivoResource { - constructor(client: any, data?: {}); - id: any; + constructor(client: any, data ? : {}); + id: any; } /** * Represents a Media Interface @@ -15,16 +36,36 @@ export class Media extends PlivoResource { * @param {object} [data] - data of call */ export class MediaInterface extends PlivoResourceInterface { - constructor(client: any, data?: {}); + constructor(client: any, data ? : {}); + /** + * Upload Media + * @method + * @fail {Error} return Error + */ + upload(files: any): Promise; /** - * Upload Media + * Get Media by given id * @method + * @param {string} media_id - id of media + * @promise {object} return {@link Media} object if success * @fail {Error} return Error */ - upload(files: any): Promise; - [clientKey]: any; + get(media_id: string): Promise; + /** + * Get All Media Detail + * @method + * @param {object} params - params to get all media details. + * @promise {object[]} returns list of Media Object + * @fail {Error} returns Error + */ + list(params: object): Promise; + [clientKey]: any; } -import { PlivoResource } from "../base.js"; -import { PlivoResourceInterface } from "../base.js"; +import { + PlivoResource +} from "../base"; +import { + PlivoResourceInterface +} from "../base"; declare const clientKey: unique symbol; -export {}; +export {}; \ No newline at end of file diff --git a/types/resources/messages.d.ts b/types/resources/messages.d.ts index e421fc3..235a835 100644 --- a/types/resources/messages.d.ts +++ b/types/resources/messages.d.ts @@ -1,21 +1,65 @@ -import { PlivoResource, PlivoResourceInterface } from '../base'; -export declare class MessageResponse { - constructor(params: object); - apiId: string - message: string - messageUuid: object +export class MessageResponse { + constructor(params: object); + apiId: string; + message: string; + messageUuid: string; +} +export class MessageGetResponse { + constructor(params: object); + apiId: string; + errorCode: string; + fromNumber: string; + messageDirection: string; + messageState: string; + messageTime: string; + messageType: string; + messageUuid: string; + resourceUri: string; + toNumber: string; + totalAmount: string; + totalRate: string; + units: string; +} +export class MessageListResponse { + constructor(params: object); + errorCode: string; + fromNumber: string; + messageDirection: string; + messageState: string; + messageTime: string; + messageType: string; + messageUuid: string; + resourceUri: string; + toNumber: string; + totalAmount: string; + totalRate: string; + units: string; +} +export class MMSMediaResponse { + constructor(params: object); + apiId: any; + objects: MMSMedia[]; +} +export class MMSMedia { + constructor(params: object); + contentType: string; + fileName: string; + mediaId: string; + mediaUrl: string; + messageUuid: string; + size: string; + uploadTime: string; } - - /** * Represents a Message * @constructor * @param {function} client - make api call * @param {object} [data] - data of call */ -export declare class Message extends PlivoResource { - constructor(client: any, data?: {}); - listMedia(): any; +export class Message extends PlivoResource { + constructor(client: any, data ? : {}); + id: any; + listMedia(): Promise < any > ; } /** * Represents a Message Interface @@ -23,38 +67,56 @@ export declare class Message extends PlivoResource { * @param {function} client - make api call * @param {object} [data] - data of call */ -export declare class MessageInterface extends PlivoResourceInterface { - constructor(client: any, data?: {}); - /** - * Send Message - * @method - * @param {string} src - source number - * @param {string} dst - destination number - * @param {string} text - text to send - * @param {object} optionalParams - Optional Params to send message - * @param {string} [optionalParams.type] - The type of message. Should be `sms` or `mms`. Defaults to `sms`. - * @param {string} [optionalParams.url] The URL to which with the status of the message is sent. - * @param {string} [optionalParams.method] The method used to call the url. Defaults to POST. - * @param {list} [optionalParams.media_urls] For sending mms, specify the media urls in list of string - * @param {boolean} [optionalParams.log] If set to false, the content of this message will not be logged on the Plivo infrastructure and the dst value will be masked (e.g., 141XXXXX528). Default is set to true. - * @promise {object} return {@link PlivoGenericMessage} object if success - * @fail {Error} return Error - */ - send(src: any, dst: string, text: string, optionalParams: object): Promise; - /** - * Create Message - * @method - * @param {string} src - source number - * @param {string} dst - destination number - * @param {string} text - text to send - * @param {object} optionalParams - Optional Params to send message - * @param {string} [optionalParams.type] - The type of message. Should be `sms` or `mms`. Defaults to `sms`. - * @param {string} [optionalParams.url] The URL to which with the status of the message is sent. - * @param {string} [optionalParams.method] The method used to call the url. Defaults to POST. - * @param {boolean} [optionalParams.log] If set to false, the content of this message will not be logged on the Plivo infrastructure and the dst value will be masked (e.g., 141XXXXX528). Default is set to true. - * @param {Array} [optionalParams.media_urls] For sending mms, specify the media urls in list of string - * @promise {object} return {@link MessageResponse} object if success - * @fail {Error} return Error - */ - createtest(src: any, dst: string, text: string, optionalParams: object, powerpackUUID: any): Promise; +export class MessageInterface extends PlivoResourceInterface { + constructor(client: any, data ? : {}); + /** + * Send Message + * @method + * @param {string} src - source number + * @param {string} dst - destination number + * @param {string} text - text to send + * @param {object} optionalParams - Optional Params to send message + * @param {string} [optionalParams.type] - The type of message. Should be `sms` or `mms`. Defaults to `sms`. + * @param {string} [optionalParams.url] The URL to which with the status of the message is sent. + * @param {string} [optionalParams.method] The method used to call the url. Defaults to POST. + * @param {list} [optionalParams.media_urls] For sending mms, specify the media urls in list of string + * @param {boolean} [optionalParams.log] If set to false, the content of this message will not be logged on the Plivo infrastructure and the dst value will be masked (e.g., 141XXXXX528). Default is set to true. + * @promise {object} return {@link PlivoGenericMessage} object if success + * @fail {Error} return Error + */ + send(src: string, dst: string, text: string, optionalParams?: { + type: string; + url: string; + method: string; + media_urls: any; + log: boolean; + }): Promise < MessageResponse > ; + /** + * Create Message + * @method + * @param {string} src - source number + * @param {string} dst - destination number + * @param {string} text - text to send + * @param {object} optionalParams - Optional Params to send message + * @param {string} [optionalParams.type] - The type of message. Should be `sms` or `mms`. Defaults to `sms`. + * @param {string} [optionalParams.url] The URL to which with the status of the message is sent. + * @param {string} [optionalParams.method] The method used to call the url. Defaults to POST. + * @param {boolean} [optionalParams.log] If set to false, the content of this message will not be logged on the Plivo infrastructure and the dst value will be masked (e.g., 141XXXXX528). Default is set to true. + * @param {Array} [optionalParams.media_urls] For sending mms, specify the media urls in list of string + * @promise {object} return {@link MessageResponse} object if success + * @fail {Error} return Error + */ + create(src: any, dst: any, text: string, optionalParams?: object, powerpackUUID?: string ): Promise < MessageResponse >; + + get(id: string): Promise; + + list(params: object): Promise < MessageListResponse> ; + + listMedia(messageUUID: string): Promise ; } +import { + PlivoResource +} from "../base"; +import { + PlivoResourceInterface +} from "../base"; \ No newline at end of file diff --git a/types/resources/numbers.d.ts b/types/resources/numbers.d.ts index a59ff0c..25fc5d6 100644 --- a/types/resources/numbers.d.ts +++ b/types/resources/numbers.d.ts @@ -1,3 +1,14 @@ +export class BuyNumberResponse { + constructor(params: object); + apiId: string; + numbers: object; + status: string; +} +export class UpdateNumberResponse { + constructor(params: object); + apiId: string; + message: string; +} /** * Represents a PhoneNumber * @constructor @@ -5,17 +16,17 @@ * @param {object} [data] - data of call */ export class PhoneNumber extends PlivoResource { - constructor(client: any, data?: {}); - id: any; - /** - * Buy Phone Number - * @method - * @param {string} appId - app id - * @promise {@link PlivoGenericResponse} return PlivoGenericResponse Object if success - * @fail {Error} return Error - */ - buy(appId: string): Promise; - [clientKey]: any; + constructor(client: any, data ? : {}); + id: any; + /** + * Buy Phone Number + * @method + * @param {string} appId - app id + * @promise {@link PlivoGenericResponse} return PlivoGenericResponse Object if success + * @fail {Error} return Error + */ + buy(appId: string): Promise < any > ; + [clientKey]: any; } /** * Represents a PhoneNumbers Interface @@ -25,16 +36,16 @@ export class PhoneNumber extends PlivoResource { * @param {string} [data.test] - test data */ export class PhoneNumberInterface extends PlivoResourceInterface { - constructor(client: any, data?: {}); - /** - * Buy Phone Number - * @method - * @param {string} appId - app id - * @promise {@link PlivoGenericResponse} return PlivoGenericResponse Object if success - * @fail {Error} return Error - */ - buy(appId: string): Promise; - [clientKey]: any; + constructor(client: any, data ? : {}); + /** + * Buy Phone Number + * @method + * @param {string} appId - app id + * @promise {@link PlivoGenericResponse} return PlivoGenericResponse Object if success + * @fail {Error} return Error + */ + buy(number: string, appId: string): Promise < BuyNumberResponse > ; + [clientKey]: any; } /** * Represents a Number @@ -43,15 +54,16 @@ export class PhoneNumberInterface extends PlivoResourceInterface { * @param {object} [data] - data of call */ export class NumberResource extends PlivoResource { - constructor(client: any, data?: {}); - id: any; - /** - * Unrent Number - * @method - * @promise {boolean} return true if success - * @fail {Error} return Error - */ - unrent(): Promise; + constructor(client: any, data ? : {}); + id: any; + /** + * Unrent Number + * @method + * @promise {boolean} return true if success + * @fail {Error} return Error + */ + unrent(number: string): Promise < any > ; + [clientKey]: any; } /** * Represents a Numbers @@ -60,63 +72,67 @@ export class NumberResource extends PlivoResource { * @param {object} [data] - data of call */ export class NumberInterface extends PlivoResourceInterface { - constructor(client: any); - /** - * Buy Phone Number - * @method - * @param {string} number - number to buy - * @param {string} appId - app id - * @promise {@link PlivoGenericResponse} return PlivoGenericResponse Object if success - * @fail {Error} return Error - */ - buy(number: string, appId: string): Promise; - /** - * 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 - */ - addOwnNumber(numbers: string, carrier: string, region: string, optionalParams: any): Promise; - /** - * 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 - */ - search(countryISO: string, optionalParams: any): Promise; - /** - * Update Number - * @method - * @param {string} number - number to update - * @param {object} params - * @param {string} [params.appId] - app id - * @param {string} [params.subAccount] - auth_id of subaccount - * @param {string} [params.alias] - textual name of number - * @promise {@link NumberResource} return NumberResource Object if success - * @fail {Error} return Error - */ - update(number: string, params: { - appId: string; - subAccount: string; - alias: string; - }): Promise; - /** - * Unrent Number - * @method - * @param {string} number - number to unrent - * @promise {boolean} return true if success - * @fail {Error} return Error - */ - unrent(number: string): Promise; - [clientKey]: any; + constructor(client: any); + /** + * Buy Phone Number + * @method + * @param {string} number - number to buy + * @param {string} appId - app id + * @promise {@link PlivoGenericResponse} return PlivoGenericResponse Object if success + * @fail {Error} return Error + */ + buy(number: string, appId: string): Promise < any > ; + /** + * 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 + */ + addOwnNumber(numbers: string, carrier: string, region: string, optionalParams: any): Promise < any > ; + /** + * 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 + */ + search(countryISO: string, optionalParams: any): Promise < any > ; + /** + * Update Number + * @method + * @param {string} number - number to update + * @param {object} params + * @param {string} [params.appId] - app id + * @param {string} [params.subAccount] - auth_id of subaccount + * @param {string} [params.alias] - textual name of number + * @promise {@link NumberResource} return NumberResource Object if success + * @fail {Error} return Error + */ + update(number: string, params: { + appId: string; + subAccount: string; + alias: string; + }): Promise < any > ; + /** + * Unrent Number + * @method + * @param {string} number - number to unrent + * @promise {boolean} return true if success + * @fail {Error} return Error + */ + unrent(number: string): Promise < any > ; + [clientKey]: any; } -import { PlivoResource } from "../base.js"; +import { + PlivoResource +} from "../base"; declare const clientKey: unique symbol; -import { PlivoResourceInterface } from "../base.js"; -export {}; +import { + PlivoResourceInterface +} from "../base"; +export {}; \ No newline at end of file diff --git a/types/resources/phlo.d.ts b/types/resources/phlo.d.ts index 9a3875a..c7b449b 100644 --- a/types/resources/phlo.d.ts +++ b/types/resources/phlo.d.ts @@ -1,3 +1,16 @@ +export class RunPHLOResponse { + constructor(params: any); + apiId: any; + phloId: any; + message: any; +} +export class RetrievePHLOResponse { + constructor(params: any); + apiId: any; + phloId: any; + name: any; + createdOn: any; +} /** * Represents a Phlo * @constructor @@ -5,16 +18,17 @@ * @param {object} [data] - data of phlo */ export class Phlo extends PlivoResource { - constructor(client: any, data?: {}); - client: any; - multiPartyCall: (nodeId: any) => PhloMultiPartyCall; - /** - * run phlo - * @method - * @promise {Boolean} return true if phlo is complete - * @fail {Error} return Error - */ - run(params: any): Promise; + constructor(client: any, data ? : {}); + client: any; + multiPartyCall: (nodeId: any) => PhloMultiPartyCall; + /** + * run phlo + * @method + * @promise {Boolean} return true if phlo is complete + * @fail {Error} return Error + */ + run(params: object): Promise < RunPHLOResponse > ; + [clientKey]: any; } /** * Represents a Phlo Interface @@ -24,7 +38,17 @@ export class Phlo extends PlivoResource { */ export class PhloInterface extends PlivoResourceInterface { constructor(client: any, data?: {}); + get(id: string): Promise; + [clientKey]: any; } -import { PlivoResource } from "../base.js"; -import { PhloMultiPartyCall } from "./phloMultipartyCall.js"; -import { PlivoResourceInterface } from "../base.js"; +import { + PlivoResource +} from "../base"; +import { + PhloMultiPartyCall +} from "./phloMultipartyCall"; +declare const clientKey: unique symbol; +import { + PlivoResourceInterface +} from "../base"; +export {}; \ No newline at end of file diff --git a/types/resources/phloMultiPartyCallMember.d.ts b/types/resources/phloMultiPartyCallMember.d.ts index 14af70c..de765d9 100644 --- a/types/resources/phloMultiPartyCallMember.d.ts +++ b/types/resources/phloMultiPartyCallMember.d.ts @@ -4,20 +4,31 @@ * @param {function} client - make api call * @param {object} [data] - data of phlo */ +export class UpdateMemberResponse { + constructor(params: object); + apiId: string; + error: string; +} export class PhloMultiPartyCallMember extends PlivoResource { - constructor(client: any, data?: {}); - action: string; - client: any; - resumeCall(): Promise; - voicemailDrop(): Promise; - hangup(): Promise; - hold(): Promise; + constructor(client: any, data ? : {}); + action: string; + client: any; + resumeCall(): Promise < any > ; + voicemailDrop(): Promise < any > ; + hangup(): Promise < any > ; + hold(): Promise < any > ; unhold(): Promise; + update(action: object): Promise; } export class PhloMultiPartyCallMemberInterface extends PlivoResourceInterface { - constructor(client: any, data?: {}); - action: string; + constructor(client: any, data ? : {}); + action: string; client: any; + get(phloId: string, nodeId: string, memberAddress: string): any; } -import { PlivoResource } from "../base.js"; -import { PlivoResourceInterface } from "../base.js"; +import { + PlivoResource +} from "../base"; +import { + PlivoResourceInterface +} from "../base"; \ No newline at end of file diff --git a/types/resources/phloMultipartyCall.d.ts b/types/resources/phloMultipartyCall.d.ts index 1177f39..c5dabc9 100644 --- a/types/resources/phloMultipartyCall.d.ts +++ b/types/resources/phloMultipartyCall.d.ts @@ -1,16 +1,42 @@ +export class UpdateMultipartyCallResponse { + constructor(params: object); + apiId: string; + error: string; +} +export class RetrieveMultipartyCallResponse { + constructor(params: object); + apiId: string; + nodeId: string; + phloId: string; + name: string; + nodeType: string; + createdOn: string; +} export class PhloMultiPartyCall extends PlivoResource { - constructor(client: any, data?: {}); - action: string; - client: any; - member: (memberAddress: any) => PhloMultiPartyCallMember; - call(triggerSource: any, to: any, role: any): Promise; - warmTransfer(triggerSource: any, to: any, role: any): Promise; - coldTransfer(triggerSource: any, to: any, role: any): Promise; + constructor(client: any, data ? : {}); + action: string; + client: any; + member: (memberAddress: any) => PhloMultiPartyCallMember; + call(triggerSource: any, to: any, role: any): Promise < any > ; + warmTransfer(triggerSource: any, to: any, role: any): Promise < UpdateMultipartyCallResponse > ; + coldTransfer(triggerSource: any, to: any, role: any): Promise < UpdateMultipartyCallResponse > ; abortTransfer(memberAddress: any): Promise; + update(action: any, triggerSource: any, to: any, role: any): Promise; + [clientKey]: any; } export class PhloMultiPartyCallInterface extends PlivoResourceInterface { constructor(client: any, data?: {}); + get(phloId: string, id: string): Promise; + [clientKey]: any; } -import { PlivoResource } from "../base.js"; -import { PhloMultiPartyCallMember } from "./phloMultiPartyCallMember.js"; -import { PlivoResourceInterface } from "../base.js"; +import { + PlivoResource +} from "../base"; +import { + PhloMultiPartyCallMember +} from "./phloMultiPartyCallMember"; +declare const clientKey: unique symbol; +import { + PlivoResourceInterface +} from "../base"; +export {}; \ No newline at end of file diff --git a/types/resources/powerpacks.d.ts b/types/resources/powerpacks.d.ts index c5aeb70..4fabcab 100644 --- a/types/resources/powerpacks.d.ts +++ b/types/resources/powerpacks.d.ts @@ -1,3 +1,111 @@ +export class ListAllNumbersResponse { + constructor(params: object); + apiId: string; + meta: object; + objects: object; +} +export class CreatePowerpackResponse { + constructor(params: any); + apiId: string; + applicationId: string; + applicationType: string; + createdOn: string; + localConnect: string; + name: string; + numberPool: string; + numberPriority: string; + stickySender: string; + uuid: string; +} +export class UpdatePowerpackResponse { + constructor(params: object); + apiId: string; + applicationId: string; + applicationType: string; + createdOn: string; + localConnect: string; + name: string; + numberPool: string; + stickySender: string; + uuid: string; +} +export class ListShortCodeResponse { + constructor(params: object); + apiId: string; + meta: object; + objects: object; +} +export class ListTollFreeResponse { + constructor(params: object); + apiId: string; + meta: object; + objects: object; +} +export class AddNumberResponse { + constructor(params: object); + apiId: string; + accountPhoneNumberResource: string; + addedOn: string; + countryIso2: string; + number: string; + numberPoolUuid: string; + type: string; + service: string; +} +export class RemoveNumberResponse { + constructor(params: object); + apiId: string; + response: string; +} +export class RemoveTollFreeNumberResponse { + constructor(params: object); + apiId: any; + response: string; +} +export class RemoveShortCodeResponse { + constructor(params: any); + apiId: any; + response: string; +} +export class AddTollFreeNumberresponse { + constructor(params: object); + apiId: string; + accountPhoneNumberResource: string; + addedOn: string; + countryIso2: string; + number: string; + numberPoolUuid: string; + type: string; + service: string; +} +export class RetrieveNumberResponse { + constructor(params: object); + apiId: string; + accountPhoneNumberResource: string; + addedOn: string; + countryIso2: string; + number: string; + numberPoolUuid: string; + type: string; +} +export class RetrieveTollFreeResponse { + constructor(params: object); + apiId: string; + accountPhoneNumberResource: string; + addedOn: string; + countryIso2: string; + number: string; + numberPoolUuid: string; + type: string; +} +export class RetrieveShortCodeResponse { + constructor(params: object); + apiId: string; + addedOn: string; + countryIso2: string; + shortCode: string; + numberPoolUuid: string; +} /** * Represents a Powerpack * @constructor @@ -5,55 +113,56 @@ * @param {object} [data] - data of call */ export class Powerpack extends PlivoResource { - constructor(client: any, data?: {}); - uuid: any; - number_pool_id: any; - number_pool: NumberPool; - list_numbers(params: any): Promise; - search_query(params: any): string; - count_numbers(params: any): Promise; - find_number(number: any): Promise; - add_number(number: any): Promise; - add_tollfree(tollfree: any): Promise; - remove_number(number: any, unrent?: boolean): Promise; - remove_tollfree(tollfree: any, unrent?: boolean): Promise; - remove_shortcode(shortcode: any): Promise; - list_shortcodes(params: any): Promise; - list_tollfree(params: any): Promise; - find_shortcode(shortcode: any): Promise; - find_tollfree(tollfree: any): Promise; - buy_add_number(params: any): any; + constructor(client: any, data ? : {}); + uuid: any; + number_pool_id: any; + number_pool: NumberPool; + list_numbers(params: object): Promise < ListAllNumbersResponse > ; + search_query(params: object): string; + count_numbers(params: object): Promise < any > ; + find_number(number: string): Promise < RetrieveNumberResponse > ; + add_number(number: string, service ? : string): Promise < AddNumberResponse > ; + add_tollfree(tollfree: string, service ? : string): Promise < AddTollFreeNumberresponse > ; + remove_number(number: string, unrent ? : boolean): Promise < RemoveNumberResponse > ; + remove_tollfree(tollfree: string, unrent ? : boolean): Promise < RemoveTollFreeNumberResponse > ; + remove_shortcode(shortcode: string): Promise < RemoveShortCodeResponse > ; + list_shortcodes(params: object): Promise < ListShortCodeResponse > ; + list_tollfree(params: object): Promise < ListTollFreeResponse > ; + find_shortcode(shortcode: object, service ? : string): Promise < RetrieveShortCodeResponse > ; + find_tollfree(tollfree: string, service ? : string): Promise < RetrieveTollFreeResponse > ; + buy_add_number(params: object): any; + [clientKey]: any; } export class NumberPool extends PlivoResource { - constructor(client: any, data?: {}); - numbers: Numbers; - shortcodes: Shortcode; - tollfree: Tollfree; + constructor(client: any, data ? : {}); + numbers: Numbers; + shortcodes: Shortcode; + tollfree: Tollfree; } export class Numbers extends PlivoResource { - constructor(client: any, data?: {}); - buy_add_number(params: any): any; - list(params: any): Promise; - count(params: any): Promise; - search_query(params: any): string; - find(number: any): Promise; - add(number: any): Promise; - remove(number: any, unrent?: boolean): Promise; + constructor(client: any, data ? : {}); + buy_add_number(params: object): any; + list(params: object): Promise < any > ; + count(params: object): Promise < any > ; + search_query(params: object): string; + find(number: object): Promise < any > ; + add(number: string, service ? : string): Promise < any > ; + remove(number: string, unrent ? : boolean): Promise < any > ; } export class Shortcode extends PlivoResource { - constructor(client: any, data?: {}); - number_pool_id: any; - list(params: any): Promise; - find(shortcode: any): Promise; - remove(shortcode: any): Promise; + constructor(client: any, data ? : {}); + number_pool_id: any; + list(params: object): Promise < any > ; + find(shortcode: object): Promise < any > ; + remove(shortcode: object): Promise < any > ; } export class Tollfree extends PlivoResource { - constructor(client: any, data?: {}); - number_pool_id: any; - add(tollfree: any): Promise; - remove(tollfree: any, unrent?: boolean): Promise; - list(params: any): Promise; - find(tollfree: any): Promise; + constructor(client: any, data ? : {}); + number_pool_id: any; + add(tollfree: string): Promise < any > ; + remove(tollfree: string, unrent ? : boolean): Promise < any > ; + list(params: object): Promise < any > ; + find(tollfree: object): Promise < any > ; } /** * Represents a Powerpack interface @@ -62,27 +171,55 @@ export class Tollfree extends PlivoResource { * @param {object} [data] - data of call */ export class PowerpackInterface extends PlivoResourceInterface { - constructor(client: any, data?: {}); + constructor(client: any, data ? : {}); /** - * update Powerpack - * @method - * @param {string} uuid - id of Powerpack - * @param {object} params - to update Powerpack - * @param {string} [params.name] - * @param {string} [params.sticky_sender] - * @param {string} [params.local_connect] - * @param {string} [params.application_type] - * @param {string} [params.application_id] - * @promise {object} return {@link Powerpack} object - * @fail {Error} return Error - */ - update(uuid: string, params: { - name: string; - sticky_sender: string; - local_connect: string; - application_type: string; - application_id: string; - }): Promise; + * get Powerpack by given id + * @method + * @param {string} uuid - id of Powerpack + * @promise {object} return {@link Powerpack} object + * @fail {Error} return Error + */ + get(uuid: string): any; + /** + * create Powerpack + * @method + * @param {string} name - name of Powerpack + * @param {object} params - params to create Powerpack + * @param {string} [params.sticky_sender] - + * @param {string} [params.local_connect] + * @param {string} [params.application_type] + * @param {string} [params.application_id] + * @promise {object} return {@link PlivoGenericResponse} object + * @fail {Error} return Error + */ + create(name: string, params?: {}): Promise; + /** + * update Powerpack + * @method + * @param {string} uuid - id of Powerpack + * @param {object} params - to update Powerpack + * @param {string} [params.name] + * @param {string} [params.sticky_sender] + * @param {string} [params.local_connect] + * @param {string} [params.application_type] + * @param {string} [params.application_id] + * @promise {object} return {@link Powerpack} object + * @fail {Error} return Error + */ + update(uuid: string, params: { + name: string; + sticky_sender: string; + local_connect: string; + application_type: string; + application_id: string; + }): Promise < UpdatePowerpackResponse > ; + [clientKey]: any; } -import { PlivoResource } from "../base.js"; -import { PlivoResourceInterface } from "../base.js"; +import { + PlivoResource +} from "../base"; +declare const clientKey: unique symbol; +import { + PlivoResourceInterface +} from "../base"; +export {}; \ No newline at end of file diff --git a/types/resources/pricings.d.ts b/types/resources/pricings.d.ts index 2c66c24..dfcf9d3 100644 --- a/types/resources/pricings.d.ts +++ b/types/resources/pricings.d.ts @@ -1,9 +1,20 @@ /** - * Represents a Pricing - * @constructor - * @param {function} client - make api call - * @param {object} [data] - data of call - */ +* Represents a Pricing +* @constructor +* @param {function} client - make api call +* @param {object} [data] - data of call +*/ +export class PricingResponse { + constructor(params: any); + apiId: string; + country: string; + countryCode: string; + countryIso: string; + message: object; + mms: object; + phoneNumbers: object; + voice: object; +} export class Pricing extends PlivoResource { constructor(client: any, data?: {}); /** @@ -12,19 +23,20 @@ export class Pricing extends PlivoResource { * @promise {object} return {@link PlivoGenericResponse} object * @fail {Error} return Error */ - get(): Promise; + get(): Promise; + [clientKey]: any; } /** - * Represents a Pricing Interface - * @constructor - * @param {function} client - make api call - * @param {object} [data] - data of call - */ +* Represents a Pricing Interface +* @constructor +* @param {function} client - make api call +* @param {object} [data] - data of call +*/ export class PricingInterface extends PlivoResourceInterface { constructor(client: any, data?: {}); [clientKey]: any; } -import { PlivoResource } from "../base.js"; -import { PlivoResourceInterface } from "../base.js"; +import { PlivoResource } from "../base"; declare const clientKey: unique symbol; +import { PlivoResourceInterface } from "../base"; export {}; diff --git a/types/resources/recordings.d.ts b/types/resources/recordings.d.ts index 60e3a25..fa51f2a 100644 --- a/types/resources/recordings.d.ts +++ b/types/resources/recordings.d.ts @@ -1,3 +1,33 @@ +export class RetrieveRecordingResponse { + constructor(params: object); + addTime: string; + apiId: string; + callUuid: string; + conferenceName: string; + recordingDurationMs: string; + recordingEndMs: string; + recordingFormat: string; + recordingId: string; + recordingStartMs: string; + recordingType: string; + recordingUrl: string; + resourceUri: string; +} +export class ListRecordingResponse { + constructor(params: object); + addTime: string; + apiId: string; + callUuid: string; + conferenceName: string; + recordingDurationMs: string; + recordingEndMs: string; + recordingFormat: string; + recordingId: string; + recordingStartMs: string; + recordingType: string; + recordingUrl: string; + resourceUri: string; +} /** * Represents a Recording * @constructor @@ -5,8 +35,9 @@ * @param {object} [data] - data of call */ export class Recording extends PlivoResource { - constructor(client: any, data?: {}); - id: any; + constructor(client: any, data ? : {}); + id: any; + [clientKey]: any; } /** * Represents a Recording Interface @@ -15,18 +46,41 @@ export class Recording extends PlivoResource { * @param {object} [data] - data of call */ export class RecordingInterface extends PlivoResourceInterface { - constructor(client: any, data?: {}); + constructor(client: any, data ? : {}); + /** + * Delete recording by id + * @method + * @param {string} id - id to delete recording + * @promise {boolean} return true if success + * @fail {Error} return Error + */ + delete(id: string): Promise < any > ; + [clientKey]: any; /** - * Delete recording by id + * Get recording by id * @method - * @param {string} id - id to delete recording - * @promise {boolean} return true if success + * @param {string} id - id to get recording information + * @promise {object} return {@link Pricing} object * @fail {Error} return Error */ - delete(id: string): Promise; - [clientKey]: any; + get(id: string): Promise; + /** + * list recordings + * @method + * @param {object} params - params to list recordings + * @param {string} [params.subaccount] - ID of the subaccount if present + * @param {string} [params.callUuid] - Call UUID of the call to filter recordings associated with it + * @param {string} [params.addTime] - Filter based on the timings they were added + * @param {string} [params.limit] - Display no of results per page + * @param {string} [params.offset] - No of value items by which results should be offset + */ + list(params?: {}): Promise; } -import { PlivoResource } from "../base.js"; -import { PlivoResourceInterface } from "../base.js"; +import { + PlivoResource +} from "../base"; declare const clientKey: unique symbol; -export {}; +import { + PlivoResourceInterface +} from "../base"; +export {}; \ No newline at end of file diff --git a/types/rest/client-test.d.ts b/types/rest/client-test.d.ts index 9fe8a7d..bbf1693 100644 --- a/types/rest/client-test.d.ts +++ b/types/rest/client-test.d.ts @@ -7,6 +7,7 @@ export class Client { conferences: ConferenceInterface; endpoints: EndpointInterface; messages: MessageInterface; + lookup: LookupInterface; powerpacks: PowerpackInterface; numbers: NumberInterface; pricings: PricingInterface; @@ -29,6 +30,7 @@ import { ApplicationInterface } from "../resources/applications.js"; import { ConferenceInterface } from "../resources/conferences.js"; import { EndpointInterface } from "../resources/endpoints.js"; import { MessageInterface } from "../resources/messages.js"; +import { LookupInterface } from "../resources/lookup.js"; import { PowerpackInterface } from "../resources/powerpacks.js"; import { NumberInterface } from "../resources/numbers.js"; import { PricingInterface } from "../resources/pricings.js"; diff --git a/types/rest/client.d.ts b/types/rest/client.d.ts index d52bce4..8723574 100644 --- a/types/rest/client.d.ts +++ b/types/rest/client.d.ts @@ -13,6 +13,7 @@ export class Client { conferences: ConferenceInterface; endpoints: EndpointInterface; messages: MessageInterface; + lookup: LookupInterface; powerpacks: PowerpackInterface; numbers: NumberInterface; pricings: PricingInterface; @@ -37,6 +38,7 @@ import { ApplicationInterface } from "../resources/applications.js"; import { ConferenceInterface } from "../resources/conferences.js"; import { EndpointInterface } from "../resources/endpoints.js"; import { MessageInterface } from "../resources/messages.js"; +import { LookupInterface } from "../resources/lookup.js"; import { PowerpackInterface } from "../resources/powerpacks.js"; import { NumberInterface } from "../resources/numbers.js"; import { PricingInterface } from "../resources/pricings.js"; diff --git a/types/utils/jsonStrinfigier.d.ts b/types/utils/jsonStrinfigier.d.ts index d985f4b..c1de530 100644 --- a/types/utils/jsonStrinfigier.d.ts +++ b/types/utils/jsonStrinfigier.d.ts @@ -2,6 +2,6 @@ export default Flatted; export function parse(text: any, reviver: any): any; export function stringify(value: any, replacer: any, space: any): string; declare namespace Flatted { - export function parse(text: any, reviver: any): any; - export function stringify(value: any, replacer: any, space: any): string; + function parse(text: any, reviver: any): any; + function stringify(value: any, replacer: any, space: any): string; } diff --git a/types/utils/jwt.d.ts b/types/utils/jwt.d.ts new file mode 100644 index 0000000..ad12cfd --- /dev/null +++ b/types/utils/jwt.d.ts @@ -0,0 +1,21 @@ +export function AccessToken(authId: any, authToken: any, username: any, validityOptions?: {}, uid?: any): void; +export class AccessToken { + constructor(authId: string, authToken: string, username: string, validityOptions?: {}, uid?: any); + authId: string; + key: string; + username: string; + validFrom: string; + lifetime: string; + uid: string; +} +export function addVoiceGrants(incoming?: boolean, outgoing?: boolean): void; +export class addVoiceGrants { + constructor(incoming?: boolean, outgoing?: boolean); + grants: { + voice: { + incoming_allow: boolean; + outgoing_allow: boolean; + }; + }; +} +export function toJwt(): any; diff --git a/types/utils/security.d.ts b/types/utils/security.d.ts index 52e3081..fd33bb2 100644 --- a/types/utils/security.d.ts +++ b/types/utils/security.d.ts @@ -1,7 +1,3 @@ -export function computeOldSignature(authId: string, uri: string, params: { - [string]: string; -}): string; -export function verifyOldSignature(authId: string, uri: string, params: { - [string]: string; -}, signature: string): boolean; -export function validateSignature(uri: string, nonce: string, signature: string, auth_token: string): boolean; +export function computeOldSignature(authId: any, uri: any, params: any): any; +export function verifyOldSignature(authId: any, uri: any, params: any, signature: any): boolean; +export function validateSignature(uri: any, nonce: any, signature: any, auth_token: any): boolean; diff --git a/types/utils/v3Security.d.ts b/types/utils/v3Security.d.ts index d1ab388..79b13cb 100644 --- a/types/utils/v3Security.d.ts +++ b/types/utils/v3Security.d.ts @@ -1 +1 @@ -export function validateV3Signature(method: string, uri: string, nonce: string, auth_token: string, v3_signature: string, params?: {}): boolean; +export function validateV3Signature(method: any, uri: any, nonce: any, auth_token: any, v3_signature: any, params?: {}): boolean;