WebUI: migrate to fetch API
[qBittorrent.git] / src / webui / www / private / scripts / cache.js
blob0873ef97475490215f1034bce1730c14fd8f6a3a
1 /*
2  * Bittorrent Client using Qt and libtorrent.
3  * Copyright (C) 2024  Mike Tzou (Chocobo1)
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
18  *
19  * In addition, as a special exception, the copyright holders give permission to
20  * link this program with the OpenSSL project's "OpenSSL" library (or with
21  * modified versions of it that use the same license as the "OpenSSL" library),
22  * and distribute the linked executables. You must obey the GNU General Public
23  * License in all respects for all of the code used other than "OpenSSL".  If you
24  * modify file(s), you may extend this exception to your version of the file(s),
25  * but you are not obligated to do so. If you do not wish to do so, delete this
26  * exception statement from your version.
27  */
29 "use strict";
31 window.qBittorrent ??= {};
32 window.qBittorrent.Cache ??= (() => {
33     const exports = () => {
34         return {
35             buildInfo: new BuildInfoCache(),
36             preferences: new PreferencesCache(),
37             qbtVersion: new QbtVersionCache()
38         };
39     };
41     const deepFreeze = (obj) => {
42         // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/freeze#examples
44         const keys = Reflect.ownKeys(obj);
45         for (const key of keys) {
46             const value = obj[key];
47             if ((value && (typeof value === "object")) || (typeof value === "function"))
48                 deepFreeze(value);
49         }
50         Object.freeze(obj);
51     };
53     class BuildInfoCache {
54         #m_store = {};
56         async init() {
57             return fetch("api/v2/app/buildInfo", {
58                     method: "GET",
59                     cache: "no-store"
60                 })
61                 .then(async (response) => {
62                     if (!response.ok)
63                         return;
65                     const responseJSON = await response.json();
66                     deepFreeze(responseJSON);
67                     this.#m_store = responseJSON;
68                 });
69         }
71         get() {
72             return this.#m_store;
73         }
74     }
76     class PreferencesCache {
77         #m_store = {};
79         // obj: {
80         //   onFailure: () => {},
81         //   onSuccess: () => {}
82         // }
83         async init(obj = {}) {
84             return fetch("api/v2/app/preferences", {
85                     method: "GET",
86                     cache: "no-store"
87                 })
88                 .then(async (response) => {
89                         if (!response.ok)
90                             return;
92                         const responseText = await response.text();
93                         const responseJSON = JSON.parse(responseText);
94                         deepFreeze(responseJSON);
95                         this.#m_store = responseJSON;
97                         if (typeof obj.onSuccess === "function")
98                             obj.onSuccess(responseJSON, responseText);
99                     },
100                     (error) => {
101                         if (typeof obj.onFailure === "function")
102                             obj.onFailure(error);
103                     });
104         }
106         get() {
107             return this.#m_store;
108         }
110         // obj: {
111         //   data: {},
112         //   onFailure: () => {},
113         //   onSuccess: () => {}
114         // }
115         set(obj) {
116             if (typeof obj !== "object")
117                 throw new Error("`obj` is not an object.");
118             if (typeof obj.data !== "object")
119                 throw new Error("`data` is not an object.");
121             fetch("api/v2/app/setPreferences", {
122                     method: "POST",
123                     body: new URLSearchParams({
124                         "json": JSON.stringify(obj.data)
125                     })
126                 })
127                 .then(async (response) => {
128                         if (!response.ok)
129                             return;
131                         this.#m_store = structuredClone(this.#m_store);
132                         for (const key in obj.data) {
133                             if (!Object.hasOwn(obj.data, key))
134                                 continue;
136                             const value = obj.data[key];
137                             this.#m_store[key] = value;
138                         }
139                         deepFreeze(this.#m_store);
141                         if (typeof obj.onSuccess === "function") {
142                             const responseText = await response.text();
143                             obj.onSuccess(responseText);
144                         }
145                     },
146                     (error) => {
147                         if (typeof obj.onFailure === "function")
148                             obj.onFailure(error);
149                     });
150         }
151     }
153     class QbtVersionCache {
154         #m_store = "";
156         async init() {
157             return fetch("api/v2/app/version", {
158                     method: "GET",
159                     cache: "no-store"
160                 })
161                 .then(async (response) => {
162                     if (!response.ok)
163                         return;
165                     const responseText = await response.text();
166                     this.#m_store = responseText;
167                 });
168         }
170         get() {
171             return this.#m_store;
172         }
173     }
175     return exports();
176 })();
177 Object.freeze(window.qBittorrent.Cache);