Skip to content

Commit

Permalink
New API
Browse files Browse the repository at this point in the history
  • Loading branch information
Adam Buczynski committed Jul 26, 2016
1 parent 1342ecd commit 7da7ab7
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 12 deletions.
3 changes: 3 additions & 0 deletions .eslintignore
@@ -0,0 +1,3 @@
node_modules
examples
test
4 changes: 2 additions & 2 deletions index.js
@@ -1,2 +1,2 @@
exports = module.exports = require("./lib/sendgrid");
exports.mail = require("./lib/helpers/mail/mail.js")
exports = module.exports = require('./lib/sendgrid');
exports.mail = require('./lib/helpers/mail/mail.js');
20 changes: 20 additions & 0 deletions 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;
57 changes: 49 additions & 8 deletions lib/sendgrid.js
Expand Up @@ -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';
Expand All @@ -21,23 +37,48 @@ 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);
}
});
};

this.globalRequest = globalRequest;
return this;
}

module.exports =
{
//Try to use native promises by default
SendGrid.Promise = Promise || null;

module.exports = {
SendGrid: SendGrid,
emptyRequest: SendGrid.emptyRequest(),
emptyRequest: getEmptyRequest(),
};
6 changes: 4 additions & 2 deletions package.json
Expand Up @@ -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.",
Expand All @@ -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": [
Expand Down

0 comments on commit 7da7ab7

Please sign in to comment.