Files
react-native-firebase/packages/remote-config/lib/index.js
Russell Wheatley 256b6a47e5 feat(remote-config)!: API update to match web SDK (#3537)
* feat(remote-config): API update to match web SDK

* feat(remote-config): reset API for android

* tests(remote-config): update assertions

* tests(remote-config): update to match api

* chore(remote-config): update

* chore(remote-config): update tests/API

* tests(remote-config): update tests

* feat(remtoe-config): PR comment updates

* Apply suggestions from code review

Co-authored-by: Mike Diarmid <mike.diarmid@gmail.com>

* chore(remote-config): Pr comment updates

* fix(remote-config): revert back mocha config

* Apply suggestions from code review

Co-authored-by: Mike Diarmid <mike.diarmid@gmail.com>

* chore(remote-config): rename array

* chore(remote-config): PR updates

* chore(remote-config): lint disable

* chore(remote-config): update types

* docs(remote-config): update docs

* docs(remote-config): note

* docs(remote-config): update

* docs(remote-config): further updates

* fix(ml-vision): cancel tests

* docs(*): RNFB not part of spelling list

* chore(*) update spelling list

* docs(*): update ts.json docs

* docs(*): update ts docs

* Apply suggestions from code review

Co-authored-by: Mike Diarmid <mike.diarmid@gmail.com>

* chore(remote-config): release updates

* chore(remote-config): types

* chore(remote-config): 'fetchTimeMillis' api

* docs(remote-config): update docs

* chore(remote-config): api update

* Update packages/remote-config/type-test.ts

* chore(remote-config): further changes

Co-authored-by: Mike Diarmid <mike.diarmid@gmail.com>
2020-07-16 12:32:58 +01:00

313 lines
8.7 KiB
JavaScript

/*
* Copyright (c) 2016-present Invertase Limited & Contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this library except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
import {
hasOwnProperty,
isNumber,
isObject,
isString,
isUndefined,
isIOS,
} from '@react-native-firebase/app/lib/common';
import Value from './RemoteConfigValue';
import {
createModuleNamespace,
FirebaseModule,
getFirebaseRoot,
} from '@react-native-firebase/app/lib/internal';
import version from './version';
const statics = {
LastFetchStatus: {
SUCCESS: 'success',
FAILURE: 'failure',
THROTTLED: 'throttled',
NO_FETCH_YET: 'no_fetch_yet',
},
ValueSource: {
REMOTE: 'remote',
DEFAULT: 'default',
STATIC: 'static',
},
};
const namespace = 'remoteConfig';
const nativeModuleName = 'RNFBConfigModule';
class FirebaseConfigModule extends FirebaseModule {
constructor(...args) {
super(...args);
this._settings = {
// defaults to 1 minute.
fetchTimeMillis: 60000,
// defaults to 12 hours.
minimumFetchIntervalMillis: 43200000,
};
this._lastFetchTime = -1;
}
getValue(key) {
if (!isString(key)) {
throw new Error("firebase.remoteConfig().getValue(): 'key' must be a string value.");
}
if (!hasOwnProperty(this._values, key)) {
return new Value({
value: '',
source: 'static',
});
}
return new Value({ value: `${this._values[key].value}`, source: this._values[key].source });
}
getBoolean(key) {
return this.getValue(key).asBoolean();
}
getNumber(key) {
return this.getValue(key).asNumber();
}
getString(key) {
return this.getValue(key).asString();
}
getAll() {
const values = {};
Object.keys(this._values).forEach(key => (values[key] = this.getValue(key)));
return values;
}
get defaultConfig() {
// eslint-disable-next-line no-console
console.warn(
'firebase.remoteConfig().defaultConfig is not supported. Default values are merged with config values',
);
}
set defaultConfig(defaults) {
// eslint-disable-next-line no-console
console.warn(
'firebase.remoteConfig().defaultConfig is not supported. Please use firebase.remoteConfig().setDefaults({ [key] : value }) to set default values',
);
}
get settings() {
return this._settings;
}
set settings(settings) {
// eslint-disable-next-line no-console
console.warn(
"firebase.remoteConfig().settings = { [key]: string }; is not supported. Please use 'firebase.remoteConfig().setConfigSettings({ ...[key]: string, })' instead'",
);
}
get fetchTimeMillis() {
// android returns -1 if no fetch yet and iOS returns 0
return this._lastFetchTime;
}
get lastFetchStatus() {
return this._lastFetchStatus;
}
get isDeveloperModeEnabled() {
// eslint-disable-next-line no-console
console.warn(
'firebase.remoteConfig().isDeveloperModeEnabled has now been removed. Please consider setting `settings.minimumFetchIntervalMillis` in remoteConfig.Settings',
);
}
get minimumFetchInterval() {
// eslint-disable-next-line no-console
console.warn(
'firebase.remoteConfig().minimumFetchInterval has been removed. Use `firebase.remoteConfig().settings.minimumFetchIntervalMillis` instead.',
);
}
/**
* Deletes all activated, fetched and defaults configs and resets all Firebase Remote Config settings.
* @returns {Promise<null>}
*/
reset() {
if (isIOS) {
return Promise.resolve(null);
}
return this._promiseWithConstants(this.native.reset());
}
setConfigSettings(settings) {
const nativeSettings = {
//iOS & Android expect seconds
fetchTimeout: this._settings.fetchTimeMillis / 1000,
minimumFetchInterval: this._settings.minimumFetchIntervalMillis / 1000,
};
if (!isObject(settings)) {
throw new Error('firebase.remoteConfig().setConfigSettings(*): settings must set an object.');
}
if (hasOwnProperty(settings, 'isDeveloperModeEnabled')) {
// eslint-disable-next-line no-console
console.warn(
"firebase.remoteConfig().setConfigSettings(): 'settings.isDeveloperModeEnabled' has now been removed. Please consider setting 'settings.minimumFetchIntervalMillis'",
);
}
if (hasOwnProperty(settings, 'minimumFetchInterval')) {
// eslint-disable-next-line no-console
console.warn(
"firebase.remoteConfig().setConfigSettings(): 'settings.minimumFetchInterval' has now been removed. Please consider setting 'settings.minimumFetchIntervalMillis'",
);
}
if (hasOwnProperty(settings, 'minimumFetchIntervalMillis')) {
if (!isNumber(settings.minimumFetchIntervalMillis)) {
throw new Error(
"firebase.remoteConfig().setConfigSettings(): 'settings.minimumFetchIntervalMillis' must be a number type in milliseconds.",
);
} else {
nativeSettings.minimumFetchInterval = settings.minimumFetchIntervalMillis / 1000;
}
}
if (hasOwnProperty(settings, 'fetchTimeMillis')) {
if (!isNumber(settings.fetchTimeMillis)) {
throw new Error(
"firebase.remoteConfig().setConfigSettings(): 'settings.fetchTimeMillis' must be a number type in milliseconds.",
);
} else {
nativeSettings.fetchTimeout = settings.fetchTimeMillis / 1000;
}
}
// this._settings = settings;
return this._promiseWithConstants(this.native.setConfigSettings(nativeSettings));
}
/**
* Activates the Fetched RemoteConfig, so that the fetched key-values take effect.
* @returns {Promise<boolean>}
*/
activate() {
return this._promiseWithConstants(this.native.activate());
}
/**
* Fetches parameter values for your app.
* @param {number} expirationDurationSeconds
* @returns {Promise}
*/
fetch(expirationDurationSeconds) {
if (!isUndefined(expirationDurationSeconds) && !isNumber(expirationDurationSeconds)) {
throw new Error(
"firebase.remoteConfig().fetch(): 'expirationDurationSeconds' must be a number value.",
);
}
return this._promiseWithConstants(
this.native.fetch(expirationDurationSeconds !== undefined ? expirationDurationSeconds : -1),
);
}
fetchAndActivate() {
return this._promiseWithConstants(this.native.fetchAndActivate());
}
ensureInitialized() {
return this._promiseWithConstants(this.native.ensureInitialized());
}
/**
* Sets defaults.
*
* @param {object} defaults
*/
setDefaults(defaults) {
if (!isObject(defaults)) {
throw new Error("firebase.remoteConfig().setDefaults(): 'defaults' must be an object.");
}
return this._promiseWithConstants(this.native.setDefaults(defaults));
}
/**
* Sets defaults based on resource.
* @param {string} resourceName
*/
setDefaultsFromResource(resourceName) {
if (!isString(resourceName)) {
throw new Error(
"firebase.remoteConfig().setDefaultsFromResource(): 'resourceName' must be a string value.",
);
}
return this._promiseWithConstants(this.native.setDefaultsFromResource(resourceName));
}
setLogLevel() {
// eslint-disable-next-line no-console
console.warn('firebase.remoteConfig().setLogLevel() is not supported natively.');
}
_updateFromConstants(constants) {
this._lastFetchTime = constants.lastFetchTime;
this._lastFetchStatus = constants.lastFetchStatus;
this._settings = {
fetchTimeMillis: constants.fetchTimeout * 1000,
minimumFetchIntervalMillis: constants.minimumFetchInterval * 1000,
};
this._values = Object.freeze(constants.values);
}
_promiseWithConstants(promise) {
return promise.then(({ result, constants }) => {
this._updateFromConstants(constants);
return result;
});
}
}
// import { SDK_VERSION } from '@react-native-firebase/remote-config';
export const SDK_VERSION = version;
// import config from '@react-native-firebase/remote-config';
// config().X(...);
export default createModuleNamespace({
statics,
version,
namespace,
nativeModuleName,
nativeEvents: false,
hasMultiAppSupport: true,
hasCustomUrlOrRegionSupport: false,
ModuleClass: FirebaseConfigModule,
});
// import config, { firebase } from '@react-native-firebase/remote-config';
// config().X(...);
// firebase.remoteConfig().X(...);
export const firebase = getFirebaseRoot();