Skip to content

Commit

Permalink
console.warn on unknown options
Browse files Browse the repository at this point in the history
  • Loading branch information
jlomas-stripe committed May 30, 2018
1 parent a3bb389 commit 5b5e921
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 13 deletions.
22 changes: 20 additions & 2 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ var utils = module.exports = {
// (the first being args and the second options) and with known
// option keys in the first so that we can warn the user about it.
if (optionKeysInArgs.length > 0 && optionKeysInArgs.length !== argKeys.length) {
console.warn( // eslint-disable-line no-console
'Stripe: Options found in arguments (' + optionKeysInArgs.join(', ') + '). Did you mean to pass an options ' +
emitStripeWarning( // eslint-disable-line no-console
'Options found in arguments (' + optionKeysInArgs.join(', ') + '). Did you mean to pass an options ' +
'object? See https://github.com/stripe/stripe-node/wiki/Passing-Options.'
);
}
Expand All @@ -98,6 +98,15 @@ var utils = module.exports = {
opts.auth = args.pop();
} else if (utils.isOptionsHash(arg)) {
var params = args.pop();

var extraKeys = Object.keys(params).filter(function(key) {
return OPTIONS_KEYS.indexOf(key) == -1;
});

if (extraKeys.length) {
emitStripeWarning('Invalid options found (' + extraKeys.join(', ') + '); ignoring.');
}

if (params.api_key) {
opts.auth = params.api_key;
}
Expand Down Expand Up @@ -195,3 +204,12 @@ var utils = module.exports = {
return obj;
},
};

function emitStripeWarning(warning) {
if (typeof process.emitWarning !== 'function') {
/* eslint-disable no-console */
return console.warn('Stripe: ' + warning);
}

return process.emitWarning(warning, 'Stripe');
}
60 changes: 49 additions & 11 deletions test/utils.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,26 +106,30 @@ describe('utils', function() {
expect(utils.getDataFromArgs(args)).to.deep.equal({});
expect(args.length).to.equal(2);
});
it('ignores a hash with only options', function() {
it('ignores a hash with only options', function(done) {
var args = [{api_key: 'foo'}];

handleConsoleWarns(function() {
handleWarnings(function() {
expect(utils.getDataFromArgs(args)).to.deep.equal({});
expect(args.length).to.equal(1);

done();
}, function(message) {
throw new Error('Should not have warned, but did: ' + message);
});
});
it('warns if the hash contains both data and options', function() {
it('warns if the hash contains both data and options', function(done) {
var args = [{foo: 'bar', api_key: 'foo', idempotency_key: 'baz'}];

handleConsoleWarns(function() {
handleWarnings(function() {
utils.getDataFromArgs(args);
}, 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.'
);

done();
});
});
it('finds the data', function() {
Expand Down Expand Up @@ -212,6 +216,25 @@ describe('utils', function() {
});
expect(args.length).to.equal(0);
});
it('warns if the hash contains something that does not belong', function(done) {
var args = [{foo: 'bar'}, {
api_key: 'sk_test_iiiiiiiiiiiiiiiiiiiiiiii',
idempotency_key: 'foo',
stripe_version: '2010-01-10',
fishsticks: true,
custard: true,
},];

handleWarnings(function() {
utils.getOptionsFromArgs(args);
}, function(message) {
expect(message).to.equal(
'Stripe: Invalid options found (fishsticks, custard); ignoring.'
);

done();
});
});
});

describe('arrayToObject', function() {
Expand Down Expand Up @@ -262,15 +285,30 @@ describe('utils', function() {
});

/* eslint-disable no-console */
function handleConsoleWarns(doWithShimmedConsoleWarn, onWarn) {
// Shim `console.warn`
var _warn = console.warn;
function handleWarnings(doWithShimmedConsoleWarn, onWarn) {
/* eslint-disable no-console */
if (typeof process.emitWarning !== 'function') {
// Shim `console.warn`
var _warn = console.warn;
console.warn = onWarn;

doWithShimmedConsoleWarn();

console.warn = onWarn;
// Un-shim `console.warn`,
console.warn = _warn;
} else {
/* eslint-disable no-inner-declarations */
function onProcessWarn(warning) {
onWarn(warning.name + ': ' + warning.message);
}

doWithShimmedConsoleWarn();
process.on('warning', onProcessWarn);

// Un-shim `console.warn`
console.warn = _warn;
doWithShimmedConsoleWarn();

process.nextTick(function() {
process.removeListener('warning', onProcessWarn);
})
}
}
/* eslint-enable no-console */

0 comments on commit 5b5e921

Please sign in to comment.