Skip to content

Commit 8ca4242

Browse files
authored
fix: Changed getFeatureVariableJson to getFeatureVariableJSON (#516)
* fixed case for getFeatureVariableJSON api * updated function name in changelog
1 parent 29ae7be commit 8ca4242

File tree

4 files changed

+67
-51
lines changed

4 files changed

+67
-51
lines changed

packages/optimizely-sdk/CHANGELOG.MD

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
1010
## [4.1.0-beta] - June 16, 2020
1111

1212
### New Features
13-
- Added support for JSON feature variables: new methods `getFeatureVariableJson` and `getAllFeatureVariables` ([#467](https://github.com/optimizely/javascript-sdk/pull/467), [#470](https://github.com/optimizely/javascript-sdk/pull/470))
13+
- Added support for JSON feature variables: new methods `getFeatureVariableJSON` and `getAllFeatureVariables` ([#467](https://github.com/optimizely/javascript-sdk/pull/467), [#470](https://github.com/optimizely/javascript-sdk/pull/470))
1414
- Added support for authenticated datafiles when running in Node.js. Pass `datafileAccessToken` within `datafileOptions` to request an authenticated datafile using the token ([#498](https://github.com/optimizely/javascript-sdk/pull/498), [#502](https://github.com/optimizely/javascript-sdk/pull/502)):
1515
```js
1616
const optimizelySDK = require('@optimizely/optimizely-sdk');

packages/optimizely-sdk/lib/index.d.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ declare module '@optimizely/optimizely-sdk' {
9191
userId: string,
9292
attributes?: UserAttributes
9393
): string | null;
94-
getFeatureVariableJson(
94+
getFeatureVariableJSON(
9595
featureKey: string,
9696
variableKey: string,
9797
userId: string,

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

+25-9
Original file line numberDiff line numberDiff line change
@@ -686,6 +686,10 @@ Optimizely.prototype.getEnabledFeatures = function(userId, attributes) {
686686

687687
Optimizely.prototype.getFeatureVariable = function(featureKey, variableKey, userId, attributes) {
688688
try {
689+
if (!this.__isValidInstance()) {
690+
this.logger.log(LOG_LEVEL.ERROR, sprintf(LOG_MESSAGES.INVALID_OBJECT, MODULE_NAME, 'getFeatureVariable'));
691+
return null;
692+
}
689693
return this._getFeatureVariableForType(featureKey, variableKey, null, userId, attributes);
690694
} catch (e) {
691695
this.logger.log(LOG_LEVEL.ERROR, e.message);
@@ -717,14 +721,6 @@ Optimizely.prototype.getFeatureVariable = function(featureKey, variableKey, user
717721
* with the type of the variable
718722
*/
719723
Optimizely.prototype._getFeatureVariableForType = function(featureKey, variableKey, variableType, userId, attributes) {
720-
if (!this.__isValidInstance()) {
721-
var apiName = variableType
722-
? 'getFeatureVariable' + variableType.charAt(0).toUpperCase() + variableType.slice(1)
723-
: 'getFeatureVariable';
724-
this.logger.log(LOG_LEVEL.ERROR, sprintf(LOG_MESSAGES.INVALID_OBJECT, MODULE_NAME, apiName));
725-
return null;
726-
}
727-
728724
if (!this.__validateInputs({ feature_key: featureKey, variable_key: variableKey, user_id: userId }, attributes)) {
729725
return null;
730726
}
@@ -875,6 +871,10 @@ Optimizely.prototype._getFeatureVariableValueFromVariation = function(featureKey
875871
*/
876872
Optimizely.prototype.getFeatureVariableBoolean = function(featureKey, variableKey, userId, attributes) {
877873
try {
874+
if (!this.__isValidInstance()) {
875+
this.logger.log(LOG_LEVEL.ERROR, sprintf(LOG_MESSAGES.INVALID_OBJECT, MODULE_NAME, 'getFeatureVariableBoolean'));
876+
return null;
877+
}
878878
return this._getFeatureVariableForType(featureKey, variableKey, FEATURE_VARIABLE_TYPES.BOOLEAN, userId, attributes);
879879
} catch (e) {
880880
this.logger.log(LOG_LEVEL.ERROR, e.message);
@@ -899,6 +899,10 @@ Optimizely.prototype.getFeatureVariableBoolean = function(featureKey, variableKe
899899
*/
900900
Optimizely.prototype.getFeatureVariableDouble = function(featureKey, variableKey, userId, attributes) {
901901
try {
902+
if (!this.__isValidInstance()) {
903+
this.logger.log(LOG_LEVEL.ERROR, sprintf(LOG_MESSAGES.INVALID_OBJECT, MODULE_NAME, 'getFeatureVariableDouble'));
904+
return null;
905+
}
902906
return this._getFeatureVariableForType(featureKey, variableKey, FEATURE_VARIABLE_TYPES.DOUBLE, userId, attributes);
903907
} catch (e) {
904908
this.logger.log(LOG_LEVEL.ERROR, e.message);
@@ -923,6 +927,10 @@ Optimizely.prototype.getFeatureVariableDouble = function(featureKey, variableKey
923927
*/
924928
Optimizely.prototype.getFeatureVariableInteger = function(featureKey, variableKey, userId, attributes) {
925929
try {
930+
if (!this.__isValidInstance()) {
931+
this.logger.log(LOG_LEVEL.ERROR, sprintf(LOG_MESSAGES.INVALID_OBJECT, MODULE_NAME, 'getFeatureVariableInteger'));
932+
return null;
933+
}
926934
return this._getFeatureVariableForType(featureKey, variableKey, FEATURE_VARIABLE_TYPES.INTEGER, userId, attributes);
927935
} catch (e) {
928936
this.logger.log(LOG_LEVEL.ERROR, e.message);
@@ -947,6 +955,10 @@ Optimizely.prototype.getFeatureVariableInteger = function(featureKey, variableKe
947955
*/
948956
Optimizely.prototype.getFeatureVariableString = function(featureKey, variableKey, userId, attributes) {
949957
try {
958+
if (!this.__isValidInstance()) {
959+
this.logger.log(LOG_LEVEL.ERROR, sprintf(LOG_MESSAGES.INVALID_OBJECT, MODULE_NAME, 'getFeatureVariableString'));
960+
return null;
961+
}
950962
return this._getFeatureVariableForType(featureKey, variableKey, FEATURE_VARIABLE_TYPES.STRING, userId, attributes);
951963
} catch (e) {
952964
this.logger.log(LOG_LEVEL.ERROR, e.message);
@@ -969,8 +981,12 @@ Optimizely.prototype.getFeatureVariableString = function(featureKey, variableKey
969981
* invalid, or there is a mismatch with the type
970982
* of the variable
971983
*/
972-
Optimizely.prototype.getFeatureVariableJson = function(featureKey, variableKey, userId, attributes) {
984+
Optimizely.prototype.getFeatureVariableJSON = function(featureKey, variableKey, userId, attributes) {
973985
try {
986+
if (!this.__isValidInstance()) {
987+
this.logger.log(LOG_LEVEL.ERROR, sprintf(LOG_MESSAGES.INVALID_OBJECT, MODULE_NAME, 'getFeatureVariableJSON'));
988+
return null;
989+
}
974990
return this._getFeatureVariableForType(featureKey, variableKey, FEATURE_VARIABLE_TYPES.JSON, userId, attributes);
975991
} catch (e) {
976992
this.logger.log(LOG_LEVEL.ERROR, e.message);

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

+40-40
Original file line numberDiff line numberDiff line change
@@ -3136,8 +3136,8 @@ describe('lib/optimizely', function() {
31363136
});
31373137
});
31383138

3139-
it('returns the right value from getFeatureVariableJson and send notification with featureEnabled true', function() {
3140-
var result = optlyInstance.getFeatureVariableJson(
3139+
it('returns the right value from getFeatureVariableJSON and send notification with featureEnabled true', function() {
3140+
var result = optlyInstance.getFeatureVariableJSON(
31413141
'test_feature_for_experiment',
31423142
'button_info',
31433143
'user1',
@@ -3334,8 +3334,8 @@ describe('lib/optimizely', function() {
33343334
});
33353335
});
33363336

3337-
it('returns the default value from getFeatureVariableJson and send notification with featureEnabled false', function() {
3338-
var result = optlyInstance.getFeatureVariableJson(
3337+
it('returns the default value from getFeatureVariableJSON and send notification with featureEnabled false', function() {
3338+
var result = optlyInstance.getFeatureVariableJSON(
33393339
'test_feature_for_experiment',
33403340
'button_info',
33413341
'user1',
@@ -3621,8 +3621,8 @@ describe('lib/optimizely', function() {
36213621
});
36223622
});
36233623

3624-
it('should return the right value from getFeatureVariableJson and send notification with featureEnabled true', function() {
3625-
var result = optlyInstance.getFeatureVariableJson('test_feature', 'message_info', 'user1', {
3624+
it('should return the right value from getFeatureVariableJSON and send notification with featureEnabled true', function() {
3625+
var result = optlyInstance.getFeatureVariableJSON('test_feature', 'message_info', 'user1', {
36263626
test_attribute: 'test_value',
36273627
});
36283628
assert.deepEqual(result, {
@@ -3894,8 +3894,8 @@ describe('lib/optimizely', function() {
38943894
});
38953895
});
38963896

3897-
it('should return the default value from getFeatureVariableJson and send notification with featureEnabled false', function() {
3898-
var result = optlyInstance.getFeatureVariableJson('test_feature', 'message_info', 'user1', {
3897+
it('should return the default value from getFeatureVariableJSON and send notification with featureEnabled false', function() {
3898+
var result = optlyInstance.getFeatureVariableJSON('test_feature', 'message_info', 'user1', {
38993899
test_attribute: 'test_value',
39003900
});
39013901
assert.deepEqual(result, {
@@ -4181,8 +4181,8 @@ describe('lib/optimizely', function() {
41814181
});
41824182
});
41834183

4184-
it('returns the variable default value from getFeatureVariableJson and send notification with featureEnabled false', function() {
4185-
var result = optlyInstance.getFeatureVariableJson(
4184+
it('returns the variable default value from getFeatureVariableJSON and send notification with featureEnabled false', function() {
4185+
var result = optlyInstance.getFeatureVariableJSON(
41864186
'test_feature_for_experiment',
41874187
'button_info',
41884188
'user1',
@@ -5065,8 +5065,8 @@ describe('lib/optimizely', function() {
50655065
);
50665066
});
50675067

5068-
it('returns the right value from getFeatureVariableJson', function() {
5069-
var result = optlyInstance.getFeatureVariableJson('test_feature_for_experiment', 'button_info', 'user1', {
5068+
it('returns the right value from getFeatureVariableJSON', function() {
5069+
var result = optlyInstance.getFeatureVariableJSON('test_feature_for_experiment', 'button_info', 'user1', {
50705070
test_attribute: 'test_value',
50715071
});
50725072
assert.deepEqual(result, {
@@ -5252,8 +5252,8 @@ describe('lib/optimizely', function() {
52525252
);
52535253
});
52545254

5255-
it('returns the variable default value from getFeatureVariableJson', function() {
5256-
var result = optlyInstance.getFeatureVariableJson(
5255+
it('returns the variable default value from getFeatureVariableJSON', function() {
5256+
var result = optlyInstance.getFeatureVariableJSON(
52575257
'test_feature_for_experiment',
52585258
'button_info',
52595259
'user1',
@@ -5450,8 +5450,8 @@ describe('lib/optimizely', function() {
54505450
);
54515451
});
54525452

5453-
it('returns the variable default value from getFeatureVariableJson', function() {
5454-
var result = optlyInstance.getFeatureVariableJson('test_feature_for_experiment', 'button_info', 'user1', {
5453+
it('returns the variable default value from getFeatureVariableJSON', function() {
5454+
var result = optlyInstance.getFeatureVariableJSON('test_feature_for_experiment', 'button_info', 'user1', {
54555455
test_attribute: 'test_value',
54565456
});
54575457
assert.deepEqual(result, {
@@ -5634,8 +5634,8 @@ describe('lib/optimizely', function() {
56345634
);
56355635
});
56365636

5637-
it('returns the right value from getFeatureVariableJson', function() {
5638-
var result = optlyInstance.getFeatureVariableJson('test_feature', 'message_info', 'user1', {
5637+
it('returns the right value from getFeatureVariableJSON', function() {
5638+
var result = optlyInstance.getFeatureVariableJSON('test_feature', 'message_info', 'user1', {
56395639
test_attribute: 'test_value',
56405640
});
56415641
assert.deepEqual(result, {
@@ -5806,8 +5806,8 @@ describe('lib/optimizely', function() {
58065806
);
58075807
});
58085808

5809-
it('returns the variable default value from getFeatureVariableJson', function() {
5810-
var result = optlyInstance.getFeatureVariableJson('test_feature', 'message_info', 'user1', {
5809+
it('returns the variable default value from getFeatureVariableJSON', function() {
5810+
var result = optlyInstance.getFeatureVariableJSON('test_feature', 'message_info', 'user1', {
58115811
test_attribute: 'test_value',
58125812
});
58135813
assert.deepEqual(result, {
@@ -5989,8 +5989,8 @@ describe('lib/optimizely', function() {
59895989
);
59905990
});
59915991

5992-
it('returns the variable default value from getFeatureVariableJson', function() {
5993-
var result = optlyInstance.getFeatureVariableJson('test_feature', 'message_info', 'user1', {
5992+
it('returns the variable default value from getFeatureVariableJSON', function() {
5993+
var result = optlyInstance.getFeatureVariableJSON('test_feature', 'message_info', 'user1', {
59945994
test_attribute: 'test_value',
59955995
});
59965996
assert.deepEqual(result, {
@@ -6170,8 +6170,8 @@ describe('lib/optimizely', function() {
61706170
);
61716171
});
61726172

6173-
it('returns the variable default value from getFeatureVariableJson', function() {
6174-
var result = optlyInstance.getFeatureVariableJson('test_feature_for_experiment', 'button_info', 'user1', {
6173+
it('returns the variable default value from getFeatureVariableJSON', function() {
6174+
var result = optlyInstance.getFeatureVariableJSON('test_feature_for_experiment', 'button_info', 'user1', {
61756175
test_attribute: 'test_value',
61766176
});
61776177
assert.deepEqual(result, {
@@ -6441,8 +6441,8 @@ describe('lib/optimizely', function() {
64416441
);
64426442
});
64436443

6444-
it('returns null from getFeatureVariableJson when called with a non-json variable', function() {
6445-
var result = optlyInstance.getFeatureVariableJson('test_feature_for_experiment', 'button_txt', 'user1');
6444+
it('returns null from getFeatureVariableJSON when called with a non-json variable', function() {
6445+
var result = optlyInstance.getFeatureVariableJSON('test_feature_for_experiment', 'button_txt', 'user1');
64466446
assert.strictEqual(result, null);
64476447
sinon.assert.calledWith(
64486448
createdLogger.log,
@@ -6593,8 +6593,8 @@ describe('lib/optimizely', function() {
65936593
);
65946594
});
65956595

6596-
it('returns null from getFeatureVariableJson if user id is null', function() {
6597-
var result = optlyInstance.getFeatureVariableJson('test_feature_for_experiment', 'button_info', null, {
6596+
it('returns null from getFeatureVariableJSON if user id is null', function() {
6597+
var result = optlyInstance.getFeatureVariableJSON('test_feature_for_experiment', 'button_info', null, {
65986598
test_attribute: 'test_value',
65996599
});
66006600
assert.strictEqual(result, null);
@@ -6605,8 +6605,8 @@ describe('lib/optimizely', function() {
66056605
);
66066606
});
66076607

6608-
it('returns null from getFeatureVariableJson if user id is undefined', function() {
6609-
var result = optlyInstance.getFeatureVariableJson('test_feature_for_experiment', 'button_info', undefined, {
6608+
it('returns null from getFeatureVariableJSON if user id is undefined', function() {
6609+
var result = optlyInstance.getFeatureVariableJSON('test_feature_for_experiment', 'button_info', undefined, {
66106610
test_attribute: 'test_value',
66116611
});
66126612
assert.strictEqual(result, null);
@@ -6617,8 +6617,8 @@ describe('lib/optimizely', function() {
66176617
);
66186618
});
66196619

6620-
it('returns null from getFeatureVariableJson if user id is not provided', function() {
6621-
var result = optlyInstance.getFeatureVariableJson('test_feature_for_experiment', 'button_info');
6620+
it('returns null from getFeatureVariableJSON if user id is not provided', function() {
6621+
var result = optlyInstance.getFeatureVariableJSON('test_feature_for_experiment', 'button_info');
66226622
assert.strictEqual(result, null);
66236623
sinon.assert.calledWith(
66246624
createdLogger.log,
@@ -6686,7 +6686,7 @@ describe('lib/optimizely', function() {
66866686
});
66876687

66886688
it('should return null and log an error', function() {
6689-
var result = optlyInstance.getFeatureVariableJson('test_feature_for_experiment', 'button_info', 'user1');
6689+
var result = optlyInstance.getFeatureVariableJSON('test_feature_for_experiment', 'button_info', 'user1');
66906690
assert.strictEqual(result, null);
66916691
sinon.assert.calledWith(
66926692
createdLogger.log,
@@ -6801,8 +6801,8 @@ describe('lib/optimizely', function() {
68016801
);
68026802
});
68036803

6804-
it('returns null from getFeatureVariableJson if the argument feature key is invalid', function() {
6805-
var result = optlyInstance.getFeatureVariableJson('thisIsNotAValidKey<><><>', 'button_info', 'user1');
6804+
it('returns null from getFeatureVariableJSON if the argument feature key is invalid', function() {
6805+
var result = optlyInstance.getFeatureVariableJSON('thisIsNotAValidKey<><><>', 'button_info', 'user1');
68066806
assert.strictEqual(result, null);
68076807
sinon.assert.calledWith(
68086808
createdLogger.log,
@@ -6867,8 +6867,8 @@ describe('lib/optimizely', function() {
68676867
);
68686868
});
68696869

6870-
it('returns null from getFeatureVariableJson if the argument variable key is invalid', function() {
6871-
var result = optlyInstance.getFeatureVariableJson(
6870+
it('returns null from getFeatureVariableJSON if the argument variable key is invalid', function() {
6871+
var result = optlyInstance.getFeatureVariableJSON(
68726872
'test_feature_for_experiment',
68736873
'thisIsNotAVariableKey****',
68746874
'user1'
@@ -6966,7 +6966,7 @@ describe('lib/optimizely', function() {
69666966
assert.strictEqual(logMessage, sprintf(LOG_MESSAGES.INVALID_OBJECT, 'OPTIMIZELY', 'getFeatureVariableString'));
69676967
});
69686968

6969-
it('returns null from getFeatureVariableJson when optimizely object is not a valid instance', function() {
6969+
it('returns null from getFeatureVariableJSON when optimizely object is not a valid instance', function() {
69706970
var instance = new Optimizely({
69716971
datafile: {},
69726972
errorHandler: errorHandler,
@@ -6976,11 +6976,11 @@ describe('lib/optimizely', function() {
69766976

69776977
createdLogger.log.reset();
69786978

6979-
instance.getFeatureVariableJson('test_feature_for_experiment', 'thisIsNotAVariableKey****', 'user1');
6979+
instance.getFeatureVariableJSON('test_feature_for_experiment', 'thisIsNotAVariableKey****', 'user1');
69806980

69816981
sinon.assert.calledOnce(createdLogger.log);
69826982
var logMessage = createdLogger.log.args[0][1];
6983-
assert.strictEqual(logMessage, sprintf(LOG_MESSAGES.INVALID_OBJECT, 'OPTIMIZELY', 'getFeatureVariableJson'));
6983+
assert.strictEqual(logMessage, sprintf(LOG_MESSAGES.INVALID_OBJECT, 'OPTIMIZELY', 'getFeatureVariableJSON'));
69846984
});
69856985
});
69866986
});

0 commit comments

Comments
 (0)