all files / firebase/modules/config/ index.js

87.88% Statements 29/33
80.77% Branches 21/26
90.91% Functions 10/11
87.88% Lines 29/33
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185                                                                                    10×     10×                                                                                                                                                                                                                                
/**
 * @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,
  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,
      namespace: NAMESPACE,
    });
    this._developerModeEnabled = false;
  }
 
  /**
   * Converts a native map to single JS value
   * @param nativeValue
   * @returns {*}
   * @private
   */
  _nativeValueToJS(nativeValue: 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;
        Iif (
          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() {
    Eif (!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>}:
   */
  fetch(expiration?: number) {
    Eif (expiration !== undefined) {
      getLogger(this).debug(
        `Fetching remote config data with expiration ${expiration.toString()}`
      );
      return getNativeModule(this).fetchWithExpirationDuration(expiration);
    }
    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)
   *  }
   */
  getValue(key: string) {
    return 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: Array<string>) {
    return getNativeModule(this)
      .getValues(keys || [])
      .then(nativeValues => {
        const values: { [string]: Object } = {};
        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?: 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
   */
  setDefaultsFromResource(resource: string | number) {
    getNativeModule(this).setDefaultsFromResource(resource);
  }
}
 
export const statics = {};