Add types for recaptcha2 (#25643)

This commit is contained in:
l-jonas
2018-05-09 11:52:14 -07:00
committed by Sheetal Nandi
parent 1786d266d6
commit c74cce8223
4 changed files with 76 additions and 0 deletions
+33
View File
@@ -0,0 +1,33 @@
// Type definitions for recaptcha2 1.3
// Project: https://github.com/fereidani/recaptcha2#readme
// Definitions by: Jonas Lochmann <https://github.com/l-jonas>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 2.2
export = Recaptcha2;
declare class Recaptcha2 {
constructor(options: Recaptcha2.Options);
getRequestOptions<T extends object>(body: T): Recaptcha2.RequestOptions<T>;
validate(token: string): Promise<void>;
validateRequest(req: object, ip: string): Promise<void>;
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<T> {
uri: string;
method: 'POST';
json: true;
form: T;
}
}
+19
View File
@@ -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');
+23
View File
@@ -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"
]
}
+1
View File
@@ -0,0 +1 @@
{ "extends": "dtslint/dt.json" }