Skip to content

Commit

Permalink
fix (#310)
Browse files Browse the repository at this point in the history
  • Loading branch information
turtledreams authored Nov 9, 2022
1 parent 72434a2 commit a1e6074
Show file tree
Hide file tree
Showing 2 changed files with 151 additions and 124 deletions.
84 changes: 56 additions & 28 deletions lib/countly.js
Original file line number Diff line number Diff line change
Expand Up @@ -1624,17 +1624,48 @@
* @param {function=} callback - Callback to notify with first param error and second param remote config object
* */
this.fetch_remote_config = function(keys, omit_keys, callback) {
var keysFiltered = null;
var omitKeysFiltered = null;
var callbackFiltered = null;

// check first param is truthy
if (keys) {
// if third parameter is falsy and first param is a function assign it as the callback function
if (!callback && typeof keys === "function") {
callbackFiltered = keys;
}
// else if first param is an array assign it as 'keys'
else if (Array.isArray(keys)) {
keysFiltered = keys;
}
}
// check second param is truthy
if (omit_keys) {
// if third parameter is falsy and second param is a function assign it as the callback function
if (!callback && typeof omit_keys === "function") {
callbackFiltered = omit_keys;
}
// else if second param is an array assign it as 'omit_keys'
else if (Array.isArray(omit_keys)) {
omitKeysFiltered = omit_keys;
}
}
// assign third param as a callback function if it was not assigned yet in first two params
if (!callbackFiltered && typeof callback === "function") {
callbackFiltered = callback;
}

// use new RC API
if (this.useExplicitRcApi) {
log(logLevelEnums.INFO, "fetch_remote_config, Fetching remote config");
// opt in is true(1) or false(0)
var opt = this.rcAutoOptinAb ? 1 : 0;
fetch_remote_config_explicit(keys, omit_keys, opt, callback);
fetch_remote_config_explicit(keysFiltered, omitKeysFiltered, opt, null, callbackFiltered);
return;
}

log(logLevelEnums.WARNING, "fetch_remote_config, Fetching remote config, with legacy API");
fetch_remote_config_explicit(keys, omit_keys, "legacy", callback);
fetch_remote_config_explicit(keysFiltered, omitKeysFiltered, null, "legacy", callbackFiltered);
};

/**
Expand All @@ -1651,31 +1682,28 @@
method: "rc"
};
// check if keys were provided
if (Array.isArray(arguments[0]) && arguments[0].length > 0) {
request.keys = JSON.stringify(arguments[0]);
if (keys.length > 0) {
request.keys = JSON.stringify(keys);
}
// check if omit_keys were provided
if (Array.isArray(arguments[1]) && arguments[1].length > 0) {
request.omit_keys = JSON.stringify(arguments[1]);
if (omit_keys.length > 0) {
request.omit_keys = JSON.stringify(omit_keys);
}
var j = arguments.length - 1;
var provivedCall;
while (j--) {
// legacy api prompt check
if (arguments[j] === "legacy") {
request.method = "fetch_remote_config";
}
// opted out/in check
if (arguments[j] === 0) {
request.oi = 0;
}
if (arguments[j] === 1) {
request.oi = 1;
}
// callback check
if (typeof arguments[j] === "function") {
provivedCall = arguments[j];
}
var providedCall;
// legacy api prompt check
if (api === "legacy") {
request.method = "fetch_remote_config";
}
// opted out/in check
if (optIn === 0) {
request.oi = 0;
}
if (optIn === 1) {
request.oi = 1;
}
// callback check
if (typeof callback === "function") {
providedCall = callback;
}
if (self.check_consent(featureEnums.SESSIONS)) {
request.metrics = JSON.stringify(getMetrics());
Expand Down Expand Up @@ -1704,17 +1732,17 @@
catch (ex) {
log(logLevelEnums.ERROR, "fetch_remote_config_explicit, Had an issue while parsing the response: " + ex);
}
if (provivedCall) {
if (providedCall) {
log(logLevelEnums.INFO, "fetch_remote_config_explicit, Callback function is provided");
provivedCall(err, remoteConfigs);
providedCall(err, remoteConfigs);
}
// JSON array can pass
}, true);
}
else {
log(logLevelEnums.ERROR, "fetch_remote_config_explicit, Remote config requires explicit consent");
if (provivedCall) {
provivedCall(new Error("Remote config requires explicit consent"), remoteConfigs);
if (providedCall) {
providedCall(new Error("Remote config requires explicit consent"), remoteConfigs);
}
}
}
Expand Down
Loading

0 comments on commit a1e6074

Please sign in to comment.