UNPKG

rocket-league-stats

Version:
325 lines 10.5 kB
// src/index.ts var PLATFORM = { Steam: "steam", Epic: "epic", Playstation: "psn", Xbox: "xbl" }; var BASE_URL = `https://api.tracker.gg/api/v2/rocket-league/standard/profile/`; var getRomanNumeral = (num) => { if (num > 4 || num < 1) return ""; const roman = ["I", "II", "III", "IV"]; return roman[num - 1]; }; var fetchData = (url) => new Promise((resolve, reject) => { fetch(url, { method: "GET", headers: { Origin: "https://rocketleague.tracker.network", Referer: "https://rocketleague.tracker.network/" } }).then((res) => res.json()).then((json) => resolve(json)).catch((err) => reject(err)); }); var API = class { platform; username; raw = void 0; expiresAfter = 1e3 * 60; // 1 minute lastFetch = 0; constructor(platform, username, options) { this.platform = platform; this.username = username; if (options?.expiresAfter) this.expiresAfter = options.expiresAfter; } /** * Fetch the user data from the tracker network * @returns The user data from the tracker network * @example * const api = new API(PLATFORM.EPIC, 'lil McNugget') * const data = await api.fetchUser() * console.log(data) * @throws {Error} - If the user is not found * @throws {Error} - If the user data is not found * */ async fetchUser() { this.raw = await fetchData(`${BASE_URL}${this.platform}/${this.username}`); if ((this.raw.errors?.length ?? 0) > 0) throw new Error(this.raw?.errors?.[0]?.message); this.lastFetch = Date.now(); return this.raw; } /** * Get the overview stats of the user * @returns The overview stats of the user * @example * const api = new API(PLATFORM.EPIC, 'lil McNugget') * const data = await api.overview() * console.log(data) * @throws {Error} - If the overview data is not found * @throws {Error} - If the user data is not found */ async overview(options) { if (!this.raw) await this.fetchUser(); if (!this.raw) throw new Error("No data found"); if (this.lastFetch + this.expiresAfter < Date.now() || options?.fresh) await this.fetchUser(); const data = this.raw.data.segments.find((x) => x.type === "overview"); if (!data) throw new Error("No overview data found"); const stats = data.stats; return { assists: stats.assists.value, goals: stats.goals.value, goalShotRatio: stats.goalShotRatio.value, mVPs: stats.mVPs.value, saves: stats.saves.value, score: stats.score.value, seasonRewardLevel: stats.seasonRewardLevel.value, seasonRewardWins: stats.seasonRewardWins.value, shots: stats.shots.value, tRNRating: stats.tRNRating.value, wins: stats.wins.value }; } /** * Return the stats of the user formatted * @private * @param stats - The stats of the user * @returns The formatted stats of the user */ returnStats(stats) { return { division: stats.division.value, deltaUp: stats.division.metadata.deltaUp ?? null, deltaDown: stats.division.metadata.deltaDown ?? null, matchesPlayed: stats.matchesPlayed.value, peakRating: stats.peakRating.value, rank: stats.tier.metadata.name, rating: stats.rating.value, tier: stats.tier.value, winStreak: stats.winStreak.displayValue === "0" ? 0 : parseInt(stats.winStreak.displayValue, 10) }; } /** * Get the rank data of the user * @private * @param playlistName - The name of the playlist * @param options - The options for the API * @returns The rank data of the user */ async getRankData(playlistName, options) { if (!this.raw) await this.fetchUser(); if (!this.raw) throw new Error("No data found"); if (this.lastFetch + this.expiresAfter < Date.now() || options?.fresh) await this.fetchUser(); const data = this.raw.data.segments.find((x) => x?.metadata?.name === playlistName); if (!data) throw new Error(`No ${playlistName} data found`); const stats = data.stats; return this.returnStats(stats); } /** * Get the 1v1 rank data of the user * @param options - The options for the API * @returns The 1v1 rank data of the user * @example * const api = new API(PLATFORM.EPIC, 'lil McNugget') * const data = await api.get1v1() * console.log(data) * @throws {Error} - If the 1v1 data is not found * @throws {Error} - If the user data is not found */ get1v1(options) { return this.getRankData("Ranked Duel 1v1", options); } /** * Get the 2v2 rank data of the user * @param options - The options for the API * @returns The 2v2 rank data of the user * @example * const api = new API(PLATFORM.EPIC, 'lil McNugget') * const data = await api.get2v2() * console.log(data) * @throws {Error} - If the 2v2 data is not found * @throws {Error} - If the user data is not found */ get2v2(options) { return this.getRankData("Ranked Doubles 2v2", options); } /** * Get the 3v3 rank data of the user * @param options - The options for the API * @returns The 3v3 rank data of the user * @example * const api = new API(PLATFORM.EPIC, 'lil McNugget') * const data = await api.get3v3() * console.log(data) * @throws {Error} - If the 3v3 data is not found * @throws {Error} - If the user data is not found */ get3v3(options) { return this.getRankData("Ranked Standard 3v3", options); } /** * Get the Hoops rank data of the user * @param options - The options for the API * @returns The Hoops rank data of the user * @example * const api = new API(PLATFORM.EPIC, 'lil McNugget') * const data = await api.getHoops() * console.log(data) * @throws {Error} - If the Hoops data is not found * @throws {Error} - If the user data is not found */ getHoops(options) { return this.getRankData("Hoops", options); } /** * Get the Rumble rank data of the user * @param options - The options for the API * @returns The Rumble rank data of the user * @example * const api = new API(PLATFORM.EPIC, 'lil McNugget') * const data = await api.getRumble() * console.log(data) * @throws {Error} - If the Rumble data is not found * @throws {Error} - If the user data is not found */ getRumble(options) { return this.getRankData("Rumble", options); } /** * Get the Dropshot rank data of the user * @param options - The options for the API * @returns The Dropshot rank data of the user * @example * const api = new API(PLATFORM.EPIC, 'lil McNugget') * const data = await api.getDropshot() * console.log(data) * @throws {Error} - If the Dropshot data is not found * @throws {Error} - If the user data is not found */ getDropshot(options) { return this.getRankData("Dropshot", options); } /** * Get the Snowday rank data of the user * @param options - The options for the API * @returns The Snowday rank data of the user * @example * const api = new API(PLATFORM.EPIC, 'lil McNugget') * const data = await api.getSnowday() * console.log(data) * @throws {Error} - If the Snowday data is not found * @throws {Error} - If the user data is not found */ getSnowday(options) { return this.getRankData("Snowday", options); } /** * Get the Tournament rank data of the user * @param options - The options for the API * @returns The Tournament rank data of the user * @example * const api = new API(PLATFORM.EPIC, 'lil McNugget') * const data = await api.getTournament() * console.log(data) * @throws {Error} - If the Tournament data is not found * @throws {Error} - If the user data is not found */ getTournament(options) { return this.getRankData("Tournament Matches", options); } /** * Get the user data * @description Get all ranks and overview data for the user * @param options - The options for the API * @returns The user data * @example * const api = new API(PLATFORM.EPIC, 'lil McNugget') * const data = await api.getData() * console.log(data) * @throws {Error} - If the user data is not found * @throws {Error} - If the overview data is not found */ async getData(options) { const result = {}; result.overview = await this.overview(options); result.gamemodes = {}; if (!this.raw) await this.fetchUser(); if (!this.raw) throw new Error("No data found"); const playlists = this.raw.data.segments.filter((x) => x.type === "playlist"); for (const playlist of playlists) { if (playlist) { const stats = playlist.stats; result.gamemodes[playlist.metadata.name] = {}; result.gamemodes[playlist.metadata.name]["rank"] = stats.tier.metadata.name; result.gamemodes[playlist.metadata.name] = this.returnStats(stats); } } return result; } /** * Get the userinfo of the user * @param options - The options for the API * @returns The userinfo of the user * @example * const api = new API(PLATFORM.EPIC, 'lil McNugget') * const data = await api.getUserinfo() * console.log(data) * @throws {Error} - If the user data is not found * @throws {Error} - If the userinfo data is not found */ async getUserinfo(options) { if (!this.raw) await this.fetchUser(); if (!this.raw) throw new Error("No data found"); if (this.lastFetch + this.expiresAfter < Date.now() || options?.fresh) await this.fetchUser(); const platform = this.raw.data.platformInfo; return { platform: platform.platformSlug, uuid: platform.platformUserId, name: platform.platformUserHandle, userid: platform.platformUserIdentifier, avatar: platform.avatarUrl }; } /** * Get the raw data of the user * @param options - The options for the API * @returns The raw data of the user * @example * const api = new API(PLATFORM.EPIC, 'lil McNugget') * const data = await api.getRaw() * console.log(data) * @throws {Error} - If the user data is not found * @throws {Error} - If the raw data is not found */ async getRaw(options) { if (!this.raw) await this.fetchUser(); if (!this.raw) throw new Error("No data found"); if (this.lastFetch + this.expiresAfter < Date.now() || options?.fresh) await this.fetchUser(); return this.raw; } }; export { API, PLATFORM, getRomanNumeral }; //# sourceMappingURL=index.js.map