Skip to content

Commit

Permalink
don't throw error with setLoadParameters when called again with the s…
Browse files Browse the repository at this point in the history
…ame values (#414)

* setLoadParameters won't error if called again with the same values

* return if params didn't change

* lint

* Update src/pure.ts

Co-authored-by: Christopher <[email protected]>

* refactoring from feedback

---------

Co-authored-by: Christopher <[email protected]>
  • Loading branch information
madhav-stripe and christopher-stripe authored Jan 31, 2023
1 parent 3faf2a2 commit 2c7373d
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
13 changes: 12 additions & 1 deletion src/pure.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,18 @@ describe('pure module', () => {
loadStripe('pk_foo');

expect(() => {
loadStripe.setLoadParameters({advancedFraudSignals: false});
loadStripe.setLoadParameters({advancedFraudSignals: true});
}).toThrow('cannot change load parameters');
});

test('does not throw an error if calling setLoadParameters after loadStripe but the parameters are the same', () => {
const {loadStripe} = require('./pure');

loadStripe.setLoadParameters({advancedFraudSignals: false});
loadStripe('pk_foo');

expect(() => {
loadStripe.setLoadParameters({advancedFraudSignals: false});
}).not.toThrow('cannot change load parameters');
});
});
21 changes: 21 additions & 0 deletions src/pure.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,27 @@ export const loadStripe: LoadStripe & {setLoadParameters: SetLoadParams} = (
};

loadStripe.setLoadParameters = (params): void => {
// we won't throw an error if setLoadParameters is called with the same values as before
if (loadStripeCalled && loadParams) {
const validatedParams = validateLoadParams(params);
const parameterKeys = Object.keys(validatedParams) as Array<
keyof LoadParams
>;

const sameParameters = parameterKeys.reduce(
(previousValue, currentValue) => {
return (
previousValue && params[currentValue] === loadParams?.[currentValue]
);
},
true
);

if (sameParameters) {
return;
}
}

if (loadStripeCalled) {
throw new Error(
'You cannot change load parameters after calling loadStripe'
Expand Down

0 comments on commit 2c7373d

Please sign in to comment.