Skip to content

Commit

Permalink
add non-generic overloads
Browse files Browse the repository at this point in the history
Signed-off-by: Todd Baert <[email protected]>
  • Loading branch information
toddbaert committed Sep 20, 2022
1 parent f61b5d2 commit 715d9a9
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 7 deletions.
36 changes: 36 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,12 @@ export interface Features {
/**
* Get a string flag value.
*/
getStringValue(
flagKey: string,
defaultValue: string,
context?: EvaluationContext,
options?: FlagEvaluationOptions
): Promise<string>;
getStringValue<T extends string = string>(
flagKey: string,
defaultValue: T,
Expand All @@ -76,6 +82,12 @@ export interface Features {
/**
* Get a string flag with additional details.
*/
getStringDetails(
flagKey: string,
defaultValue: string,
context?: EvaluationContext,
options?: FlagEvaluationOptions
): Promise<EvaluationDetails<string>>;
getStringDetails<T extends string = string>(
flagKey: string,
defaultValue: T,
Expand All @@ -86,6 +98,12 @@ export interface Features {
/**
* Get a number flag value.
*/
getNumberValue(
flagKey: string,
defaultValue: number,
context?: EvaluationContext,
options?: FlagEvaluationOptions
): Promise<number>;
getNumberValue<T extends number = number>(
flagKey: string,
defaultValue: T,
Expand All @@ -96,6 +114,12 @@ export interface Features {
/**
* Get a number flag with additional details.
*/
getNumberDetails(
flagKey: string,
defaultValue: number,
context?: EvaluationContext,
options?: FlagEvaluationOptions
): Promise<EvaluationDetails<number>>;
getNumberDetails<T extends number = number>(
flagKey: string,
defaultValue: T,
Expand All @@ -106,6 +130,12 @@ export interface Features {
/**
* Get an object (JSON) flag value.
*/
getObjectValue(
flagKey: string,
defaultValue: JsonValue,
context?: EvaluationContext,
options?: FlagEvaluationOptions
): Promise<JsonValue>;
getObjectValue<T extends JsonValue = JsonValue>(
flagKey: string,
defaultValue: T,
Expand All @@ -122,6 +152,12 @@ export interface Features {
context?: EvaluationContext,
options?: FlagEvaluationOptions
): Promise<EvaluationDetails<JsonValue>>;
getObjectDetails<T extends JsonValue = JsonValue>(
flagKey: string,
defaultValue: T,
context?: EvaluationContext,
options?: FlagEvaluationOptions
): Promise<EvaluationDetails<T>>;
}

/**
Expand Down
14 changes: 7 additions & 7 deletions test/client.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ describe('OpenFeatureClient', () => {
it('should return string, and call string resolver', async () => {
const stringFlag = 'my-string-flag';
const defaultStringValue = 'default-value';
const value = await client.getStringValue(stringFlag, defaultStringValue);
const value: string = await client.getStringValue(stringFlag, defaultStringValue);

expect(value).toEqual(STRING_VALUE);
expect(MOCK_PROVIDER.resolveStringEvaluation).toHaveBeenCalledWith(stringFlag, defaultStringValue, {}, {});
Expand All @@ -127,7 +127,7 @@ describe('OpenFeatureClient', () => {
const stringFlag = 'my-string-flag';
type MyRestrictedString = 'val' | 'other';
const defaultStringValue = 'other';
const value = await client.getStringValue<MyRestrictedString>(stringFlag, defaultStringValue);
const value: MyRestrictedString = await client.getStringValue<MyRestrictedString>(stringFlag, defaultStringValue);

expect(value).toEqual(STRING_VALUE);
expect(MOCK_PROVIDER.resolveStringEvaluation).toHaveBeenCalledWith(stringFlag, defaultStringValue, {}, {});
Expand All @@ -140,7 +140,7 @@ describe('OpenFeatureClient', () => {
it('should return number, and call number resolver', async () => {
const numberFlag = 'my-number-flag';
const defaultNumberValue = 1970;
const value = await client.getNumberValue(numberFlag, defaultNumberValue);
const value: number = await client.getNumberValue(numberFlag, defaultNumberValue);

expect(value).toEqual(NUMBER_VALUE);
expect(MOCK_PROVIDER.resolveNumberEvaluation).toHaveBeenCalledWith(numberFlag, defaultNumberValue, {}, {});
Expand All @@ -152,7 +152,7 @@ describe('OpenFeatureClient', () => {
const numberFlag = 'my-number-flag';
type MyRestrictedNumber = 4096 | 2048;
const defaultNumberValue = 4096;
const value = await client.getNumberValue<MyRestrictedNumber>(numberFlag, defaultNumberValue);
const value: MyRestrictedNumber = await client.getNumberValue<MyRestrictedNumber>(numberFlag, defaultNumberValue);

expect(value).toEqual(NUMBER_VALUE);
expect(MOCK_PROVIDER.resolveNumberEvaluation).toHaveBeenCalledWith(numberFlag, defaultNumberValue, {}, {});
Expand All @@ -166,7 +166,7 @@ describe('OpenFeatureClient', () => {
it('should return JsonValue, and call object resolver', async () => {
const objectFlag = 'my-object-flag';
const defaultObjectFlag = {};
const value = await client.getObjectValue(objectFlag, defaultObjectFlag);
const value: JsonValue = await client.getObjectValue(objectFlag, defaultObjectFlag);

// compare the object
expect(value).toEqual(OBJECT_VALUE);
Expand Down Expand Up @@ -197,12 +197,12 @@ describe('OpenFeatureClient', () => {
}
}

const defaultMyTYpeFlag: MyType = {
const defaultMyTypeFlag: MyType = {
inner: {
booleanKey: false
}
};
const value = await client.getObjectValue<MyType>(objectFlag, defaultMyTYpeFlag);
const value: MyType = await client.getObjectValue<MyType>(objectFlag, defaultMyTypeFlag);

const innerBooleanValue: boolean = value.inner.booleanKey;
expect(innerBooleanValue).toBeTruthy();
Expand Down

0 comments on commit 715d9a9

Please sign in to comment.