diff --git a/src/plugins/api/api.ts b/src/plugins/api/api.ts index 0fbbf9c3055..20848054b21 100644 --- a/src/plugins/api/api.ts +++ b/src/plugins/api/api.ts @@ -136,6 +136,10 @@ export class Api { } } + /** + * Get a system savedata. + * @param sessionId The savedata session ID + */ public async getSystemSavedata(sessionId: string) { try { const params = new URLSearchParams(); @@ -177,6 +181,26 @@ export class Api { return null; } + /** + * Update a system savedata. + * @param clientSessionId The savedata session ID + * @param rawSystemData The raw {@linkcode SystemSaveData} + * @returns an error message if something went wrong + */ + public async updateSystemSavedata(clientSessionId: string, rawSystemData: string) { + try { + const params = new URLSearchParams(); + params.append("clientSessionId", clientSessionId); + const response = await this.doPost(`/savedata/system/update?${params}`, rawSystemData); + + return (await response.json()) as string; + } catch (err) { + console.warn("Could not update system savedata!", err); + } + + return null; + } + /** * Get a session savedata. * @param slotId The slot ID to load @@ -285,15 +309,19 @@ export class Api { * @param bodyData The body-data to send. * @param dataType The data-type of the {@linkcode bodyData}. */ - private async doPost>(path: string, bodyData: D, dataType: DataType = "json") { + private async doPost(path: string, bodyData: D, dataType: DataType = "json") { let body: string = ""; const headers: HeadersInit = {}; if (dataType === "json") { - body = JSON.stringify(bodyData); + body = typeof bodyData === "string" ? bodyData : JSON.stringify(bodyData); headers["Content-Type"] = "application/json"; } else if (dataType === "form-urlencoded") { - body = new URLSearchParams(Object.entries(bodyData).map(([k, v]) => [k, v.toString()])).toString(); + if (bodyData instanceof Object) { + body = new URLSearchParams(Object.entries(bodyData).map(([k, v]) => [k, v.toString()])).toString(); + } else { + console.warn("Could not add body data to form-urlencoded!", bodyData); + } headers["Content-Type"] = "application/x-www-form-urlencoded"; } else { console.warn(`Unsupported data type: ${dataType}`); diff --git a/src/system/game-data.ts b/src/system/game-data.ts index e87da3cdeae..f3f32b7805b 100644 --- a/src/system/game-data.ts +++ b/src/system/game-data.ts @@ -398,8 +398,7 @@ export class GameData { localStorage.setItem(`data_${loggedInUser?.username}`, encrypt(systemData, bypassLogin)); if (!bypassLogin) { - Utils.apiPost(`savedata/system/update?clientSessionId=${clientSessionId}`, systemData, undefined, true) - .then(response => response.text()) + api.updateSystemSavedata(clientSessionId, systemData) .then(error => { this.scene.ui.savingIcon.hide(); if (error) {