summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorAdam Buczynski <adambuczynski@gmail.com>2016-07-27 12:54:31 +1200
committerAdam Buczynski <adambuczynski@gmail.com>2016-07-27 12:54:31 +1200
commitbc6295358f4424e343ed9377cc7f0ffdb17dc98c (patch)
treeeedc3c853fa30dcbd376cef0d2dafc43cae3c39e /lib
parentd051d8f055631619f51719648f7e6e29005623fd (diff)
parentea4e9fd24a7cd715f24027cc34f588cc64597682 (diff)
downloadsendgrid-nodejs-bc6295358f4424e343ed9377cc7f0ffdb17dc98c.zip
sendgrid-nodejs-bc6295358f4424e343ed9377cc7f0ffdb17dc98c.tar.gz
sendgrid-nodejs-bc6295358f4424e343ed9377cc7f0ffdb17dc98c.tar.bz2
Merge branch 'callback-function-signature' into eslint
Diffstat (limited to 'lib')
-rw-r--r--lib/helpers/error.js20
-rw-r--r--lib/sendgrid.js111
2 files changed, 111 insertions, 20 deletions
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 d083c2a..d497058 100644
--- a/lib/sendgrid.js
+++ b/lib/sendgrid.js
@@ -2,45 +2,116 @@
'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 get empty request
-function getEmptyRequest() {
- return JSON.parse(JSON.stringify(emptyRequest));
+/**
+ * Helper to check if response is valid
+ */
+function isValidResponse(response) {
+ return (
+ response &&
+ response.statusCode &&
+ response.statusCode >= 200 &&
+ response.statusCode <= 299
+ );
}
-// 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);
- globalRequest.headers['Accept'] = 'application/json';
- globalRequest.headers['User-Agent'] =
- 'sendgrid/' + package_json.version + ';nodejs';
+/**
+ * Helper to get a new empty request
+ */
+function getEmptyRequest(data) {
+ let request = JSON.parse(JSON.stringify(emptyRequest));
+ if (data && typeof data === 'object') {
+ for (var key in data) {
+ if (data.hasOwnProperty(key)) {
+ request[key] = JSON.parse(JSON.stringify(data[key]));
+ }
+ }
+ }
+ return request;
+}
+
+/**
+ * Helper to make headers
+ */
+function makeHeaders(apiKey, globalHeaders) {
+ var headers = {};
+ headers['Authorization'] = 'Bearer '.concat(apiKey);
+ headers['Accept'] = 'application/json';
+ headers['User-Agent'] = '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)) {
+ headers[key] = globalHeaders[obj][key];
+ }
+ }
}
}
}
- var client = new Client(globalRequest);
+ return headers;
+}
- this.emptyRequest = getEmptyRequest;
+/**
+ * SendGrid allows for quick and easy access to the v3 Web API
+ */
+function SendGrid(apiKey, host, globalHeaders) {
+
+ //Create global request
+ var globalRequest = getEmptyRequest({
+ host: host || 'api.sendgrid.com',
+ headers: makeHeaders(apiKey, globalHeaders),
+ });
- // Interact with the API with this function
+ //Initialize new client
+ var client = new Client(globalRequest);
+
+ //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);
+ }
});
};
+ //Set requests
+ this.emptyRequest = getEmptyRequest;
this.globalRequest = globalRequest;
return this;
}
-module.exports =
-{
+//Try to use native promises by default
+SendGrid.Promise = Promise || null;
+
+//Export
+module.exports = {
SendGrid: SendGrid,
emptyRequest: getEmptyRequest(),
};