@@ -2,6 +2,7 @@ import type { IConfigCache, IConfigCatCache } from "./ConfigCatCache";
2
2
import { ExternalConfigCache , InMemoryConfigCache } from "./ConfigCatCache" ;
3
3
import type { IConfigCatLogger } from "./ConfigCatLogger" ;
4
4
import { ConfigCatConsoleLogger , LoggerWrapper } from "./ConfigCatLogger" ;
5
+ import type { IConfigCatConfigFetcher } from "./ConfigFetcher" ;
5
6
import type { IEventEmitter } from "./EventEmitter" ;
6
7
import { NullEventEmitter } from "./EventEmitter" ;
7
8
import type { FlagOverrides } from "./FlagOverrides" ;
@@ -66,6 +67,14 @@ export interface IOptions {
66
67
*/
67
68
cache ?: IConfigCatCache | null ;
68
69
70
+ /**
71
+ * The config fetcher implementation to use for performing ConfigCat config fetch operations.
72
+ *
73
+ * If not set, a default implementation will be used depending on the current platform.
74
+ * If you want to use custom a config fetcher, you can provide an implementation of `IConfigCatConfigFetcher`.
75
+ */
76
+ configFetcher ?: IConfigCatConfigFetcher | null ;
77
+
69
78
/** The flag override to use. If not set, no flag override will be used. */
70
79
flagOverrides ?: FlagOverrides | null ;
71
80
@@ -123,6 +132,14 @@ export type OptionsForPollingMode<TMode extends PollingMode | undefined> =
123
132
TMode extends undefined ? IAutoPollOptions :
124
133
never ;
125
134
135
+ export interface IConfigCatKernel {
136
+ sdkType : string ;
137
+ sdkVersion : string ;
138
+ eventEmitterFactory ?: ( ) => IEventEmitter ;
139
+ defaultCacheFactory ?: ( options : OptionsBase ) => IConfigCache ;
140
+ configFetcherFactory : ( options : OptionsBase ) => IConfigCatConfigFetcher ;
141
+ }
142
+
126
143
/* eslint-disable @typescript-eslint/no-inferrable-types */
127
144
128
145
export abstract class OptionsBase {
@@ -145,6 +162,8 @@ export abstract class OptionsBase {
145
162
146
163
cache : IConfigCache ;
147
164
165
+ configFetcher : IConfigCatConfigFetcher ;
166
+
148
167
flagOverrides ?: FlagOverrides ;
149
168
150
169
defaultUser ?: IUser ;
@@ -153,9 +172,7 @@ export abstract class OptionsBase {
153
172
154
173
hooks : SafeHooksWrapper ;
155
174
156
- constructor ( sdkKey : string , clientVersion : string , options ?: IOptions | null ,
157
- defaultCacheFactory ?: ( ( options : OptionsBase ) => IConfigCache ) | null ,
158
- eventEmitterFactory ?: ( ( ) => IEventEmitter ) | null ) {
175
+ constructor ( sdkKey : string , kernel : IConfigCatKernel , clientVersion : string , options ?: IOptions | null ) {
159
176
160
177
if ( ! sdkKey ) {
161
178
throw new Error ( "Invalid 'sdkKey' value" ) ;
@@ -175,7 +192,7 @@ export abstract class OptionsBase {
175
192
break ;
176
193
}
177
194
178
- const eventEmitter = eventEmitterFactory ?.( ) ?? new NullEventEmitter ( ) ;
195
+ const eventEmitter = kernel . eventEmitterFactory ?.( ) ?? new NullEventEmitter ( ) ;
179
196
const hooks = new Hooks ( eventEmitter ) ;
180
197
const hooksWeakRef = new ( isWeakRefAvailable ( ) ? WeakRef : getWeakRefStub ( ) ) ( hooks ) ;
181
198
this . hooks = < SafeHooksWrapper & { hooksWeakRef : WeakRef < Hooks > } > {
@@ -188,10 +205,12 @@ export abstract class OptionsBase {
188
205
189
206
let logger : IConfigCatLogger | null | undefined ;
190
207
let cache : IConfigCatCache | null | undefined ;
208
+ let configFetcher : IConfigCatConfigFetcher | null | undefined ;
191
209
192
210
if ( options ) {
193
211
logger = options . logger ;
194
212
cache = options . cache ;
213
+ configFetcher = options . configFetcher ;
195
214
196
215
if ( options . requestTimeoutMs ) {
197
216
if ( options . requestTimeoutMs < 0 ) {
@@ -225,7 +244,9 @@ export abstract class OptionsBase {
225
244
226
245
this . cache = cache
227
246
? new ExternalConfigCache ( cache , this . logger )
228
- : ( defaultCacheFactory ? defaultCacheFactory ( this ) : new InMemoryConfigCache ( ) ) ;
247
+ : ( kernel . defaultCacheFactory ? kernel . defaultCacheFactory ( this ) : new InMemoryConfigCache ( ) ) ;
248
+
249
+ this . configFetcher = configFetcher ?? kernel . configFetcherFactory ( this ) ;
229
250
}
230
251
231
252
yieldHooks ( ) : Hooks {
@@ -250,11 +271,9 @@ export class AutoPollOptions extends OptionsBase {
250
271
251
272
maxInitWaitTimeSeconds : number = 5 ;
252
273
253
- constructor ( sdkKey : string , sdkType : string , sdkVersion : string , options ?: IAutoPollOptions | null ,
254
- defaultCacheFactory ?: ( ( options : OptionsBase ) => IConfigCache ) | null ,
255
- eventEmitterFactory ?: ( ( ) => IEventEmitter ) | null ) {
274
+ constructor ( sdkKey : string , kernel : IConfigCatKernel , options ?: IAutoPollOptions | null ) {
256
275
257
- super ( sdkKey , sdkType + "/a-" + sdkVersion , options , defaultCacheFactory , eventEmitterFactory ) ;
276
+ super ( sdkKey , kernel , kernel . sdkType + "/a-" + kernel . sdkVersion , options ) ;
258
277
259
278
if ( options ) {
260
279
@@ -282,23 +301,19 @@ export class AutoPollOptions extends OptionsBase {
282
301
}
283
302
284
303
export class ManualPollOptions extends OptionsBase {
285
- constructor ( sdkKey : string , sdkType : string , sdkVersion : string , options ?: IManualPollOptions | null ,
286
- defaultCacheFactory ?: ( ( options : OptionsBase ) => IConfigCache ) | null ,
287
- eventEmitterFactory ?: ( ( ) => IEventEmitter ) | null ) {
304
+ constructor ( sdkKey : string , kernel : IConfigCatKernel , options ?: IManualPollOptions | null ) {
288
305
289
- super ( sdkKey , sdkType + "/m-" + sdkVersion , options , defaultCacheFactory , eventEmitterFactory ) ;
306
+ super ( sdkKey , kernel , kernel . sdkType + "/m-" + kernel . sdkVersion , options ) ;
290
307
}
291
308
}
292
309
293
310
export class LazyLoadOptions extends OptionsBase {
294
311
295
312
cacheTimeToLiveSeconds : number = 60 ;
296
313
297
- constructor ( sdkKey : string , sdkType : string , sdkVersion : string , options ?: ILazyLoadingOptions | null ,
298
- defaultCacheFactory ?: ( ( options : OptionsBase ) => IConfigCache ) | null ,
299
- eventEmitterFactory ?: ( ( ) => IEventEmitter ) | null ) {
314
+ constructor ( sdkKey : string , kernel : IConfigCatKernel , options ?: ILazyLoadingOptions | null ) {
300
315
301
- super ( sdkKey , sdkType + "/l-" + sdkVersion , options , defaultCacheFactory , eventEmitterFactory ) ;
316
+ super ( sdkKey , kernel , kernel . sdkType + "/l-" + kernel . sdkVersion , options ) ;
302
317
303
318
if ( options ) {
304
319
if ( options . cacheTimeToLiveSeconds != null ) {
0 commit comments