Skip to content

Commit

Permalink
Merge pull request #51 from keen/apryka-params-validation
Browse files Browse the repository at this point in the history
add basic validation for client
  • Loading branch information
apryka authored Mar 13, 2019
2 parents 5e9b5c2 + f5b3489 commit 6b92d35
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 0 deletions.
3 changes: 3 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import KeenLibrary from 'keen-core';

import each from 'keen-core/lib/utils/each';
import extend from 'keen-core/lib/utils/extend';

import { validateAuthCredentials } from './utils/validate-auth-credentials';
import { mapKeysToUnderscore } from './utils/keys-to-underscore';

import pkg from '../package.json';
Expand All @@ -14,6 +16,7 @@ KeenLibrary.prototype.readKey = function(str){
};

KeenLibrary.prototype.query = function(a, b = undefined, options = {}){
validateAuthCredentials(this.config);
// a - analysis type or config object
// b - params
const mapObj = {
Expand Down
1 change: 1 addition & 0 deletions lib/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ request.prototype.timeout = function(num){
};

request.prototype.send = function(obj){
if (this.config && !this.config.api_key) throw new Error('Please provide valid API key');
if (obj) {
this.config.params = (obj && typeof obj === 'object') ? mapKeysToUnderscore(obj) : {};
}
Expand Down
6 changes: 6 additions & 0 deletions lib/utils/validate-auth-credentials.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export function validateAuthCredentials(config) {
if (typeof config !== 'object') return;
if (!config.projectId) throw new Error('Please provide valid project ID');
if (!config.masterKey && !config.readKey) throw new Error('Please provide valid API key');
return true;
}

0 comments on commit 6b92d35

Please sign in to comment.