@openfeature/react-sdk 0.2.3-experimental → 0.3.0-experimental

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,20 +1,33 @@
1
- import { EvaluationDetails, JsonValue, FlagEvaluationOptions, Client } from '@openfeature/web-sdk';
1
+ import { FlagEvaluationOptions, FlagValue as FlagValue$1, JsonValue, EvaluationDetails as EvaluationDetails$1, Client } from '@openfeature/web-sdk';
2
2
  export * from '@openfeature/web-sdk';
3
+ import { FlagValue, EvaluationDetails, FlagMetadata, StandardResolutionReasons, ErrorCode } from '@openfeature/core';
3
4
  import * as React from 'react';
4
5
 
5
- type ReactFlagEvaluationOptions = {
6
+ type ReactFlagEvaluationOptions = ({
7
+ /**
8
+ * Enable or disable all suspense functionality.
9
+ * Cannot be used in conjunction with `suspendUntilReady` and `suspendWhileReconciling` options.
10
+ */
11
+ suspend?: boolean;
12
+ suspendUntilReady?: never;
13
+ suspendWhileReconciling?: never;
14
+ } | {
6
15
  /**
7
16
  * Suspend flag evaluations while the provider is not ready.
8
17
  * Set to false if you don't want to show suspense fallbacks until the provider is initialized.
9
18
  * Defaults to true.
19
+ * Cannot be used in conjunction with `suspend` option.
10
20
  */
11
21
  suspendUntilReady?: boolean;
12
22
  /**
13
23
  * Suspend flag evaluations while the provider's context is being reconciled.
14
24
  * Set to true if you want to show suspense fallbacks while flags are re-evaluated after context changes.
15
- * Defaults to false.
25
+ * Defaults to true.
26
+ * Cannot be used in conjunction with `suspend` option.
16
27
  */
17
28
  suspendWhileReconciling?: boolean;
29
+ suspend?: never;
30
+ }) & {
18
31
  /**
19
32
  * Update the component if the provider emits a ConfigurationChanged event.
20
33
  * Set to false to prevent components from re-rendering when flag value changes
@@ -30,9 +43,71 @@ type ReactFlagEvaluationOptions = {
30
43
  */
31
44
  updateOnContextChanged?: boolean;
32
45
  } & FlagEvaluationOptions;
46
+
47
+ interface FlagQuery<T extends FlagValue = FlagValue> {
48
+ /**
49
+ * A structure representing the result of the flag evaluation process
50
+ */
51
+ readonly details: EvaluationDetails<T>;
52
+ /**
53
+ * The flag value
54
+ */
55
+ readonly value: T;
56
+ /**
57
+ * A variant is a semantic identifier for a value.
58
+ * Not available from all providers.
59
+ */
60
+ readonly variant: string | undefined;
61
+ /**
62
+ * Arbitrary data associated with this flag or evaluation.
63
+ * Not available from all providers.
64
+ */
65
+ readonly flagMetadata: FlagMetadata;
66
+ /**
67
+ * The reason the evaluation resolved to the particular value.
68
+ * Not available from all providers.
69
+ */
70
+ readonly reason: typeof StandardResolutionReasons | string | undefined;
71
+ /**
72
+ * Indicates if this flag defaulted due to an error.
73
+ * Specifically, indicates reason equals {@link StandardResolutionReasons.ERROR} or the errorCode is set, this field is truthy.
74
+ */
75
+ readonly isError: boolean;
76
+ /**
77
+ * The error code, see {@link ErrorCode}.
78
+ */
79
+ readonly errorCode: ErrorCode | undefined;
80
+ /**
81
+ * A message associated with the error.
82
+ */
83
+ readonly errorMessage: string | undefined;
84
+ /**
85
+ * Indicates this flag is up-to-date and in sync with the source of truth.
86
+ * Specifically, indicates the evaluation did not default due to error, and the reason is neither {@link StandardResolutionReasons.STALE} or {@link StandardResolutionReasons.DISABLED}.
87
+ */
88
+ readonly isAuthoritative: boolean;
89
+ /**
90
+ * The type of the value, as returned by "typeof" operator.
91
+ */
92
+ readonly type: 'string' | 'number' | 'bigint' | 'boolean' | 'symbol' | 'undefined' | 'object' | 'function';
93
+ }
94
+
95
+ /**
96
+ * Evaluates a feature flag generically, returning an react-flavored queryable object.
97
+ * The resolver method to use is based on the type of the defaultValue.
98
+ * For type-specific hooks, use {@link useBooleanFlagValue}, {@link useBooleanFlagDetails} and equivalents.
99
+ * By default, components will re-render when the flag value changes.
100
+ * @param {string} flagKey the flag identifier
101
+ * @template {FlagValue} T A optional generic argument constraining the default.
102
+ * @param {T} defaultValue the default value; used to determine what resolved type should be used.
103
+ * @param {ReactFlagEvaluationOptions} options for this evaluation
104
+ * @returns { FlagQuery } a queryable object containing useful information about the flag.
105
+ */
106
+ declare function useFlag<T extends FlagValue$1 = FlagValue$1>(flagKey: string, defaultValue: T, options?: ReactFlagEvaluationOptions): FlagQuery<T extends boolean ? boolean : T extends number ? number : T extends string ? string : T extends JsonValue ? T : JsonValue>;
33
107
  /**
34
108
  * Evaluates a feature flag, returning a boolean.
35
109
  * By default, components will re-render when the flag value changes.
110
+ * For a generic hook returning a queryable interface, see {@link useFlag}.
36
111
  * @param {string} flagKey the flag identifier
37
112
  * @param {boolean} defaultValue the default value
38
113
  * @param {ReactFlagEvaluationOptions} options options for this evaluation
@@ -42,55 +117,61 @@ declare function useBooleanFlagValue(flagKey: string, defaultValue: boolean, opt
42
117
  /**
43
118
  * Evaluates a feature flag, returning evaluation details.
44
119
  * By default, components will re-render when the flag value changes.
120
+ * For a generic hook returning a queryable interface, see {@link useFlag}.
45
121
  * @param {string} flagKey the flag identifier
46
122
  * @param {boolean} defaultValue the default value
47
123
  * @param {ReactFlagEvaluationOptions} options options for this evaluation
48
124
  * @returns { EvaluationDetails<boolean>} a EvaluationDetails object for this evaluation
49
125
  */
50
- declare function useBooleanFlagDetails(flagKey: string, defaultValue: boolean, options?: ReactFlagEvaluationOptions): EvaluationDetails<boolean>;
126
+ declare function useBooleanFlagDetails(flagKey: string, defaultValue: boolean, options?: ReactFlagEvaluationOptions): EvaluationDetails$1<boolean>;
51
127
  /**
52
128
  * Evaluates a feature flag, returning a string.
53
129
  * By default, components will re-render when the flag value changes.
130
+ * For a generic hook returning a queryable interface, see {@link useFlag}.
54
131
  * @param {string} flagKey the flag identifier
55
132
  * @template {string} [T=string] A optional generic argument constraining the string
56
133
  * @param {T} defaultValue the default value
57
134
  * @param {ReactFlagEvaluationOptions} options options for this evaluation
58
135
  * @returns { boolean} a EvaluationDetails object for this evaluation
59
136
  */
60
- declare function useStringFlagValue<T extends string = string>(flagKey: string, defaultValue: T, options?: ReactFlagEvaluationOptions): T;
137
+ declare function useStringFlagValue<T extends string = string>(flagKey: string, defaultValue: T, options?: ReactFlagEvaluationOptions): string;
61
138
  /**
62
139
  * Evaluates a feature flag, returning evaluation details.
63
140
  * By default, components will re-render when the flag value changes.
141
+ * For a generic hook returning a queryable interface, see {@link useFlag}.
64
142
  * @param {string} flagKey the flag identifier
65
143
  * @template {string} [T=string] A optional generic argument constraining the string
66
144
  * @param {T} defaultValue the default value
67
145
  * @param {ReactFlagEvaluationOptions} options options for this evaluation
68
146
  * @returns { EvaluationDetails<string>} a EvaluationDetails object for this evaluation
69
147
  */
70
- declare function useStringFlagDetails<T extends string = string>(flagKey: string, defaultValue: T, options?: ReactFlagEvaluationOptions): EvaluationDetails<T>;
148
+ declare function useStringFlagDetails<T extends string = string>(flagKey: string, defaultValue: T, options?: ReactFlagEvaluationOptions): EvaluationDetails$1<string>;
71
149
  /**
72
150
  * Evaluates a feature flag, returning a number.
73
151
  * By default, components will re-render when the flag value changes.
152
+ * For a generic hook returning a queryable interface, see {@link useFlag}.
74
153
  * @param {string} flagKey the flag identifier
75
154
  * @template {number} [T=number] A optional generic argument constraining the number
76
155
  * @param {T} defaultValue the default value
77
156
  * @param {ReactFlagEvaluationOptions} options options for this evaluation
78
157
  * @returns { boolean} a EvaluationDetails object for this evaluation
79
158
  */
80
- declare function useNumberFlagValue<T extends number = number>(flagKey: string, defaultValue: T, options?: ReactFlagEvaluationOptions): T;
159
+ declare function useNumberFlagValue<T extends number = number>(flagKey: string, defaultValue: T, options?: ReactFlagEvaluationOptions): number;
81
160
  /**
82
161
  * Evaluates a feature flag, returning evaluation details.
83
162
  * By default, components will re-render when the flag value changes.
163
+ * For a generic hook returning a queryable interface, see {@link useFlag}.
84
164
  * @param {string} flagKey the flag identifier
85
165
  * @template {number} [T=number] A optional generic argument constraining the number
86
166
  * @param {T} defaultValue the default value
87
167
  * @param {ReactFlagEvaluationOptions} options options for this evaluation
88
168
  * @returns { EvaluationDetails<number>} a EvaluationDetails object for this evaluation
89
169
  */
90
- declare function useNumberFlagDetails<T extends number = number>(flagKey: string, defaultValue: T, options?: ReactFlagEvaluationOptions): EvaluationDetails<T>;
170
+ declare function useNumberFlagDetails<T extends number = number>(flagKey: string, defaultValue: T, options?: ReactFlagEvaluationOptions): EvaluationDetails$1<number>;
91
171
  /**
92
172
  * Evaluates a feature flag, returning an object.
93
173
  * By default, components will re-render when the flag value changes.
174
+ * For a generic hook returning a queryable interface, see {@link useFlag}.
94
175
  * @param {string} flagKey the flag identifier
95
176
  * @template {JsonValue} [T=JsonValue] A optional generic argument describing the structure
96
177
  * @param {T} defaultValue the default value
@@ -101,13 +182,14 @@ declare function useObjectFlagValue<T extends JsonValue = JsonValue>(flagKey: st
101
182
  /**
102
183
  * Evaluates a feature flag, returning evaluation details.
103
184
  * By default, components will re-render when the flag value changes.
185
+ * For a generic hook returning a queryable interface, see {@link useFlag}.
104
186
  * @param {string} flagKey the flag identifier
105
187
  * @param {T} defaultValue the default value
106
188
  * @template {JsonValue} [T=JsonValue] A optional generic argument describing the structure
107
189
  * @param {ReactFlagEvaluationOptions} options options for this evaluation
108
190
  * @returns { EvaluationDetails<T>} a EvaluationDetails object for this evaluation
109
191
  */
110
- declare function useObjectFlagDetails<T extends JsonValue = JsonValue>(flagKey: string, defaultValue: T, options?: ReactFlagEvaluationOptions): EvaluationDetails<T>;
192
+ declare function useObjectFlagDetails<T extends JsonValue = JsonValue>(flagKey: string, defaultValue: T, options?: ReactFlagEvaluationOptions): EvaluationDetails$1<T>;
111
193
 
112
194
  type ClientOrDomain = {
113
195
  /**
@@ -125,8 +207,29 @@ type ClientOrDomain = {
125
207
  };
126
208
  type ProviderProps = {
127
209
  children?: React.ReactNode;
128
- } & ClientOrDomain;
129
- declare const OpenFeatureProvider: ({ client, domain, children }: ProviderProps) => React.JSX.Element;
130
- declare const useOpenFeatureClient: () => Client;
210
+ } & ClientOrDomain & ReactFlagEvaluationOptions;
211
+ /**
212
+ * Provides a scope for evaluating feature flags by binding a client to all child components.
213
+ * @param {ProviderProps} properties props for the context provider
214
+ * @returns {OpenFeatureProvider} context provider
215
+ */
216
+ declare function OpenFeatureProvider({ client, domain, children, ...options }: ProviderProps): React.JSX.Element;
217
+
218
+ /**
219
+ * Get the {@link Client} instance for this OpenFeatureProvider context.
220
+ * Note that the provider to which this is bound is determined by the OpenFeatureProvider's domain.
221
+ * @returns {Client} client for this scope
222
+ */
223
+ declare function useOpenFeatureClient(): Client;
224
+
225
+ type Options = Pick<ReactFlagEvaluationOptions, 'suspendUntilReady'>;
226
+ /**
227
+ * Utility hook that triggers suspense until the provider is {@link ProviderStatus.READY}, without evaluating any flags.
228
+ * Especially useful for React v16/17 "Legacy Suspense", in which siblings to suspending components are
229
+ * initially mounted and then hidden (see: https://github.com/reactwg/react-18/discussions/7).
230
+ * @param {Options} options options for suspense
231
+ * @returns {boolean} boolean indicating if provider is {@link ProviderStatus.READY}, useful if suspense is disabled and you want to handle loaders on your own
232
+ */
233
+ declare function useWhenProviderReady(options?: Options): boolean;
131
234
 
132
- export { OpenFeatureProvider, useBooleanFlagDetails, useBooleanFlagValue, useNumberFlagDetails, useNumberFlagValue, useObjectFlagDetails, useObjectFlagValue, useOpenFeatureClient, useStringFlagDetails, useStringFlagValue };
235
+ export { FlagQuery, OpenFeatureProvider, useBooleanFlagDetails, useBooleanFlagValue, useFlag, useNumberFlagDetails, useNumberFlagValue, useObjectFlagDetails, useObjectFlagValue, useOpenFeatureClient, useStringFlagDetails, useStringFlagValue, useWhenProviderReady };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@openfeature/react-sdk",
3
- "version": "0.2.3-experimental",
3
+ "version": "0.3.0-experimental",
4
4
  "description": "OpenFeature React SDK",
5
5
  "main": "./dist/cjs/index.js",
6
6
  "files": [
@@ -46,7 +46,7 @@
46
46
  },
47
47
  "homepage": "https://github.com/open-feature/js-sdk#readme",
48
48
  "peerDependencies": {
49
- "@openfeature/web-sdk": ">=1.0.0",
49
+ "@openfeature/web-sdk": "^1.0.2",
50
50
  "react": ">=16.8.0"
51
51
  },
52
52
  "devDependencies": {