Skip to content

Commit fea56cd

Browse files
gvanderestsheetalkamat
authored andcommitted
bwip-js - Add Browser implementation of the BwipJs function (DefinitelyTyped#37548)
* Add Browser implementation of the BwipJs function * Use $ExpectType in unit test for types
1 parent 8266be6 commit fea56cd

File tree

3 files changed

+33
-3
lines changed

3 files changed

+33
-3
lines changed

types/bwip-js/bwip-js-tests.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,16 @@ bwipjs.toBuffer({
3737
// png.readUInt32BE(20) : PNG image height
3838
}
3939
});
40+
41+
// Browser canvas implementation
42+
const canvas = document.createElement('canvas') as HTMLCanvasElement;
43+
bwipjs(canvas, {
44+
bcid: 'qrcode',
45+
text: 'example',
46+
}, (err?: string | Error, cvs?: HTMLCanvasElement): void => {
47+
if (err) {
48+
err; // $ExpectType string | Error
49+
} else if (cvs) {
50+
cvs; // $ExpectType HTMLCanvasElement
51+
}
52+
});

types/bwip-js/index.d.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
// Type definitions for bwip-js 1.1.1
1+
// Type definitions for bwip-js 1.7.3
22
// Project: https://github.com/metafloor/bwip-js
33
// Definitions by: TANAKA Koichi <https://github.com/MugeSo>
4+
// Guillaume VanderEst <https://github.com/gvanderest>
45
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
56

67
/// <reference types="node" />
@@ -80,4 +81,19 @@ declare namespace BwipJs {
8081

8182
declare function BwipJs(req: Request, res: Response, opts?:BwipJs.ToBufferOptions): void;
8283

84+
/**
85+
* The Browser version of the library's functionality, which makes use of an HTMLCanvasElement for rendering.
86+
* @param canvas ID string or HTML element of the canvas to render within
87+
* @param opts Options to use for rendering
88+
* @param callback Function to execute when rendering has completed or failed
89+
*/
90+
declare function BwipJs(
91+
canvas: string | HTMLCanvasElement,
92+
opts:BwipJs.ToBufferOptions,
93+
callback: (
94+
err: undefined | string | Error,
95+
canvas?: HTMLCanvasElement,
96+
) => void,
97+
): void;
98+
8399
export = BwipJs;

types/bwip-js/tsconfig.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
"compilerOptions": {
33
"module": "commonjs",
44
"lib": [
5-
"es6"
5+
"es6",
6+
"dom"
67
],
78
"noImplicitAny": true,
89
"noImplicitThis": true,
@@ -20,4 +21,4 @@
2021
"index.d.ts",
2122
"bwip-js-tests.ts"
2223
]
23-
}
24+
}

0 commit comments

Comments
 (0)