UNPKG

@nativescript/firebase-core

Version:
442 lines 14.9 kB
import { Application, knownFolders } from '@nativescript/core'; export * from './utils'; export class FirebaseError extends Error { static fromNative(native, message) { const error = new FirebaseError(message || native?.localizedDescription); error._native = native; return error; } get native() { return this._native; } intoNative() { if (!this._native) { const exception = NSException.exceptionWithNameReasonUserInfo(NSGenericException, this.message, null); const info = {}; info['ExceptionName'] = exception.name; info['ExceptionReason'] = exception.reason; info['ExceptionCallStackReturnAddresses'] = exception.callStackReturnAddresses; info['ExceptionCallStackSymbols'] = exception.callStackSymbols; info['ExceptionUserInfo'] = exception.userInfo; const error = NSError.alloc().initWithDomainCodeUserInfo('NativeScript', 1000, info); return error; } return this._native; } } export class FirebaseOptions { static fromNative(native) { if (native instanceof FIRApp) { const opts = new FirebaseOptions(); opts._nativeApp = native; return opts; } return null; } get ios() { return this.native; } get native() { return this._nativeApp?.options; } get name() { return this._nativeApp?.name; } set apiKey(value) { this._apiKey = value; } get apiKey() { if (this._apiKey) { return this._apiKey; } return this.native?.APIKey; } set gcmSenderId(value) { this._gcmSenderId = value; } get gcmSenderId() { if (this._gcmSenderId) { return this._gcmSenderId; } return this.native?.GCMSenderID; } set androidClientId(value) { this._androidClientId = value; } get androidClientId() { if (this._androidClientId) { return this._androidClientId; } return this.native?.androidClientID; } set appGroupId(value) { this._appGroupId = value; } get appGroupId() { if (this._appGroupId) { return this._appGroupId; } return this.native?.appGroupID; } set bundleId(value) { this._bundleId = value; } get bundleId() { if (this._bundleId) { return this._bundleId; } return this.native?.bundleID; } set clientId(value) { this._clientId = value; } get clientId() { if (this._clientId) { return this._clientId; } return this.native?.clientID; } set databaseURL(value) { this._databaseURL = value; } get databaseURL() { if (this._databaseURL) { return this._databaseURL; } return this.native?.databaseURL; } set deepLinkURLScheme(value) { this._deepLinkURLScheme = value; } get deepLinkURLScheme() { if (this._deepLinkURLScheme) { return this._deepLinkURLScheme; } return this.native?.deepLinkURLScheme; } set googleAppId(value) { this._googleAppId = value; } get googleAppId() { if (this._googleAppId) { return this._googleAppId; } return this.native?.googleAppID; } set projectId(value) { this._projectId = value; } get projectId() { if (this._projectId) { return this._projectId; } return this.native?.projectID; } set storageBucket(value) { this._storageBucket = value; } get storageBucket() { if (this._storageBucket) { return this._storageBucket; } return this.native?.storageBucket; } set trackingId(value) { this._trackingId = value; } get trackingId() { if (this._trackingId) { return this._trackingId; } return this.native?.trackingID; } } let firebaseInstance; let defaultApp; const firebaseApps = new Map(); export class FirebaseApp { static fromNative(app) { if (app instanceof FIRApp) { const fb = new FirebaseApp(); fb._native = app; return fb; } return null; } get native() { return this._native; } get ios() { return this.native; } get name() { return this.native.name; } get options() { if (!this._options) { this._options = FirebaseOptions.fromNative(this._native); } return this._options; } delete() { return new Promise((resolve, reject) => { this.native.deleteApp((done) => { if (done) { firebaseApps.delete(this.native.name); resolve(); } else { reject(); } }); }); } get apps() { const apps = []; const keys = FIRApp.allApps.allKeys; const count = keys.count; for (let i = 0; i < count; i++) { const key = keys.objectAtIndex(i); const nativeApp = FIRApp.allApps.objectForKey(key); const app = new FirebaseApp(); app._native = nativeApp; apps.push(app); } return apps; } } const launchQueue = []; const launchCallback = () => { launchQueue.forEach((item) => item()); launchQueue.splice(0); }; TNSFirebaseCore.setOnAppFinishLaunchingCallback(launchCallback); export class Firebase { static addToResumeQueue(callback) { if (typeof callback !== 'function') { return; } Firebase._onResumeQueue.push(callback); } static addToActivityCreatedQueue(callback) { // noop } static get inForeground() { return Firebase._inForeground; } constructor() { if (firebaseInstance) { return firebaseInstance; } firebaseInstance = this; Application.on('launch', (args) => { Firebase._onResumeQueue.forEach((callback) => { callback(); }); Firebase._onResumeQueue.splice(0); }); Application.on('resume', (args) => { Firebase._inForeground = true; Firebase._appDidLaunch = true; }); Application.on('suspend', (args) => { Firebase._inForeground = false; }); return firebaseInstance; } app(name) { if (name) { if (firebaseApps.has(name)) { return firebaseApps.get(name); } return FirebaseApp.fromNative(FIRApp.appNamed(name)); } if (!defaultApp) { defaultApp = FirebaseApp.fromNative(FIRApp.defaultApp()); } return defaultApp; } initializeApp(options = null, configOrName) { return new Promise((resolve, reject) => { const initApp = () => { try { const name = typeof configOrName === 'string' ? configOrName : configOrName?.name; let nativeOptions; if (name) { nativeOptions = FIROptions.alloc().initWithGoogleAppIDGCMSenderID(options.googleAppId, options.gcmSenderId); } if (!nativeOptions && options) { nativeOptions = FIROptions.defaultOptions(); } if (options?.apiKey) { nativeOptions.APIKey = options.apiKey; } if (options?.gcmSenderId) { nativeOptions.GCMSenderID = options.gcmSenderId; } if (options?.androidClientId) { nativeOptions.androidClientID = options.androidClientId; } if (options?.appGroupId) { nativeOptions.appGroupID = options.appGroupId; } if (options?.bundleId) { nativeOptions.bundleID = options.bundleId; } if (options?.clientId) { nativeOptions.clientID = options.clientId; } if (options?.databaseURL) { nativeOptions.databaseURL = options.databaseURL; } if (options?.deepLinkURLScheme) { nativeOptions.deepLinkURLScheme = options.deepLinkURLScheme; } if (options?.googleAppId) { nativeOptions.googleAppID = options.googleAppId; } if (options?.projectId) { nativeOptions.projectID = options.projectId; } if (options?.storageBucket) { nativeOptions.storageBucket = options.storageBucket; } if (options?.trackingId) { nativeOptions.trackingID = options.trackingId; } let app; let isDefault = false; if (name) { FIRApp.configureWithNameOptions(name, nativeOptions); app = FIRApp.appNamed(name); } else { if (defaultApp) { resolve(defaultApp); return; } if (nativeOptions) { FIRApp.configureWithOptions(nativeOptions); } else { FIRApp.configure(); } app = FIRApp.defaultApp(); isDefault = true; } if (app && typeof configOrName === 'object' && typeof configOrName.automaticDataCollectionEnabled === 'boolean') { app.dataCollectionDefaultEnabled = configOrName.automaticDataCollectionEnabled; } const fbApp = FirebaseApp.fromNative(app); if (isDefault) { defaultApp = fbApp; // For backward compat remove @v3 global.__defaultFirebaseApp = fbApp; } if (!isDefault) { firebaseApps.set(name, fbApp); } resolve(fbApp); } catch (e) { reject(new FirebaseError(e.message)); } }; if (!UIApplication.sharedApplication) { launchQueue.push(() => { initApp(); }); } else { initApp(); } }); } initializeAppWithPath(path, options = null, config) { return new Promise((resolve, reject) => { const initApp = () => { try { if (path.startsWith('res://')) { path = NSBundle.mainBundle.pathForResourceOfType(path.replace('res://', '').replace('.plist', ''), 'plist'); } else if (path.startsWith('~/')) { path = knownFolders.currentApp().path + '/' + path.replace('~/', ''); } const nativeOptions = FIROptions.alloc().initWithContentsOfFile(path); if (options?.apiKey) { nativeOptions.APIKey = options.apiKey; } if (options?.gcmSenderId) { nativeOptions.GCMSenderID = options.gcmSenderId; } if (options?.androidClientId) { nativeOptions.androidClientID = options.androidClientId; } if (options?.appGroupId) { nativeOptions.appGroupID = options.appGroupId; } if (options?.bundleId) { nativeOptions.bundleID = options.bundleId; } if (options?.clientId) { nativeOptions.clientID = options.clientId; } if (options?.databaseURL) { nativeOptions.databaseURL = options.databaseURL; } if (options?.deepLinkURLScheme) { nativeOptions.deepLinkURLScheme = options.deepLinkURLScheme; } if (options?.googleAppId) { nativeOptions.googleAppID = options.googleAppId; } if (options?.projectId) { nativeOptions.projectID = options.projectId; } if (options?.storageBucket) { nativeOptions.storageBucket = options.storageBucket; } if (options?.trackingId) { nativeOptions.trackingID = options.trackingId; } FIRApp.configureWithOptions(nativeOptions); const app = FIRApp.defaultApp(); if (app && typeof config === 'object' && typeof config.automaticDataCollectionEnabled === 'boolean') { app.dataCollectionDefaultEnabled = config.automaticDataCollectionEnabled; } const fbApp = FirebaseApp.fromNative(app); if (!defaultApp) { defaultApp = fbApp; } resolve(fbApp); } catch (e) { reject(new FirebaseError(e.message)); } }; if (!UIApplication.sharedApplication) { launchQueue.push(() => { initApp(); }); } else { initApp(); } }); } // For backward compat remove @v3 admob() { return global?.__admob; } } Firebase._onResumeQueue = []; Firebase._inForeground = false; Firebase._appDidLaunch = false; export function firebase() { if (firebaseInstance) { return firebaseInstance; } firebaseInstance = new Firebase(); return firebaseInstance; } //# sourceMappingURL=index.ios.js.map