Skip to content

Commit

Permalink
Throw an error if options are provided inside the data argument
Browse files Browse the repository at this point in the history
  • Loading branch information
jlomas-stripe committed Aug 8, 2017
1 parent 7350d83 commit 087dca4
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 8 deletions.
7 changes: 6 additions & 1 deletion lib/StripeMethod.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,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: 22 additions & 6 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,30 @@ 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])) {
getDataFromArgs: function(args) {
if (args.length < 1 || !isPlainObject(args[0])) {
return {};
}

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

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

var optionKeysInArgs = argKeys.filter(function(key) {
return ['api_key', 'idempotency_key', 'stripe_account'].indexOf(key) > -1;
});

if (argKeys.length > optionKeysInArgs.length) {
throw new Error(
'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 {};
},

/**
* Return the options hash from a list of arguments
Expand Down
6 changes: 5 additions & 1 deletion test/utils.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,15 @@ 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'}];
expect(function() { utils.getDataFromArgs(args); }).to.throw(/Options found in arguments/);
});
it('finds the data', function() {
var args = [{foo: 'bar'}, {api_key: 'foo'}];
expect(utils.getDataFromArgs(args)).to.deep.equal({foo: 'bar'});
Expand Down

0 comments on commit 087dca4

Please sign in to comment.