UNPKG

capacitor-firebase-kit

Version:

Provider-less Firebase Kit - Universal Firebase services integration for React, React Native, and Capacitor apps

149 lines 6.82 kB
import { PlatformAdapter } from '../core/platform-adapter'; export class CapacitorAdapter extends PlatformAdapter { constructor() { super(...arguments); this.capacitorPlugin = null; } async initialize(config) { this.validateConfig(config); this.config = config; try { // Dynamically import Capacitor core const { registerPlugin } = await import('@capacitor/core'); // Import and register the plugin this.capacitorPlugin = registerPlugin('CapacitorFirebaseKit', { web: async () => { const { FirebaseKitPluginImplementation } = await import('../plugin-implementation'); return new FirebaseKitPluginImplementation(); }, }); } catch (_a) { throw new Error('Capacitor not found. Please install @capacitor/core to use Capacitor adapter.'); } } async getService(serviceName) { if (!this.isSupported(serviceName)) { throw new Error(`Service ${serviceName} is not supported on Capacitor platform`); } if (this.serviceCache.has(serviceName)) { return this.serviceCache.get(serviceName); } const service = await this.loadServiceModule(serviceName); this.serviceCache.set(serviceName, service); return service; } async loadServiceModule(serviceName) { if (!this.capacitorPlugin) { throw new Error('Capacitor plugin not initialized'); } // Create service wrapper that delegates to the Capacitor plugin switch (serviceName) { case 'analytics': return this.createAnalyticsService(); case 'appCheck': return this.createAppCheckService(); case 'adMob': return this.createAdMobService(); case 'crashlytics': return this.createCrashlyticsService(); case 'performance': return this.createPerformanceService(); case 'remoteConfig': return this.createRemoteConfigService(); default: throw new Error(`Unknown service: ${serviceName}`); } } createAnalyticsService() { const plugin = this.capacitorPlugin; return { logEvent: (eventName, eventParams) => plugin.analyticsLogEvent({ eventName, eventParams }), setUserId: (userId) => plugin.analyticsSetUserId({ userId }), setUserProperties: (properties) => plugin.analyticsSetUserProperties({ properties }), setCurrentScreen: (screenName, screenClassOverride) => plugin.analyticsSetCurrentScreen({ screenName, screenClassOverride }), setEnabled: (enabled) => plugin.analyticsSetEnabled({ enabled }), isSupported: () => plugin.analyticsIsSupported(), }; } createAppCheckService() { const plugin = this.capacitorPlugin; return { initialize: (options) => plugin.appCheckInitialize(options), getToken: (forceRefresh) => plugin.appCheckGetToken({ forceRefresh }), setTokenAutoRefreshEnabled: (enabled) => plugin.appCheckSetTokenAutoRefreshEnabled({ enabled }), }; } createAdMobService() { const plugin = this.capacitorPlugin; return { initialize: (options) => plugin.adMobInitialize(options || {}), showBanner: (options) => plugin.adMobShowBanner(options), hideBanner: () => plugin.adMobHideBanner(), resumeBanner: () => plugin.adMobResumeBanner(), removeBanner: () => plugin.adMobRemoveBanner(), prepareInterstitial: (options) => plugin.adMobPrepareInterstitial(options), showInterstitial: () => plugin.adMobShowInterstitial(), prepareRewardVideoAd: (options) => plugin.adMobPrepareRewardVideoAd(options), showRewardVideoAd: () => plugin.adMobShowRewardVideoAd(), setApplicationMuted: (muted) => plugin.adMobSetApplicationMuted({ muted }), setApplicationVolume: (volume) => plugin.adMobSetApplicationVolume({ volume }), }; } createCrashlyticsService() { const plugin = this.capacitorPlugin; return { crash: () => plugin.crashlyticsCrash(), setUserId: (userId) => plugin.crashlyticsSetUserId({ userId }), log: (message) => plugin.crashlyticsLog({ message }), setEnabled: (enabled) => plugin.crashlyticsSetEnabled({ enabled }), isEnabled: () => plugin.crashlyticsIsEnabled(), recordException: (error) => plugin.crashlyticsRecordException({ message: error.message || 'Unknown error', stacktrace: error.stack || '', }), setCustomKey: (key, value) => plugin.crashlyticsSetCustomKey({ key, value }), setCustomKeys: (customKeys) => plugin.crashlyticsSetCustomKeys({ customKeys }), }; } createPerformanceService() { const plugin = this.capacitorPlugin; return { startTrace: (traceName) => plugin.performanceStartTrace({ traceName }), stopTrace: (traceName) => plugin.performanceStopTrace({ traceName }), incrementMetric: (traceName, metricName, value) => plugin.performanceIncrementMetric({ traceName, metricName, value }), setEnabled: (enabled) => plugin.performanceSetEnabled({ enabled }), isEnabled: () => plugin.performanceIsEnabled(), }; } createRemoteConfigService() { const plugin = this.capacitorPlugin; return { initialize: (options) => plugin.remoteConfigInitialize(options), fetchAndActivate: () => plugin.remoteConfigFetchAndActivate(), fetchConfig: () => plugin.remoteConfigFetchConfig(), activate: () => plugin.remoteConfigActivate(), getValue: (key) => plugin.remoteConfigGetValue({ key }), getString: (key) => plugin.remoteConfigGetString({ key }), getNumber: (key) => plugin.remoteConfigGetNumber({ key }), getBoolean: (key) => plugin.remoteConfigGetBoolean({ key }), setLogLevel: (logLevel) => plugin.remoteConfigSetLogLevel({ logLevel }), }; } isSupported(serviceName) { const supportedServices = [ 'analytics', 'appCheck', 'adMob', 'crashlytics', 'performance', 'remoteConfig', ]; return supportedServices.includes(serviceName); } async cleanup() { this.serviceCache.clear(); this.capacitorPlugin = null; } } //# sourceMappingURL=capacitor-adapter.js.map