@openfeature/web-sdk 0.4.0 → 0.4.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.
package/dist/types.d.ts CHANGED
@@ -1,3 +1,133 @@
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
+
1
131
  declare enum ProviderEvents {
2
132
  /**
3
133
  * The provider is ready to evaluate flags.
@@ -17,10 +147,20 @@ declare enum ProviderEvents {
17
147
  Stale = "PROVIDER_STALE"
18
148
  }
19
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
+
20
159
  type EventMetadata = {
21
160
  [key: string]: string | boolean | number;
22
161
  };
23
162
  type CommonEventDetails = {
163
+ providerName: string;
24
164
  clientName?: string;
25
165
  };
26
166
  type CommonEventProps = {
@@ -136,135 +276,10 @@ declare class InternalEventEmitter extends GenericEventEmitter<CommonEventDetail
136
276
  interface Metadata {
137
277
  }
138
278
 
139
- type FlagValueType = 'boolean' | 'string' | 'number' | 'object';
140
- type PrimitiveValue = null | boolean | string | number;
141
- type JsonObject = {
142
- [key: string]: JsonValue;
143
- };
144
- type JsonArray = JsonValue[];
145
- /**
146
- * Represents a JSON node value.
147
- */
148
- type JsonValue = PrimitiveValue | JsonObject | JsonArray;
149
279
  /**
150
- * Represents a JSON node value, or Date.
280
+ * Defines where the library is intended to be run.
151
281
  */
152
- type FlagValue = boolean | string | number | JsonValue;
153
- type ResolutionReason = keyof typeof StandardResolutionReasons | (string & Record<never, never>);
154
- /**
155
- * A structure which supports definition of arbitrary properties, with keys of type string, and values of type boolean, string, or number.
156
- *
157
- * This structure is populated by a provider for use by an Application Author (via the Evaluation API) or an Application Integrator (via hooks).
158
- */
159
- type FlagMetadata = Record<string, string | number | boolean>;
160
- type ResolutionDetails<U> = {
161
- value: U;
162
- variant?: string;
163
- flagMetadata?: FlagMetadata;
164
- reason?: ResolutionReason;
165
- errorCode?: ErrorCode;
166
- errorMessage?: string;
167
- };
168
- type EvaluationDetails<T extends FlagValue> = {
169
- flagKey: string;
170
- flagMetadata: Readonly<FlagMetadata>;
171
- } & ResolutionDetails<T>;
172
- declare const StandardResolutionReasons: {
173
- /**
174
- * The resolved value was the result of a dynamic evaluation, such as a rule or specific user-targeting.
175
- */
176
- readonly TARGETING_MATCH: "TARGETING_MATCH";
177
- /**
178
- * The resolved value was the result of pseudorandom assignment.
179
- */
180
- readonly SPLIT: "SPLIT";
181
- /**
182
- * The resolved value was the result of the flag being disabled in the management system.
183
- */
184
- readonly DISABLED: "DISABLED";
185
- /**
186
- * The resolved value was configured statically, or otherwise fell back to a pre-configured value.
187
- */
188
- readonly DEFAULT: "DEFAULT";
189
- /**
190
- * The reason for the resolved value could not be determined.
191
- */
192
- readonly UNKNOWN: "UNKNOWN";
193
- /**
194
- * The resolved value is static (no dynamic evaluation).
195
- */
196
- readonly STATIC: "STATIC";
197
- /**
198
- * The resolved value was retrieved from cache.
199
- */
200
- readonly CACHED: "CACHED";
201
- /**
202
- * The resolved value was the result of an error.
203
- *
204
- * Note: The `errorCode` and `errorMessage` fields may contain additional details of this error.
205
- */
206
- readonly ERROR: "ERROR";
207
- };
208
- declare enum ErrorCode {
209
- /**
210
- * The value was resolved before the provider was ready.
211
- */
212
- PROVIDER_NOT_READY = "PROVIDER_NOT_READY",
213
- /**
214
- * The flag could not be found.
215
- */
216
- FLAG_NOT_FOUND = "FLAG_NOT_FOUND",
217
- /**
218
- * An error was encountered parsing data, such as a flag configuration.
219
- */
220
- PARSE_ERROR = "PARSE_ERROR",
221
- /**
222
- * The type of the flag value does not match the expected type.
223
- */
224
- TYPE_MISMATCH = "TYPE_MISMATCH",
225
- /**
226
- * The provider requires a targeting key and one was not provided in the evaluation context.
227
- */
228
- TARGETING_KEY_MISSING = "TARGETING_KEY_MISSING",
229
- /**
230
- * The evaluation context does not meet provider requirements.
231
- */
232
- INVALID_CONTEXT = "INVALID_CONTEXT",
233
- /**
234
- * An error with an unspecified code.
235
- */
236
- GENERAL = "GENERAL"
237
- }
238
-
239
- type EvaluationContextValue = PrimitiveValue | Date | {
240
- [key: string]: EvaluationContextValue;
241
- } | EvaluationContextValue[];
242
- /**
243
- * A container for arbitrary contextual data that can be used as a basis for dynamic evaluation
244
- */
245
- type EvaluationContext = {
246
- /**
247
- * A string uniquely identifying the subject (end-user, or client service) of a flag evaluation.
248
- * Providers may require this field for fractional flag evaluation, rules, or overrides targeting specific users.
249
- * Such providers may behave unpredictably if a targeting key is not specified at flag resolution.
250
- */
251
- targetingKey?: string;
252
- } & Record<string, EvaluationContextValue>;
253
- interface ManageContext<T> {
254
- /**
255
- * Access the evaluation context set on the receiver.
256
- * @returns {EvaluationContext} Evaluation context
257
- */
258
- getContext(): EvaluationContext;
259
- /**
260
- * Sets evaluation context that will be used during flag evaluations
261
- * on this receiver.
262
- * @template T The type of the receiver
263
- * @param {EvaluationContext} context Evaluation context
264
- * @returns {T} The receiver (this object)
265
- */
266
- setContext(context: EvaluationContext): T;
267
- }
282
+ type Paradigm = 'server' | 'client';
268
283
 
269
284
  /**
270
285
  * The state of the provider.
@@ -281,7 +296,11 @@ declare enum ProviderStatus {
281
296
  /**
282
297
  * The provider is in an error state and unable to evaluate flags.
283
298
  */
284
- ERROR = "ERROR"
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"
285
304
  }
