UNPKG

osrs-wrapper

Version:

A simple wrapper around the Old School Runescape API

50 lines (49 loc) 1.88 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getHiscores = getHiscores; const hiscore_types_enum_1 = require("./hiscore-types.enum"); const hiscores_constants_1 = require("./hiscores.constants"); /** * Fetches player hiscores for specified game type. Includes Skills, Minigames, and Boss kill counts. * * @export * @param {string} username * @param {HiscoreTypes} [type=HiscoreTypes.normal] * @returns {Promise<Player>} */ async function getHiscores(username, type = hiscore_types_enum_1.HiscoreTypes.normal) { const response = await fetch(`http://services.runescape.com/m=${type}/index_lite.json?player=${encodeURIComponent(username)}`); if (!response.ok) { console.error(`Failed to fetch hiscores for ${username}`, await response.text()); throw new Error(`Failed to fetch hiscores for ${username}`); } const data = (await response.json()); const player = { skills: {}, minigames: {}, bosses: {}, }; for (const skill of data.skills) { const { rank, level, xp, name } = skill; const skillName = hiscores_constants_1.SKILL_NAMES[name]; player.skills[skillName] = { rank, level, xp }; } for (const activity of data.activities) { const { rank, score, name } = activity; if (isMinigameName(name)) { const minigameName = hiscores_constants_1.MINIGAME_NAMES[name]; player.minigames[minigameName] = { rank, score }; } if (isBossName(name)) { const bossName = hiscores_constants_1.BOSS_NAMES[name]; player.bosses[bossName] = { rank, kills: score }; } } return player; } function isMinigameName(name) { return name in hiscores_constants_1.MINIGAME_NAMES; } function isBossName(name) { return name in hiscores_constants_1.BOSS_NAMES; }