From c753b40348fad81136cd989ed54945a328519f71 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Belin?= Date: Fri, 27 Sep 2024 22:39:54 +0200 Subject: [PATCH] Revert "Replace async/await by Promise instances" This reverts commit c1af46f2e110b9e5d12a8f208b76b1a6009e429f. --- src/client.ts | 47 ++++++++++++++++++++++++++--------------------- 1 file changed, 26 insertions(+), 21 deletions(-) diff --git a/src/client.ts b/src/client.ts index 80e7d80..7a95e9e 100644 --- a/src/client.ts +++ b/src/client.ts @@ -64,9 +64,11 @@ export class Client { * @param comment The comment to be checked. * @returns A value indicating whether the specified comment is spam. */ - checkComment(comment: Comment): Promise { - 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 { + 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; } /** @@ -74,10 +76,9 @@ export class Client { * @param comment The comment to be submitted. * @returns Resolves once the comment has been submitted. */ - submitHam(comment: Comment): Promise { - 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 { + const response = await this.#fetch("1.1/submit-ham", comment.toJSON()); + if (await response.text() != Client.#success) throw Error("Invalid server response."); } /** @@ -85,18 +86,23 @@ export class Client { * @param comment The comment to be submitted. * @returns Resolves once the comment has been submitted. */ - submitSpam(comment: Comment): Promise { - 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 { + 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 { - return this.#fetch("1.1/verify-key").then(response => response.text()).then(text => text == "valid").catch(() => false); + async verifyKey(): Promise { + try { + const response = await this.#fetch("1.1/verify-key"); + return await response.text() == "valid"; + } + catch { + return false; + } } /** @@ -105,7 +111,7 @@ export class Client { * @param fields The fields describing the query body. * @returns The server response. */ - #fetch(endpoint: string, fields: Record = {}): Promise { + async #fetch(endpoint: string, fields: Record = {}): Promise { const body = new URLSearchParams({...this.blog.toJSON(), api_key: this.apiKey}); if (this.isTest) body.set("is_test", "1"); @@ -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; } }