UNPKG

sunmi-device-sdk

Version:

JavaScript SDK for Sunmi card readers and printers

163 lines (143 loc) 3.94 kB
/** * Native Bridge * Handles communication with native platform code */ // Global type declarations declare const cordova: any; declare const require: any; interface NativeBridge { execute(plugin: string, method: string, args: any[]): Promise<any>; executeWithCallback( plugin: string, method: string, args: any[], callback: Function ): void; } class CordovaBridge implements NativeBridge { execute(plugin: string, method: string, args: any[]): Promise<any> { return new Promise((resolve, reject) => { if (typeof cordova !== 'undefined') { cordova.exec(resolve, reject, plugin, method, args); } else { reject(new Error('Cordova is not available')); } }); } executeWithCallback( plugin: string, method: string, args: any[], callback: Function ): void { if (typeof cordova !== 'undefined') { cordova.exec(callback, callback, plugin, method, args); } else { throw new Error('Cordova is not available'); } } } class ReactNativeBridge implements NativeBridge { private nativeModule: any; constructor() { try { const { NativeModules } = require('react-native'); this.nativeModule = { SunmiCardReader: NativeModules.SunmiCardReader, Printer: NativeModules.SunmiPrinter }; } catch (e) { throw new Error('React Native modules not available'); } } execute(plugin: string, method: string, args: any[]): Promise<any> { const module = this.nativeModule[plugin]; if (!module || !module[method]) { return Promise.reject( new Error(`Method ${method} not found in ${plugin}`) ); } return module[method](...args); } executeWithCallback( plugin: string, method: string, args: any[], callback: Function ): void { const module = this.nativeModule[plugin]; if (!module || !module[method]) { throw new Error(`Method ${method} not found in ${plugin}`); } module[method](...args, callback); } } class CapacitorBridge implements NativeBridge { private plugins: any; constructor() { try { const { Plugins } = require('@capacitor/core'); this.plugins = Plugins; } catch (e) { throw new Error('Capacitor is not available'); } } execute(plugin: string, method: string, args: any[]): Promise<any> { const capacitorPlugin = this.plugins[plugin]; if (!capacitorPlugin || !capacitorPlugin[method]) { return Promise.reject( new Error(`Method ${method} not found in ${plugin}`) ); } return capacitorPlugin[method]({ args }); } executeWithCallback( plugin: string, method: string, args: any[], callback: Function ): void { const capacitorPlugin = this.plugins[plugin]; if (!capacitorPlugin || !capacitorPlugin[method]) { throw new Error(`Method ${method} not found in ${plugin}`); } capacitorPlugin[method]({ args }).then(callback).catch(callback); } } let bridgeInstance: NativeBridge | null = null; /** * Get the appropriate native bridge for the current platform */ export function getNativeBridge(): NativeBridge { if (bridgeInstance) { return bridgeInstance; } // Try Cordova first if (typeof cordova !== 'undefined') { bridgeInstance = new CordovaBridge(); return bridgeInstance; } // Try React Native try { bridgeInstance = new ReactNativeBridge(); return bridgeInstance; } catch (e) { // React Native not available } // Try Capacitor try { bridgeInstance = new CapacitorBridge(); return bridgeInstance; } catch (e) { // Capacitor not available } throw new Error( 'No native bridge available. This package requires Cordova, React Native, or Capacitor.' ); } /** * Set a custom native bridge implementation */ export function setNativeBridge(bridge: NativeBridge): void { bridgeInstance = bridge; }