Skip to content

Commit

Permalink
Fixing typings declaration and add example in typescript
Browse files Browse the repository at this point in the history
  • Loading branch information
ikr4-m committed Oct 16, 2020
1 parent a35b002 commit 279baba
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 10 deletions.
6 changes: 6 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,14 @@
"canvas": "*"
},
"devDependencies": {
"@types/node": "^14.11.8",
"jsdoc": "^3.6.5",
"jsdoc-skyceil": "^1.0.5",
"typescript": "^4.0.2"
},
"directories": {
"doc": "docs",
"example": "examples"
}
},
"types": "./typings/index.d.ts"
}
4 changes: 2 additions & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -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/"]
}
50 changes: 43 additions & 7 deletions typings/index.d.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<Buffer>;
/**
* 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 {
Expand Down
21 changes: 21 additions & 0 deletions typings/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/// <reference path="index.d.ts" />

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));

0 comments on commit 279baba

Please sign in to comment.