Merge pull request #16721 from plantain-00/qrcode

[qrcode]improve types of qrcode@0.8.2
This commit is contained in:
Yui
2017-06-01 08:33:37 -07:00
committed by GitHub
2 changed files with 243 additions and 33 deletions

View File

@@ -1,35 +1,203 @@
// Type definitions for qrcode
// Type definitions for qrcode 0.8.2
// Project: https://github.com/soldair/node-qrcode
// Definitions by: York Yao <https://github.com/plantain-00/>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
type QRCodeOptions = {
errorCorrectLevel?: "minimum" | "medium" | "high" | "max";
/// <reference types="node" />
import * as stream from "stream";
export interface QRCodeOptions {
/**
* QR Code version. If not specified the more suitable value will be calculated.
*/
version?: number;
/**
* Error correction level.
* Possible values are low, medium, quartile, high or L, M, Q, H.
* Default: M
*/
errorCorrectionLevel?: "low" | "medium" | "quartile" | "high" | "L" | "M" | "Q" | "H";
/**
* Helper function used internally to convert a kanji to its Shift JIS value.
* Provide this function if you need support for Kanji mode.
*/
toSJISFunc?: Function;
}
declare module "qrcode" {
var qrcode: {
toDataURL(text: string, callback: (error: Error, dataURL: string) => void): void;
toDataURL(text: string, options: QRCodeOptions, callback: (error: Error, dataURL: string) => void): void;
draw(text: string, callback: (error: Error, canvas: HTMLCanvasElement) => void): void;
draw(text: string, options: QRCodeOptions, callback: (error: Error, canvas: HTMLCanvasElement) => void): void;
drawSvg(text: string, callback: (error: Error, svgString: string) => void): void;
drawSvg(text: string, options: QRCodeOptions, callback: (error: Error, svgString: string) => void): void;
save(path: string, text: string, callback: (error: Error, written: any) => void): void;
save(path: string, text: string, options: QRCodeOptions, callback: (error: Error, written: any) => void): void;
drawText(text: string, callback: (error: Error) => void): void;
drawText(text: string, options: QRCodeOptions, callback: (error: Error) => void): void;
drawBitArray(text: string, callback: (error: Error, bits: any, width: number) => void): void;
drawBitArray(text: string, options: QRCodeOptions, callback: (error: Error, bits: any, width: number) => void): void;
export interface QRCodeToDataURLOptions extends QRCodeRenderersOptions {
/**
* Data URI format.
* Default: image/png
*/
type?: "image/png" | "image/jpeg" | "image/webp";
rendererOpts?: {
/**
* A Number between 0 and 1 indicating image quality if the requested type is image/jpeg or image/webp.
* Default: 0.92
*/
quality?: number;
};
export = qrcode;
}
declare var QRCodeLib: {
QRCodeDraw: {
new (): {
draw(element: HTMLCanvasElement, text: string, callback: (error: Error, canvas: HTMLCanvasElement) => void): void;
draw(element: HTMLCanvasElement, text: string, options: QRCodeOptions, callback: (error: Error, canvas: HTMLCanvasElement) => void): void;
}
export interface QRCodeToStringOptions extends QRCodeOptions {
/**
* Output format.
* Default: utf8
*/
type?: "utf8" | "svg" | "terminal";
}
export interface QRCodeToFileOptions extends QRCodeRenderersOptions {
/**
* Output format.
* Default: png
*/
type?: "png" | "svg" | "utf8";
rendererOpts?: {
/**
* Compression level for deflate.
* Default: 9
*/
deflateLevel?: number;
/**
* Compression strategy for deflate.
* Default: 3
*/
deflateStrategy?: number;
};
};
}
export interface QRCodeRenderersOptions extends QRCodeOptions {
/**
* Define how much wide the quiet zone should be.
* Default: 4
*/
margin?: number;
/**
* Scale factor. A value of 1 means 1px per modules (black dots).
* Default: 4
*/
scale?: number;
color?: {
/**
* Color of dark module. Value must be in hex format (RGBA).
* Note: dark color should always be darker than color.light.
* Default: #000000ff
*/
dark?: string;
/**
* Color of light module. Value must be in hex format (RGBA).
* Default: #ffffffff
*/
light?: string;
};
}
export interface QRCodeSegment {
data: string;
mode: 'alphanumeric' | 'numeric';
}
export interface QRCode {
/**
* Bitmatrix class with modules data
*/
modules: any;
/**
* Calculated QR Code version
*/
version: number;
/**
* Error Correction Level
*/
errorCorrectionLevel: number;
/**
* Calculated Mask pattern
*/
maskPattern: any;
/**
* Generated segments
*/
segments: QRCodeSegment[];
}
/**
* Creates QR Code symbol and returns a qrcode object.
*/
export function create(text: string | QRCodeSegment[], options: QRCodeOptions): QRCode;
/**
* Draws qr code symbol to canvas.
*/
export function toCanvas(canvasElement: HTMLCanvasElement, text: string | QRCodeSegment[], callback: (error: Error) => void): void;
/**
* Draws qr code symbol to canvas.
*/
export function toCanvas(canvasElement: HTMLCanvasElement, text: string | QRCodeSegment[], options: QRCodeOptions, callback: (error: Error) => void): void;
/**
* Draws qr code symbol to canvas.
*/
export function toCanvas(text: string | QRCodeSegment[], callback: (error: Error, canvas: HTMLCanvasElement) => void): void;
/**
* Draws qr code symbol to canvas.
*/
export function toCanvas(text: string | QRCodeSegment[], options: QRCodeOptions, callback: (error: Error, canvas: HTMLCanvasElement) => void): void;
/**
* Draws qr code symbol to node canvas.
*/
export function toCanvas(canvas: any, text: string | QRCodeSegment[], callback: (error: Error) => void): void;
/**
* Draws qr code symbol to node canvas.
*/
export function toCanvas(canvas: any, text: string | QRCodeSegment[], options: QRCodeOptions, callback: (error: Error) => void): void;
/**
* Returns a Data URI containing a representation of the QR Code image.
*/
export function toDataURL(canvasElement: HTMLCanvasElement, text: string | QRCodeSegment[], callback: (error: Error, url: string) => void): void;
/**
* Returns a Data URI containing a representation of the QR Code image.
*/
export function toDataURL(canvasElement: HTMLCanvasElement, text: string | QRCodeSegment[], options: QRCodeToDataURLOptions, callback: (error: Error, url: string) => void): void;
/**
* Returns a Data URI containing a representation of the QR Code image.
*/
export function toDataURL(text: string | QRCodeSegment[], callback: (error: Error, url: string) => void): void;
/**
* Returns a Data URI containing a representation of the QR Code image.
*/
export function toDataURL(text: string | QRCodeSegment[], options: QRCodeToDataURLOptions, callback: (error: Error, url: string) => void): void;
/**
* Returns a string representation of the QR Code.
* If choosen output format is svg it will returns a string containing xml code.
*/
export function toString(text: string | QRCodeSegment[], callback: (error: Error, string: string) => void): void;
/**
* Returns a string representation of the QR Code.
* If choosen output format is svg it will returns a string containing xml code.
*/
export function toString(text: string | QRCodeSegment[], options: QRCodeToStringOptions, callback: (error: Error, string: string) => void): void;
/**
* Saves QR Code to image file.
* If options.type is not specified, the format will be guessed from file extension.
* Recognized extensions are png, svg, txt.
*/
export function toFile(path: string, text: string | QRCodeSegment[], callback: (error: Error) => void): void;
/**
* Saves QR Code to image file.
* If options.type is not specified, the format will be guessed from file extension.
* Recognized extensions are png, svg, txt.
*/
export function toFile(path: string, text: string | QRCodeSegment[], options: QRCodeToFileOptions, callback: (error: Error) => void): void;
/**
* Writes QR Code image to stream. Only works with png format for now.
*/
export function toFileStream(stream: stream.Writable, text: string | QRCodeSegment[], callback: (error: Error) => void): void;
/**
* Writes QR Code image to stream. Only works with png format for now.
*/
export function toFileStream(stream: stream.Writable, text: string | QRCodeSegment[], options: QRCodeOptions, callback: (error: Error) => void): void;

View File

@@ -1,14 +1,56 @@
import * as QRCode from 'qrcode';
QRCode.toDataURL('i am a pony!', function (err, url) {
const canvas = document.getElementById('canvas') as HTMLCanvasElement;
QRCode.toCanvas(canvas, 'sample text', function (error) {
if (error) console.error(error)
console.log('success!');
});
QRCode.toDataURL('I am a pony!', function (err, url) {
console.log(url);
});
var qrcodedraw = new QRCodeLib.QRCodeDraw();
qrcodedraw.draw(document.getElementById('test') as HTMLCanvasElement, "this text will be in the code!", function (error, canvas) {
if (error) {
return console.log('Error =( ', error);
}
console.log('success!');
QRCode.toDataURL('some text', { errorCorrectionLevel: 'H' }, function (err, url) {
console.log(url);
});
QRCode.toDataURL('some text', { version: 2 }, function (err, url) {
console.log(url);
});
QRCode.toDataURL([
{ data: 'ABCDEFG', mode: 'alphanumeric' },
{ data: '0123456', mode: 'numeric' }
], function (err, url) {
console.log(url);
});
QRCode.toCanvas('text', { errorCorrectionLevel: 'H' }, function (err, canvas) {
if (err) throw err;
});
QRCode.toDataURL('text', {
errorCorrectionLevel: 'H',
type: 'image/jpeg',
rendererOpts: {
quality: 0.3
}
}, function (err, url) {
if (err) throw err;
});
QRCode.toString('http://www.google.com', function (err, string) {
if (err) throw err;
console.log(string);
});
QRCode.toFile('path/to/filename.png', 'Some text', {
color: {
dark: '#00F', // Blue dots
light: '#0000' // Transparent background
}
}, function (err) {
if (err) throw err;
console.log('done');
});