UNPKG

6.95 kBTypeScriptView Raw
1// Type definitions for qrcode 0.8.2
2// Project: https://github.com/soldair/node-qrcode
3// Definitions by: York Yao <https://github.com/plantain-00/>
4// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
5
6/// <reference types="node" />
7
8import * as stream from "stream";
9
10export interface QRCodeOptions {
11 /**
12 * QR Code version. If not specified the more suitable value will be calculated.
13 */
14 version?: number;
15 /**
16 * Error correction level.
17 * Possible values are low, medium, quartile, high or L, M, Q, H.
18 * Default: M
19 */
20 errorCorrectionLevel?: "low" | "medium" | "quartile" | "high" | "L" | "M" | "Q" | "H";
21 /**
22 * Helper function used internally to convert a kanji to its Shift JIS value.
23 * Provide this function if you need support for Kanji mode.
24 */
25 toSJISFunc?: Function;
26}
27
28export interface QRCodeToDataURLOptions extends QRCodeRenderersOptions {
29 /**
30 * Data URI format.
31 * Default: image/png
32 */
33 type?: "image/png" | "image/jpeg" | "image/webp";
34 rendererOpts?: {
35 /**
36 * A Number between 0 and 1 indicating image quality if the requested type is image/jpeg or image/webp.
37 * Default: 0.92
38 */
39 quality?: number;
40 };
41}
42
43export interface QRCodeToStringOptions extends QRCodeOptions {
44 /**
45 * Output format.
46 * Default: utf8
47 */
48 type?: "utf8" | "svg" | "terminal";
49}
50
51export interface QRCodeToFileOptions extends QRCodeRenderersOptions {
52 /**
53 * Output format.
54 * Default: png
55 */
56 type?: "png" | "svg" | "utf8";
57 rendererOpts?: {
58 /**
59 * Compression level for deflate.
60 * Default: 9
61 */
62 deflateLevel?: number;
63 /**
64 * Compression strategy for deflate.
65 * Default: 3
66 */
67 deflateStrategy?: number;
68 };
69}
70
71export interface QRCodeRenderersOptions extends QRCodeOptions {
72 /**
73 * Define how much wide the quiet zone should be.
74 * Default: 4
75 */
76 margin?: number;
77 /**
78 * Scale factor. A value of 1 means 1px per modules (black dots).
79 * Default: 4
80 */
81 scale?: number;
82 color?: {
83 /**
84 * Color of dark module. Value must be in hex format (RGBA).
85 * Note: dark color should always be darker than color.light.
86 * Default: #000000ff
87 */
88 dark?: string;
89 /**
90 * Color of light module. Value must be in hex format (RGBA).
91 * Default: #ffffffff
92 */
93 light?: string;
94 };
95}
96
97export interface QRCodeSegment {
98 data: string;
99 mode: 'alphanumeric' | 'numeric';
100}
101
102export interface QRCode {
103 /**
104 * Bitmatrix class with modules data
105 */
106 modules: any;
107 /**
108 * Calculated QR Code version
109 */
110 version: number;
111 /**
112 * Error Correction Level
113 */
114 errorCorrectionLevel: number;
115 /**
116 * Calculated Mask pattern
117 */
118 maskPattern: any;
119 /**
120 * Generated segments
121 */
122 segments: QRCodeSegment[];
123}
124
125/**
126 * Creates QR Code symbol and returns a qrcode object.
127 */
128export function create(text: string | QRCodeSegment[], options: QRCodeOptions): QRCode;
129
130/**
131 * Draws qr code symbol to canvas.
132 */
133export function toCanvas(canvasElement: HTMLCanvasElement, text: string | QRCodeSegment[], callback: (error: Error) => void): void;
134/**
135 * Draws qr code symbol to canvas.
136 */
137export function toCanvas(canvasElement: HTMLCanvasElement, text: string | QRCodeSegment[], options: QRCodeOptions, callback: (error: Error) => void): void;
138/**
139 * Draws qr code symbol to canvas.
140 */
141export function toCanvas(text: string | QRCodeSegment[], callback: (error: Error, canvas: HTMLCanvasElement) => void): void;
142/**
143 * Draws qr code symbol to canvas.
144 */
145export function toCanvas(text: string | QRCodeSegment[], options: QRCodeOptions, callback: (error: Error, canvas: HTMLCanvasElement) => void): void;
146/**
147 * Draws qr code symbol to node canvas.
148 */
149export function toCanvas(canvas: any, text: string | QRCodeSegment[], callback: (error: Error) => void): void;
150/**
151 * Draws qr code symbol to node canvas.
152 */
153export function toCanvas(canvas: any, text: string | QRCodeSegment[], options: QRCodeOptions, callback: (error: Error) => void): void;
154
155/**
156 * Returns a Data URI containing a representation of the QR Code image.
157 */
158export function toDataURL(canvasElement: HTMLCanvasElement, text: string | QRCodeSegment[], callback: (error: Error, url: string) => void): void;
159/**
160 * Returns a Data URI containing a representation of the QR Code image.
161 */
162export function toDataURL(canvasElement: HTMLCanvasElement, text: string | QRCodeSegment[], options: QRCodeToDataURLOptions, callback: (error: Error, url: string) => void): void;
163/**
164 * Returns a Data URI containing a representation of the QR Code image.
165 */
166export function toDataURL(text: string | QRCodeSegment[], callback: (error: Error, url: string) => void): void;
167/**
168 * Returns a Data URI containing a representation of the QR Code image.
169 */
170export function toDataURL(text: string | QRCodeSegment[], options: QRCodeToDataURLOptions, callback: (error: Error, url: string) => void): void;
171
172/**
173 * Returns a string representation of the QR Code.
174 * If choosen output format is svg it will returns a string containing xml code.
175 */
176export function toString(text: string | QRCodeSegment[], callback: (error: Error, string: string) => void): void;
177/**
178 * Returns a string representation of the QR Code.
179 * If choosen output format is svg it will returns a string containing xml code.
180 */
181export function toString(text: string | QRCodeSegment[], options: QRCodeToStringOptions, callback: (error: Error, string: string) => void): void;
182
183/**
184 * Saves QR Code to image file.
185 * If options.type is not specified, the format will be guessed from file extension.
186 * Recognized extensions are png, svg, txt.
187 */
188export function toFile(path: string, text: string | QRCodeSegment[], callback: (error: Error) => void): void;
189/**
190 * Saves QR Code to image file.
191 * If options.type is not specified, the format will be guessed from file extension.
192 * Recognized extensions are png, svg, txt.
193 */
194export function toFile(path: string, text: string | QRCodeSegment[], options: QRCodeToFileOptions, callback: (error: Error) => void): void;
195
196/**
197 * Writes QR Code image to stream. Only works with png format for now.
198 */
199export function toFileStream(stream: stream.Writable, text: string | QRCodeSegment[], callback: (error: Error) => void): void;
200/**
201 * Writes QR Code image to stream. Only works with png format for now.
202 */
203export function toFileStream(stream: stream.Writable, text: string | QRCodeSegment[], options: QRCodeOptions, callback: (error: Error) => void): void;