@openfeature/server-sdk 1.6.2

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.
@@ -0,0 +1,923 @@
1
+ type FlagValueType = 'boolean' | 'string' | 'number' | 'object';
2
+ type PrimitiveValue = null | boolean | string | number;
3
+ type JsonObject = {
4
+ [key: string]: JsonValue;
5
+ };
6
+ type JsonArray = JsonValue[];
7
+ /**
8
+ * Represents a JSON node value.
9
+ */
10
+ type JsonValue = PrimitiveValue | JsonObject | JsonArray;
11
+ /**
12
+ * Represents a JSON node value, or Date.
13
+ */
14
+ type FlagValue = boolean | string | number | JsonValue;
15
+ type ResolutionReason = keyof typeof StandardResolutionReasons | (string & Record<never, never>);
16
+ /**
17
+ * A structure which supports definition of arbitrary properties, with keys of type string, and values of type boolean, string, or number.
18
+ *
19
+ * This structure is populated by a provider for use by an Application Author (via the Evaluation API) or an Application Integrator (via hooks).
20
+ */
21
+ type FlagMetadata = Record<string, string | number | boolean>;
22
+ type ResolutionDetails<U> = {
23
+ value: U;
24
+ variant?: string;
25
+ flagMetadata?: FlagMetadata;
26
+ reason?: ResolutionReason;
27
+ errorCode?: ErrorCode;
28
+ errorMessage?: string;
29
+ };
30
+ type EvaluationDetails<T extends FlagValue> = {
31
+ flagKey: string;
32
+ flagMetadata: Readonly<FlagMetadata>;
33
+ } & ResolutionDetails<T>;
34
+ declare const StandardResolutionReasons: {
35
+ /**
36
+ * The resolved value was the result of a dynamic evaluation, such as a rule or specific user-targeting.
37
+ */
38
+ readonly TARGETING_MATCH: "TARGETING_MATCH";
39
+ /**
40
+ * The resolved value was the result of pseudorandom assignment.
41
+ */
42
+ readonly SPLIT: "SPLIT";
43
+ /**
44
+ * The resolved value was the result of the flag being disabled in the management system.
45
+ */
46
+ readonly DISABLED: "DISABLED";
47
+ /**
48
+ * The resolved value was configured statically, or otherwise fell back to a pre-configured value.
49
+ */
50
+ readonly DEFAULT: "DEFAULT";
51
+ /**
52
+ * The reason for the resolved value could not be determined.
53
+ */
54
+ readonly UNKNOWN: "UNKNOWN";
55
+ /**
56
+ * The resolved value is static (no dynamic evaluation).
57
+ */
58
+ readonly STATIC: "STATIC";
59
+ /**
60
+ * The resolved value was retrieved from cache.
61
+ */
62
+ readonly CACHED: "CACHED";
63
+ /**
64
+ * The resolved value was the result of an error.
65
+ *
66
+ * Note: The `errorCode` and `errorMessage` fields may contain additional details of this error.
67
+ */
68
+ readonly ERROR: "ERROR";
69
+ };
70
+ declare enum ErrorCode {
71
+ /**
72
+ * The value was resolved before the provider was ready.
73
+ */
74
+ PROVIDER_NOT_READY = "PROVIDER_NOT_READY",
75
+ /**
76
+ * The flag could not be found.
77
+ */
78
+ FLAG_NOT_FOUND = "FLAG_NOT_FOUND",
79
+ /**
80
+ * An error was encountered parsing data, such as a flag configuration.
81
+ */
82
+ PARSE_ERROR = "PARSE_ERROR",
83
+ /**
84
+ * The type of the flag value does not match the expected type.
85
+ */
86
+ TYPE_MISMATCH = "TYPE_MISMATCH",
87
+ /**
88
+ * The provider requires a targeting key and one was not provided in the evaluation context.
89
+ */
90
+ TARGETING_KEY_MISSING = "TARGETING_KEY_MISSING",
91
+ /**
92
+ * The evaluation context does not meet provider requirements.
93
+ */
94
+ INVALID_CONTEXT = "INVALID_CONTEXT",
95
+ /**
96
+ * An error with an unspecified code.
97
+ */
98
+ GENERAL = "GENERAL"
99
+ }
100
+
101
+ type EvaluationContextValue = PrimitiveValue | Date | {
102
+ [key: string]: EvaluationContextValue;
103
+ } | EvaluationContextValue[];
104
+ /**
105
+ * A container for arbitrary contextual data that can be used as a basis for dynamic evaluation
106
+ */
107
+ type EvaluationContext = {
108
+ /**
109
+ * A string uniquely identifying the subject (end-user, or client service) of a flag evaluation.
110
+ * Providers may require this field for fractional flag evaluation, rules, or overrides targeting specific users.
111
+ * Such providers may behave unpredictably if a targeting key is not specified at flag resolution.
112
+ */
113
+ targetingKey?: string;
114
+ } & Record<string, EvaluationContextValue>;
115
+ interface ManageContext<T> {
116
+ /**
117
+ * Access the evaluation context set on the receiver.
118
+ * @returns {EvaluationContext} Evaluation context
119
+ */
120
+ getContext(): EvaluationContext;
121
+ /**
122
+ * Sets evaluation context that will be used during flag evaluations
123
+ * on this receiver.
124
+ * @template T The type of the receiver
125
+ * @param {EvaluationContext} context Evaluation context
126
+ * @returns {T} The receiver (this object)
127
+ */
128
+ setContext(context: EvaluationContext): T;
129
+ }
130
+
131
+ declare enum ProviderEvents {
132
+ /**
133
+ * The provider is ready to evaluate flags.
134
+ */
135
+ Ready = "PROVIDER_READY",
136
+ /**
137
+ * The provider is in an error state.
138
+ */
139
+ Error = "PROVIDER_ERROR",
140
+ /**
141
+ * The flag configuration in the source-of-truth has changed.
142
+ */
143
+ ConfigurationChanged = "PROVIDER_CONFIGURATION_CHANGED",
144
+ /**
145
+ * The provider's cached state is no longer valid and may not be up-to-date with the source of truth.
146
+ */
147
+ Stale = "PROVIDER_STALE"
148
+ }
149
+
150
+ /**
151
+ * Returns true if the provider's status corresponds to the event.
152
+ * If the provider's status is not defined, it matches READY.
153
+ * @param {ProviderEvents} event event to match
154
+ * @param {ProviderStatus} status status of provider
155
+ * @returns {boolean} boolean indicating if the provider status corresponds to the event.
156
+ */
157
+ declare const statusMatchesEvent: (event: ProviderEvents, status?: ProviderStatus) => boolean;
158
+
159
+ type EventMetadata = {
160
+ [key: string]: string | boolean | number;
161
+ };
162
+ type CommonEventDetails = {
163
+ providerName: string;
164
+ clientName?: string;
165
+ };
166
+ type CommonEventProps = {
167
+ message?: string;
168
+ metadata?: EventMetadata;
169
+ };
170
+ type ReadyEvent = CommonEventProps;
171
+ type ErrorEvent = CommonEventProps;
172
+ type StaleEvent = CommonEventProps;
173
+ type ConfigChangeEvent = CommonEventProps & {
174
+ flagsChanged?: string[];
175
+ };
176
+ type EventMap = {
177
+ [ProviderEvents.Ready]: ReadyEvent;
178
+ [ProviderEvents.Error]: ErrorEvent;
179
+ [ProviderEvents.Stale]: StaleEvent;
180
+ [ProviderEvents.ConfigurationChanged]: ConfigChangeEvent;
181
+ };
182
+ type EventContext<T extends ProviderEvents, U extends Record<string, unknown> = Record<string, unknown>> = EventMap[T] & U;
183
+ type EventDetails<T extends ProviderEvents> = EventContext<T> & CommonEventDetails;
184
+ type EventHandler<T extends ProviderEvents> = (eventDetails?: EventDetails<T>) => Promise<unknown> | unknown;
185
+ interface Eventing {
186
+ /**
187
+ * Adds a handler for the given provider event type.
188
+ * The handlers are called in the order they have been added.
189
+ * @param {ProviderEvents} eventType The provider event type to listen to
190
+ * @param {EventHandler} handler The handler to run on occurrence of the event type
191
+ */
192
+ addHandler<T extends ProviderEvents>(eventType: T, handler: EventHandler<T>): void;
193
+ /**
194
+ * Removes a handler for the given provider event type.
195
+ * @param {ProviderEvents} eventType The provider event type to remove the listener for
196
+ * @param {EventHandler} handler The handler to remove for the provider event type
197
+ */
198
+ removeHandler<T extends ProviderEvents>(eventType: T, handler: EventHandler<T>): void;
199
+ /**
200
+ * Gets the current handlers for the given provider event type.
201
+ * @param {ProviderEvents} eventType The provider event type to get the current handlers for
202
+ * @returns {EventHandler[]} The handlers currently attached to the given provider event type
203
+ */
204
+ getHandlers<T extends ProviderEvents>(eventType: T): EventHandler<T>[];
205
+ }
206
+
207
+ interface Logger {
208
+ error(...args: unknown[]): void;
209
+ warn(...args: unknown[]): void;
210
+ info(...args: unknown[]): void;
211
+ debug(...args: unknown[]): void;
212
+ }
213
+ interface ManageLogger<T> {
214
+ /**
215
+ * Sets a logger on this receiver. This logger supersedes to the global logger
216
+ * and is passed to various components in the SDK.
217
+ * The logger configured on the global API object will be used for all evaluations,
218
+ * unless overridden in a particular client.
219
+ * @template T The type of the receiver
220
+ * @param {Logger} logger The logger to be used
221
+ * @returns {T} The receiver (this object)
222
+ */
223
+ setLogger(logger: Logger): T;
224
+ }
225
+
226
+ declare class DefaultLogger implements Logger {
227
+ error(...args: unknown[]): void;
228
+ warn(...args: unknown[]): void;
229
+ info(): void;
230
+ debug(): void;
231
+ }
232
+
233
+ declare const LOG_LEVELS: Array<keyof Logger>;
234
+ declare class SafeLogger implements Logger {
235
+ private readonly logger;
236
+ private readonly fallbackLogger;
237
+ constructor(logger: Logger);
238
+ error(...args: unknown[]): void;
239
+ warn(...args: unknown[]): void;
240
+ info(...args: unknown[]): void;
241
+ debug(...args: unknown[]): void;
242
+ private log;
243
+ }
244
+
245
+ declare abstract class GenericEventEmitter<AdditionalContext extends Record<string, unknown> = Record<string, unknown>> implements ManageLogger<GenericEventEmitter<AdditionalContext>> {
246
+ private readonly globalLogger?;
247
+ private readonly _handlers;
248
+ private readonly eventEmitter;
249
+ private _eventLogger?;
250
+ constructor(globalLogger?: (() => Logger) | undefined);
251
+ emit<T extends ProviderEvents>(eventType: T, context?: EventContext<T, AdditionalContext>): void;
252
+ addHandler<T extends ProviderEvents>(eventType: T, handler: EventHandler<T>): void;
253
+ removeHandler<T extends ProviderEvents>(eventType: T, handler: EventHandler<T>): void;
254
+ removeAllHandlers(eventType?: ProviderEvents): void;
255
+ getHandlers<T extends ProviderEvents>(eventType: T): EventHandler<T>[];
256
+ setLogger(logger: Logger): this;
257
+ private get _logger();
258
+ }
259
+ /**
260
+ * The OpenFeatureEventEmitter can be used by provider developers to emit
261
+ * events at various parts of the provider lifecycle.
262
+ *
263
+ * NOTE: Ready and error events are automatically emitted by the SDK based on
264
+ * the result of the initialize method.
265
+ */
266
+ declare class OpenFeatureEventEmitter extends GenericEventEmitter {
267
+ }
268
+ /**
269
+ * The InternalEventEmitter should only be used within the SDK. It extends the
270
+ * OpenFeatureEventEmitter to include additional properties that can be included
271
+ * in the event details.
272
+ */
273
+ declare class InternalEventEmitter extends GenericEventEmitter<CommonEventDetails> {
274
+ }
275
+
276
+ interface Metadata {
277
+ }
278
+
279
+ /**
280
+ * Defines where the library is intended to be run.
281
+ */
282
+ type Paradigm = 'server' | 'client';
283
+
284
+ /**
285
+ * The state of the provider.
286
+ */
287
+ declare enum ProviderStatus {
288
+ /**
289
+ * The provider has not been initialized and cannot yet evaluate flags.
290
+ */
291
+ NOT_READY = "NOT_READY",
292
+ /**
293
+ * The provider is ready to resolve flags.
294
+ */
295
+ READY = "READY",
296
+ /**
297
+ * The provider is in an error state and unable to evaluate flags.
298
+ */
299
+ ERROR = "ERROR",
300
+ /**
301
+ * The provider's cached state is no longer valid and may not be up-to-date with the source of truth.
302
+ */
303
+ STALE = "STALE"
304
+ }
305
+ /**
306
+ * Static data about the provider.
307
+ */
308
+ interface ProviderMetadata extends Metadata {
309
+ readonly name: string;
310
+ }
311
+ interface CommonProvider {
312
+ readonly metadata: ProviderMetadata;
313
+ /**
314
+ * Represents where the provider is intended to be run. If defined,
315
+ * the SDK will enforce that the defined paradigm at runtime.
316
+ */
317
+ readonly runsOn?: Paradigm;
318
+ /**
319
+ * Returns a representation of the current readiness of the provider.
320
+ * If the provider needs to be initialized, it should return {@link ProviderStatus.READY}.
321
+ * If the provider is in an error state, it should return {@link ProviderStatus.ERROR}.
322
+ * If the provider is functioning normally, it should return {@link ProviderStatus.NOT_READY}.
323
+ *
324
+ * _Providers which do not implement this method are assumed to be ready immediately._
325
+ */
326
+ readonly status?: ProviderStatus;
327
+ /**
328
+ * An event emitter for ProviderEvents.
329
+ * @see ProviderEvents
330
+ */
331
+ events?: OpenFeatureEventEmitter;
332
+ /**
333
+ * A function used to shut down the provider.
334
+ * Called when this provider is replaced with a new one, or when the OpenFeature is shut down.
335
+ */
336
+ onClose?(): Promise<void>;
337
+ /**
338
+ * A function used to setup the provider.
339
+ * Called by the SDK after the provider is set if the provider's status is {@link ProviderStatus.NOT_READY}.
340
+ * When the returned promise resolves, the SDK fires the ProviderEvents.Ready event.
341
+ * If the returned promise rejects, the SDK fires the ProviderEvents.Error event.
342
+ * Use this function to perform any context-dependent setup within the provider.
343
+ * @param context
344
+ */
345
+ initialize?(context?: EvaluationContext): Promise<void>;
346
+ }
347
+
348
+ interface ClientMetadata extends Metadata {
349
+ readonly version?: string;
350
+ readonly name?: string;
351
+ readonly providerMetadata: ProviderMetadata;
352
+ }
353
+
354
+ type HookHints = Readonly<Record<string, unknown>>;
355
+ interface HookContext<T extends FlagValue = FlagValue> {
356
+ readonly flagKey: string;
357
+ readonly defaultValue: T;
358
+ readonly flagValueType: FlagValueType;
359
+ readonly context: Readonly<EvaluationContext>;
360
+ readonly clientMetadata: ClientMetadata;
361
+ readonly providerMetadata: ProviderMetadata;
362
+ readonly logger: Logger;
363
+ }
364
+ interface BeforeHookContext extends HookContext {
365
+ context: EvaluationContext;
366
+ }
367
+
368
+ interface Hook<T extends FlagValue = FlagValue> {
369
+ /**
370
+ * Runs before flag values are resolved from the provider.
371
+ * If an EvaluationContext is returned, it will be merged with the pre-existing EvaluationContext.
372
+ * @param hookContext
373
+ * @param hookHints
374
+ */
375
+ before?(hookContext: BeforeHookContext, hookHints?: HookHints): Promise<EvaluationContext | void> | EvaluationContext | void;
376
+ /**
377
+ * Runs after flag values are successfully resolved from the provider.
378
+ * @param hookContext
379
+ * @param evaluationDetails
380
+ * @param hookHints
381
+ */
382
+ after?(hookContext: Readonly<HookContext<T>>, evaluationDetails: EvaluationDetails<T>, hookHints?: HookHints): Promise<void> | void;
383
+ /**
384
+ * Runs in the event of an unhandled error or promise rejection during flag resolution, or any attached hooks.
385
+ * @param hookContext
386
+ * @param error
387
+ * @param hookHints
388
+ */
389
+ error?(hookContext: Readonly<HookContext<T>>, error: unknown, hookHints?: HookHints): Promise<void> | void;
390
+ /**
391
+ * Runs after all other hook stages, regardless of success or error.
392
+ * Errors thrown here are unhandled by the client and will surface in application code.
393
+ * @param hookContext
394
+ * @param hookHints
395
+ */
396
+ finally?(hookContext: Readonly<HookContext<T>>, hookHints?: HookHints): Promise<void> | void;
397
+ }
398
+
399
+ interface EvaluationLifeCycle<T> {
400
+ /**
401
+ * Adds hooks that will run during flag evaluations on this receiver.
402
+ * Hooks are executed in the order they were registered. Adding additional hooks
403
+ * will not remove existing hooks.
404
+ * Hooks registered on the global API object run with all evaluations.
405
+ * Hooks registered on the client run with all evaluations on that client.
406
+ * @template T The type of the receiver
407
+ * @param {Hook<FlagValue>[]} hooks A list of hooks that should always run
408
+ * @returns {T} The receiver (this object)
409
+ */
410
+ addHooks(...hooks: Hook<FlagValue>[]): T;
411
+ /**
412
+ * Access all the hooks that are registered on this receiver.
413
+ * @returns {Hook<FlagValue>[]} A list of the client hooks
414
+ */
415
+ getHooks(): Hook<FlagValue>[];
416
+ /**
417
+ * Clears all the hooks that are registered on this receiver.
418
+ * @template T The type of the receiver
419
+ * @returns {T} The receiver (this object)
420
+ */
421
+ clearHooks(): T;
422
+ }
423
+
424
+ declare abstract class OpenFeatureError extends Error {
425
+ abstract code: ErrorCode;
426
+ constructor(message?: string);
427
+ }
428
+
429
+ declare class GeneralError extends OpenFeatureError {
430
+ code: ErrorCode;
431
+ constructor(message?: string);
432
+ }
433
+
434
+ declare class FlagNotFoundError extends OpenFeatureError {
435
+ code: ErrorCode;
436
+ constructor(message?: string);
437
+ }
438
+
439
+ declare class ParseError extends OpenFeatureError {
440
+ code: ErrorCode;
441
+ constructor(message?: string);
442
+ }
443
+
444
+ declare class TypeMismatchError extends OpenFeatureError {
445
+ code: ErrorCode;
446
+ constructor(message?: string);
447
+ }
448
+
449
+ declare class TargetingKeyMissingError extends OpenFeatureError {
450
+ code: ErrorCode;
451
+ constructor(message?: string);
452
+ }
453
+
454
+ declare class InvalidContextError extends OpenFeatureError {
455
+ code: ErrorCode;
456
+ constructor(message?: string);
457
+ }
458
+
459
+ /**
460
+ * Transaction context is a mechanism for adding transaction specific context that
461
+ * is merged with evaluation context prior to flag evaluation. Examples of potential
462
+ * transaction specific context include: a user id, user agent, or request path.
463
+ */
464
+ type TransactionContext = EvaluationContext;
465
+ interface ManageTransactionContextPropagator<T> extends TransactionContextPropagator {
466
+ /**
467
+ * EXPERIMENTAL: Transaction context propagation is experimental and subject to change.
468
+ * The OpenFeature Enhancement Proposal regarding transaction context can be found [here](https://github.com/open-feature/ofep/pull/32).
469
+ *
470
+ * Sets a transaction context propagator on this receiver. The transaction context
471
+ * propagator is responsible for persisting context for the duration of a single
472
+ * transaction.
473
+ * @experimental
474
+ * @template T The type of the receiver
475
+ * @param {TransactionContextPropagator} transactionContextPropagator The context propagator to be used
476
+ * @returns {T} The receiver (this object)
477
+ */
478
+ setTransactionContextPropagator(transactionContextPropagator: TransactionContextPropagator): T;
479
+ }
480
+ interface TransactionContextPropagator {
481
+ /**
482
+ * EXPERIMENTAL: Transaction context propagation is experimental and subject to change.
483
+ * The OpenFeature Enhancement Proposal regarding transaction context can be found [here](https://github.com/open-feature/ofep/pull/32).
484
+ *
485
+ * Returns the currently defined transaction context using the registered transaction
486
+ * context propagator.
487
+ * @experimental
488
+ * @returns {TransactionContext} The current transaction context
489
+ */
490
+ getTransactionContext(): TransactionContext;
491
+ /**
492
+ * EXPERIMENTAL: Transaction context propagation is experimental and subject to change.
493
+ * The OpenFeature Enhancement Proposal regarding transaction context can be found [here](https://github.com/open-feature/ofep/pull/32).
494
+ *
495
+ * Sets the transaction context using the registered transaction context propagator.
496
+ * @experimental
497
+ * @template R The return value of the callback
498
+ * @param {TransactionContext} transactionContext The transaction specific context
499
+ * @param {(...args: unknown[]) => R} callback Callback function used to set the transaction context on the stack
500
+ * @param {...unknown[]} args Optional arguments that are passed to the callback function
501
+ */
502
+ setTransactionContext<R>(transactionContext: TransactionContext, callback: (...args: unknown[]) => R, ...args: unknown[]): void;
503
+ }
504
+
505
+ declare class NoopTransactionContextPropagator implements TransactionContextPropagator {
506
+ getTransactionContext(): EvaluationContext;
507
+ setTransactionContext(_: EvaluationContext, callback: () => void): void;
508
+ }
509
+ declare const NOOP_TRANSACTION_CONTEXT_PROPAGATOR: NoopTransactionContextPropagator;
510
+
511
+ /**
512
+ * Checks whether the parameter is a string.
513
+ * @param {unknown} value The value to check
514
+ * @returns {value is string} True if the value is a string
515
+ */
516
+ declare function isString(value: unknown): value is string;
517
+ /**
518
+ * Returns the parameter if it is a string, otherwise returns undefined.
519
+ * @param {unknown} value The value to check
520
+ * @returns {string|undefined} The parameter if it is a string, otherwise undefined
521
+ */
522
+ declare function stringOrUndefined(value: unknown): string | undefined;
523
+ /**
524
+ * Checks whether the parameter is an object.
525
+ * @param {unknown} value The value to check
526
+ * @returns {value is string} True if the value is an object
527
+ */
528
+ declare function isObject<T extends object>(value: unknown): value is T;
529
+ /**
530
+ * Returns the parameter if it is an object, otherwise returns undefined.
531
+ * @param {unknown} value The value to check
532
+ * @returns {object|undefined} The parameter if it is an object, otherwise undefined
533
+ */
534
+ declare function objectOrUndefined<T extends object>(value: unknown): T | undefined;
535
+
536
+ declare abstract class OpenFeatureCommonAPI<P extends CommonProvider = CommonProvider> implements Eventing, EvaluationLifeCycle<OpenFeatureCommonAPI<P>>, ManageLogger<OpenFeatureCommonAPI<P>>, ManageTransactionContextPropagator<OpenFeatureCommonAPI<P>> {
537
+ protected _hooks: Hook[];
538
+ protected _transactionContextPropagator: TransactionContextPropagator;
539
+ protected _context: EvaluationContext;
540
+ protected _logger: Logger;
541
+ protected abstract _defaultProvider: P;
542
+ private readonly _events;
543
+ private readonly _clientEventHandlers;
544
+ protected _clientProviders: Map<string, P>;
545
+ protected _clientEvents: Map<string | undefined, InternalEventEmitter>;
546
+ protected _runsOn: Paradigm;
547
+ constructor(category: Paradigm);
548
+ addHooks(...hooks: Hook<FlagValue>[]): this;
549
+ getHooks(): Hook<FlagValue>[];
550
+ clearHooks(): this;
551
+ setLogger(logger: Logger): this;
552
+ /**
553
+ * Get metadata about registered provider.
554
+ * @returns {ProviderMetadata} Provider Metadata
555
+ */
556
+ get providerMetadata(): ProviderMetadata;
557
+ /**
558
+ * Adds a handler for the given provider event type.
559
+ * The handlers are called in the order they have been added.
560
+ * API (global) events run for all providers.
561
+ * @param {ProviderEvents} eventType The provider event type to listen to
562
+ * @param {EventHandler} handler The handler to run on occurrence of the event type
563
+ */
564
+ addHandler<T extends ProviderEvents>(eventType: T, handler: EventHandler<T>): void;
565
+ /**
566
+ * Removes a handler for the given provider event type.
567
+ * @param {ProviderEvents} eventType The provider event type to remove the listener for
568
+ * @param {EventHandler} handler The handler to remove for the provider event type
569
+ */
570
+ removeHandler<T extends ProviderEvents>(eventType: T, handler: EventHandler<T>): void;
571
+ /**
572
+ * Gets the current handlers for the given provider event type.
573
+ * @param {ProviderEvents} eventType The provider event type to get the current handlers for
574
+ * @returns {EventHandler[]} The handlers currently attached to the given provider event type
575
+ */
576
+ getHandlers<T extends ProviderEvents>(eventType: T): EventHandler<T>[];
577
+ /**
578
+ * Sets the default provider for flag evaluations and returns a promise that resolves when the provider is ready.
579
+ * This provider will be used by unnamed clients and named clients to which no provider is bound.
580
+ * Setting a provider supersedes the current provider used in new and existing clients without a name.
581
+ * @template P
582
+ * @param {P} provider The provider responsible for flag evaluations.
583
+ * @returns {Promise<void>}
584
+ * @throws Uncaught exceptions thrown by the provider during initialization.
585
+ */
586
+ setProviderAndWait(provider: P): Promise<void>;
587
+ /**
588
+ * Sets the provider that OpenFeature will use for flag evaluations of providers with the given name.
589
+ * A promise is returned that resolves when the provider is ready.
590
+ * Setting a provider supersedes the current provider used in new and existing clients with that name.
591
+ * @template P
592
+ * @param {string} clientName The name to identify the client
593
+ * @param {P} provider The provider responsible for flag evaluations.
594
+ * @returns {Promise<void>}
595
+ * @throws Uncaught exceptions thrown by the provider during initialization.
596
+ */
597
+ setProviderAndWait(clientName: string, provider: P): Promise<void>;
598
+ /**
599
+ * Sets the default provider for flag evaluations.
600
+ * This provider will be used by unnamed clients and named clients to which no provider is bound.
601
+ * Setting a provider supersedes the current provider used in new and existing clients without a name.
602
+ * @template P
603
+ * @param {P} provider The provider responsible for flag evaluations.
604
+ * @returns {this} OpenFeature API
605
+ */
606
+ setProvider(provider: P): this;
607
+ /**
608
+ * Sets the provider that OpenFeature will use for flag evaluations of providers with the given name.
609
+ * Setting a provider supersedes the current provider used in new and existing clients with that name.
610
+ * @template P
611
+ * @param {string} clientName The name to identify the client
612
+ * @param {P} provider The provider responsible for flag evaluations.
613
+ * @returns {this} OpenFeature API
614
+ */
615
+ setProvider(clientName: string, provider: P): this;
616
+ private setAwaitableProvider;
617
+ protected getProviderForClient(name?: string): P;
618
+ protected buildAndCacheEventEmitterForClient(name?: string): InternalEventEmitter;
619
+ private getUnboundEmitters;
620
+ private getAssociatedEventEmitters;
621
+ private transferListeners;
622
+ close(): Promise<void>;
623
+ private handleShutdownError;
624
+ setTransactionContextPropagator(transactionContextPropagator: TransactionContextPropagator): OpenFeatureCommonAPI<P>;
625
+ setTransactionContext<R>(transactionContext: TransactionContext, callback: (...args: unknown[]) => R, ...args: unknown[]): void;
626
+ getTransactionContext(): TransactionContext;
627
+ }
628
+
629
+ interface FlagEvaluationOptions {
630
+ hooks?: Hook[];
631
+ hookHints?: HookHints;
632
+ }
633
+ interface Features {
634
+ /**
635
+ * Performs a flag evaluation that returns a boolean.
636
+ * @param {string} flagKey The flag key uniquely identifies a particular flag
637
+ * @param {boolean} defaultValue The value returned if an error occurs
638
+ * @param {EvaluationContext} context The evaluation context used on an individual flag evaluation
639
+ * @param {FlagEvaluationOptions} options Additional flag evaluation options
640
+ * @returns {Promise<boolean>} Flag evaluation response
641
+ */
642
+ getBooleanValue(flagKey: string, defaultValue: boolean, context?: EvaluationContext, options?: FlagEvaluationOptions): Promise<boolean>;
643
+ /**
644
+ * Performs a flag evaluation that a returns an evaluation details object.
645
+ * @param {string} flagKey The flag key uniquely identifies a particular flag
646
+ * @param {boolean} defaultValue The value returned if an error occurs
647
+ * @param {EvaluationContext} context The evaluation context used on an individual flag evaluation
648
+ * @param {FlagEvaluationOptions} options Additional flag evaluation options
649
+ * @returns {Promise<EvaluationDetails<boolean>>} Flag evaluation details response
650
+ */
651
+ getBooleanDetails(flagKey: string, defaultValue: boolean, context?: EvaluationContext, options?: FlagEvaluationOptions): Promise<EvaluationDetails<boolean>>;
652
+ /**
653
+ * Performs a flag evaluation that returns a string.
654
+ * @param {string} flagKey The flag key uniquely identifies a particular flag
655
+ * @template {string} T A optional generic argument constraining the string
656
+ * @param {T} defaultValue The value returned if an error occurs
657
+ * @param {EvaluationContext} context The evaluation context used on an individual flag evaluation
658
+ * @param {FlagEvaluationOptions} options Additional flag evaluation options
659
+ * @returns {Promise<T>} Flag evaluation response
660
+ */
661
+ getStringValue(flagKey: string, defaultValue: string, context?: EvaluationContext, options?: FlagEvaluationOptions): Promise<string>;
662
+ getStringValue<T extends string = string>(flagKey: string, defaultValue: T, context?: EvaluationContext, options?: FlagEvaluationOptions): Promise<T>;
663
+ /**
664
+ * Performs a flag evaluation that a returns an evaluation details object.
665
+ * @param {string} flagKey The flag key uniquely identifies a particular flag
666
+ * @template {string} T A optional generic argument constraining the string
667
+ * @param {T} defaultValue The value returned if an error occurs
668
+ * @param {EvaluationContext} context The evaluation context used on an individual flag evaluation
669
+ * @param {FlagEvaluationOptions} options Additional flag evaluation options
670
+ * @returns {Promise<EvaluationDetails<T>>} Flag evaluation details response
671
+ */
672
+ getStringDetails(flagKey: string, defaultValue: string, context?: EvaluationContext, options?: FlagEvaluationOptions): Promise<EvaluationDetails<string>>;
673
+ getStringDetails<T extends string = string>(flagKey: string, defaultValue: T, context?: EvaluationContext, options?: FlagEvaluationOptions): Promise<EvaluationDetails<T>>;
674
+ /**
675
+ * Performs a flag evaluation that returns a number.
676
+ * @param {string} flagKey The flag key uniquely identifies a particular flag
677
+ * @template {number} T A optional generic argument constraining the number
678
+ * @param {T} defaultValue The value returned if an error occurs
679
+ * @param {EvaluationContext} context The evaluation context used on an individual flag evaluation
680
+ * @param {FlagEvaluationOptions} options Additional flag evaluation options
681
+ * @returns {Promise<T>} Flag evaluation response
682
+ */
683
+ getNumberValue(flagKey: string, defaultValue: number, context?: EvaluationContext, options?: FlagEvaluationOptions): Promise<number>;
684
+ getNumberValue<T extends number = number>(flagKey: string, defaultValue: T, context?: EvaluationContext, options?: FlagEvaluationOptions): Promise<T>;
685
+ /**
686
+ * Performs a flag evaluation that a returns an evaluation details object.
687
+ * @param {string} flagKey The flag key uniquely identifies a particular flag
688
+ * @template {number} T A optional generic argument constraining the number
689
+ * @param {T} defaultValue The value returned if an error occurs
690
+ * @param {EvaluationContext} context The evaluation context used on an individual flag evaluation
691
+ * @param {FlagEvaluationOptions} options Additional flag evaluation options
692
+ * @returns {Promise<EvaluationDetails<T>>} Flag evaluation details response
693
+ */
694
+ getNumberDetails(flagKey: string, defaultValue: number, context?: EvaluationContext, options?: FlagEvaluationOptions): Promise<EvaluationDetails<number>>;
695
+ getNumberDetails<T extends number = number>(flagKey: string, defaultValue: T, context?: EvaluationContext, options?: FlagEvaluationOptions): Promise<EvaluationDetails<T>>;
696
+ /**
697
+ * Performs a flag evaluation that returns an object.
698
+ * @param {string} flagKey The flag key uniquely identifies a particular flag
699
+ * @template {JsonValue} T A optional generic argument describing the structure
700
+ * @param {T} defaultValue The value returned if an error occurs
701
+ * @param {EvaluationContext} context The evaluation context used on an individual flag evaluation
702
+ * @param {FlagEvaluationOptions} options Additional flag evaluation options
703
+ * @returns {Promise<T>} Flag evaluation response
704
+ */
705
+ getObjectValue(flagKey: string, defaultValue: JsonValue, context?: EvaluationContext, options?: FlagEvaluationOptions): Promise<JsonValue>;
706
+ getObjectValue<T extends JsonValue = JsonValue>(flagKey: string, defaultValue: T, context?: EvaluationContext, options?: FlagEvaluationOptions): Promise<T>;
707
+ /**
708
+ * Performs a flag evaluation that a returns an evaluation details object.
709
+ * @param {string} flagKey The flag key uniquely identifies a particular flag
710
+ * @template {JsonValue} T A optional generic argument describing the structure
711
+ * @param {T} defaultValue The value returned if an error occurs
712
+ * @param {EvaluationContext} context The evaluation context used on an individual flag evaluation
713
+ * @param {FlagEvaluationOptions} options Additional flag evaluation options
714
+ * @returns {Promise<EvaluationDetails<T>>} Flag evaluation details response
715
+ */
716
+ getObjectDetails(flagKey: string, defaultValue: JsonValue, context?: EvaluationContext, options?: FlagEvaluationOptions): Promise<EvaluationDetails<JsonValue>>;
717
+ getObjectDetails<T extends JsonValue = JsonValue>(flagKey: string, defaultValue: T, context?: EvaluationContext, options?: FlagEvaluationOptions): Promise<EvaluationDetails<T>>;
718
+ }
719
+
720
+ interface Client extends EvaluationLifeCycle<Client>, Features, ManageContext<Client>, ManageLogger<Client>, Eventing {
721
+ readonly metadata: ClientMetadata;
722
+ }
723
+
724
+ /**
725
+ * Interface that providers must implement to resolve flag values for their particular
726
+ * backend or vendor.
727
+ *
728
+ * Implementation for resolving all the required flag types must be defined.
729
+ */
730
+ interface Provider extends CommonProvider {
731
+ /**
732
+ * A provider hook exposes a mechanism for provider authors to register hooks
733
+ * to tap into various stages of the flag evaluation lifecycle. These hooks can
734
+ * be used to perform side effects and mutate the context for purposes of the
735
+ * provider. Provider hooks are not configured or controlled by the application author.
736
+ */
737
+ readonly hooks?: Hook[];
738
+ /**
739
+ * Resolve a boolean flag and its evaluation details.
740
+ */
741
+ resolveBooleanEvaluation(flagKey: string, defaultValue: boolean, context: EvaluationContext, logger: Logger): Promise<ResolutionDetails<boolean>>;
742
+ /**
743
+ * Resolve a string flag and its evaluation details.
744
+ */
745
+ resolveStringEvaluation(flagKey: string, defaultValue: string, context: EvaluationContext, logger: Logger): Promise<ResolutionDetails<string>>;
746
+ /**
747
+ * Resolve a numeric flag and its evaluation details.
748
+ */
749
+ resolveNumberEvaluation(flagKey: string, defaultValue: number, context: EvaluationContext, logger: Logger): Promise<ResolutionDetails<number>>;
750
+ /**
751
+ * Resolve and parse an object flag and its evaluation details.
752
+ */
753
+ resolveObjectEvaluation<T extends JsonValue>(flagKey: string, defaultValue: T, context: EvaluationContext, logger: Logger): Promise<ResolutionDetails<T>>;
754
+ }
755
+
756
+ /**
757
+ * The No-op provider is set by default, and simply always returns the default value.
758
+ */
759
+ declare class NoopFeatureProvider implements Provider {
760
+ readonly metadata: {
761
+ readonly name: "No-op Provider";
762
+ };
763
+ get status(): ProviderStatus;
764
+ resolveBooleanEvaluation(_: string, defaultValue: boolean): Promise<ResolutionDetails<boolean>>;
765
+ resolveStringEvaluation(_: string, defaultValue: string): Promise<ResolutionDetails<string>>;
766
+ resolveNumberEvaluation(_: string, defaultValue: number): Promise<ResolutionDetails<number>>;
767
+ resolveObjectEvaluation<T extends JsonValue>(_: string, defaultValue: T): Promise<ResolutionDetails<T>>;
768
+ private noOp;
769
+ }
770
+ declare const NOOP_PROVIDER: NoopFeatureProvider;
771
+
772
+ /**
773
+ * Don't export types from this file publicly.
774
+ * It might cause confusion since these types are not a part of the general API,
775
+ * but just for the in-memory provider.
776
+ */
777
+
778
+ type Variants<T> = Record<string, T>;
779
+ /**
780
+ * A Feature Flag definition, containing it's specification
781
+ */
782
+ type Flag = {
783
+ /**
784
+ * An object containing all possible flags mappings (variant -> flag value)
785
+ */
786
+ variants: Variants<boolean> | Variants<string> | Variants<number> | Variants<JsonValue>;
787
+ /**
788
+ * The variant it will resolve to in STATIC evaluation
789
+ */
790
+ defaultVariant: string;
791
+ /**
792
+ * Determines if flag evaluation is enabled or not for this flag.
793
+ * If false, falls back to the default value provided to the client
794
+ */
795
+ disabled: boolean;
796
+ /**
797
+ * Function used in order to evaluate a flag to a specific value given the provided context.
798
+ * It should return a variant key.
799
+ * If it does not return a valid variant it falls back to the default value provided to the client
800
+ * @param EvaluationContext
801
+ */
802
+ contextEvaluator?: (ctx: EvaluationContext) => string;
803
+ };
804
+ type FlagConfiguration = Record<string, Flag>;
805
+
806
+ /**
807
+ * A simple OpenFeature provider intended for demos and as a test stub.
808
+ */
809
+ declare class InMemoryProvider implements Provider {
810
+ readonly events: OpenFeatureEventEmitter;
811
+ readonly runsOn = "server";
812
+ readonly metadata: {
813
+ readonly name: "in-memory";
814
+ };
815
+ private _flagConfiguration;
816
+ constructor(flagConfiguration?: FlagConfiguration);
817
+ /**
818
+ * Overwrites the configured flags.
819
+ * @param { FlagConfiguration } flagConfiguration new flag configuration
820
+ */
821
+ putConfiguration(flagConfiguration: FlagConfiguration): void;
822
+ resolveBooleanEvaluation(flagKey: string, defaultValue: boolean, context?: EvaluationContext, logger?: Logger): Promise<ResolutionDetails<boolean>>;
823
+ resolveNumberEvaluation(flagKey: string, defaultValue: number, context?: EvaluationContext, logger?: Logger): Promise<ResolutionDetails<number>>;
824
+ resolveStringEvaluation(flagKey: string, defaultValue: string, context?: EvaluationContext, logger?: Logger): Promise<ResolutionDetails<string>>;
825
+ resolveObjectEvaluation<T extends JsonValue>(flagKey: string, defaultValue: T, context?: EvaluationContext, logger?: Logger): Promise<ResolutionDetails<T>>;
826
+ private resolveFlagWithReason;
827
+ private lookupFlagValue;
828
+ }
829
+
830
+ type OpenFeatureClientOptions = {
831
+ name?: string;
832
+ version?: string;
833
+ };
834
+ declare class OpenFeatureClient implements Client, ManageContext<OpenFeatureClient> {
835
+ private readonly providerAccessor;
836
+ private readonly emitterAccessor;
837
+ private readonly globalLogger;
838
+ private readonly options;
839
+ private _context;
840
+ private _hooks;
841
+ private _clientLogger?;
842
+ constructor(providerAccessor: () => Provider, emitterAccessor: () => InternalEventEmitter, globalLogger: () => Logger, options: OpenFeatureClientOptions, context?: EvaluationContext);
843
+ get metadata(): ClientMetadata;
844
+ addHandler<T extends ProviderEvents>(eventType: T, handler: EventHandler<T>): void;
845
+ removeHandler<T extends ProviderEvents>(eventType: T, handler: EventHandler<T>): void;
846
+ getHandlers(eventType: ProviderEvents): EventHandler<ProviderEvents>[];
847
+ setLogger(logger: Logger): OpenFeatureClient;
848
+ setContext(context: EvaluationContext): OpenFeatureClient;
849
+ getContext(): EvaluationContext;
850
+ addHooks(...hooks: Hook<FlagValue>[]): OpenFeatureClient;
851
+ getHooks(): Hook<FlagValue>[];
852
+ clearHooks(): OpenFeatureClient;
853
+ getBooleanValue(flagKey: string, defaultValue: boolean, context?: EvaluationContext, options?: FlagEvaluationOptions): Promise<boolean>;
854
+ getBooleanDetails(flagKey: string, defaultValue: boolean, context?: EvaluationContext, options?: FlagEvaluationOptions): Promise<EvaluationDetails<boolean>>;
855
+ getStringValue<T extends string = string>(flagKey: string, defaultValue: T, context?: EvaluationContext, options?: FlagEvaluationOptions): Promise<T>;
856
+ getStringDetails<T extends string = string>(flagKey: string, defaultValue: T, context?: EvaluationContext, options?: FlagEvaluationOptions): Promise<EvaluationDetails<T>>;
857
+ getNumberValue<T extends number = number>(flagKey: string, defaultValue: T, context?: EvaluationContext, options?: FlagEvaluationOptions): Promise<T>;
858
+ getNumberDetails<T extends number = number>(flagKey: string, defaultValue: T, context?: EvaluationContext, options?: FlagEvaluationOptions): Promise<EvaluationDetails<T>>;
859
+ getObjectValue<T extends JsonValue = JsonValue>(flagKey: string, defaultValue: T, context?: EvaluationContext, options?: FlagEvaluationOptions): Promise<T>;
860
+ getObjectDetails<T extends JsonValue = JsonValue>(flagKey: string, defaultValue: T, context?: EvaluationContext, options?: FlagEvaluationOptions): Promise<EvaluationDetails<T>>;
861
+ private evaluate;
862
+ private beforeHooks;
863
+ private afterHooks;
864
+ private errorHooks;
865
+ private finallyHooks;
866
+ private get _provider();
867
+ private get _logger();
868
+ }
869
+
870
+ declare class OpenFeatureAPI extends OpenFeatureCommonAPI<Provider> implements ManageContext<OpenFeatureAPI> {
871
+ protected _defaultProvider: Provider;
872
+ private constructor();
873
+ /**
874
+ * Gets a singleton instance of the OpenFeature API.
875
+ * @ignore
876
+ * @returns {OpenFeatureAPI} OpenFeature API
877
+ */
878
+ static getInstance(): OpenFeatureAPI;
879
+ setContext(context: EvaluationContext): this;
880
+ getContext(): EvaluationContext;
881
+ /**
882
+ * A factory function for creating new unnamed OpenFeature clients. Clients can contain
883
+ * their own state (e.g. logger, hook, context). Multiple clients can be used
884
+ * to segment feature flag configuration.
885
+ *
886
+ * All unnamed clients use the same provider set via {@link this.setProvider setProvider}.
887
+ * @param {EvaluationContext} context Evaluation context that should be set on the client to used during flag evaluations
888
+ * @returns {Client} OpenFeature Client
889
+ */
890
+ getClient(context?: EvaluationContext): Client;
891
+ /**
892
+ * A factory function for creating new named OpenFeature clients. Clients can contain
893
+ * their own state (e.g. logger, hook, context). Multiple clients can be used
894
+ * to segment feature flag configuration.
895
+ *
896
+ * If there is already a provider bound to this name via {@link this.setProvider setProvider}, this provider will be used.
897
+ * Otherwise, the default provider is used until a provider is assigned to that name.
898
+ * @param {string} name The name of the client
899
+ * @param {EvaluationContext} context Evaluation context that should be set on the client to used during flag evaluations
900
+ * @returns {Client} OpenFeature Client
901
+ */
902
+ getClient(name: string, context?: EvaluationContext): Client;
903
+ /**
904
+ * A factory function for creating new named OpenFeature clients. Clients can contain
905
+ * their own state (e.g. logger, hook, context). Multiple clients can be used
906
+ * to segment feature flag configuration.
907
+ *
908
+ * If there is already a provider bound to this name via {@link this.setProvider setProvider}, this provider will be used.
909
+ * Otherwise, the default provider is used until a provider is assigned to that name.
910
+ * @param {string} name The name of the client
911
+ * @param {string} version The version of the client (only used for metadata)
912
+ * @param {EvaluationContext} context Evaluation context that should be set on the client to used during flag evaluations
913
+ * @returns {Client} OpenFeature Client
914
+ */
915
+ getClient(name: string, version: string, context?: EvaluationContext): Client;
916
+ }
917
+ /**
918
+ * A singleton instance of the OpenFeature API.
919
+ * @returns {OpenFeatureAPI} OpenFeature API
920
+ */
921
+ declare const OpenFeature: OpenFeatureAPI;
922
+
923
+ export { 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, Hook, HookContext, HookHints, InMemoryProvider, InternalEventEmitter, InvalidContextError, JsonArray, JsonObject, JsonValue, LOG_LEVELS, Logger, ManageContext, ManageLogger, ManageTransactionContextPropagator, Metadata, NOOP_PROVIDER, NOOP_TRANSACTION_CONTEXT_PROPAGATOR, OpenFeature, OpenFeatureAPI, OpenFeatureClient, OpenFeatureCommonAPI, OpenFeatureError, OpenFeatureEventEmitter, Paradigm, ParseError, PrimitiveValue, Provider, ProviderEvents, ProviderMetadata, ProviderStatus, ReadyEvent, ResolutionDetails, ResolutionReason, SafeLogger, StaleEvent, StandardResolutionReasons, TargetingKeyMissingError, TransactionContext, TransactionContextPropagator, TypeMismatchError, isObject, isString, objectOrUndefined, statusMatchesEvent, stringOrUndefined };