@@ -4,9 +4,10 @@ import {
44 EvaluationResultWithMetadata ,
55 FeatureContextWithMetadata ,
66 SDKFeatureMetadata ,
7- FlagResultWithMetadata
7+ FlagResultWithMetadata ,
8+ GenericEvaluationContext
89} from './evaluation/models.js' ;
9- import { getIdentitySegments , getIdentityKey } from './segments/evaluators.js' ;
10+ import { getIdentitySegments } from './segments/evaluators.js' ;
1011import { EvaluationResultFlags } from './evaluation/models.js' ;
1112import { TARGETING_REASONS } from './features/types.js' ;
1213import { getHashedPercentageForObjIds } from './utils/hashing/index.js' ;
@@ -37,12 +38,29 @@ export type SegmentOverrides = Record<string, SegmentOverride>;
3738export function getEvaluationResult (
3839 context : EvaluationContextWithMetadata
3940) : EvaluationResultWithMetadata {
40- const { segments, segmentOverrides } = evaluateSegments ( context ) ;
41- const flags = evaluateFeatures ( context , segmentOverrides ) ;
41+ const enrichedContext = getEnrichedContext ( context ) ;
42+ const { segments, segmentOverrides } = evaluateSegments ( enrichedContext ) ;
43+ const flags = evaluateFeatures ( enrichedContext , segmentOverrides ) ;
4244
4345 return { flags, segments } ;
4446}
4547
48+ function getEnrichedContext ( context : EvaluationContextWithMetadata ) : EvaluationContextWithMetadata {
49+ const identityKey = getIdentityKey ( context ) ;
50+ if ( ! identityKey ) return context ;
51+
52+ return {
53+ ...context ,
54+ ...( context . identity && {
55+ identity : {
56+ identifier : context . identity . identifier ,
57+ key : identityKey ,
58+ traits : context . identity . traits || { }
59+ }
60+ } )
61+ } ;
62+ }
63+
4664/**
4765 * Evaluates which segments the identity belongs to and collects feature overrides.
4866 *
@@ -234,3 +252,8 @@ const getTargetingMatchReason = (matchObject: TargetingMatchReason) => {
234252
235253 return TARGETING_REASONS . DEFAULT ;
236254} ;
255+
256+ const getIdentityKey = ( context : GenericEvaluationContext ) : string | undefined => {
257+ if ( ! context . identity ) return undefined ;
258+ return context . identity . key || `${ context . environment . key } _${ context . identity ?. identifier } ` ;
259+ } ;
0 commit comments