-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: variation calls to fallback to ldClient.
- Loading branch information
Showing
2 changed files
with
34 additions
and
56 deletions.
There are no files selected for viewing
88 changes: 33 additions & 55 deletions
88
packages/sdk/react-native/src/hooks/variation/useTypedVariation.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,76 +1,54 @@ | ||
import { useMemo } from 'react'; | ||
|
||
import { LDEvaluationDetailTyped } from '@launchdarkly/js-client-sdk-common'; | ||
|
||
import useLDClient from '../useLDClient'; | ||
import useLDDataSourceStatus from '../useLDDataSourceStatus'; | ||
|
||
export const useTypedVariation = <T extends boolean | number | string | unknown>( | ||
flagKey: string, | ||
defaultValue: T, | ||
): T => { | ||
const ldClient = useLDClient(); | ||
const { status } = useLDDataSourceStatus(); | ||
|
||
if (status === 'ready') { | ||
switch (typeof defaultValue) { | ||
case 'boolean': | ||
return ldClient.boolVariation(flagKey, defaultValue as boolean) as T; | ||
case 'number': | ||
return ldClient.numberVariation(flagKey, defaultValue as number) as T; | ||
case 'string': | ||
return ldClient.stringVariation(flagKey, defaultValue as string) as T; | ||
case 'undefined': | ||
case 'object': | ||
return ldClient.jsonVariation(flagKey, defaultValue) as T; | ||
default: | ||
return defaultValue; | ||
} | ||
switch (typeof defaultValue) { | ||
case 'boolean': | ||
return ldClient.boolVariation(flagKey, defaultValue as boolean) as T; | ||
case 'number': | ||
return ldClient.numberVariation(flagKey, defaultValue as number) as T; | ||
case 'string': | ||
return ldClient.stringVariation(flagKey, defaultValue as string) as T; | ||
case 'undefined': | ||
case 'object': | ||
return ldClient.jsonVariation(flagKey, defaultValue) as T; | ||
default: | ||
return ldClient.variation(flagKey, defaultValue); | ||
} | ||
|
||
return defaultValue; | ||
}; | ||
|
||
export const useTypedVariationDetail = <T extends boolean | number | string | unknown>( | ||
flagKey: string, | ||
defaultValue: T, | ||
): LDEvaluationDetailTyped<T> => { | ||
const ldClient = useLDClient(); | ||
const { status } = useLDDataSourceStatus(); | ||
const unsupportedType = useMemo( | ||
() => ({ | ||
value: defaultValue, | ||
variationIndex: null, | ||
reason: { kind: 'ERROR', errorKind: 'UNSUPPORTED_VARIATION_TYPE' }, | ||
}), | ||
[defaultValue], | ||
); | ||
|
||
if (status === 'ready') { | ||
switch (typeof defaultValue) { | ||
case 'boolean': | ||
return ldClient.boolVariationDetail( | ||
flagKey, | ||
defaultValue as boolean, | ||
) as LDEvaluationDetailTyped<T>; | ||
case 'number': | ||
return ldClient.numberVariationDetail( | ||
flagKey, | ||
defaultValue as number, | ||
) as LDEvaluationDetailTyped<T>; | ||
case 'string': | ||
return ldClient.stringVariationDetail( | ||
flagKey, | ||
defaultValue as string, | ||
) as LDEvaluationDetailTyped<T>; | ||
case 'undefined': | ||
case 'object': | ||
return ldClient.jsonVariationDetail(flagKey, defaultValue) as LDEvaluationDetailTyped<T>; | ||
default: { | ||
return unsupportedType; | ||
} | ||
} | ||
switch (typeof defaultValue) { | ||
case 'boolean': | ||
return ldClient.boolVariationDetail( | ||
flagKey, | ||
defaultValue as boolean, | ||
) as LDEvaluationDetailTyped<T>; | ||
case 'number': | ||
return ldClient.numberVariationDetail( | ||
flagKey, | ||
defaultValue as number, | ||
) as LDEvaluationDetailTyped<T>; | ||
case 'string': | ||
return ldClient.stringVariationDetail( | ||
flagKey, | ||
defaultValue as string, | ||
) as LDEvaluationDetailTyped<T>; | ||
case 'undefined': | ||
case 'object': | ||
return ldClient.jsonVariationDetail(flagKey, defaultValue) as LDEvaluationDetailTyped<T>; | ||
default: | ||
return ldClient.variationDetail(flagKey, defaultValue); | ||
} | ||
|
||
return unsupportedType; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters