diff --git a/src/main/services/bs-version-lib.service.ts b/src/main/services/bs-version-lib.service.ts index 8265a81b..4c4fdd10 100644 --- a/src/main/services/bs-version-lib.service.ts +++ b/src/main/services/bs-version-lib.service.ts @@ -30,7 +30,7 @@ export class BSVersionLibService{ } private getRemoteVersions(): Promise{ - return this.requestService.get(this.REMOTE_BS_VERSIONS_URL); + return this.requestService.getJSON(this.REMOTE_BS_VERSIONS_URL); } private async getLocalVersions(): Promise{ diff --git a/src/main/services/mods/beat-mods-api.service.ts b/src/main/services/mods/beat-mods-api.service.ts index f1b447b3..74b4b79a 100644 --- a/src/main/services/mods/beat-mods-api.service.ts +++ b/src/main/services/mods/beat-mods-api.service.ts @@ -38,7 +38,7 @@ export class BeatModsApiService { private async getVersionAlias(): Promise>{ if(this.aliasesCache.size){ return this.aliasesCache; } - return this.requestService.get>(this.BEAT_MODS_ALIAS).then(rawAliases => { + return this.requestService.getJSON>(this.BEAT_MODS_ALIAS).then(rawAliases => { Object.entries(rawAliases).forEach(([key, value]) => { this.aliasesCache.set(key, value.map(s => ({BSVersion: s} as BSVersion))); }); @@ -64,7 +64,7 @@ export class BeatModsApiService { const alias = await this.getAliasOfVersion(version); - return this.requestService.get(this.getVersionModsUrl(alias)).then(mods => { + return this.requestService.getJSON(this.getVersionModsUrl(alias)).then(mods => { mods = mods.map(mod => this.asignDependencies(mod, mods)); this.versionModsCache.set(version.BSVersion, mods); return mods; @@ -73,16 +73,10 @@ export class BeatModsApiService { public async getAllMods(): Promise{ if(!!this.allModsCache){ return this.allModsCache; } - return this.requestService.get(this.getAllModsUrl()).then(mods => { + return this.requestService.getJSON(this.getAllModsUrl()).then(mods => { this.allModsCache = mods; return this.allModsCache; }); } - public loadAllMods(){ return this.getAllMods(); } - - - - - } \ No newline at end of file diff --git a/src/main/services/mods/bs-mods-manager.service.ts b/src/main/services/mods/bs-mods-manager.service.ts index 70ecbad9..87cc42d4 100644 --- a/src/main/services/mods/bs-mods-manager.service.ts +++ b/src/main/services/mods/bs-mods-manager.service.ts @@ -245,7 +245,7 @@ export class BsModsManagerService { public async getInstalledMods(version: BSVersion): Promise{ this.manifestMatches = []; - await this.beatModsApi.loadAllMods(); + await this.beatModsApi.getAllMods(); const bsipa = await this.getBsipaInstalled(version); return Promise.all([ this.getModsInDir(version, ModsInstallFolder.PLUGINS_PENDING), diff --git a/src/main/services/request.service.ts b/src/main/services/request.service.ts index 3693dfd9..29032993 100644 --- a/src/main/services/request.service.ts +++ b/src/main/services/request.service.ts @@ -1,8 +1,9 @@ -import { RequestOptions, get } from "https"; +import { get } from "https"; import { createWriteStream, unlink } from "fs"; -import { Progression, unlinkPath } from "main/helpers/fs.helpers"; -import { Observable, buffer, shareReplay, tap } from "rxjs"; +import { Progression } from "main/helpers/fs.helpers"; +import { Observable, shareReplay, tap } from "rxjs"; import log from "electron-log"; +import fetch, { RequestInfo, RequestInit } from "node-fetch"; export class RequestService { @@ -15,18 +16,20 @@ export class RequestService { private constructor(){} - public get(options: string|RequestOptions): Promise{ + public async getJSON(url: RequestInfo, options?: RequestInit): Promise{ + try { + const response = await fetch(url, options); - return new Promise((resolve, reject) => { - let body = '' - get(options, (res) => { - res.on('data', chunk => body += chunk); - res.on('end', () => { - resolve(JSON.parse(body)); - }); - res.on('error', (err) => {log.error(err); reject(err)}) - }).on("error", err =>{log.error(err); reject(err)}); - }); + if (!response.ok) { + throw new Error(`HTTP error! status: ${response.status} ${response}`); + } + + return await response.json(); + } + catch (err) { + log.error(err); + throw err; + } } public downloadFile(url: string, dest: string): Observable>{