Migrate RewriteTest.ts to the fetch-based request API
authorTim Düsterhus <duesterhus@woltlab.com>
Fri, 25 Aug 2023 08:20:56 +0000 (10:20 +0200)
committerTim Düsterhus <duesterhus@woltlab.com>
Fri, 25 Aug 2023 08:22:53 +0000 (10:22 +0200)
This makes the code much more readable and also prevents browser-side caching
by default.

ts/WoltLabSuite/Core/Acp/Ui/Option/RewriteTest.ts
wcfsetup/install/files/js/WoltLabSuite/Core/Acp/Ui/Option/RewriteTest.js

index dbda953dcbc10922f21029a32ce49ab4db4f7992..65dbe00deea87fc9dbb466eb91cde9f2d715593d 100644 (file)
@@ -6,11 +6,11 @@
  * @license  GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
  */
 
-import AjaxRequest from "../../../Ajax/Request";
 import * as Language from "../../../Language";
 import UiDialog from "../../../Ui/Dialog";
 import { DialogCallbackSetup } from "../../../Ui/Dialog/Data";
 import DomUtil from "../../../Dom/Util";
+import { prepareRequest } from "WoltLabSuite/Core/Ajax/Backend";
 
 interface TestResult {
   app: string;
@@ -72,35 +72,29 @@ class RewriteTest {
     this.setStatus("running");
 
     const tests: Promise<TestResult>[] = Array.from(this.apps).map(([app, url]) => {
-      return new Promise((resolve, reject) => {
-        const request = new AjaxRequest({
-          ignoreError: true,
-          // bypass the LinkHandler, because rewrites aren't enabled yet
-          url: url,
-          type: "GET",
-          includeRequestedWith: false,
-          success: (data) => {
+      return prepareRequest(url)
+        .get()
+        .disableLoadingIndicator()
+        .fetchAsJson()
+        .then(
+          (data: any) => {
             if (
               !Object.prototype.hasOwnProperty.call(data, "core_rewrite_test") ||
               data.core_rewrite_test !== "passed"
             ) {
-              reject({ app, pass: false });
+              return false;
             } else {
-              resolve({ app, pass: true });
+              return true;
             }
           },
-          failure: () => {
-            reject({ app, pass: false });
-
-            return true;
-          },
+          () => false,
+        )
+        .then((pass) => {
+          return { app, pass };
         });
-
-        request.sendRequest(false);
-      });
     });
 
-    const results: TestResult[] = await Promise.all(tests.map((test) => test.catch((result: TestResult) => result)));
+    const results: TestResult[] = await Promise.all(tests);
 
     const passed = results.every((result) => result.pass);
 
index 47feff9e38f8e945602ce0c13c93782eb8c4801f..ac3e08c94c7887f65ce038f4fe90159e5bef7754 100644 (file)
@@ -5,11 +5,10 @@
  * @copyright  2001-2019 WoltLab GmbH
  * @license  GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
  */
-define(["require", "exports", "tslib", "../../../Ajax/Request", "../../../Language", "../../../Ui/Dialog", "../../../Dom/Util"], function (require, exports, tslib_1, Request_1, Language, Dialog_1, Util_1) {
+define(["require", "exports", "tslib", "../../../Language", "../../../Ui/Dialog", "../../../Dom/Util", "WoltLabSuite/Core/Ajax/Backend"], function (require, exports, tslib_1, Language, Dialog_1, Util_1, Backend_1) {
     "use strict";
     Object.defineProperty(exports, "__esModule", { value: true });
     exports.init = void 0;
-    Request_1 = tslib_1.__importDefault(Request_1);
     Language = tslib_1.__importStar(Language);
     Dialog_1 = tslib_1.__importDefault(Dialog_1);
     Util_1 = tslib_1.__importDefault(Util_1);
@@ -58,31 +57,24 @@ define(["require", "exports", "tslib", "../../../Ajax/Request", "../../../Langua
             this.buttonStartTest.classList.add("disabled");
             this.setStatus("running");
             const tests = Array.from(this.apps).map(([app, url]) => {
-                return new Promise((resolve, reject) => {
-                    const request = new Request_1.default({
-                        ignoreError: true,
-                        // bypass the LinkHandler, because rewrites aren't enabled yet
-                        url: url,
-                        type: "GET",
-                        includeRequestedWith: false,
-                        success: (data) => {
-                            if (!Object.prototype.hasOwnProperty.call(data, "core_rewrite_test") ||
-                                data.core_rewrite_test !== "passed") {
-                                reject({ app, pass: false });
-                            }
-                            else {
-                                resolve({ app, pass: true });
-                            }
-                        },
-                        failure: () => {
-                            reject({ app, pass: false });
-                            return true;
-                        },
-                    });
-                    request.sendRequest(false);
+                return (0, Backend_1.prepareRequest)(url)
+                    .get()
+                    .disableLoadingIndicator()
+                    .fetchAsJson()
+                    .then((data) => {
+                    if (!Object.prototype.hasOwnProperty.call(data, "core_rewrite_test") ||
+                        data.core_rewrite_test !== "passed") {
+                        return false;
+                    }
+                    else {
+                        return true;
+                    }
+                }, () => false)
+                    .then((pass) => {
+                    return { app, pass };
                 });
             });
-            const results = await Promise.all(tests.map((test) => test.catch((result) => result)));
+            const results = await Promise.all(tests);
             const passed = results.every((result) => result.pass);
             // Delay the status update to prevent UI flicker.
             await new Promise((resolve) => window.setTimeout(resolve, 500));