Skip to content

Commit a1e6074

Browse files
authored
fix (#310)
1 parent 72434a2 commit a1e6074

File tree

2 files changed

+151
-124
lines changed

2 files changed

+151
-124
lines changed

lib/countly.js

Lines changed: 56 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1624,17 +1624,48 @@
16241624
* @param {function=} callback - Callback to notify with first param error and second param remote config object
16251625
* */
16261626
this.fetch_remote_config = function(keys, omit_keys, callback) {
1627+
var keysFiltered = null;
1628+
var omitKeysFiltered = null;
1629+
var callbackFiltered = null;
1630+
1631+
// check first param is truthy
1632+
if (keys) {
1633+
// if third parameter is falsy and first param is a function assign it as the callback function
1634+
if (!callback && typeof keys === "function") {
1635+
callbackFiltered = keys;
1636+
}
1637+
// else if first param is an array assign it as 'keys'
1638+
else if (Array.isArray(keys)) {
1639+
keysFiltered = keys;
1640+
}
1641+
}
1642+
// check second param is truthy
1643+
if (omit_keys) {
1644+
// if third parameter is falsy and second param is a function assign it as the callback function
1645+
if (!callback && typeof omit_keys === "function") {
1646+
callbackFiltered = omit_keys;
1647+
}
1648+
// else if second param is an array assign it as 'omit_keys'
1649+
else if (Array.isArray(omit_keys)) {
1650+
omitKeysFiltered = omit_keys;
1651+
}
1652+
}
1653+
// assign third param as a callback function if it was not assigned yet in first two params
1654+
if (!callbackFiltered && typeof callback === "function") {
1655+
callbackFiltered = callback;
1656+
}
1657+
16271658
// use new RC API
16281659
if (this.useExplicitRcApi) {
16291660
log(logLevelEnums.INFO, "fetch_remote_config, Fetching remote config");
16301661
// opt in is true(1) or false(0)
16311662
var opt = this.rcAutoOptinAb ? 1 : 0;
1632-
fetch_remote_config_explicit(keys, omit_keys, opt, callback);
1663+
fetch_remote_config_explicit(keysFiltered, omitKeysFiltered, opt, null, callbackFiltered);
16331664
return;
16341665
}
16351666

16361667
log(logLevelEnums.WARNING, "fetch_remote_config, Fetching remote config, with legacy API");
1637-
fetch_remote_config_explicit(keys, omit_keys, "legacy", callback);
1668+
fetch_remote_config_explicit(keysFiltered, omitKeysFiltered, null, "legacy", callbackFiltered);
16381669
};
16391670

16401671
/**
@@ -1651,31 +1682,28 @@
16511682
method: "rc"
16521683
};
16531684
// check if keys were provided
1654-
if (Array.isArray(arguments[0]) && arguments[0].length > 0) {
1655-
request.keys = JSON.stringify(arguments[0]);
1685+
if (keys.length > 0) {
1686+
request.keys = JSON.stringify(keys);
16561687
}
16571688
// check if omit_keys were provided
1658-
if (Array.isArray(arguments[1]) && arguments[1].length > 0) {
1659-
request.omit_keys = JSON.stringify(arguments[1]);
1689+
if (omit_keys.length > 0) {
1690+
request.omit_keys = JSON.stringify(omit_keys);
16601691
}
1661-
var j = arguments.length - 1;
1662-
var provivedCall;
1663-
while (j--) {
1664-
// legacy api prompt check
1665-
if (arguments[j] === "legacy") {
1666-
request.method = "fetch_remote_config";
1667-
}
1668-
// opted out/in check
1669-
if (arguments[j] === 0) {
1670-
request.oi = 0;
1671-
}
1672-
if (arguments[j] === 1) {
1673-
request.oi = 1;
1674-
}
1675-
// callback check
1676-
if (typeof arguments[j] === "function") {
1677-
provivedCall = arguments[j];
1678-
}
1692+
var providedCall;
1693+
// legacy api prompt check
1694+
if (api === "legacy") {
1695+
request.method = "fetch_remote_config";
1696+
}
1697+
// opted out/in check
1698+
if (optIn === 0) {
1699+
request.oi = 0;
1700+
}
1701+
if (optIn === 1) {
1702+
request.oi = 1;
1703+
}
1704+
// callback check
1705+
if (typeof callback === "function") {
1706+
providedCall = callback;
16791707
}
16801708
if (self.check_consent(featureEnums.SESSIONS)) {
16811709
request.metrics = JSON.stringify(getMetrics());
@@ -1704,17 +1732,17 @@
17041732
catch (ex) {
17051733
log(logLevelEnums.ERROR, "fetch_remote_config_explicit, Had an issue while parsing the response: " + ex);
17061734
}
1707-
if (provivedCall) {
1735+
if (providedCall) {
17081736
log(logLevelEnums.INFO, "fetch_remote_config_explicit, Callback function is provided");
1709-
provivedCall(err, remoteConfigs);
1737+
providedCall(err, remoteConfigs);
17101738
}
17111739
// JSON array can pass
17121740
}, true);
17131741
}
17141742
else {
17151743
log(logLevelEnums.ERROR, "fetch_remote_config_explicit, Remote config requires explicit consent");
1716-
if (provivedCall) {
1717-
provivedCall(new Error("Remote config requires explicit consent"), remoteConfigs);
1744+
if (providedCall) {
1745+
providedCall(new Error("Remote config requires explicit consent"), remoteConfigs);
17181746
}
17191747
}
17201748
}

0 commit comments

Comments
 (0)