Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add regional support #563

Merged
merged 16 commits into from
Apr 27, 2020
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion lib/rest/Twilio.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,13 +146,15 @@ declare namespace Twilio {
* Options to pass to the Twilio Client constructor
*
* @property accountSid - The default accountSid. This is set to username if not provided
* @property edge - Twilio region to use. Defaults to none
thinkingserious marked this conversation as resolved.
Show resolved Hide resolved
* @property env - The environment object. Defaults to process.env
* @property httpClient - The client used for http requests. Defaults to RequestClient
* @property lazyLoading - Enable lazy loading, loading time will decrease if enabled
* @property region - Twilio region to use. Defaults to none
* @property region - Twilio region to use. Defaults to defaultRegion
*/
export interface TwilioClientOptions {
accountSid?: string;
edge?: string;
env?: object;
httpClient?: RequestClient;
lazyLoading?: boolean;
Expand Down
44 changes: 34 additions & 10 deletions lib/rest/Twilio.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
/* jshint ignore:end */

var moduleInfo = require('../../package.json'); /* jshint ignore:line */
var _ = require('lodash'); /* jshint ignore:line */
var url = require('url'); /* jshint ignore:line */
var util = require('util'); /* jshint ignore:line */
var RestException = require('../base/RestException'); /* jshint ignore:line */

Expand Down Expand Up @@ -102,8 +102,9 @@ var RestException = require('../base/RestException'); /* jshint ignore:line */
* The client used for http requests. Defaults to RequestClient
* @param {string} [opts.accountSid] -
* The default accountSid. This is set to username if not provided
* @param {string} [opts.edge] - Twilio region to use. Defaults to none
thinkingserious marked this conversation as resolved.
Show resolved Hide resolved
* @param {object} [opts.env] - The environment object. Defaults to process.env
* @param {string} [opts.region] - Twilio region to use. Defaults to none
* @param {string} [opts.region] - Twilio region to use. Defaults to defaultRegion
thinkingserious marked this conversation as resolved.
Show resolved Hide resolved
* @param {boolean} [opts.lazyLoading] -
* Enable lazy loading, loading time will decrease if enabled
*
Expand All @@ -122,6 +123,7 @@ function Twilio(username, password, opts) {
this._httpClient = this.httpClient;
}
this.region = opts.region;
this.edge = opts.edge;

if (!this.username) {
throw new Error('username is required');
Expand All @@ -131,7 +133,7 @@ function Twilio(username, password, opts) {
throw new Error('password is required');
}

if (!_.startsWith(this.accountSid, 'AC')) {
if (!this.accountSid.startsWith('AC')) {
throw new Error('accountSid must start with AC');
}

Expand Down Expand Up @@ -220,6 +222,7 @@ function Twilio(username, password, opts) {
*/
/* jshint ignore:end */
Twilio.prototype.request = function request(opts) {
const defaultRegion = 'us1';
opts = opts || {};

if (!opts.method) {
Expand Down Expand Up @@ -249,18 +252,39 @@ Twilio.prototype.request = function request(opts) {
headers.Accept = 'application/json';
}

var uri = opts.uri;
if (this.region) {
var parts = _.split(uri, '.');

if (parts.length > 1 && !_.isEqual(parts[1], this.region)) {
uri = _.join(_.concat([parts[0], this.region], _.slice(parts, 1)), '.');
var product;
var edge;
var region;
var subdomain;
var domain;
var uri = new url.URL(opts.uri);
var parsedHostname = uri.hostname.split('.');
if (parsedHostname.length === 5) { // https://product.edge.region.subdomain.domain
thinkingserious marked this conversation as resolved.
Show resolved Hide resolved
[product, edge, region, subdomain, domain] = uri.hostname.split('.');
if (this.edge) {
uri.hostname = [product, this.edge || edge, this.region || region, subdomain, domain].join('.');
} else if (this.region) {
uri.hostname = [product, this.edge || edge, this.region, subdomain, domain].join('.');
}
} else if (parsedHostname.length === 4) { // https://product.region.subdomain.domain
[product, region, subdomain, domain] = uri.hostname.split('.');
if (this.edge) {
uri.hostname = [product, this.edge, this.region || region, subdomain, domain].join('.');
} else if (this.region) {
uri.hostname = [product, this.region || region, subdomain, domain].join('.');
}
} else if (parsedHostname.length === 3) { // https://product.subdomain.domain
[product, subdomain, domain] = uri.hostname.split('.');
if (this.edge) {
uri.hostname = [product, this.edge, this.region || defaultRegion, subdomain, domain].join('.');
} else if (this.region) {
uri.hostname = [product, this.region, subdomain, domain].join('.');
}
}

return this.httpClient.request({
method: opts.method,
uri: uri,
uri: uri.href,
username: username,
password: password,
headers: headers,
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
"jsdoc": "^3.6.3",
"jshint": "^2.11.0",
"mock-fs": "^4.11.0",
"nock": "^12.0.3",
thinkingserious marked this conversation as resolved.
Show resolved Hide resolved
"node-mocks-http": "^1.8.1",
"proxyquire": "^2.1.3",
"typescript": "^2.8.3"
Expand Down
59 changes: 59 additions & 0 deletions spec/unit/rest/Twilio.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
'use strict';
const nock = require('nock');
var url = require('url'); /* jshint ignore:line */

describe('client', () => {
var client;

beforeEach(() => {
var twilio = require('../../../lib');
client = new twilio('ACXXXXXXXX', 'test-password');
nock('https://api.twilio.com')
.get('/')
.reply(200, 'test response');
});

describe('setting the region', () => {
it('should use the default region if only edge is defined', () => {
client.edge = 'edge';
client.messages.create({
body: 'Hello from Node',
to: '+XXXXXXXXXX',
from: '+XXXXXXXXXX',
});
var uri = new url.URL(client._httpClient.lastRequest.url);
thinkingserious marked this conversation as resolved.
Show resolved Hide resolved
expect(uri.hostname).toEqual('api.edge.us1.twilio.com');
});
it('should set the region properly if only the region is specified', () => {
client.region = 'region';
client.messages.create({
body: 'Hello from Node',
to: '+XXXXXXXXXX',
from: '+XXXXXXXXXX',
});
var uri = new url.URL(client._httpClient.lastRequest.url);
expect(uri.hostname).toEqual('api.region.twilio.com');
});
it('should use the default region if only edge is defined', () => {
client.edge = 'edge';
client.messages.create({
body: 'Hello from Node',
to: '+XXXXXXXXXX',
from: '+XXXXXXXXXX',
});
var uri = new url.URL(client._httpClient.lastRequest.url);
expect(uri.hostname).toEqual('api.edge.us1.twilio.com');
});
it('should set the region and edge properly', () => {
client.edge = 'edge';
client.region = 'region';
client.messages.create({
body: 'Hello from Node',
to: '+XXXXXXXXXX',
from: '+XXXXXXXXXX',
});
var uri = new url.URL(client._httpClient.lastRequest.url);
expect(uri.hostname).toEqual('api.edge.region.twilio.com');
thinkingserious marked this conversation as resolved.
Show resolved Hide resolved
});
});
});