From e52e2dcb617c974f5d83e30c326aaf91cc566586 Mon Sep 17 00:00:00 2001 From: Taken Date: Wed, 16 Oct 2024 10:58:13 +0200 Subject: [PATCH] Updated hypixel api wrapper --- src/interfaces/IGuild.ts | 6 +-- src/interfaces/IPlayer.ts | 6 +-- src/utils/HypixelFunctions/account.ts | 77 ++++++++++++++------------- 3 files changed, 45 insertions(+), 44 deletions(-) diff --git a/src/interfaces/IGuild.ts b/src/interfaces/IGuild.ts index fc6445b..8451fbe 100644 --- a/src/interfaces/IGuild.ts +++ b/src/interfaces/IGuild.ts @@ -1,9 +1,7 @@ // dprint-ignore-file export interface IGuild { - data: { - success: boolean - guild: IGuildData - } + success: boolean + guild: IGuildData } export interface IGuildData { diff --git a/src/interfaces/IPlayer.ts b/src/interfaces/IPlayer.ts index 1ec6f6e..0c18238 100644 --- a/src/interfaces/IPlayer.ts +++ b/src/interfaces/IPlayer.ts @@ -1,9 +1,7 @@ // dprint-ignore-file export interface IPlayer { - data: { - success: boolean - player: IPlayerData - } + success: boolean + player: IPlayerData } export interface IPlayerData { diff --git a/src/utils/HypixelFunctions/account.ts b/src/utils/HypixelFunctions/account.ts index 2c9f46b..b9dee97 100644 --- a/src/utils/HypixelFunctions/account.ts +++ b/src/utils/HypixelFunctions/account.ts @@ -1,4 +1,4 @@ -import fetch from "axios" +import axios from "axios" import { IPlayer, IPlayerData } from "~/interfaces" import { IGuild, IGuildData } from "~/interfaces" import env from "~/utils/Env.js" @@ -10,25 +10,21 @@ const guild = "https://api.hypixel.net/guild" const minotar = "https://minotar.net/helm/" type GuildQueryType = "player" | "name" | "id" -type Profile = { - data: { - id: string - name: string - } +type UUIDData = { + id: string + name: string } -type Profile2 = { - data: { - id: string - name: string - properties: { name: string, value: string }[] - profileActions: [] - } +type IGNData = { + id: string + name: string + properties: { name: string, value: string }[] + profileActions: [] } async function getUUID(ign: string): Promise { try { - const req: Profile = await fetch(mojang + ign) + const req = await axios.get(mojang + ign) return req.data.id // eslint-disable-next-line @typescript-eslint/no-unused-vars } catch (err) { @@ -38,7 +34,7 @@ async function getUUID(ign: string): Promise { async function getIGN(uuid: string): Promise { try { - const req: Profile2 = await fetch(mojanguuid + uuid) + const req = await axios.get(mojanguuid + uuid) return req.data.name // eslint-disable-next-line @typescript-eslint/no-unused-vars } catch (err) { @@ -47,39 +43,48 @@ async function getIGN(uuid: string): Promise { } async function getPlayer(uuid: string): Promise { - const playerReq: IPlayer = await fetch(hypixel, { - params: { - uuid: uuid - }, - headers: { - "API-Key": apikey + try { + const req = await axios.get(hypixel, { + params: { + uuid: uuid + }, + headers: { + "API-Key": apikey + } + }) + if (!req.data.player) { + return null } - }) - if (!playerReq.data.player) { + return req.data.player + // eslint-disable-next-line @typescript-eslint/no-unused-vars + } catch (err) { return null } - - return playerReq.data.player } async function getGuild(query: string, type?: GuildQueryType): Promise { const reqType = type ? type : "player" - const guildReq: IGuild = await fetch(guild, { - params: { - [reqType]: query - }, - headers: { - "API-Key": apikey - } - }) + try { + const req = await axios.get(guild, { + params: { + [reqType]: query + }, + headers: { + "API-Key": apikey + } + }) - if (!guildReq.data.guild) { + if (!req.data.guild) { + return null + } + + return req.data.guild + // eslint-disable-next-line @typescript-eslint/no-unused-vars + } catch (err) { return null } - - return guildReq.data.guild } async function getHeadURL(ign: string): Promise {