@launchdarkly/react-native-client-sdk
Version:
React Native LaunchDarkly SDK
73 lines • 3.2 kB
JavaScript
import useLDClient from '../useLDClient';
/**
* Determines the strongly typed variation of a feature flag.
*
* @param key The unique key of the feature flag.
* @param defaultValue The default value of the flag, to be used if the value is not available
* from LaunchDarkly.
* @returns
* The strongly typed value.
*/
export const useTypedVariation = (key, defaultValue) => {
const ldClient = useLDClient();
switch (typeof defaultValue) {
case 'boolean':
return ldClient.boolVariation(key, defaultValue);
case 'number':
return ldClient.numberVariation(key, defaultValue);
case 'string':
return ldClient.stringVariation(key, defaultValue);
case 'undefined':
case 'object':
return ldClient.jsonVariation(key, defaultValue);
default:
return ldClient.variation(key, defaultValue);
}
};
/**
* Determines the strongly typed variation of a feature flag for a context, along with information about
* how it was calculated.
*
* The `reason` property of the result will also be included in analytics events, if you are
* capturing detailed event data for this flag.
*
* If the flag variation does not have the specified type, defaultValue is returned. The reason will
* indicate an error of the type `WRONG_KIND` in this case.
*
* For more information, see the [SDK reference
* guide](https://docs.launchdarkly.com/sdk/features/evaluation-reasons#react-native).
*
* @param key The unique key of the feature flag.
* @param defaultValue The default value of the flag, to be used if the value is not available
* from LaunchDarkly.
* @returns
* The result (as an {@link LDEvaluationDetailTyped<T>}).
*/
export const useTypedVariationDetail = (key, defaultValue) => {
var _a, _b, _c, _d, _e;
const ldClient = useLDClient();
switch (typeof defaultValue) {
case 'boolean': {
const detail = ldClient.boolVariationDetail(key, defaultValue);
return Object.assign(Object.assign({}, detail), { reason: (_a = detail.reason) !== null && _a !== void 0 ? _a : null });
}
case 'number': {
const detail = ldClient.numberVariationDetail(key, defaultValue);
return Object.assign(Object.assign({}, detail), { reason: (_b = detail.reason) !== null && _b !== void 0 ? _b : null });
}
case 'string': {
const detail = ldClient.stringVariationDetail(key, defaultValue);
return Object.assign(Object.assign({}, detail), { reason: (_c = detail.reason) !== null && _c !== void 0 ? _c : null });
}
case 'undefined':
case 'object': {
const detail = ldClient.jsonVariationDetail(key, defaultValue);
return Object.assign(Object.assign({}, detail), { reason: (_d = detail.reason) !== null && _d !== void 0 ? _d : null });
}
default: {
const detail = ldClient.variationDetail(key, defaultValue);
return Object.assign(Object.assign({}, detail), { reason: (_e = detail.reason) !== null && _e !== void 0 ? _e : null });
}
}
};
//# sourceMappingURL=useTypedVariation.js.map