@@ -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 ,
@@ -265,16 +266,6 @@ export default class Optimizely implements Client {
265
266
return this . projectConfigManager . getConfig ( ) || null ;
266
267
}
267
268
268
- /**
269
- * 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
- * @return {boolean }
273
- */
274
- isValidInstance ( ) : boolean {
275
- return ! ! this . projectConfigManager . getConfig ( ) ;
276
- }
277
-
278
269
/**
279
270
* Buckets visitor and sends impression event to Optimizely.
280
271
* @param {string } experimentKey
@@ -284,20 +275,16 @@ export default class Optimizely implements Client {
284
275
*/
285
276
activate ( experimentKey : string , userId : string , attributes ?: UserAttributes ) : string | null {
286
277
try {
287
- if ( ! this . isValidInstance ( ) ) {
288
- this . logger ?. error ( INVALID_OBJECT , 'activate' ) ;
278
+ const configObj = this . getProjectConfig ( ) ;
279
+ if ( ! configObj ) {
280
+ this . errorReporter . report ( NO_PROJECT_CONFIG_FAILURE , 'activate' ) ;
289
281
return null ;
290
282
}
291
283
292
284
if ( ! this . validateInputs ( { experiment_key : experimentKey , user_id : userId } , attributes ) ) {
293
285
return this . notActivatingExperiment ( experimentKey , userId ) ;
294
286
}
295
287
296
- const configObj = this . projectConfigManager . getConfig ( ) ;
297
- if ( ! configObj ) {
298
- return null ;
299
- }
300
-
301
288
try {
302
289
const variationKey = this . getVariation ( experimentKey , userId , attributes ) ;
303
290
if ( variationKey === null ) {
@@ -353,7 +340,7 @@ export default class Optimizely implements Client {
353
340
return ;
354
341
}
355
342
356
- const configObj = this . projectConfigManager . getConfig ( ) ;
343
+ const configObj = this . getProjectConfig ( ) ;
357
344
if ( ! configObj ) {
358
345
return ;
359
346
}
@@ -394,20 +381,16 @@ export default class Optimizely implements Client {
394
381
return ;
395
382
}
396
383
397
- if ( ! this . isValidInstance ( ) ) {
398
- this . logger ?. error ( INVALID_OBJECT , 'track' ) ;
384
+ const configObj = this . getProjectConfig ( ) ;
385
+ if ( ! configObj ) {
386
+ this . errorReporter . report ( NO_PROJECT_CONFIG_FAILURE , 'track' ) ;
399
387
return ;
400
388
}
401
389
402
390
if ( ! this . validateInputs ( { user_id : userId , event_key : eventKey } , attributes , eventTags ) ) {
403
391
return ;
404
392
}
405
393
406
- const configObj = this . projectConfigManager . getConfig ( ) ;
407
- if ( ! configObj ) {
408
- return ;
409
- }
410
-
411
394
412
395
if ( ! projectConfig . eventWithKeyExists ( configObj , eventKey ) ) {
413
396
this . logger ?. warn ( EVENT_KEY_NOT_FOUND , eventKey ) ;
@@ -453,8 +436,9 @@ export default class Optimizely implements Client {
453
436
*/
454
437
getVariation ( experimentKey : string , userId : string , attributes ?: UserAttributes ) : string | null {
455
438
try {
456
- if ( ! this . isValidInstance ( ) ) {
457
- this . logger ?. error ( INVALID_OBJECT , 'getVariation' ) ;
439
+ const configObj = this . getProjectConfig ( ) ;
440
+ if ( ! configObj ) {
441
+ this . errorReporter . report ( NO_PROJECT_CONFIG_FAILURE , 'getVariation' ) ;
458
442
return null ;
459
443
}
460
444
@@ -463,11 +447,6 @@ export default class Optimizely implements Client {
463
447
return null ;
464
448
}
465
449
466
- const configObj = this . projectConfigManager . getConfig ( ) ;
467
- if ( ! configObj ) {
468
- return null ;
469
- }
470
-
471
450
const experiment = configObj . experimentKeyMap [ experimentKey ] ;
472
451
if ( ! experiment || experiment . isRollout ) {
473
452
this . logger ?. debug ( INVALID_EXPERIMENT_KEY_INFO , experimentKey ) ;
@@ -517,7 +496,7 @@ export default class Optimizely implements Client {
517
496
return false ;
518
497
}
519
498
520
- const configObj = this . projectConfigManager . getConfig ( ) ;
499
+ const configObj = this . getProjectConfig ( ) ;
521
500
if ( ! configObj ) {
522
501
return false ;
523
502
}
@@ -541,7 +520,7 @@ export default class Optimizely implements Client {
541
520
return null ;
542
521
}
543
522
544
- const configObj = this . projectConfigManager . getConfig ( ) ;
523
+ const configObj = this . getProjectConfig ( ) ;
545
524
if ( ! configObj ) {
546
525
return null ;
547
526
}
@@ -624,20 +603,16 @@ export default class Optimizely implements Client {
624
603
*/
625
604
isFeatureEnabled ( featureKey : string , userId : string , attributes ?: UserAttributes ) : boolean {
626
605
try {
627
- if ( ! this . isValidInstance ( ) ) {
628
- this . logger ?. error ( INVALID_OBJECT , 'isFeatureEnabled' ) ;
606
+ const configObj = this . getProjectConfig ( ) ;
607
+ if ( ! configObj ) {
608
+ this . errorReporter . report ( NO_PROJECT_CONFIG_FAILURE , 'isFeatureEnabled' ) ;
629
609
return false ;
630
610
}
631
611
632
612
if ( ! this . validateInputs ( { feature_key : featureKey , user_id : userId } , attributes ) ) {
633
613
return false ;
634
614
}
635
615
636
- const configObj = this . projectConfigManager . getConfig ( ) ;
637
- if ( ! configObj ) {
638
- return false ;
639
- }
640
-
641
616
const feature = projectConfig . getFeatureFromKey ( configObj , featureKey , this . logger ) ;
642
617
if ( ! feature ) {
643
618
return false ;
@@ -704,17 +679,14 @@ export default class Optimizely implements Client {
704
679
getEnabledFeatures ( userId : string , attributes ?: UserAttributes ) : string [ ] {
705
680
try {
706
681
const enabledFeatures : string [ ] = [ ] ;
707
- if ( ! this . isValidInstance ( ) ) {
708
- this . logger ?. error ( INVALID_OBJECT , 'getEnabledFeatures' ) ;
709
- return enabledFeatures ;
710
- }
711
682
712
- if ( ! this . validateInputs ( { user_id : userId } ) ) {
683
+ const configObj = this . getProjectConfig ( ) ;
684
+ if ( ! configObj ) {
685
+ this . errorReporter . report ( NO_PROJECT_CONFIG_FAILURE , 'getEnabledFeatures' ) ;
713
686
return enabledFeatures ;
714
687
}
715
688
716
- const configObj = this . projectConfigManager . getConfig ( ) ;
717
- if ( ! configObj ) {
689
+ if ( ! this . validateInputs ( { user_id : userId } ) ) {
718
690
return enabledFeatures ;
719
691
}
720
692
@@ -752,8 +724,8 @@ export default class Optimizely implements Client {
752
724
attributes ?: UserAttributes
753
725
) : FeatureVariableValue {
754
726
try {
755
- if ( ! this . isValidInstance ( ) ) {
756
- this . logger ?. error ( INVALID_OBJECT , 'getFeatureVariable' ) ;
727
+ if ( ! this . getProjectConfig ( ) ) {
728
+ this . errorReporter . report ( NO_PROJECT_CONFIG_FAILURE , 'getFeatureVariable' ) ;
757
729
return null ;
758
730
}
759
731
return this . getFeatureVariableForType ( featureKey , variableKey , null , userId , attributes ) ;
@@ -796,7 +768,7 @@ export default class Optimizely implements Client {
796
768
return null ;
797
769
}
798
770
799
- const configObj = this . projectConfigManager . getConfig ( ) ;
771
+ const configObj = this . getProjectConfig ( ) ;
800
772
if ( ! configObj ) {
801
773
return null ;
802
774
}
@@ -882,7 +854,7 @@ export default class Optimizely implements Client {
882
854
variable : FeatureVariable ,
883
855
userId : string
884
856
) : FeatureVariableValue {
885
- const configObj = this . projectConfigManager . getConfig ( ) ;
857
+ const configObj = this . getProjectConfig ( ) ;
886
858
if ( ! configObj ) {
887
859
return null ;
888
860
}
@@ -946,8 +918,8 @@ export default class Optimizely implements Client {
946
918
attributes ?: UserAttributes
947
919
) : boolean | null {
948
920
try {
949
- if ( ! this . isValidInstance ( ) ) {
950
- this . logger ?. error ( INVALID_OBJECT , 'getFeatureVariableBoolean' ) ;
921
+ if ( ! this . getProjectConfig ( ) ) {
922
+ this . errorReporter . report ( NO_PROJECT_CONFIG_FAILURE , 'getFeatureVariableBoolean' ) ;
951
923
return null ;
952
924
}
953
925
return this . getFeatureVariableForType (
@@ -984,8 +956,8 @@ export default class Optimizely implements Client {
984
956
attributes ?: UserAttributes
985
957
) : number | null {
986
958
try {
987
- if ( ! this . isValidInstance ( ) ) {
988
- this . logger ?. error ( INVALID_OBJECT , 'getFeatureVariableDouble' ) ;
959
+ if ( ! this . getProjectConfig ( ) ) {
960
+ this . errorReporter . report ( NO_PROJECT_CONFIG_FAILURE , 'getFeatureVariableDouble' ) ;
989
961
return null ;
990
962
}
991
963
return this . getFeatureVariableForType (
@@ -1022,8 +994,8 @@ export default class Optimizely implements Client {
1022
994
attributes ?: UserAttributes
1023
995
) : number | null {
1024
996
try {
1025
- if ( ! this . isValidInstance ( ) ) {
1026
- this . logger ?. error ( INVALID_OBJECT , 'getFeatureVariableInteger' ) ;
997
+ if ( ! this . getProjectConfig ( ) ) {
998
+ this . errorReporter . report ( NO_PROJECT_CONFIG_FAILURE , 'getFeatureVariableInteger' ) ;
1027
999
return null ;
1028
1000
}
1029
1001
return this . getFeatureVariableForType (
@@ -1060,8 +1032,8 @@ export default class Optimizely implements Client {
1060
1032
attributes ?: UserAttributes
1061
1033
) : string | null {
1062
1034
try {
1063
- if ( ! this . isValidInstance ( ) ) {
1064
- this . logger ?. error ( INVALID_OBJECT , 'getFeatureVariableString' ) ;
1035
+ if ( ! this . getProjectConfig ( ) ) {
1036
+ this . errorReporter . report ( NO_PROJECT_CONFIG_FAILURE , 'getFeatureVariableString' ) ;
1065
1037
return null ;
1066
1038
}
1067
1039
return this . getFeatureVariableForType (
@@ -1093,8 +1065,8 @@ export default class Optimizely implements Client {
1093
1065
*/
1094
1066
getFeatureVariableJSON ( featureKey : string , variableKey : string , userId : string , attributes : UserAttributes ) : unknown {
1095
1067
try {
1096
- if ( ! this . isValidInstance ( ) ) {
1097
- this . logger ?. error ( INVALID_OBJECT , 'getFeatureVariableJSON' ) ;
1068
+ if ( ! this . getProjectConfig ( ) ) {
1069
+ this . errorReporter . report ( NO_PROJECT_CONFIG_FAILURE , 'getFeatureVariableJSON' ) ;
1098
1070
return null ;
1099
1071
}
1100
1072
return this . getFeatureVariableForType ( featureKey , variableKey , FEATURE_VARIABLE_TYPES . JSON , userId , attributes ) ;
@@ -1120,17 +1092,14 @@ export default class Optimizely implements Client {
1120
1092
attributes ?: UserAttributes
1121
1093
) : { [ variableKey : string ] : unknown } | null {
1122
1094
try {
1123
- if ( ! this . isValidInstance ( ) ) {
1124
- this . logger ?. error ( INVALID_OBJECT , 'getAllFeatureVariables' ) ;
1125
- return null ;
1126
- }
1095
+ const configObj = this . getProjectConfig ( ) ;
1127
1096
1128
- if ( ! this . validateInputs ( { feature_key : featureKey , user_id : userId } , attributes ) ) {
1097
+ if ( ! configObj ) {
1098
+ this . errorReporter . report ( NO_PROJECT_CONFIG_FAILURE , 'getAllFeatureVariables' ) ;
1129
1099
return null ;
1130
1100
}
1131
1101
1132
- const configObj = this . projectConfigManager . getConfig ( ) ;
1133
- if ( ! configObj ) {
1102
+ if ( ! this . validateInputs ( { feature_key : featureKey , user_id : userId } , attributes ) ) {
1134
1103
return null ;
1135
1104
}
1136
1105
@@ -1224,7 +1193,7 @@ export default class Optimizely implements Client {
1224
1193
*/
1225
1194
getOptimizelyConfig ( ) : OptimizelyConfig | null {
1226
1195
try {
1227
- const configObj = this . projectConfigManager . getConfig ( ) ;
1196
+ const configObj = this . getProjectConfig ( ) ;
1228
1197
if ( ! configObj ) {
1229
1198
return null ;
1230
1199
}
@@ -1423,10 +1392,10 @@ export default class Optimizely implements Client {
1423
1392
}
1424
1393
1425
1394
decide ( user : OptimizelyUserContext , key : string , options : OptimizelyDecideOption [ ] = [ ] ) : OptimizelyDecision {
1426
- const configObj = this . projectConfigManager . getConfig ( ) ;
1395
+ const configObj = this . getProjectConfig ( ) ;
1427
1396
1428
- if ( ! this . isValidInstance ( ) || ! configObj ) {
1429
- this . logger ?. error ( INVALID_OBJECT , 'decide' ) ;
1397
+ if ( ! configObj ) {
1398
+ this . errorReporter . report ( NO_PROJECT_CONFIG_FAILURE , 'decide' ) ;
1430
1399
return newErrorDecision ( key , user , [ DECISION_MESSAGES . SDK_NOT_READY ] ) ;
1431
1400
}
1432
1401
@@ -1568,10 +1537,10 @@ export default class Optimizely implements Client {
1568
1537
const flagsWithoutForcedDecision = [ ] ;
1569
1538
const validKeys = [ ] ;
1570
1539
1571
- const configObj = this . projectConfigManager . getConfig ( )
1540
+ const configObj = this . getProjectConfig ( )
1572
1541
1573
- if ( ! this . isValidInstance ( ) || ! configObj ) {
1574
- this . logger ?. error ( INVALID_OBJECT , 'decideForKeys' ) ;
1542
+ if ( ! configObj ) {
1543
+ this . errorReporter . report ( NO_PROJECT_CONFIG_FAILURE , 'decideForKeys' ) ;
1575
1544
return decisionMap ;
1576
1545
}
1577
1546
if ( keys . length === 0 ) {
@@ -1638,10 +1607,10 @@ export default class Optimizely implements Client {
1638
1607
user : OptimizelyUserContext ,
1639
1608
options : OptimizelyDecideOption [ ] = [ ]
1640
1609
) : { [ key : string ] : OptimizelyDecision } {
1641
- const configObj = this . projectConfigManager . getConfig ( ) ;
1642
1610
const decisionMap : { [ key : string ] : OptimizelyDecision } = { } ;
1643
- if ( ! this . isValidInstance ( ) || ! configObj ) {
1644
- this . logger ?. error ( INVALID_OBJECT , 'decideAll' ) ;
1611
+ const configObj = this . getProjectConfig ( ) ;
1612
+ if ( ! configObj ) {
1613
+ this . errorReporter . report ( NO_PROJECT_CONFIG_FAILURE , 'decideAll' ) ;
1645
1614
return decisionMap ;
1646
1615
}
1647
1616
@@ -1654,7 +1623,7 @@ export default class Optimizely implements Client {
1654
1623
* Updates ODP Config with most recent ODP key, host, pixelUrl, and segments from the project config
1655
1624
*/
1656
1625
private updateOdpSettings ( ) : void {
1657
- const projectConfig = this . projectConfigManager . getConfig ( ) ;
1626
+ const projectConfig = this . getProjectConfig ( ) ;
1658
1627
1659
1628
if ( ! projectConfig ) {
1660
1629
return ;
@@ -1696,7 +1665,7 @@ export default class Optimizely implements Client {
1696
1665
* @returns { boolean } `true` if ODP settings were found in the datafile otherwise `false`
1697
1666
*/
1698
1667
public isOdpIntegrated ( ) : boolean {
1699
- return this . projectConfigManager . getConfig ( ) ?. odpIntegrationConfig ?. integrated ?? false ;
1668
+ return this . getProjectConfig ( ) ?. odpIntegrationConfig ?. integrated ?? false ;
1700
1669
}
1701
1670
1702
1671
/**
0 commit comments