react-native-esc-pos-printer
Version:
An unofficial React Native library for printing on an EPSON TM printer with the Epson ePOS SDK for iOS and Epson ePOS SDK for Android
321 lines (319 loc) • 10.5 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.PrinterWrapper = void 0;
var _reactNative = require("react-native");
var _index = require("./constants/index.js");
var _index2 = require("./utils/index.js");
var _index3 = require("../specs/index.js");
class PrinterWrapper {
printerPaperWidth = null;
currentFontWidth = 1;
constructor(target) {
this.target = target;
}
init = async ({
deviceName,
lang
}) => {
try {
await _index3.EscPosPrinter.initWithPrinterDeviceName(this.target, deviceName, lang);
this.currentFontWidth = 1;
} catch (error) {
(0, _index2.throwProcessedError)({
methodName: 'init',
errorCode: error.message,
messagesMapping: _index.InitPrinterErrorMessageMapping
});
}
};
connect = async (timeout = 15000) => {
try {
await _index3.EscPosPrinter.connect(this.target, timeout);
} catch (error) {
(0, _index2.throwProcessedError)({
methodName: 'connect',
errorCode: error.message,
messagesMapping: _index.ConnectPrinterErrorMessageMapping
});
}
};
disconnect = async () => {
try {
await _index3.EscPosPrinter.disconnect(this.target);
} catch (error) {
(0, _index2.throwProcessedError)({
methodName: 'disconnect',
errorCode: error.message,
messagesMapping: _index.DisconnectPrinterErrorMessageMapping
});
}
};
/**
* Forcefully Clears the command buffer of the printer
* Caution ☢️: Only use this method if disconnecting the printer is not an option.
*
* Disconnecting will automatically clear the command buffer.
*/
clearCommandBuffer = async () => {
try {
await _index3.EscPosPrinter.clearCommandBuffer(this.target);
} catch (error) {
(0, _index2.throwProcessedError)({
methodName: 'clearCommandBuffer',
errorCode: error.message,
messagesMapping: _index.CommonOperationErrorMessageMapping
});
}
};
addText = async data => {
try {
await _index3.EscPosPrinter.addText(this.target, data);
} catch (error) {
(0, _index2.throwProcessedError)({
methodName: 'addText',
errorCode: error.message,
messagesMapping: _index.CommonOperationErrorMessageMapping
});
}
};
addTextLang = async lang => {
try {
await _index3.EscPosPrinter.addTextLang(this.target, lang);
} catch (error) {
(0, _index2.throwProcessedError)({
methodName: 'addTextLang',
errorCode: error.message,
messagesMapping: _index.CommonOperationErrorMessageMapping
});
}
};
addFeedLine = async (line = 1) => {
try {
await _index3.EscPosPrinter.addFeedLine(this.target, line);
} catch (error) {
(0, _index2.throwProcessedError)({
methodName: 'addFeedLine',
errorCode: error.message,
messagesMapping: _index.CommonOperationErrorMessageMapping
});
}
};
addLineSpace = async linespc => {
try {
await _index3.EscPosPrinter.addLineSpace(this.target, linespc);
} catch (error) {
(0, _index2.throwProcessedError)({
methodName: 'addLineSpace',
errorCode: error.message,
messagesMapping: _index.CommonOperationErrorMessageMapping
});
}
};
addCut = async (type = _index.PrinterConstants.PARAM_DEFAULT) => {
try {
await _index3.EscPosPrinter.addCut(this.target, type);
} catch (error) {
(0, _index2.throwProcessedError)({
methodName: 'addCut',
errorCode: error.message,
messagesMapping: _index.CommonOperationErrorMessageMapping
});
}
};
sendData = async (timeout = 5000) => {
try {
const result = await _index3.EscPosPrinter.sendData(this.target, timeout);
return (0, _index2.parsePrinterStatus)(result);
} catch (error) {
const {
errorType,
data
} = (0, _index2.processComplextError)(error.message);
(0, _index2.throwProcessedError)({
methodName: 'sendData',
errorCode: data,
messagesMapping: errorType === 'code' ? _index.PrintErrorCodeMessageMapping : _index.SendDataPrinterErrorMessageMapping,
statusMapping: errorType === 'code' ? _index.PrinterErrorCodeStatusMapping : _index.PrinterErrorStatusMapping
});
}
};
getPrinterSetting = async (type, timeout = 10000) => {
try {
const isPrinterPaperWidthRequested = type === _index.PrinterGetSettingsType.PRINTER_SETTING_PAPERWIDTH;
if (isPrinterPaperWidthRequested && this.printerPaperWidth) {
return (0, _index2.parsePrinterSettings)({
type,
value: this.printerPaperWidth
});
}
const result = await _index3.EscPosPrinter.getPrinterSetting(this.target, timeout, type);
if (isPrinterPaperWidthRequested) {
this.printerPaperWidth = result.value;
}
return (0, _index2.parsePrinterSettings)(result);
} catch (error) {
const {
errorType,
data
} = (0, _index2.processComplextError)(error.message);
(0, _index2.throwProcessedError)({
methodName: 'getPrinterSetting',
errorCode: data,
messagesMapping: errorType === 'code' ? _index.PrintErrorCodeMessageMapping : _index.CommonOperationErrorMessageMapping,
statusMapping: errorType === 'code' ? _index.PrinterErrorCodeStatusMapping : _index.PrinterErrorStatusMapping
});
}
};
getStatus = async () => {
try {
const result = await _index3.EscPosPrinter.getStatus(this.target);
return (0, _index2.parsePrinterStatus)(result);
} catch (error) {
(0, _index2.throwProcessedError)({
methodName: 'getStatus',
errorCode: error.message,
messagesMapping: _index.CommonOperationErrorMessageMapping
});
}
};
addImage = async ({
source,
width,
color = _index.PrinterConstants.PARAM_DEFAULT,
mode = _index.PrinterConstants.PARAM_DEFAULT,
halftone = _index.PrinterConstants.PARAM_DEFAULT,
brightness = _index.PrinterConstants.PARAM_DEFAULT,
compress = _index.PrinterConstants.PARAM_DEFAULT
}) => {
try {
const resolvedSource = _reactNative.Image.resolveAssetSource(source);
await _index3.EscPosPrinter.addImage(this.target, resolvedSource, width, color, mode, halftone, brightness, compress);
} catch (error) {
(0, _index2.throwProcessedError)({
methodName: 'addImage',
errorCode: error.message,
messagesMapping: _index.CommonOperationErrorMessageMapping
});
}
};
addBarcode = async ({
data,
type,
hri = _index.PrinterConstants.PARAM_DEFAULT,
font = _index.PrinterConstants.PARAM_DEFAULT,
width = _index.PrinterConstants.PARAM_UNSPECIFIED,
height = _index.PrinterConstants.PARAM_UNSPECIFIED
}) => {
try {
await _index3.EscPosPrinter.addBarcode(this.target, data, type, hri, font, width, height);
} catch (error) {
(0, _index2.throwProcessedError)({
methodName: 'addBarcode',
errorCode: error.message,
messagesMapping: _index.CommonOperationErrorMessageMapping
});
}
};
addSymbol = async ({
data,
type,
level = _index.PrinterConstants.PARAM_DEFAULT,
width,
height,
size
}) => {
try {
await _index3.EscPosPrinter.addSymbol(this.target, data, type, level, width || size, height || size, size);
} catch (error) {
(0, _index2.throwProcessedError)({
methodName: 'addSymbol',
errorCode: error.message,
messagesMapping: _index.CommonOperationErrorMessageMapping
});
}
};
addCommand = async uint8Array => {
try {
const buffer = new _index2.BufferHelper();
const base64String = buffer.bytesToString(uint8Array, 'base64');
await _index3.EscPosPrinter.addCommand(this.target, base64String);
} catch (error) {
(0, _index2.throwProcessedError)({
methodName: 'addCommand',
errorCode: error.message,
messagesMapping: _index.CommonOperationErrorMessageMapping
});
}
};
addPulse = async ({
drawer = _index.PrinterConstants.PARAM_DEFAULT,
time = _index.PrinterConstants.PARAM_DEFAULT
} = {}) => {
try {
await _index3.EscPosPrinter.addPulse(this.target, drawer, time);
} catch (error) {
(0, _index2.throwProcessedError)({
methodName: 'addPulse',
errorCode: error.message,
messagesMapping: _index.CommonOperationErrorMessageMapping
});
}
};
addTextAlign = async (align = _index.PrinterConstants.PARAM_DEFAULT) => {
try {
await _index3.EscPosPrinter.addTextAlign(this.target, align);
} catch (error) {
(0, _index2.throwProcessedError)({
methodName: 'addTextAlign',
errorCode: error.message,
messagesMapping: _index.CommonOperationErrorMessageMapping
});
}
};
addTextSize = async ({
width = _index.PrinterConstants.PARAM_DEFAULT,
height = _index.PrinterConstants.PARAM_DEFAULT
} = {}) => {
try {
await _index3.EscPosPrinter.addTextSize(this.target, width, height);
this.currentFontWidth = width || this.currentFontWidth;
} catch (error) {
(0, _index2.throwProcessedError)({
methodName: 'addTextSize',
errorCode: error.message,
messagesMapping: _index.CommonOperationErrorMessageMapping
});
}
};
addTextSmooth = async (smooth = _index.PrinterConstants.TRUE) => {
try {
await _index3.EscPosPrinter.addTextSmooth(this.target, smooth);
} catch (error) {
(0, _index2.throwProcessedError)({
methodName: 'addTextSmooth',
errorCode: error.message,
messagesMapping: _index.CommonOperationErrorMessageMapping
});
}
};
addTextStyle = async ({
color = _index.PrinterConstants.PARAM_DEFAULT,
em = _index.PrinterConstants.PARAM_DEFAULT,
reverse = _index.PrinterConstants.PARAM_DEFAULT,
ul = _index.PrinterConstants.PARAM_DEFAULT
} = {}) => {
try {
await _index3.EscPosPrinter.addTextStyle(this.target, reverse, ul, em, color);
} catch (error) {
(0, _index2.throwProcessedError)({
methodName: 'addTextStyle',
errorCode: error.message,
messagesMapping: _index.CommonOperationErrorMessageMapping
});
}
};
}
exports.PrinterWrapper = PrinterWrapper;
//# sourceMappingURL=PrinterWrapper.js.map