react-native-firebase/lib/modules/config/index.js

186 lines
5.4 KiB
JavaScript
Raw Normal View History

/**
* @flow
* Remote Config representation wrapper
*/
import { getLogger } from '../../utils/log';
import ModuleBase from '../../utils/ModuleBase';
import { getNativeModule } from '../../utils/native';
import type App from '../core/app';
type NativeValue = {
stringValue?: string,
numberValue?: number,
dataValue?: Object,
boolValue?: boolean,
2018-01-25 18:25:39 +00:00
source:
| 'remoteConfigSourceRemote'
| 'remoteConfigSourceDefault'
| ' remoteConfigSourceStatic',
};
export const MODULE_NAME = 'RNFirebaseRemoteConfig';
export const NAMESPACE = 'config';
/**
* @class Config
*/
export default class RemoteConfig extends ModuleBase {
_developerModeEnabled: boolean;
constructor(app: App) {
super(app, {
moduleName: MODULE_NAME,
multiApp: false,
hasShards: false,
namespace: NAMESPACE,
});
this._developerModeEnabled = false;
}
2017-05-23 13:08:59 +00:00
/**
* Converts a native map to single JS value
* @param nativeValue
* @returns {*}
* @private
*/
_nativeValueToJS(nativeValue: NativeValue) {
2017-05-23 13:08:59 +00:00
return {
source: nativeValue.source,
val() {
2018-01-25 18:25:39 +00:00
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;
2017-05-23 13:08:59 +00:00
return nativeValue.stringValue;
},
};
}
/**
* Enable Remote Config developer mode to allow for frequent refreshes of the cache
*/
enableDeveloperMode() {
if (!this._developerModeEnabled) {
getLogger(this).debug('Enabled developer mode');
getNativeModule(this).enableDeveloperMode();
this._developerModeEnabled = true;
}
}
/**
* Fetches Remote Config data
* Call activateFetched to make fetched data available in app
* @returns {*|Promise.<String>}:
*/
2017-05-23 13:08:59 +00:00
fetch(expiration?: number) {
if (expiration !== undefined) {
2018-01-25 18:25:39 +00:00
getLogger(this).debug(
`Fetching remote config data with expiration ${expiration.toString()}`
);
return getNativeModule(this).fetchWithExpirationDuration(expiration);
2017-05-23 13:08:59 +00:00
}
getLogger(this).debug('Fetching remote config data');
return 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() {
getLogger(this).debug('Activating remote config');
return 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)
* }
*/
2018-02-14 23:17:05 +00:00
getValue(key: string) {
return getNativeModule(this)
2017-05-23 13:08:59 +00:00
.getValue(key || '')
.then(this._nativeValueToJS);
}
/**
* Gets the config value of the default namespace.
2017-05-23 13:08:59 +00:00
* @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)
* }
*/
2018-03-09 04:16:40 +00:00
getValues(keys: Array<string>) {
return getNativeModule(this)
2017-05-23 13:08:59 +00:00
.getValues(keys || [])
2018-01-25 18:25:39 +00:00
.then(nativeValues => {
2018-03-09 04:16:40 +00:00
const values: { [string]: Object } = {};
2017-05-23 13:08:59 +00:00
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>>}
*/
2018-02-14 23:17:05 +00:00
getKeysByPrefix(prefix?: string) {
return 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: Object) {
getNativeModule(this).setDefaults(defaults);
}
/**
* Sets default configs from plist for default namespace;
* @param resource: The plist file name or resource ID
*/
2018-02-14 23:17:05 +00:00
setDefaultsFromResource(resource: string | number) {
getNativeModule(this).setDefaultsFromResource(resource);
}
}
2017-11-23 17:29:40 +00:00
export const statics = {};