diff --git a/.eslintignore b/.eslintignore new file mode 100644 index 000000000..15d354055 --- /dev/null +++ b/.eslintignore @@ -0,0 +1,3 @@ +node_modules +examples +test diff --git a/index.js b/index.js index b3feaca03..447313671 100644 --- a/index.js +++ b/index.js @@ -1,2 +1,2 @@ -exports = module.exports = require("./lib/sendgrid"); -exports.mail = require("./lib/helpers/mail/mail.js") \ No newline at end of file +exports = module.exports = require('./lib/sendgrid'); +exports.mail = require('./lib/helpers/mail/mail.js'); diff --git a/lib/helpers/error.js b/lib/helpers/error.js new file mode 100644 index 000000000..2beb88626 --- /dev/null +++ b/lib/helpers/error.js @@ -0,0 +1,20 @@ +'use strict'; + +//Error constructor +function SendGridError(message) { + this.message = message; + if (Error.captureStackTrace) { + Error.captureStackTrace(this, this.constructor); + } + else { + this.stack = (new Error()).stack; + } +} + +//Extend prototype +SendGridError.prototype = new Error(); +SendGridError.prototype.constructor = SendGridError; +SendGridError.prototype.name = 'SendGridError'; + +//Export +module.exports = SendGridError; diff --git a/lib/sendgrid.js b/lib/sendgrid.js index 2c5cb6b8c..08f86467a 100644 --- a/lib/sendgrid.js +++ b/lib/sendgrid.js @@ -2,11 +2,27 @@ 'use strict'; var package_json = require('./../package.json'); var emptyRequest = require('sendgrid-rest').emptyRequest; +var SendGridError = require('./helpers/error'); + +//Helper to check if response is valid +function isValidResponse(response) { + return ( + response && + response.statusCode && + response.statusCode >= 200 && + response.statusCode <= 299 + ); +} + +//Helper to get a new 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 = this.emptyRequest(); + var globalRequest = getEmptyRequest(); globalRequest.host = host || 'api.sendgrid.com'; globalRequest.headers['Authorization'] = 'Bearer '.concat(apiKey); globalRequest.headers['Accept'] = 'application/json'; @@ -21,14 +37,37 @@ function SendGrid(apiKey, host, globalHeaders) { } var client = new Client(globalRequest); - this.emptyRequest = function() { - return JSON.parse(JSON.stringify(emptyRequest)); - }; + this.emptyRequest = getEmptyRequest; // Interact with the API with this function this.API = function(request, callback) { + + //If no callback provided, we will return a promise + if (!callback) { + if (!SendGrid.Promise) { + throw new SendGridError('Promise API not supported'); + } + return new SendGrid.Promise(function(resolve, reject) { + client.API(request, function(response) { + if (isValidResponse(response)) { + resolve(response); + } + else { + reject(response); + } + }); + }); + } + + //Use callback client.API(request, function(response) { - callback(response); + if (isValidResponse(response)) { + callback(null, response); + } + else { + var error = new SendGridError('Response error'); + callback(error, response); + } }); }; @@ -36,8 +75,10 @@ function SendGrid(apiKey, host, globalHeaders) { return this; } -module.exports = -{ +//Try to use native promises by default +SendGrid.Promise = Promise || null; + +module.exports = { SendGrid: SendGrid, - emptyRequest: SendGrid.emptyRequest(), + emptyRequest: getEmptyRequest(), }; diff --git a/package.json b/package.json index 9d5b70ba1..bce6e0226 100644 --- a/package.json +++ b/package.json @@ -7,7 +7,8 @@ "Brandon West ", "Scott Motte ", "Robert Acosta ", - "Elmer Thomas " + "Elmer Thomas ", + "Adam Buczynski " ], "name": "sendgrid", "description": "Official SendGrid NodeJS library.", @@ -25,12 +26,13 @@ }, "devDependencies": { "chai": "^3.5.0", - "eslint": "^2.7.0", + "eslint": "^3.1.0", "eslint-config-standard": "^5.1.0", "eslint-plugin-standard": "^1.3.2", "mocha": "^2.4.5" }, "scripts": { + "pretest": "eslint . --fix", "test": "mocha" }, "tags": [