@@ -22,6 +22,7 @@ import { OdpManager } from '../odp/odp_manager';
22
22
import { VuidManager } from '../vuid/vuid_manager' ;
23
23
import { OdpEvent } from '../odp/event_manager/odp_event' ;
24
24
import { OptimizelySegmentOption } from '../odp/segment_manager/optimizely_segment_option' ;
25
+ import { BaseService } from '../service' ;
25
26
26
27
import {
27
28
UserAttributes ,
@@ -71,7 +72,7 @@ import {
71
72
ODP_EVENT_FAILED_ODP_MANAGER_MISSING ,
72
73
UNABLE_TO_GET_VUID_VUID_MANAGER_NOT_AVAILABLE ,
73
74
UNRECOGNIZED_DECIDE_OPTION ,
74
- INVALID_OBJECT ,
75
+ NO_PROJECT_CONFIG_FAILURE ,
75
76
EVENT_KEY_NOT_FOUND ,
76
77
NOT_TRACKING_USER ,
77
78
VARIABLE_REQUESTED_WITH_WRONG_TYPE ,
@@ -267,11 +268,9 @@ export default class Optimizely implements Client {
267
268
268
269
/**
269
270
* Returns a truthy value if this instance currently has a valid project config
270
- * object, and the initial configuration object that was passed into the
271
- * constructor was also valid.
272
271
* @return {boolean }
273
272
*/
274
- isValidInstance ( ) : boolean {
273
+ private hasProjectConfig ( ) : boolean {
275
274
return ! ! this . projectConfigManager . getConfig ( ) ;
276
275
}
277
276
@@ -284,20 +283,16 @@ export default class Optimizely implements Client {
284
283
*/
285
284
activate ( experimentKey : string , userId : string , attributes ?: UserAttributes ) : string | null {
286
285
try {
287
- if ( ! this . isValidInstance ( ) ) {
288
- this . logger ?. error ( INVALID_OBJECT , 'activate' ) ;
286
+ const configObj = this . projectConfigManager . getConfig ( ) ;
287
+ if ( ! configObj ) {
288
+ this . errorReporter . report ( NO_PROJECT_CONFIG_FAILURE , 'activate' ) ;
289
289
return null ;
290
290
}
291
291
292
292
if ( ! this . validateInputs ( { experiment_key : experimentKey , user_id : userId } , attributes ) ) {
293
293
return this . notActivatingExperiment ( experimentKey , userId ) ;
294
294
}
295
295
296
- const configObj = this . projectConfigManager . getConfig ( ) ;
297
- if ( ! configObj ) {
298
- return null ;
299
- }
300
-
301
296
try {
302
297
const variationKey = this . getVariation ( experimentKey , userId , attributes ) ;
303
298
if ( variationKey === null ) {
@@ -394,20 +389,16 @@ export default class Optimizely implements Client {
394
389
return ;
395
390
}
396
391
397
- if ( ! this . isValidInstance ( ) ) {
398
- this . logger ?. error ( INVALID_OBJECT , 'track' ) ;
392
+ const configObj = this . projectConfigManager . getConfig ( ) ;
393
+ if ( ! configObj ) {
394
+ this . errorReporter . report ( NO_PROJECT_CONFIG_FAILURE , 'track' ) ;
399
395
return ;
400
396
}
401
397
402
398
if ( ! this . validateInputs ( { user_id : userId , event_key : eventKey } , attributes , eventTags ) ) {
403
399
return ;
404
400
}
405
401
406
- const configObj = this . projectConfigManager . getConfig ( ) ;
407
- if ( ! configObj ) {
408
- return ;
409
- }
410
-
411
402
412
403
if ( ! projectConfig . eventWithKeyExists ( configObj , eventKey ) ) {
413
404
this . logger ?. warn ( EVENT_KEY_NOT_FOUND , eventKey ) ;
@@ -453,8 +444,9 @@ export default class Optimizely implements Client {
453
444
*/
454
445
getVariation ( experimentKey : string , userId : string , attributes ?: UserAttributes ) : string | null {
455
446
try {
456
- if ( ! this . isValidInstance ( ) ) {
457
- this . logger ?. error ( INVALID_OBJECT , 'getVariation' ) ;
447
+ const configObj = this . projectConfigManager . getConfig ( ) ;
448
+ if ( ! configObj ) {
449
+ this . errorReporter . report ( NO_PROJECT_CONFIG_FAILURE , 'getVariation' ) ;
458
450
return null ;
459
451
}
460
452
@@ -463,11 +455,6 @@ export default class Optimizely implements Client {
463
455
return null ;
464
456
}
465
457
466
- const configObj = this . projectConfigManager . getConfig ( ) ;
467
- if ( ! configObj ) {
468
- return null ;
469
- }
470
-
471
458
const experiment = configObj . experimentKeyMap [ experimentKey ] ;
472
459
if ( ! experiment || experiment . isRollout ) {
473
460
this . logger ?. debug ( INVALID_EXPERIMENT_KEY_INFO , experimentKey ) ;
@@ -624,20 +611,16 @@ export default class Optimizely implements Client {
624
611
*/
625
612
isFeatureEnabled ( featureKey : string , userId : string , attributes ?: UserAttributes ) : boolean {
626
613
try {
627
- if ( ! this . isValidInstance ( ) ) {
628
- this . logger ?. error ( INVALID_OBJECT , 'isFeatureEnabled' ) ;
614
+ const configObj = this . projectConfigManager . getConfig ( ) ;
615
+ if ( ! configObj ) {
616
+ this . errorReporter . report ( NO_PROJECT_CONFIG_FAILURE , 'isFeatureEnabled' ) ;
629
617
return false ;
630
618
}
631
619
632
620
if ( ! this . validateInputs ( { feature_key : featureKey , user_id : userId } , attributes ) ) {
633
621
return false ;
634
622
}
635
623
636
- const configObj = this . projectConfigManager . getConfig ( ) ;
637
- if ( ! configObj ) {
638
- return false ;
639
- }
640
-
641
624
const feature = projectConfig . getFeatureFromKey ( configObj , featureKey , this . logger ) ;
642
625
if ( ! feature ) {
643
626
return false ;
@@ -704,17 +687,14 @@ export default class Optimizely implements Client {
704
687
getEnabledFeatures ( userId : string , attributes ?: UserAttributes ) : string [ ] {
705
688
try {
706
689
const enabledFeatures : string [ ] = [ ] ;
707
- if ( ! this . isValidInstance ( ) ) {
708
- this . logger ?. error ( INVALID_OBJECT , 'getEnabledFeatures' ) ;
709
- return enabledFeatures ;
710
- }
711
690
712
- if ( ! this . validateInputs ( { user_id : userId } ) ) {
691
+ const configObj = this . projectConfigManager . getConfig ( ) ;
692
+ if ( ! configObj ) {
693
+ this . errorReporter . report ( NO_PROJECT_CONFIG_FAILURE , 'getEnabledFeatures' ) ;
713
694
return enabledFeatures ;
714
695
}
715
696
716
- const configObj = this . projectConfigManager . getConfig ( ) ;
717
- if ( ! configObj ) {
697
+ if ( ! this . validateInputs ( { user_id : userId } ) ) {
718
698
return enabledFeatures ;
719
699
}
720
700
@@ -752,8 +732,8 @@ export default class Optimizely implements Client {
752
732
attributes ?: UserAttributes
753
733
) : FeatureVariableValue {
754
734
try {
755
- if ( ! this . isValidInstance ( ) ) {
756
- this . logger ?. error ( INVALID_OBJECT , 'getFeatureVariable' ) ;
735
+ if ( ! this . hasProjectConfig ( ) ) {
736
+ this . errorReporter . report ( NO_PROJECT_CONFIG_FAILURE , 'getFeatureVariable' ) ;
757
737
return null ;
758
738
}
759
739
return this . getFeatureVariableForType ( featureKey , variableKey , null , userId , attributes ) ;
@@ -946,8 +926,8 @@ export default class Optimizely implements Client {
946
926
attributes ?: UserAttributes
947
927
) : boolean | null {
948
928
try {
949
- if ( ! this . isValidInstance ( ) ) {
950
- this . logger ?. error ( INVALID_OBJECT , 'getFeatureVariableBoolean' ) ;
929
+ if ( ! this . hasProjectConfig ( ) ) {
930
+ this . errorReporter . report ( NO_PROJECT_CONFIG_FAILURE , 'getFeatureVariableBoolean' ) ;
951
931
return null ;
952
932
}
953
933
return this . getFeatureVariableForType (
@@ -984,8 +964,8 @@ export default class Optimizely implements Client {
984
964
attributes ?: UserAttributes
985
965
) : number | null {
986
966
try {
987
- if ( ! this . isValidInstance ( ) ) {
988
- this . logger ?. error ( INVALID_OBJECT , 'getFeatureVariableDouble' ) ;
967
+ if ( ! this . hasProjectConfig ( ) ) {
968
+ this . errorReporter . report ( NO_PROJECT_CONFIG_FAILURE , 'getFeatureVariableDouble' ) ;
989
969
return null ;
990
970
}
991
971
return this . getFeatureVariableForType (
@@ -1022,8 +1002,8 @@ export default class Optimizely implements Client {
1022
1002
attributes ?: UserAttributes
1023
1003
) : number | null {
1024
1004
try {
1025
- if ( ! this . isValidInstance ( ) ) {
1026
- this . logger ?. error ( INVALID_OBJECT , 'getFeatureVariableInteger' ) ;
1005
+ if ( ! this . hasProjectConfig ( ) ) {
1006
+ this . errorReporter . report ( NO_PROJECT_CONFIG_FAILURE , 'getFeatureVariableInteger' ) ;
1027
1007
return null ;
1028
1008
}
1029
1009
return this . getFeatureVariableForType (
@@ -1060,8 +1040,8 @@ export default class Optimizely implements Client {
1060
1040
attributes ?: UserAttributes
1061
1041
) : string | null {
1062
1042
try {
1063
- if ( ! this . isValidInstance ( ) ) {
1064
- this . logger ?. error ( INVALID_OBJECT , 'getFeatureVariableString' ) ;
1043
+ if ( ! this . hasProjectConfig ( ) ) {
1044
+ this . errorReporter . report ( NO_PROJECT_CONFIG_FAILURE , 'getFeatureVariableString' ) ;
1065
1045
return null ;
1066
1046
}
1067
1047
return this . getFeatureVariableForType (
@@ -1093,8 +1073,8 @@ export default class Optimizely implements Client {
1093
1073
*/
1094
1074
getFeatureVariableJSON ( featureKey : string , variableKey : string , userId : string , attributes : UserAttributes ) : unknown {
1095
1075
try {
1096
- if ( ! this . isValidInstance ( ) ) {
1097
- this . logger ?. error ( INVALID_OBJECT , 'getFeatureVariableJSON' ) ;
1076
+ if ( ! this . hasProjectConfig ( ) ) {
1077
+ this . errorReporter . report ( NO_PROJECT_CONFIG_FAILURE , 'getFeatureVariableJSON' ) ;
1098
1078
return null ;
1099
1079
}
1100
1080
return this . getFeatureVariableForType ( featureKey , variableKey , FEATURE_VARIABLE_TYPES . JSON , userId , attributes ) ;
@@ -1120,17 +1100,14 @@ export default class Optimizely implements Client {
1120
1100
attributes ?: UserAttributes
1121
1101
) : { [ variableKey : string ] : unknown } | null {
1122
1102
try {
1123
- if ( ! this . isValidInstance ( ) ) {
1124
- this . logger ?. error ( INVALID_OBJECT , 'getAllFeatureVariables' ) ;
1125
- return null ;
1126
- }
1103
+ const configObj = this . projectConfigManager . getConfig ( ) ;
1127
1104
1128
- if ( ! this . validateInputs ( { feature_key : featureKey , user_id : userId } , attributes ) ) {
1105
+ if ( ! configObj ) {
1106
+ this . errorReporter . report ( NO_PROJECT_CONFIG_FAILURE , 'getAllFeatureVariables' ) ;
1129
1107
return null ;
1130
1108
}
1131
1109
1132
- const configObj = this . projectConfigManager . getConfig ( ) ;
1133
- if ( ! configObj ) {
1110
+ if ( ! this . validateInputs ( { feature_key : featureKey , user_id : userId } , attributes ) ) {
1134
1111
return null ;
1135
1112
}
1136
1113
@@ -1425,8 +1402,8 @@ export default class Optimizely implements Client {
1425
1402
decide ( user : OptimizelyUserContext , key : string , options : OptimizelyDecideOption [ ] = [ ] ) : OptimizelyDecision {
1426
1403
const configObj = this . projectConfigManager . getConfig ( ) ;
1427
1404
1428
- if ( ! this . isValidInstance ( ) || ! configObj ) {
1429
- this . logger ?. error ( INVALID_OBJECT , 'decide' ) ;
1405
+ if ( ! configObj ) {
1406
+ this . errorReporter . report ( NO_PROJECT_CONFIG_FAILURE , 'decide' ) ;
1430
1407
return newErrorDecision ( key , user , [ DECISION_MESSAGES . SDK_NOT_READY ] ) ;
1431
1408
}
1432
1409
@@ -1570,8 +1547,8 @@ export default class Optimizely implements Client {
1570
1547
1571
1548
const configObj = this . projectConfigManager . getConfig ( )
1572
1549
1573
- if ( ! this . isValidInstance ( ) || ! configObj ) {
1574
- this . logger ?. error ( INVALID_OBJECT , 'decideForKeys' ) ;
1550
+ if ( ! configObj ) {
1551
+ this . errorReporter . report ( NO_PROJECT_CONFIG_FAILURE , 'decideForKeys' ) ;
1575
1552
return decisionMap ;
1576
1553
}
1577
1554
if ( keys . length === 0 ) {
@@ -1638,10 +1615,10 @@ export default class Optimizely implements Client {
1638
1615
user : OptimizelyUserContext ,
1639
1616
options : OptimizelyDecideOption [ ] = [ ]
1640
1617
) : { [ key : string ] : OptimizelyDecision } {
1641
- const configObj = this . projectConfigManager . getConfig ( ) ;
1642
1618
const decisionMap : { [ key : string ] : OptimizelyDecision } = { } ;
1643
- if ( ! this . isValidInstance ( ) || ! configObj ) {
1644
- this . logger ?. error ( INVALID_OBJECT , 'decideAll' ) ;
1619
+ const configObj = this . projectConfigManager . getConfig ( ) ;
1620
+ if ( ! configObj ) {
1621
+ this . errorReporter . report ( NO_PROJECT_CONFIG_FAILURE , 'decideAll' ) ;
1645
1622
return decisionMap ;
1646
1623
}
1647
1624
0 commit comments