diff options
Diffstat (limited to 'lib/sendgrid.js')
-rw-r--r-- | lib/sendgrid.js | 21 |
1 files changed, 10 insertions, 11 deletions
diff --git a/lib/sendgrid.js b/lib/sendgrid.js index 06d9902..de7e591 100644 --- a/lib/sendgrid.js +++ b/lib/sendgrid.js @@ -2,6 +2,7 @@ 'use strict'; var package_json = require('./../package.json'); var emptyRequest = require('sendgrid-rest').emptyRequest; +var Client = require('sendgrid-rest').Client; var SendGridError = require('./helpers/error'); //Helper to check if response is valid @@ -19,14 +20,8 @@ function getEmptyRequest() { return JSON.parse(JSON.stringify(emptyRequest)); } -//Helper to get empty request -function getEmptyRequest() { - return JSON.parse(JSON.stringify(emptyRequest)); -} - // SendGrid allows for quick and easy access to the v3 Web API function SendGrid(apiKey, host, globalHeaders) { - var Client = require('sendgrid-rest').Client; var globalRequest = getEmptyRequest(); globalRequest.host = host || 'api.sendgrid.com'; globalRequest.headers['Authorization'] = 'Bearer '.concat(apiKey); @@ -35,16 +30,19 @@ function SendGrid(apiKey, host, globalHeaders) { 'sendgrid/' + package_json.version + ';nodejs'; if (globalHeaders) { for (var obj in globalHeaders) { - for (var key in globalHeaders[obj]) { - globalRequest.headers[key] = globalHeaders[obj][key]; + if (globalHeaders.hasOwnProperty(obj) && + typeof globalHeaders[obj] === 'object') { + for (var key in globalHeaders[obj]) { + if (globalHeaders[obj].hasOwnProperty(key)) { + globalRequest.headers[key] = globalHeaders[obj][key]; + } + } } } } var client = new Client(globalRequest); - this.emptyRequest = getEmptyRequest; - - // Interact with the API with this function + //Interact with the API with this function this.API = function(request, callback) { //If no callback provided, we will return a promise @@ -76,6 +74,7 @@ function SendGrid(apiKey, host, globalHeaders) { }); }; + this.emptyRequest = getEmptyRequest; this.globalRequest = globalRequest; return this; } |