@plevands/epson-thermal-printer 0.1.1 → 0.2.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 CHANGED
@@ -77,7 +77,7 @@ configurePdfWorker('/assets/pdf.worker.min.mjs');
77
77
  import { useEpsonPrinter, usePrinterConfig } from '@plevands/epson-thermal-printer';
78
78
 
79
79
  function MyPrintComponent() {
80
- const { config } = usePrinterConfig();
80
+ const { config, isConfigured } = usePrinterConfig();
81
81
  const { print, isLoading, error } = useEpsonPrinter(config);
82
82
 
83
83
  const handlePrint = async () => {
@@ -93,7 +93,8 @@ function MyPrintComponent() {
93
93
 
94
94
  return (
95
95
  <div>
96
- <button onClick={handlePrint} disabled={isLoading}>
96
+ {!isConfigured && <p>Please configure the printer first</p>}
97
+ <button onClick={handlePrint} disabled={isLoading || !isConfigured}>
97
98
  {isLoading ? 'Printing...' : 'Print'}
98
99
  </button>
99
100
  {error && <p>Error: {error}</p>}
@@ -102,6 +103,41 @@ function MyPrintComponent() {
102
103
  }
103
104
  ```
104
105
 
106
+ ### Text-Only Printing with Builder (Hooks API)
107
+
108
+ ```typescript
109
+ import { useEpsonPrinter, usePrinterConfig } from '@plevands/epson-thermal-printer';
110
+
111
+ function TextReceipt() {
112
+ const { config, isConfigured } = usePrinterConfig({
113
+ initialConfig: { printerIP: '192.168.1.100' },
114
+ });
115
+ const { printWithBuilder, isLoading, error } = useEpsonPrinter(config, { align: 'left' });
116
+
117
+ const handlePrintText = async () => {
118
+ const result = await printWithBuilder((builder) => {
119
+ builder.addTextAlign('left');
120
+ builder.addText('TICKET #123\n');
121
+ builder.addText('Producto A x1 10.00\n');
122
+ builder.addText('TOTAL 10.00\n');
123
+ builder.addFeedLine(2);
124
+ builder.addCut('feed');
125
+ });
126
+
127
+ if (!result.success) {
128
+ console.error(result.message);
129
+ }
130
+ };
131
+
132
+ return (
133
+ <button onClick={handlePrintText} disabled={isLoading || !isConfigured}>
134
+ Imprimir texto
135
+ {error ? ` (${error})` : ''}
136
+ </button>
137
+ );
138
+ }
139
+ ```
140
+
105
141
  ### Service API (No React)
106
142
 
107
143
  ```typescript
@@ -115,6 +151,13 @@ const service = new EposPrintService({
115
151
 
116
152
  // SDK loads automatically from configured path (default: /epos-2.27.0.js)
117
153
  const result = await service.printCanvas(canvas);
154
+
155
+ // With HTTPS (e.g., for printers with TLS enabled)
156
+ const secureService = new EposPrintService({
157
+ printerIP: '192.168.1.100',
158
+ useHttps: true, // Uses port 443 by default when true
159
+ deviceId: 'local_printer',
160
+ });
118
161
  ```
119
162
 
120
163
  ### Check Printer Connection
@@ -128,16 +171,21 @@ import { useEpsonPrinter, usePrinterConfig } from '@plevands/epson-thermal-print
128
171
  import { useEffect, useState } from 'react';
129
172
 
130
173
  function PrinterPanel() {
131
- const { config } = usePrinterConfig();
174
+ const { config, isConfigured } = usePrinterConfig();
132
175
  const { checkConnection, print, isLoading, error } = useEpsonPrinter(config);
133
176
  const [isOnline, setIsOnline] = useState<boolean | null>(null);
134
177
 
135
178
  // Check connection when component mounts or config changes
136
179
  useEffect(() => {
180
+ if (!isConfigured) return;
137
181
  checkConnection().then(result => {
138
182
  setIsOnline(result.success);
139
183
  });
140
- }, [checkConnection]);
184
+ }, [checkConnection, isConfigured]);
185
+
186
+ if (!isConfigured) {
187
+ return <span>⚙️ Please configure the printer</span>;
188
+ }
141
189
 
142
190
  if (isOnline === null) {
143
191
  return <span>Checking printer connection...</span>;
@@ -198,7 +246,7 @@ import {
198
246
  } from '@plevands/epson-thermal-printer';
199
247
 
200
248
  function PdfPrinter() {
201
- const { config } = usePrinterConfig();
249
+ const { config, isConfigured } = usePrinterConfig();
202
250
  const { processFile } = usePdfProcessor({
203
251
  enabled: true,
204
252
  trimMargins: { top: 10, bottom: 10, left: 5, right: 5 },
@@ -265,9 +313,10 @@ interface PrintOptions {
265
313
  ```typescript
266
314
  interface EpsonPrinterConfig {
267
315
  printerIP: string; // Required
268
- printerPort?: number; // Default: 80
316
+ printerPort?: number; // Default: 80 (or 443 if useHttps is true)
269
317
  deviceId?: string; // Default: 'local_printer'
270
318
  timeout?: number; // Default: 60000ms
319
+ useHttps?: boolean; // Use HTTPS instead of HTTP (default: false)
271
320
  }
272
321
  ```
273
322
 
@@ -279,23 +328,51 @@ interface EpsonPrinterConfig {
279
328
 
280
329
  Main hook for printer operations with automatic SDK loading.
281
330
 
331
+ **Parameters:**
332
+ - `config` - `EpsonPrinterConfig | null` — printer configuration (pass `null` or unconfigured hook value; operations will return `NOT_CONFIGURED` error)
333
+ - `options?` - `PrintOptions` — default print options
334
+
282
335
  **Returns:**
283
336
  - `print(canvas)` - Print a single canvas
284
337
  - `printPages(canvases, options?)` - Print multiple pages
338
+ - `printWithBuilder(builder => ...)` - Print custom ePOSBuilder commands (ideal for text receipts)
339
+ - `checkConnection()` - Check printer connection without printing
285
340
  - `testConnection()` - Test printer connection
286
341
  - `isLoading` - Loading state
287
342
  - `error` - Error message if any
288
343
  - `sdkStatus` - SDK loading status
289
344
 
290
- #### `usePrinterConfig()`
345
+ #### `usePrinterConfig(options?)`
291
346
 
292
347
  Manages printer configuration with localStorage persistence.
293
348
 
349
+ Without arguments, `config` starts as `null` until the user calls `updateConfig`. Pass `initialConfig` to provide defaults for new users. Pass `storageKey` to use a custom localStorage key — useful when managing multiple printer configurations (e.g. one per network).
350
+
351
+ **Options (`UsePrinterConfigOptions`):**
352
+ | Option | Type | Default | Description |
353
+ |--------|------|---------|-------------|
354
+ | `initialConfig` | `EpsonPrinterConfig` | `undefined` | Defaults for new users. localStorage values take priority for returning users. |
355
+ | `storageKey` | `string` | `'epson-printer-config'` | localStorage key for this configuration instance. |
356
+
357
+ ```typescript
358
+ // No defaults — config is null until user configures
359
+ const { config, isConfigured } = usePrinterConfig();
360
+
361
+ // With defaults — config starts pre-filled
362
+ const { config, isConfigured } = usePrinterConfig({
363
+ initialConfig: { printerIP: '10.0.0.50' },
364
+ });
365
+
366
+ // Multiple configs for different networks
367
+ const office = usePrinterConfig({ storageKey: 'printer-office' });
368
+ const warehouse = usePrinterConfig({ storageKey: 'printer-warehouse' });
369
+ ```
370
+
294
371
  **Returns:**
295
- - `config` - Current configuration
296
- - `updateConfig(partial)` - Update configuration
297
- - `resetConfig()` - Reset to defaults
298
- - `isConfigured` - Boolean
372
+ - `config` - Current configuration (`EpsonPrinterConfig | null`)
373
+ - `updateConfig(partial)` - Update configuration (persists to localStorage)
374
+ - `resetConfig()` - Reset to initial value (`initialConfig` if provided, otherwise `null`) and clear localStorage
375
+ - `isConfigured` - `true` when config is not null and has a valid printer IP
299
376
 
300
377
  #### `usePdfProcessor(config?)`
301
378
 
@@ -1,2 +1,2 @@
1
1
  import { EpsonPrinterConfig, PrintOptions, UseEpsonPrinterReturn } from '../types';
2
- export declare function useEpsonPrinter(config: EpsonPrinterConfig, options?: PrintOptions): UseEpsonPrinterReturn;
2
+ export declare function useEpsonPrinter(config: EpsonPrinterConfig | null, options?: PrintOptions): UseEpsonPrinterReturn;
@@ -1,2 +1,2 @@
1
- import { UsePrinterConfigReturn } from '../types';
2
- export declare function usePrinterConfig(): UsePrinterConfigReturn;
1
+ import { UsePrinterConfigOptions, UsePrinterConfigReturn } from '../types';
2
+ export declare function usePrinterConfig(options?: UsePrinterConfigOptions): UsePrinterConfigReturn;