Skip to content

Commit 920a81f

Browse files
authored
Added setting debug in initialization (#188)
* Added setting debug to initialization * Added note for debug about exposing api key * Added note about no support for older nodejs versions
1 parent 3933653 commit 920a81f

File tree

4 files changed

+39
-19
lines changed

4 files changed

+39
-19
lines changed

README.md

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ Before using this library, you must have:
2121
npm install sparkpost
2222
```
2323

24+
*Note: Node.js versions 0.10 and 0.12 are no longer supported. For versions < 4, please continue using [sparkpost v1.3.8](https://github.com/SparkPost/node-sparkpost/tree/1.3.8)*
25+
2426
## Initialization
2527
**new SparkPost(apiKey[, options])** - Initialization
2628

@@ -40,24 +42,25 @@ npm install sparkpost
4042
* Required: no
4143
* Type: `Object`
4244
* set headers that apply to all requests
45+
* `options.debug`
46+
* Required: no
47+
* Type: `Boolean`
48+
* Default: `false`
49+
* appends full response from request client as `debug` when `true` for debugging purposes<br/>
50+
*Note: This will expose your api key to the client-side. Do not use in production.*
4351

4452
## Methods
45-
* **request(options[, callback]) &rarr; `{Promise}`**
53+
54+
*Note: All methods return promises and accept an optional last argument callback. [Read about how we handle callbacks and promises](/docs/async.md).*
55+
56+
* **request(options[, callback])**
4657
* `options` - [see request modules options](https://github.com/mikeal/request#requestoptions-callback)
4758
* `options.uri` - can either be a full url or a path that is appended to `options.origin` used at initialization ([url.resolve](http://nodejs.org/api/url.html#url_url_resolve_from_to))
4859
* `options.debug` - setting to `true` includes full response from request client for debugging purposes
49-
* `callback` - executed after task is completed if provided*
50-
* standard `callback(err, data)`
51-
* `err` - any error that occurred
52-
* `data` - results from API call
53-
* `data.debug` - full response from request client when `options.debug` is `true`
54-
* **get | post | put | delete(options[, callback]) &rarr; `{Promise}`**
60+
* **get | post | put | delete(options[, callback])**
5561
* `options` - see request options
56-
* `callback` - see request options
5762
* Request method will be overwritten and set to the same value as the name of these methods.
5863

59-
*callback is optional because all methods return a Promise.
60-
6164
## Creating a SparkPost Client
6265

6366
Passing in an API key

docs/async.md

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,7 @@ let client = new SparkPost(key);
1111

1212
client.templates.get(id)
1313
.then((data) => {
14-
// this is either:
15-
// a) the `results` key of the API response body, or
16-
// b) the full API response body
14+
// this the full API response body
1715
})
1816
.catch((err) => {
1917
// handle the sad error
@@ -33,8 +31,6 @@ client.templates.get(id, (err, data) => {
3331
return;
3432
}
3533

36-
// this is either:
37-
// a) the `results` key of the API response body, or
38-
// b) the full API response body
34+
// this is the full API response body
3935
});
4036
```

lib/sparkpost.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ var version = require('../package.json').version
1010
//REST API Config Defaults
1111
defaults = {
1212
origin: 'https://api.sparkpost.com:443',
13-
apiVersion: 'v1'
13+
apiVersion: 'v1',
14+
debug: false
1415
};
1516

1617
resolveUri = function(origin, uri) {
@@ -66,6 +67,7 @@ SparkPost = function(apiKey, options) {
6667
//Optional client config
6768
this.origin = options.origin;
6869
this.apiVersion = options.apiVersion || defaults.apiVersion;
70+
this.debug = (typeof options.debug === 'boolean') ? options.debug : defaults.debug;
6971

7072
this.inboundDomains = require('./inboundDomains')(this);
7173
this.messageEvents = require('./messageEvents')(this);
@@ -106,6 +108,9 @@ SparkPost.prototype.request = function(options, callback) {
106108
options.gzip = true;
107109
}
108110

111+
// set debug
112+
options.debug = (typeof options.debug === 'boolean') ? options.debug : this.debug;
113+
109114
return new Promise(function(resolve, reject) {
110115
request(options, function(err, res, body) {
111116
var invalidCodeRegex = /(5|4)[0-9]{2}/

test/spec/sparkpost.spec.js

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1+
'use strict';
2+
13
var chai = require('chai')
24
, expect = chai.expect
35
, sinon = require('sinon')
4-
, sinonChai = require('sinon-chai')
56
, zlib = require('zlib')
67
, nock = require('nock')
78
, SparkPost = require('../../lib/sparkpost');
89

9-
chai.use(sinonChai);
10+
chai.use(require('sinon-chai'));
1011

1112
describe('SparkPost Library', function() {
1213

@@ -65,6 +66,21 @@ describe('SparkPost Library', function() {
6566
expect(client.origin).to.equal('https://dev.sparkpost.com');
6667
});
6768

69+
it('should allow debug to be set in options', function() {
70+
const key = '12345678901234567890';
71+
let options = {}
72+
, client;
73+
74+
// testing default initialization
75+
client = new SparkPost(key, options);
76+
expect(client.debug).to.equal(false);
77+
78+
// testing setting flag
79+
options.debug = true;
80+
client = new SparkPost(key, options);
81+
expect(client.debug).to.equal(true);
82+
});
83+
6884
describe('request method', function() {
6985
var client;
7086

0 commit comments

Comments
 (0)