Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added setting debug in initialization #188

Merged
merged 3 commits into from
Nov 2, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 13 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ Before using this library, you must have:
npm install sparkpost
```

*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)*

## Initialization
**new SparkPost(apiKey[, options])** - Initialization

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

## Methods
* **request(options[, callback]) &rarr; `{Promise}`**

*Note: All methods return promises and accept an optional last argument callback. [Read about how we handle callbacks and promises](/docs/async.md).*

* **request(options[, callback])**
* `options` - [see request modules options](https://github.com/mikeal/request#requestoptions-callback)
* `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))
* `options.debug` - setting to `true` includes full response from request client for debugging purposes
* `callback` - executed after task is completed if provided*
* standard `callback(err, data)`
* `err` - any error that occurred
* `data` - results from API call
* `data.debug` - full response from request client when `options.debug` is `true`
* **get | post | put | delete(options[, callback]) &rarr; `{Promise}`**
* **get | post | put | delete(options[, callback])**
* `options` - see request options
* `callback` - see request options
* Request method will be overwritten and set to the same value as the name of these methods.

*callback is optional because all methods return a Promise.

## Creating a SparkPost Client

Passing in an API key
Expand Down
8 changes: 2 additions & 6 deletions docs/async.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@ let client = new SparkPost(key);

client.templates.get(id)
.then((data) => {
// this is either:
// a) the `results` key of the API response body, or
// b) the full API response body
// this the full API response body
})
.catch((err) => {
// handle the sad error
Expand All @@ -33,8 +31,6 @@ client.templates.get(id, (err, data) => {
return;
}

// this is either:
// a) the `results` key of the API response body, or
// b) the full API response body
// this is the full API response body
});
```
7 changes: 6 additions & 1 deletion lib/sparkpost.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ var version = require('../package.json').version
//REST API Config Defaults
defaults = {
origin: 'https://api.sparkpost.com:443',
apiVersion: 'v1'
apiVersion: 'v1',
debug: false
};

resolveUri = function(origin, uri) {
Expand Down Expand Up @@ -66,6 +67,7 @@ SparkPost = function(apiKey, options) {
//Optional client config
this.origin = options.origin;
this.apiVersion = options.apiVersion || defaults.apiVersion;
this.debug = (typeof options.debug === 'boolean') ? options.debug : defaults.debug;

this.inboundDomains = require('./inboundDomains')(this);
this.messageEvents = require('./messageEvents')(this);
Expand Down Expand Up @@ -106,6 +108,9 @@ SparkPost.prototype.request = function(options, callback) {
options.gzip = true;
}

// set debug
options.debug = (typeof options.debug === 'boolean') ? options.debug : this.debug;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

couldn't we just use this.debug?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So only set it at the top level? Not give the ability to override or set it all the individual call level? I mean... sure. Maybe I'm just overthinking it.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh... i didn't get what this was doing... right, so this line is setting the override for a single call, defaulting to the client config. I got it now 😄


return new Promise(function(resolve, reject) {
request(options, function(err, res, body) {
var invalidCodeRegex = /(5|4)[0-9]{2}/
Expand Down
20 changes: 18 additions & 2 deletions test/spec/sparkpost.spec.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
'use strict';

var chai = require('chai')
, expect = chai.expect
, sinon = require('sinon')
, sinonChai = require('sinon-chai')
, zlib = require('zlib')
, nock = require('nock')
, SparkPost = require('../../lib/sparkpost');

chai.use(sinonChai);
chai.use(require('sinon-chai'));

describe('SparkPost Library', function() {

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

it('should allow debug to be set in options', function() {
const key = '12345678901234567890';
let options = {}
, client;

// testing default initialization
client = new SparkPost(key, options);
expect(client.debug).to.equal(false);

// testing setting flag
options.debug = true;
client = new SparkPost(key, options);
expect(client.debug).to.equal(true);
});

describe('request method', function() {
var client;

Expand Down