|
| 1 | +/** |
| 2 | + * Copyright (c) Statsify |
| 3 | + * |
| 4 | + * This source code is licensed under the GNU GPL v3 license found in the |
| 5 | + * LICENSE file in the root directory of this source tree. |
| 6 | + * https://github.com/Statsify/statsify/blob/main/LICENSE |
| 7 | + */ |
| 8 | + |
| 9 | +import Axios, { AxiosError, AxiosInstance } from "axios"; |
| 10 | +import { HypixelPlayer } from "./types"; |
| 11 | +import { setTimeout } from "node:timers/promises"; |
| 12 | + |
| 13 | +export class HypixelKeyRejectedError extends Error { |
| 14 | + public constructor(message: string) { |
| 15 | + super(message); |
| 16 | + this.name = "EHYAPIKEY"; |
| 17 | + } |
| 18 | +} |
| 19 | + |
| 20 | +export class HypixelAPIError extends Error { |
| 21 | + public constructor(message: string) { |
| 22 | + super(message); |
| 23 | + this.name = "EHYAPI"; |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +export type HypixelAPIConfig = { |
| 28 | + key: string; |
| 29 | + autoSleep: boolean; |
| 30 | +}; |
| 31 | + |
| 32 | +export class HypixelAPI { |
| 33 | + public limit: number; |
| 34 | + public remaining: number; |
| 35 | + |
| 36 | + private axios: AxiosInstance; |
| 37 | + private config: HypixelAPIConfig; |
| 38 | + |
| 39 | + public constructor(config: HypixelAPIConfig) { |
| 40 | + this.axios = Axios.create({ |
| 41 | + baseURL: "https://api.hypixel.net/", |
| 42 | + headers: { "API-KEY": config.key }, |
| 43 | + }); |
| 44 | + this.config = config; |
| 45 | + } |
| 46 | + |
| 47 | + public player(uuid: string): Promise<HypixelPlayer | undefined> { |
| 48 | + return this.get<HypixelPlayer>("player", { uuid }); |
| 49 | + } |
| 50 | + |
| 51 | + public friends(uuid: string) { |
| 52 | + return this.get("friends", { uuid }); |
| 53 | + } |
| 54 | + |
| 55 | + public recentgames(uuid: string) { |
| 56 | + return this.get("recentgames", { uuid }); |
| 57 | + } |
| 58 | + |
| 59 | + public status(uuid: string) { |
| 60 | + return this.get("status", { uuid }); |
| 61 | + } |
| 62 | + |
| 63 | + public guild(params: { id?: string; player?: string; name?: string }) { |
| 64 | + return this.get("guild", params); |
| 65 | + } |
| 66 | + |
| 67 | + public boosters() { |
| 68 | + return this.get("boosters"); |
| 69 | + } |
| 70 | + |
| 71 | + public counts() { |
| 72 | + return this.get("counts"); |
| 73 | + } |
| 74 | + |
| 75 | + public leaderboards() { |
| 76 | + return this.get("leaderboards"); |
| 77 | + } |
| 78 | + |
| 79 | + public punishmentstats() { |
| 80 | + return this.get("punishmentstats"); |
| 81 | + } |
| 82 | + |
| 83 | + public skyblockNews() { |
| 84 | + return this.get("skyblock/news"); |
| 85 | + } |
| 86 | + |
| 87 | + public skyblockAuction(params: { uuid?: string; player?: string; profile?: string }) { |
| 88 | + return this.get("skyblock/auction", params); |
| 89 | + } |
| 90 | + |
| 91 | + public skyblockAuctions(page: number) { |
| 92 | + return this.get("skyblock/auctions", { page: `${page}` }); |
| 93 | + } |
| 94 | + |
| 95 | + public skyblockAuctionsEnded() { |
| 96 | + return this.get("skyblock/auctions_ended"); |
| 97 | + } |
| 98 | + |
| 99 | + public skyblockBazaar() { |
| 100 | + return this.get("skyblock/bazaar"); |
| 101 | + } |
| 102 | + |
| 103 | + public skyblockProfile(profile: string) { |
| 104 | + return this.get("skyblock/profile", { profile }); |
| 105 | + } |
| 106 | + |
| 107 | + public skyblockProfiles(uuid: string) { |
| 108 | + return this.get("skyblock/profiles", { uuid }); |
| 109 | + } |
| 110 | + |
| 111 | + public skyblockBingo(uuid: string) { |
| 112 | + return this.get("skyblock/bingo", { uuid }); |
| 113 | + } |
| 114 | + |
| 115 | + public skyblockFiresales() { |
| 116 | + return this.get("skyblock/firesales"); |
| 117 | + } |
| 118 | + |
| 119 | + public async getResource( |
| 120 | + resource: |
| 121 | + | "games" |
| 122 | + | "achievements" |
| 123 | + | "challenges" |
| 124 | + | "quests" |
| 125 | + | "guilds/achievements" |
| 126 | + | "vanity/pets" |
| 127 | + | "vanity/companions" |
| 128 | + | "skyblock/collections" |
| 129 | + | "skyblock/skills" |
| 130 | + | "skyblock/items" |
| 131 | + | "skyblock/election" |
| 132 | + | "skyblock/bingo" |
| 133 | + ) { |
| 134 | + const response = await Axios.get(resource, { |
| 135 | + baseURL: "https://api.hypixel.net/resources/", |
| 136 | + }); |
| 137 | + return response.data; |
| 138 | + } |
| 139 | + |
| 140 | + private async get<T>( |
| 141 | + endpoint: string, |
| 142 | + params?: Record<string, string | undefined> |
| 143 | + ): Promise<T | undefined> { |
| 144 | + try { |
| 145 | + const response = await this.axios.get(endpoint, { params }); |
| 146 | + |
| 147 | + this.limit = Number(response.headers["rateLimit-limit"]); |
| 148 | + this.remaining = Number(response.headers["rateLimit-remaining"]); |
| 149 | + |
| 150 | + return response.data; |
| 151 | + } catch (error: any) { |
| 152 | + switch (error.code) { |
| 153 | + case 429: { |
| 154 | + if (this.config.autoSleep) { |
| 155 | + await setTimeout( |
| 156 | + Number((error as AxiosError)?.response?.headers?.["retry-after"] ?? 60) * |
| 157 | + 1000 |
| 158 | + ); |
| 159 | + return await this.get(endpoint, params); |
| 160 | + } else { |
| 161 | + throw error; |
| 162 | + } |
| 163 | + } |
| 164 | + |
| 165 | + case 403: { |
| 166 | + throw new HypixelKeyRejectedError( |
| 167 | + `Hypixel API key "${this.config.key}" was rejected!` |
| 168 | + ); |
| 169 | + } |
| 170 | + |
| 171 | + case 500: { |
| 172 | + throw new HypixelAPIError( |
| 173 | + "The Hypixel API has encountered an error with our request!" |
| 174 | + ); |
| 175 | + } |
| 176 | + |
| 177 | + default: { |
| 178 | + throw error; |
| 179 | + } |
| 180 | + } |
| 181 | + } |
| 182 | + } |
| 183 | +} |
0 commit comments