@@ -17,7 +17,7 @@ import {
1717import { decodePrecomputedFlag } from '../decoding' ;
1818import { FlagEvaluationWithoutDetails } from '../evaluator' ;
1919import FetchHttpClient from '../http-client' ;
20- import { FormatEnum , PrecomputedFlag } from '../interfaces' ;
20+ import { PrecomputedFlag , VariationType } from '../interfaces' ;
2121import { getMD5Hash } from '../obfuscation' ;
2222import initPoller , { IPoller } from '../poller' ;
2323import PrecomputedRequestor from '../precomputed-requestor' ;
@@ -130,16 +130,12 @@ export default class EppoPrecomputedClient {
130130 }
131131 }
132132
133- /**
134- * Maps a subject to a string variation for a given experiment.
135- *
136- * @param flagKey feature flag identifier
137- * @param defaultValue default value to return if the subject is not part of the experiment sample
138- * The subject attributes are used for evaluating any targeting rules tied to the experiment.
139- * @returns a variation value if a flag was precomputed for the subject, otherwise the default value
140- * @public
141- */
142- public getStringAssignment ( flagKey : string , defaultValue : string ) : string {
133+ private getPrecomputedAssignment < T > (
134+ flagKey : string ,
135+ defaultValue : T ,
136+ expectedType : VariationType ,
137+ valueTransformer : ( value : unknown ) => T = ( v ) => v as T ,
138+ ) : T {
143139 validateNotBlank ( flagKey , 'Invalid argument: flagKey cannot be blank' ) ;
144140
145141 const preComputedFlag = this . getPrecomputedFlag ( flagKey ) ;
@@ -149,6 +145,14 @@ export default class EppoPrecomputedClient {
149145 return defaultValue ;
150146 }
151147
148+ // Check variation type
149+ if ( preComputedFlag . variationType !== expectedType ) {
150+ logger . error (
151+ `[Eppo SDK] Type mismatch: expected ${ expectedType } but flag ${ flagKey } has type ${ preComputedFlag . variationType } ` ,
152+ ) ;
153+ return defaultValue ;
154+ }
155+
152156 const result : FlagEvaluationWithoutDetails = {
153157 flagKey,
154158 format : this . precomputedFlagStore . getFormat ( ) ?? '' ,
@@ -170,7 +174,77 @@ export default class EppoPrecomputedClient {
170174 } catch ( error ) {
171175 logger . error ( `[Eppo SDK] Error logging assignment event: ${ error } ` ) ;
172176 }
173- return ( result . variation ?. value as string ) ?? defaultValue ;
177+
178+ try {
179+ return result . variation ?. value !== undefined
180+ ? valueTransformer ( result . variation . value )
181+ : defaultValue ;
182+ } catch ( error ) {
183+ logger . error ( `[Eppo SDK] Error transforming value: ${ error } ` ) ;
184+ return defaultValue ;
185+ }
186+ }
187+
188+ /**
189+ * Maps a subject to a string variation for a given experiment.
190+ *
191+ * @param flagKey feature flag identifier
192+ * @param defaultValue default value to return if the subject is not part of the experiment sample
193+ * @returns a variation value if a flag was precomputed for the subject, otherwise the default value
194+ * @public
195+ */
196+ public getStringAssignment ( flagKey : string , defaultValue : string ) : string {
197+ return this . getPrecomputedAssignment ( flagKey , defaultValue , VariationType . STRING ) ;
198+ }
199+
200+ /**
201+ * Maps a subject to a boolean variation for a given experiment.
202+ *
203+ * @param flagKey feature flag identifier
204+ * @param defaultValue default value to return if the subject is not part of the experiment sample
205+ * @returns a variation value if a flag was precomputed for the subject, otherwise the default value
206+ * @public
207+ */
208+ public getBooleanAssignment ( flagKey : string , defaultValue : boolean ) : boolean {
209+ return this . getPrecomputedAssignment ( flagKey , defaultValue , VariationType . BOOLEAN ) ;
210+ }
211+
212+ /**
213+ * Maps a subject to an integer variation for a given experiment.
214+ *
215+ * @param flagKey feature flag identifier
216+ * @param defaultValue default value to return if the subject is not part of the experiment sample
217+ * @returns a variation value if a flag was precomputed for the subject, otherwise the default value
218+ * @public
219+ */
220+ public getIntegerAssignment ( flagKey : string , defaultValue : number ) : number {
221+ return this . getPrecomputedAssignment ( flagKey , defaultValue , VariationType . INTEGER ) ;
222+ }
223+
224+ /**
225+ * Maps a subject to a numeric (floating point) variation for a given experiment.
226+ *
227+ * @param flagKey feature flag identifier
228+ * @param defaultValue default value to return if the subject is not part of the experiment sample
229+ * @returns a variation value if a flag was precomputed for the subject, otherwise the default value
230+ * @public
231+ */
232+ public getNumericAssignment ( flagKey : string , defaultValue : number ) : number {
233+ return this . getPrecomputedAssignment ( flagKey , defaultValue , VariationType . NUMERIC ) ;
234+ }
235+
236+ /**
237+ * Maps a subject to a JSON object variation for a given experiment.
238+ *
239+ * @param flagKey feature flag identifier
240+ * @param defaultValue default value to return if the subject is not part of the experiment sample
241+ * @returns a parsed JSON object if a flag was precomputed for the subject, otherwise the default value
242+ * @public
243+ */
244+ public getJSONAssignment ( flagKey : string , defaultValue : object ) : object {
245+ return this . getPrecomputedAssignment ( flagKey , defaultValue , VariationType . JSON , ( value ) =>
246+ typeof value === 'string' ? JSON . parse ( value ) : defaultValue ,
247+ ) ;
174248 }
175249
176250 private getPrecomputedFlag ( flagKey : string ) : PrecomputedFlag | null {
0 commit comments