react-native-purchases-ui
Version:
React Native in-app purchases and subscriptions made easy. Supports iOS and Android.
83 lines (78 loc) • 2.09 kB
JavaScript
/**
* A value type for custom paywall variables that can be passed to paywalls at runtime.
*
* Custom variables allow developers to personalize paywall text with dynamic values.
* Variables are defined in the RevenueCat dashboard and can be overridden at runtime.
*
* Currently only string values are supported. Additional types may be added in the future.
*
* @example
* ```typescript
* RevenueCatUI.presentPaywall({
* customVariables: {
* 'player_name': CustomVariableValue.string('John'),
* 'level': CustomVariableValue.string('42'),
* },
* });
* ```
*
* In the paywall text (configured in the dashboard), use the `custom.` prefix:
* ```
* Hello {{ custom.player_name }}!
* ```
*/
/**
* Factory methods for creating CustomVariableValue instances.
*/
export const CustomVariableValue = {
/**
* Creates a string custom variable value.
* @param value The string value for the custom variable.
* @returns A CustomVariableValue containing the string.
*/
string: value => ({
type: 'string',
value
})
};
/**
* A map of custom variable names to their values.
*/
/**
* Internal type for custom variables as sent to native bridge.
* Currently only string values are supported.
* @internal
*/
/**
* Converts CustomVariables to a string map for native bridge.
* @internal
* @visibleForTesting
*/
export function convertCustomVariablesToStringMap(customVariables) {
if (!customVariables) return null;
const result = {};
for (const key of Object.keys(customVariables)) {
const variable = customVariables[key];
if (variable) {
result[key] = variable.value;
}
}
return result;
}
/**
* Transforms options to native format, converting CustomVariables to string map.
* @internal
* @visibleForTesting
*/
export function transformOptionsForNative(options) {
if (!options) return undefined;
const {
customVariables,
...rest
} = options;
return {
...rest,
customVariables: convertCustomVariablesToStringMap(customVariables)
};
}
//# sourceMappingURL=customVariables.js.map