Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixing typings declaration and add example in typescript #21

Merged
merged 3 commits into from
Oct 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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/"]
}
72 changes: 54 additions & 18 deletions typings/index.d.ts
Original file line number Diff line number Diff line change
@@ -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<Buffer>;
public generateSync(options?: {background?: Image}): Buffer;
}
interface SetCaptchaOptions {
characters?: number;
text?: string;
Expand All @@ -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<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;
}
}
22 changes: 22 additions & 0 deletions typings/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/* eslint no-console: "off" */
/* tslint:disable:no-reference */
/// <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));