Skip to content

Commit f650aaa

Browse files
brandonhudavidmikeproeng37
authored andcommitted
feat(api): Implement getFeatureVariable and create tests (#298)
1 parent ae941da commit f650aaa

File tree

3 files changed

+782
-27
lines changed

3 files changed

+782
-27
lines changed

package-lock.json

+4-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/optimizely-sdk/lib/optimizely/index.js

+45-16
Original file line numberDiff line numberDiff line change
@@ -647,29 +647,56 @@ Optimizely.prototype.getEnabledFeatures = function(userId, attributes) {
647647
}
648648
};
649649

650+
/**
651+
* Returns dynamically-typed value of the variable attached to the given
652+
* feature flag. Returns null if the feature key or variable key is invalid.
653+
*
654+
* @param {string} featureKey Key of the feature whose variable's
655+
* value is being accessed
656+
* @param {string} variableKey Key of the variable whose value is
657+
* being accessed
658+
* @param {string} userId ID for the user
659+
* @param {Object} attributes Optional user attributes
660+
* @return {string|boolean|number|null} Value of the variable cast to the appropriate
661+
* type, or null if the feature key is invalid or
662+
* the variable key is invalid
663+
*/
664+
665+
Optimizely.prototype.getFeatureVariable = function(featureKey, variableKey, userId, attributes) {
666+
try {
667+
return this._getFeatureVariableForType(featureKey, variableKey, null, userId, attributes);
668+
} catch (e) {
669+
this.logger.log(LOG_LEVEL.ERROR, e.message);
670+
this.errorHandler.handleError(e);
671+
return null;
672+
}
673+
};
674+
650675
/**
651676
* Helper method to get the value for a variable of a certain type attached to a
652677
* feature flag. Returns null if the feature key is invalid, the variable key is
653678
* invalid, the given variable type does not match the variable's actual type,
654-
* or the variable value cannot be cast to the required type.
679+
* or the variable value cannot be cast to the required type. If the given variable
680+
* type is null, the value of the variable cast to the appropriate type is returned.
655681
*
656-
* @param {string} featureKey Key of the feature whose variable's value is
657-
* being accessed
658-
* @param {string} variableKey Key of the variable whose value is being
659-
* accessed
660-
* @param {string} variableType Type of the variable whose value is being
661-
* accessed (must be one of FEATURE_VARIABLE_TYPES
662-
* in lib/utils/enums/index.js)
663-
* @param {string} userId ID for the user
664-
* @param {Object} attributes Optional user attributes
665-
* @return {*} Value of the variable cast to the appropriate
666-
* type, or null if the feature key is invalid, the
667-
* variable key is invalid, or there is a mismatch
668-
* with the type of the variable
682+
* @param {string} featureKey Key of the feature whose variable's value is
683+
* being accessed
684+
* @param {string} variableKey Key of the variable whose value is being
685+
* accessed
686+
* @param {string|null} variableType Type of the variable whose value is being
687+
* accessed (must be one of FEATURE_VARIABLE_TYPES
688+
* in lib/utils/enums/index.js), or null to return the
689+
* value of the variable cast to the appropriate type
690+
* @param {string} userId ID for the user
691+
* @param {Object} attributes Optional user attributes
692+
* @return {string|boolean|number|null} Value of the variable cast to the appropriate
693+
* type, or null if the feature key is invalid, the
694+
* variable key is invalid, or there is a mismatch
695+
* with the type of the variable
669696
*/
670697
Optimizely.prototype._getFeatureVariableForType = function(featureKey, variableKey, variableType, userId, attributes) {
671698
if (!this.__isValidInstance()) {
672-
var apiName = 'getFeatureVariable' + variableType.charAt(0).toUpperCase() + variableType.slice(1);
699+
var apiName = (variableType) ? 'getFeatureVariable' + variableType.charAt(0).toUpperCase() + variableType.slice(1) : 'getFeatureVariable';
673700
this.logger.log(LOG_LEVEL.ERROR, sprintf(LOG_MESSAGES.INVALID_OBJECT, MODULE_NAME, apiName));
674701
return null;
675702
}
@@ -693,7 +720,9 @@ Optimizely.prototype._getFeatureVariableForType = function(featureKey, variableK
693720
return null;
694721
}
695722

696-
if (variable.type !== variableType) {
723+
if (!variableType) {
724+
variableType = variable.type;
725+
} else if (variable.type !== variableType) {
697726
this.logger.log(
698727
LOG_LEVEL.WARNING,
699728
sprintf(LOG_MESSAGES.VARIABLE_REQUESTED_WITH_WRONG_TYPE, MODULE_NAME, variableType, variable.type)

0 commit comments

Comments
 (0)