isahaq-barcode 1.9.0 → 2.0.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/index.d.ts ADDED
@@ -0,0 +1,497 @@
1
+ /**
2
+ * Type definitions for isahaq-barcode
3
+ *
4
+ * The runtime is CommonJS (`module.exports = BarcodeGenerator`), so the class is
5
+ * exported with `export =` and every supporting type is re-exported from the
6
+ * merged namespace:
7
+ *
8
+ * import BarcodeGenerator = require('isahaq-barcode');
9
+ * import BarcodeGenerator from 'isahaq-barcode'; // with esModuleInterop
10
+ * const png = await BarcodeGenerator.png('123', 'code128');
11
+ * const opts: BarcodeGenerator.RenderOptions = { width: 3 };
12
+ */
13
+
14
+ /// <reference types="node" />
15
+
16
+ type BarcodeType =
17
+ | 'code128'
18
+ | 'code128a'
19
+ | 'code128b'
20
+ | 'code128c'
21
+ | 'code128auto'
22
+ | 'code39'
23
+ | 'code39extended'
24
+ | 'code39checksum'
25
+ | 'code39auto'
26
+ | 'code93'
27
+ | 'code25'
28
+ | 'code25auto'
29
+ | 'code32'
30
+ | 'standard25'
31
+ | 'standard25checksum'
32
+ | 'interleaved25'
33
+ | 'interleaved25checksum'
34
+ | 'interleaved25auto'
35
+ | 'msi'
36
+ | 'msichecksum'
37
+ | 'msiauto'
38
+ | 'ean13'
39
+ | 'ean8'
40
+ | 'ean2'
41
+ | 'ean5'
42
+ | 'upca'
43
+ | 'upce'
44
+ | 'itf14'
45
+ | 'postnet'
46
+ | 'planet'
47
+ | 'rms4cc'
48
+ | 'kix'
49
+ | 'imb'
50
+ | 'codabar'
51
+ | 'code11'
52
+ | 'pharmacode'
53
+ | 'pharmacodetwotracks'
54
+ | 'qrcode'
55
+ | 'datamatrix'
56
+ | 'aztec'
57
+ | 'pdf417'
58
+ | 'microqr'
59
+ | 'maxicode'
60
+ | 'code16k'
61
+ | 'code49';
62
+
63
+ /** Any registered type id, while still offering completion for the known ones */
64
+ type BarcodeTypeInput = BarcodeType | (string & {});
65
+
66
+ type RenderFormat = 'png' | 'svg' | 'html' | 'pdf';
67
+
68
+ type SyncRenderFormat = 'svg' | 'html';
69
+
70
+ type BarcodeCategory =
71
+ 'LINEAR' | 'EAN_UPC' | 'POSTAL' | 'SPECIALIZED' | 'MATRIX_2D' | 'STACKED';
72
+
73
+ /** Rotation: none, right, left, inverted */
74
+ type Rotation = 'N' | 'R' | 'L' | 'I';
75
+
76
+ interface RenderOptions {
77
+ /** Module (narrow bar) width in pixels. Default 2 */
78
+ width?: number;
79
+ /** Bar height in pixels, 1D symbologies only. Default 100 */
80
+ height?: number;
81
+ /** Target size in pixels for 2D and stacked symbologies */
82
+ size?: number;
83
+ /** Print the human readable text. Default true */
84
+ displayValue?: boolean;
85
+ /** Human readable text size in pixels. Default 20 */
86
+ fontSize?: number;
87
+ /** Text alignment. Default 'center' */
88
+ textAlign?: 'center' | 'left' | 'right' | 'justify';
89
+ /** Gap between the bars and the text in pixels. Default 2 */
90
+ textMargin?: number;
91
+ /** Text colour; falls back to lineColor */
92
+ textColor?: string;
93
+ /** Background colour as #rgb, #rrggbb or rrggbb. Default '#ffffff' */
94
+ background?: string;
95
+ /** Bar colour as #rgb, #rrggbb or rrggbb. Default '#000000' */
96
+ lineColor?: string;
97
+ /** Quiet zone in pixels. Defaults to `quietZone` modules when omitted */
98
+ margin?: number;
99
+ marginTop?: number;
100
+ marginBottom?: number;
101
+ marginLeft?: number;
102
+ marginRight?: number;
103
+ /** Quiet zone in modules, used when no pixel margin is given. Default 10 */
104
+ quietZone?: number;
105
+ /** Rotation. Default 'N' */
106
+ rotate?: Rotation;
107
+ /** Raw bwip-js options, merged last, for advanced use */
108
+ bwipOptions?: Record<string, unknown>;
109
+ }
110
+
111
+ interface HtmlRenderOptions extends RenderOptions {
112
+ /** id attribute of the wrapper element */
113
+ id?: string;
114
+ /** class attribute of the wrapper element. Default 'barcode' */
115
+ className?: string;
116
+ /** Add inline styles to the wrapper element. Default true */
117
+ includeStyles?: boolean;
118
+ }
119
+
120
+ interface PdfRenderOptions extends RenderOptions {
121
+ /** Page width in points. Default 612 */
122
+ pageWidth?: number;
123
+ /** Page height in points. Default 792 */
124
+ pageHeight?: number;
125
+ /** Named page size such as 'A4'. Takes precedence over pageWidth/pageHeight */
126
+ pageSize?: string;
127
+ /** Size the page to the barcode plus its margins */
128
+ fitToBarcode?: boolean;
129
+ /** PDF document title */
130
+ title?: string;
131
+ }
132
+
133
+ type GenerateOptions = RenderOptions & HtmlRenderOptions & PdfRenderOptions;
134
+
135
+ interface ValidationSuccess {
136
+ valid: true;
137
+ data: string;
138
+ type: string;
139
+ length: number;
140
+ charset: string;
141
+ }
142
+
143
+ interface ValidationFailure {
144
+ valid: false;
145
+ error: string;
146
+ }
147
+
148
+ type ValidationResult = ValidationSuccess | ValidationFailure;
149
+
150
+ interface BarcodeTypeInfo {
151
+ type: string;
152
+ name: string;
153
+ description: string;
154
+ category: BarcodeCategory;
155
+ /** Underlying symbology id used by the encoder */
156
+ symbology: string;
157
+ minLength: number;
158
+ maxLength: number;
159
+ charset: string;
160
+ }
161
+
162
+ interface BarcodeTypeConfig {
163
+ minLength: number;
164
+ maxLength: number;
165
+ charset: string;
166
+ }
167
+
168
+ interface RenderFormatInfo {
169
+ format: RenderFormat;
170
+ name: string;
171
+ mimeType: string;
172
+ extension: string;
173
+ returns: 'Buffer' | 'string';
174
+ description: string;
175
+ }
176
+
177
+ interface Dimensions {
178
+ width: number;
179
+ height: number;
180
+ }
181
+
182
+ interface BatchItem {
183
+ data: string;
184
+ type?: BarcodeTypeInput;
185
+ format?: RenderFormat;
186
+ options?: GenerateOptions;
187
+ }
188
+
189
+ interface BatchSuccess {
190
+ index: number;
191
+ success: true;
192
+ result: Buffer | string;
193
+ data: string;
194
+ type: string;
195
+ format: string;
196
+ }
197
+
198
+ interface BatchFailure {
199
+ index: number;
200
+ success: false;
201
+ error: string;
202
+ data?: string;
203
+ type: string;
204
+ format: string;
205
+ }
206
+
207
+ type BatchResult = BatchSuccess | BatchFailure;
208
+
209
+ type WatermarkPosition =
210
+ | 'top-left'
211
+ | 'top-center'
212
+ | 'top-right'
213
+ | 'left-center'
214
+ | 'center'
215
+ | 'right-center'
216
+ | 'bottom-left'
217
+ | 'bottom-center'
218
+ | 'bottom-right';
219
+
220
+ type ColorValue = string | [number, number, number];
221
+
222
+ type QrOutputFormat = 'png' | 'jpg' | 'jpeg' | 'svg';
223
+
224
+ interface QrCodeOptions {
225
+ data?: string;
226
+ /** Output size in pixels, including margins. Default 300 */
227
+ size?: number;
228
+ /** Margin in pixels. Default 10 */
229
+ margin?: number;
230
+ errorCorrectionLevel?: 'L' | 'M' | 'Q' | 'H';
231
+ foregroundColor?: ColorValue;
232
+ backgroundColor?: ColorValue;
233
+ /** Path, URL or data URI of a logo image. Requires the optional canvas package */
234
+ logoPath?: string | null;
235
+ /** Logo size as a percentage of the QR code size. Default 20 */
236
+ logoSize?: number;
237
+ /** Label drawn under the code. Requires the optional canvas package */
238
+ label?: string | null;
239
+ labelFont?: string | null;
240
+ labelFontSize?: number;
241
+ /** Watermark text or image path. Requires the optional canvas package */
242
+ watermark?: string | null;
243
+ watermarkPosition?: WatermarkPosition;
244
+ /** Output format. Default 'png' */
245
+ format?: QrOutputFormat;
246
+ }
247
+
248
+ declare class QrCodeResult {
249
+ readonly options: QrCodeOptions;
250
+ constructor(options: QrCodeOptions);
251
+ /** Whether canvas compositing is needed (logo, label or watermark) */
252
+ needsCompositing(): boolean;
253
+ /** Generate the code: a Buffer for png/jpeg, SVG markup for svg */
254
+ generate(): Promise<Buffer | string>;
255
+ saveToFile(filePath: string): Promise<string>;
256
+ getDataUri(): Promise<string>;
257
+ getString(): Promise<Buffer | string>;
258
+ getWatermarkPosition(size: number): { x: number; y: number };
259
+ rgbToHex(rgb: ColorValue): string;
260
+ }
261
+
262
+ declare class QrCodeBuilder {
263
+ readonly options: QrCodeOptions;
264
+ constructor(options?: QrCodeOptions);
265
+ static create(options?: QrCodeOptions): QrCodeBuilder;
266
+ data(data: string): this;
267
+ size(size: number): this;
268
+ margin(margin: number): this;
269
+ errorCorrectionLevel(level: 'L' | 'M' | 'Q' | 'H'): this;
270
+ foregroundColor(color: ColorValue): this;
271
+ backgroundColor(color: ColorValue): this;
272
+ logoPath(logoPath: string): this;
273
+ logoSize(logoSize: number): this;
274
+ label(label: string): this;
275
+ labelFont(fontPath: string, fontSize?: number): this;
276
+ watermark(watermark: string, position?: WatermarkPosition): this;
277
+ format(format: QrOutputFormat): this;
278
+ build(): QrCodeResult;
279
+ generate(): Promise<Buffer | string>;
280
+ saveToFile(filePath: string): Promise<string>;
281
+ getDataUri(): Promise<string>;
282
+ }
283
+
284
+ declare class Validator {
285
+ validate(data: string, type: string): ValidationResult;
286
+ calculateEANCheckDigit(data: string): number;
287
+ }
288
+
289
+ declare class BarcodeTypes {
290
+ static getAll(): string[];
291
+ static getTypes(): string[];
292
+ static getByCategory(category: string): string[];
293
+ static getCategories(): BarcodeCategory[];
294
+ static isValid(type: string): boolean;
295
+ static getConfig(type: string): BarcodeTypeConfig;
296
+ static getDescription(type: string): string;
297
+ static getTypeInfo(type: string): BarcodeTypeInfo | null;
298
+ static getSymbology(type: string): string | null;
299
+ static getEncodeOptions(type: string): Record<string, unknown>;
300
+ }
301
+
302
+ declare class RenderFormats {
303
+ static getAll(): RenderFormat[];
304
+ static getFormats(): RenderFormat[];
305
+ static isValid(format: string): boolean;
306
+ static getMimeType(format: string): string;
307
+ static getExtension(format: string): string;
308
+ static getDescription(format: string): string;
309
+ static getFormatInfo(format: string): RenderFormatInfo | null;
310
+ }
311
+
312
+ declare class BarcodeEncoder {
313
+ static getDefaultOptions(): RenderOptions;
314
+ static isMatrix(type: string): boolean;
315
+ static buildOptions(
316
+ data: string,
317
+ type: string,
318
+ options?: RenderOptions
319
+ ): Record<string, unknown>;
320
+ static toSvg(data: string, type: string, options?: RenderOptions): string;
321
+ static toPng(
322
+ data: string,
323
+ type: string,
324
+ options?: RenderOptions
325
+ ): Promise<Buffer>;
326
+ static getDimensions(
327
+ data: string,
328
+ type: string,
329
+ options?: RenderOptions
330
+ ): Dimensions;
331
+ }
332
+
333
+ declare class BarcodeService {
334
+ generate(
335
+ data: string,
336
+ type?: BarcodeTypeInput,
337
+ format?: RenderFormat,
338
+ options?: GenerateOptions
339
+ ): Promise<Buffer | string>;
340
+ generateSync(
341
+ data: string,
342
+ type?: BarcodeTypeInput,
343
+ format?: SyncRenderFormat,
344
+ options?: GenerateOptions
345
+ ): string;
346
+ png(
347
+ data: string,
348
+ type?: BarcodeTypeInput,
349
+ options?: RenderOptions
350
+ ): Promise<Buffer>;
351
+ svg(
352
+ data: string,
353
+ type?: BarcodeTypeInput,
354
+ options?: RenderOptions
355
+ ): Promise<string>;
356
+ svgSync(
357
+ data: string,
358
+ type?: BarcodeTypeInput,
359
+ options?: RenderOptions
360
+ ): string;
361
+ html(
362
+ data: string,
363
+ type?: BarcodeTypeInput,
364
+ options?: HtmlRenderOptions
365
+ ): Promise<string>;
366
+ htmlSync(
367
+ data: string,
368
+ type?: BarcodeTypeInput,
369
+ options?: HtmlRenderOptions
370
+ ): string;
371
+ pdf(
372
+ data: string,
373
+ type?: BarcodeTypeInput,
374
+ options?: PdfRenderOptions
375
+ ): Promise<Buffer>;
376
+ batch(items: BatchItem[]): Promise<BatchResult[]>;
377
+ validate(data: string, type: string): ValidationResult;
378
+ getBarcodeTypes(): string[];
379
+ getRenderFormats(): RenderFormat[];
380
+ }
381
+
382
+ declare class BarcodeGenerator {
383
+ /** Generate a barcode in any supported format */
384
+ static generate(
385
+ data: string,
386
+ type?: BarcodeTypeInput,
387
+ format?: RenderFormat,
388
+ options?: GenerateOptions
389
+ ): Promise<Buffer | string>;
390
+
391
+ /** Generate a PNG barcode */
392
+ static png(
393
+ data: string,
394
+ type?: BarcodeTypeInput,
395
+ options?: RenderOptions
396
+ ): Promise<Buffer>;
397
+
398
+ /** Generate an SVG barcode */
399
+ static svg(
400
+ data: string,
401
+ type?: BarcodeTypeInput,
402
+ options?: RenderOptions
403
+ ): Promise<string>;
404
+
405
+ /** Generate an SVG barcode synchronously */
406
+ static svgSync(
407
+ data: string,
408
+ type?: BarcodeTypeInput,
409
+ options?: RenderOptions
410
+ ): string;
411
+
412
+ /** Generate an HTML fragment containing an inline SVG barcode */
413
+ static html(
414
+ data: string,
415
+ type?: BarcodeTypeInput,
416
+ options?: HtmlRenderOptions
417
+ ): Promise<string>;
418
+
419
+ /** Generate an HTML fragment synchronously */
420
+ static htmlSync(
421
+ data: string,
422
+ type?: BarcodeTypeInput,
423
+ options?: HtmlRenderOptions
424
+ ): string;
425
+
426
+ /** Generate a single page PDF containing the barcode */
427
+ static pdf(
428
+ data: string,
429
+ type?: BarcodeTypeInput,
430
+ options?: PdfRenderOptions
431
+ ): Promise<Buffer>;
432
+
433
+ /** Create a QR code builder for logo, label and watermark support */
434
+ static qrCode(data: string, options?: QrCodeOptions): QrCodeBuilder;
435
+
436
+ /** Create a QR code builder from an options object */
437
+ static modernQr(options?: QrCodeOptions): QrCodeBuilder;
438
+
439
+ /** Validate data for a barcode type */
440
+ static validate(data: string, type: string): ValidationResult;
441
+
442
+ /** Generate many barcodes; per-item failures are reported, not thrown */
443
+ static batch(items: BatchItem[]): Promise<BatchResult[]>;
444
+
445
+ static getBarcodeTypes(): string[];
446
+ static getBarcodeCategories(): BarcodeCategory[];
447
+ static getBarcodeTypesByCategory(category: string): string[];
448
+ static getRenderFormats(): RenderFormat[];
449
+ static getWatermarkPositions(): WatermarkPosition[];
450
+ static getBarcodeTypeInfo(type: string): BarcodeTypeInfo | null;
451
+ static getRenderFormatInfo(format: string): RenderFormatInfo | null;
452
+ static getDefaultOptions(): RenderOptions;
453
+ }
454
+
455
+ /**
456
+ * The classes below are exposed as properties of the default export
457
+ * (`BarcodeGenerator.QrCodeBuilder`, ...) and are re-exported here so they can
458
+ * be used as both values and types.
459
+ */
460
+ declare namespace BarcodeGenerator {
461
+ export {
462
+ BarcodeCategory,
463
+ BarcodeEncoder,
464
+ BarcodeService,
465
+ BarcodeType,
466
+ BarcodeTypeConfig,
467
+ BarcodeTypeInfo,
468
+ BarcodeTypeInput,
469
+ BarcodeTypes,
470
+ BatchFailure,
471
+ BatchItem,
472
+ BatchResult,
473
+ BatchSuccess,
474
+ ColorValue,
475
+ Dimensions,
476
+ GenerateOptions,
477
+ HtmlRenderOptions,
478
+ PdfRenderOptions,
479
+ QrCodeBuilder,
480
+ QrCodeOptions,
481
+ QrCodeResult,
482
+ QrOutputFormat,
483
+ RenderFormat,
484
+ RenderFormatInfo,
485
+ RenderFormats,
486
+ RenderOptions,
487
+ Rotation,
488
+ SyncRenderFormat,
489
+ ValidationFailure,
490
+ ValidationResult,
491
+ ValidationSuccess,
492
+ Validator,
493
+ WatermarkPosition,
494
+ };
495
+ }
496
+
497
+ export = BarcodeGenerator;