UNPKG

15.3 kBTypeScriptView Raw
1// Type definitions for qrcode 1.5
2// Project: https://github.com/soldair/node-qrcode
3// Definitions by: York Yao <https://github.com/plantain-00>
4// Michael Nahkies <https://github.com/mnahkies>
5// Rémi Sormain <https://github.com/Marchelune>
6// BendingBender <https://github.com/BendingBender>
7// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
8
9/// <reference types="node" />
10
11import * as stream from 'stream';
12
13export type QRCodeErrorCorrectionLevel = 'low' | 'medium' | 'quartile' | 'high' | 'L' | 'M' | 'Q' | 'H';
14export type QRCodeMaskPattern = 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7;
15export type QRCodeToSJISFunc = (codePoint: string) => number;
16
17export interface QRCodeOptions {
18 /**
19 * QR Code version. If not specified the more suitable value will be calculated.
20 */
21 version?: number | undefined;
22 /**
23 * Error correction level.
24 * @default 'M'
25 */
26 errorCorrectionLevel?: QRCodeErrorCorrectionLevel | undefined;
27 /**
28 * Mask pattern used to mask the symbol.
29 *
30 * If not specified the more suitable value will be calculated.
31 */
32 maskPattern?: QRCodeMaskPattern | undefined;
33 /**
34 * Helper function used internally to convert a kanji to its Shift JIS value.
35 * Provide this function if you need support for Kanji mode.
36 */
37 toSJISFunc?: QRCodeToSJISFunc | undefined;
38}
39
40export type QRCodeDataURLType = 'image/png' | 'image/jpeg' | 'image/webp';
41export type QRCodeToDataURLOptions = QRCodeToDataURLOptionsJpegWebp | QRCodeToDataURLOptionsOther;
42export interface QRCodeToDataURLOptionsJpegWebp extends QRCodeRenderersOptions {
43 /**
44 * Data URI format.
45 * @default 'image/png'
46 */
47 type: 'image/jpeg' | 'image/webp';
48 rendererOpts?:
49 | {
50 /**
51 * A number between `0` and `1` indicating image quality.
52 * @default 0.92
53 */
54 quality?: number | undefined;
55 }
56 | undefined;
57}
58export interface QRCodeToDataURLOptionsOther extends QRCodeRenderersOptions {
59 /**
60 * Data URI format.
61 * @default 'image/png'
62 */
63 type?: Exclude<QRCodeDataURLType, 'image/jpeg' | 'image/webp'> | undefined;
64}
65
66export type QRCodeStringType = 'utf8' | 'svg' | 'terminal';
67export type QRCodeToStringOptions = QRCodeToStringOptionsTerminal | QRCodeToStringOptionsOther;
68export interface QRCodeToStringOptionsTerminal extends QRCodeRenderersOptions {
69 /**
70 * Output format.
71 * @default 'utf8'
72 */
73 type: 'terminal';
74 /**
75 * Outputs smaller QR code.
76 * @default false
77 */
78 small?: boolean | undefined;
79}
80export interface QRCodeToStringOptionsOther extends QRCodeRenderersOptions {
81 /**
82 * Output format.
83 * @default 'utf8'
84 */
85 type?: Exclude<QRCodeStringType, 'terminal'> | undefined;
86}
87
88export type QRCodeFileType = 'png' | 'svg' | 'utf8';
89export type QRCodeToFileOptions = QRCodeToFileOptionsPng | QRCodeToFileOptionsOther;
90export interface QRCodeToFileOptionsPng extends QRCodeRenderersOptions {
91 /**
92 * Output format.
93 * @default 'png'
94 */
95 type?: 'png' | undefined;
96 rendererOpts?:
97 | {
98 /**
99 * Compression level for deflate.
100 * @default 9
101 */
102 deflateLevel?: number | undefined;
103 /**
104 * Compression strategy for deflate.
105 * @default 3
106 */
107 deflateStrategy?: number | undefined;
108 }
109 | undefined;
110}
111export interface QRCodeToFileOptionsOther extends QRCodeRenderersOptions {
112 /**
113 * Output format.
114 * @default 'png'
115 */
116 type: Exclude<QRCodeFileType, 'png'> | undefined;
117}
118
119export type QRCodeFileStreamType = 'png';
120export interface QRCodeToFileStreamOptions extends QRCodeRenderersOptions {
121 /**
122 * Output format. Only png supported for file stream.
123 */
124 type?: QRCodeFileStreamType | undefined;
125 rendererOpts?:
126 | {
127 /**
128 * Compression level for deflate.
129 * @default 9
130 */
131 deflateLevel?: number | undefined;
132 /**
133 * Compression strategy for deflate.
134 * @default 3
135 */
136 deflateStrategy?: number | undefined;
137 }
138 | undefined;
139}
140
141export type QRCodeBufferType = 'png';
142export interface QRCodeToBufferOptions extends QRCodeRenderersOptions {
143 /**
144 * Output format. Only png supported for Buffer.
145 */
146 type?: QRCodeBufferType | undefined;
147 rendererOpts?:
148 | {
149 /**
150 * Compression level for deflate.
151 * @default 9
152 */
153 deflateLevel?: number | undefined;
154 /**
155 * Compression strategy for deflate.
156 * @default 3
157 */
158 deflateStrategy?: number | undefined;
159 }
160 | undefined;
161}
162
163export interface QRCodeRenderersOptions extends QRCodeOptions {
164 /**
165 * Define how much wide the quiet zone should be.
166 * @default 4
167 */
168 margin?: number | undefined;
169 /**
170 * Scale factor. A value of `1` means 1px per modules (black dots).
171 * @default 4
172 */
173 scale?: number | undefined;
174 /**
175 * Forces a specific width for the output image.
176 * If width is too small to contain the qr symbol, this option will be ignored.
177 * Takes precedence over `scale`.
178 */
179 width?: number | undefined;
180 color?:
181 | {
182 /**
183 * Color of dark module. Value must be in hex format (RGBA).
184 * Note: dark color should always be darker than `color.light`.
185 * @default '#000000ff'
186 */
187 dark?: string | undefined;
188 /**
189 * Color of light module. Value must be in hex format (RGBA).
190 * @default '#ffffffff'
191 */
192 light?: string | undefined;
193 }
194 | undefined;
195}
196
197export type QRCodeSegmentMode = 'alphanumeric' | 'numeric' | 'byte' | 'kanji';
198export type QRCodeSegment =
199 | QRCodeNumericSegment
200 | QRCodeAlphanumericSegment
201 | QRCodeByteSegment
202 | QRCodeKanjiSegment
203 | {
204 data: string | Buffer | Uint8ClampedArray | Uint8Array;
205 };
206
207export interface QRCodeNumericSegment {
208 mode: 'numeric';
209 data: string | number;
210}
211
212export interface QRCodeAlphanumericSegment {
213 mode: 'alphanumeric';
214 data: string;
215}
216
217export interface QRCodeByteSegment {
218 mode: 'byte';
219 data: Buffer | Uint8ClampedArray | Uint8Array;
220}
221
222export interface QRCodeKanjiSegment {
223 mode: 'kanji';
224 data: string;
225}
226
227export interface QRCode {
228 /**
229 * BitMatrix class with modules data
230 */
231 modules: BitMatrix;
232 /**
233 * Calculated QR Code version
234 */
235 version: number;
236 /**
237 * Error Correction Level
238 */
239 errorCorrectionLevel: ErrorCorrectionLevel;
240 /**
241 * Calculated Mask pattern
242 */
243 maskPattern: QRCodeMaskPattern | undefined;
244 /**
245 * Generated segments
246 */
247 segments: GeneratedQRCodeSegment[];
248}
249
250/**
251 * Helper class to handle QR Code symbol modules.
252 */
253export interface BitMatrix {
254 /**
255 * Symbol size.
256 */
257 size: number;
258 data: Uint8Array;
259 reservedBit: Uint8Array;
260 /**
261 * Set bit value at specified location
262 * If reserved flag is set, this bit will be ignored during masking process.
263 */
264 set(row: number, col: number, value: number, reserved: boolean): void;
265 /**
266 * @return Bit value at specified location.
267 */
268 get(row: number, col: number): number;
269 /**
270 * Applies xor operator at specified location (used during masking process).
271 */
272 xor(row: number, col: number, value: number): void;
273 /**
274 * Check if bit at specified location is reserved.
275 */
276 isReserved(row: number, col: number): number;
277}
278
279export interface ErrorCorrectionLevel {
280 bit: 0 | 1 | 2 | 3;
281}
282
283export type ModeId = 'Numeric' | 'Alphanumeric' | 'Byte' | 'Kanji';
284export interface Mode<TModeId extends ModeId = ModeId> {
285 id: TModeId;
286 bit: number;
287 ccBits: readonly number[];
288}
289
290export type GeneratedQRCodeSegment = NumericData | AlphanumericData | ByteData | KanjiData;
291
292export interface DataSegment {
293 getLength(): number;
294 getBitsLength(): number;
295}
296
297export interface NumericData extends DataSegment {
298 mode: Mode<'Numeric'>;
299 data: string;
300}
301
302export interface AlphanumericData extends DataSegment {
303 mode: Mode<'Alphanumeric'>;
304 data: string;
305}
306
307export interface ByteData extends DataSegment {
308 mode: Mode<'Byte'>;
309 data: Uint8Array;
310}
311
312export interface KanjiData extends DataSegment {
313 mode: Mode<'Kanji'>;
314 data: string;
315}
316
317/**
318 * Creates QR Code symbol and returns a qrcode object.
319 * @param text Text to encode or a list of objects describing segments.
320 */
321export function create(text: string | QRCodeSegment[], options?: QRCodeOptions): QRCode;
322
323/**
324 * Draws qr code symbol to canvas.
325 */
326export function toCanvas(
327 canvasElement: HTMLCanvasElement,
328 text: string | QRCodeSegment[],
329 callback: (error: Error | null | undefined) => void,
330): void;
331/**
332 * Draws qr code symbol to canvas.
333 */
334export function toCanvas(
335 canvasElement: HTMLCanvasElement,
336 text: string | QRCodeSegment[],
337 options?: QRCodeRenderersOptions,
338): Promise<void>;
339/**
340 * Draws qr code symbol to canvas.
341 */
342export function toCanvas(
343 canvasElement: HTMLCanvasElement,
344 text: string | QRCodeSegment[],
345 options: QRCodeRenderersOptions,
346 callback: (error: Error | null | undefined) => void,
347): void;
348/**
349 * Draws qr code symbol to canvas.
350 */
351export function toCanvas(
352 text: string | QRCodeSegment[],
353 callback: (error: Error | null | undefined, canvas: HTMLCanvasElement) => void,
354): void;
355/**
356 * Draws qr code symbol to canvas.
357 */
358export function toCanvas(text: string | QRCodeSegment[], options?: QRCodeRenderersOptions): Promise<HTMLCanvasElement>;
359/**
360 * Draws qr code symbol to canvas.
361 */
362export function toCanvas(
363 text: string | QRCodeSegment[],
364 options: QRCodeRenderersOptions,
365 callback: (error: Error | null | undefined, canvas: HTMLCanvasElement) => void,
366): void;
367/**
368 * Draws qr code symbol to node canvas.
369 */
370export function toCanvas(
371 canvas: any,
372 text: string | QRCodeSegment[],
373 callback: (error: Error | null | undefined) => void,
374): void;
375/**
376 * Draws qr code symbol to node canvas.
377 */
378export function toCanvas(canvas: any, text: string | QRCodeSegment[], options?: QRCodeRenderersOptions): Promise<void>;
379/**
380 * Draws qr code symbol to node canvas.
381 */
382export function toCanvas(
383 canvas: any,
384 text: string | QRCodeSegment[],
385 options: QRCodeRenderersOptions,
386 callback: (error: Error | null | undefined) => void,
387): void;
388
389/**
390 * Returns a Data URI containing a representation of the QR Code image.
391 */
392export function toDataURL(
393 canvasElement: HTMLCanvasElement,
394 text: string | QRCodeSegment[],
395 callback: (error: Error | null | undefined, url: string) => void,
396): void;
397/**
398 * Returns a Data URI containing a representation of the QR Code image.
399 */
400export function toDataURL(
401 canvasElement: HTMLCanvasElement,
402 text: string | QRCodeSegment[],
403 options?: QRCodeToDataURLOptions,
404): Promise<string>;
405/**
406 * Returns a Data URI containing a representation of the QR Code image.
407 */
408export function toDataURL(
409 canvasElement: HTMLCanvasElement,
410 text: string | QRCodeSegment[],
411 options: QRCodeToDataURLOptions,
412 callback: (error: Error | null | undefined, url: string) => void,
413): void;
414
415/**
416 * Returns a Data URI containing a representation of the QR Code image.
417 */
418export function toDataURL(
419 text: string | QRCodeSegment[],
420 callback: (error: Error | null | undefined, url: string) => void,
421): void;
422/**
423 * Returns a Data URI containing a representation of the QR Code image.
424 */
425export function toDataURL(text: string | QRCodeSegment[], options?: QRCodeToDataURLOptions): Promise<string>;
426/**
427 * Returns a Data URI containing a representation of the QR Code image.
428 */
429export function toDataURL(
430 text: string | QRCodeSegment[],
431 options: QRCodeToDataURLOptions,
432 callback: (error: Error | null | undefined, url: string) => void,
433): void;
434
435/**
436 * Returns a string representation of the QR Code.
437 */
438export function toString(
439 text: string | QRCodeSegment[],
440 callback: (error: Error | null | undefined, string: string) => void,
441): void;
442/**
443 * Returns a string representation of the QR Code.
444 */
445export function toString(text: string | QRCodeSegment[], options?: QRCodeToStringOptions): Promise<string>;
446/**
447 * Returns a string representation of the QR Code.
448 */
449export function toString(
450 text: string | QRCodeSegment[],
451 options: QRCodeToStringOptions,
452 callback: (error: Error | null | undefined, string: string) => void,
453): void;
454
455/**
456 * Saves QR Code to image file.
457 * If `options.type` is not specified, the format will be guessed from file extension.
458 */
459export function toFile(
460 path: string,
461 text: string | QRCodeSegment[],
462 callback: (error: Error | null | undefined) => void,
463): void;
464/**
465 * Saves QR Code to image file.
466 * If `options.type` is not specified, the format will be guessed from file extension.
467 */
468export function toFile(path: string, text: string | QRCodeSegment[], options?: QRCodeToFileOptions): Promise<void>;
469/**
470 * Saves QR Code to image file.
471 * If `options.type` is not specified, the format will be guessed from file extension.
472 */
473export function toFile(
474 path: string,
475 text: string | QRCodeSegment[],
476 options: QRCodeToFileOptions,
477 callback: (error: Error | null | undefined) => void,
478): void;
479
480/**
481 * Writes QR Code image to stream. Only works with png format for now.
482 */
483export function toFileStream(
484 stream: stream.Writable,
485 text: string | QRCodeSegment[],
486 callback: (error: Error | null | undefined) => void,
487): void;
488/**
489 * Writes QR Code image to stream. Only works with png format for now.
490 */
491export function toFileStream(
492 stream: stream.Writable,
493 text: string | QRCodeSegment[],
494 options?: QRCodeToFileStreamOptions,
495): Promise<void>;
496/**
497 * Writes QR Code image to stream. Only works with png format for now.
498 */
499export function toFileStream(
500 stream: stream.Writable,
501 text: string | QRCodeSegment[],
502 options: QRCodeToFileStreamOptions,
503 callback: (error: Error | null | undefined) => void,
504): void;
505
506/**
507 * Returns a Buffer containing a representation of the QR Code image. Only works with png format.
508 */
509export function toBuffer(
510 text: string | QRCodeSegment[],
511 callback: (error: Error | null | undefined, buffer: Buffer) => void,
512): void;
513/**
514 * Returns a Buffer containing a representation of the QR Code image. Only works with png format.
515 */
516export function toBuffer(text: string | QRCodeSegment[], options?: QRCodeToBufferOptions): Promise<Buffer>;
517/**
518 * Returns a Buffer containing a representation of the QR Code image. Only works with png format.
519 */
520export function toBuffer(
521 text: string | QRCodeSegment[],
522 options: QRCodeToBufferOptions,
523 callback: (error: Error | null | undefined, buffer: Buffer) => void,
524): void;