sunmi-device-sdk
Version:
JavaScript SDK for Sunmi card readers and printers
119 lines (118 loc) • 3.65 kB
JavaScript
;
/**
* Native Bridge
* Handles communication with native platform code
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.getNativeBridge = getNativeBridge;
exports.setNativeBridge = setNativeBridge;
class CordovaBridge {
execute(plugin, method, args) {
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, method, args, callback) {
if (typeof cordova !== 'undefined') {
cordova.exec(callback, callback, plugin, method, args);
}
else {
throw new Error('Cordova is not available');
}
}
}
class ReactNativeBridge {
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, method, args) {
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, method, args, callback) {
const module = this.nativeModule[plugin];
if (!module || !module[method]) {
throw new Error(`Method ${method} not found in ${plugin}`);
}
module[method](...args, callback);
}
}
class CapacitorBridge {
constructor() {
try {
const { Plugins } = require('@capacitor/core');
this.plugins = Plugins;
}
catch (e) {
throw new Error('Capacitor is not available');
}
}
execute(plugin, method, args) {
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, method, args, callback) {
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 = null;
/**
* Get the appropriate native bridge for the current platform
*/
function getNativeBridge() {
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
*/
function setNativeBridge(bridge) {
bridgeInstance = bridge;
}