@rejaul/react-native-printer-kit 0.3.4 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +174 -13
- package/android/build.gradle +1 -1
- package/android/src/main/java/com/rejaul/reactnativeprinterkit/ReactNativePrinterKitModule.kt +175 -31
- package/lib/module/NativeReactNativePrinterKit.js +9 -0
- package/lib/module/NativeReactNativePrinterKit.js.map +1 -1
- package/lib/module/enums.js +36 -0
- package/lib/module/enums.js.map +1 -0
- package/lib/module/index.js +69 -6
- package/lib/module/index.js.map +1 -1
- package/lib/typescript/src/NativeReactNativePrinterKit.d.ts +117 -5
- package/lib/typescript/src/NativeReactNativePrinterKit.d.ts.map +1 -1
- package/lib/typescript/src/enums.d.ts +34 -0
- package/lib/typescript/src/enums.d.ts.map +1 -0
- package/lib/typescript/src/index.d.ts +47 -7
- package/lib/typescript/src/index.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/NativeReactNativePrinterKit.ts +138 -5
- package/src/enums.ts +56 -0
- package/src/index.tsx +98 -9
package/src/enums.ts
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
FileLocation as FileLocationValue,
|
|
3
|
+
PrinterConfidence as PrinterConfidenceValue,
|
|
4
|
+
PrinterConnectionType as PrinterConnectionTypeValue,
|
|
5
|
+
PrinterFilter as PrinterFilterValue,
|
|
6
|
+
PrinterImageType as PrinterImageTypeValue,
|
|
7
|
+
} from './NativeReactNativePrinterKit';
|
|
8
|
+
|
|
9
|
+
// Runtime values for the string unions the native module uses, so callers can write
|
|
10
|
+
// `PrinterFilter.LIKELY_PRINTERS` instead of a bare string. Each object is checked
|
|
11
|
+
// against its union with `satisfies`, so a typo or a value the native side does not
|
|
12
|
+
// know fails to compile. The plain strings keep working everywhere too.
|
|
13
|
+
|
|
14
|
+
/** How sure the library is that a paired Bluetooth device is a printer. */
|
|
15
|
+
export const PrinterConfidence = {
|
|
16
|
+
CONFIDENT: 'CONFIDENT',
|
|
17
|
+
LIKELY: 'LIKELY',
|
|
18
|
+
UNKNOWN: 'UNKNOWN',
|
|
19
|
+
} as const satisfies Record<PrinterConfidenceValue, PrinterConfidenceValue>;
|
|
20
|
+
export type PrinterConfidence =
|
|
21
|
+
(typeof PrinterConfidence)[keyof typeof PrinterConfidence];
|
|
22
|
+
|
|
23
|
+
/** `ALL` lists every paired device, `LIKELY_PRINTERS` hides non-printers, `STRICT` keeps only certain ones. */
|
|
24
|
+
export const PrinterFilter = {
|
|
25
|
+
ALL: 'ALL',
|
|
26
|
+
LIKELY_PRINTERS: 'LIKELY_PRINTERS',
|
|
27
|
+
STRICT: 'STRICT',
|
|
28
|
+
} as const satisfies Record<PrinterFilterValue, PrinterFilterValue>;
|
|
29
|
+
export type PrinterFilter = (typeof PrinterFilter)[keyof typeof PrinterFilter];
|
|
30
|
+
|
|
31
|
+
export const PrinterConnectionType = {
|
|
32
|
+
BLUETOOTH: 'BLUETOOTH',
|
|
33
|
+
NETWORK: 'NETWORK',
|
|
34
|
+
USB: 'USB',
|
|
35
|
+
} as const satisfies Record<
|
|
36
|
+
PrinterConnectionTypeValue,
|
|
37
|
+
PrinterConnectionTypeValue
|
|
38
|
+
>;
|
|
39
|
+
export type PrinterConnectionType =
|
|
40
|
+
(typeof PrinterConnectionType)[keyof typeof PrinterConnectionType];
|
|
41
|
+
|
|
42
|
+
export const PrinterImageType = {
|
|
43
|
+
PNG: 'PNG',
|
|
44
|
+
JPEG: 'JPEG',
|
|
45
|
+
} as const satisfies Record<PrinterImageTypeValue, PrinterImageTypeValue>;
|
|
46
|
+
export type PrinterImageType =
|
|
47
|
+
(typeof PrinterImageType)[keyof typeof PrinterImageType];
|
|
48
|
+
|
|
49
|
+
export const FileLocation = {
|
|
50
|
+
CACHE: 'CACHE',
|
|
51
|
+
APP_FILES: 'APP_FILES',
|
|
52
|
+
DOWNLOADS: 'DOWNLOADS',
|
|
53
|
+
DOCUMENTS: 'DOCUMENTS',
|
|
54
|
+
CUSTOM_FOLDER: 'CUSTOM_FOLDER',
|
|
55
|
+
} as const satisfies Record<FileLocationValue, FileLocationValue>;
|
|
56
|
+
export type FileLocation = (typeof FileLocation)[keyof typeof FileLocation];
|
package/src/index.tsx
CHANGED
|
@@ -1,15 +1,21 @@
|
|
|
1
1
|
import ReactNativePrinterKit, {
|
|
2
2
|
type BluetoothPrinterDevice,
|
|
3
3
|
type ConnectPrinterParams,
|
|
4
|
+
type GetBondedPrintersParams,
|
|
4
5
|
type HtmlToPdfParams,
|
|
5
6
|
type OpenFileParams,
|
|
6
7
|
type PdfToImageParams,
|
|
7
|
-
type
|
|
8
|
+
type PrinterStatus,
|
|
9
|
+
type PrinterStatusParams,
|
|
8
10
|
type PrintHtmlParams,
|
|
9
11
|
type PrintImageBase64Params,
|
|
10
12
|
type PrintImageParams,
|
|
11
13
|
type PrintPdfParams,
|
|
12
14
|
type PrintTextParams,
|
|
15
|
+
type SaveFileParams,
|
|
16
|
+
type SaveFileResult,
|
|
17
|
+
type UsbDeviceParams,
|
|
18
|
+
type UsbPrinterDevice,
|
|
13
19
|
} from './NativeReactNativePrinterKit';
|
|
14
20
|
|
|
15
21
|
/**
|
|
@@ -35,16 +41,47 @@ export function requestBluetoothPermission(): Promise<boolean> {
|
|
|
35
41
|
}
|
|
36
42
|
|
|
37
43
|
/**
|
|
38
|
-
* Get
|
|
44
|
+
* Get the Bluetooth devices already paired (bonded) with this device, best printer
|
|
45
|
+
* matches first. Every device carries a `confidence`. Pass
|
|
46
|
+
* `{ filter: 'LIKELY_PRINTERS' }` to hide devices that do not look like printers
|
|
47
|
+
* (headphones, watches...), or `extraNameKeywords` for a printer with an unusual name.
|
|
39
48
|
*/
|
|
40
|
-
export function getBondedBluetoothPrinters(
|
|
41
|
-
|
|
49
|
+
export function getBondedBluetoothPrinters(
|
|
50
|
+
params: GetBondedPrintersParams = {}
|
|
51
|
+
): BluetoothPrinterDevice[] {
|
|
52
|
+
return ReactNativePrinterKit.getBondedBluetoothPrinters(params);
|
|
42
53
|
}
|
|
43
54
|
|
|
44
55
|
/**
|
|
45
|
-
*
|
|
46
|
-
|
|
47
|
-
|
|
56
|
+
* USB printers currently plugged in. Needs a phone with USB host (OTG).
|
|
57
|
+
*/
|
|
58
|
+
export function getUsbPrinters(): UsbPrinterDevice[] {
|
|
59
|
+
return ReactNativePrinterKit.getUsbPrinters();
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Whether the user already granted this app access to that USB printer.
|
|
64
|
+
*/
|
|
65
|
+
export function hasUsbPermission(params: UsbDeviceParams): boolean {
|
|
66
|
+
return ReactNativePrinterKit.hasUsbPermission(params);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Shows the system USB permission dialog. Call before `connectPrinter` with
|
|
71
|
+
* `type: 'USB'`. The grant is lost when the app is reinstalled.
|
|
72
|
+
*/
|
|
73
|
+
export function requestUsbPermission(
|
|
74
|
+
params: UsbDeviceParams
|
|
75
|
+
): Promise<boolean> {
|
|
76
|
+
return ReactNativePrinterKit.requestUsbPermission(params);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Connect to a printer over Bluetooth (default; paired printer's MAC address),
|
|
81
|
+
* Wi-Fi/LAN (`type: 'NETWORK'`, `address` = IP) or USB (`type: 'USB'` with
|
|
82
|
+
* `vendorId`/`productId`). The connection survives the app being swiped from
|
|
83
|
+
* Recents, and is reconnected to automatically the next time the app starts.
|
|
84
|
+
* Resolves `false` if it could not connect.
|
|
48
85
|
*/
|
|
49
86
|
export function connectPrinter(params: ConnectPrinterParams): Promise<boolean> {
|
|
50
87
|
return ReactNativePrinterKit.connectPrinter(params);
|
|
@@ -71,6 +108,41 @@ export function getConnectedPrinter(): Promise<BluetoothPrinterDevice | null> {
|
|
|
71
108
|
return ReactNativePrinterKit.getConnectedPrinter();
|
|
72
109
|
}
|
|
73
110
|
|
|
111
|
+
/**
|
|
112
|
+
* Whether a printer is connected and currently accepts data. Does not check
|
|
113
|
+
* paper or cover - use `getPrinterStatus()`.
|
|
114
|
+
*/
|
|
115
|
+
export function isPrinterReady(): Promise<boolean> {
|
|
116
|
+
return ReactNativePrinterKit.isPrinterReady();
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* Asks the printer for its state (online / paper out / cover open / error).
|
|
121
|
+
* Many printers do not answer: then `responded` is `false` and the other fields
|
|
122
|
+
* are `null` (unknown, not "ok"). Some printers report an open cover as
|
|
123
|
+
* `paperOut`, so treat `paperOut: true` as "no paper or cover open".
|
|
124
|
+
*/
|
|
125
|
+
export function getPrinterStatus(
|
|
126
|
+
params: PrinterStatusParams = {}
|
|
127
|
+
): Promise<PrinterStatus> {
|
|
128
|
+
return ReactNativePrinterKit.getPrinterStatus(params);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* Prints a short ASCII test page.
|
|
133
|
+
*/
|
|
134
|
+
export function printTestPage(): Promise<void> {
|
|
135
|
+
return ReactNativePrinterKit.printTestPage();
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* Copies a file (e.g. the PDF from `htmlToPdf`) to cache, app storage, public
|
|
140
|
+
* Downloads/Documents or a chosen folder. Never rejects: check `result.success`.
|
|
141
|
+
*/
|
|
142
|
+
export function saveFile(params: SaveFileParams): Promise<SaveFileResult> {
|
|
143
|
+
return ReactNativePrinterKit.saveFile(params);
|
|
144
|
+
}
|
|
145
|
+
|
|
74
146
|
/**
|
|
75
147
|
* Print raw text using the printer's built-in font (ASCII only - use
|
|
76
148
|
* printHtml() for Bangla or other non-Latin scripts).
|
|
@@ -90,7 +162,9 @@ export function printImage(params: PrintImageParams): Promise<void> {
|
|
|
90
162
|
/**
|
|
91
163
|
* Print a base64-encoded image as an ESC/POS raster image.
|
|
92
164
|
*/
|
|
93
|
-
export function printImageBase64(
|
|
165
|
+
export function printImageBase64(
|
|
166
|
+
params: PrintImageBase64Params
|
|
167
|
+
): Promise<void> {
|
|
94
168
|
return ReactNativePrinterKit.printImageBase64(params);
|
|
95
169
|
}
|
|
96
170
|
|
|
@@ -134,19 +208,34 @@ export function openFile(params: OpenFileParams): boolean {
|
|
|
134
208
|
return ReactNativePrinterKit.openFile(params);
|
|
135
209
|
}
|
|
136
210
|
|
|
211
|
+
// Export enum-like constants (usable at runtime, and as types of the same name)
|
|
212
|
+
export {
|
|
213
|
+
FileLocation,
|
|
214
|
+
PrinterConfidence,
|
|
215
|
+
PrinterConnectionType,
|
|
216
|
+
PrinterFilter,
|
|
217
|
+
PrinterImageType,
|
|
218
|
+
} from './enums';
|
|
219
|
+
|
|
137
220
|
// Export types
|
|
138
221
|
export type {
|
|
139
222
|
BluetoothPrinterDevice,
|
|
140
223
|
ConnectPrinterParams,
|
|
224
|
+
GetBondedPrintersParams,
|
|
141
225
|
HtmlToPdfParams,
|
|
142
226
|
OpenFileParams,
|
|
143
227
|
PdfToImageParams,
|
|
144
|
-
|
|
228
|
+
PrinterStatus,
|
|
229
|
+
PrinterStatusParams,
|
|
145
230
|
PrintHtmlParams,
|
|
146
231
|
PrintImageBase64Params,
|
|
147
232
|
PrintImageParams,
|
|
148
233
|
PrintPdfParams,
|
|
149
234
|
PrintTextParams,
|
|
235
|
+
SaveFileParams,
|
|
236
|
+
SaveFileResult,
|
|
237
|
+
UsbDeviceParams,
|
|
238
|
+
UsbPrinterDevice,
|
|
150
239
|
};
|
|
151
240
|
|
|
152
241
|
// Export native module
|