UNPKG

react-native-firebase-compiled

Version:

A well tested, feature rich Firebase implementation for React Native, supporting iOS & Android. Individual module support for Admob, Analytics, Auth, Crash Reporting, Cloud Firestore, Database, Dynamic Links, Functions, Messaging (FCM), Remote Config, Sto

178 lines (143 loc) 5.62 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.statics = exports.default = exports.NAMESPACE = exports.MODULE_NAME = void 0; var _log = require("../../utils/log"); var _ModuleBase = _interopRequireDefault(require("../../utils/ModuleBase")); var _native = require("../../utils/native"); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } const MODULE_NAME = 'RNFirebaseRemoteConfig'; exports.MODULE_NAME = MODULE_NAME; const NAMESPACE = 'config'; exports.NAMESPACE = NAMESPACE; /** * @class Config */ class RemoteConfig extends _ModuleBase.default { constructor(app) { super(app, { moduleName: MODULE_NAME, hasMultiAppSupport: false, hasCustomUrlSupport: false, namespace: NAMESPACE }); _defineProperty(this, "_developerModeEnabled", void 0); this._developerModeEnabled = false; } /** * Converts a native map to single JS value * @param nativeValue * @returns {*} * @private */ _nativeValueToJS(nativeValue) { return { source: nativeValue.source, val() { if (nativeValue.boolValue !== null && (nativeValue.stringValue === 'true' || nativeValue.stringValue === 'false' || nativeValue.stringValue === null)) return nativeValue.boolValue; if (nativeValue.numberValue !== null && nativeValue.numberValue !== undefined && (nativeValue.stringValue == null || nativeValue.stringValue === '' || nativeValue.numberValue.toString() === nativeValue.stringValue)) return nativeValue.numberValue; if (nativeValue.dataValue !== nativeValue.stringValue && (nativeValue.stringValue == null || nativeValue.stringValue === '')) return nativeValue.dataValue; return nativeValue.stringValue; } }; } /** * Enable Remote Config developer mode to allow for frequent refreshes of the cache */ enableDeveloperMode() { if (!this._developerModeEnabled) { (0, _log.getLogger)(this).debug('Enabled developer mode'); (0, _native.getNativeModule)(this).enableDeveloperMode(); this._developerModeEnabled = true; } } /** * Fetches Remote Config data * Call activateFetched to make fetched data available in app * @returns {*|Promise.<String>}: */ fetch(expiration) { if (expiration !== undefined) { (0, _log.getLogger)(this).debug(`Fetching remote config data with expiration ${expiration.toString()}`); return (0, _native.getNativeModule)(this).fetchWithExpirationDuration(expiration); } (0, _log.getLogger)(this).debug('Fetching remote config data'); return (0, _native.getNativeModule)(this).fetch(); } /** * Applies Fetched Config data to the Active Config * @returns {*|Promise.<Bool>} * resolves if there was a Fetched Config, and it was activated, * rejects if no Fetched Config was found, or the Fetched Config was already activated. */ activateFetched() { (0, _log.getLogger)(this).debug('Activating remote config'); return (0, _native.getNativeModule)(this).activateFetched(); } /** * Gets the config value of the default namespace. * @param key: Config key * @returns {*|Promise.<Object>}, will always resolve * Object looks like * { * "stringValue" : stringValue, * "numberValue" : numberValue, * "dataValue" : dataValue, * "boolValue" : boolValue, * "source" : OneOf<String>(remoteConfigSourceRemote|remoteConfigSourceDefault|remoteConfigSourceStatic) * } */ getValue(key) { return (0, _native.getNativeModule)(this).getValue(key || '').then(this._nativeValueToJS); } /** * Gets the config value of the default namespace. * @param keys: Config key * @returns {*|Promise.<Object>}, will always resolve. * Result will be a dictionary of key and config objects * Object looks like * { * "stringValue" : stringValue, * "numberValue" : numberValue, * "dataValue" : dataValue, * "boolValue" : boolValue, * "source" : OneOf<String>(remoteConfigSourceRemote|remoteConfigSourceDefault|remoteConfigSourceStatic) * } */ getValues(keys) { return (0, _native.getNativeModule)(this).getValues(keys || []).then(nativeValues => { const values = {}; for (let i = 0, len = keys.length; i < len; i++) { values[keys[i]] = this._nativeValueToJS(nativeValues[i]); } return values; }); } /** * Get the set of parameter keys that start with the given prefix, from the default namespace * @param prefix: The key prefix to look for. If prefix is nil or empty, returns all the keys. * @returns {*|Promise.<Array<String>>} */ getKeysByPrefix(prefix) { return (0, _native.getNativeModule)(this).getKeysByPrefix(prefix); } /** * Sets config defaults for parameter keys and values in the default namespace config. * @param defaults: A dictionary mapping a String key to a Object values. */ setDefaults(defaults) { (0, _native.getNativeModule)(this).setDefaults(defaults); } /** * Sets default configs from plist for default namespace; * @param resource: The plist file name or resource ID */ setDefaultsFromResource(resource) { (0, _native.getNativeModule)(this).setDefaultsFromResource(resource); } } exports.default = RemoteConfig; const statics = {}; exports.statics = statics;