|
1 | | -import { Client, EvaluationDetails, FlagEvaluationOptions, FlagValue, ProviderEvents, ProviderStatus } from '@openfeature/web-sdk'; |
| 1 | +import { Client, EvaluationDetails, FlagEvaluationOptions, FlagValue, JsonValue, ProviderEvents, ProviderStatus } from '@openfeature/web-sdk'; |
2 | 2 | import { Dispatch, SetStateAction, useEffect, useState } from 'react'; |
3 | 3 | import { useOpenFeatureClient } from './provider'; |
4 | 4 |
|
@@ -37,15 +37,117 @@ enum SuspendState { |
37 | 37 | Error |
38 | 38 | } |
39 | 39 |
|
| 40 | +/** |
| 41 | + * Evaluates a feature flag, returning a boolean. |
| 42 | + * By default, components will re-render when the flag value changes. |
| 43 | + * @param {string} flagKey the flag identifier |
| 44 | + * @param {boolean} defaultValue the default value |
| 45 | + * @param {ReactFlagEvaluationOptions} options options for this evaluation |
| 46 | + * @returns { boolean} a EvaluationDetails object for this evaluation |
| 47 | + */ |
| 48 | +export function useBooleanFlagValue(flagKey: string, defaultValue: boolean, options?: ReactFlagEvaluationOptions): boolean { |
| 49 | + return useBooleanFlagDetails(flagKey, defaultValue, options).value; |
| 50 | +} |
| 51 | + |
40 | 52 | /** |
41 | 53 | * Evaluates a feature flag, returning evaluation details. |
42 | | - * @param {string}flagKey the flag identifier |
| 54 | + * By default, components will re-render when the flag value changes. |
| 55 | + * @param {string} flagKey the flag identifier |
| 56 | + * @param {boolean} defaultValue the default value |
| 57 | + * @param {ReactFlagEvaluationOptions} options options for this evaluation |
| 58 | + * @returns { EvaluationDetails<boolean>} a EvaluationDetails object for this evaluation |
| 59 | + */ |
| 60 | +export function useBooleanFlagDetails(flagKey: string, defaultValue: boolean, options?: ReactFlagEvaluationOptions): EvaluationDetails<boolean> { |
| 61 | + return attachHandlersAndResolve(flagKey, defaultValue, (client) => { |
| 62 | + return client.getBooleanDetails; |
| 63 | + }, options); |
| 64 | +} |
| 65 | + |
| 66 | +/** |
| 67 | + * Evaluates a feature flag, returning a string. |
| 68 | + * By default, components will re-render when the flag value changes. |
| 69 | + * @param {string} flagKey the flag identifier |
| 70 | + * @template {string} [T=string] A optional generic argument constraining the string |
43 | 71 | * @param {T} defaultValue the default value |
44 | 72 | * @param {ReactFlagEvaluationOptions} options options for this evaluation |
45 | | - * @template T flag type |
| 73 | + * @returns { boolean} a EvaluationDetails object for this evaluation |
| 74 | + */ |
| 75 | +export function useStringFlagValue<T extends string = string>(flagKey: string, defaultValue: T, options?: ReactFlagEvaluationOptions): T { |
| 76 | + return useStringFlagDetails(flagKey, defaultValue, options).value; |
| 77 | +} |
| 78 | + |
| 79 | +/** |
| 80 | + * Evaluates a feature flag, returning evaluation details. |
| 81 | + * By default, components will re-render when the flag value changes. |
| 82 | + * @param {string} flagKey the flag identifier |
| 83 | + * @template {string} [T=string] A optional generic argument constraining the string |
| 84 | + * @param {T} defaultValue the default value |
| 85 | + * @param {ReactFlagEvaluationOptions} options options for this evaluation |
| 86 | + * @returns { EvaluationDetails<string>} a EvaluationDetails object for this evaluation |
| 87 | + */ |
| 88 | +export function useStringFlagDetails<T extends string = string>(flagKey: string, defaultValue: T, options?: ReactFlagEvaluationOptions): EvaluationDetails<T> { |
| 89 | + return attachHandlersAndResolve(flagKey, defaultValue, (client) => { |
| 90 | + return client.getStringDetails<T>; |
| 91 | + }, options); |
| 92 | +} |
| 93 | + |
| 94 | +/** |
| 95 | + * Evaluates a feature flag, returning a number. |
| 96 | + * By default, components will re-render when the flag value changes. |
| 97 | + * @param {string} flagKey the flag identifier |
| 98 | + * @template {number} [T=number] A optional generic argument constraining the number |
| 99 | + * @param {T} defaultValue the default value |
| 100 | + * @param {ReactFlagEvaluationOptions} options options for this evaluation |
| 101 | + * @returns { boolean} a EvaluationDetails object for this evaluation |
| 102 | + */ |
| 103 | +export function useNumberFlagValue<T extends number = number>(flagKey: string, defaultValue: T, options?: ReactFlagEvaluationOptions): T { |
| 104 | + return useNumberFlagDetails(flagKey, defaultValue, options).value; |
| 105 | +} |
| 106 | + |
| 107 | +/** |
| 108 | + * Evaluates a feature flag, returning evaluation details. |
| 109 | + * By default, components will re-render when the flag value changes. |
| 110 | + * @param {string} flagKey the flag identifier |
| 111 | + * @template {number} [T=number] A optional generic argument constraining the number |
| 112 | + * @param {T} defaultValue the default value |
| 113 | + * @param {ReactFlagEvaluationOptions} options options for this evaluation |
| 114 | + * @returns { EvaluationDetails<number>} a EvaluationDetails object for this evaluation |
| 115 | + */ |
| 116 | +export function useNumberFlagDetails<T extends number = number>(flagKey: string, defaultValue: T, options?: ReactFlagEvaluationOptions): EvaluationDetails<T> { |
| 117 | + return attachHandlersAndResolve(flagKey, defaultValue, (client) => { |
| 118 | + return client.getNumberDetails<T>; |
| 119 | + }, options); |
| 120 | +} |
| 121 | + |
| 122 | +/** |
| 123 | + * Evaluates a feature flag, returning an object. |
| 124 | + * By default, components will re-render when the flag value changes. |
| 125 | + * @param {string} flagKey the flag identifier |
| 126 | + * @template {JsonValue} [T=JsonValue] A optional generic argument describing the structure |
| 127 | + * @param {T} defaultValue the default value |
| 128 | + * @param {ReactFlagEvaluationOptions} options options for this evaluation |
| 129 | + * @returns { boolean} a EvaluationDetails object for this evaluation |
| 130 | + */ |
| 131 | +export function useObjectFlagValue<T extends JsonValue = JsonValue>(flagKey: string, defaultValue: T, options?: ReactFlagEvaluationOptions): T { |
| 132 | + return useObjectFlagDetails<T>(flagKey, defaultValue, options).value; |
| 133 | +} |
| 134 | + |
| 135 | +/** |
| 136 | + * Evaluates a feature flag, returning evaluation details. |
| 137 | + * By default, components will re-render when the flag value changes. |
| 138 | + * @param {string} flagKey the flag identifier |
| 139 | + * @param {T} defaultValue the default value |
| 140 | + * @template {JsonValue} [T=JsonValue] A optional generic argument describing the structure |
| 141 | + * @param {ReactFlagEvaluationOptions} options options for this evaluation |
46 | 142 | * @returns { EvaluationDetails<T>} a EvaluationDetails object for this evaluation |
47 | 143 | */ |
48 | | -export function useFeatureFlag<T extends FlagValue>(flagKey: string, defaultValue: T, options?: ReactFlagEvaluationOptions): EvaluationDetails<T> { |
| 144 | +export function useObjectFlagDetails<T extends JsonValue = JsonValue>(flagKey: string, defaultValue: T, options?: ReactFlagEvaluationOptions): EvaluationDetails<T> { |
| 145 | + return attachHandlersAndResolve(flagKey, defaultValue, (client) => { |
| 146 | + return client.getObjectDetails<T>; |
| 147 | + }, options); |
| 148 | +} |
| 149 | + |
| 150 | +function attachHandlersAndResolve<T extends FlagValue>(flagKey: string, defaultValue: T, resolver: (client: Client) => (flagKey: string, defaultValue: T) => EvaluationDetails<T>, options?: ReactFlagEvaluationOptions): EvaluationDetails<T> { |
49 | 151 | const defaultedOptions = { ...DEFAULT_OPTIONS, ...options }; |
50 | 152 | const [, updateState] = useState<object | undefined>(); |
51 | 153 | const forceUpdate = () => { |
@@ -80,19 +182,7 @@ export function useFeatureFlag<T extends FlagValue>(flagKey: string, defaultValu |
80 | 182 | }; |
81 | 183 | }, [client]); |
82 | 184 |
|
83 | | - return getFlag(client, flagKey, defaultValue); |
84 | | -} |
85 | | - |
86 | | -function getFlag<T extends FlagValue>(client: Client, flagKey: string, defaultValue: T): EvaluationDetails<T> { |
87 | | - if (typeof defaultValue === 'boolean') { |
88 | | - return client.getBooleanDetails(flagKey, defaultValue) as EvaluationDetails<T>; |
89 | | - } else if (typeof defaultValue === 'string') { |
90 | | - return client.getStringDetails(flagKey, defaultValue) as EvaluationDetails<T>; |
91 | | - } else if (typeof defaultValue === 'number') { |
92 | | - return client.getNumberDetails(flagKey, defaultValue) as EvaluationDetails<T>; |
93 | | - } else { |
94 | | - return client.getObjectDetails(flagKey, defaultValue) as EvaluationDetails<T>; |
95 | | - } |
| 185 | + return resolver(client).call(client, flagKey, defaultValue); |
96 | 186 | } |
97 | 187 |
|
98 | 188 | /** |
|
0 commit comments