diff --git a/src/hiscores.ts b/src/hiscores.ts index d7e012d..db5dc8f 100644 --- a/src/hiscores.ts +++ b/src/hiscores.ts @@ -1,4 +1,4 @@ -import { AxiosResponse } from 'axios'; +import { AxiosRequestConfig, AxiosResponse } from 'axios'; import { BinaryData, JSDOM } from 'jsdom'; import { Player, @@ -40,13 +40,17 @@ import { * @param rsn Username of the player. * @returns Formatted version of the rsn. */ -export async function getRSNFormat(rsn: string): Promise { +export async function getRSNFormat( + rsn: string, + config?: AxiosRequestConfig +): Promise { validateRSN(rsn); const url = getPlayerTableURL('main', rsn); try { const response = await httpGet( - url + url, + config ); const dom = new JSDOM(response.data); const anchor = dom.window.document.querySelector( @@ -174,7 +178,10 @@ export async function getStats( ]; const shouldGetFormattedRsn = options?.shouldGetFormattedRsn ?? true; - const mainRes = await httpGet(getStatsURL('main', rsn)); + const mainRes = await httpGet( + getStatsURL('main', rsn), + options?.axiosConfigs?.main + ); if (mainRes.status === 200) { const emptyResponse: AxiosResponse = { status: 404, @@ -187,10 +194,15 @@ export async function getStats( mode: Extract ): Promise> => otherGamemodes.includes(mode) - ? httpGet(getStatsURL(mode, rsn)).catch((err) => err) + ? httpGet( + getStatsURL(mode, rsn), + options?.axiosConfigs?.[mode] + ).catch((err) => err) : emptyResponse; const formattedName = shouldGetFormattedRsn - ? await getRSNFormat(rsn).catch(() => undefined) + ? await getRSNFormat(rsn, options?.axiosConfigs?.rsn).catch( + () => undefined + ) : undefined; const player: Player = { @@ -258,17 +270,19 @@ export async function getStats( * * @param rsn Username of the player. * @param mode Gamemode to fetch ranks for. + * @param config Optional axios request config object. * @returns Stats object. */ export async function getStatsByGamemode( rsn: string, - mode: Gamemode = 'main' + mode: Gamemode = 'main', + config?: AxiosRequestConfig ): Promise { validateRSN(rsn); if (!GAMEMODES.includes(mode)) { throw Error('Invalid game mode'); } - const response = await httpGet(getStatsURL(mode, rsn)); + const response = await httpGet(getStatsURL(mode, rsn), config); if (response.status !== 200) { throw Error('Player not found'); } @@ -280,7 +294,8 @@ export async function getStatsByGamemode( export async function getSkillPage( skill: SkillName, mode: Gamemode = 'main', - page: number = 1 + page: number = 1, + config?: AxiosRequestConfig ): Promise { if (!GAMEMODES.includes(mode)) { throw Error('Invalid game mode'); @@ -291,7 +306,10 @@ export async function getSkillPage( } const url = getSkillPageURL(mode, skill, page); - const response = await httpGet(url); + const response = await httpGet( + url, + config + ); const dom = new JSDOM(response.data); const playersHTML = dom.window.document.querySelectorAll( '.personal-hiscores__row' @@ -328,7 +346,8 @@ export async function getSkillPage( export async function getActivityPage( activity: ActivityName, mode: Gamemode = 'main', - page: number = 1 + page: number = 1, + config?: AxiosRequestConfig ): Promise { if (!GAMEMODES.includes(mode)) { throw Error('Invalid game mode'); @@ -339,7 +358,10 @@ export async function getActivityPage( } const url = getActivityPageURL(mode, activity, page); - const response = await httpGet(url); + const response = await httpGet( + url, + config + ); const dom = new JSDOM(response.data); const playersHTML = dom.window.document.querySelectorAll( '.personal-hiscores__row'