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..686c073 100644 --- a/typings/index.d.ts +++ b/typings/index.d.ts @@ -1,23 +1,6 @@ -import { Image } from "canvas"; - declare module "captcha-canvas" { + import { Image } from "canvas"; export const version: string; - export class CaptchaGenerator { - constructor(options?: {height?: number, width?: number}) - public height: number; - public width: number; - public captcha: SetCaptchaOptions; - 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); - public generate(): Promise; - public generateSync(options?: {background?: Image}): Buffer; - } interface SetCaptchaOptions { characters?: number; text?: string; @@ -37,4 +20,57 @@ declare module "captcha-canvas" { font?: string; size?: number; } + + /** + * Initatiates the creation of captcha image generation. + */ + export class CaptchaGenerator { + constructor(options?: {height?: number, width?: number}) + public height: number; + public width: number; + public captcha: SetCaptchaOptions; + public trace: SetTraceOptions; + public decoy: SetDecoyOptions; + + /** + * 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; + } } \ No newline at end of file diff --git a/typings/index.ts b/typings/index.ts new file mode 100644 index 0000000..8e79592 --- /dev/null +++ b/typings/index.ts @@ -0,0 +1,22 @@ +/* eslint no-console: "off" */ +/* tslint:disable:no-reference */ +/// + +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));