286
305
  /**
287
306
  * Static data about the provider.
@@ -291,6 +310,11 @@ interface ProviderMetadata extends Metadata {
291
310
  }
292
311
  interface CommonProvider {
293
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;
294
318
  /**
295
319
  * Returns a representation of the current readiness of the provider.
296
320
  * If the provider needs to be initialized, it should return {@link ProviderStatus.READY}.
@@ -432,58 +456,6 @@ declare class InvalidContextError extends OpenFeatureError {
432
456
  constructor(message?: string);
433
457
  }
434
458
 
435
- /**
436
- * Transaction context is a mechanism for adding transaction specific context that
437
- * is merged with evaluation context prior to flag evaluation. Examples of potential
438
- * transaction specific context include: a user id, user agent, or request path.
439
- */
440
- type TransactionContext = EvaluationContext;
441
- interface ManageTransactionContextPropagator<T> extends TransactionContextPropagator {
442
- /**
443
- * EXPERIMENTAL: Transaction context propagation is experimental and subject to change.
444
- * The OpenFeature Enhancement Proposal regarding transaction context can be found [here](https://github.com/open-feature/ofep/pull/32).
445
- *
446
- * Sets a transaction context propagator on this receiver. The transaction context
447
- * propagator is responsible for persisting context for the duration of a single
448
- * transaction.
449
- * @experimental
450
- * @template T The type of the receiver
451
- * @param {TransactionContextPropagator} transactionContextPropagator The context propagator to be used
452
- * @returns {T} The receiver (this object)
453
- */
454
- setTransactionContextPropagator(transactionContextPropagator: TransactionContextPropagator): T;
455
- }
456
- interface TransactionContextPropagator {
457
- /**
458
- * EXPERIMENTAL: Transaction context propagation is experimental and subject to change.
459
- * The OpenFeature Enhancement Proposal regarding transaction context can be found [here](https://github.com/open-feature/ofep/pull/32).
460
- *
461
- * Returns the currently defined transaction context using the registered transaction
462
- * context propagator.
463
- * @experimental
464
- * @returns {TransactionContext} The current transaction context
465
- */
466
- getTransactionContext(): TransactionContext;
467
- /**
468
- * EXPERIMENTAL: Transaction context propagation is experimental and subject to change.
469
- * The OpenFeature Enhancement Proposal regarding transaction context can be found [here](https://github.com/open-feature/ofep/pull/32).
470
- *
471
- * Sets the transaction context using the registered transaction context propagator.
472
- * @experimental
473
- * @template R The return value of the callback
474
- * @param {TransactionContext} transactionContext The transaction specific context
475
- * @param {(...args: unknown[]) => R} callback Callback function used to set the transaction context on the stack
476
- * @param {...unknown[]} args Optional arguments that are passed to the callback function
477
- */
478
- setTransactionContext<R>(transactionContext: TransactionContext, callback: (...args: unknown[]) => R, ...args: unknown[]): void;
479
- }
480
-
481
- declare class NoopTransactionContextPropagator implements TransactionContextPropagator {
482
- getTransactionContext(): EvaluationContext;
483
- setTransactionContext(_: EvaluationContext, callback: () => void): void;
484
- }
485
- declare const NOOP_TRANSACTION_CONTEXT_PROPAGATOR: NoopTransactionContextPropagator;
486
-
487
459
  /**
488
460
  * Checks whether the parameter is a string.
489
461
  * @param {unknown} value The value to check
@@ -509,9 +481,8 @@ declare function isObject<T extends object>(value: unknown): value is T;
509
481
  */
