@openfeature/web-sdk 0.4.4 → 0.4.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/types.d.ts CHANGED
@@ -1,8 +1,569 @@
1
- import { Hook, HookHints, EvaluationDetails, JsonValue, EvaluationLifeCycle, ManageLogger, Eventing, ClientMetadata, CommonProvider, EvaluationContext, Logger, ResolutionDetails, ProviderStatus, OpenFeatureEventEmitter, ProviderEvents, EventHandler, FlagValue, OpenFeatureCommonAPI, ManageContext } from '@openfeature/core';
2
- export * from '@openfeature/core';
1
+ import EventEmitter from 'events';
2
+
3
+ type FlagValueType = 'boolean' | 'string' | 'number' | 'object';
4
+ type PrimitiveValue = null | boolean | string | number;
5
+ type JsonObject = {
6
+ [key: string]: JsonValue;
7
+ };
8
+ type JsonArray = JsonValue[];
9
+ /**
10
+ * Represents a JSON node value.
11
+ */
12
+ type JsonValue = PrimitiveValue | JsonObject | JsonArray;
13
+ /**
14
+ * Represents a JSON node value, or Date.
15
+ */
16
+ type FlagValue = boolean | string | number | JsonValue;
17
+ type ResolutionReason = keyof typeof StandardResolutionReasons | (string & Record<never, never>);
18
+ /**
19
+ * A structure which supports definition of arbitrary properties, with keys of type string, and values of type boolean, string, or number.
20
+ *
21
+ * This structure is populated by a provider for use by an Application Author (via the Evaluation API) or an Application Integrator (via hooks).
22
+ */
23
+ type FlagMetadata = Record<string, string | number | boolean>;
24
+ type ResolutionDetails<U> = {
25
+ value: U;
26
+ variant?: string;
27
+ flagMetadata?: FlagMetadata;
28
+ reason?: ResolutionReason;
29
+ errorCode?: ErrorCode;
30
+ errorMessage?: string;
31
+ };
32
+ type EvaluationDetails<T extends FlagValue> = {
33
+ flagKey: string;
34
+ flagMetadata: Readonly<FlagMetadata>;
35
+ } & ResolutionDetails<T>;
36
+ declare const StandardResolutionReasons: {
37
+ /**
38
+ * The resolved value was the result of a dynamic evaluation, such as a rule or specific user-targeting.
39
+ */
40
+ readonly TARGETING_MATCH: "TARGETING_MATCH";
41
+ /**
42
+ * The resolved value was the result of pseudorandom assignment.
43
+ */
44
+ readonly SPLIT: "SPLIT";
45
+ /**
46
+ * The resolved value was the result of the flag being disabled in the management system.
47
+ */
48
+ readonly DISABLED: "DISABLED";
49
+ /**
50
+ * The resolved value was configured statically, or otherwise fell back to a pre-configured value.
51
+ */
52
+ readonly DEFAULT: "DEFAULT";
53
+ /**
54
+ * The reason for the resolved value could not be determined.
55
+ */
56
+ readonly UNKNOWN: "UNKNOWN";
57
+ /**
58
+ * The resolved value is static (no dynamic evaluation).
59
+ */
60
+ readonly STATIC: "STATIC";
61
+ /**
62
+ * The resolved value was retrieved from cache.
63
+ */
64
+ readonly CACHED: "CACHED";
65
+ /**
66
+ * The resolved value was the result of an error.
67
+ *
68
+ * Note: The `errorCode` and `errorMessage` fields may contain additional details of this error.
69
+ */
70
+ readonly ERROR: "ERROR";
71
+ };
72
+ declare enum ErrorCode {
73
+ /**
74
+ * The value was resolved before the provider was ready.
75
+ */
76
+ PROVIDER_NOT_READY = "PROVIDER_NOT_READY",
77
+ /**
78
+ * The flag could not be found.
79
+ */
80
+ FLAG_NOT_FOUND = "FLAG_NOT_FOUND",
81
+ /**
82
+ * An error was encountered parsing data, such as a flag configuration.
83
+ */
84
+ PARSE_ERROR = "PARSE_ERROR",
85
+ /**
86
+ * The type of the flag value does not match the expected type.
87
+ */
88
+ TYPE_MISMATCH = "TYPE_MISMATCH",
89
+ /**
90
+ * The provider requires a targeting key and one was not provided in the evaluation context.
91
+ */
92
+ TARGETING_KEY_MISSING = "TARGETING_KEY_MISSING",
93
+ /**
94
+ * The evaluation context does not meet provider requirements.
95
+ */
96
+ INVALID_CONTEXT = "INVALID_CONTEXT",
97
+ /**
98
+ * An error with an unspecified code.
99
+ */
100
+ GENERAL = "GENERAL"
101
+ }
102
+
103
+ type EvaluationContextValue = PrimitiveValue | Date | {
104
+ [key: string]: EvaluationContextValue;
105
+ } | EvaluationContextValue[];
106
+ /**
107
+ * A container for arbitrary contextual data that can be used as a basis for dynamic evaluation
108
+ */
109
+ type EvaluationContext = {
110
+ /**
111
+ * A string uniquely identifying the subject (end-user, or client service) of a flag evaluation.
112
+ * Providers may require this field for fractional flag evaluation, rules, or overrides targeting specific users.
113
+ * Such providers may behave unpredictably if a targeting key is not specified at flag resolution.
114
+ */
115
+ targetingKey?: string;
116
+ } & Record<string, EvaluationContextValue>;
117
+ interface ManageContext<T> {
118
+ /**
119
+ * Access the evaluation context set on the receiver.
120
+ * @returns {EvaluationContext} Evaluation context
121
+ */
122
+ getContext(): EvaluationContext;
123
+ /**
124
+ * Sets evaluation context that will be used during flag evaluations
125
+ * on this receiver.
126
+ * @template T The type of the receiver
127
+ * @param {EvaluationContext} context Evaluation context
128
+ * @returns {T} The receiver (this object)
129
+ */
130
+ setContext(context: EvaluationContext): T;
131
+ }
132
+
133
+ declare enum ProviderEvents {
134
+ /**
135
+ * The provider is ready to evaluate flags.
136
+ */
137
+ Ready = "PROVIDER_READY",
138
+ /**
139
+ * The provider is in an error state.
140
+ */
141
+ Error = "PROVIDER_ERROR",
142
+ /**
143
+ * The flag configuration in the source-of-truth has changed.
144
+ */
145
+ ConfigurationChanged = "PROVIDER_CONFIGURATION_CHANGED",
146
+ /**
147
+ * The provider's cached state is no longer valid and may not be up-to-date with the source of truth.
148
+ */
149
+ Stale = "PROVIDER_STALE"
150
+ }
151
+
152
+ /**
153
+ * Returns true if the provider's status corresponds to the event.
154
+ * If the provider's status is not defined, it matches READY.
155
+ * @param {ProviderEvents} event event to match
156
+ * @param {ProviderStatus} status status of provider
157
+ * @returns {boolean} boolean indicating if the provider status corresponds to the event.
158
+ */
159
+ declare const statusMatchesEvent: (event: ProviderEvents, status?: ProviderStatus) => boolean;
160
+
161
+ type EventMetadata = {
162
+ [key: string]: string | boolean | number;
163
+ };
164
+ type CommonEventDetails = {
165
+ providerName: string;
166
+ clientName?: string;
167
+ };
168
+ type CommonEventProps = {
169
+ message?: string;
170
+ metadata?: EventMetadata;
171
+ };
172
+ type ReadyEvent = CommonEventProps;
173
+ type ErrorEvent = CommonEventProps;
174
+ type StaleEvent = CommonEventProps;
175
+ type ConfigChangeEvent = CommonEventProps & {
176
+ flagsChanged?: string[];
177
+ };
178
+ type EventMap = {
179
+ [ProviderEvents.Ready]: ReadyEvent;
180
+ [ProviderEvents.Error]: ErrorEvent;
181
+ [ProviderEvents.Stale]: StaleEvent;
182
+ [ProviderEvents.ConfigurationChanged]: ConfigChangeEvent;
183
+ };
184
+ type EventContext<T extends ProviderEvents, U extends Record<string, unknown> = Record<string, unknown>> = EventMap[T] & U;
185
+ type EventDetails<T extends ProviderEvents> = EventContext<T> & CommonEventDetails;
186
+ type EventHandler<T extends ProviderEvents> = (eventDetails?: EventDetails<T>) => Promise<unknown> | unknown;
187
+ interface Eventing {
188
+ /**
189
+ * Adds a handler for the given provider event type.
190
+ * The handlers are called in the order they have been added.
191
+ * @param {ProviderEvents} eventType The provider event type to listen to
192
+ * @param {EventHandler} handler The handler to run on occurrence of the event type
193
+ */
194
+ addHandler<T extends ProviderEvents>(eventType: T, handler: EventHandler<T>): void;
195
+ /**
196
+ * Removes a handler for the given provider event type.
197
+ * @param {ProviderEvents} eventType The provider event type to remove the listener for
198
+ * @param {EventHandler} handler The handler to remove for the provider event type
199
+ */
200
+ removeHandler<T extends ProviderEvents>(eventType: T, handler: EventHandler<T>): void;
201
+ /**
202
+ * Gets the current handlers for the given provider event type.
203
+ * @param {ProviderEvents} eventType The provider event type to get the current handlers for
204
+ * @returns {EventHandler[]} The handlers currently attached to the given provider event type
205
+ */
206
+ getHandlers<T extends ProviderEvents>(eventType: T): EventHandler<T>[];
207
+ }
208
+
209
+ interface Logger {
210
+ error(...args: unknown[]): void;
211
+ warn(...args: unknown[]): void;
212
+ info(...args: unknown[]): void;
213
+ debug(...args: unknown[]): void;
214
+ }
215
+ interface ManageLogger<T> {
216
+ /**
217
+ * Sets a logger on this receiver. This logger supersedes to the global logger
218
+ * and is passed to various components in the SDK.
219
+ * The logger configured on the global API object will be used for all evaluations,
220
+ * unless overridden in a particular client.
221
+ * @template T The type of the receiver
222
+ * @param {Logger} logger The logger to be used
223
+ * @returns {T} The receiver (this object)
224
+ */
225
+ setLogger(logger: Logger): T;
226
+ }
227
+
228
+ declare class DefaultLogger implements Logger {
229
+ error(...args: unknown[]): void;
230
+ warn(...args: unknown[]): void;
231
+ info(): void;
232
+ debug(): void;
233
+ }
234
+
235
+ declare const LOG_LEVELS: Array<keyof Logger>;
236
+ declare class SafeLogger implements Logger {
237
+ private readonly logger;
238
+ private readonly fallbackLogger;
239
+ constructor(logger: Logger);
240
+ error(...args: unknown[]): void;
241
+ warn(...args: unknown[]): void;
242
+ info(...args: unknown[]): void;
243
+ debug(...args: unknown[]): void;
244
+ private log;
245
+ }
246
+
247
+ /**
248
+ * The GenericEventEmitter should only be used within the SDK. It supports additional properties that can be included
249
+ * in the event details.
250
+ */
251
+ declare abstract class GenericEventEmitter<AdditionalContext extends Record<string, unknown> = Record<string, unknown>> implements ManageLogger<GenericEventEmitter<AdditionalContext>> {
252
+ private readonly globalLogger?;
253
+ protected abstract readonly eventEmitter: NodeJS.EventEmitter;
254
+ private readonly _handlers;
255
+ private _eventLogger?;
256
+ constructor(globalLogger?: (() => Logger) | undefined);
257
+ emit<T extends ProviderEvents>(eventType: T, context?: EventContext<T, AdditionalContext>): void;
258
+ addHandler<T extends ProviderEvents>(eventType: T, handler: EventHandler<T>): void;
259
+ removeHandler<T extends ProviderEvents>(eventType: T, handler: EventHandler<T>): void;
260
+ removeAllHandlers(eventType?: ProviderEvents): void;
261
+ getHandlers<T extends ProviderEvents>(eventType: T): EventHandler<T>[];
262
+ setLogger(logger: Logger): this;
263
+ protected get _logger(): Logger | undefined;
264
+ }
265
+
266
+ interface Metadata {
267
+ }
268
+
269
+ /**
270
+ * Defines where the library is intended to be run.
271
+ */
272
+ type Paradigm = 'server' | 'client';
273
+
274
+ /**
275
+ * The state of the provider.
276
+ */
277
+ declare enum ProviderStatus {
278
+ /**
279
+ * The provider has not been initialized and cannot yet evaluate flags.
280
+ */
281
+ NOT_READY = "NOT_READY",
282
+ /**
283
+ * The provider is ready to resolve flags.
284
+ */
285
+ READY = "READY",
286
+ /**
287
+ * The provider is in an error state and unable to evaluate flags.
288
+ */
289
+ ERROR = "ERROR",
290
+ /**
291
+ * The provider's cached state is no longer valid and may not be up-to-date with the source of truth.
292
+ */
293
+ STALE = "STALE"
294
+ }
295
+ /**
296
+ * Static data about the provider.
297
+ */
298
+ interface ProviderMetadata extends Metadata {
299
+ readonly name: string;
300
+ }
301
+ interface CommonProvider {
302
+ readonly metadata: ProviderMetadata;
303
+ /**
304
+ * Represents where the provider is intended to be run. If defined,
305
+ * the SDK will enforce that the defined paradigm at runtime.
306
+ */
307
+ readonly runsOn?: Paradigm;
308
+ /**
309
+ * Returns a representation of the current readiness of the provider.
310
+ * If the provider needs to be initialized, it should return {@link ProviderStatus.READY}.
311
+ * If the provider is in an error state, it should return {@link ProviderStatus.ERROR}.
312
+ * If the provider is functioning normally, it should return {@link ProviderStatus.NOT_READY}.
313
+ *
314
+ * _Providers which do not implement this method are assumed to be ready immediately._
315
+ */
316
+ readonly status?: ProviderStatus;
317
+ /**
318
+ * An event emitter for ProviderEvents.
319
+ * @see ProviderEvents
320
+ */
321
+ events?: GenericEventEmitter;
322
+ /**
323
+ * A function used to shut down the provider.
324
+ * Called when this provider is replaced with a new one, or when the OpenFeature is shut down.
325
+ */
326
+ onClose?(): Promise<void>;
327
+ /**
328
+ * A function used to setup the provider.
329
+ * Called by the SDK after the provider is set if the provider's status is {@link ProviderStatus.NOT_READY}.
330
+ * When the returned promise resolves, the SDK fires the ProviderEvents.Ready event.
331
+ * If the returned promise rejects, the SDK fires the ProviderEvents.Error event.
332
+ * Use this function to perform any context-dependent setup within the provider.
333
+ * @param context
334
+ */
335
+ initialize?(context?: EvaluationContext): Promise<void>;
336
+ }
337
+
338
+ interface ClientMetadata extends Metadata {
339
+ readonly version?: string;
340
+ readonly name?: string;
341
+ readonly providerMetadata: ProviderMetadata;
342
+ }
343
+
344
+ type HookHints = Readonly<Record<string, unknown>>;
345
+ interface HookContext<T extends FlagValue = FlagValue> {
346
+ readonly flagKey: string;
347
+ readonly defaultValue: T;
348
+ readonly flagValueType: FlagValueType;
349
+ readonly context: Readonly<EvaluationContext>;
350
+ readonly clientMetadata: ClientMetadata;
351
+ readonly providerMetadata: ProviderMetadata;
352
+ readonly logger: Logger;
353
+ }
354
+ interface BeforeHookContext extends HookContext {
355
+ context: EvaluationContext;
356
+ }
357
+
358
+ interface BaseHook<T extends FlagValue = FlagValue, BeforeHookReturn = unknown, HooksReturn = unknown> {
359
+ /**
360
+ * Runs before flag values are resolved from the provider.
361
+ * If an EvaluationContext is returned, it will be merged with the pre-existing EvaluationContext.
362
+ * @param hookContext
363
+ * @param hookHints
364
+ */
365
+ before?(hookContext: BeforeHookContext, hookHints?: HookHints): BeforeHookReturn;
366
+ /**
367
+ * Runs after flag values are successfully resolved from the provider.
368
+ * @param hookContext
369
+ * @param evaluationDetails
370
+ * @param hookHints
371
+ */
372
+ after?(hookContext: Readonly<HookContext<T>>, evaluationDetails: EvaluationDetails<T>, hookHints?: HookHints): HooksReturn;
373
+ /**
374
+ * Runs in the event of an unhandled error or promise rejection during flag resolution, or any attached hooks.
375
+ * @param hookContext
376
+ * @param error
377
+ * @param hookHints
378
+ */
379
+ error?(hookContext: Readonly<HookContext<T>>, error: unknown, hookHints?: HookHints): HooksReturn;
380
+ /**
381
+ * Runs after all other hook stages, regardless of success or error.
382
+ * Errors thrown here are unhandled by the client and will surface in application code.
383
+ * @param hookContext
384
+ * @param hookHints
385
+ */
386
+ finally?(hookContext: Readonly<HookContext<T>>, hookHints?: HookHints): HooksReturn;
387
+ }
388
+
389
+ interface EvaluationLifeCycle<T> {
390
+ /**
391
+ * Adds hooks that will run during flag evaluations on this receiver.
392
+ * Hooks are executed in the order they were registered. Adding additional hooks
393
+ * will not remove existing hooks.
394
+ * Hooks registered on the global API object run with all evaluations.
395
+ * Hooks registered on the client run with all evaluations on that client.
396
+ * @template T The type of the receiver
397
+ * @param {BaseHook[]} hooks A list of hooks that should always run
398
+ * @returns {T} The receiver (this object)
399
+ */
400
+ addHooks(...hooks: BaseHook[]): T;
401
+ /**
402
+ * Access all the hooks that are registered on this receiver.
403
+ * @returns {BaseHook<FlagValue>[]} A list of the client hooks
404
+ */
405
+ getHooks(): BaseHook[];
406
+ /**
407
+ * Clears all the hooks that are registered on this receiver.
408
+ * @template T The type of the receiver
409
+ * @returns {T} The receiver (this object)
410
+ */
411
+ clearHooks(): T;
412
+ }
413
+
414
+ declare abstract class OpenFeatureError extends Error {
415
+ abstract code: ErrorCode;
416
+ constructor(message?: string);
417
+ }
418
+
419
+ declare class GeneralError extends OpenFeatureError {
420
+ code: ErrorCode;
421
+ constructor(message?: string);
422
+ }
423
+
424
+ declare class FlagNotFoundError extends OpenFeatureError {
425
+ code: ErrorCode;
426
+ constructor(message?: string);
427
+ }
428
+
429
+ declare class ParseError extends OpenFeatureError {
430
+ code: ErrorCode;
431
+ constructor(message?: string);
432
+ }
433
+
434
+ declare class TypeMismatchError extends OpenFeatureError {
435
+ code: ErrorCode;
436
+ constructor(message?: string);
437
+ }
438
+
439
+ declare class TargetingKeyMissingError extends OpenFeatureError {
440
+ code: ErrorCode;
441
+ constructor(message?: string);
442
+ }
443
+
444
+ declare class InvalidContextError extends OpenFeatureError {
445
+ code: ErrorCode;
446
+ constructor(message?: string);
447
+ }
448
+
449
+ /**
450
+ * Checks whether the parameter is a string.
451
+ * @param {unknown} value The value to check
452
+ * @returns {value is string} True if the value is a string
453
+ */
454
+ declare function isString(value: unknown): value is string;
455
+ /**
456
+ * Returns the parameter if it is a string, otherwise returns undefined.
457
+ * @param {unknown} value The value to check
458
+ * @returns {string|undefined} The parameter if it is a string, otherwise undefined
459
+ */
460
+ declare function stringOrUndefined(value: unknown): string | undefined;
461
+ /**
462
+ * Checks whether the parameter is an object.
463
+ * @param {unknown} value The value to check
464
+ * @returns {value is string} True if the value is an object
465
+ */
466
+ declare function isObject<T extends object>(value: unknown): value is T;
467
+ /**
468
+ * Returns the parameter if it is an object, otherwise returns undefined.
469
+ * @param {unknown} value The value to check
470
+ * @returns {object|undefined} The parameter if it is an object, otherwise undefined
471
+ */
472
+ declare function objectOrUndefined<T extends object>(value: unknown): T | undefined;
473
+
474
+ declare abstract class OpenFeatureCommonAPI<P extends CommonProvider = CommonProvider, H extends BaseHook = BaseHook> implements Eventing, EvaluationLifeCycle<OpenFeatureCommonAPI<P>>, ManageLogger<OpenFeatureCommonAPI<P>> {
475
+ protected abstract _createEventEmitter(): GenericEventEmitter;
476
+ protected abstract _defaultProvider: P;
477
+ protected abstract readonly _events: GenericEventEmitter;
478
+ protected _hooks: H[];
479
+ protected _context: EvaluationContext;
480
+ protected _logger: Logger;
481
+ private readonly _clientEventHandlers;
482
+ protected _clientProviders: Map<string, P>;
483
+ protected _clientEvents: Map<string | undefined, GenericEventEmitter>;
484
+ protected _runsOn: Paradigm;
485
+ constructor(category: Paradigm);
486
+ addHooks(...hooks: H[]): this;
487
+ getHooks(): H[];
488
+ clearHooks(): this;
489
+ setLogger(logger: Logger): this;
490
+ /**
491
+ * Get metadata about registered provider.
492
+ * @returns {ProviderMetadata} Provider Metadata
493
+ */
494
+ get providerMetadata(): ProviderMetadata;
495
+ /**
496
+ * Adds a handler for the given provider event type.
497
+ * The handlers are called in the order they have been added.
498
+ * API (global) events run for all providers.
499
+ * @param {ProviderEvents} eventType The provider event type to listen to
500
+ * @param {EventHandler} handler The handler to run on occurrence of the event type
501
+ */
502
+ addHandler<T extends ProviderEvents>(eventType: T, handler: EventHandler<T>): void;
503
+ /**
504
+ * Removes a handler for the given provider event type.
505
+ * @param {ProviderEvents} eventType The provider event type to remove the listener for
506
+ * @param {EventHandler} handler The handler to remove for the provider event type
507
+ */
508
+ removeHandler<T extends ProviderEvents>(eventType: T, handler: EventHandler<T>): void;
509
+ /**
510
+ * Gets the current handlers for the given provider event type.
511
+ * @param {ProviderEvents} eventType The provider event type to get the current handlers for
512
+ * @returns {EventHandler[]} The handlers currently attached to the given provider event type
513
+ */
514
+ getHandlers<T extends ProviderEvents>(eventType: T): EventHandler<T>[];
515
+ /**
516
+ * Sets the default provider for flag evaluations and returns a promise that resolves when the provider is ready.
517
+ * This provider will be used by unnamed clients and named clients to which no provider is bound.
518
+ * Setting a provider supersedes the current provider used in new and existing clients without a name.
519
+ * @template P
520
+ * @param {P} provider The provider responsible for flag evaluations.
521
+ * @returns {Promise<void>}
522
+ * @throws Uncaught exceptions thrown by the provider during initialization.
523
+ */
524
+ setProviderAndWait(provider: P): Promise<void>;
525
+ /**
526
+ * Sets the provider that OpenFeature will use for flag evaluations of providers with the given name.
527
+ * A promise is returned that resolves when the provider is ready.
528
+ * Setting a provider supersedes the current provider used in new and existing clients with that name.
529
+ * @template P
530
+ * @param {string} clientName The name to identify the client
531
+ * @param {P} provider The provider responsible for flag evaluations.
532
+ * @returns {Promise<void>}
533
+ * @throws Uncaught exceptions thrown by the provider during initialization.
534
+ */
535
+ setProviderAndWait(clientName: string, provider: P): Promise<void>;
536
+ /**
537
+ * Sets the default provider for flag evaluations.
538
+ * This provider will be used by unnamed clients and named clients to which no provider is bound.
539
+ * Setting a provider supersedes the current provider used in new and existing clients without a name.
540
+ * @template P
541
+ * @param {P} provider The provider responsible for flag evaluations.
542
+ * @returns {this} OpenFeature API
543
+ */
544
+ setProvider(provider: P): this;
545
+ /**
546
+ * Sets the provider that OpenFeature will use for flag evaluations of providers with the given name.
547
+ * Setting a provider supersedes the current provider used in new and existing clients with that name.
548
+ * @template P
549
+ * @param {string} clientName The name to identify the client
550
+ * @param {P} provider The provider responsible for flag evaluations.
551
+ * @returns {this} OpenFeature API
552
+ */
553
+ setProvider(clientName: string, provider: P): this;
554
+ private setAwaitableProvider;
555
+ protected getProviderForClient(name?: string): P;
556
+ protected buildAndCacheEventEmitterForClient(name?: string): GenericEventEmitter;
557
+ private getUnboundEmitters;
558
+ private getAssociatedEventEmitters;
559
+ private transferListeners;
560
+ close(): Promise<void>;
561
+ protected clearProvidersAndSetDefault(defaultProvider: P): Promise<void>;
562
+ private handleShutdownError;
563
+ }
3
564
 
