summaryrefslogtreecommitdiffstats
path: root/lib/sendgrid.js
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 /lib/sendgrid.js
parent1342ecde82d7af2f5098632b467563b17d9b8d5d (diff)
downloadsendgrid-nodejs-7da7ab726d1b7a3375b6b4b9ceef88755270b0e2.zip
sendgrid-nodejs-7da7ab726d1b7a3375b6b4b9ceef88755270b0e2.tar.gz
sendgrid-nodejs-7da7ab726d1b7a3375b6b4b9ceef88755270b0e2.tar.bz2
New API
Diffstat (limited to 'lib/sendgrid.js')
-rw-r--r--lib/sendgrid.js57
1 files changed, 49 insertions, 8 deletions
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(),
};