diff --git a/types/recaptcha2/index.d.ts b/types/recaptcha2/index.d.ts new file mode 100644 index 0000000000..78391ade16 --- /dev/null +++ b/types/recaptcha2/index.d.ts @@ -0,0 +1,33 @@ +// Type definitions for recaptcha2 1.3 +// Project: https://github.com/fereidani/recaptcha2#readme +// Definitions by: Jonas Lochmann +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 2.2 + +export = Recaptcha2; + +declare class Recaptcha2 { + constructor(options: Recaptcha2.Options); + + getRequestOptions(body: T): Recaptcha2.RequestOptions; + validate(token: string): Promise; + validateRequest(req: object, ip: string): Promise; + translateErrors(errorCodes: string): string; + translateErrors(errorCodes: string[]): string[]; + formElement(htmlClass: string): string; +} + +declare namespace Recaptcha2 { + interface Options { + siteKey: string; + secretKey: string; + ssl?: boolean; + } + + interface RequestOptions { + uri: string; + method: 'POST'; + json: true; + form: T; + } +} diff --git a/types/recaptcha2/recaptcha2-tests.ts b/types/recaptcha2/recaptcha2-tests.ts new file mode 100644 index 0000000000..15ef3537e7 --- /dev/null +++ b/types/recaptcha2/recaptcha2-tests.ts @@ -0,0 +1,19 @@ +import * as reCAPTCHA from 'recaptcha2'; + +const recaptcha = new reCAPTCHA({ + siteKey: 'your-site-key', + secretKey: 'your-secret-key' +}); + +const key = 'invalid'; + +recaptcha.validate(key) + .then(() => { + // validated and secure + }) + .catch((errorCodes) => { + // translate error codes to human readable text + const message = recaptcha.translateErrors(errorCodes); + }); + +const formSnippet = recaptcha.formElement('recaptcha'); diff --git a/types/recaptcha2/tsconfig.json b/types/recaptcha2/tsconfig.json new file mode 100644 index 0000000000..196e86baf0 --- /dev/null +++ b/types/recaptcha2/tsconfig.json @@ -0,0 +1,23 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "strictFunctionTypes": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "recaptcha2-tests.ts" + ] +} diff --git a/types/recaptcha2/tslint.json b/types/recaptcha2/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/recaptcha2/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" }