taro-bluetooth-print 2.8.3 → 2.9.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.
@@ -62,11 +62,28 @@ export declare class BluetoothPrinter extends EventEmitter<PrinterEvents> {
62
62
  /**
63
63
  * Creates a new BluetoothPrinter instance
64
64
  *
65
- * @param connectionManagerOrAdapter - Connection manager instance or printer adapter instance (for backward compatibility)
66
- * @param printJobManagerOrDriver - Print job manager instance or printer driver instance (for backward compatibility)
67
- * @param commandBuilder - Command builder instance (optional)
65
+ * Supports two calling conventions:
66
+ * - Modern DI: new BluetoothPrinter(connectionManager, printJobManager, commandBuilder)
67
+ * - Legacy API: new BluetoothPrinter(adapter) - adapter is wrapped in ConnectionManager
68
+ *
69
+ * @param connectionManagerOrAdapter - Connection manager (recommended) or IPrinterAdapter (legacy)
70
+ * @param printJobManager - Print job manager instance
71
+ * @param commandBuilder - Command builder instance
72
+ *
73
+ * @example
74
+ * ```typescript
75
+ * // Recommended: use the factory
76
+ * import { createBluetoothPrinter } from 'taro-bluetooth-print';
77
+ * const printer = createBluetoothPrinter({ adapter: myAdapter });
78
+ *
79
+ * // Direct instantiation with DI
80
+ * const printer = new BluetoothPrinter(connectionManager, printJobManager, commandBuilder);
81
+ *
82
+ * // Legacy API (backward compatible)
83
+ * const printer = new BluetoothPrinter(adapter);
84
+ * ```
68
85
  */
69
- constructor(connectionManagerOrAdapter?: IConnectionManager | IPrinterAdapter, printJobManagerOrDriver?: IPrintJobManager, commandBuilder?: ICommandBuilder);
86
+ constructor(connectionManagerOrAdapter?: IConnectionManager | IPrinterAdapter, printJobManager?: IPrintJobManager, commandBuilder?: ICommandBuilder);
70
87
  /**
71
88
  * Updates the current state based on the connection manager and print job manager states
72
89
  */
@@ -0,0 +1,40 @@
1
+ import { BluetoothPrintError } from './BluetoothError';
2
+ /**
3
+ * Command building error codes
4
+ */
5
+ export declare enum CommandBuildErrorCode {
6
+ /** Invalid configuration */
7
+ INVALID_CONFIG = "INVALID_CONFIGURATION",
8
+ /** Encoding not supported */
9
+ ENCODING_NOT_SUPPORTED = "ENCODING_NOT_SUPPORTED",
10
+ /** Invalid image data */
11
+ INVALID_IMAGE = "INVALID_IMAGE_DATA",
12
+ /** Invalid QR code data */
13
+ INVALID_QR = "INVALID_QR_DATA",
14
+ /** Driver error */
15
+ DRIVER_ERROR = "DRIVER_ERROR"
16
+ }
17
+ /**
18
+ * CommandBuildError - Specialized error for command building failures
19
+ *
20
+ * @example
21
+ * ```typescript
22
+ * throw new CommandBuildError(
23
+ * CommandBuildErrorCode.ENCODING_NOT_SUPPORTED,
24
+ * 'Encoding "EUC-JP" is not supported',
25
+ * originalError
26
+ * );
27
+ * ```
28
+ */
29
+ export declare class CommandBuildError extends BluetoothPrintError {
30
+ readonly buildErrorCode: CommandBuildErrorCode;
31
+ constructor(buildErrorCode: CommandBuildErrorCode, message: string, originalError?: Error);
32
+ /**
33
+ * Converts CommandBuildErrorCode to base ErrorCode
34
+ */
35
+ private static _toBaseCode;
36
+ /**
37
+ * Checks if an error is a command build error
38
+ */
39
+ static isCommandBuildError(error: unknown): error is CommandBuildError;
40
+ }
@@ -0,0 +1,45 @@
1
+ import { BluetoothPrintError } from './BluetoothError';
2
+ /**
3
+ * Connection-related error codes
4
+ */
5
+ export declare enum ConnectionErrorCode {
6
+ /** Failed to establish connection */
7
+ FAILED = "CONNECTION_FAILED",
8
+ /** Connection attempt timed out */
9
+ TIMEOUT = "CONNECTION_TIMEOUT",
10
+ /** Device not found during discovery */
11
+ NOT_FOUND = "DEVICE_NOT_FOUND",
12
+ /** Device disconnected unexpectedly */
13
+ DISCONNECTED = "DEVICE_DISCONNECTED",
14
+ /** Bluetooth service not found on device */
15
+ SERVICE_NOT_FOUND = "SERVICE_NOT_FOUND",
16
+ /** Bluetooth characteristic not found */
17
+ CHARACTERISTIC_NOT_FOUND = "CHARACTERISTIC_NOT_FOUND",
18
+ /** Service discovery failed */
19
+ DISCOVERY_FAILED = "SERVICE_DISCOVERY_FAILED",
20
+ /** Platform doesn't support Bluetooth */
21
+ PLATFORM_UNSUPPORTED = "PLATFORM_NOT_SUPPORTED"
22
+ }
23
+ /**
24
+ * ConnectionError - Specialized error for connection-related failures
25
+ *
26
+ * @example
27
+ * ```typescript
28
+ * throw new ConnectionError(
29
+ * ConnectionErrorCode.TIMEOUT,
30
+ * 'Connection timed out after 30s',
31
+ * originalError
32
+ * );
33
+ * ```
34
+ */
35
+ export declare class ConnectionError extends BluetoothPrintError {
36
+ constructor(code: ConnectionErrorCode, message: string, originalError?: Error);
37
+ /**
38
+ * Converts ConnectionErrorCode to base ErrorCode
39
+ */
40
+ private static _toBaseCode;
41
+ /**
42
+ * Checks if an error is a connection-related error
43
+ */
44
+ static isConnectionError(error: unknown): error is ConnectionError;
45
+ }
@@ -0,0 +1,42 @@
1
+ import { BluetoothPrintError } from './BluetoothError';
2
+ /**
3
+ * Print job-related error codes
4
+ */
5
+ export declare enum PrintJobErrorCode {
6
+ /** Print job failed */
7
+ FAILED = "PRINT_JOB_FAILED",
8
+ /** Print job is already in progress */
9
+ IN_PROGRESS = "PRINT_JOB_IN_PROGRESS",
10
+ /** Print job was cancelled */
11
+ CANCELLED = "PRINT_JOB_CANCELLED",
12
+ /** Print data is invalid */
13
+ INVALID_DATA = "INVALID_IMAGE_DATA",
14
+ /** Write operation failed */
15
+ WRITE_FAILED = "WRITE_FAILED",
16
+ /** Write operation timed out */
17
+ WRITE_TIMEOUT = "WRITE_TIMEOUT"
18
+ }
19
+ /**
20
+ * PrintJobError - Specialized error for print job failures
21
+ *
22
+ * @example
23
+ * ```typescript
24
+ * throw new PrintJobError(
25
+ * PrintJobErrorCode.WRITE_FAILED,
26
+ * 'Failed to send data to printer',
27
+ * originalError
28
+ * );
29
+ * ```
30
+ */
31
+ export declare class PrintJobError extends BluetoothPrintError {
32
+ readonly jobErrorCode: PrintJobErrorCode;
33
+ constructor(jobErrorCode: PrintJobErrorCode, message: string, originalError?: Error);
34
+ /**
35
+ * Converts PrintJobErrorCode to base ErrorCode
36
+ */
37
+ private static _toBaseCode;
38
+ /**
39
+ * Checks if an error is a print job error
40
+ */
41
+ static isPrintJobError(error: unknown): error is PrintJobError;
42
+ }
@@ -0,0 +1,9 @@
1
+ /**
2
+ * Errors Module
3
+ *
4
+ * @packageDocumentation
5
+ */
6
+ export { BluetoothPrintError, ErrorCode } from './BluetoothError';
7
+ export { ConnectionError, ConnectionErrorCode } from './ConnectionError';
8
+ export { PrintJobError, PrintJobErrorCode } from './PrintJobError';
9
+ export { CommandBuildError, CommandBuildErrorCode } from './CommandBuildError';
@@ -0,0 +1,108 @@
1
+ import { BluetoothPrinter } from '../core/BluetoothPrinter';
2
+ import { IPrinterAdapter } from '../types';
3
+ import { IConnectionManager, IPrintJobManager, ICommandBuilder } from '../services/interfaces';
4
+ /**
5
+ * Options for creating a BluetoothPrinter via the factory
6
+ */
7
+ export interface PrinterFactoryOptions {
8
+ /**
9
+ * Printer adapter to use for Bluetooth communication.
10
+ * Required if not providing custom connectionManager.
11
+ *
12
+ * Common adapters:
13
+ * - WebBluetoothAdapter (browser Web Bluetooth API)
14
+ * - TaroAdapter (Taro framework)
15
+ * - AlipayAdapter (Alipay mini-program)
16
+ * - ReactNativeAdapter (React Native)
17
+ *
18
+ * @example
19
+ * ```typescript
20
+ * import { WebBluetoothAdapter } from 'taro-bluetooth-print';
21
+ *
22
+ * const printer = createBluetoothPrinter({
23
+ * adapter: new WebBluetoothAdapter()
24
+ * });
25
+ * ```
26
+ */
27
+ adapter?: IPrinterAdapter;
28
+ /**
29
+ * Custom connection manager instance.
30
+ * If provided, overrides the default ConnectionManager.
31
+ * Useful for testing or custom connection handling.
32
+ */
33
+ connectionManager?: IConnectionManager;
34
+ /**
35
+ * Custom print job manager instance.
36
+ * If provided, overrides the default PrintJobManager.
37
+ * Useful for custom job scheduling or queue management.
38
+ */
39
+ printJobManager?: IPrintJobManager;
40
+ /**
41
+ * Custom command builder instance.
42
+ * If provided, overrides the default CommandBuilder.
43
+ * Useful for custom command formatting or driver selection.
44
+ */
45
+ commandBuilder?: ICommandBuilder;
46
+ }
47
+ /**
48
+ * Creates a new BluetoothPrinter instance with properly configured services.
49
+ *
50
+ * This is the recommended factory function for creating printer instances.
51
+ * It handles dependency injection and ensures all services are properly wired.
52
+ *
53
+ * @param options - Factory options for configuring the printer
54
+ * @returns A fully configured BluetoothPrinter instance
55
+ *
56
+ * @example
57
+ * ```typescript
58
+ * // Basic usage with WebBluetooth
59
+ * import { createBluetoothPrinter, WebBluetoothAdapter } from 'taro-bluetooth-print';
60
+ *
61
+ * const printer = createBluetoothPrinter({
62
+ * adapter: new WebBluetoothAdapter()
63
+ * });
64
+ *
65
+ * await printer.connect('device-id');
66
+ * await printer.text('Hello').feed(2).cut().print();
67
+ * ```
68
+ *
69
+ * @example
70
+ * ```typescript
71
+ * // Advanced usage with custom services
72
+ * import { createBluetoothPrinter, TaroAdapter } from 'taro-bluetooth-print';
73
+ *
74
+ * const printer = createBluetoothPrinter({
75
+ * adapter: new TaroAdapter(),
76
+ * printJobManager: customPrintJobManager,
77
+ * });
78
+ * ```
79
+ */
80
+ export declare function createBluetoothPrinter(options?: PrinterFactoryOptions): BluetoothPrinter;
81
+ /**
82
+ * Creates a BluetoothPrinter instance for the Web Bluetooth API.
83
+ *
84
+ * This is a convenience function specifically for browser environments
85
+ * using the Web Bluetooth API.
86
+ *
87
+ * @param adapterOptions - Options to pass to the WebBluetoothAdapter
88
+ * @returns A BluetoothPrinter instance configured for Web Bluetooth
89
+ *
90
+ * @example
91
+ * ```typescript
92
+ * import { createWebBluetoothPrinter } from 'taro-bluetooth-print';
93
+ *
94
+ * const printer = createWebBluetoothPrinter();
95
+ * await printer.connect('device-id');
96
+ * ```
97
+ */
98
+ export declare function createWebBluetoothPrinter(): Promise<BluetoothPrinter>;
99
+ /**
100
+ * Default printer factory instance
101
+ *
102
+ * @deprecated Use the factory functions directly. This export exists
103
+ * for backward compatibility only.
104
+ */
105
+ export declare const PrinterFactory: {
106
+ create: typeof createBluetoothPrinter;
107
+ createWebBluetooth: typeof createWebBluetoothPrinter;
108
+ };
@@ -0,0 +1,6 @@
1
+ /**
2
+ * Factory Module
3
+ *
4
+ * @packageDocumentation
5
+ */
6
+ export { createBluetoothPrinter, createWebBluetoothPrinter, PrinterFactory, type PrinterFactoryOptions, } from './PrinterFactory';
@@ -40,6 +40,10 @@ export { Encoding } from './utils/encoding';
40
40
  export { ImageProcessing } from './utils/image';
41
41
  export { PlatformType, detectPlatform, isPlatformSupported } from './utils/platform';
42
42
  export { BluetoothPrintError, ErrorCode } from './errors/BluetoothError';
43
+ export { ConnectionError, ConnectionErrorCode } from './errors/ConnectionError';
44
+ export { PrintJobError, PrintJobErrorCode } from './errors/PrintJobError';
45
+ export { CommandBuildError, CommandBuildErrorCode } from './errors/CommandBuildError';
46
+ export { createBluetoothPrinter, createWebBluetoothPrinter, PrinterFactory, type PrinterFactoryOptions, } from './factory';
43
47
  export { DEFAULT_CONFIG, mergeConfig } from './config/PrinterConfig';
44
48
  export type { PrinterConfig, AdapterConfig, DriverConfig, LoggingConfig, } from './config/PrinterConfig';
45
49
  export { PrinterConfigManager, printerConfigManager } from './config/PrinterConfigManager';
@@ -0,0 +1,123 @@
1
+ import { IQrOptions } from '../../types';
2
+ import { TextAlign, TextStyle } from '../../formatter';
3
+ import { BarcodeOptions } from '../../barcode';
4
+ /**
5
+ * Command Builder Interface
6
+ *
7
+ * Builds print commands using the printer driver
8
+ */
9
+ export interface ICommandBuilder {
10
+ /**
11
+ * Adds text to the print queue
12
+ *
13
+ * @param content - Text content
14
+ * @param encoding - Text encoding
15
+ * @returns this - For method chaining
16
+ */
17
+ text(content: string, encoding?: string): this;
18
+ /**
19
+ * Adds line feeds to the print queue
20
+ *
21
+ * @param lines - Number of lines to feed
22
+ * @returns this - For method chaining
23
+ */
24
+ feed(lines?: number): this;
25
+ /**
26
+ * Adds a paper cut command to the print queue
27
+ *
28
+ * @returns this - For method chaining
29
+ */
30
+ cut(): this;
31
+ /**
32
+ * Adds an image to the print queue
33
+ *
34
+ * @param data - Image data as Uint8Array
35
+ * @param width - Image width
36
+ * @param height - Image height
37
+ * @returns this - For method chaining
38
+ */
39
+ image(data: Uint8Array, width: number, height: number): this;
40
+ /**
41
+ * Adds a QR code to the print queue
42
+ *
43
+ * @param content - QR code content
44
+ * @param options - QR code options
45
+ * @returns this - For method chaining
46
+ */
47
+ qr(content: string, options?: IQrOptions): this;
48
+ /**
49
+ * Clears the print queue
50
+ *
51
+ * @returns this - For method chaining
52
+ */
53
+ clear(): this;
54
+ /**
55
+ * Sets text alignment
56
+ *
57
+ * @param alignment - Text alignment (left, center, right)
58
+ * @returns this - For method chaining
59
+ */
60
+ align(alignment: TextAlign): this;
61
+ /**
62
+ * Sets character size (width and height scale)
63
+ *
64
+ * @param width - Width scale factor (1-8)
65
+ * @param height - Height scale factor (1-8)
66
+ * @returns this - For method chaining
67
+ */
68
+ setSize(width: number, height: number): this;
69
+ /**
70
+ * Sets bold text mode
71
+ *
72
+ * @param enabled - Enable or disable bold
73
+ * @returns this - For method chaining
74
+ */
75
+ setBold(enabled: boolean): this;
76
+ /**
77
+ * Sets underline text mode
78
+ *
79
+ * @param enabled - Enable or disable underline
80
+ * @returns this - For method chaining
81
+ */
82
+ setUnderline(enabled: boolean): this;
83
+ /**
84
+ * Sets inverse printing mode (white on black)
85
+ *
86
+ * @param enabled - Enable or disable inverse
87
+ * @returns this - For method chaining
88
+ */
89
+ setInverse(enabled: boolean): this;
90
+ /**
91
+ * Sets multiple text style properties at once
92
+ *
93
+ * @param style - Text style configuration
94
+ * @returns this - For method chaining
95
+ */
96
+ setStyle(style: TextStyle): this;
97
+ /**
98
+ * Resets all text formatting to default
99
+ *
100
+ * @returns this - For method chaining
101
+ */
102
+ resetStyle(): this;
103
+ /**
104
+ * Adds a 1D barcode to the print queue
105
+ *
106
+ * @param content - Barcode content/data
107
+ * @param options - Barcode options
108
+ * @returns this - For method chaining
109
+ */
110
+ barcode(content: string, options: BarcodeOptions): this;
111
+ /**
112
+ * Gets the current buffer
113
+ *
114
+ * @returns Uint8Array - Current print buffer
115
+ */
116
+ getBuffer(): Uint8Array;
117
+ /**
118
+ * Gets the total number of bytes in the buffer
119
+ *
120
+ * @returns number - Total bytes
121
+ */
122
+ getTotalBytes(): number;
123
+ }
@@ -0,0 +1,49 @@
1
+ import { PrinterState, IPrinterAdapter } from '../../types';
2
+ /**
3
+ * Connection Manager Interface
4
+ *
5
+ * Manages Bluetooth device connections
6
+ */
7
+ export interface IConnectionManager {
8
+ /**
9
+ * Connects to a Bluetooth device
10
+ *
11
+ * @param deviceId - Bluetooth device ID
12
+ * @param options - Connection options
13
+ * @returns Promise<void>
14
+ */
15
+ connect(deviceId: string, options?: {
16
+ retries?: number;
17
+ timeout?: number;
18
+ }): Promise<void>;
19
+ /**
20
+ * Disconnects from the current device
21
+ *
22
+ * @returns Promise<void>
23
+ */
24
+ disconnect(): Promise<void>;
25
+ /**
26
+ * Checks if a device is connected
27
+ *
28
+ * @returns boolean - True if connected, false otherwise
29
+ */
30
+ isConnected(): boolean;
31
+ /**
32
+ * Gets the current device ID
33
+ *
34
+ * @returns string | null - Device ID or null if not connected
35
+ */
36
+ getDeviceId(): string | null;
37
+ /**
38
+ * Gets the current connection state
39
+ *
40
+ * @returns PrinterState - Current state
41
+ */
42
+ getState(): PrinterState;
43
+ /**
44
+ * Gets the printer adapter instance
45
+ *
46
+ * @returns IPrinterAdapter - Printer adapter
47
+ */
48
+ getAdapter(): IPrinterAdapter;
49
+ }
@@ -0,0 +1,67 @@
1
+ import { IAdapterOptions } from '../../types';
2
+ /**
3
+ * Print Job Manager Interface
4
+ *
5
+ * Manages print jobs, including pause/resume/cancel functionality
6
+ */
7
+ export interface IPrintJobManager {
8
+ /**
9
+ * Starts a print job
10
+ *
11
+ * @param buffer - Print data buffer
12
+ * @returns Promise<void>
13
+ */
14
+ start(buffer: Uint8Array, options?: {
15
+ jobId?: string;
16
+ }): Promise<void>;
17
+ /**
18
+ * Pauses the current print job
19
+ */
20
+ pause(): void;
21
+ /**
22
+ * Resumes a paused print job
23
+ *
24
+ * @returns Promise<void>
25
+ */
26
+ resume(jobId?: string): Promise<void>;
27
+ /**
28
+ * Cancels the current print job
29
+ */
30
+ cancel(): void;
31
+ /**
32
+ * Gets the number of bytes remaining to print
33
+ *
34
+ * @returns number - Bytes remaining
35
+ */
36
+ remaining(): number;
37
+ /**
38
+ * Checks if the print job is paused
39
+ *
40
+ * @returns boolean - True if paused, false otherwise
41
+ */
42
+ isPaused(): boolean;
43
+ /**
44
+ * Checks if a print job is in progress
45
+ *
46
+ * @returns boolean - True if in progress, false otherwise
47
+ */
48
+ isInProgress(): boolean;
49
+ /**
50
+ * Sets adapter options for write operations
51
+ *
52
+ * @param options - Adapter options
53
+ */
54
+ setOptions(options: IAdapterOptions): void;
55
+ /**
56
+ * Sets the progress callback
57
+ *
58
+ * @param callback - Progress callback function
59
+ */
60
+ setProgressCallback(callback?: (sent: number, total: number) => void): void;
61
+ /**
62
+ * Sets the job state change callback
63
+ *
64
+ * @param callback - Job state change callback function
65
+ */
66
+ setJobStateCallback(callback?: (state: 'in-progress' | 'paused' | 'completed' | 'cancelled') => void): void;
67
+ }