-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathapiKey.js
43 lines (38 loc) · 1.95 KB
/
apiKey.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import { isString } from '../lang';
import logFactory from '../logger';
const log = logFactory('', {
// Errors on API key validation are important enough so that one day we might force logging them or throw an exception on startup.
displayAllErrors: true
});
const usedKeysMap = {};
export function validateApiKey(maybeApiKey) {
let apiKey = false;
if (maybeApiKey == undefined) { // eslint-disable-line eqeqeq
log.error('Factory instantiation: you passed a null or undefined api_key, api_key must be a non-empty string.');
} else if (isString(maybeApiKey)) {
if (maybeApiKey.length > 0)
apiKey = maybeApiKey;
else
log.error('Factory instantiation: you passed an empty api_key, api_key must be a non-empty string.');
} else {
log.error('Factory instantiation: you passed an invalid api_key, api_key must be a non-empty string.');
}
// If the apiKey is correct, we'll save it as the instance creation should work.
if (apiKey) {
if (!usedKeysMap[apiKey]) {
// If this key is not present, only warning scenarios is that we have factories for other keys.
usedKeysMap[apiKey] = 1;
if (Object.keys(usedKeysMap).length > 1) {
log.warn('Factory instantiation: You already have an instance of the Split factory. Make sure you definitely want this additional instance. We recommend keeping only one instance of the factory at all times (Singleton pattern) and reusing it throughout your application.');
}
} else {
log.warn(`Factory instantiation: You already have ${usedKeysMap[apiKey]} ${usedKeysMap[apiKey] === 1 ? 'factory' : 'factories'} with this API Key. We recommend keeping only one instance of the factory at all times (Singleton pattern) and reusing it throughout your application.`);
usedKeysMap[apiKey]++;
}
}
return apiKey;
}
export function releaseApiKey(apiKey) {
if (usedKeysMap[apiKey]) usedKeysMap[apiKey]--;
if (usedKeysMap[apiKey] === 0) delete usedKeysMap[apiKey];
}