UNPKG

@orders.co/epson-tm-epos-sdk

Version:

React Native SDK for Epson ePOS-Print TM printers

282 lines (242 loc) 7.84 kB
import { NativeModules, NativeEventEmitter, Platform } from 'react-native'; const { EposPrint } = NativeModules; if (!EposPrint) { throw new Error(`The package 'react-native-epos-print' is not linked properly.`); } // Create event emitter for status change events const eventEmitter = new NativeEventEmitter(EposPrint); class Builder { constructor() { this.commands = []; } async initialize(printerModel = 'TM-m30', printerLanguage = EposPrint.LANG_EN) { try { await EposPrint.initializeBuilder(); return true; } catch (error) { throw error; } } async clear() { try { await EposPrint.clearBuilder(); return true; } catch (error) { throw error; } } async addText(text) { try { await EposPrint.addText(text); return this; } catch (error) { throw error; } } async addTextAlign(align) { try { await EposPrint.addTextAlign(align); return this; } catch (error) { throw error; } } async addTextSize(width, height) { try { await EposPrint.addTextSize(width, height); return this; } catch (error) { throw error; } } async addTextFont(font) { try { await EposPrint.addTextFont(font); return this; } catch (error) { throw error; } } async addTextSmooth(smooth) { try { await EposPrint.addTextSmooth(smooth); return this; } catch (error) { throw error; } } async addFeed() { try { await EposPrint.addFeed(); return this; } catch (error) { throw error; } } async addFeedLine(line) { try { await EposPrint.addFeedLine(line); return this; } catch (error) { throw error; } } async addCut(cutType = EposPrint.CUT_FEED) { try { await EposPrint.addCut(cutType); return this; } catch (error) { throw error; } } async addBarcode(data, symbology, height, width, hri) { try { await EposPrint.addBarcode(data, symbology, height, width, hri); return this; } catch (error) { throw error; } } async addSymbol(data, type, level, width, height) { try { await EposPrint.addSymbol(data, type, level, width, height); return this; } catch (error) { throw error; } } async addImage(base64Image, x, y, width, height) { try { await EposPrint.addImage(base64Image, x, y, width, height); return this; } catch (error) { throw error; } } async beginPageMode(x, y, width, height, direction) { try { await EposPrint.beginPageMode(x, y, width, height, direction); return this; } catch (error) { throw error; } } async endPageMode() { try { await EposPrint.endPageMode(); return this; } catch (error) { throw error; } } } class EposPrintSdk { static async initialize() { try { await EposPrint.initializePrinter(); return true; } catch (error) { throw error; } } static async openPrinter(config) { try { await EposPrint.openPrinter(config); return true; } catch (error) { throw error; } } static async closePrinter() { try { await EposPrint.closePrinter(); return true; } catch (error) { throw error; } } static async getPrinterStatus() { try { const status = await EposPrint.getPrinterStatus(); return status; } catch (error) { throw error; } } static async sendData(builder) { try { const result = await EposPrint.sendData(); return result; } catch (error) { throw error; } } static addStatusListener(callback) { return eventEmitter.addListener('onStatusChange', callback); } static addBatteryStatusListener(callback) { return eventEmitter.addListener('onBatteryStatusChange', callback); } static removeStatusListener(subscription) { subscription.remove(); } static removeBatteryStatusListener(subscription) { subscription.remove(); } static createBuilder() { return new Builder(); } } // Export constants from native module export const DEVTYPE_TCP = EposPrint.DEVTYPE_TCP; export const DEVTYPE_BLUETOOTH = EposPrint.DEVTYPE_BLUETOOTH; export const DEVTYPE_USB = EposPrint.DEVTYPE_USB; export const LANG_EN = EposPrint.LANG_EN; export const LANG_JA = EposPrint.LANG_JA; export const LANG_ZH_CN = EposPrint.LANG_ZH_CN; export const LANG_ZH_TW = EposPrint.LANG_ZH_TW; export const LANG_KO = EposPrint.LANG_KO; export const LANG_TH = EposPrint.LANG_TH; export const LANG_VI = EposPrint.LANG_VI; export const ALIGN_LEFT = EposPrint.ALIGN_LEFT; export const ALIGN_CENTER = EposPrint.ALIGN_CENTER; export const ALIGN_RIGHT = EposPrint.ALIGN_RIGHT; export const FONT_A = EposPrint.FONT_A; export const FONT_B = EposPrint.FONT_B; export const FONT_C = EposPrint.FONT_C; export const CUT_FEED = EposPrint.CUT_FEED; export const CUT_NO_FEED = EposPrint.CUT_NO_FEED; export const BARCODE_UPC_A = EposPrint.BARCODE_UPC_A; export const BARCODE_UPC_E = EposPrint.BARCODE_UPC_E; export const BARCODE_EAN13 = EposPrint.BARCODE_EAN13; export const BARCODE_JAN13 = EposPrint.BARCODE_JAN13; export const BARCODE_EAN8 = EposPrint.BARCODE_EAN8; export const BARCODE_JAN8 = EposPrint.BARCODE_JAN8; export const BARCODE_CODE39 = EposPrint.BARCODE_CODE39; export const BARCODE_ITF = EposPrint.BARCODE_ITF; export const BARCODE_CODABAR = EposPrint.BARCODE_CODABAR; export const BARCODE_CODE93 = EposPrint.BARCODE_CODE93; export const BARCODE_CODE128 = EposPrint.BARCODE_CODE128; export const BARCODE_GS1_128 = EposPrint.BARCODE_GS1_128; export const BARCODE_GS1_DATABAR_OMNIDIRECTIONAL = EposPrint.BARCODE_GS1_DATABAR_OMNIDIRECTIONAL; export const BARCODE_GS1_DATABAR_TRUNCATED = EposPrint.BARCODE_GS1_DATABAR_TRUNCATED; export const BARCODE_GS1_DATABAR_LIMITED = EposPrint.BARCODE_GS1_DATABAR_LIMITED; export const BARCODE_GS1_DATABAR_EXPANDED = EposPrint.BARCODE_GS1_DATABAR_EXPANDED; export const HRI_NONE = EposPrint.HRI_NONE; export const HRI_ABOVE = EposPrint.HRI_ABOVE; export const HRI_BELOW = EposPrint.HRI_BELOW; export const HRI_BOTH = EposPrint.HRI_BOTH; export const SYMBOL_PDF417_STANDARD = EposPrint.SYMBOL_PDF417_STANDARD; export const SYMBOL_PDF417_TRUNCATED = EposPrint.SYMBOL_PDF417_TRUNCATED; export const SYMBOL_QRCODE_MODEL_1 = EposPrint.SYMBOL_QRCODE_MODEL_1; export const SYMBOL_QRCODE_MODEL_2 = EposPrint.SYMBOL_QRCODE_MODEL_2; export const SYMBOL_QRCODE_MICRO = EposPrint.SYMBOL_QRCODE_MICRO; export const LEVEL_L = EposPrint.LEVEL_L; export const LEVEL_M = EposPrint.LEVEL_M; export const LEVEL_Q = EposPrint.LEVEL_Q; export const LEVEL_H = EposPrint.LEVEL_H; export const DIRECTION_LEFT_TO_RIGHT = EposPrint.DIRECTION_LEFT_TO_RIGHT; export const DIRECTION_BOTTOM_TO_TOP = EposPrint.DIRECTION_BOTTOM_TO_TOP; export const DIRECTION_RIGHT_TO_LEFT = EposPrint.DIRECTION_RIGHT_TO_LEFT; export const DIRECTION_TOP_TO_BOTTOM = EposPrint.DIRECTION_TOP_TO_BOTTOM; export default EposPrintSdk;