510
482
  declare function objectOrUndefined<T extends object>(value: unknown): T | undefined;
511
483
 
512
- declare abstract class OpenFeatureCommonAPI<P extends CommonProvider = CommonProvider> implements Eventing, EvaluationLifeCycle<OpenFeatureCommonAPI<P>>, ManageLogger<OpenFeatureCommonAPI<P>>, ManageTransactionContextPropagator<OpenFeatureCommonAPI<P>> {
484
+ declare abstract class OpenFeatureCommonAPI<P extends CommonProvider = CommonProvider> implements Eventing, EvaluationLifeCycle<OpenFeatureCommonAPI<P>>, ManageLogger<OpenFeatureCommonAPI<P>> {
513
485
  protected _hooks: Hook[];
514
- protected _transactionContextPropagator: TransactionContextPropagator;
515
486
  protected _context: EvaluationContext;
516
487
  protected _logger: Logger;
517
488
  protected abstract _defaultProvider: P;
@@ -519,6 +490,8 @@ declare abstract class OpenFeatureCommonAPI<P extends CommonProvider = CommonPro
519
490
  private readonly _clientEventHandlers;
520
491
  protected _clientProviders: Map<string, P>;
521
492
  protected _clientEvents: Map<string | undefined, InternalEventEmitter>;
493
+ protected _runsOn: Paradigm;
494
+ constructor(category: Paradigm);
522
495
  addHooks(...hooks: Hook<FlagValue>[]): this;
523
496
  getHooks(): Hook<FlagValue>[];
524
497
  clearHooks(): this;
@@ -531,7 +504,7 @@ declare abstract class OpenFeatureCommonAPI<P extends CommonProvider = CommonPro
531
504
  /**
532
505
  * Adds a handler for the given provider event type.
533
506
  * The handlers are called in the order they have been added.
534
- * When changing the provider, the currently attached handlers will listen to the events of the new provider.
507
+ * API (global) events run for all providers.
535
508
  * @param {ProviderEvents} eventType The provider event type to listen to
536
509
  * @param {EventHandler} handler The handler to run on occurrence of the event type
537
510
  */
@@ -548,13 +521,34 @@ declare abstract class OpenFeatureCommonAPI<P extends CommonProvider = CommonPro
548
521
  * @returns {EventHandler[]} The handlers currently attached to the given provider event type
549
522
  */
550
523
  getHandlers<T extends ProviderEvents>(eventType: T): EventHandler<T>[];
524
+ /**
525
+ * Sets the default provider for flag evaluations and returns a promise that resolves when the provider is ready.
526
+ * This provider will be used by unnamed clients and named clients to which no provider is bound.
527
+ * Setting a provider supersedes the current provider used in new and existing clients without a name.
528
+ * @template P
529
+ * @param {P} provider The provider responsible for flag evaluations.
530
+ * @returns {Promise<void>}
531
+ * @throws Uncaught exceptions thrown by the provider during initialization.
532
+ */
533
+ setProviderAndWait(provider: P): Promise<void>;
534
+ /**
535
+ * Sets the provider that OpenFeature will use for flag evaluations of providers with the given name.
536
+ * A promise is returned that resolves when the provider is ready.
537
+ * Setting a provider supersedes the current provider used in new and existing clients with that name.
538
+ * @template P
539
+ * @param {string} clientName The name to identify the client
540
+ * @param {P} provider The provider responsible for flag evaluations.
541
+ * @returns {Promise<void>}
542
+ * @throws Uncaught exceptions thrown by the provider during initialization.
543
+ */
544
+ setProviderAndWait(clientName: string, provider: P): Promise<void>;
551
545
  /**
552
546
  * Sets the default provider for flag evaluations.
553
547
  * This provider will be used by unnamed clients and named clients to which no provider is bound.
554
548
  * Setting a provider supersedes the current provider used in new and existing clients without a name.
555
549
  * @template P
556
550
  * @param {P} provider The provider responsible for flag evaluations.
557
- * @returns {OpenFeatureCommonAPI} OpenFeature API
551
+ * @returns {this} OpenFeature API
558
552
  */
559
553
  setProvider(provider: P): this;
560
554
  /**
@@ -566,16 +560,15 @@ declare abstract class OpenFeatureCommonAPI<P extends CommonProvider = CommonPro
566
560
  * @returns {this} OpenFeature API
567
561
  */
568
562
  setProvider(clientName: string, provider: P): this;
563
+ private setAwaitableProvider;
569
564
  protected getProviderForClient(name?: string): P;
570
565
  protected buildAndCacheEventEmitterForClient(name?: string): InternalEventEmitter;
571
566
  private getUnboundEmitters;
572
567
  private getAssociatedEventEmitters;
573
568
  private transferListeners;
574
569
  close(): Promise<void>;
570
+ protected clearProvidersAndSetDefault(defaultProvider: P): Promise<void>;
575
571
  private handleShutdownError;
576
- setTransactionContextPropagator(transactionContextPropagator: TransactionContextPropagator): OpenFeatureCommonAPI<P>;
577
- setTransactionContext<R>(transactionContext: TransactionContext, callback: (...args: unknown[]) => R, ...args: unknown[]): void;
578
- getTransactionContext(): TransactionContext;
579
572
  }
580
573
 
581
574
  interface FlagEvaluationOptions {
@@ -780,6 +773,11 @@ declare class OpenFeatureAPI extends OpenFeatureCommonAPI<Provider> implements M
780
773
  * @returns {Client} OpenFeature Client
781
774
  */
782
775
  getClient(name?: string, version?: string): Client;
776
+ /**
777
+ * Clears all registered providers and resets the default provider.
778
+ * @returns {Promise<void>}
779
+ */
780
+ clearProviders(): Promise<void>;
783
781
  }
784
782
  /**
785
783
  * A singleton instance of the OpenFeature API.
@@ -787,4 +785,4 @@ declare class OpenFeatureAPI extends OpenFeatureCommonAPI<Provider> implements M
787
785
  */
788
786
  declare const OpenFeature: OpenFeatureAPI;
789
787
 
790
- 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, InternalEventEmitter, InvalidContextError, JsonArray, JsonObject, JsonValue, LOG_LEVELS, Logger, ManageContext, ManageLogger, ManageTransactionContextPropagator, Metadata, NOOP_PROVIDER, NOOP_TRANSACTION_CONTEXT_PROPAGATOR, OpenFeature, OpenFeatureAPI, OpenFeatureClient, OpenFeatureCommonAPI, OpenFeatureError, OpenFeatureEventEmitter, ParseError, PrimitiveValue, Provider, ProviderEvents, ProviderMetadata, ProviderStatus, ReadyEvent, ResolutionDetails, ResolutionReason, SafeLogger, StaleEvent, StandardResolutionReasons, TargetingKeyMissingError, TransactionContext, TransactionContextPropagator, TypeMismatchError, isObject, isString, objectOrUndefined, stringOrUndefined };
788
+ 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, InternalEventEmitter, 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 };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@openfeature/web-sdk",
3
- "version": "0.4.0",
3
+ "version": "0.4.2",
4
4
  "description": "OpenFeature SDK for Web",
5
5
  "main": "./dist/cjs/index.js",
6
6
  "files": [