@@ -14,6 +14,8 @@ import {
1414import { FeatureFlag } from './feature-flag' ;
1515import { MemoryStorage , Storage } from './storage' ;
1616
17+ type ResolutionDetailsWithFlagMetadata < T > = Required < Pick < ResolutionDetails < T > , 'flagMetadata' > > & ResolutionDetails < T > ;
18+
1719/**
1820 * Expose flag configuration setter and flag resolving methods.
1921 */
@@ -26,16 +28,6 @@ export class FlagdCore implements Storage {
2628 this . _storage = storage ? storage : new MemoryStorage ( this . _logger ) ;
2729 }
2830
29- /**
30- * Sets the logger for the FlagdCore instance.
31- * @param logger - The logger to be set.
32- * @returns - The FlagdCore instance with the logger set.
33- */
34- setLogger ( logger : Logger ) {
35- this . _logger = new SafeLogger ( logger ) ;
36- return this ;
37- }
38-
3931 setConfigurations ( cfg : string ) : string [ ] {
4032 return this . _storage . setConfigurations ( cfg ) ;
4133 }
@@ -48,20 +40,22 @@ export class FlagdCore implements Storage {
4840 return this . _storage . getFlags ( ) ;
4941 }
5042
43+ getFlagSetMetadata ( ) : { flagSetId ?: string ; flagSetVersion ?: string } {
44+ return this . _storage . getFlagSetMetadata ( ) ;
45+ }
46+
5147 /**
5248 * Resolve the flag evaluation to a boolean value.
5349 * @param flagKey - The key of the flag to be evaluated.
5450 * @param defaultValue - The default value to be returned if the flag is not found.
5551 * @param evalCtx - The evaluation context to be used for targeting.
56- * @param logger - The logger to be used to troubleshoot targeting errors. Overrides the default logger.
5752 * @returns - The resolved value and the reason for the resolution.
5853 */
5954 resolveBooleanEvaluation (
6055 flagKey : string ,
6156 defaultValue : boolean ,
6257 evalCtx ?: EvaluationContext ,
63- // logger?: Logger,
64- ) : ResolutionDetails < boolean > {
58+ ) : ResolutionDetailsWithFlagMetadata < boolean > {
6559 return this . resolve ( 'boolean' , flagKey , defaultValue , evalCtx ) ;
6660 }
6761
@@ -70,15 +64,13 @@ export class FlagdCore implements Storage {
7064 * @param flagKey - The key of the flag to be evaluated.
7165 * @param defaultValue - The default value to be returned if the flag is not found.
7266 * @param evalCtx - The evaluation context to be used for targeting.
73- * @param logger - The logger to be used to troubleshoot targeting errors. Overrides the default logger.
7467 * @returns - The resolved value and the reason for the resolution.
7568 */
7669 resolveStringEvaluation (
7770 flagKey : string ,
7871 defaultValue : string ,
7972 evalCtx ?: EvaluationContext ,
80- // logger?: Logger,
81- ) : ResolutionDetails < string > {
73+ ) : ResolutionDetailsWithFlagMetadata < string > {
8274 return this . resolve ( 'string' , flagKey , defaultValue , evalCtx ) ;
8375 }
8476
@@ -87,15 +79,13 @@ export class FlagdCore implements Storage {
8779 * @param flagKey - The key of the flag to evaluate.
8880 * @param defaultValue - The default value to return if the flag is not found or the evaluation fails.
8981 * @param evalCtx - The evaluation context to be used for targeting.
90- * @param logger - The logger to be used to troubleshoot targeting errors. Overrides the default logger.
9182 * @returns - The resolved value and the reason for the resolution.
9283 */
9384 resolveNumberEvaluation (
9485 flagKey : string ,
9586 defaultValue : number ,
9687 evalCtx ?: EvaluationContext ,
97- // logger?: Logger,
98- ) : ResolutionDetails < number > {
88+ ) : ResolutionDetailsWithFlagMetadata < number > {
9989 return this . resolve ( 'number' , flagKey , defaultValue , evalCtx ) ;
10090 }
10191
@@ -105,26 +95,22 @@ export class FlagdCore implements Storage {
10595 * @param flagKey - The key of the flag to resolve.
10696 * @param defaultValue - The default value to use if the flag is not found.
10797 * @param evalCtx - The evaluation context to be used for targeting.
108- * @param logger - The logger to be used to troubleshoot targeting errors. Overrides the default logger.
10998 * @returns - The resolved value and the reason for the resolution.
11099 */
111100 resolveObjectEvaluation < T extends JsonValue > (
112101 flagKey : string ,
113102 defaultValue : T ,
114103 evalCtx ?: EvaluationContext ,
115- // logger?: Logger,
116- ) : ResolutionDetails < T > {
104+ ) : ResolutionDetailsWithFlagMetadata < T > {
117105 return this . resolve ( 'object' , flagKey , defaultValue , evalCtx ) ;
118106 }
119107
120108 /**
121109 * Resolve the flag evaluation for all enabled flags.
122110 * @param evalCtx - The evaluation context to be used for targeting.
123- * @param logger - The logger to be used to troubleshoot targeting errors. Overrides the default logger.
124111 * @returns - The list of evaluation details for all enabled flags.
125112 */
126113 resolveAll ( evalCtx : EvaluationContext = { } ) : EvaluationDetails < JsonValue > [ ] {
127- // logger ??= this._logger;
128114 const values : EvaluationDetails < JsonValue > [ ] = [ ] ;
129115 for ( const [ key , flag ] of this . getFlags ( ) ) {
130116 try {
@@ -151,7 +137,6 @@ export class FlagdCore implements Storage {
151137 * @param {string } flagKey - The key of the flag.
152138 * @param {T } defaultValue - The default value of the flag.
153139 * @param {EvaluationContext } evalCtx - The evaluation context for targeting rules.
154- * @param {Logger } [logger] - The optional logger for logging errors.
155140 * @returns {ResolutionDetails<T> } - The resolved value and the reason for the resolution.
156141 * @throws {FlagNotFoundError } - If the flag with the given key is not found.
157142 * @throws {TypeMismatchError } - If the evaluated type of the flag does not match the expected type.
@@ -162,8 +147,7 @@ export class FlagdCore implements Storage {
162147 flagKey : string ,
163148 defaultValue : T ,
164149 evalCtx : EvaluationContext = { } ,
165- // logger?: Logger,
166- ) : ResolutionDetails < T > {
150+ ) : ResolutionDetailsWithFlagMetadata < T > {
167151 const flag = this . _storage . getFlag ( flagKey ) ;
168152 // flag exist check
169153 if ( ! flag ) {
@@ -172,14 +156,17 @@ export class FlagdCore implements Storage {
172156 reason : StandardResolutionReasons . ERROR ,
173157 errorCode : ErrorCode . FLAG_NOT_FOUND ,
174158 errorMessage : `flag '${ flagKey } ' not found` ,
159+ flagMetadata : this . _storage . getFlagSetMetadata ( ) ,
175160 } ;
176161 }
177162
178163 // flag status check
179164 if ( flag . state === 'DISABLED' ) {
180165 return {
181166 value : defaultValue ,
182- reason : StandardResolutionReasons . DISABLED ,
167+ reason : StandardResolutionReasons . ERROR ,
168+ errorCode : ErrorCode . FLAG_NOT_FOUND ,
169+ errorMessage : `flag '${ flagKey } ' is disabled` ,
183170 flagMetadata : flag . metadata ,
184171 } ;
185172 }
@@ -200,6 +187,7 @@ export class FlagdCore implements Storage {
200187 value : value as T ,
201188 reason,
202189 variant,
190+ flagMetadata : flag . metadata ,
203191 } ;
204192 }
205193}
0 commit comments