diff --git a/src/main/services/request.service.ts b/src/main/services/request.service.ts index 29032993..51d4a3a0 100644 --- a/src/main/services/request.service.ts +++ b/src/main/services/request.service.ts @@ -21,7 +21,7 @@ export class RequestService { const response = await fetch(url, options); if (!response.ok) { - throw new Error(`HTTP error! status: ${response.status} ${response}`); + throw new Error(`HTTP error! status: ${response.status} ${response}`); } return await response.json(); diff --git a/src/main/services/supporters.service.ts b/src/main/services/supporters.service.ts index d02abbed..cf362f32 100644 --- a/src/main/services/supporters.service.ts +++ b/src/main/services/supporters.service.ts @@ -1,10 +1,10 @@ import { writeFileSync } from "fs"; import { readJSON } from "fs-extra"; -import { get } from "https"; -import isOnline from "is-online"; import path from "path"; import { Supporter } from "shared/models/supporters/supporter.interface"; import { UtilsService } from "./utils.service"; +import { RequestService } from "./request.service"; +import { allSettled } from "../../shared/helpers/promise.helpers"; export class SupportersService { @@ -16,6 +16,8 @@ export class SupportersService { private cache: Supporter[]; private readonly utilsService: UtilsService; + private readonly requestService: RequestService; + public static getInstance(): SupportersService{ if(!SupportersService.instance){ SupportersService.instance = new SupportersService(); } @@ -24,6 +26,7 @@ export class SupportersService { private constructor(){ this.utilsService = UtilsService.getInstance(); + this.requestService = RequestService.getInstance(); } private async updateLocalSupporters(supporters: Supporter[]): Promise{ @@ -31,15 +34,8 @@ export class SupportersService { writeFileSync(patreonsPath, JSON.stringify(supporters, null, "\t"), {encoding: 'utf-8', flag: 'w'}); } - private async getRemoteSupporters(): Promise{ - return new Promise((resolve, reject) => { - let body = '' - get(this.PATREONS_URL, (res) => { - res.on('data', chunk => body += chunk); - res.on('end', () => resolve(JSON.parse(body))); - res.on('error', () => reject(null)) - }) - }) + private getRemoteSupporters(): Promise{ + return this.requestService.getJSON(this.PATREONS_URL); } private async getLocalSupporters(): Promise{ @@ -50,11 +46,9 @@ export class SupportersService { private async loadSupporters(): Promise{ if(!!this.cache && this.cache.length){ return this.cache; } - const isOnlineRes = await isOnline({timeout: 1500}); - - const [localVersions, remoteVersions] = await Promise.all([ - this.getLocalSupporters(), (isOnlineRes && this.getRemoteSupporters()) - ]); + const [localVersions, remoteVersions] = await allSettled([ + this.getLocalSupporters(), this.getRemoteSupporters() + ], {keepStructure: true}); const supporters = remoteVersions?.length ? remoteVersions : localVersions;