6
6
* - OpenFeature.getClient().addHooks(new OpenFeatureIntegrationHook());
7
7
*/
8
8
import type { Client , Event , EventHint , IntegrationFn } from '@sentry/types' ;
9
- import type { EvaluationDetails , FlagValue , HookContext , HookHints , JsonValue , OpenFeatureHook } from './types' ;
9
+ import type { EvaluationDetails , HookContext , HookHints , JsonValue , OpenFeatureHook } from './types' ;
10
10
11
- import { defineIntegration , getCurrentScope } from '@sentry/core' ;
12
- import { insertToFlagBuffer } from '../../../utils/featureFlags' ;
11
+ import { defineIntegration } from '@sentry/core' ;
12
+ import { copyFlagsFromScopeToEvent , insertFlagToScope } from '../../../utils/featureFlags' ;
13
13
14
14
export const openFeatureIntegration = defineIntegration ( ( ) => {
15
15
return {
16
16
name : 'OpenFeature' ,
17
17
18
18
processEvent ( event : Event , _hint : EventHint , _client : Client ) : Event {
19
- const scope = getCurrentScope ( ) ;
20
- const flagContext = scope . getScopeData ( ) . contexts . flags ;
21
- const flagBuffer = flagContext ? flagContext . values : [ ] ;
22
-
23
- if ( event . contexts === undefined ) {
24
- event . contexts = { } ;
25
- }
26
- event . contexts . flags = { values : [ ...flagBuffer ] } ;
27
- return event ;
19
+ return copyFlagsFromScopeToEvent ( event ) ;
28
20
} ,
29
21
} ;
30
22
} ) satisfies IntegrationFn ;
@@ -37,25 +29,13 @@ export class OpenFeatureIntegrationHook implements OpenFeatureHook {
37
29
* Successful evaluation result.
38
30
*/
39
31
public after ( _hookContext : Readonly < HookContext < JsonValue > > , evaluationDetails : EvaluationDetails < JsonValue > ) : void {
40
- processEvent ( evaluationDetails . flagKey , evaluationDetails . value ) ;
32
+ insertFlagToScope ( evaluationDetails . flagKey , evaluationDetails . value ) ;
41
33
}
42
34
43
35
/**
44
36
* On error evaluation result.
45
37
*/
46
38
public error ( hookContext : Readonly < HookContext < JsonValue > > , _error : unknown , _hookHints ?: HookHints ) : void {
47
- processEvent ( hookContext . flagKey , hookContext . defaultValue ) ;
48
- }
49
- }
50
-
51
- function processEvent ( key : string , value : FlagValue ) : void {
52
- if ( typeof value === 'boolean' ) {
53
- const scopeContexts = getCurrentScope ( ) . getScopeData ( ) . contexts ;
54
- if ( ! scopeContexts . flags ) {
55
- scopeContexts . flags = { values : [ ] } ;
56
- }
57
- const flagBuffer = scopeContexts . flags . values ;
58
- insertToFlagBuffer ( flagBuffer , key , value ) ;
39
+ insertFlagToScope ( hookContext . flagKey , hookContext . defaultValue ) ;
59
40
}
60
- return ;
61
41
}
0 commit comments