@@ -6,34 +6,40 @@ import {
6
6
ParseError ,
7
7
Provider ,
8
8
ProviderEvents ,
9
+ ProviderStatus ,
9
10
ResolutionDetails ,
10
11
ResolutionReason ,
11
12
StandardResolutionReasons ,
12
13
TypeMismatchError ,
13
14
} from '@openfeature/js-sdk' ;
14
- import { getClient , IConfigCatClient , IEvaluationDetails , SettingValue } from 'configcat-js' ;
15
+ import { getClient , IConfig , IConfigCatClient , IEvaluationDetails , SettingValue } from 'configcat-js' ;
15
16
import { transformContext } from './context-transformer' ;
16
17
17
18
export class ConfigCatProvider implements Provider {
18
- private readonly clientParameters : Parameters < typeof getClient > ;
19
19
public readonly events = new OpenFeatureEventEmitter ( ) ;
20
- private client ?: IConfigCatClient ;
20
+ private _status : ProviderStatus = ProviderStatus . NOT_READY ;
21
+ private readonly _clientParameters : Parameters < typeof getClient > ;
22
+ private _client ?: IConfigCatClient ;
21
23
22
24
public metadata = {
23
25
name : ConfigCatProvider . name ,
24
26
} ;
25
27
26
28
constructor ( ...params : Parameters < typeof getClient > ) {
27
- this . clientParameters = params ;
29
+ this . _clientParameters = params ;
28
30
}
29
31
30
32
public static create ( ...params : Parameters < typeof getClient > ) {
31
33
return new ConfigCatProvider ( ...params ) ;
32
34
}
33
35
36
+ get status ( ) {
37
+ return this . _status ;
38
+ }
39
+
34
40
public async initialize ( ) : Promise < void > {
35
41
return new Promise ( ( resolve ) => {
36
- const originalParameters = this . clientParameters ;
42
+ const originalParameters = this . _clientParameters ;
37
43
originalParameters [ 2 ] ??= { } ;
38
44
39
45
const options = originalParameters [ 2 ] ;
@@ -43,48 +49,54 @@ export class ConfigCatProvider implements Provider {
43
49
oldSetupHooks ?.( hooks ) ;
44
50
45
51
// After resolving, once, we can simply emit events the next time
46
- hooks . once ( 'clientReady' , ( ) => {
47
- hooks . on ( 'clientReady' , ( ) => this . events . emit ( ProviderEvents . Ready ) ) ;
52
+ const onProviderReady = ( ) => {
48
53
this . events . emit ( ProviderEvents . Ready ) ;
54
+ this . _status = ProviderStatus . READY ;
55
+ } ;
56
+
57
+ hooks . once ( 'clientReady' , ( ) => {
58
+ hooks . on ( 'clientReady' , onProviderReady ) ;
59
+ onProviderReady ( ) ;
49
60
resolve ( ) ;
50
61
} ) ;
51
62
52
- hooks . on ( 'configChanged' , ( projectConfig ) =>
63
+ hooks . on ( 'configChanged' , ( projectConfig : IConfig | undefined ) =>
53
64
this . events . emit ( ProviderEvents . ConfigurationChanged , {
54
- flagsChanged : Object . keys ( projectConfig . settings ) ,
65
+ flagsChanged : projectConfig ? Object . keys ( projectConfig . settings ) : undefined ,
55
66
} )
56
67
) ;
57
68
58
- hooks . on ( 'clientError' , ( message : string , error ) =>
69
+ hooks . on ( 'clientError' , ( message : string , error ) => {
70
+ this . _status = ProviderStatus . ERROR ;
59
71
this . events . emit ( ProviderEvents . Error , {
60
72
message : message ,
61
73
metadata : error ,
62
- } )
63
- ) ;
74
+ } ) ;
75
+ } ) ;
64
76
} ;
65
77
66
- this . client = getClient ( ...originalParameters ) ;
78
+ this . _client = getClient ( ...originalParameters ) ;
67
79
} ) ;
68
80
}
69
81
70
82
public get configCatClient ( ) {
71
- return this . client ;
83
+ return this . _client ;
72
84
}
73
85
74
86
public async onClose ( ) : Promise < void > {
75
- await this . client ?. dispose ( ) ;
87
+ await this . _client ?. dispose ( ) ;
76
88
}
77
89
78
90
async resolveBooleanEvaluation (
79
91
flagKey : string ,
80
92
defaultValue : boolean ,
81
93
context : EvaluationContext
82
94
) : Promise < ResolutionDetails < boolean > > {
83
- if ( ! this . client ) {
95
+ if ( ! this . _client ) {
84
96
throw new GeneralError ( 'Provider is not initialized' ) ;
85
97
}
86
98
87
- const { value, ...evaluationData } = await this . client . getValueDetailsAsync < SettingValue > (
99
+ const { value, ...evaluationData } = await this . _client . getValueDetailsAsync < SettingValue > (
88
100
flagKey ,
89
101
undefined ,
90
102
transformContext ( context )
@@ -102,11 +114,11 @@ export class ConfigCatProvider implements Provider {
102
114
defaultValue : string ,
103
115
context : EvaluationContext
104
116
) : Promise < ResolutionDetails < string > > {
105
- if ( ! this . client ) {
117
+ if ( ! this . _client ) {
106
118
throw new GeneralError ( 'Provider is not initialized' ) ;
107
119
}
108
120
109
- const { value, ...evaluationData } = await this . client . getValueDetailsAsync < SettingValue > (
121
+ const { value, ...evaluationData } = await this . _client . getValueDetailsAsync < SettingValue > (
110
122
flagKey ,
111
123
undefined ,
112
124
transformContext ( context )
@@ -124,11 +136,11 @@ export class ConfigCatProvider implements Provider {
124
136
defaultValue : number ,
125
137
context : EvaluationContext
126
138
) : Promise < ResolutionDetails < number > > {
127
- if ( ! this . client ) {
139
+ if ( ! this . _client ) {
128
140
throw new GeneralError ( 'Provider is not initialized' ) ;
129
141
}
130
142
131
- const { value, ...evaluationData } = await this . client . getValueDetailsAsync < SettingValue > (
143
+ const { value, ...evaluationData } = await this . _client . getValueDetailsAsync < SettingValue > (
132
144
flagKey ,
133
145
undefined ,
134
146
transformContext ( context )
@@ -146,11 +158,11 @@ export class ConfigCatProvider implements Provider {
146
158
defaultValue : U ,
147
159
context : EvaluationContext
148
160
) : Promise < ResolutionDetails < U > > {
149
- if ( ! this . client ) {
161
+ if ( ! this . _client ) {
150
162
throw new GeneralError ( 'Provider is not initialized' ) ;
151
163
}
152
164
153
- const { value, ...evaluationData } = await this . client . getValueDetailsAsync (
165
+ const { value, ...evaluationData } = await this . _client . getValueDetailsAsync (
154
166
flagKey ,
155
167
undefined ,
156
168
transformContext ( context )
0 commit comments