Skip to content

Commit

Permalink
fix: variation calls to fallback to ldClient.
Browse files Browse the repository at this point in the history
  • Loading branch information
yusinto committed Nov 30, 2023
1 parent 0c70c03 commit 5c76d3f
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 56 deletions.
88 changes: 33 additions & 55 deletions packages/sdk/react-native/src/hooks/variation/useTypedVariation.ts
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;
};
2 changes: 1 addition & 1 deletion packages/shared/sdk-client/src/LDClientImpl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ export default class LDClientImpl implements LDClient {
typeChecker?: (value: any) => [boolean, string],
): LDFlagValue {
if (!this.context) {
this.logger?.warn(ClientMessages.missingContextKeyNoEvent);
this.logger?.debug(ClientMessages.missingContextKeyNoEvent);
return createErrorEvaluationDetail(ErrorKinds.UserNotSpecified, defaultValue);
}

Expand Down

0 comments on commit 5c76d3f

Please sign in to comment.