@openfeature/react-sdk 1.1.0 → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/types.d.ts CHANGED
@@ -1,8 +1,56 @@
1
- import { FlagEvaluationOptions, FlagValue as FlagValue$1, JsonValue, EvaluationDetails as EvaluationDetails$1, Client, ProviderStatus, Provider, EvaluationContext, Tracking } from '@openfeature/web-sdk';
2
- export * from '@openfeature/web-sdk';
3
- import { FlagValue, EvaluationDetails, FlagMetadata, StandardResolutionReasons, ErrorCode } from '@openfeature/core';
4
1
  import * as React from 'react';
5
2
  import React__default from 'react';
3
+ import { FlagValue, EvaluationDetails, FlagMetadata, StandardResolutionReasons, ErrorCode } from '@openfeature/core';
4
+ import { FlagEvaluationOptions, FlagValue as FlagValue$1, JsonValue, EvaluationDetails as EvaluationDetails$1, Client, ProviderStatus, Provider, EvaluationContext, Tracking } from '@openfeature/web-sdk';
5
+ export * from '@openfeature/web-sdk';
6
+
7
+ interface FlagQuery<T extends FlagValue = FlagValue> {
8
+ /**
9
+ * A structure representing the result of the flag evaluation process
10
+ */
11
+ readonly details: EvaluationDetails<T>;
12
+ /**
13
+ * The flag value
14
+ */
15
+ readonly value: T;
16
+ /**
17
+ * A variant is a semantic identifier for a value.
18
+ * Not available from all providers.
19
+ */
20
+ readonly variant: string | undefined;
21
+ /**
22
+ * Arbitrary data associated with this flag or evaluation.
23
+ * Not available from all providers.
24
+ */
25
+ readonly flagMetadata: FlagMetadata;
26
+ /**
27
+ * The reason the evaluation resolved to the particular value.
28
+ * Not available from all providers.
29
+ */
30
+ readonly reason: typeof StandardResolutionReasons | string | undefined;
31
+ /**
32
+ * Indicates if this flag defaulted due to an error.
33
+ * Specifically, indicates reason equals {@link StandardResolutionReasons.ERROR} or the errorCode is set, this field is truthy.
34
+ */
35
+ readonly isError: boolean;
36
+ /**
37
+ * The error code, see {@link ErrorCode}.
38
+ */
39
+ readonly errorCode: ErrorCode | undefined;
40
+ /**
41
+ * A message associated with the error.
42
+ */
43
+ readonly errorMessage: string | undefined;
44
+ /**
45
+ * Indicates this flag is up-to-date and in sync with the source of truth.
46
+ * Specifically, indicates the evaluation did not default due to error, and the reason is neither {@link StandardResolutionReasons.STALE} or {@link StandardResolutionReasons.DISABLED}.
47
+ */
48
+ readonly isAuthoritative: boolean;
49
+ /**
50
+ * The type of the value, as returned by "typeof" operator.
51
+ */
52
+ readonly type: 'string' | 'number' | 'bigint' | 'boolean' | 'symbol' | 'undefined' | 'object' | 'function';
53
+ }
6
54
 
7
55
  type ReactFlagEvaluationOptions = ({
8
56
  /**
@@ -50,53 +98,69 @@ type ReactFlagEvaluationOptions = ({
50
98
  type ReactFlagEvaluationNoSuspenseOptions = Omit<ReactFlagEvaluationOptions, 'suspend' | 'suspendUntilReady' | 'suspendWhileReconciling'>;
51
99
  type NormalizedOptions = Omit<ReactFlagEvaluationOptions, 'suspend'>;
52
100
 
53
- interface FlagQuery<T extends FlagValue = FlagValue> {
54
- /**
55
- * A structure representing the result of the flag evaluation process
56
- */
57
- readonly details: EvaluationDetails<T>;
101
+ /**
102
+ * Props for the FeatureFlag component that conditionally renders content based on feature flag state.
103
+ * @interface FeatureFlagProps
104
+ */
105
+ interface FeatureFlagProps<T extends FlagValue = FlagValue> {
58
106
  /**
59
- * The flag value
107
+ * The key of the feature flag to evaluate.
60
108
  */
61
- readonly value: T;
109
+ flagKey: string;
62
110
  /**
63
- * A variant is a semantic identifier for a value.
64
- * Not available from all providers.
65
- */
66
- readonly variant: string | undefined;
67
- /**
68
- * Arbitrary data associated with this flag or evaluation.
69
- * Not available from all providers.
111
+ * Optional predicate function for custom matching logic.
112
+ * If provided, this function will be used instead of the default equality check.
113
+ * @param matchValue The value to match (matchValue prop)
114
+ * @param details The evaluation details
115
+ * @returns true if the condition is met, false otherwise
70
116
  */
71
- readonly flagMetadata: FlagMetadata;
117
+ predicate?: (matchValue: T | undefined, details: EvaluationDetails<T>) => boolean;
72
118
  /**
73
- * The reason the evaluation resolved to the particular value.
74
- * Not available from all providers.
119
+ * Content to render when the feature flag condition is met.
120
+ * Can be a React node or a function that receives flag query details and returns a React node.
75
121
  */
76
- readonly reason: typeof StandardResolutionReasons | string | undefined;
122
+ children: React__default.ReactNode | ((details: FlagQuery<T>) => React__default.ReactNode);
77
123
  /**
78
- * Indicates if this flag defaulted due to an error.
79
- * Specifically, indicates reason equals {@link StandardResolutionReasons.ERROR} or the errorCode is set, this field is truthy.
124
+ * Optional content to render when the feature flag condition is not met.
125
+ * Can be a React node or a function that receives evaluation details and returns a React node.
80
126
  */
81
- readonly isError: boolean;
127
+ fallback?: React__default.ReactNode | ((details: EvaluationDetails<T>) => React__default.ReactNode);
82
128
  /**
83
- * The error code, see {@link ErrorCode}.
129
+ * Flag evaluation options that will be passed to useFlag hook.
84
130
  */
85
- readonly errorCode: ErrorCode | undefined;
131
+ evaluationOptions?: ReactFlagEvaluationOptions;
132
+ }
133
+ /**
134
+ * Configuration for matching flag values.
135
+ * For boolean flags, `match` is optional (defaults to checking truthiness).
136
+ * For non-boolean flags (string, number, object), `match` is required to determine when to render.
137
+ */
138
+ type FeatureFlagMatchConfig<T extends FlagValue> = {
86
139
  /**
87
- * A message associated with the error.
140
+ * Default value to use when the feature flag is not found.
88
141
  */
89
- readonly errorMessage: string | undefined;
142
+ defaultValue: T;
143
+ } & (T extends boolean ? {
90
144
  /**
91
- * Indicates this flag is up-to-date and in sync with the source of truth.
92
- * Specifically, indicates the evaluation did not default due to error, and the reason is neither {@link StandardResolutionReasons.STALE} or {@link StandardResolutionReasons.DISABLED}.
145
+ * Optional value to match against the feature flag value.
93
146
  */
94
- readonly isAuthoritative: boolean;
147
+ matchValue?: T | undefined;
148
+ } : {
95
149
  /**
96
- * The type of the value, as returned by "typeof" operator.
150
+ * Value to match against the feature flag value.
151
+ * Required for non-boolean flags to determine when children should render.
152
+ * By default, strict equality is used for comparison.
97
153
  */
98
- readonly type: 'string' | 'number' | 'bigint' | 'boolean' | 'symbol' | 'undefined' | 'object' | 'function';
99
- }
154
+ matchValue: T;
155
+ });
156
+ type FeatureFlagComponentProps<T extends FlagValue> = FeatureFlagProps<T> & FeatureFlagMatchConfig<T>;
157
+ /**
158
+ * @experimental This API is experimental, and is subject to change.
159
+ * FeatureFlag component that conditionally renders its children based on the evaluation of a feature flag.
160
+ * @param {FeatureFlagComponentProps} props The properties for the FeatureFlag component.
161
+ * @returns {React.ReactElement | null} The rendered component or null if the feature is not enabled.
162
+ */
163
+ declare function FeatureFlag<T extends FlagValue = FlagValue>({ flagKey, matchValue, predicate, defaultValue, children, evaluationOptions, fallback, }: FeatureFlagComponentProps<T>): React__default.ReactElement | null;
100
164
 
101
165
  /**
102
166
  * Evaluates a feature flag generically, returning an react-flavored queryable object.
@@ -230,7 +294,7 @@ type ProviderProps = {
230
294
  * @param {ProviderProps} properties props for the context provider
231
295
  * @returns {OpenFeatureProvider} context provider
232
296
  */
233
- declare function OpenFeatureProvider({ client, domain, children, ...options }: ProviderProps): JSX.Element;
297
+ declare function OpenFeatureProvider({ client, domain, children, ...options }: ProviderProps): React.JSX.Element;
234
298
 
235
299
  /**
236
300
  * Get the {@link Client} instance for this OpenFeatureProvider context.
@@ -294,7 +358,7 @@ declare function OpenFeatureTestProvider(testProviderOptions: TestProviderProps)
294
358
  type ContextMutationOptions = {
295
359
  /**
296
360
  * Mutate the default context instead of the domain scoped context applied at the `<OpenFeatureProvider/>`.
297
- * Note, if the `<OpenFeatureProvider/>` has no domain specified, the default is used.
361
+ * By default, will use the domain set on `<OpenFeatureProvider/>` (or the domain associated with the client set on `<OpenFeatureProvider/>`).
298
362
  * See the {@link https://openfeature.dev/docs/reference/technologies/client/web/#manage-evaluation-context-for-domains|documentation} for more information.
299
363
  * @default false
300
364
  */
@@ -333,5 +397,5 @@ type Track = {
333
397
  */
334
398
  declare function useTrack(): Track;
335
399
 
336
- export { OpenFeatureProvider, OpenFeatureTestProvider, useBooleanFlagDetails, useBooleanFlagValue, useContextMutator, useFlag, useNumberFlagDetails, useNumberFlagValue, useObjectFlagDetails, useObjectFlagValue, useOpenFeatureClient, useOpenFeatureClientStatus, useStringFlagDetails, useStringFlagValue, useSuspenseFlag, useTrack, useWhenProviderReady };
400
+ export { FeatureFlag, OpenFeatureProvider, OpenFeatureTestProvider, useBooleanFlagDetails, useBooleanFlagValue, useContextMutator, useFlag, useNumberFlagDetails, useNumberFlagValue, useObjectFlagDetails, useObjectFlagValue, useOpenFeatureClient, useOpenFeatureClientStatus, useStringFlagDetails, useStringFlagValue, useSuspenseFlag, useTrack, useWhenProviderReady };
337
401
  export type { ContextMutation, ContextMutationOptions, FlagQuery, NormalizedOptions, ReactFlagEvaluationNoSuspenseOptions, ReactFlagEvaluationOptions, Track };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@openfeature/react-sdk",
3
- "version": "1.1.0",
3
+ "version": "1.2.0",
4
4
  "description": "OpenFeature React SDK",
5
5
  "main": "./dist/cjs/index.js",
6
6
  "files": [