UNPKG

@react-native-firebase/app

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

238 lines (223 loc) 8.52 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.deleteApp = deleteApp; exports.getApp = getApp; exports.getApps = getApps; exports.initializeApp = initializeApp; exports.initializeNativeApps = initializeNativeApps; exports.setLogLevel = setLogLevel; exports.setOnAppCreate = setOnAppCreate; exports.setOnAppDestroy = setOnAppDestroy; exports.setReactNativeAsyncStorage = setReactNativeAsyncStorage; var _index = require("../../common/index.js"); var _FirebaseApp = _interopRequireDefault(require("../../FirebaseApp.js")); var _constants = require("../constants.js"); var _asyncStorage = require("../asyncStorage.js"); var _nativeModule = require("./nativeModule.js"); var _logger = require("../logger.js"); function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; } /* * 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. * */ const APP_REGISTRY = {}; let onAppCreateFn = null; let onAppDestroyFn = null; let initializedNativeApps = false; /** * This was needed to avoid metro require cycles... * @param fn */ function setOnAppCreate(fn) { onAppCreateFn = fn; } /** * This was needed to avoid metro require cycles... * @param fn */ function setOnAppDestroy(fn) { onAppDestroyFn = fn; } /** * Initializes all apps that were created natively (android/ios), * e.g. the Default firebase app from plist/json google services file. */ function initializeNativeApps() { const nativeModule = (0, _nativeModule.getAppModule)(); const { NATIVE_FIREBASE_APPS } = nativeModule; if (NATIVE_FIREBASE_APPS && NATIVE_FIREBASE_APPS.length) { for (let i = 0; i < NATIVE_FIREBASE_APPS.length; i++) { const nativeApp = NATIVE_FIREBASE_APPS[i]; if (!nativeApp) continue; const { appConfig, options } = nativeApp; const name = appConfig.name; APP_REGISTRY[name] = new _FirebaseApp.default(options, appConfig, true, deleteApp.bind(null, name, true)); onAppCreateFn?.(APP_REGISTRY[name]); } } initializedNativeApps = true; } /** * Get an app by name; or the default app. * * On first call of this method it will initialize any * natively created apps in JS. This makes this 'lazy load'. * * @param name */ function getApp(name = _constants.DEFAULT_APP_NAME) { (0, _index.warnIfNotModularCall)(arguments, 'getApp()'); if (!initializedNativeApps) { initializeNativeApps(); } const app = APP_REGISTRY[name]; if (!app) { throw new Error(`No Firebase App '${name}' has been created - call firebase.initializeApp()`); } return app; } /** * Gets all app instances, used for `firebase.apps` */ function getApps() { (0, _index.warnIfNotModularCall)(arguments, 'getApps()'); if (!initializedNativeApps) { initializeNativeApps(); } return Object.values(APP_REGISTRY); } /** * * @param options * @param configOrName */ function initializeApp(options = {}, configOrName) { (0, _index.warnIfNotModularCall)(arguments, 'initializeApp()'); let appConfig = configOrName; if (!(0, _index.isObject)(configOrName) || (0, _index.isNull)(configOrName)) { appConfig = { name: configOrName, automaticResourceManagement: false, automaticDataCollectionEnabled: true }; } if ((0, _index.isUndefined)(appConfig.name)) { appConfig.name = _constants.DEFAULT_APP_NAME; } const { name } = appConfig; if (!name || !(0, _index.isString)(name)) { return Promise.reject(new Error(`Illegal App name: '${name}'`)); } if (APP_REGISTRY[name]) { return Promise.reject(new Error(`Firebase App named '${name}' already exists`)); } // VALIDATE OPTIONS if (!(0, _index.isObject)(options)) { return Promise.reject(new Error(`firebase.initializeApp(options, <- expects an Object but got '${typeof options}'`)); } if (!(0, _index.isString)(options.apiKey)) { return Promise.reject(new Error("Missing or invalid FirebaseOptions property 'apiKey'.")); } if (!(0, _index.isString)(options.appId)) { return Promise.reject(new Error("Missing or invalid FirebaseOptions property 'appId'.")); } // TODO - make required only if database module exists - init app on native ios&android needs changing also if (!(0, _index.isString)(options.databaseURL)) { return Promise.reject(new Error("Missing or invalid FirebaseOptions property 'databaseURL'.")); } // TODO - make required only if messaging/notifications module exists - init app on native ios&android needs changing also if (!(0, _index.isString)(options.messagingSenderId)) { return Promise.reject(new Error("Missing or invalid FirebaseOptions property 'messagingSenderId'.")); } if (!(0, _index.isString)(options.projectId)) { return Promise.reject(new Error("Missing or invalid FirebaseOptions property 'projectId'.")); } // TODO - make required only if database module exists - init app on native ios&android needs changing also if (!(0, _index.isString)(options.storageBucket)) { return Promise.reject(new Error("Missing or invalid FirebaseOptions property 'storageBucket'.")); } const app = new _FirebaseApp.default(options, appConfig, false, deleteApp.bind(null, name, true)); // Note these initialization actions with side effects are performed prior to knowledge of // successful initialization in the native code. Native code *may* throw an error. APP_REGISTRY[name] = app; onAppCreateFn?.(APP_REGISTRY[name]); return (0, _nativeModule.getAppModule)().initializeApp(options, appConfig).then(() => { app._initialized = true; return app; }).catch(e => { // we need to clean the app entry from registry as the app does not actually exist // There are still possible side effects from `onAppCreateFn` to consider but as existing // code may rely on that function running prior to native create, re-ordering it is a semantic change // and will be avoided delete APP_REGISTRY[name]; // Now allow calling code to handle the initialization issue throw e; }); } function setLogLevel(logLevel) { (0, _index.warnIfNotModularCall)(arguments, 'setLogLevel()'); if (!['error', 'warn', 'info', 'debug', 'verbose'].includes(logLevel)) { throw new Error('LogLevel must be one of "error", "warn", "info", "debug", "verbose"'); } // This is setting LogLevel for VertexAI which does not wrap around native SDK (0, _logger.setLogLevelInternal)(logLevel); if (_index.isIOS || _index.isOther) { (0, _nativeModule.getAppModule)().setLogLevel(logLevel); } } function setReactNativeAsyncStorage(asyncStorage) { (0, _index.warnIfNotModularCall)(arguments, 'setReactNativeAsyncStorage()'); if (!(0, _index.isObject)(asyncStorage)) { throw new Error("setReactNativeAsyncStorage(*) 'asyncStorage' must be an object."); } if (!(0, _index.isFunction)(asyncStorage.setItem)) { throw new Error("setReactNativeAsyncStorage(*) 'asyncStorage.setItem' must be a function."); } if (!(0, _index.isFunction)(asyncStorage.getItem)) { throw new Error("setReactNativeAsyncStorage(*) 'asyncStorage.getItem' must be a function."); } if (!(0, _index.isFunction)(asyncStorage.removeItem)) { throw new Error("setReactNativeAsyncStorage(*) 'asyncStorage.removeItem' must be a function."); } (0, _asyncStorage.setReactNativeAsyncStorageInternal)(asyncStorage); } /** * */ function deleteApp(name, nativeInitialized) { if (name === _constants.DEFAULT_APP_NAME && nativeInitialized) { return Promise.reject(new Error('Unable to delete the default native firebase app instance.')); } const app = APP_REGISTRY[name]; if (app === undefined) { throw new Error(`Firebase App named '${name}' already deleted`); } const nativeModule = (0, _nativeModule.getAppModule)(); return nativeModule.deleteApp(name).then(() => { app._deleted = true; onAppDestroyFn?.(app); delete APP_REGISTRY[name]; }); } //# sourceMappingURL=app.js.map