UNPKG

10.8 kBTypeScriptView Raw
1// Type definitions for qrcode 1.4
2// Project: http://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// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
7// TypeScript Version: 2.1
8
9/// <reference types="node" />
10
11import * as stream from 'stream';
12
13export type QRCodeErrorCorrectionLevel = 'low' | 'medium' | 'quartile' | 'high' | 'L' | 'M' | 'Q' | 'H';
14
15export interface QRCodeOptions {
16 /**
17 * QR Code version. If not specified the more suitable value will be calculated.
18 */
19 version?: number;
20 /**
21 * Error correction level.
22 * Possible values are low, medium, quartile, high or L, M, Q, H.
23 * Default: M
24 */
25 errorCorrectionLevel?: QRCodeErrorCorrectionLevel;
26 /**
27 * Helper function used internally to convert a kanji to its Shift JIS value.
28 * Provide this function if you need support for Kanji mode.
29 */
30 toSJISFunc?: (codePoint: string) => number;
31}
32
33export interface QRCodeToDataURLOptions extends QRCodeRenderersOptions {
34 /**
35 * Data URI format.
36 * Default: image/png
37 */
38 type?: 'image/png' | 'image/jpeg' | 'image/webp';
39 rendererOpts?: {
40 /**
41 * A Number between 0 and 1 indicating image quality if the requested type is image/jpeg or image/webp.
42 * Default: 0.92
43 */
44 quality?: number;
45 };
46}
47
48export interface QRCodeToStringOptions extends QRCodeRenderersOptions {
49 /**
50 * Output format.
51 * Default: utf8
52 */
53 type?: 'utf8' | 'svg' | 'terminal';
54}
55
56export interface QRCodeToFileOptions extends QRCodeRenderersOptions {
57 /**
58 * Output format.
59 * Default: png
60 */
61 type?: 'png' | 'svg' | 'utf8';
62 rendererOpts?: {
63 /**
64 * Compression level for deflate.
65 * Default: 9
66 */
67 deflateLevel?: number;
68 /**
69 * Compression strategy for deflate.
70 * Default: 3
71 */
72 deflateStrategy?: number;
73 };
74}
75
76export interface QRCodeToFileStreamOptions extends QRCodeRenderersOptions {
77 /**
78 * Output format. Only png supported for file stream
79 */
80 type?: 'png';
81 rendererOpts?: {
82 /**
83 * Compression level for deflate.
84 * Default: 9
85 */
86 deflateLevel?: number;
87 /**
88 * Compression strategy for deflate.
89 * Default: 3
90 */
91 deflateStrategy?: number;
92 };
93}
94
95export interface QRCodeToBufferOptions extends QRCodeRenderersOptions {
96 /**
97 * Output format. Only png supported for Buffer.
98 */
99 type?: 'png';
100 rendererOpts?: {
101 /**
102 * Compression level for deflate.
103 * Default: 9
104 */
105 deflateLevel?: number;
106 /**
107 * Compression strategy for deflate.
108 * Default: 3
109 */
110 deflateStrategy?: number;
111 };
112}
113
114export interface QRCodeRenderersOptions extends QRCodeOptions {
115 /**
116 * Define how much wide the quiet zone should be.
117 * Default: 4
118 */
119 margin?: number;
120 /**
121 * Scale factor. A value of 1 means 1px per modules (black dots).
122 * Default: 4
123 */
124 scale?: number;
125 /**
126 * Forces a specific width for the output image.
127 * If width is too small to contain the qr symbol, this option will be ignored.
128 * Takes precedence over scale.
129 */
130 width?: number;
131 color?: {
132 /**
133 * Color of dark module. Value must be in hex format (RGBA).
134 * Note: dark color should always be darker than color.light.
135 * Default: #000000ff
136 */
137 dark?: string;
138 /**
139 * Color of light module. Value must be in hex format (RGBA).
140 * Default: #ffffffff
141 */
142 light?: string;
143 };
144}
145
146export interface QRCodeSegment {
147 data: string | Buffer | Uint8ClampedArray;
148 mode: 'alphanumeric' | 'numeric' | 'kanji' | 'byte';
149}
150
151export interface QRCode {
152 /**
153 * Bitmatrix class with modules data
154 */
155 modules: any;
156 /**
157 * Calculated QR Code version
158 */
159 version: number;
160 /**
161 * Error Correction Level
162 */
163 errorCorrectionLevel: number;
164 /**
165 * Calculated Mask pattern
166 */
167 maskPattern: any;
168 /**
169 * Generated segments
170 */
171 segments: QRCodeSegment[];
172}
173
174/**
175 * Creates QR Code symbol and returns a qrcode object.
176 */
177export function create(text: string | QRCodeSegment[], options: QRCodeOptions): QRCode;
178
179/**
180 * Draws qr code symbol to canvas.
181 */
182export function toCanvas(
183 canvasElement: HTMLCanvasElement,
184 text: string | QRCodeSegment[],
185 callback: (error: Error) => void,
186): void;
187/**
188 * Draws qr code symbol to canvas.
189 */
190export function toCanvas(
191 canvasElement: HTMLCanvasElement,
192 text: string | QRCodeSegment[],
193 options?: QRCodeRenderersOptions,
194): Promise<any>;
195/**
196 * Draws qr code symbol to canvas.
197 */
198export function toCanvas(
199 canvasElement: HTMLCanvasElement,
200 text: string | QRCodeSegment[],
201 options: QRCodeRenderersOptions,
202 callback: (error: Error) => void,
203): void;
204/**
205 * Draws qr code symbol to canvas.
206 */
207export function toCanvas(
208 text: string | QRCodeSegment[],
209 callback: (error: Error, canvas: HTMLCanvasElement) => void,
210): void;
211/**
212 * Draws qr code symbol to canvas.
213 */
214export function toCanvas(text: string | QRCodeSegment[], options?: QRCodeRenderersOptions): Promise<any>;
215/**
216 * Draws qr code symbol to canvas.
217 */
218export function toCanvas(
219 text: string | QRCodeSegment[],
220 options: QRCodeRenderersOptions,
221 callback: (error: Error, canvas: HTMLCanvasElement) => void,
222): void;
223/**
224 * Draws qr code symbol to node canvas.
225 */
226export function toCanvas(canvas: any, text: string | QRCodeSegment[], callback: (error: Error) => void): void;
227/**
228 * Draws qr code symbol to node canvas.
229 */
230export function toCanvas(canvas: any, text: string | QRCodeSegment[], options?: QRCodeRenderersOptions): Promise<any>;
231/**
232 * Draws qr code symbol to node canvas.
233 */
234export function toCanvas(
235 canvas: any,
236 text: string | QRCodeSegment[],
237 options: QRCodeRenderersOptions,
238 callback: (error: Error) => void,
239): void;
240
241/**
242 * Returns a Data URI containing a representation of the QR Code image.
243 */
244export function toDataURL(
245 canvasElement: HTMLCanvasElement,
246 text: string | QRCodeSegment[],
247 callback: (error: Error, url: string) => void,
248): void;
249/**
250 * Returns a Data URI containing a representation of the QR Code image.
251 */
252export function toDataURL(
253 canvasElement: HTMLCanvasElement,
254 text: string | QRCodeSegment[],
255 options?: QRCodeToDataURLOptions,
256): Promise<string>;
257/**
258 * Returns a Data URI containing a representation of the QR Code image.
259 */
260export function toDataURL(
261 canvasElement: HTMLCanvasElement,
262 text: string | QRCodeSegment[],
263 options: QRCodeToDataURLOptions,
264 callback: (error: Error, url: string) => void,
265): void;
266
267/**
268 * Returns a Data URI containing a representation of the QR Code image.
269 */
270export function toDataURL(text: string | QRCodeSegment[], callback: (error: Error, url: string) => void): void;
271/**
272 * Returns a Data URI containing a representation of the QR Code image.
273 */
274export function toDataURL(text: string | QRCodeSegment[], options?: QRCodeToDataURLOptions): Promise<string>;
275/**
276 * Returns a Data URI containing a representation of the QR Code image.
277 */
278export function toDataURL(
279 text: string | QRCodeSegment[],
280 options: QRCodeToDataURLOptions,
281 callback: (error: Error, url: string) => void,
282): void;
283
284/**
285 * Returns a string representation of the QR Code.
286 * If choosen output format is svg it will returns a string containing xml code.
287 */
288export function toString(text: string | QRCodeSegment[], callback: (error: Error, string: string) => void): void;
289/**
290 * Returns a string representation of the QR Code.
291 * If choosen output format is svg it will returns a string containing xml code.
292 */
293export function toString(text: string | QRCodeSegment[], options?: QRCodeToStringOptions): Promise<string>;
294/**
295 * Returns a string representation of the QR Code.
296 * If choosen output format is svg it will returns a string containing xml code.
297 */
298export function toString(
299 text: string | QRCodeSegment[],
300 options: QRCodeToStringOptions,
301 callback: (error: Error, string: string) => void,
302): void;
303
304/**
305 * Saves QR Code to image file.
306 * If options.type is not specified, the format will be guessed from file extension.
307 * Recognized extensions are png, svg, txt.
308 */
309export function toFile(path: string, text: string | QRCodeSegment[], callback: (error: Error) => void): void;
310/**
311 * Saves QR Code to image file.
312 * If options.type is not specified, the format will be guessed from file extension.
313 * Recognized extensions are png, svg, txt.
314 */
315export function toFile(path: string, text: string | QRCodeSegment[], options?: QRCodeToFileOptions): Promise<any>;
316/**
317 * Saves QR Code to image file.
318 * If options.type is not specified, the format will be guessed from file extension.
319 * Recognized extensions are png, svg, txt.
320 */
321export function toFile(
322 path: string,
323 text: string | QRCodeSegment[],
324 options: QRCodeToFileOptions,
325 callback: (error: Error) => void,
326): void;
327
328/**
329 * Writes QR Code image to stream. Only works with png format for now.
330 */
331export function toFileStream(
332 stream: stream.Writable,
333 text: string | QRCodeSegment[],
334 callback: (error: Error) => void,
335): void;
336/**
337 * Writes QR Code image to stream. Only works with png format for now.
338 */
339export function toFileStream(
340 stream: stream.Writable,
341 text: string | QRCodeSegment[],
342 options?: QRCodeToFileStreamOptions,
343): Promise<any>;
344/**
345 * Writes QR Code image to stream. Only works with png format for now.
346 */
347export function toFileStream(
348 stream: stream.Writable,
349 text: string | QRCodeSegment[],
350 options: QRCodeToFileStreamOptions,
351 callback: (error: Error) => void,
352): void;
353
354/**
355 * Returns a Buffer containing a representation of the QR Code image. Only works with png format.
356 */
357export function toBuffer(text: string | QRCodeSegment[], callback: (error: Error, buffer: Buffer) => void): void;
358/**
359 * Returns a Buffer containing a representation of the QR Code image. Only works with png format.
360 */
361export function toBuffer(text: string | QRCodeSegment[], options?: QRCodeToBufferOptions): Promise<Buffer>;
362/**
363 * Returns a Buffer containing a representation of the QR Code image. Only works with png format.
364 */
365export function toBuffer(
366 text: string | QRCodeSegment[],
367 options: QRCodeToBufferOptions,
368 callback: (error: Error, buffer: Buffer) => void,
369): void;