summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorNick Quinlan <nicholas.quinlan@sendgrid.com>2014-06-23 10:29:08 -0700
committerNick Quinlan <nicholas.quinlan@sendgrid.com>2014-06-23 10:29:08 -0700
commite61b7a4ab49b1a1565c613cfd26ade7cba24ee13 (patch)
tree5b8edce1cf77fe4b20204cadaee546ee1c7a412f
parent2d3ad204c88e3d3f9ca0d1c08388f028fea2efdf (diff)
downloadsendgrid-nodejs-e61b7a4ab49b1a1565c613cfd26ade7cba24ee13.zip
sendgrid-nodejs-e61b7a4ab49b1a1565c613cfd26ade7cba24ee13.tar.gz
sendgrid-nodejs-e61b7a4ab49b1a1565c613cfd26ade7cba24ee13.tar.bz2
added ability to change uri via options
this brings the library in line with: https://github.com/scottmotte/sendgrid-opensource-proposals/blob/master/proposals/PORT.md as this uses parameters that also could be used by request, this may cause problems with those using the protocol parameter (however, I believe that any use of the protocol parameter in the past, would not have worked, anyway)
-rw-r--r--README.md15
-rw-r--r--lib/sendgrid.js19
-rw-r--r--test/lib/sendgrid.test.js10
3 files changed, 41 insertions, 3 deletions
diff --git a/README.md b/README.md
index 8284886..4736e16 100644
--- a/README.md
+++ b/README.md
@@ -351,6 +351,21 @@ email.addHtml('<div>Our logo:<img src="cid:the_logo"></div>');
## Options
+### Changing URL
+You may change the URL sendgrid-nodejs uses to send email by supplying various parameters to `options`, all parameters are optional:
+
+```javascript
+var sendgrid = require('sendgrid')('username', 'password', { "protocol" : "http", "host" : "sendgrid.org", "endpoint" : "/send", "port" : "80" });
+```
+
+A full URI may also be provided:
+
+```javascript
+var sendgrid = require('sendgrid')('username', 'password', { "uri" : "http://sendgrid.org:80/send" });
+```
+
+### Request
+
sendgrid-nodejs uses the node request module. You can pass in options
to be merged. This enables you to use your own https.Agent, node-tunnel
or the request proxy url. Please note that sendgrid requires https.
diff --git a/lib/sendgrid.js b/lib/sendgrid.js
index 559ba23..4a41e4d 100644
--- a/lib/sendgrid.js
+++ b/lib/sendgrid.js
@@ -12,6 +12,20 @@ var Sendgrid = function(api_user, api_key, options) {
var _this = this;
this.options = options || {};
+
+ // do this to mantain similarity to other libs
+ var uriParts = {};
+ uriParts.protocol = this.options.protocol || "https";
+ uriParts.host = this.options.host || "api.sendgrid.com";
+ uriParts.port = this.options.port || "";
+ uriParts.endpoint = this.options.endpoint || "/api/mail.send.json";
+ delete this.options.protocol;
+ delete this.options.host;
+ delete this.options.port;
+ delete this.options.endpoint;
+ this.options.uriParts = uriParts;
+
+ this.options.uri = this.options.uri || uriParts.protocol + "://" + uriParts.host + (uriParts.port ? ":" + uriParts.port : "") + uriParts.endpoint;
var send = function(email, callback) {
var callback = callback || function() { };
@@ -24,11 +38,10 @@ var Sendgrid = function(api_user, api_key, options) {
var _send = function(email, callback) {
var postOptions = {
- method : 'POST',
- uri : "https://api.sendgrid.com/api/mail.send.json"
+ method : 'POST'
};
- var options = _.merge(this.options, postOptions);
+ var options = _.merge(postOptions, this.options);
var req = request(options, function(err, resp, body) {
var json;
diff --git a/test/lib/sendgrid.test.js b/test/lib/sendgrid.test.js
index fd641eb..b640b1b 100644
--- a/test/lib/sendgrid.test.js
+++ b/test/lib/sendgrid.test.js
@@ -22,6 +22,16 @@ describe('SendGrid', function () {
expect( typeof sendgrid.options).to.equal('object');
});
+ it('should have uri set to the default', function() {
+ expect(sendgrid.options.uri).to.equal("https://api.sendgrid.com/api/mail.send.json");
+ });
+
+ it('should allow uri to change', function() {
+ var options = { "protocol" : "http", "host" : "sendgrid.org", "endpoint" : "/send", "port" : "80" };
+ var sendgrid2 = require('../../lib/sendgrid')(API_USER, API_KEY, options);
+ expect(sendgrid2.options.uri).to.equal("http://sendgrid.org:80/send");
+ });
+
it('should have web options agent global', function() {
var options = { web: { pool: global.http.globalAgent } };
var sendgrid2 = require('../../lib/sendgrid')(API_USER, API_KEY, options);