diff --git a/examples/webhook-signing/express.js b/examples/webhook-signing/express.js index e8e57b2..b00046a 100644 --- a/examples/webhook-signing/express.js +++ b/examples/webhook-signing/express.js @@ -38,6 +38,13 @@ app.post('/webhooks', bodyParser.json(), function(req, res) { console.log('Success', event.data.id); + // inbound call control + if (event.data.event_type === 'call.initiated') { + const call = new telnyx.Call({call_control_id: event.data.payload.call_control_id}); + + call.answer(); + } + // Event was 'constructed', so we can respond with a 200 OK res.status(200).send('Signed Webhook Received: ' + event.data.id); }); diff --git a/lib/ResourceNamespace.js b/lib/ResourceNamespace.js deleted file mode 100644 index 26fda39..0000000 --- a/lib/ResourceNamespace.js +++ /dev/null @@ -1,22 +0,0 @@ -'use strict'; - -// ResourceNamespace allows you to create nested resources, i.e. `telnyx.issuing.cards`. -// It also works recursively, so you could do i.e. `telnyx.billing.invoicing.pay`. - -function ResourceNamespace(telnyx, resources) { - for (var name in resources) { - var camelCaseName = name[0].toLowerCase() + name.substring(1); - - var resource = new resources[name](telnyx); - - this[camelCaseName] = resource; - } -} - -module.exports = function(namespace, resources) { - return function (telnyx) { - return new ResourceNamespace(telnyx, resources); - }; -}; - -module.exports.ResourceNamespace = ResourceNamespace; diff --git a/lib/telnyx.js b/lib/telnyx.js index 7470375..2c6cb2f 100644 --- a/lib/telnyx.js +++ b/lib/telnyx.js @@ -270,8 +270,8 @@ Telnyx.prototype = { }, _createEmptyConstructor: function(content) { - return function() { - return content; + return function(args) { + return Object.assign(content, args || {}); } } }; diff --git a/test/resources/Calls.spec.js b/test/resources/Calls.spec.js index 7a7ed4d..83e9c58 100644 --- a/test/resources/Calls.spec.js +++ b/test/resources/Calls.spec.js @@ -110,7 +110,6 @@ describe('Calls Resource', function() { } } - function responseFn(response) { expect(response.data).to.include({result: 'ok'}); } @@ -137,9 +136,15 @@ describe('Calls Resource', function() { COMMANDS.forEach(function(command) { describe(command, function() { const camelCaseCommand = utils.snakeToCamelCase(command); + let telnyxInstance; + + this.beforeEach(() => { + // make specs independent + telnyxInstance = require('../../testUtils').getTelnyxMock(); + }); it('Sends the correct request', function() { - return telnyx.calls.create(callCreateData) + return telnyxInstance.calls.create(callCreateData) .then(function(response) { const call = response.data; call[utils.snakeToCamelCase(command)](callCommandsData[command] || {}) @@ -150,7 +155,7 @@ describe('Calls Resource', function() { }) }); it('Sends the correct request [with specified auth]', function() { - return telnyx.calls.create(callCreateData) + return telnyxInstance.calls.create(callCreateData) .then(function(response) { const call = response.data; call[utils.snakeToCamelCase(command)](callCommandsData[command] || {}, TEST_AUTH_KEY) @@ -162,31 +167,29 @@ describe('Calls Resource', function() { }); it('Sends the correct method request [with empty resource instance]', function() { - const call = new telnyx.Call(); - call.call_control_id = '3fa85f55-5717-4562-b3fc-2c963f63vga6'; + const call = new telnyxInstance.Call({call_control_id: '3fa85f55-5717-4562-b3fc-2c963f63vga6'}); call[utils.snakeToCamelCase(command)](callCommandsData[command] || {}) .then(responseFn); - return call[command](callCommandsData[command] || {}) + call[utils.snakeToCamelCase(command)](callCommandsData[command] || {}, TEST_AUTH_KEY) .then(responseFn); }); it('Sends the correct method request [with empty resource instance and specified auth]', function() { - const call = new telnyx.Call(); - call.call_control_id = '3fa85f55-5717-4562-b3fc-2c963f63vga6'; + const call = new telnyxInstance.Call({call_control_id: '3fa85f55-5717-4562-b3fc-2c963f63vga6'}); - call[utils.snakeToCamelCase(command)](callCommandsData[command] || {}, TEST_AUTH_KEY) + call[command](callCommandsData[command] || {}, TEST_AUTH_KEY) .then(responseFn); - return call[command](callCommandsData[command] || {}, TEST_AUTH_KEY) + return call[command](callCommandsData[command] || {}) .then(responseFn); }); if (camelCaseCommand !== command) { describe(camelCaseCommand, function() { it('Sends the correct request', function() { - return telnyx.calls.create(callCreateData) + return telnyxInstance.calls.create(callCreateData) .then(function(response) { const call = response.data; @@ -195,7 +198,7 @@ describe('Calls Resource', function() { }) }); it('Sends the correct request [with specified auth]', function() { - return telnyx.calls.create(callCreateData) + return telnyxInstance.calls.create(callCreateData) .then(function(response) { const call = response.data; diff --git a/test/resources/Conferences.spec.js b/test/resources/Conferences.spec.js index 949ae6d..127ddd4 100644 --- a/test/resources/Conferences.spec.js +++ b/test/resources/Conferences.spec.js @@ -68,8 +68,15 @@ describe('Calls Resource', function() { CONFERENCES.forEach(function(action) { describe(action, function() { + let telnyxInstance; + + this.beforeEach(() => { + // make specs independent + telnyxInstance = require('../../testUtils').getTelnyxMock(); + }); + it('Sends the correct request', function() { - return telnyx.conferences.create(conferenceCreateData) + return telnyxInstance.conferences.create(conferenceCreateData) .then(function(response) { const conference = response.data; return conference[action](callConferencesData[action] || {}) @@ -77,7 +84,7 @@ describe('Calls Resource', function() { }) }); it('Sends the correct request [with specified auth]', function() { - return telnyx.conferences.create(conferenceCreateData) + return telnyxInstance.conferences.create(conferenceCreateData) .then(function(response) { const conference = response.data; return conference[action](callConferencesData[action] || {}, TEST_AUTH_KEY) @@ -86,15 +93,13 @@ describe('Calls Resource', function() { }); it('Sends the correct request [with empty resource instance]', function() { - const conference = new telnyx.Conference(); - conference.call_control_id = '891510ac-f3e4-11e8-af5b-de00688a4901'; + const conference = new telnyxInstance.Conference({id: '891510ac-f3e4-11e8-af5b-de00688a4901'}); return conference[action](callConferencesData[action] || {}) .then(responseFn); }); it('Sends the correct request [with empty resource instance and specified auth]', function() { - const conference = new telnyx.Conference(); - conference.call_control_id = '891510ac-f3e4-11e8-af5b-de00688a4901'; + const conference = new telnyxInstance.Conference({id: '891510ac-f3e4-11e8-af5b-de00688a4901'}); return conference[action](callConferencesData[action] || {}, TEST_AUTH_KEY) .then(responseFn); diff --git a/testUtils/index.js b/testUtils/index.js index f572d24..b979f15 100644 --- a/testUtils/index.js +++ b/testUtils/index.js @@ -6,8 +6,6 @@ require('mocha'); // Ensure we are using the 'as promised' libs before any tests are run: require('chai').use(require('chai-as-promised')); -var ResourceNamespace = require('../lib/ResourceNamespace').ResourceNamespace; - var utils = module.exports = { getUserTelnyxKey: function() { @@ -32,12 +30,6 @@ var utils = module.exports = { function makeInstanceSpyable(telnyxInstance, thisInstance) { if (thisInstance instanceof telnyx.TelnyxResource) { patchRequest(telnyxInstance, thisInstance); - } else if (thisInstance instanceof ResourceNamespace) { - var namespace = thisInstance; - - for (var j in namespace) { - makeInstanceSpyable(telnyxInstance, namespace[j]); - } } }