Comparing version 1.10.0 to 1.11.0
var jwt = require('jwt-simple'), | ||
qs = require('querystring'); | ||
qs = require('querystring'), | ||
utils = require('./utils'); | ||
function Capability(sid, tkn) { | ||
// Initialize from environment variables, if present | ||
if (!sid || !tkn) { | ||
if (process.env.TWILIO_ACCOUNT_SID && process.env.TWILIO_AUTH_TOKEN) { | ||
this.accountSid = process.env.TWILIO_ACCOUNT_SID; | ||
this.authToken = process.env.TWILIO_AUTH_TOKEN; | ||
} | ||
else { | ||
throw 'Capability requires an Account SID and Auth Token set explicitly ' + | ||
'or via the TWILIO_ACCOUNT_SID and TWILIO_AUTH_TOKEN environment variables'; | ||
} | ||
} | ||
else { | ||
//if auth token/SID passed in manually, trim spaces | ||
this.accountSid = sid.replace(/ /g,''); | ||
this.authToken = tkn.replace(/ /g,''); | ||
} | ||
utils.initializeTokens(this, 'Capability', sid, tkn); | ||
this.capabilities = []; | ||
@@ -82,2 +68,2 @@ } | ||
module.exports = Capability; | ||
module.exports = Capability; |
@@ -10,3 +10,4 @@ /** | ||
var webhooks = require('./webhooks'), | ||
RestClient = require('./RestClient'); | ||
RestClient = require('./RestClient'), | ||
TaskRouterClient = require('./TaskRouterClient'); | ||
@@ -20,3 +21,5 @@ //Shorthand to automatically create a RestClient | ||
initializer.RestClient = RestClient; | ||
initializer.TaskRouterClient = TaskRouterClient; | ||
initializer.Capability = require('./Capability'); | ||
initializer.TaskRouterCapability = require('./TaskRouterCapability'); | ||
initializer.TwimlResponse = require('./TwimlResponse'); | ||
@@ -23,0 +26,0 @@ |
@@ -24,6 +24,2 @@ /** | ||
messages: require('./Messages')(client, sid), | ||
media:ListInstanceResource(client, sid, 'Media', | ||
['GET', 'DELETE'], | ||
['GET'] | ||
), | ||
sms:{ | ||
@@ -30,0 +26,0 @@ messages:ListInstanceResource(client, sid, 'SMS/Messages', |
@@ -10,12 +10,6 @@ /** | ||
//Dependencies | ||
var Q = require('q'), | ||
querystring = require('querystring'), | ||
request = require('request'), | ||
moduleinfo = require('../package.json'), | ||
_ = require('underscore'); | ||
var _ = require('underscore'); | ||
var Client = require('./Client'); | ||
var util = require('util'); | ||
//REST API Config Defaults | ||
var defaultHost = 'api.twilio.com', | ||
defaultApiVersion = '2010-04-01'; | ||
/** | ||
@@ -31,24 +25,4 @@ The Twilio REST API client | ||
function RestClient(sid, tkn, options) { | ||
//Required client config | ||
if (!sid || !tkn) { | ||
if (process.env.TWILIO_ACCOUNT_SID && process.env.TWILIO_AUTH_TOKEN) { | ||
this.accountSid = process.env.TWILIO_ACCOUNT_SID; | ||
this.authToken = process.env.TWILIO_AUTH_TOKEN; | ||
} | ||
else { | ||
throw 'RestClient requires an Account SID and Auth Token set explicitly ' + | ||
'or via the TWILIO_ACCOUNT_SID and TWILIO_AUTH_TOKEN environment variables'; | ||
} | ||
} | ||
else { | ||
//if auth token/SID passed in manually, trim spaces | ||
this.accountSid = sid.replace(/ /g,''); | ||
this.authToken = tkn.replace(/ /g,''); | ||
} | ||
//Optional client config | ||
options = options || {}; | ||
this.host = options.host || defaultHost; | ||
this.apiVersion = options.apiVersion || defaultApiVersion; | ||
this.timeout = options.timeout || 31000; // request timeout in milliseconds | ||
RestClient.super_.call(this, sid, tkn, options.host, options.apiVersion, options.timeout); | ||
@@ -84,125 +58,12 @@ //REST Resource - shorthand for just "account" and "accounts" to match the REST API | ||
//process data and make available in a more JavaScripty format | ||
function processKeys(source) { | ||
if (_.isObject(source)) { | ||
Object.keys(source).forEach(function(key) { | ||
util.inherits(RestClient, Client); | ||
//Supplement underscore values with camel-case | ||
if (key.indexOf('_') > 0) { | ||
var cc = key.replace(/_([a-z])/g, function (g) { | ||
return g[1].toUpperCase() | ||
}); | ||
source[cc] = source[key]; | ||
} | ||
RestClient.prototype.request = function(options, callback) { | ||
var client = this; | ||
//process any nested arrays... | ||
if (Array.isArray(source[key])) { | ||
source[key].forEach(processKeys); | ||
} | ||
else if (_.isObject(source[key])) { | ||
processKeys(source[key]); | ||
} | ||
}); | ||
//Look for and convert date strings for specific keys | ||
['startDate', 'endDate', 'dateCreated', 'dateUpdated', 'startTime', 'endTime'].forEach(function(dateKey) { | ||
if (source[dateKey]) { | ||
source[dateKey] = new Date(source[dateKey]); | ||
} | ||
}); | ||
} | ||
} | ||
/** | ||
Get the base URL which we'll use for all requests with this client | ||
@returns {string} - the API base URL | ||
*/ | ||
RestClient.prototype.getBaseUrl = function () { | ||
return 'https://' + this.accountSid + ':' + this.authToken + '@' + this.host + '/' + this.apiVersion; | ||
// Force .json for Coke Classic API | ||
options.url = options.url + '.json'; | ||
return RestClient.super_.prototype.request.call(this, options, callback); | ||
}; | ||
/** | ||
Make an authenticated request against the Twilio backend. Uses the request | ||
library, and largely passes through to its API for options: | ||
https://github.com/mikeal/request | ||
@param {object} options - options for HTTP request | ||
@param {function} callback - callback function for when request is complete | ||
- @param {object} error - an error object if there was a problem processing the request | ||
- @param {object} data - the JSON-parsed data | ||
- @param {http.ClientResponse} response - the raw node http.ClientResponse object | ||
*/ | ||
RestClient.prototype.request = function (options, callback) { | ||
var client = this, | ||
deferred = Q.defer(); | ||
//Prepare request options | ||
options.url = client.getBaseUrl() + options.url + '.json'; | ||
options.headers = { | ||
'Accept':'application/json', | ||
'Accept-Charset': 'utf-8', | ||
'User-Agent':'twilio-node/' + moduleinfo.version | ||
}; | ||
options.timeout = client.timeout; | ||
// Manually create POST body if there's a form object. Sadly, request | ||
// turns multiple key parameters into array-ified queries, like this: | ||
// MediaUrl[0]=foo&MediaUrl[1]=bar. Node querystring does the right thing so | ||
// we use that here. Also see https://github.com/mikeal/request/issues/644 | ||
if (options.form) { | ||
options.body = querystring.stringify(options.form).toString('utf-8'); | ||
options.headers['Content-Type'] = 'application/x-www-form-urlencoded; charset=utf-8'; | ||
options.form = null; | ||
} | ||
//Initiate HTTP request | ||
request(options, function (err, response, body) { | ||
var data; | ||
try { | ||
data = err || !body ? {status: 500, message: 'Empty body'} : JSON.parse(body); | ||
} catch (e) { | ||
data = { status: 500, message: (e.message || 'Invalid JSON body') }; | ||
} | ||
//request doesn't think 4xx is an error - we want an error for any non-2xx status codes | ||
var error = null; | ||
if (err || (response && (response.statusCode < 200 || response.statusCode > 206))) { | ||
error = {}; | ||
// response is null if server is unreachable | ||
if (response) { | ||
error.status = response.statusCode; | ||
error.message = data ? data.message : 'Unable to complete HTTP request'; | ||
error.code = data && data.code; | ||
error.moreInfo = data && data.more_info; | ||
} else { | ||
error.status = err.code; | ||
error.message = 'Unable to reach host: "'+client.host+'"'; | ||
} | ||
} | ||
processKeys(data); | ||
//hang response off the JSON-serialized data, as unenumerable to allow for stringify. | ||
Object.defineProperty(data, 'nodeClientResponse', { | ||
value: response, | ||
configurable: true, | ||
writeable: true, | ||
enumerable: false | ||
}); | ||
// Resolve promise | ||
if (error) { | ||
deferred.reject(error); | ||
} else { | ||
deferred.resolve(data); | ||
} | ||
}); | ||
// Return promise, but also support original node callback style | ||
return deferred.promise.nodeify(callback); | ||
}; | ||
module.exports = RestClient; |
var crypto = require('crypto'), | ||
_ = require('underscore'), | ||
scmp = require('scmp'); | ||
scmp = require('scmp'), | ||
url = require('url'); | ||
@@ -33,6 +34,6 @@ /** | ||
exports.validateExpressRequest = function(request, authToken, opts) { | ||
var options = opts||{}, url; | ||
var options = opts||{}, webhookUrl; | ||
if (options.url) { | ||
// Let the user specify the full URL | ||
url = options.url; | ||
webhookUrl = options.url; | ||
} else { | ||
@@ -42,3 +43,7 @@ // Use configured host/protocol, or infer based on request | ||
var host = options.host||request.headers.host; | ||
url = protocol + '://' + host + request.originalUrl; | ||
webhookUrl = url.format({ | ||
protocol: protocol, | ||
host: host, | ||
pathname: request.originalUrl | ||
}); | ||
} | ||
@@ -49,3 +54,3 @@ | ||
request.header('X-Twilio-Signature'), | ||
url, | ||
webhookUrl, | ||
request.body||{} | ||
@@ -52,0 +57,0 @@ ); |
{ | ||
"name": "twilio", | ||
"description": "A Twilio helper library", | ||
"version": "1.10.0", | ||
"version": "1.11.0", | ||
"author": "Kevin Whinnery <kevin.whinnery@gmail.com>", | ||
@@ -6,0 +6,0 @@ "contributors": [ |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
72729
35
1443
7