From e756c7f01fa7536179a5cd3cc2f54372bb12b1fe Mon Sep 17 00:00:00 2001 From: Alex Rattray Date: Wed, 8 Jan 2020 16:04:28 -0800 Subject: [PATCH] Throw an error if a deprecated and non-deprecated version of the same request option is passed --- lib/utils.js | 5 +++++ test/utils.spec.js | 48 ++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 51 insertions(+), 2 deletions(-) diff --git a/lib/utils.js b/lib/utils.js index 32860467bb..818d98f790 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -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). */ diff --git a/test/utils.spec.js b/test/utils.spec.js index 620a14694f..e9dc40c345 100644 --- a/test/utils.spec.js +++ b/test/utils.spec.js @@ -317,7 +317,6 @@ describe('utils', () => { idempotency_key: 'key', stripe_account: 'acct_123', stripe_version: '2019-08-08', - stripeVersion: '2019-08-08', }, ]; const desiredWarnings = [ @@ -325,7 +324,6 @@ describe('utils', () => { "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 = []; @@ -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'},