Skip to content

Commit

Permalink
Merge 1a804e0 into e8de1cb
Browse files Browse the repository at this point in the history
  • Loading branch information
DeividVeloso committed Nov 10, 2020
2 parents e8de1cb + 1a804e0 commit df4e8cd
Show file tree
Hide file tree
Showing 6 changed files with 129 additions and 15 deletions.
2 changes: 1 addition & 1 deletion lib/TelnyxResource.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ TelnyxResource.prototype = {
self._telnyx._emitter.emit('response', responseEvent);

try {
response = JSON.parse(response);
response = utils.tryParseJSON(response);

if (response.errors) {
var error = {};
Expand Down
31 changes: 31 additions & 0 deletions lib/resources/TelephonyCredentials.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
'use strict';

var TelnyxResource = require('../TelnyxResource');
var utils = require('../utils');
var telnyxMethod = TelnyxResource.method;

function transformResponseData(response, telnyx) {
return utils.addResourceToResponseData(
response,
telnyx,
'telephonyCredentials'
);
}

module.exports = TelnyxResource.extend({
path: 'telephony_credentials',

create: telnyxMethod({
method: 'POST',

transformResponseData: transformResponseData,
}),

retrieve: telnyxMethod({
method: 'POST',
path: '/{id}/token',
urlParams: ['id'],

transformResponseData: transformResponseData,
}),
});
1 change: 1 addition & 0 deletions lib/telnyx.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ var resources = {
Faxes: require('./resources/Faxes'),
ShortCodes: require('./resources/ShortCodes'),
MessagingProfileMetrics: require('./resources/MessagingProfileMetrics'),
TelephonyCredentials: require('./resources/TelephonyCredentials'),
};

Telnyx.TelnyxResource = require('./TelnyxResource');
Expand Down
52 changes: 38 additions & 14 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -245,20 +245,24 @@ var utils = module.exports = {
* @param [resourceName] Resource name in camelCase
* @param [methods] resource methods to include
*/
addResourceToResponseData: function(response, telnyx, resourceName, methods) {

/*
* make nested methods. e.g.: call.bridge();
* nested methods should be used from basic methods response. See specs for an example
*/
var resourceFulData = telnyx[resourceName];

Object.assign(
resourceFulData,
response.data,
methods
);
response.data = resourceFulData;
addResourceToResponseData: function (
response,
telnyx,
resourceName,
methods
) {
if (response && response.data && typeof response.data === 'object') {

/*
* make nested methods. e.g.: call.bridge();
* nested methods should be used from basic methods response. See specs for an example
*/
var resourceFulData = telnyx[resourceName];

Object.assign(resourceFulData, response.data, methods);

response.data = resourceFulData;
}

return response;
},
Expand All @@ -281,6 +285,8 @@ var utils = module.exports = {
},

emitWarning: emitWarning,

tryParseJSON: tryParseJSON,
};

function emitWarning(warning) {
Expand All @@ -290,3 +296,21 @@ function emitWarning(warning) {

return process.emitWarning(warning, 'Telnyx');
}

/**
* tryParseJSON used to only parse JSON response,
* if it is not a JSON response sends the value inside a data object to keep the standard.
*
* @param [jsonString] Response object
*/
function tryParseJSON (jsonString) {
try {
return JSON.parse(jsonString);
} catch (e) {
const defaultValue = {
data: jsonString
};

return defaultValue;
}
}
1 change: 1 addition & 0 deletions test/resources/Addresses.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ var addressData = {
last_name: 'Foster',
locality: 'Chicago',
postal_code: '2904',
street_address: '311 W Superior Street'
};

describe('Addresses Resource', function () {
Expand Down
57 changes: 57 additions & 0 deletions test/resources/TelephonyCredentials.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
'use strict';

var utils = require('../../testUtils');
var telnyx = utils.getTelnyxMock();
var expect = require('chai').expect;

var TEST_AUTH_KEY = utils.getUserTelnyxKey();

var credentialCreateData = {
credential: {
connection_id: '1474011037387720344',
},
};

describe('TelephonyCredentials Resource', function () {
describe('create', function () {
function responseFn(response) {
expect(response.data).to.have.property('created_at');
expect(response.data).to.have.property('expired');
expect(response.data).to.have.property('expires_at');
expect(response.data).to.have.property('id');
expect(response.data).to.have.property('name');
expect(response.data).to.have.property('record_type');
expect(response.data).to.have.property('resource_id');
expect(response.data).to.have.property('sip_password');
expect(response.data).to.have.property('sip_username');
}

it('Sends the correct request', function () {
return telnyx.telephonyCredentials.create(credentialCreateData).then(responseFn);
});

it('Sends the correct request [with specified auth]', function () {
return telnyx.telephonyCredentials.create(credentialCreateData, TEST_AUTH_KEY).then(responseFn);
});

it('Sends the correct request [with specified auth in options]', function () {
return telnyx.telephonyCredentials
.create(credentialCreateData, {api_key: TEST_AUTH_KEY})
.then(responseFn);
});
});

describe('retrieve', function () {
function responseFn(response) {
expect(response.data).to.not.be.null
}

it('Sends the correct request', function () {
return telnyx.telephonyCredentials.retrieve('9fb9e45f-958a-4d9f-81ff-735ffbcaa133').then(responseFn);
});

it('Sends the correct request [with specified auth]', function () {
return telnyx.telephonyCredentials.retrieve('9fb9e45f-958a-4d9f-81ff-735ffbcaa133', TEST_AUTH_KEY).then(responseFn);
});
});
});

0 comments on commit df4e8cd

Please sign in to comment.