summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAdam Buczynski <adambuczynski@gmail.com>2016-07-27 11:29:25 +1200
committerAdam Buczynski <adambuczynski@gmail.com>2016-07-27 11:29:25 +1200
commit7da7ab726d1b7a3375b6b4b9ceef88755270b0e2 (patch)
treeb9f4020f12ccdece33ed18e64a353bb650b438d1
parent1342ecde82d7af2f5098632b467563b17d9b8d5d (diff)
downloadsendgrid-nodejs-7da7ab726d1b7a3375b6b4b9ceef88755270b0e2.zip
sendgrid-nodejs-7da7ab726d1b7a3375b6b4b9ceef88755270b0e2.tar.gz
sendgrid-nodejs-7da7ab726d1b7a3375b6b4b9ceef88755270b0e2.tar.bz2
New API
-rw-r--r--.eslintignore3
-rw-r--r--index.js4
-rw-r--r--lib/helpers/error.js20
-rw-r--r--lib/sendgrid.js57
-rw-r--r--package.json6
5 files changed, 78 insertions, 12 deletions
diff --git a/.eslintignore b/.eslintignore
new file mode 100644
index 0000000..15d3540
--- /dev/null
+++ b/.eslintignore
@@ -0,0 +1,3 @@
+node_modules
+examples
+test
diff --git a/index.js b/index.js
index b3feaca..4473136 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 0000000..2beb886
--- /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 2c5cb6b..08f8646 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 9d5b70b..bce6e02 100644
--- a/package.json
+++ b/package.json
@@ -7,7 +7,8 @@
"Brandon West <brandon.west@sendgrid.com>",
"Scott Motte <scott.motte@sendgrid.com>",
"Robert Acosta <robert.acosta@sendgrid.com>",
- "Elmer Thomas <elmer.thomas@sendgrid.com>"
+ "Elmer Thomas <elmer.thomas@sendgrid.com>",
+ "Adam Buczynski <me@adambuczynski.com>"
],
"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": [