taro-bluetooth-print 2.8.4 → 2.9.1
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/CHANGELOG.md +33 -0
- package/README.md +53 -4
- package/dist/index.cjs.js +1 -1
- package/dist/index.es.js +1 -1
- package/dist/index.umd.js +1 -1
- package/dist/types/core/BluetoothPrinter.d.ts +21 -4
- package/dist/types/core/di/Container.d.ts +84 -0
- package/dist/types/core/di/Tokens.d.ts +29 -0
- package/dist/types/core/di/index.d.ts +3 -0
- package/dist/types/core/event/EventBus.d.ts +66 -0
- package/dist/types/core/event/index.d.ts +2 -0
- package/dist/types/core/index.d.ts +5 -4
- package/dist/types/core/plugin/PluginManager.d.ts +64 -0
- package/dist/types/core/plugin/index.d.ts +2 -0
- package/dist/types/device/MultiPrinterManager.d.ts +2 -0
- package/dist/types/errors/CommandBuildError.d.ts +40 -0
- package/dist/types/errors/ConnectionError.d.ts +45 -0
- package/dist/types/errors/PrintJobError.d.ts +42 -0
- package/dist/types/errors/index.d.ts +9 -0
- package/dist/types/factory/PrinterFactory.d.ts +108 -0
- package/dist/types/factory/di-factory.d.ts +52 -0
- package/dist/types/factory/index.d.ts +6 -0
- package/dist/types/index.d.ts +9 -1
- package/dist/types/providers/ServiceProvider.d.ts +56 -0
- package/dist/types/providers/index.d.ts +2 -0
- package/dist/types/services/interfaces/ICommandBuilder.d.ts +123 -0
- package/dist/types/services/interfaces/IConnectionManager.d.ts +49 -0
- package/dist/types/services/interfaces/IPrintJobManager.d.ts +67 -0
- package/dist/types/services/interfaces/index.d.ts +6 -233
- package/dist/types/template/TemplateEngine.d.ts +24 -68
- package/dist/types/template/engines/TemplateRenderer.d.ts +71 -0
- package/dist/types/template/parsers/TemplateParser.d.ts +23 -0
- package/dist/types/utils/index.d.ts +8 -0
- package/dist/types/utils/logger.d.ts +4 -3
- package/dist/types/utils/outputLimiter.d.ts +87 -0
- package/dist/types/utils/validation.d.ts +11 -309
- package/dist/types/utils/validators/array.d.ts +19 -0
- package/dist/types/utils/validators/buffer.d.ts +18 -0
- package/dist/types/utils/validators/chain.d.ts +31 -0
- package/dist/types/utils/validators/common.d.ts +22 -0
- package/dist/types/utils/validators/number.d.ts +20 -0
- package/dist/types/utils/validators/object.d.ts +24 -0
- package/dist/types/utils/validators/printer.d.ts +40 -0
- package/dist/types/utils/validators/types.d.ts +125 -0
- package/dist/types/utils/validators/uuid.d.ts +23 -0
- package/package.json +1 -1
- package/src/core/BluetoothPrinter.ts +48 -50
- package/src/core/di/Container.ts +330 -0
- package/src/core/di/Tokens.ts +45 -0
- package/src/core/di/index.ts +3 -0
- package/src/core/event/EventBus.ts +251 -0
- package/src/core/event/index.ts +2 -0
- package/src/core/index.ts +10 -4
- package/src/core/plugin/PluginManager.ts +161 -0
- package/src/core/plugin/index.ts +2 -0
- package/src/device/MultiPrinterManager.ts +15 -6
- package/src/errors/CommandBuildError.ts +72 -0
- package/src/errors/ConnectionError.ts +78 -0
- package/src/errors/PrintJobError.ts +75 -0
- package/src/errors/index.ts +10 -0
- package/src/factory/PrinterFactory.ts +139 -0
- package/src/factory/di-factory.ts +61 -0
- package/src/factory/index.ts +12 -0
- package/src/index.ts +61 -1
- package/src/providers/ServiceProvider.ts +213 -0
- package/src/providers/index.ts +2 -0
- package/src/services/interfaces/ICommandBuilder.ts +145 -0
- package/src/services/interfaces/IConnectionManager.ts +58 -0
- package/src/services/interfaces/IPrintJobManager.ts +83 -0
- package/src/services/interfaces/index.ts +5 -265
- package/src/template/TemplateEngine.ts +27 -792
- package/src/template/engines/TemplateRenderer.ts +762 -0
- package/src/template/parsers/TemplateParser.ts +94 -0
- package/src/utils/index.ts +9 -0
- package/src/utils/logger.ts +17 -4
- package/src/utils/outputLimiter.ts +227 -0
- package/src/utils/validation.ts +21 -1138
- package/src/utils/validators/array.ts +95 -0
- package/src/utils/validators/buffer.ts +81 -0
- package/src/utils/validators/chain.ts +181 -0
- package/src/utils/validators/common.ts +216 -0
- package/src/utils/validators/number.ts +101 -0
- package/src/utils/validators/object.ts +63 -0
- package/src/utils/validators/printer.ts +294 -0
- package/src/utils/validators/types.ts +105 -0
- package/src/utils/validators/uuid.ts +49 -0
|
@@ -62,11 +62,28 @@ export declare class BluetoothPrinter extends EventEmitter<PrinterEvents> {
|
|
|
62
62
|
/**
|
|
63
63
|
* Creates a new BluetoothPrinter instance
|
|
64
64
|
*
|
|
65
|
-
*
|
|
66
|
-
*
|
|
67
|
-
*
|
|
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,
|
|
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,84 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 依赖注入容器
|
|
3
|
+
* 提供现代化的依赖管理和服务定位功能
|
|
4
|
+
*/
|
|
5
|
+
export type Constructor<T = object> = new (...args: any[]) => T;
|
|
6
|
+
export type Factory<T> = (container: Container) => T;
|
|
7
|
+
export type Provider<T> = Constructor<T> | Factory<T> | T;
|
|
8
|
+
export interface RegistrationOptions {
|
|
9
|
+
/** 是否单例 */
|
|
10
|
+
singleton?: boolean;
|
|
11
|
+
/** 生命周期 */
|
|
12
|
+
lifecycle?: 'transient' | 'singleton' | 'scoped';
|
|
13
|
+
/** 标签,用于区分同一接口的不同实现 */
|
|
14
|
+
tag?: string;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Angular 风格的服务提供者配置
|
|
18
|
+
*/
|
|
19
|
+
export interface ServiceProviderConfig<T = unknown> {
|
|
20
|
+
/** 使用类 */
|
|
21
|
+
useClass?: Constructor<T>;
|
|
22
|
+
/** 使用工厂函数 */
|
|
23
|
+
useFactory?: Factory<T>;
|
|
24
|
+
/** 使用实例值 */
|
|
25
|
+
useValue?: T;
|
|
26
|
+
/** 生命周期 */
|
|
27
|
+
lifecycle?: 'transient' | 'singleton' | 'scoped';
|
|
28
|
+
/** 依赖项 */
|
|
29
|
+
dependencies?: (string | symbol | Constructor<unknown>)[];
|
|
30
|
+
}
|
|
31
|
+
export declare class Container {
|
|
32
|
+
private registrations;
|
|
33
|
+
private parent?;
|
|
34
|
+
private scopedInstances;
|
|
35
|
+
constructor(parent?: Container);
|
|
36
|
+
/**
|
|
37
|
+
* 创建子容器
|
|
38
|
+
*/
|
|
39
|
+
createChild(): Container;
|
|
40
|
+
/**
|
|
41
|
+
* 注册服务
|
|
42
|
+
*/
|
|
43
|
+
register<T>(token: string | symbol | Constructor<T>, provider: Provider<T> | ServiceProviderConfig<T>, options?: RegistrationOptions): this;
|
|
44
|
+
/**
|
|
45
|
+
* 注册单例服务
|
|
46
|
+
*/
|
|
47
|
+
registerSingleton<T>(token: string | symbol | Constructor<T>, provider: Provider<T>): this;
|
|
48
|
+
/**
|
|
49
|
+
* 注册类型映射
|
|
50
|
+
*/
|
|
51
|
+
registerType<T>(from: string | symbol | Constructor<T>, to: Constructor<T>, options?: RegistrationOptions): this;
|
|
52
|
+
/**
|
|
53
|
+
* 注册实例
|
|
54
|
+
*/
|
|
55
|
+
registerInstance<T>(token: string | symbol | Constructor<T>, instance: T): this;
|
|
56
|
+
/**
|
|
57
|
+
* 解析服务
|
|
58
|
+
*/
|
|
59
|
+
resolve<T>(token: string | symbol | Constructor<T>, tag?: string): T;
|
|
60
|
+
/**
|
|
61
|
+
* 解析所有注册的服务
|
|
62
|
+
*/
|
|
63
|
+
resolveAll<T>(token: string | symbol | Constructor<T>): T[];
|
|
64
|
+
/**
|
|
65
|
+
* 检查是否已注册
|
|
66
|
+
*/
|
|
67
|
+
isRegistered<T>(token: string | symbol | Constructor<T>): boolean;
|
|
68
|
+
/**
|
|
69
|
+
* 清除所有注册
|
|
70
|
+
*/
|
|
71
|
+
clear(): void;
|
|
72
|
+
/**
|
|
73
|
+
* 创建作用域
|
|
74
|
+
*/
|
|
75
|
+
createScope(): Container;
|
|
76
|
+
private getTokenKey;
|
|
77
|
+
private isConstructor;
|
|
78
|
+
private getOrCreateInstance;
|
|
79
|
+
private createProviderInstance;
|
|
80
|
+
private createInstance;
|
|
81
|
+
}
|
|
82
|
+
export declare const rootContainer: Container;
|
|
83
|
+
export declare function injectable<T extends Constructor>(constructor: T): T;
|
|
84
|
+
export declare function inject(token: string | symbol): (target: any, _propertyKey: string | symbol, parameterIndex: number) => any;
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 服务标识符(Tokens)
|
|
3
|
+
* 用于依赖注入的常量定义
|
|
4
|
+
*/
|
|
5
|
+
export declare const ADAPTER_TOKEN: unique symbol;
|
|
6
|
+
export declare const ADAPTER_FACTORY_TOKEN: unique symbol;
|
|
7
|
+
export declare const DRIVER_TOKEN: unique symbol;
|
|
8
|
+
export declare const DRIVER_FACTORY_TOKEN: unique symbol;
|
|
9
|
+
export declare const DEVICE_MANAGER_TOKEN: unique symbol;
|
|
10
|
+
export declare const CONNECTION_MANAGER_TOKEN: unique symbol;
|
|
11
|
+
export declare const PRINT_JOB_MANAGER_TOKEN: unique symbol;
|
|
12
|
+
export declare const PRINT_QUEUE_TOKEN: unique symbol;
|
|
13
|
+
export declare const OFFLINE_CACHE_TOKEN: unique symbol;
|
|
14
|
+
export declare const CONFIG_MANAGER_TOKEN: unique symbol;
|
|
15
|
+
export declare const COMMAND_BUILDER_TOKEN: unique symbol;
|
|
16
|
+
export declare const PRINTER_STATUS_TOKEN: unique symbol;
|
|
17
|
+
export declare const PRINT_HISTORY_TOKEN: unique symbol;
|
|
18
|
+
export declare const PRINT_STATISTICS_TOKEN: unique symbol;
|
|
19
|
+
export declare const CLOUD_PRINT_MANAGER_TOKEN: unique symbol;
|
|
20
|
+
export declare const SCHEDULED_RETRY_MANAGER_TOKEN: unique symbol;
|
|
21
|
+
export declare const BATCH_PRINT_MANAGER_TOKEN: unique symbol;
|
|
22
|
+
export declare const LOGGER_TOKEN: unique symbol;
|
|
23
|
+
export declare const ENCODING_SERVICE_TOKEN: unique symbol;
|
|
24
|
+
export declare const IMAGE_PROCESSING_TOKEN: unique symbol;
|
|
25
|
+
export declare const BARCODE_GENERATOR_TOKEN: unique symbol;
|
|
26
|
+
export declare const TEMPLATE_ENGINE_TOKEN: unique symbol;
|
|
27
|
+
export declare const EVENT_BUS_TOKEN: unique symbol;
|
|
28
|
+
export declare const PLUGIN_MANAGER_TOKEN: unique symbol;
|
|
29
|
+
export declare const PERFORMANCE_MONITOR_TOKEN: unique symbol;
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 事件总线 - 提供强大的事件驱动能力
|
|
3
|
+
* 支持事件订阅、发布、一次性监听、异步处理等
|
|
4
|
+
*/
|
|
5
|
+
export type EventHandler<T = any> = (payload: T) => void | Promise<void>;
|
|
6
|
+
export type EventFilter<T = any> = (payload: T) => boolean;
|
|
7
|
+
export interface EventSubscription {
|
|
8
|
+
unsubscribe(): void;
|
|
9
|
+
}
|
|
10
|
+
export interface EventOptions {
|
|
11
|
+
/** 优先级,数字越小优先级越高 */
|
|
12
|
+
priority?: number;
|
|
13
|
+
/** 过滤器 */
|
|
14
|
+
filter?: EventFilter<any>;
|
|
15
|
+
/** 是否只监听一次 */
|
|
16
|
+
once?: boolean;
|
|
17
|
+
/** 超时时间(毫秒) */
|
|
18
|
+
timeout?: number;
|
|
19
|
+
}
|
|
20
|
+
export declare class EventBus {
|
|
21
|
+
private listeners;
|
|
22
|
+
private listenerIdCounter;
|
|
23
|
+
/**
|
|
24
|
+
* 订阅事件
|
|
25
|
+
*/
|
|
26
|
+
on<T>(event: string | symbol, handler: EventHandler<T>, options?: EventOptions): EventSubscription;
|
|
27
|
+
/**
|
|
28
|
+
* 只监听一次
|
|
29
|
+
*/
|
|
30
|
+
once<T>(event: string | symbol, handler: EventHandler<T>, options?: Omit<EventOptions, 'once'>): EventSubscription;
|
|
31
|
+
/**
|
|
32
|
+
* 取消订阅
|
|
33
|
+
*/
|
|
34
|
+
off<T>(event: string | symbol, handler: EventHandler<T>): void;
|
|
35
|
+
/**
|
|
36
|
+
* 发布事件
|
|
37
|
+
*/
|
|
38
|
+
emit<T>(event: string | symbol, payload: T): Promise<void>;
|
|
39
|
+
/**
|
|
40
|
+
* 同步发布事件(不等待处理完成)
|
|
41
|
+
*/
|
|
42
|
+
emitSync<T>(event: string | symbol, payload: T): void;
|
|
43
|
+
/**
|
|
44
|
+
* 等待特定事件
|
|
45
|
+
*/
|
|
46
|
+
waitFor<T>(event: string | symbol, timeout?: number, _filter?: EventFilter<T>): Promise<T>;
|
|
47
|
+
/**
|
|
48
|
+
* 检查是否有监听器
|
|
49
|
+
*/
|
|
50
|
+
hasListeners(event: string | symbol): boolean;
|
|
51
|
+
/**
|
|
52
|
+
* 获取监听器数量
|
|
53
|
+
*/
|
|
54
|
+
listenerCount(event: string | symbol): number;
|
|
55
|
+
/**
|
|
56
|
+
* 移除所有监听器
|
|
57
|
+
*/
|
|
58
|
+
removeAllListeners(event?: string | symbol): void;
|
|
59
|
+
/**
|
|
60
|
+
* 获取所有事件名称
|
|
61
|
+
*/
|
|
62
|
+
eventNames(): (string | symbol)[];
|
|
63
|
+
private generateListenerId;
|
|
64
|
+
private executeWithTimeout;
|
|
65
|
+
}
|
|
66
|
+
export declare const globalEventBus: EventBus;
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
2
|
+
* 核心架构模块
|
|
3
|
+
* 提供依赖注入、事件总线、插件管理等核心能力
|
|
3
4
|
*/
|
|
4
|
-
export
|
|
5
|
-
export
|
|
6
|
-
export
|
|
5
|
+
export * from './di';
|
|
6
|
+
export * from './event';
|
|
7
|
+
export * from './plugin';
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { EventBus } from '../event/EventBus';
|
|
2
|
+
import { Container } from '../di/Container';
|
|
3
|
+
export interface PluginContext {
|
|
4
|
+
/** 事件总线 */
|
|
5
|
+
eventBus: EventBus;
|
|
6
|
+
/** DI容器 */
|
|
7
|
+
container: Container;
|
|
8
|
+
/** 配置 */
|
|
9
|
+
config: Record<string, unknown>;
|
|
10
|
+
}
|
|
11
|
+
export interface Plugin {
|
|
12
|
+
/** 插件名称 */
|
|
13
|
+
name: string;
|
|
14
|
+
/** 插件版本 */
|
|
15
|
+
version: string;
|
|
16
|
+
/** 插件依赖 */
|
|
17
|
+
dependencies?: string[];
|
|
18
|
+
/** 安装插件 */
|
|
19
|
+
install(context: PluginContext): void | Promise<void>;
|
|
20
|
+
/** 卸载插件 */
|
|
21
|
+
uninstall?(context: PluginContext): void | Promise<void>;
|
|
22
|
+
}
|
|
23
|
+
export interface PluginRegistration {
|
|
24
|
+
plugin: Plugin;
|
|
25
|
+
config: Record<string, unknown>;
|
|
26
|
+
installed: boolean;
|
|
27
|
+
}
|
|
28
|
+
export declare class PluginManager {
|
|
29
|
+
private plugins;
|
|
30
|
+
private context;
|
|
31
|
+
constructor(context: PluginContext);
|
|
32
|
+
/**
|
|
33
|
+
* 注册插件
|
|
34
|
+
*/
|
|
35
|
+
register(plugin: Plugin, config?: Record<string, unknown>): void;
|
|
36
|
+
/**
|
|
37
|
+
* 安装插件
|
|
38
|
+
*/
|
|
39
|
+
install(pluginName: string): Promise<void>;
|
|
40
|
+
/**
|
|
41
|
+
* 卸载插件
|
|
42
|
+
*/
|
|
43
|
+
uninstall(pluginName: string): Promise<void>;
|
|
44
|
+
/**
|
|
45
|
+
* 批量安装插件
|
|
46
|
+
*/
|
|
47
|
+
installAll(): Promise<void>;
|
|
48
|
+
/**
|
|
49
|
+
* 获取插件
|
|
50
|
+
*/
|
|
51
|
+
getPlugin(name: string): Plugin | undefined;
|
|
52
|
+
/**
|
|
53
|
+
* 检查插件是否已安装
|
|
54
|
+
*/
|
|
55
|
+
isInstalled(name: string): boolean;
|
|
56
|
+
/**
|
|
57
|
+
* 获取所有已安装的插件
|
|
58
|
+
*/
|
|
59
|
+
getInstalledPlugins(): Plugin[];
|
|
60
|
+
/**
|
|
61
|
+
* 获取所有已注册的插件
|
|
62
|
+
*/
|
|
63
|
+
getRegisteredPlugins(): Plugin[];
|
|
64
|
+
}
|
|
@@ -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,52 @@
|
|
|
1
|
+
import { BluetoothPrinter } from '../core/BluetoothPrinter';
|
|
2
|
+
import { createServiceProvider, ServiceProviderOptions } from '../providers';
|
|
3
|
+
import { IConnectionManager, IPrintJobManager, ICommandBuilder } from '../services/interfaces';
|
|
4
|
+
/**
|
|
5
|
+
* 创建打印机实例的选项
|
|
6
|
+
*/
|
|
7
|
+
export interface CreatePrinterOptions extends ServiceProviderOptions {
|
|
8
|
+
/** 自定义连接管理器 */
|
|
9
|
+
connectionManager?: IConnectionManager;
|
|
10
|
+
/** 自定义打印任务管理器 */
|
|
11
|
+
printJobManager?: IPrintJobManager;
|
|
12
|
+
/** 自定义命令构建器 */
|
|
13
|
+
commandBuilder?: ICommandBuilder;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* 使用依赖注入创建 BluetoothPrinter 实例
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* ```typescript
|
|
20
|
+
* // 基本用法
|
|
21
|
+
* const printer = createPrinter();
|
|
22
|
+
*
|
|
23
|
+
* // 使用自定义配置
|
|
24
|
+
* const printer = createPrinter({
|
|
25
|
+
* config: { debug: true },
|
|
26
|
+
* useGlobalEventBus: true
|
|
27
|
+
* });
|
|
28
|
+
* ```
|
|
29
|
+
*/
|
|
30
|
+
export declare function createPrinter(options?: CreatePrinterOptions): BluetoothPrinter;
|
|
31
|
+
/**
|
|
32
|
+
* 获取服务提供者(用于访问所有服务)
|
|
33
|
+
*/
|
|
34
|
+
export declare function getServiceProvider(options?: ServiceProviderOptions): {
|
|
35
|
+
getConnectionManager: () => unknown;
|
|
36
|
+
getPrintJobManager: () => unknown;
|
|
37
|
+
getCommandBuilder: () => unknown;
|
|
38
|
+
getDeviceManager: () => unknown;
|
|
39
|
+
getPrintQueue: () => unknown;
|
|
40
|
+
getOfflineCache: () => unknown;
|
|
41
|
+
getConfigManager: () => unknown;
|
|
42
|
+
getPrinterStatus: () => unknown;
|
|
43
|
+
getPrintHistory: () => unknown;
|
|
44
|
+
getPrintStatistics: () => unknown;
|
|
45
|
+
getCloudPrintManager: () => unknown;
|
|
46
|
+
getScheduledRetryManager: () => unknown;
|
|
47
|
+
getBatchPrintManager: () => unknown;
|
|
48
|
+
getEventBus: () => unknown;
|
|
49
|
+
getPluginManager: () => unknown;
|
|
50
|
+
getAdapter: () => unknown;
|
|
51
|
+
};
|
|
52
|
+
export { createServiceProvider, ServiceProviderOptions };
|
package/dist/types/index.d.ts
CHANGED
|
@@ -39,12 +39,20 @@ export { Logger, LogLevel } from './utils/logger';
|
|
|
39
39
|
export { Encoding } from './utils/encoding';
|
|
40
40
|
export { ImageProcessing } from './utils/image';
|
|
41
41
|
export { PlatformType, detectPlatform, isPlatformSupported } from './utils/platform';
|
|
42
|
+
export { truncateString, truncateForLog, batchProcess, createLimitedLogger, generateSummary, } from './utils/outputLimiter';
|
|
43
|
+
export type { TruncateOptions } from './utils/outputLimiter';
|
|
42
44
|
export { BluetoothPrintError, ErrorCode } from './errors/BluetoothError';
|
|
45
|
+
export { ConnectionError, ConnectionErrorCode } from './errors/ConnectionError';
|
|
46
|
+
export { PrintJobError, PrintJobErrorCode } from './errors/PrintJobError';
|
|
47
|
+
export { CommandBuildError, CommandBuildErrorCode } from './errors/CommandBuildError';
|
|
48
|
+
export { createBluetoothPrinter, createWebBluetoothPrinter, PrinterFactory, type PrinterFactoryOptions, } from './factory';
|
|
49
|
+
export { createPrinter, getServiceProvider, createServiceProvider, type CreatePrinterOptions, type ServiceProviderOptions, } from './factory/di-factory';
|
|
50
|
+
export { Container, rootContainer, injectable, inject, EventBus, globalEventBus, PluginManager, } from './core';
|
|
51
|
+
export { ADAPTER_TOKEN, DRIVER_TOKEN, DEVICE_MANAGER_TOKEN, CONNECTION_MANAGER_TOKEN, PRINT_JOB_MANAGER_TOKEN, PRINT_QUEUE_TOKEN, OFFLINE_CACHE_TOKEN, CONFIG_MANAGER_TOKEN, LOGGER_TOKEN, EVENT_BUS_TOKEN, PLUGIN_MANAGER_TOKEN, PERFORMANCE_MONITOR_TOKEN, COMMAND_BUILDER_TOKEN, PRINTER_STATUS_TOKEN, PRINT_HISTORY_TOKEN, PRINT_STATISTICS_TOKEN, CLOUD_PRINT_MANAGER_TOKEN, SCHEDULED_RETRY_MANAGER_TOKEN, BATCH_PRINT_MANAGER_TOKEN, } from './core';
|
|
43
52
|
export { DEFAULT_CONFIG, mergeConfig } from './config/PrinterConfig';
|
|
44
53
|
export type { PrinterConfig, AdapterConfig, DriverConfig, LoggingConfig, } from './config/PrinterConfig';
|
|
45
54
|
export { PrinterConfigManager, printerConfigManager } from './config/PrinterConfigManager';
|
|
46
55
|
export type { SavedPrinter, GlobalConfig, IConfigStorage } from './config/PrinterConfigManager';
|
|
47
|
-
export { PluginManager } from './plugins/PluginManager';
|
|
48
56
|
export { createLoggingPlugin, createRetryPlugin } from './plugins';
|
|
49
57
|
export type { Plugin, PluginHooks, PluginOptions, PluginFactory } from './plugins/types';
|
|
50
58
|
export * from './types';
|