bwip-js - Add Browser implementation of the BwipJs function (#37548)

* Add Browser implementation of the BwipJs function

* Use $ExpectType in unit test for types
This commit is contained in:
Guillaume VanderEst 2019-08-20 11:22:57 -07:00 committed by Sheetal Nandi
parent 8266be6d79
commit fea56cdd61
3 changed files with 33 additions and 3 deletions

View File

@ -37,3 +37,16 @@ bwipjs.toBuffer({
// png.readUInt32BE(20) : PNG image height
}
});
// Browser canvas implementation
const canvas = document.createElement('canvas') as HTMLCanvasElement;
bwipjs(canvas, {
bcid: 'qrcode',
text: 'example',
}, (err?: string | Error, cvs?: HTMLCanvasElement): void => {
if (err) {
err; // $ExpectType string | Error
} else if (cvs) {
cvs; // $ExpectType HTMLCanvasElement
}
});

View File

@ -1,6 +1,7 @@
// Type definitions for bwip-js 1.1.1
// Type definitions for bwip-js 1.7.3
// Project: https://github.com/metafloor/bwip-js
// Definitions by: TANAKA Koichi <https://github.com/MugeSo>
// Guillaume VanderEst <https://github.com/gvanderest>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
/// <reference types="node" />
@ -80,4 +81,19 @@ declare namespace BwipJs {
declare function BwipJs(req: Request, res: Response, opts?:BwipJs.ToBufferOptions): void;
/**
* The Browser version of the library's functionality, which makes use of an HTMLCanvasElement for rendering.
* @param canvas ID string or HTML element of the canvas to render within
* @param opts Options to use for rendering
* @param callback Function to execute when rendering has completed or failed
*/
declare function BwipJs(
canvas: string | HTMLCanvasElement,
opts:BwipJs.ToBufferOptions,
callback: (
err: undefined | string | Error,
canvas?: HTMLCanvasElement,
) => void,
): void;
export = BwipJs;

View File

@ -2,7 +2,8 @@
"compilerOptions": {
"module": "commonjs",
"lib": [
"es6"
"es6",
"dom"
],
"noImplicitAny": true,
"noImplicitThis": true,
@ -20,4 +21,4 @@
"index.d.ts",
"bwip-js-tests.ts"
]
}
}