Skip to content

Commit

Permalink
Allow for a combined options/data object without losing the data
Browse files Browse the repository at this point in the history
  • Loading branch information
jlomas-stripe committed Feb 16, 2017
1 parent b46a0d1 commit cf658d2
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 11 deletions.
20 changes: 15 additions & 5 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,22 @@ 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();
}
return {};

// We have data and options intermingled. Let's get the data.
return Object.keys(args[0]).reduce(function(accum, key) {
if (['api_key', 'idempotency_key', 'stripe_account'].indexOf(key) === -1) {
accum[key] = args[0][key];
}

return accum;
}, {});
},

/**
Expand Down
58 changes: 58 additions & 0 deletions test/resources/Charges.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,63 @@ describe('Charge Resource', function() {
});
});

it('Sends the correct direct charge request with `stripe_account` in the data', function() {
stripe.charges.create({
amount: '1500',
currency: 'usd',
shipping: {
address: {
line1: 'foo',
},
},
stripe_account: 'acct_foobarbaz'
});
expect(stripe.LAST_REQUEST).to.deep.equal({
method: 'POST',
url: '/v1/charges',
data: {
amount: '1500',
currency: 'usd',
shipping: {
address: {
line1: 'foo',
},
},
},
headers: {
'Stripe-Account': 'acct_foobarbaz'
},
});
});

it('Sends the correct direct charge request with `stripe_account` in the options', function() {
stripe.charges.create({
amount: '1500',
currency: 'usd',
shipping: {
address: {
line1: 'foo',
},
},
}, { stripe_account: 'acct_foobarbaz' });
expect(stripe.LAST_REQUEST).to.deep.equal({
method: 'POST',
url: '/v1/charges',
data: {
amount: '1500',
currency: 'usd',
shipping: {
address: {
line1: 'foo',
},
},
},
headers: {
'Stripe-Account': 'acct_foobarbaz'
},
});
});

it('Sends the correct request for Bitcoin', function() {
var receiver = stripe.bitcoinReceivers.create({
amount: 100,
Expand Down Expand Up @@ -70,6 +127,7 @@ describe('Charge Resource', function() {
},
})
});

});

describe('list', function() {
Expand Down
43 changes: 37 additions & 6 deletions test/utils.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ describe('utils', function() {
it('handles an empty list', function() {
expect(utils.getDataFromArgs([])).to.deep.equal({});
});
it('handles an list with no object', function() {
it('handles a list with no object', function() {
var args = [1, 3];
expect(utils.getDataFromArgs(args)).to.deep.equal({});
expect(args.length).to.equal(2);
Expand All @@ -116,6 +116,11 @@ describe('utils', function() {
expect(utils.getDataFromArgs(args)).to.deep.equal({foo: 'bar'});
expect(args.length).to.equal(1);
});
it('finds the data - and only the data - even if there are options in there', function() {
var args = [{foo: 'bar', stripe_account: 'acct_foobarbaz'}];
expect(utils.getDataFromArgs(args)).to.deep.equal({foo: 'bar'});
expect(args.length).to.equal(1);
});
});

describe('getOptsFromArgs', function() {
Expand All @@ -141,6 +146,16 @@ describe('utils', function() {
});
expect(args.length).to.equal(1);
});
it('pulls the options out of a single options/data object', function() {
var args = [{foo: 'bar', stripe_account: 'acct_foobarbaz'}];
expect(utils.getOptionsFromArgs(args)).to.deep.equal({
auth: null,
headers: {
'Stripe-Account': 'acct_foobarbaz'
},
});
expect(args.length).to.equal(0);
});
it('parses an api key', function() {
var args = ['sk_test_iiiiiiiiiiiiiiiiiiiiiiii'];
expect(utils.getOptionsFromArgs(args)).to.deep.equal({
Expand All @@ -149,33 +164,49 @@ describe('utils', function() {
});
expect(args.length).to.equal(0);
});
it('parse an idempotency key', function() {
it('parses an idempotency key', function() {
var args = [{foo: 'bar'}, {idempotency_key: 'foo'}];
expect(utils.getOptionsFromArgs(args)).to.deep.equal({
auth: null,
headers: {'Idempotency-Key': 'foo'},
});
expect(args.length).to.equal(1);
});
it('parse an idempotency key and api key (with data)', function() {
it('parses a stripe_account', function() {
var args = [{foo: 'bar'}, {stripe_account: 'acct_foobarbaz'}];
expect(utils.getOptionsFromArgs(args)).to.deep.equal({
auth: null,
headers: {'Stripe-Account': 'acct_foobarbaz'},
});
expect(args.length).to.equal(1);
});
it('parses an idempotency key, api key and stripe_account (with data)', function() {
var args = [{foo: 'bar'}, {
api_key: 'sk_test_iiiiiiiiiiiiiiiiiiiiiiii',
idempotency_key: 'foo',
stripe_account: 'acct_foobarbaz',
},];
expect(utils.getOptionsFromArgs(args)).to.deep.equal({
auth: 'sk_test_iiiiiiiiiiiiiiiiiiiiiiii',
headers: {'Idempotency-Key': 'foo'},
headers: {
'Idempotency-Key': 'foo',
'Stripe-Account': 'acct_foobarbaz',
},
});
expect(args.length).to.equal(1);
});
it('parse an idempotency key and api key', function() {
it('parses an idempotency key, api key and stripe_account', function() {
var args = [{
api_key: 'sk_test_iiiiiiiiiiiiiiiiiiiiiiii',
idempotency_key: 'foo',
stripe_account: 'acct_foobarbaz',
},];
expect(utils.getOptionsFromArgs(args)).to.deep.equal({
auth: 'sk_test_iiiiiiiiiiiiiiiiiiiiiiii',
headers: {'Idempotency-Key': 'foo'},
headers: {
'Idempotency-Key': 'foo',
'Stripe-Account': 'acct_foobarbaz',
},
});
expect(args.length).to.equal(0);
});
Expand Down

0 comments on commit cf658d2

Please sign in to comment.