1
1
/**
2
- * Copyright 2020-2024 , Optimizely
2
+ * Copyright 2020-2025 , Optimizely
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
@@ -39,8 +39,11 @@ export interface IOptimizelyUserContext {
39
39
getAttributes ( ) : UserAttributes ;
40
40
setAttribute ( key : string , value : unknown ) : void ;
41
41
decide ( key : string , options ?: OptimizelyDecideOption [ ] ) : OptimizelyDecision ;
42
+ decideAsync ( key : string , options ?: OptimizelyDecideOption [ ] ) : Promise < OptimizelyDecision > ;
42
43
decideForKeys ( keys : string [ ] , options ?: OptimizelyDecideOption [ ] ) : { [ key : string ] : OptimizelyDecision } ;
44
+ decideForKeysAsync ( keys : string [ ] , options ?: OptimizelyDecideOption [ ] ) : Promise < Record < string , OptimizelyDecision > > ;
43
45
decideAll ( options ?: OptimizelyDecideOption [ ] ) : { [ key : string ] : OptimizelyDecision } ;
46
+ decideAllAsync ( options ?: OptimizelyDecideOption [ ] ) : Promise < Record < string , OptimizelyDecision > > ;
44
47
trackEvent ( eventName : string , eventTags ?: EventTags ) : void ;
45
48
setForcedDecision ( context : OptimizelyDecisionContext , decision : OptimizelyForcedDecision ) : boolean ;
46
49
getForcedDecision ( context : OptimizelyDecisionContext ) : OptimizelyForcedDecision | null ;
@@ -60,7 +63,7 @@ export default class OptimizelyUserContext implements IOptimizelyUserContext {
60
63
constructor ( { optimizely, userId, attributes } : OptimizelyUserContextConfig ) {
61
64
this . optimizely = optimizely ;
62
65
this . userId = userId ;
63
- this . attributes = { ...attributes } ?? { } ;
66
+ this . attributes = { ...attributes } ;
64
67
this . forcedDecisionsMap = { } ;
65
68
}
66
69
@@ -104,6 +107,17 @@ export default class OptimizelyUserContext implements IOptimizelyUserContext {
104
107
return this . optimizely . decide ( this . cloneUserContext ( ) , key , options ) ;
105
108
}
106
109
110
+ /**
111
+ * Returns a promise that resolves in decision result for a given flag key and a user context, which contains all data required to deliver the flag.
112
+ * If the SDK finds an error, it will return a decision with null for variationKey. The decision will include an error message in reasons.
113
+ * @param {string } key A flag key for which a decision will be made.
114
+ * @param {OptimizelyDecideOption } options An array of options for decision-making.
115
+ * @return {Promise<OptimizelyDecision> } A Promise that resolves decision result.
116
+ */
117
+ decideAsync ( key : string , options ?: OptimizelyDecideOption [ ] ) : Promise < OptimizelyDecision > {
118
+ return this . optimizely . decideAsync ( this . cloneUserContext ( ) , key , options ) ;
119
+ }
120
+
107
121
/**
108
122
* Returns an object of decision results for multiple flag keys and a user context.
109
123
* If the SDK finds an error for a key, the response will include a decision for the key showing reasons for the error.
@@ -116,6 +130,17 @@ export default class OptimizelyUserContext implements IOptimizelyUserContext {
116
130
return this . optimizely . decideForKeys ( this . cloneUserContext ( ) , keys , options ) ;
117
131
}
118
132
133
+ /**
134
+ * Returns a promise that resolves in an object of decision results for multiple flag keys and a user context.
135
+ * If the SDK finds an error for a key, the response will include a decision for the key showing reasons for the error.
136
+ * The SDK will always return key-mapped decisions. When it cannot process requests, it will return an empty map after logging the errors.
137
+ * @param {string[] } keys An array of flag keys for which decisions will be made.
138
+ * @param {OptimizelyDecideOption[] } options An array of options for decision-making.
139
+ * @return {Promise<Record<string, OptimizelyDecision>> } A promise that resolves in an object of decision results mapped by flag keys.
140
+ */
141
+ decideForKeysAsync ( keys : string [ ] , options ?: OptimizelyDecideOption [ ] ) : Promise < Record < string , OptimizelyDecision > > {
142
+ return this . optimizely . decideForKeysAsync ( this . cloneUserContext ( ) , keys , options ) ;
143
+ }
119
144
/**
120
145
* Returns an object of decision results for all active flag keys.
121
146
* @param {OptimizelyDecideOption[] } options An array of options for decision-making.
@@ -125,6 +150,15 @@ export default class OptimizelyUserContext implements IOptimizelyUserContext {
125
150
return this . optimizely . decideAll ( this . cloneUserContext ( ) , options ) ;
126
151
}
127
152
153
+ /**
154
+ * Returns a promise that resolves in an object of decision results for all active flag keys.
155
+ * @param {OptimizelyDecideOption[] } options An array of options for decision-making.
156
+ * @return {Promise<Record<string ,OptimizelyDecision>> } A promise that resolves in an object of all decision results mapped by flag keys.
157
+ */
158
+ decideAllAsync ( options : OptimizelyDecideOption [ ] = [ ] ) : Promise < Record < string , OptimizelyDecision > > {
159
+ return this . optimizely . decideAllAsync ( this . cloneUserContext ( ) , options ) ;
160
+ }
161
+
128
162
/**
129
163
* Tracks an event.
130
164
* @param {string } eventName The event name.
0 commit comments