Skip to content

Commit

Permalink
Revert "Replace async/await by Promise instances"
Browse files Browse the repository at this point in the history
This reverts commit c1af46f.
  • Loading branch information
cedx committed Sep 27, 2024
1 parent c1af46f commit c753b40
Showing 1 changed file with 26 additions and 21 deletions.
47 changes: 26 additions & 21 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,39 +64,45 @@ export class Client {
* @param comment The comment to be checked.
* @returns A value indicating whether the specified comment is spam.
*/
checkComment(comment: Comment): Promise<CheckResult> {
return this.#fetch("1.1/comment-check", comment.toJSON()).then(response => response.text()
.then(text => text == "false" ? CheckResult.ham : response.headers.get("x-akismet-pro-tip") == "discard" ? CheckResult.pervasiveSpam : CheckResult.spam));
async checkComment(comment: Comment): Promise<CheckResult> {
const response = await this.#fetch("1.1/comment-check", comment.toJSON());
return await response.text() == "false"
? CheckResult.ham
: response.headers.get("x-akismet-pro-tip") == "discard" ? CheckResult.pervasiveSpam : CheckResult.spam;
}

/**
* Submits the specified comment that was incorrectly marked as spam but should not have been.
* @param comment The comment to be submitted.
* @returns Resolves once the comment has been submitted.
*/
submitHam(comment: Comment): Promise<void> {
return this.#fetch("1.1/submit-ham", comment.toJSON()).then(response => response.text()).then(text => {
if (text != Client.#success) throw Error("Invalid server response.");
});
async submitHam(comment: Comment): Promise<void> {
const response = await this.#fetch("1.1/submit-ham", comment.toJSON());
if (await response.text() != Client.#success) throw Error("Invalid server response.");
}

/**
* Submits the specified comment that was not marked as spam but should have been.
* @param comment The comment to be submitted.
* @returns Resolves once the comment has been submitted.
*/
submitSpam(comment: Comment): Promise<void> {
return this.#fetch("1.1/submit-spam", comment.toJSON()).then(response => response.text()).then(text => {
if (text != Client.#success) throw Error("Invalid server response.");
});
async submitSpam(comment: Comment): Promise<void> {
const response = await this.#fetch("1.1/submit-spam", comment.toJSON());
if (await response.text() != Client.#success) throw Error("Invalid server response.");
}

/**
* Checks the API key against the service database, and returns a value indicating whether it is valid.
* @returns `true` if the specified API key is valid, otherwise `false`.
*/
verifyKey(): Promise<boolean> {
return this.#fetch("1.1/verify-key").then(response => response.text()).then(text => text == "valid").catch(() => false);
async verifyKey(): Promise<boolean> {
try {
const response = await this.#fetch("1.1/verify-key");
return await response.text() == "valid";
}
catch {
return false;
}
}

/**
Expand All @@ -105,7 +111,7 @@ export class Client {
* @param fields The fields describing the query body.
* @returns The server response.
*/
#fetch(endpoint: string, fields: Record<string, any> = {}): Promise<Response> {
async #fetch(endpoint: string, fields: Record<string, any> = {}): Promise<Response> {
const body = new URLSearchParams({...this.blog.toJSON(), api_key: this.apiKey});
if (this.isTest) body.set("is_test", "1");

Expand All @@ -116,14 +122,13 @@ export class Client {
for (const item of value) body.set(`${key}[${index++}]`, String(item));
}

return fetch(new URL(endpoint, this.baseUrl), {method: "POST", headers: {"user-agent": this.userAgent}, body}).then(response => {
if (!response.ok) throw Error(`${response.status} ${response.statusText}`);
const response = await fetch(new URL(endpoint, this.baseUrl), {method: "POST", headers: {"user-agent": this.userAgent}, body});
if (!response.ok) throw Error(`${response.status} ${response.statusText}`);

const {headers} = response;
if (headers.has("x-akismet-alert-code")) throw Error(`${headers.get("x-akismet-alert-code")} ${headers.get("x-akismet-alert-msg")}`);
if (headers.has("x-akismet-debug-help")) throw Error(headers.get("x-akismet-debug-help")!);
return response;
});
const {headers} = response;
if (headers.has("x-akismet-alert-code")) throw Error(`${headers.get("x-akismet-alert-code")} ${headers.get("x-akismet-alert-msg")}`);
if (headers.has("x-akismet-debug-help")) throw Error(headers.get("x-akismet-debug-help")!);
return response;
}
}

Expand Down

0 comments on commit c753b40

Please sign in to comment.