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

Throw an error if options are provided inside the data argument #306

Merged
merged 2 commits into from
Sep 25, 2017
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
7 changes: 6 additions & 1 deletion lib/StripeMethod.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,12 @@ function stripeMethod(spec) {
urlData[param] = args.shift();
}

var data = encode(utils.getDataFromArgs(args));
var data;
try {
data = encode(utils.getDataFromArgs(args));
} catch (e) {
reject(e);
}
var opts = utils.getOptionsFromArgs(args);

if (args.length) {
Expand Down
28 changes: 23 additions & 5 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,16 @@ var crypto = require('crypto');
var hasOwn = {}.hasOwnProperty;
var isPlainObject = require('lodash.isplainobject');

var OPTIONS_KEYS = ['api_key', 'idempotency_key', 'stripe_account', 'stripe_version'];

var utils = module.exports = {

isAuthKey: function(key) {
return typeof key == 'string' && /^(?:[a-z]{2}_)?[A-z0-9]{32}$/.test(key);
},

isOptionsHash: function(o) {
return isPlainObject(o) && ['api_key', 'idempotency_key', 'stripe_account', 'stripe_version'].some(function(key) {
return isPlainObject(o) && OPTIONS_KEYS.some(function(key) {
return hasOwn.call(o, key);
});
},
Expand Down Expand Up @@ -54,11 +56,27 @@ var utils = module.exports = {
* Return the data argument from a list of arguments
*/
getDataFromArgs: function(args) {
if (args.length > 0) {
if (isPlainObject(args[0]) && !utils.isOptionsHash(args[0])) {
return args.shift();
}
if (args.length < 1 || !isPlainObject(args[0])) {
return {};
}

if (!utils.isOptionsHash(args[0])) {
return args.shift();
}

var argKeys = Object.keys(args[0]);

var optionKeysInArgs = argKeys.filter(function(key) {
return OPTIONS_KEYS.indexOf(key) > -1;
});

if (optionKeysInArgs.length > 0) {
console.warn(
'Stripe: Options found in arguments (' + optionKeysInArgs.join(', ') + '). Did you mean to pass an options ' +
'object? See https://github.com/stripe/stripe-node/wiki/Passing-Options.'
);
}

return {};
},

Expand Down
19 changes: 18 additions & 1 deletion test/utils.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,28 @@ describe('utils', function() {
expect(utils.getDataFromArgs(args)).to.deep.equal({});
expect(args.length).to.equal(2);
});
it('ignores an options hash', function() {
it('ignores a hash with only options', function() {
var args = [{api_key: 'foo'}];
expect(utils.getDataFromArgs(args)).to.deep.equal({});
expect(args.length).to.equal(1);
});
it('throws an error if the hash contains both data and options', function() {
var args = [{foo: 'bar', api_key: 'foo', idempotency_key: 'baz'}];

// Hack to make sure we're logging to console.warn here:
var _warn = console.warn;

console.warn = function(message) {
expect(message).to.equal(
'Stripe: Options found in arguments (api_key, idempotency_key).' +
' Did you mean to pass an options object? See https://github.com/stripe/stripe-node/wiki/Passing-Options.'
);
};

utils.getDataFromArgs(args);

console.warn = _warn;
});
it('finds the data', function() {
var args = [{foo: 'bar'}, {api_key: 'foo'}];
expect(utils.getDataFromArgs(args)).to.deep.equal({foo: 'bar'});
Expand Down