configcat-common
Advanced tools
Comparing version 2.3.2 to 2.4.1
@@ -19,5 +19,5 @@ import { IConfigCatKernel } from "."; | ||
/** Returns the Variation ID (analytics) of a feature flag or setting based on it's key */ | ||
getVariationId(key: string, defaultValue: any, callback: (variationId: string) => void, user?: User): void; | ||
getVariationId(key: string, defaultVariationId: any, callback: (variationId: string) => void, user?: User): void; | ||
/** Returns the Variation ID (analytics) of a feature flag or setting based on it's key */ | ||
getVariationIdAsync(key: string, defaultValue: any, user?: User): Promise<string>; | ||
getVariationIdAsync(key: string, defaultVariationId: any, user?: User): Promise<string>; | ||
/** Returns the Variation IDs (analytics) of all feature flags or settings */ | ||
@@ -39,6 +39,6 @@ getAllVariationIds(callback: (variationIds: string[]) => void, user?: User): void; | ||
getAllKeysAsync(): Promise<string[]>; | ||
getVariationId(key: string, defaultValue: any, callback: (variationId: string) => void, user?: User): void; | ||
getVariationIdAsync(key: string, defaultValue: any, user?: User): Promise<string>; | ||
getVariationId(key: string, defaultVariationId: any, callback: (variationId: string) => void, user?: User): void; | ||
getVariationIdAsync(key: string, defaultVariationId: any, user?: User): Promise<string>; | ||
getAllVariationIds(callback: (variationIds: string[]) => void, user?: User): void; | ||
getAllVariationIdsAsync(user?: User): Promise<string[]>; | ||
} |
@@ -43,3 +43,2 @@ "use strict"; | ||
var RolloutEvaluator_1 = require("./RolloutEvaluator"); | ||
var Sha1_1 = require("./Sha1"); | ||
exports.CONFIG_CHANGE_EVENT_NAME = "changed"; | ||
@@ -90,3 +89,3 @@ var ConfigCatClient = /** @class */ (function () { | ||
result = defaultValue; | ||
result = this.evaluator.Evaluate(config, key, defaultValue, user); | ||
result = this.evaluator.Evaluate(config, key, defaultValue, user).Value; | ||
resolve(result); | ||
@@ -142,42 +141,19 @@ return [2 /*return*/]; | ||
}; | ||
ConfigCatClient.prototype.getVariationId = function (key, defaultValue, callback, user) { | ||
this.getVariationIdAsync(key, defaultValue, user).then(function (variationId) { | ||
ConfigCatClient.prototype.getVariationId = function (key, defaultVariationId, callback, user) { | ||
this.getVariationIdAsync(key, defaultVariationId, user).then(function (variationId) { | ||
callback(variationId); | ||
}); | ||
}; | ||
ConfigCatClient.prototype.getVariationIdAsync = function (key, defaultValue, user) { | ||
ConfigCatClient.prototype.getVariationIdAsync = function (key, defaultVariationId, user) { | ||
var _this = this; | ||
return new Promise(function (resolve) { return __awaiter(_this, void 0, void 0, function () { | ||
var value, variationId, numberString, modulo; | ||
var config, result; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: return [4 /*yield*/, this.getValueAsync(key, defaultValue, user)]; | ||
case 0: return [4 /*yield*/, this.configService.getConfig()]; | ||
case 1: | ||
value = _a.sent(); | ||
variationId = key + '_'; | ||
if (value === null || value === undefined) { | ||
variationId += 'null'; | ||
} | ||
else { | ||
switch (typeof value) { | ||
case 'boolean': | ||
variationId += ('' + value).toLowerCase(); | ||
break; | ||
case 'number': | ||
numberString = '' + Math.trunc(value); | ||
modulo = Math.abs(value % 1); | ||
if (modulo) { | ||
numberString += '.' + Math.trunc((modulo) * 1000000); | ||
while (numberString.charAt(numberString.length - 1) === '0') { | ||
numberString = numberString.substring(0, numberString.length - 1); | ||
} | ||
} | ||
variationId += numberString; | ||
break; | ||
default: | ||
variationId += Sha1_1.sha1('' + value); | ||
break; | ||
} | ||
} | ||
resolve(variationId); | ||
config = _a.sent(); | ||
result = defaultVariationId; | ||
result = this.evaluator.Evaluate(config, key, null, user, defaultVariationId).VariationId; | ||
resolve(result); | ||
return [2 /*return*/]; | ||
@@ -184,0 +160,0 @@ } |
@@ -15,2 +15,3 @@ export declare class ProjectConfig { | ||
static RolloutRules: string; | ||
static VariationId: string; | ||
} | ||
@@ -23,2 +24,3 @@ export declare class RolloutRules { | ||
static Value: string; | ||
static VariationId: string; | ||
} | ||
@@ -29,2 +31,3 @@ export declare class RolloutPercentageItems { | ||
static Percentage: string; | ||
static VariationId: string; | ||
} |
@@ -19,2 +19,3 @@ "use strict"; | ||
Setting.RolloutRules = "r"; | ||
Setting.VariationId = "i"; | ||
return Setting; | ||
@@ -31,2 +32,3 @@ }()); | ||
RolloutRules.Value = "v"; | ||
RolloutRules.VariationId = "i"; | ||
return RolloutRules; | ||
@@ -41,4 +43,5 @@ }()); | ||
RolloutPercentageItems.Percentage = "p"; | ||
RolloutPercentageItems.VariationId = "i"; | ||
return RolloutPercentageItems; | ||
}()); | ||
exports.RolloutPercentageItems = RolloutPercentageItems; |
import { IConfigCatLogger } from "."; | ||
import { ProjectConfig } from "./ProjectConfig"; | ||
export interface IRolloutEvaluator { | ||
Evaluate(config: ProjectConfig, key: string, defaultValue: any, user?: User): any; | ||
Evaluate(config: ProjectConfig, key: string, defaultValue: any, user?: User, defaultVariationId?: any): ValueAndVariationId; | ||
} | ||
@@ -23,3 +23,3 @@ /** Object for variation evaluation */ | ||
constructor(logger: IConfigCatLogger); | ||
Evaluate(config: ProjectConfig, key: string, defaultValue: any, user?: User): any; | ||
Evaluate(config: ProjectConfig, key: string, defaultValue: any, user?: User, defaultVariationId?: any): ValueAndVariationId; | ||
private EvaluateRules; | ||
@@ -32,1 +32,6 @@ private EvaluateVariations; | ||
} | ||
declare class ValueAndVariationId { | ||
Value: any; | ||
VariationId: any; | ||
} | ||
export {}; |
@@ -27,6 +27,6 @@ "use strict"; | ||
} | ||
RolloutEvaluator.prototype.Evaluate = function (config, key, defaultValue, user) { | ||
RolloutEvaluator.prototype.Evaluate = function (config, key, defaultValue, user, defaultVariationId) { | ||
if (!config || !config.ConfigJSON) { | ||
this.logger.error("JSONConfig is not present. Returning default value: '" + defaultValue + "'."); | ||
return defaultValue; | ||
return { Value: defaultValue, VariationId: defaultVariationId }; | ||
} | ||
@@ -37,3 +37,3 @@ if (!config.ConfigJSON[key]) { | ||
this.logger.error(s); | ||
return defaultValue; | ||
return { Value: defaultValue, VariationId: defaultVariationId }; | ||
} | ||
@@ -48,6 +48,9 @@ var eLog = new EvaluateLogger(); | ||
result = this.EvaluateRules(config.ConfigJSON[key][ProjectConfig_1.Setting.RolloutRules], user, eLog); | ||
if (result.Value == null) { | ||
result.Value = result.EvaluateLog.ReturnValue = this.EvaluateVariations(config.ConfigJSON[key][ProjectConfig_1.Setting.RolloutPercentageItems], key, user); | ||
if (result.ValueAndVariationId == null) { | ||
result.ValueAndVariationId = this.EvaluateVariations(config.ConfigJSON[key][ProjectConfig_1.Setting.RolloutPercentageItems], key, user); | ||
if (result.ValueAndVariationId) { | ||
result.EvaluateLog.ReturnValue = result.ValueAndVariationId.Value; | ||
} | ||
if (config.ConfigJSON[key][ProjectConfig_1.Setting.RolloutPercentageItems].length > 0) { | ||
result.EvaluateLog.OpAppendLine("Evaluating % options => " + (result.Value == null ? "user not targeted" : "user targeted")); | ||
result.EvaluateLog.OpAppendLine("Evaluating % options => " + (result.ValueAndVariationId == null ? "user not targeted" : "user targeted")); | ||
} | ||
@@ -65,11 +68,15 @@ } | ||
} | ||
if (result.Value == null) { | ||
result.EvaluateLog.ReturnValue = config.ConfigJSON[key][ProjectConfig_1.Setting.Value]; | ||
if (result.ValueAndVariationId == null) { | ||
result.ValueAndVariationId = { | ||
Value: config.ConfigJSON[key][ProjectConfig_1.Setting.Value], | ||
VariationId: config.ConfigJSON[key][ProjectConfig_1.Setting.VariationId], | ||
}; | ||
result.EvaluateLog.ReturnValue = result.ValueAndVariationId.Value; | ||
} | ||
this.logger.info(result.EvaluateLog.GetLog()); | ||
return result.Value == null ? config.ConfigJSON[key][ProjectConfig_1.Setting.Value] : result.Value; | ||
return result.ValueAndVariationId; | ||
}; | ||
RolloutEvaluator.prototype.EvaluateRules = function (rolloutRules, user, eLog) { | ||
var result = new EvaluateResult(); | ||
result.Value = null; | ||
result.ValueAndVariationId = null; | ||
if (rolloutRules && rolloutRules.length > 0) { | ||
@@ -92,3 +99,7 @@ var _loop_1 = function (i) { | ||
eLog.OpAppendLine(log); | ||
result.Value = eLog.ReturnValue = rule[ProjectConfig_1.RolloutRules.Value]; | ||
result.ValueAndVariationId = { | ||
Value: rule[ProjectConfig_1.RolloutRules.Value], | ||
VariationId: rule[ProjectConfig_1.RolloutRules.VariationId] | ||
}; | ||
eLog.ReturnValue = result.ValueAndVariationId.Value; | ||
result.EvaluateLog = eLog; | ||
@@ -109,3 +120,7 @@ return { value: result }; | ||
eLog.OpAppendLine(log); | ||
result.Value = eLog.ReturnValue = rule[ProjectConfig_1.RolloutRules.Value]; | ||
result.ValueAndVariationId = { | ||
Value: rule[ProjectConfig_1.RolloutRules.Value], | ||
VariationId: rule[ProjectConfig_1.RolloutRules.VariationId] | ||
}; | ||
eLog.ReturnValue = result.ValueAndVariationId.Value; | ||
result.EvaluateLog = eLog; | ||
@@ -120,3 +135,7 @@ return { value: result }; | ||
eLog.OpAppendLine(log); | ||
result.Value = eLog.ReturnValue = rule[ProjectConfig_1.RolloutRules.Value]; | ||
result.ValueAndVariationId = { | ||
Value: rule[ProjectConfig_1.RolloutRules.Value], | ||
VariationId: rule[ProjectConfig_1.RolloutRules.VariationId] | ||
}; | ||
eLog.ReturnValue = result.ValueAndVariationId.Value; | ||
result.EvaluateLog = eLog; | ||
@@ -131,3 +150,7 @@ return { value: result }; | ||
eLog.OpAppendLine(log); | ||
result.Value = eLog.ReturnValue = rule[ProjectConfig_1.RolloutRules.Value]; | ||
result.ValueAndVariationId = { | ||
Value: rule[ProjectConfig_1.RolloutRules.Value], | ||
VariationId: rule[ProjectConfig_1.RolloutRules.VariationId] | ||
}; | ||
eLog.ReturnValue = result.ValueAndVariationId.Value; | ||
result.EvaluateLog = eLog; | ||
@@ -147,3 +170,7 @@ return { value: result }; | ||
eLog.OpAppendLine(log); | ||
result.Value = eLog.ReturnValue = rule[ProjectConfig_1.RolloutRules.Value]; | ||
result.ValueAndVariationId = { | ||
Value: rule[ProjectConfig_1.RolloutRules.Value], | ||
VariationId: rule[ProjectConfig_1.RolloutRules.VariationId] | ||
}; | ||
eLog.ReturnValue = result.ValueAndVariationId.Value; | ||
result.EvaluateLog = eLog; | ||
@@ -163,3 +190,7 @@ return { value: result }; | ||
eLog.OpAppendLine(log); | ||
result.Value = eLog.ReturnValue = rule[ProjectConfig_1.RolloutRules.Value]; | ||
result.ValueAndVariationId = { | ||
Value: rule[ProjectConfig_1.RolloutRules.Value], | ||
VariationId: rule[ProjectConfig_1.RolloutRules.VariationId] | ||
}; | ||
eLog.ReturnValue = result.ValueAndVariationId.Value; | ||
result.EvaluateLog = eLog; | ||
@@ -176,3 +207,7 @@ return { value: result }; | ||
eLog.OpAppendLine(log); | ||
result.Value = eLog.ReturnValue = rule[ProjectConfig_1.RolloutRules.Value]; | ||
result.ValueAndVariationId = { | ||
Value: rule[ProjectConfig_1.RolloutRules.Value], | ||
VariationId: rule[ProjectConfig_1.RolloutRules.VariationId] | ||
}; | ||
eLog.ReturnValue = result.ValueAndVariationId.Value; | ||
result.EvaluateLog = eLog; | ||
@@ -193,3 +228,7 @@ return { value: result }; | ||
eLog.OpAppendLine(log); | ||
result.Value = eLog.ReturnValue = rule[ProjectConfig_1.RolloutRules.Value]; | ||
result.ValueAndVariationId = { | ||
Value: rule[ProjectConfig_1.RolloutRules.Value], | ||
VariationId: rule[ProjectConfig_1.RolloutRules.VariationId] | ||
}; | ||
eLog.ReturnValue = result.ValueAndVariationId.Value; | ||
result.EvaluateLog = eLog; | ||
@@ -225,3 +264,6 @@ return { value: result }; | ||
if (hashScale < bucket) { | ||
return variation[ProjectConfig_1.RolloutPercentageItems.Value]; | ||
return { | ||
Value: variation[ProjectConfig_1.RolloutPercentageItems.Value], | ||
VariationId: variation[ProjectConfig_1.RolloutPercentageItems.VariationId] | ||
}; | ||
} | ||
@@ -380,2 +422,7 @@ } | ||
exports.RolloutEvaluator = RolloutEvaluator; | ||
var ValueAndVariationId = /** @class */ (function () { | ||
function ValueAndVariationId() { | ||
} | ||
return ValueAndVariationId; | ||
}()); | ||
var EvaluateResult = /** @class */ (function () { | ||
@@ -382,0 +429,0 @@ function EvaluateResult() { |
{ | ||
"name": "configcat-common", | ||
"version": "2.3.2", | ||
"version": "2.4.1", | ||
"description": "ConfigCat is a configuration as a service that lets you manage your features and configurations without actually deploying new code.", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
82207
1837