From 12c4e530a27cca716f6db37d61eea9ca9c83da64 Mon Sep 17 00:00:00 2001 From: maxswa Date: Fri, 11 Aug 2023 14:59:49 -0400 Subject: [PATCH] Update get stats functions to use JSON endpoint. --- src/hiscores.ts | 29 +++++++++++++---------------- 1 file changed, 13 insertions(+), 16 deletions(-) diff --git a/src/hiscores.ts b/src/hiscores.ts index 2c45a1d..46f8524 100644 --- a/src/hiscores.ts +++ b/src/hiscores.ts @@ -279,24 +279,24 @@ export async function getStats( ]; const shouldGetFormattedRsn = options?.shouldGetFormattedRsn ?? true; - const mainRes = await httpGet( - getStatsURL('main', rsn), + const mainRes = await httpGet( + getStatsURL('main', rsn, true), options?.axiosConfigs?.main ); if (mainRes.status === 200) { - const emptyResponse: AxiosResponse = { + const emptyResponse: AxiosResponse = { status: 404, - data: '', + data: { skills: [], activities: [] }, statusText: '', headers: {}, config: {} }; const getModeStats = async ( mode: Extract - ): Promise> => + ): Promise> => otherGamemodes.includes(mode) - ? httpGet( - getStatsURL(mode, rsn), + ? httpGet( + getStatsURL(mode, rsn, true), options?.axiosConfigs?.[mode] ).catch((err) => err) : emptyResponse; @@ -313,16 +313,16 @@ export async function getStats( deulted: false, deironed: false }; - player.main = parseStats(mainRes.data); + player.main = parseJsonStats(mainRes.data); const ironRes = await getModeStats('ironman'); if (ironRes.status === 200) { - player.ironman = parseStats(ironRes.data); + player.ironman = parseJsonStats(ironRes.data); const hcRes = await getModeStats('hardcore'); const ultRes = await getModeStats('ultimate'); if (hcRes.status === 200) { player.mode = 'hardcore'; - player.hardcore = parseStats(hcRes.data); + player.hardcore = parseJsonStats(hcRes.data); if ( player.ironman.skills.overall.xp !== player.hardcore.skills.overall.xp ) { @@ -337,7 +337,7 @@ export async function getStats( } } else if (ultRes.status === 200) { player.mode = 'ultimate'; - player.ultimate = parseStats(ultRes.data); + player.ultimate = parseJsonStats(ultRes.data); if ( player.ironman.skills.overall.xp !== player.ultimate.skills.overall.xp ) { @@ -383,11 +383,8 @@ export async function getStatsByGamemode( if (!GAMEMODES.includes(mode)) { throw Error('Invalid game mode'); } - const response = await httpGet(getStatsURL(mode, rsn), config); - if (response.status !== 200) { - throw Error(PLAYER_NOT_FOUND_ERROR); - } - const stats = parseStats(response.data); + const response = await getOfficialStats(rsn, mode, config); + const stats = parseJsonStats(response); return stats; }