Skip to content

Commit

Permalink
Throw an error if a deprecated and non-deprecated version of the same…
Browse files Browse the repository at this point in the history
… request option is passed
  • Loading branch information
rattrayalex-stripe committed Jan 9, 2020
1 parent fe375e0 commit e756c7f
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 2 deletions.
5 changes: 5 additions & 0 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,11 @@ const utils = (module.exports = {
return true;
}
const newParam = DEPRECATED_OPTIONS[key];
if (params[newParam]) {
throw Error(
`Both '${newParam}' and '${key}' were provided; please remove '${key}', which is deprecated.`
);
}
/**
* TODO turn this into a hard error in a future major version (once we have fixed our docs).
*/
Expand Down
48 changes: 46 additions & 2 deletions test/utils.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -317,15 +317,13 @@ describe('utils', () => {
idempotency_key: 'key',
stripe_account: 'acct_123',
stripe_version: '2019-08-08',
stripeVersion: '2019-08-08',
},
];
const desiredWarnings = [
"Stripe: 'api_key' is deprecated; use 'apiKey' instead.",
"Stripe: 'idempotency_key' is deprecated; use 'idempotencyKey' instead.",
"Stripe: 'stripe_account' is deprecated; use 'stripeAccount' instead.",
"Stripe: 'stripe_version' is deprecated; use 'apiVersion' instead.",
"Stripe: 'stripeVersion' is deprecated; use 'apiVersion' instead.",
];

const warnings = [];
Expand All @@ -350,6 +348,52 @@ describe('utils', () => {
});
});

it('parses stripeVersion for backwards compatibility', () => {
return new Promise((resolve, reject) => {
const args = [
{
apiKey: 'sk_test_iiiiiiiiiiiiiiiiiiiiiiii',
stripeVersion: '2019-08-08',
},
];
const desiredWarnings = [
"Stripe: 'stripeVersion' is deprecated; use 'apiVersion' instead.",
];

const warnings = [];
const onWarn = (message) => {
warnings.push(message);
if (warnings.length === desiredWarnings.length) {
expect(warnings).to.deep.equal(desiredWarnings);
resolve();
}
};
handleWarnings(() => {
expect(utils.getOptionsFromArgs(args)).to.deep.equal({
auth: 'sk_test_iiiiiiiiiiiiiiiiiiiiiiii',
headers: {
'Stripe-Version': '2019-08-08',
},
settings: {},
});
}, onWarn);
});
});

it('errors if you pass both a deprecated and non-deprecated version of the same param', () => {
const args = [
{
stripeVersion: 'bad',
apiVersion: 'good',
},
];
expect(() => {
utils.getOptionsFromArgs(args);
}).to.throw(
"Both 'apiVersion' and 'stripeVersion' were provided; please remove 'stripeVersion', which is deprecated."
);
});

it('warns if the hash contains something that does not belong', (done) => {
const args = [
{foo: 'bar'},
Expand Down

0 comments on commit e756c7f

Please sign in to comment.