4
565
  interface FlagEvaluationOptions {
5
- hooks?: Hook[];
566
+ hooks?: BaseHook[];
6
567
  hookHints?: HookHints;
7
568
  }
8
569
  interface Features {
@@ -88,6 +649,8 @@ interface Client extends EvaluationLifeCycle<Client>, Features, ManageLogger<Cli
88
649
  readonly metadata: ClientMetadata;
89
650
  }
90
651
 
652
+ type Hook = BaseHook<FlagValue, void, void>;
653
+
91
654
  /**
92
655
  * Interface that providers must implement to resolve flag values for their particular
93
656
  * backend or vendor.
@@ -143,6 +706,88 @@ declare class NoopFeatureProvider implements Provider {
143
706
  }
144
707
  declare const NOOP_PROVIDER: NoopFeatureProvider;
145
708
 
709
+ /**
710
+ * The OpenFeatureEventEmitter can be used by provider developers to emit
711
+ * events at various parts of the provider lifecycle.
712
+ *
713
+ * NOTE: Ready and error events are automatically emitted by the SDK based on
714
+ * the result of the initialize method.
715
+ */
716
+ declare class OpenFeatureEventEmitter extends GenericEventEmitter {
717
+ protected readonly eventEmitter: EventEmitter;
718
+ constructor();
719
+ }
720
+
721
+ /**
722
+ * Don't export types from this file publicly.
723
+ * It might cause confusion since these types are not a part of the general API,
724
+ * but just for the in-memory provider.
725
+ */
726
+
727
+ type Variants<T> = Record<string, T>;
728
+ /**
729
+ * A Feature Flag definition, containing it's specification
730
+ */
731
+ type Flag = {
732
+ /**
733
+ * An object containing all possible flags mappings (variant -> flag value)
734
+ */
735
+ variants: Variants<boolean> | Variants<string> | Variants<number> | Variants<JsonValue>;
736
+ /**
737
+ * The variant it will resolve to in STATIC evaluation
738
+ */
739
+ defaultVariant: string;
740
+ /**
741
+ * Determines if flag evaluation is enabled or not for this flag.
742
+ * If false, falls back to the default value provided to the client
743
+ */
744
+ disabled: boolean;
745
+ /**
746
+ * Function used in order to evaluate a flag to a specific value given the provided context.
747
+ * It should return a variant key.
748
+ * If it does not return a valid variant it falls back to the default value provided to the client
749
+ * @param EvaluationContext
750
+ */
751
+ contextEvaluator?: (ctx: EvaluationContext) => string;
752
+ };
753
+ type FlagConfiguration = Record<string, Flag>;
754
+
755
+ /**
756
+ * A simple OpenFeature provider intended for demos and as a test stub.
757
+ */
758
+ declare class InMemoryProvider implements Provider {
759
+ readonly events: OpenFeatureEventEmitter;
760
+ readonly runsOn = "client";
761
+ status: ProviderStatus;
762
+ readonly metadata: {
763
+ readonly name: "in-memory";
764
+ };
765
+ private _flagConfiguration;
766
+ private _context;
767
+ constructor(flagConfiguration?: FlagConfiguration);
768
+ initialize(context?: EvaluationContext | undefined): Promise<void>;
769
+ /**
770
+ * Overwrites the configured flags.
771
+ * @param { FlagConfiguration } flagConfiguration new flag configuration
772
+ */
773
+ putConfiguration(flagConfiguration: FlagConfiguration): Promise<void>;
774
+ resolveBooleanEvaluation(flagKey: string, defaultValue: boolean, context?: EvaluationContext, logger?: Logger): ResolutionDetails<boolean>;
775
+ resolveNumberEvaluation(flagKey: string, defaultValue: number, context?: EvaluationContext, logger?: Logger): ResolutionDetails<number>;
776
+ resolveStringEvaluation(flagKey: string, defaultValue: string, context?: EvaluationContext, logger?: Logger): ResolutionDetails<string>;
777
+ resolveObjectEvaluation<T extends JsonValue>(flagKey: string, defaultValue: T, context?: EvaluationContext, logger?: Logger): ResolutionDetails<T>;
778
+ private resolveAndCheckFlag;
779
+ private resolveFlagWithReason;
780
+ private lookupFlagValue;
781
+ }
782
+
783
+ /**
784
+ * The InternalEventEmitter is not exported publicly and should only be used within the SDK. It extends the
785
+ * OpenFeatureEventEmitter to include additional properties that can be included
786
+ * in the event details.
787
+ */
788
+ declare abstract class InternalEventEmitter extends GenericEventEmitter<CommonEventDetails> {
789
+ }
790
+
146
791
  type OpenFeatureClientOptions = {
147
792
  name?: string;
148
793
  version?: string;
@@ -154,14 +799,14 @@ declare class OpenFeatureClient implements Client {
154
799
  private readonly options;
155
800
  private _hooks;
156
801
  private _clientLogger?;
157
- constructor(providerAccessor: () => Provider, emitterAccessor: () => OpenFeatureEventEmitter, globalLogger: () => Logger, options: OpenFeatureClientOptions);
802
+ constructor(providerAccessor: () => Provider, emitterAccessor: () => InternalEventEmitter, globalLogger: () => Logger, options: OpenFeatureClientOptions);
158
803
  get metadata(): ClientMetadata;
159
804
  addHandler<T extends ProviderEvents>(eventType: T, handler: EventHandler<T>): void;
160
805
  removeHandler<T extends ProviderEvents>(notificationType: T, handler: EventHandler<T>): void;
161
806
  getHandlers(eventType: ProviderEvents): EventHandler<ProviderEvents>[];
162
807
  setLogger(logger: Logger): this;
163
- addHooks(...hooks: Hook<FlagValue>[]): this;
164
- getHooks(): Hook<FlagValue>[];
808
+ addHooks(...hooks: Hook[]): this;
809
+ getHooks(): Hook[];
165
810
  clearHooks(): this;
166
811
  getBooleanValue(flagKey: string, defaultValue: boolean, options?: FlagEvaluationOptions): boolean;
167
812
  getBooleanDetails(flagKey: string, defaultValue: boolean, options?: FlagEvaluationOptions): EvaluationDetails<boolean>;
@@ -180,8 +825,10 @@ declare class OpenFeatureClient implements Client {
180
825
  private get _logger();
181
826
  }
182
827
 
183
- declare class OpenFeatureAPI extends OpenFeatureCommonAPI<Provider> implements ManageContext<Promise<void>> {
828
+ declare class OpenFeatureAPI extends OpenFeatureCommonAPI<Provider, Hook> implements ManageContext<Promise<void>> {
829
+ protected _events: OpenFeatureEventEmitter;
184
830
  protected _defaultProvider: Provider;
831
+ protected _createEventEmitter: () => OpenFeatureEventEmitter;
185
832
  private constructor();
186
833
  /**
187
834
  * Gets a singleton instance of the OpenFeature API.
@@ -215,4 +862,4 @@ declare class OpenFeatureAPI extends OpenFeatureCommonAPI<Provider> implements M
215
862
  */
216
863
  declare const OpenFeature: OpenFeatureAPI;
217
864
 
218
- export { Client, Features, FlagEvaluationOptions, NOOP_PROVIDER, OpenFeature, OpenFeatureAPI, OpenFeatureClient, Provider };
865
+ export { BaseHook, BeforeHookContext, Client, ClientMetadata, CommonEventDetails, CommonProvider, ConfigChangeEvent, DefaultLogger, ErrorCode, ErrorEvent, EvaluationContext, EvaluationContextValue, EvaluationDetails, EvaluationLifeCycle, EventContext, EventDetails, EventHandler, EventMetadata, Eventing, Features, FlagEvaluationOptions, FlagMetadata, FlagNotFoundError, FlagValue, FlagValueType, GeneralError, GenericEventEmitter, Hook, HookContext, HookHints, InMemoryProvider, InvalidContextError, JsonArray, JsonObject, JsonValue, LOG_LEVELS, Logger, ManageContext, ManageLogger, Metadata, NOOP_PROVIDER, OpenFeature, OpenFeatureAPI, OpenFeatureClient, OpenFeatureCommonAPI, OpenFeatureError, OpenFeatureEventEmitter, Paradigm, ParseError, PrimitiveValue, Provider, ProviderEvents, ProviderMetadata, ProviderStatus, ReadyEvent, ResolutionDetails, ResolutionReason, SafeLogger, StaleEvent, StandardResolutionReasons, TargetingKeyMissingError, TypeMismatchError, isObject, isString, objectOrUndefined, statusMatchesEvent, stringOrUndefined };