Skip to content

Commit

Permalink
Add config option to allow using Commerce.js client with secret keys (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
amaau committed Sep 3, 2021
1 parent 1fe49bb commit e5cc131
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions src/commerce.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,26 @@ const defaultEventCallback = e => {
};

class Commerce {
constructor(publicKey, debug = false, config = {}) {
constructor(apiKey, debug = false, config = {}) {
this.options = {
version: 'v1',
url: 'https://api.chec.io/',
eventCallback: defaultEventCallback,
disableStorage: false,
cartLifetime: 30,
allowSecretKey: false,
...config,
publicKey: publicKey,
debug: debug,
apiKey,
debug,
};

if (typeof publicKey !== 'string' || publicKey.length === 0) {
if (typeof apiKey !== 'string' || apiKey.length === 0) {
throw new Error('⚠️ Invalid public key given to Commerce.js client');
}

if (publicKey.toLowerCase().substring(0, 3) === 'sk_') {
if (!config.allowSecretKey && apiKey.toLowerCase().substring(0, 3) === 'sk_') {
throw new Error(
'Secret key provided. You must use a public key with Commerce.js!',
'Secret key provided. You must use a public key with Commerce.js, or explicitly allow secret keys in the config object.',
);
}

Expand Down Expand Up @@ -77,7 +78,7 @@ class Commerce {
returnFullResponse = false,
) {
const headers = {
'X-Authorization': this.options.publicKey,
'X-Authorization': this.options.apiKey,
'X-Chec-Agent': 'commerce.js/v2',
'Content-Type': 'application/json',
};
Expand Down

0 comments on commit e5cc131

Please sign in to comment.