diff --git a/api/react.api.md b/api/react.api.md index b609327..3fd87b8 100644 --- a/api/react.api.md +++ b/api/react.api.md @@ -40,9 +40,21 @@ export class ConfidenceReact implements EventSender, Trackable, FlagResolver { get contextState(): string; // @internal readonly delegate: Confidence; - evaluateFlag(path: string, defaultValue: T): FlagEvaluation>; + evaluateFlag(path: string, defaultValue: string): FlagEvaluation; + // (undocumented) + evaluateFlag(path: string, defaultValue: boolean): FlagEvaluation; + // (undocumented) + evaluateFlag(path: string, defaultValue: number): FlagEvaluation; + // (undocumented) + evaluateFlag(path: string, defaultValue: T): FlagEvaluation; getContext(): Context; - getFlag(path: string, defaultValue: T): Promise>; + getFlag(path: string, defaultValue: string): Promise; + // (undocumented) + getFlag(path: string, defaultValue: boolean): Promise; + // (undocumented) + getFlag(path: string, defaultValue: number): Promise; + // (undocumented) + getFlag(path: string, defaultValue: T): Promise; setContext(context: Context, { transition }?: { transition?: boolean | undefined; }): void; @@ -50,8 +62,20 @@ export class ConfidenceReact implements EventSender, Trackable, FlagResolver { track(name: string, message?: Value.Struct): void; track(manager: Trackable.Manager): Closer; useContext(): Context; - useEvaluateFlag(path: string, defaultValue: T): FlagEvaluation>; - useFlag(path: string, defaultValue: T): Value.Widen; + useEvaluateFlag(path: string, defaultValue: string): FlagEvaluation; + // (undocumented) + useEvaluateFlag(path: string, defaultValue: number): FlagEvaluation; + // (undocumented) + useEvaluateFlag(path: string, defaultValue: boolean): FlagEvaluation; + // (undocumented) + useEvaluateFlag(path: string, defaultValue: T): FlagEvaluation; + useFlag(path: string, defaultValue: string): string; + // (undocumented) + useFlag(path: string, defaultValue: number): number; + // (undocumented) + useFlag(path: string, defaultValue: boolean): boolean; + // (undocumented) + useFlag(path: string, defaultValue: T): T; useWithContext(context: Context): ConfidenceReact; withContext(context: Context): ConfidenceReact; } @@ -62,11 +86,37 @@ export const useConfidence: () => ConfidenceReact; // @public export function useConfidenceContext(confidence?: ConfidenceReact): Context; +// Warning: (ae-missing-release-tag) "useEvaluateFlag" is part of the package's API, but it is missing a release tag (@alpha, @beta, @public, or @internal) +// Warning: (ae-missing-release-tag) "useEvaluateFlag" is part of the package's API, but it is missing a release tag (@alpha, @beta, @public, or @internal) +// Warning: (ae-missing-release-tag) "useEvaluateFlag" is part of the package's API, but it is missing a release tag (@alpha, @beta, @public, or @internal) +// // @public -export function useEvaluateFlag(path: string, defaultValue: T, confidence?: ConfidenceReact): FlagEvaluation>; +export function useEvaluateFlag(path: string, defaultValue: string, confidence?: ConfidenceReact): FlagEvaluation; +// @public (undocumented) +export function useEvaluateFlag(path: string, defaultValue: number, confidence?: ConfidenceReact): FlagEvaluation; + +// @public (undocumented) +export function useEvaluateFlag(path: string, defaultValue: boolean, confidence?: ConfidenceReact): FlagEvaluation; + +// @public (undocumented) +export function useEvaluateFlag(path: string, defaultValue: T, confidence?: ConfidenceReact): FlagEvaluation; + +// Warning: (ae-missing-release-tag) "useFlag" is part of the package's API, but it is missing a release tag (@alpha, @beta, @public, or @internal) +// Warning: (ae-missing-release-tag) "useFlag" is part of the package's API, but it is missing a release tag (@alpha, @beta, @public, or @internal) +// Warning: (ae-missing-release-tag) "useFlag" is part of the package's API, but it is missing a release tag (@alpha, @beta, @public, or @internal) +// // @public -export function useFlag(path: string, defaultValue: T, confidence?: ConfidenceReact): Value.Widen; +export function useFlag(path: string, defaultValue: string, confidence?: ConfidenceReact): string; + +// @public (undocumented) +export function useFlag(path: string, defaultValue: number, confidence?: ConfidenceReact): number; + +// @public (undocumented) +export function useFlag(path: string, defaultValue: boolean, confidence?: ConfidenceReact): boolean; + +// @public (undocumented) +export function useFlag(path: string, defaultValue: T, confidence?: ConfidenceReact): T; // @public export function useWithContext(context: Context, parent?: ConfidenceReact): ConfidenceReact; diff --git a/api/sdk.api.md b/api/sdk.api.md index 25280b5..205b336 100644 --- a/api/sdk.api.md +++ b/api/sdk.api.md @@ -24,10 +24,22 @@ export class Confidence implements EventSender, Trackable, FlagResolver { readonly contextChanges: Subscribe; static create({ clientSecret, region, timeout, environment, fetchImplementation, logger, }: ConfidenceOptions): Confidence; get environment(): string; - evaluateFlag(path: string, defaultValue: T): FlagEvaluation>; + evaluateFlag(path: string, defaultValue: string): FlagEvaluation; + // (undocumented) + evaluateFlag(path: string, defaultValue: boolean): FlagEvaluation; + // (undocumented) + evaluateFlag(path: string, defaultValue: number): FlagEvaluation; + // (undocumented) + evaluateFlag(path: string, defaultValue: T): FlagEvaluation; get flagState(): State; getContext(): Context; - getFlag(path: string, defaultValue: T): Promise>; + getFlag(path: string, defaultValue: string): Promise; + // (undocumented) + getFlag(path: string, defaultValue: boolean): Promise; + // (undocumented) + getFlag(path: string, defaultValue: number): Promise; + // (undocumented) + getFlag(path: string, defaultValue: T): Promise; // Warning: (ae-forgotten-export) The symbol "AccessiblePromise" needs to be exported by the entry point index.d.ts protected resolveFlags(): AccessiblePromise; setContext(context: Context): boolean; @@ -125,8 +137,14 @@ export type FlagEvaluation = FlagEvaluation.Resolved | FlagEvaluation.Stal // @public export interface FlagResolver extends Contextual { - evaluateFlag(path: string, defaultValue: T): FlagEvaluation>; - getFlag(path: string, defaultValue: T): Promise>; + evaluateFlag(path: string, defaultValue: string): FlagEvaluation; + evaluateFlag(path: string, defaultValue: boolean): FlagEvaluation; + evaluateFlag(path: string, defaultValue: number): FlagEvaluation; + evaluateFlag(path: string, defaultValue: T): FlagEvaluation; + getFlag(path: string, defaultValue: string): Promise; + getFlag(path: string, defaultValue: boolean): Promise; + getFlag(path: string, defaultValue: number): Promise; + getFlag(path: string, defaultValue: T): Promise; subscribe(onStateChange?: StateObserver): () => void; } @@ -178,7 +196,6 @@ export namespace Value { readonly [key: string]: Value; }; export type TypeName = 'number' | 'string' | 'boolean' | 'Struct' | 'List' | 'undefined'; - export type Widen = T extends number ? number : T extends string ? string : T extends boolean ? boolean : T; } // @public diff --git a/packages/react/src/index.tsx b/packages/react/src/index.tsx index 80d4bf4..17d92ae 100644 --- a/packages/react/src/index.tsx +++ b/packages/react/src/index.tsx @@ -119,12 +119,20 @@ export class ConfidenceReact implements EventSender, Trackable, FlagResolver { return new ConfidenceReact(this.delegate.withContext(context)); } /** Evaluates a flag */ - evaluateFlag(path: string, defaultValue: T): FlagEvaluation> { + evaluateFlag(path: string, defaultValue: string): FlagEvaluation; + evaluateFlag(path: string, defaultValue: boolean): FlagEvaluation; + evaluateFlag(path: string, defaultValue: number): FlagEvaluation; + evaluateFlag(path: string, defaultValue: T): FlagEvaluation; + evaluateFlag(path: string, defaultValue: T): FlagEvaluation { this.assertContext('evaluateFlag', 'useEvaluateFlag'); return this.delegate.evaluateFlag(path, defaultValue); } /** Returns flag value for a given flag */ - getFlag(path: string, defaultValue: T): Promise> { + getFlag(path: string, defaultValue: string): Promise; + getFlag(path: string, defaultValue: boolean): Promise; + getFlag(path: string, defaultValue: number): Promise; + getFlag(path: string, defaultValue: T): Promise; + getFlag(path: string, defaultValue: T): Promise { this.assertContext('getFlag', 'useFlag'); return this.delegate.getFlag(path, defaultValue); } @@ -142,12 +150,20 @@ export class ConfidenceReact implements EventSender, Trackable, FlagResolver { return useWithContext(context, this); } /** Hook to use EvaluateFlag functionality */ - useEvaluateFlag(path: string, defaultValue: T): FlagEvaluation> { + useEvaluateFlag(path: string, defaultValue: string): FlagEvaluation; + useEvaluateFlag(path: string, defaultValue: number): FlagEvaluation; + useEvaluateFlag(path: string, defaultValue: boolean): FlagEvaluation; + useEvaluateFlag(path: string, defaultValue: T): FlagEvaluation; + useEvaluateFlag(path: string, defaultValue: T): FlagEvaluation { this.assertContext('useEvaluateFlag', 'evaluateFlag'); return useEvaluateFlag(path, defaultValue, this); } /** Hook to use getFlag functionality */ - useFlag(path: string, defaultValue: T): Value.Widen { + useFlag(path: string, defaultValue: string): string; + useFlag(path: string, defaultValue: number): number; + useFlag(path: string, defaultValue: boolean): boolean; + useFlag(path: string, defaultValue: T): T; + useFlag(path: string, defaultValue: T): T { this.assertContext('useFlag', 'getFlag'); return useFlag(path, defaultValue, this); } @@ -233,12 +249,32 @@ export function useConfidenceContext(confidence = useConfidence()): Context { /** * Use EvaluateFlag * @public */ +export function useEvaluateFlag( + path: string, + defaultValue: string, + confidence?: ConfidenceReact, +): FlagEvaluation; +export function useEvaluateFlag( + path: string, + defaultValue: number, + confidence?: ConfidenceReact, +): FlagEvaluation; +export function useEvaluateFlag( + path: string, + defaultValue: boolean, + confidence?: ConfidenceReact, +): FlagEvaluation; +export function useEvaluateFlag( + path: string, + defaultValue: T, + confidence?: ConfidenceReact, +): FlagEvaluation; export function useEvaluateFlag( path: string, defaultValue: T, // eslint-disable-next-line react-hooks/rules-of-hooks confidence = useConfidence(), -): FlagEvaluation> { +): FlagEvaluation { const evaluation = confidence.delegate.evaluateFlag(path, defaultValue); const [, setState] = useState(() => confidence.contextState); useEffect(() => { @@ -254,7 +290,11 @@ export function useEvaluateFlag( * Use Flag * @public */ +export function useFlag(path: string, defaultValue: string, confidence?: ConfidenceReact): string; +export function useFlag(path: string, defaultValue: number, confidence?: ConfidenceReact): number; +export function useFlag(path: string, defaultValue: boolean, confidence?: ConfidenceReact): boolean; +export function useFlag(path: string, defaultValue: T, confidence?: ConfidenceReact): T; // eslint-disable-next-line react-hooks/rules-of-hooks -export function useFlag(path: string, defaultValue: T, confidence = useConfidence()): Value.Widen { +export function useFlag(path: string, defaultValue: T, confidence = useConfidence()): T { return useEvaluateFlag(path, defaultValue, confidence).value; } diff --git a/packages/sdk/src/Confidence.ts b/packages/sdk/src/Confidence.ts index 48a190a..c27fd8d 100644 --- a/packages/sdk/src/Confidence.ts +++ b/packages/sdk/src/Confidence.ts @@ -276,7 +276,11 @@ export class Confidence implements EventSender, Trackable, FlagResolver { } /** Evaluates a flag */ - evaluateFlag(path: string, defaultValue: T): FlagEvaluation> { + evaluateFlag(path: string, defaultValue: string): FlagEvaluation; + evaluateFlag(path: string, defaultValue: boolean): FlagEvaluation; + evaluateFlag(path: string, defaultValue: number): FlagEvaluation; + evaluateFlag(path: string, defaultValue: T): FlagEvaluation; + evaluateFlag(path: string, defaultValue: T): FlagEvaluation { let evaluation: FlagEvaluation; // resolveFlags might update state synchronously if (!this.currentFlags && !this.pendingFlags) this.resolveFlags(); @@ -297,13 +301,17 @@ export class Confidence implements EventSender, Trackable, FlagResolver { ...evaluation, then, }; - return staleEvaluation as FlagEvaluation>; + return staleEvaluation; } - return evaluation as FlagEvaluation>; + return evaluation; } /** Returns flag value for a given flag */ - async getFlag(path: string, defaultValue: T): Promise> { + getFlag(path: string, defaultValue: string): Promise; + getFlag(path: string, defaultValue: boolean): Promise; + getFlag(path: string, defaultValue: number): Promise; + getFlag(path: string, defaultValue: T): Promise; + async getFlag(path: string, defaultValue: any): Promise { return (await this.evaluateFlag(path, defaultValue)).value; } diff --git a/packages/sdk/src/Value.ts b/packages/sdk/src/Value.ts index 6025568..c05c897 100644 --- a/packages/sdk/src/Value.ts +++ b/packages/sdk/src/Value.ts @@ -20,15 +20,6 @@ export namespace Value { /** Readonly List */ export type List = ReadonlyArray | ReadonlyArray | ReadonlyArray; - /** Sets Confidence used Values to be implementations of primitive types */ - export type Widen = T extends number - ? number - : T extends string - ? string - : T extends boolean - ? boolean - : T; - /** Asserts a Value */ export function assertValue(value: unknown): asserts value is Value { switch (typeof value) { diff --git a/packages/sdk/src/flags.ts b/packages/sdk/src/flags.ts index dbbef75..bc08849 100644 --- a/packages/sdk/src/flags.ts +++ b/packages/sdk/src/flags.ts @@ -69,9 +69,21 @@ export interface FlagResolver extends Contextual { /** Subscribe to flag changes in Confidence */ subscribe(onStateChange?: StateObserver): () => void; + /** Evaluates a string flag */ + evaluateFlag(path: string, defaultValue: string): FlagEvaluation; + /** Evaluates a boolean flag */ + evaluateFlag(path: string, defaultValue: boolean): FlagEvaluation; + /** Evaluates a numeric flag */ + evaluateFlag(path: string, defaultValue: number): FlagEvaluation; /** Evaluates a flag */ - evaluateFlag(path: string, defaultValue: T): FlagEvaluation>; + evaluateFlag(path: string, defaultValue: T): FlagEvaluation; - /** Returns flag value for a given flag */ - getFlag(path: string, defaultValue: T): Promise>; + /** Returns flag value for a string flag */ + getFlag(path: string, defaultValue: string): Promise; + /** Returns flag value for a boolean flag */ + getFlag(path: string, defaultValue: boolean): Promise; + /** Returns flag value for a numeric flag */ + getFlag(path: string, defaultValue: number): Promise; + /** Returns flag value for a flag */ + getFlag(path: string, defaultValue: T): Promise; }