diff --git a/lib/index.js b/lib/index.js index 7fe47ee774..0084157694 100644 --- a/lib/index.js +++ b/lib/index.js @@ -11,19 +11,59 @@ var initializer = function(accountSid, authToken, opts) { // Main functional components of the Twilio module initializer.Twilio = Twilio; -initializer.jwt = { - AccessToken: require('./jwt/AccessToken'), - ClientCapability: require('./jwt/ClientCapability'), - taskrouter: { - TaskRouterCapability: require('./jwt/taskrouter/TaskRouterCapability'), - util: require('./jwt/taskrouter/util') - } -}; -initializer.twiml = { - VoiceResponse: require('./twiml/VoiceResponse'), - MessagingResponse: require('./twiml/MessagingResponse'), - FaxResponse: require('./twiml/FaxResponse') -}; +initializer.jwt = { }; +initializer.twiml = { }; + +var AccessToken; +Object.defineProperty(initializer.jwt, + 'AccessToken', { + get: function() { + return AccessToken = AccessToken || require('./jwt/AccessToken'); + } +}); + +var ClientCapability; +Object.defineProperty(initializer.jwt, + 'ClientCapability', { + get: function() { + return ClientCapability = ClientCapability || require('./jwt/ClientCapability'); + } +}); + +var taskrouter; +Object.defineProperty(initializer.jwt, + 'taskrouter', { + get: function() { + return taskrouter = taskrouter || { + TaskRouterCapability: require('./jwt/taskrouter/TaskRouterCapability'), + util: require('./jwt/taskrouter/util') + }; + } +}); + +var VoiceResponse; +Object.defineProperty(initializer.twiml, + 'VoiceResponse', { + get: function() { + return VoiceResponse = VoiceResponse || require('./twiml/VoiceResponse'); + } +}); + +var MessagingResponse; +Object.defineProperty(initializer.twiml, + 'MessagingResponse', { + get: function() { + return MessagingResponse = MessagingResponse || require('./twiml/MessagingResponse'); + } +}); + +var FaxResponse; +Object.defineProperty(initializer.twiml, + 'FaxResponse', { + get: function() { + return FaxResponse = FaxResponse || require('./twiml/FaxResponse'); + } +}); // Add obsolete clients initializer.RestClient = obsolete.RestClient; diff --git a/lib/rest/Twilio.d.ts b/lib/rest/Twilio.d.ts index c50401c205..6c071b6468 100644 --- a/lib/rest/Twilio.d.ts +++ b/lib/rest/Twilio.d.ts @@ -144,12 +144,14 @@ declare namespace Twilio { * @property env - The environment object. Defaults to process.env * @property httpClient - The client used for http requests. Defaults to RequestClient * @property region - Twilio region to use. Defaults to none + * @property lazyLoading - Enable lazy loading or not, loading time will decrease in enabled */ export interface TwilioClientOptions { accountSid?: string; env?: object; httpClient?: RequestClient; region?: string; + lazyLoading?: boolean } } diff --git a/lib/rest/Twilio.js b/lib/rest/Twilio.js index bb0a5a4cbb..918a17f4f3 100644 --- a/lib/rest/Twilio.js +++ b/lib/rest/Twilio.js @@ -12,35 +12,7 @@ var moduleInfo = require('../../package.json'); /* jshint ignore:line */ var _ = require('lodash'); /* jshint ignore:line */ var util = require('util'); /* jshint ignore:line */ -var Accounts = require('./Accounts'); /* jshint ignore:line */ -var Api = require('./Api'); /* jshint ignore:line */ -var Authy = require('./Authy'); /* jshint ignore:line */ -var Autopilot = require('./Autopilot'); /* jshint ignore:line */ -var Chat = require('./Chat'); /* jshint ignore:line */ -var Conversations = require('./Conversations'); /* jshint ignore:line */ -var Fax = require('./Fax'); /* jshint ignore:line */ -var FlexApi = require('./FlexApi'); /* jshint ignore:line */ -var Insights = require('./Insights'); /* jshint ignore:line */ -var IpMessaging = require('./IpMessaging'); /* jshint ignore:line */ -var Lookups = require('./Lookups'); /* jshint ignore:line */ -var Messaging = require('./Messaging'); /* jshint ignore:line */ -var Monitor = require('./Monitor'); /* jshint ignore:line */ -var Notify = require('./Notify'); /* jshint ignore:line */ -var Numbers = require('./Numbers'); /* jshint ignore:line */ -var Preview = require('./Preview'); /* jshint ignore:line */ -var Pricing = require('./Pricing'); /* jshint ignore:line */ -var Proxy = require('./Proxy'); /* jshint ignore:line */ -var RequestClient = require('../base/RequestClient'); /* jshint ignore:line */ -var Serverless = require('./Serverless'); /* jshint ignore:line */ -var Studio = require('./Studio'); /* jshint ignore:line */ -var Sync = require('./Sync'); /* jshint ignore:line */ -var Taskrouter = require('./Taskrouter'); /* jshint ignore:line */ -var Trunking = require('./Trunking'); /* jshint ignore:line */ -var Verify = require('./Verify'); /* jshint ignore:line */ -var Video = require('./Video'); /* jshint ignore:line */ -var Voice = require('./Voice'); /* jshint ignore:line */ -var Wireless = require('./Wireless'); /* jshint ignore:line */ - +var RestException = require('../base/RestException'); /* jshint ignore:line */ /* jshint ignore:start */ /** @@ -129,6 +101,7 @@ var Wireless = require('./Wireless'); /* jshint ignore:line */ * The default accountSid. This is set to username if not provided * @param {object} [opts.env] - The environment object. Defaults to process.env * @param {string} [opts.region] - Twilio region to use. Defaults to none + * @param {boolean} [opts.lazyLoading] - Enable lazy loading or not, loading time will decrease in enabled * * @returns {Twilio} A new instance of Twilio client */ @@ -140,7 +113,10 @@ function Twilio(username, password, opts) { this.username = username || env.TWILIO_ACCOUNT_SID; this.password = password || env.TWILIO_AUTH_TOKEN; this.accountSid = opts.accountSid || this.username; - this.httpClient = opts.httpClient || new RequestClient(); + this._httpClient = opts.httpClient; + if (!opts.lazyLoading) { + this._httpClient = this.httpClient; + } this.region = opts.region; if (!this.username) { @@ -183,6 +159,36 @@ function Twilio(username, password, opts) { this._video = undefined; this._voice = undefined; this._wireless = undefined; + + if (!opts.lazyLoading) { + this._accounts = this.accounts; + this._api = this.api; + this._authy = this.authy; + this._autopilot = this.autopilot; + this._chat = this.chat; + this._conversations = this.conversations; + this._fax = this.fax; + this._flexApi = this.flexApi; + this._insights = this.insights; + this._ipMessaging = this.ipMessaging; + this._lookups = this.lookups; + this._messaging = this.messaging; + this._monitor = this.monitor; + this._notify = this.notify; + this._numbers = this.numbers; + this._preview = this.preview; + this._pricing = this.pricing; + this._proxy = this.proxy; + this._serverless = this.serverless; + this._studio = this.studio; + this._sync = this.sync; + this._taskrouter = this.taskrouter; + this._trunking = this.trunking; + this._verify = this.verify; + this._video = this.video; + this._voice = this.voice; + this._wireless = this.wireless; + } } /* jshint ignore:start */ @@ -273,17 +279,31 @@ Twilio.prototype.validateSslCert = function validateSslCert() { uri: 'https://api.twilio.com:8443/2010-04-01/.json', }).then((response) => { if (response.statusCode < 200 || response.statusCode >= 300) { - throw RestException(response); + throw new RestException(response); } return response; }); }; +Object.defineProperty(Twilio.prototype, + 'httpClient', { + get: function() { + if (!this._httpClient) { + var RequestClient = require('../base/RequestClient'); /* jshint ignore:line */ + this._httpClient = new RequestClient(); + } + return this._httpClient; + } +}); + Object.defineProperty(Twilio.prototype, 'accounts', { get: function() { - this._accounts = this._accounts || new Accounts(this); + if (!this._accounts) { + var Accounts = require('./Accounts'); /* jshint ignore:line */ + this._accounts = new Accounts(this); + } return this._accounts; } }); @@ -291,7 +311,10 @@ Object.defineProperty(Twilio.prototype, Object.defineProperty(Twilio.prototype, 'api', { get: function() { - this._api = this._api || new Api(this); + if (!this._api) { + var Api = require('./Api'); /* jshint ignore:line */ + this._api = new Api(this); + } return this._api; } }); @@ -299,7 +322,10 @@ Object.defineProperty(Twilio.prototype, Object.defineProperty(Twilio.prototype, 'authy', { get: function() { - this._authy = this._authy || new Authy(this); + if (!this._authy) { + var Authy = require('./Authy'); /* jshint ignore:line */ + this._authy = new Authy(this); + } return this._authy; } }); @@ -307,7 +333,10 @@ Object.defineProperty(Twilio.prototype, Object.defineProperty(Twilio.prototype, 'autopilot', { get: function() { - this._autopilot = this._autopilot || new Autopilot(this); + if (!this._autopilot) { + var Autopilot = require('./Autopilot'); /* jshint ignore:line */ + this._autopilot = new Autopilot(this); + } return this._autopilot; } }); @@ -315,7 +344,10 @@ Object.defineProperty(Twilio.prototype, Object.defineProperty(Twilio.prototype, 'chat', { get: function() { - this._chat = this._chat || new Chat(this); + if (!this._chat) { + var Chat = require('./Chat'); /* jshint ignore:line */ + this._chat = new Chat(this); + } return this._chat; } }); @@ -323,7 +355,10 @@ Object.defineProperty(Twilio.prototype, Object.defineProperty(Twilio.prototype, 'conversations', { get: function() { - this._conversations = this._conversations || new Conversations(this); + if (!this._conversations) { + var Conversations = require('./Conversations'); /* jshint ignore:line */ + this._conversations = new Conversations(this); + } return this._conversations; } }); @@ -331,7 +366,10 @@ Object.defineProperty(Twilio.prototype, Object.defineProperty(Twilio.prototype, 'fax', { get: function() { - this._fax = this._fax || new Fax(this); + if (!this._fax) { + var Fax = require('./Fax'); /* jshint ignore:line */ + this._fax = new Fax(this); + } return this._fax; } }); @@ -339,7 +377,10 @@ Object.defineProperty(Twilio.prototype, Object.defineProperty(Twilio.prototype, 'flexApi', { get: function() { - this._flexApi = this._flexApi || new FlexApi(this); + if (!this._flexApi) { + var FlexApi = require('./FlexApi'); /* jshint ignore:line */ + this._flexApi = new FlexApi(this); + } return this._flexApi; } }); @@ -347,7 +388,10 @@ Object.defineProperty(Twilio.prototype, Object.defineProperty(Twilio.prototype, 'insights', { get: function() { - this._insights = this._insights || new Insights(this); + if (!this._insights) { + var Insights = require('./Insights'); /* jshint ignore:line */ + this._insights = new Insights(this); + } return this._insights; } }); @@ -355,7 +399,10 @@ Object.defineProperty(Twilio.prototype, Object.defineProperty(Twilio.prototype, 'ipMessaging', { get: function() { - this._ipMessaging = this._ipMessaging || new IpMessaging(this); + if (!this._ipMessaging) { + var IpMessaging = require('./IpMessaging'); /* jshint ignore:line */ + this._ipMessaging = new IpMessaging(this); + } return this._ipMessaging; } }); @@ -363,7 +410,10 @@ Object.defineProperty(Twilio.prototype, Object.defineProperty(Twilio.prototype, 'lookups', { get: function() { - this._lookups = this._lookups || new Lookups(this); + if (!this._lookups) { + var Lookups = require('./Lookups'); /* jshint ignore:line */ + this._lookups = new Lookups(this); + } return this._lookups; } }); @@ -371,7 +421,10 @@ Object.defineProperty(Twilio.prototype, Object.defineProperty(Twilio.prototype, 'messaging', { get: function() { - this._messaging = this._messaging || new Messaging(this); + if (!this._messaging) { + var Messaging = require('./Messaging'); /* jshint ignore:line */ + this._messaging = new Messaging(this); + } return this._messaging; } }); @@ -379,7 +432,10 @@ Object.defineProperty(Twilio.prototype, Object.defineProperty(Twilio.prototype, 'monitor', { get: function() { - this._monitor = this._monitor || new Monitor(this); + if (!this._monitor) { + var Monitor = require('./Monitor'); /* jshint ignore:line */ + this._monitor = new Monitor(this); + } return this._monitor; } }); @@ -387,7 +443,10 @@ Object.defineProperty(Twilio.prototype, Object.defineProperty(Twilio.prototype, 'notify', { get: function() { - this._notify = this._notify || new Notify(this); + if (!this._notify) { + var Notify = require('./Notify'); /* jshint ignore:line */ + this._notify = new Notify(this); + } return this._notify; } }); @@ -395,7 +454,10 @@ Object.defineProperty(Twilio.prototype, Object.defineProperty(Twilio.prototype, 'numbers', { get: function() { - this._numbers = this._numbers || new Numbers(this); + if (!this._numbers) { + var Numbers = require('./Numbers'); /* jshint ignore:line */ + this._numbers = new Numbers(this); + } return this._numbers; } }); @@ -403,7 +465,10 @@ Object.defineProperty(Twilio.prototype, Object.defineProperty(Twilio.prototype, 'preview', { get: function() { - this._preview = this._preview || new Preview(this); + if (!this._preview) { + var Preview = require('./Preview'); /* jshint ignore:line */ + this._preview = new Preview(this); + } return this._preview; } }); @@ -411,7 +476,10 @@ Object.defineProperty(Twilio.prototype, Object.defineProperty(Twilio.prototype, 'pricing', { get: function() { - this._pricing = this._pricing || new Pricing(this); + if (!this._pricing) { + var Pricing = require('./Pricing'); /* jshint ignore:line */ + this._pricing = new Pricing(this); + } return this._pricing; } }); @@ -419,7 +487,10 @@ Object.defineProperty(Twilio.prototype, Object.defineProperty(Twilio.prototype, 'proxy', { get: function() { - this._proxy = this._proxy || new Proxy(this); + if (!this._proxy) { + var Proxy = require('./Proxy'); /* jshint ignore:line */ + this._proxy = new Proxy(this); + } return this._proxy; } }); @@ -427,7 +498,10 @@ Object.defineProperty(Twilio.prototype, Object.defineProperty(Twilio.prototype, 'serverless', { get: function() { - this._serverless = this._serverless || new Serverless(this); + if (!this._serverless) { + var Serverless = require('./Serverless'); /* jshint ignore:line */ + this._serverless = new Serverless(this); + } return this._serverless; } }); @@ -435,7 +509,10 @@ Object.defineProperty(Twilio.prototype, Object.defineProperty(Twilio.prototype, 'studio', { get: function() { - this._studio = this._studio || new Studio(this); + if (!this._studio) { + var Studio = require('./Studio'); /* jshint ignore:line */ + this._studio = new Studio(this); + } return this._studio; } }); @@ -443,7 +520,10 @@ Object.defineProperty(Twilio.prototype, Object.defineProperty(Twilio.prototype, 'sync', { get: function() { - this._sync = this._sync || new Sync(this); + if (!this._sync) { + var Sync = require('./Sync'); /* jshint ignore:line */ + this._sync = new Sync(this); + } return this._sync; } }); @@ -451,7 +531,10 @@ Object.defineProperty(Twilio.prototype, Object.defineProperty(Twilio.prototype, 'taskrouter', { get: function() { - this._taskrouter = this._taskrouter || new Taskrouter(this); + if (!this._taskrouter) { + var Taskrouter = require('./Taskrouter'); /* jshint ignore:line */ + this._taskrouter = new Taskrouter(this); + } return this._taskrouter; } }); @@ -459,7 +542,10 @@ Object.defineProperty(Twilio.prototype, Object.defineProperty(Twilio.prototype, 'trunking', { get: function() { - this._trunking = this._trunking || new Trunking(this); + if (!this._trunking) { + var Trunking = require('./Trunking'); /* jshint ignore:line */ + this._trunking = new Trunking(this); + } return this._trunking; } }); @@ -467,7 +553,10 @@ Object.defineProperty(Twilio.prototype, Object.defineProperty(Twilio.prototype, 'verify', { get: function() { - this._verify = this._verify || new Verify(this); + if (!this._verify) { + var Verify = require('./Verify'); /* jshint ignore:line */ + this._verify = new Verify(this); + } return this._verify; } }); @@ -475,7 +564,10 @@ Object.defineProperty(Twilio.prototype, Object.defineProperty(Twilio.prototype, 'video', { get: function() { - this._video = this._video || new Video(this); + if (!this._video) { + var Video = require('./Video'); /* jshint ignore:line */ + this._video = new Video(this); + } return this._video; } }); @@ -483,7 +575,10 @@ Object.defineProperty(Twilio.prototype, Object.defineProperty(Twilio.prototype, 'voice', { get: function() { - this._voice = this._voice || new Voice(this); + if (!this._voice) { + var Voice = require('./Voice'); /* jshint ignore:line */ + this._voice = new Voice(this); + } return this._voice; } }); @@ -491,7 +586,10 @@ Object.defineProperty(Twilio.prototype, Object.defineProperty(Twilio.prototype, 'wireless', { get: function() { - this._wireless = this._wireless || new Wireless(this); + if (!this._wireless) { + var Wireless = require('./Wireless'); /* jshint ignore:line */ + this._wireless = new Wireless(this); + } return this._wireless; } });