@@ -647,29 +647,56 @@ Optimizely.prototype.getEnabledFeatures = function(userId, attributes) {
647
647
}
648
648
} ;
649
649
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
+
650
675
/**
651
676
* Helper method to get the value for a variable of a certain type attached to a
652
677
* feature flag. Returns null if the feature key is invalid, the variable key is
653
678
* 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.
655
681
*
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
669
696
*/
670
697
Optimizely . prototype . _getFeatureVariableForType = function ( featureKey , variableKey , variableType , userId , attributes ) {
671
698
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' ;
673
700
this . logger . log ( LOG_LEVEL . ERROR , sprintf ( LOG_MESSAGES . INVALID_OBJECT , MODULE_NAME , apiName ) ) ;
674
701
return null ;
675
702
}
@@ -693,7 +720,9 @@ Optimizely.prototype._getFeatureVariableForType = function(featureKey, variableK
693
720
return null ;
694
721
}
695
722
696
- if ( variable . type !== variableType ) {
723
+ if ( ! variableType ) {
724
+ variableType = variable . type ;
725
+ } else if ( variable . type !== variableType ) {
697
726
this . logger . log (
698
727
LOG_LEVEL . WARNING ,
699
728
sprintf ( LOG_MESSAGES . VARIABLE_REQUESTED_WITH_WRONG_TYPE , MODULE_NAME , variableType , variable . type )
0 commit comments