Skip to content

Commit

Permalink
centralice prettier config
Browse files Browse the repository at this point in the history
  • Loading branch information
maaaathis committed Aug 12, 2024
1 parent 65f2de6 commit cb2acef
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 76 deletions.
7 changes: 7 additions & 0 deletions .idea/prettier.xml

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

1 change: 1 addition & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"maathis/prettier"
7 changes: 0 additions & 7 deletions .prettierrc.cjs

This file was deleted.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
"devDependencies": {
"@arethetypeswrong/cli": "^0.15.3",
"@types/node": "22.2.0",
"maathis": "^0.1.1",
"prettier": "^3.3.2",
"tsup": "^8.2.3",
"typescript": "^5.5.4"
Expand Down
135 changes: 66 additions & 69 deletions src/TurnstileVerify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,99 +4,96 @@
* Defines the arguments required to create a new TurnstileVerify instance.
*/
export type TurnstileConstructorArgs = {
/**
* The access token used for validation with Cloudflare Turnstile.
*/
token: string;
/**
* The access token used for validation with Cloudflare Turnstile.
*/
token: string;
};

/**
* Defines the arguments accepted by the `validate` method of TurnstileVerify.
*/
export type TurnstileValidateArgs = {
/**
* The user's response from the Cloudflare Turnstile challenge.
*/
response: string;
/**
* The user's response from the Cloudflare Turnstile challenge.
*/
response: string;

/**
* Optional: The user's remote IP address. Can be useful for fraud detection.
*/
remoteip?: string;
/**
* Optional: The user's remote IP address. Can be useful for fraud detection.
*/
remoteip?: string;

/**
* Optional: An idempotency key to ensure only one validation is processed per key.
*/
idempotency_key?: string;
/**
* Optional: An idempotency key to ensure only one validation is processed per key.
*/
idempotency_key?: string;
};

/**
* Defines the structure of the response object returned by the `validate` method.
*/
export type TurnstileValidationResponse = {
/**
* Indicates whether the validation was successful.
*/
valid: boolean;
/**
* Indicates whether the validation was successful.
*/
valid: boolean;

/**
* An array of messages related to the validation result.
* - On success, contains a single "success" message.
* - On failure, contains error codes from Cloudflare Turnstile.
*/
messages: string[];
/**
* An array of messages related to the validation result.
* - On success, contains a single "success" message.
* - On failure, contains error codes from Cloudflare Turnstile.
*/
messages: string[];
};

// **TurnstileVerify Class**

export class TurnstileVerify {
private readonly accessToken: string;
private readonly accessToken: string;

/**
* Creates a new TurnstileVerify instance.
* @param args - The arguments required for initialization.
*/
constructor(args: TurnstileConstructorArgs) {
this.accessToken = args.token;
}
/**
* Creates a new TurnstileVerify instance.
* @param args - The arguments required for initialization.
*/
constructor(args: TurnstileConstructorArgs) {
this.accessToken = args.token;
}

/**
* Validates a user's response from a Cloudflare Turnstile challenge.
* @param validationArgs - The arguments containing the user's response and optional data.
* @returns A Promise resolving to a TurnstileValidationResponse object.
*/
async validate(
validationArgs: TurnstileValidateArgs
): Promise<TurnstileValidationResponse> {
const form = new FormData();
form.append('secret', this.accessToken);
form.append('response', validationArgs.response);
if (validationArgs.remoteip) {
form.append('remoteip', validationArgs.remoteip);
}
if (validationArgs.idempotency_key) {
form.append('idempotency_key', validationArgs.idempotency_key);
}
/**
* Validates a user's response from a Cloudflare Turnstile challenge.
* @param validationArgs - The arguments containing the user's response and optional data.
* @returns A Promise resolving to a TurnstileValidationResponse object.
*/
async validate(validationArgs: TurnstileValidateArgs): Promise<TurnstileValidationResponse> {
const form = new FormData();
form.append('secret', this.accessToken);
form.append('response', validationArgs.response);
if (validationArgs.remoteip) {
form.append('remoteip', validationArgs.remoteip);
}
if (validationArgs.idempotency_key) {
form.append('idempotency_key', validationArgs.idempotency_key);
}

const verificationUrl =
'https://challenges.cloudflare.com/turnstile/v0/siteverify';
const response = await fetch(verificationUrl, {
body: form,
method: 'POST',
});
const verificationUrl = 'https://challenges.cloudflare.com/turnstile/v0/siteverifys';
const response = await fetch(verificationUrl, {
body: form,
method: 'POST',
});

const verificationResponse = await response.json();
const verificationResponse = await response.json();

if (!verificationResponse.success) {
return {
valid: false,
messages: verificationResponse['error-codes'],
};
}
if (!verificationResponse.success) {
return {
valid: false,
messages: verificationResponse['error-codes'],
};
}

return {
valid: true,
messages: ['success'],
};
}
return {
valid: true,
messages: ['success'],
};
}
}
10 changes: 10 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1324,6 +1324,15 @@ __metadata:
languageName: node
linkType: hard

"maathis@npm:^0.1.1":
version: 0.1.1
resolution: "maathis@npm:0.1.1"
peerDependencies:
react: "*"
checksum: 10c0/3121c9027d0bf9e31c4c296626971cf642d01edc42f09b7e37428faf404b195ca66d37432db68a5047b31e894af074a7bb0ffa89505b7dfff8af679c73654646
languageName: node
linkType: hard

"make-fetch-happen@npm:^13.0.0":
version: 13.0.1
resolution: "make-fetch-happen@npm:13.0.1"
Expand Down Expand Up @@ -2177,6 +2186,7 @@ __metadata:
dependencies:
"@arethetypeswrong/cli": "npm:^0.15.3"
"@types/node": "npm:22.2.0"
maathis: "npm:^0.1.1"
prettier: "npm:^3.3.2"
tsup: "npm:^8.2.3"
typescript: "npm:^5.5.4"
Expand Down

0 comments on commit cb2acef

Please sign in to comment.