@openfeature/web-sdk 0.4.0 → 0.4.1

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
279
  /**
146
- * Represents a JSON node value.
280
+ * Defines where the library is intended to be run.
147
281
  */
148
- type JsonValue = PrimitiveValue | JsonObject | JsonArray;
149
- /**
150
- * Represents a JSON node value, or Date.
151
- */
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}.
@@ -519,6 +543,8 @@ declare abstract class OpenFeatureCommonAPI<P extends CommonProvider = CommonPro
519
543
  private readonly _clientEventHandlers;
520
544
  protected _clientProviders: Map<string, P>;
521
545
  protected _clientEvents: Map<string | undefined, InternalEventEmitter>;
546
+ protected _runsOn: Paradigm;
547
+ constructor(category: Paradigm);
522
548
  addHooks(...hooks: Hook<FlagValue>[]): this;
523
549
  getHooks(): Hook<FlagValue>[];
524
550
  clearHooks(): this;
@@ -531,7 +557,7 @@ declare abstract class OpenFeatureCommonAPI<P extends CommonProvider = CommonPro
531
557
  /**
532
558
  * Adds a handler for the given provider event type.
533
559
  * 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.
560
+ * API (global) events run for all providers.
535
561
  * @param {ProviderEvents} eventType The provider event type to listen to
536
562
  * @param {EventHandler} handler The handler to run on occurrence of the event type
537
563
  */
@@ -548,13 +574,34 @@ declare abstract class OpenFeatureCommonAPI<P extends CommonProvider = CommonPro
548
574
  * @returns {EventHandler[]} The handlers currently attached to the given provider event type
549
575
  */
550
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>;
551
598
  /**
552
599
  * Sets the default provider for flag evaluations.
553
600
  * This provider will be used by unnamed clients and named clients to which no provider is bound.
554
601
  * Setting a provider supersedes the current provider used in new and existing clients without a name.
555
602
  * @template P
556
603
  * @param {P} provider The provider responsible for flag evaluations.
557
- * @returns {OpenFeatureCommonAPI} OpenFeature API
604
+ * @returns {this} OpenFeature API
558
605
  */
559
606
  setProvider(provider: P): this;
560
607
  /**
@@ -566,6 +613,7 @@ declare abstract class OpenFeatureCommonAPI<P extends CommonProvider = CommonPro
566
613
  * @returns {this} OpenFeature API
567
614
  */
568
615
  setProvider(clientName: string, provider: P): this;
616
+ private setAwaitableProvider;
569
617
  protected getProviderForClient(name?: string): P;
570
618
  protected buildAndCacheEventEmitterForClient(name?: string): InternalEventEmitter;
571
619
  private getUnboundEmitters;
@@ -787,4 +835,4 @@ declare class OpenFeatureAPI extends OpenFeatureCommonAPI<Provider> implements M
787
835
  */
788
836
  declare const OpenFeature: OpenFeatureAPI;
789
837
 
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 };
838
+ 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, Paradigm, ParseError, PrimitiveValue, Provider, ProviderEvents, ProviderMetadata, ProviderStatus, ReadyEvent, ResolutionDetails, ResolutionReason, SafeLogger, StaleEvent, StandardResolutionReasons, TargetingKeyMissingError, TransactionContext, TransactionContextPropagator, 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.1",
4
4
  "description": "OpenFeature SDK for Web",
5
5
  "main": "./dist/cjs/index.js",
6
6
  "files": [