diff options
author | Nick Quinlan <nicholas.quinlan@sendgrid.com> | 2014-06-23 10:29:08 -0700 |
---|---|---|
committer | Nick Quinlan <nicholas.quinlan@sendgrid.com> | 2014-06-23 10:29:08 -0700 |
commit | e61b7a4ab49b1a1565c613cfd26ade7cba24ee13 (patch) | |
tree | 5b8edce1cf77fe4b20204cadaee546ee1c7a412f | |
parent | 2d3ad204c88e3d3f9ca0d1c08388f028fea2efdf (diff) | |
download | sendgrid-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.md | 15 | ||||
-rw-r--r-- | lib/sendgrid.js | 19 | ||||
-rw-r--r-- | test/lib/sendgrid.test.js | 10 |
3 files changed, 41 insertions, 3 deletions
@@ -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); |