From 279baba3d4dffdad7213a14b875b87a739f79a56 Mon Sep 17 00:00:00 2001 From: Ikramullah Latif Date: Fri, 16 Oct 2020 15:58:42 +0800 Subject: [PATCH 1/3] Fixing typings declaration and add example in typescript --- package-lock.json | 6 ++++++ package.json | 4 +++- tsconfig.json | 4 ++-- typings/index.d.ts | 50 +++++++++++++++++++++++++++++++++++++++------- typings/index.ts | 21 +++++++++++++++++++ 5 files changed, 75 insertions(+), 10 deletions(-) create mode 100644 typings/index.ts diff --git a/package-lock.json b/package-lock.json index b8156a8..1f2016a 100644 --- a/package-lock.json +++ b/package-lock.json @@ -10,6 +10,12 @@ "integrity": "sha512-MggwidiH+E9j5Sh8pbrX5sJvMcsqS5o+7iB42M9/k0CD63MjYbdP4nhSh7uB5wnv2/RVzTZFTxzF/kIa5mrCqA==", "dev": true }, + "@types/node": { + "version": "14.11.8", + "resolved": "https://registry.npmjs.org/@types/node/-/node-14.11.8.tgz", + "integrity": "sha512-KPcKqKm5UKDkaYPTuXSx8wEP7vE9GnuaXIZKijwRYcePpZFDVuy2a57LarFKiORbHOuTOOwYzxVxcUzsh2P2Pw==", + "dev": true + }, "abbrev": { "version": "1.1.1", "resolved": "https://registry.npmjs.org/abbrev/-/abbrev-1.1.1.tgz", diff --git a/package.json b/package.json index d321f55..bba220f 100644 --- a/package.json +++ b/package.json @@ -32,6 +32,7 @@ "canvas": "*" }, "devDependencies": { + "@types/node": "^14.11.8", "jsdoc": "^3.6.5", "jsdoc-skyceil": "^1.0.5", "typescript": "^4.0.2" @@ -39,5 +40,6 @@ "directories": { "doc": "docs", "example": "examples" - } + }, + "types": "./typings/index.d.ts" } diff --git a/tsconfig.json b/tsconfig.json index ae4cda0..1ea718c 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -14,9 +14,9 @@ "esnext.asynciterable", "esnext.intl", "esnext.symbol" - ], + ], "sourceMap": false, "skipDefaultLibCheck": true }, - "include": ["index.js", "src/"] + "exclude": [".github/", "aseets/", "docs/", "examples/", "node_modules/"] } \ No newline at end of file diff --git a/typings/index.d.ts b/typings/index.d.ts index 83a539c..7b9b529 100644 --- a/typings/index.d.ts +++ b/typings/index.d.ts @@ -1,7 +1,10 @@ -import { Image } from "canvas"; - declare module "captcha-canvas" { + import { Image } from "canvas"; export const version: string; + + /** + * Initatiates the creation of captcha image generation. + */ export class CaptchaGenerator { constructor(options?: {height?: number, width?: number}) public height: number; @@ -10,12 +13,45 @@ declare module "captcha-canvas" { public trace: SetTraceOptions; public decoy: SetDecoyOptions; - public setBackgroud(image?: string); - public setDecoy(SetDecoyOptions); - public setTrace(SetTraceOptions); - public setCaptcha(SetCaptchaOptions); - public setDimension(height?: number, width?: number); + /** + * Get the text of captcha. + */ + public text: string; + + /** + * Set background for captcha image. + * @param image Buffer/url/path of image. + */ + public setBackgroud(image: Buffer | string): CaptchaGenerator; + /** + * Change decoy options + * @param options Decoy characters customisation options + */ + public setDecoy(options: SetDecoyOptions): CaptchaGenerator; + /** + * Change trace creation options. + * @param options Trace Line appearance options. + */ + public setTrace(options: SetTraceOptions): CaptchaGenerator; + /** + * Change captcha text options + * @param options Captcha appearance options. + */ + public setCaptcha(options: SetCaptchaOptions): CaptchaGenerator; + /** + * set dimension for your captcha image + * @param height Height of captcha image. + * @param width Width of captcha image. + */ + public setDimension(height: number, width: number): CaptchaGenerator; + /** + * Method which returns image buffer + */ public generate(): Promise; + /** + * Non asynchronous method to generate captcha image. + * @description It do not use setBackground method value for background image. If you want to set background and also use generateSync method then use background option in genrateSync method. + */ public generateSync(options?: {background?: Image}): Buffer; } interface SetCaptchaOptions { diff --git a/typings/index.ts b/typings/index.ts new file mode 100644 index 0000000..969e1cf --- /dev/null +++ b/typings/index.ts @@ -0,0 +1,21 @@ +/// + +import { CaptchaGenerator } from 'captcha-canvas'; + +/** + * This code from documentation, for typing check. + */ +const captcha = new CaptchaGenerator() + .setDimension(150, 450) + .setCaptcha({ text: 'CUSTOM05', size: 60, color: 'deeppink' }) + .setDecoy({ opacity: 0.5 }); + +// Get captcha text +console.log(captcha.text); + +// For some reason, you'll want to generate synchronously +console.log(captcha.generateSync()); + +// For some reason, you'll want to generate asynchronously +captcha.generate() + .then(buffer => console.log(buffer)); From 2c5e8167abe450926e547546c21af1825a24f51f Mon Sep 17 00:00:00 2001 From: Ikramullah Latif Date: Fri, 16 Oct 2020 16:19:35 +0800 Subject: [PATCH 2/3] Fix pull req --- typings/index.d.ts | 38 +++++++++++++++++++------------------- typings/index.ts | 9 +++++---- 2 files changed, 24 insertions(+), 23 deletions(-) diff --git a/typings/index.d.ts b/typings/index.d.ts index 7b9b529..686c073 100644 --- a/typings/index.d.ts +++ b/typings/index.d.ts @@ -1,6 +1,25 @@ declare module "captcha-canvas" { import { Image } from "canvas"; export const version: string; + interface SetCaptchaOptions { + characters?: number; + text?: string; + color?: string; + font?: string; + size?: number; + opacity?: number; + } + interface SetTraceOptions { + size?: number; + color?: string; + opacity?: number; + } + interface SetDecoyOptions { + color?: string; + opacity?: number; + font?: string; + size?: number; + } /** * Initatiates the creation of captcha image generation. @@ -54,23 +73,4 @@ declare module "captcha-canvas" { */ public generateSync(options?: {background?: Image}): Buffer; } - interface SetCaptchaOptions { - characters?: number; - text?: string; - color?: string; - font?: string; - size?: number; - opacity?: number; - } - interface SetTraceOptions { - size?: number; - color?: string; - opacity?: number; - } - interface SetDecoyOptions { - color?: string; - opacity?: number; - font?: string; - size?: number; - } } \ No newline at end of file diff --git a/typings/index.ts b/typings/index.ts index 969e1cf..e0b2652 100644 --- a/typings/index.ts +++ b/typings/index.ts @@ -1,13 +1,15 @@ +/* eslint no-console: "off" */ +/* tslint:disable:no-reference */ /// -import { CaptchaGenerator } from 'captcha-canvas'; +import { CaptchaGenerator } from "captcha-canvas"; /** * This code from documentation, for typing check. */ const captcha = new CaptchaGenerator() .setDimension(150, 450) - .setCaptcha({ text: 'CUSTOM05', size: 60, color: 'deeppink' }) + .setCaptcha({ text: "CUSTOM05", size: 60, color: "deeppink" }) .setDecoy({ opacity: 0.5 }); // Get captcha text @@ -17,5 +19,4 @@ console.log(captcha.text); console.log(captcha.generateSync()); // For some reason, you'll want to generate asynchronously -captcha.generate() - .then(buffer => console.log(buffer)); +captcha.generate().then(buffer => console.log(buffer)); From 06e17d76b8b9c5666b8ee49af00e3870f4ee8766 Mon Sep 17 00:00:00 2001 From: Ikramullah Latif Date: Fri, 16 Oct 2020 16:22:38 +0800 Subject: [PATCH 3/3] Fix arrow-parens error --- typings/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/typings/index.ts b/typings/index.ts index e0b2652..8e79592 100644 --- a/typings/index.ts +++ b/typings/index.ts @@ -19,4 +19,4 @@ console.log(captcha.text); console.log(captcha.generateSync()); // For some reason, you'll want to generate asynchronously -captcha.generate().then(buffer => console.log(buffer)); +captcha.generate().then((buffer) => console.log(buffer));