Skip to content
This repository has been archived by the owner on Dec 11, 2024. It is now read-only.

Commit

Permalink
Convert src/API to typescript
Browse files Browse the repository at this point in the history
  • Loading branch information
Kathund committed Jul 28, 2024
1 parent f92b603 commit d74cf02
Show file tree
Hide file tree
Showing 36 changed files with 807 additions and 399 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"axios": "^1.7.2",
"node-cache": "^5.1.2",
"prismarine-nbt": "^2.5.0",
"rss-parser": "^3.13.0",
"skyhelper-networth": "^1.21.0"
},
"license": "MIT",
Expand Down
685 changes: 288 additions & 397 deletions pnpm-lock.yaml

Large diffs are not rendered by default.

8 changes: 8 additions & 0 deletions src/API/getAPIStatus.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import Status from '../structures/APIStatus.js';
import Rss from 'rss-parser';
const Parser = new Rss();
export default async function (options: any) {
const parsed = await Parser.parseURL('https://status.hypixel.net/history.rss');
if (options && options.raw) return parsed;
return new Status(parsed);
}
7 changes: 7 additions & 0 deletions src/API/getAchievements.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import Achievements from '../structures/Static/Achievements';
export default async function (this: any) {
// eslint-disable-next-line no-underscore-dangle
const res = await this._makeRequest('/resources/achievements');
if (res.raw) return res;
return new Achievements(res);
}
7 changes: 7 additions & 0 deletions src/API/getBoosters.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import Booster from '../structures/Boosters/Booster';
export default async function (this: any) {
// eslint-disable-next-line no-underscore-dangle
const res = await this._makeRequest('/boosters');
if (res.raw) return res;
return res.boosters.length ? res.boosters.map((b: any) => new Booster(b)).reverse() : [];
}
7 changes: 7 additions & 0 deletions src/API/getChallenges.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import Challenges from '../structures/Static/Challenges';
export default async function (this: any) {
// eslint-disable-next-line no-underscore-dangle
const res = await this._makeRequest('/resources/challenges');
if (res.raw) return res;
return new Challenges(res);
}
7 changes: 7 additions & 0 deletions src/API/getGameCounts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import GameCounts from '../structures/GameCounts';
export default async function (this: any) {
// eslint-disable-next-line no-underscore-dangle
const res = await this._makeRequest('/counts');
if (res.raw) return res;
return new GameCounts(res);
}
20 changes: 20 additions & 0 deletions src/API/getGuild.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { GuildSearchParameter } from '../typings';
import Guild from '../structures/Guild/Guild';
import isGuildID from '../utils/isGuildID';
import toUuid from '../utils/toUuid';
import Errors from '../Errors';
export default async function (this: any, searchParameter: GuildSearchParameter, query: string) {
if (!query) throw new Error(Errors.NO_GUILD_QUERY);
if ('id' === searchParameter && !isGuildID(query)) throw new Error(Errors.INVALID_GUILD_ID);
const isPlayerQuery = 'player' === searchParameter;
if (isPlayerQuery) query = await toUuid(query, this.options.mojangCacheTime, this.options.useThirdPartyAPI);
if (!['id', 'name', 'player'].includes(searchParameter)) throw new Error(Errors.INVALID_GUILD_SEARCH_PARAMETER);
// eslint-disable-next-line no-underscore-dangle
const res = await this._makeRequest(`/guild?${searchParameter}=${encodeURI(query)}`);
if (res.raw) return res;
if (!res.guild && 'player' !== searchParameter) {
throw new Error(Errors.GUILD_DOES_NOT_EXIST);
}

return res.guild ? new Guild(res.guild, isPlayerQuery ? query : undefined) : null;
}
7 changes: 7 additions & 0 deletions src/API/getGuildAchievements.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import GuildAchievements from '../structures/Static/GuildAchievements';
export default async function (this: any) {
// eslint-disable-next-line no-underscore-dangle
const res = await this._makeRequest('/resources/guilds/achievements');
if (res.raw) return res;
return new GuildAchievements(res);
}
16 changes: 16 additions & 0 deletions src/API/getLeaderboards.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import Leaderboard from '../structures/Leaderboard';
import Constants from '../utils/Constants';
import Errors from '../Errors';
export default async function (this: any) {
// eslint-disable-next-line no-underscore-dangle
const res = await this._makeRequest('/leaderboards');
if (res.raw) return res;
if (!res.leaderboards) throw new Error(Errors.SOMETHING_WENT_WRONG.replace(/{cause}/, 'Try again.'));
const lbnames = Object.create(Constants.leaderboardNames);
for (const name in lbnames) {
lbnames[name] = res.leaderboards[lbnames[name]].length
? res.leaderboards[lbnames[name]].map((lb: any) => new Leaderboard(lb))
: [];
}
return lbnames;
}
23 changes: 23 additions & 0 deletions src/API/getPlayer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import Errors from '../Errors';
import toUuid from '../utils/toUuid';
import getGuild from './getGuild';
import getRecentGames from './getRecentGames';
import Player from '../structures/Player';
export default async function (this: any, query: string, options = { guild: false, recentGames: false }) {
if (!query) throw new Error(Errors.NO_NICKNAME_UUID);
query = await toUuid(query, this.options.mojangCacheTime, this.options.useThirdPartyAPI);
// eslint-disable-next-line no-underscore-dangle
const res = await this._makeRequest(`/player?uuid=${query}`);
if (res.raw) return res;
if (query && !res.player) throw new Error(Errors.PLAYER_HAS_NEVER_LOGGED);
let guild = null;
let recentGames = null;
if (options.guild) {
guild = getGuild.call(this, 'player', query);
}
if (options.recentGames) {
recentGames = getRecentGames.call(this, query);
}
[guild, recentGames] = await Promise.all([guild, recentGames]);
return new Player(res.player, { guild, recentGames });
}
7 changes: 7 additions & 0 deletions src/API/getQuests.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import Quests from '../structures/Static/Quests';
export default async function (this: any) {
// eslint-disable-next-line no-underscore-dangle
const res = await this._makeRequest('/resources/quests');
if (res.raw) return res;
return new Quests(res);
}
14 changes: 14 additions & 0 deletions src/API/getRecentGames.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import RecentGame from '../structures/RecentGame';
import toUuid from '../utils/toUuid';
import Errors from '../Errors';
export default async function (this: any, query: string) {
if (!query) throw new Error(Errors.NO_NICKNAME_UUID);
query = await toUuid(query, this.options.mojangCacheTime, this.options.useThirdPartyAPI);
// eslint-disable-next-line no-underscore-dangle
const res = await this._makeRequest(`/recentgames?uuid=${query}`);
if (res.raw) return res;
if (0 === res.games.length) {
return [];
}
return res.games.map((x: any) => new RecentGame(x));
}
9 changes: 9 additions & 0 deletions src/API/getStatus.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import Status from '../structures/Status';
import toUuid from '../utils/toUuid';
export default async function (this: any, query: string) {
query = await toUuid(query, this.options.mojangCacheTime, this.options.useThirdPartyAPI);
// eslint-disable-next-line no-underscore-dangle
const res = await this._makeRequest(`/status?uuid=${query}`);
if (res.raw) return res;
return new Status(res.session);
}
8 changes: 8 additions & 0 deletions src/API/getWatchdogStats.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import WatchdogStats from '../structures/Watchdog/Stats';

export default async function (this:any) {
// eslint-disable-next-line no-underscore-dangle
const res = await this._makeRequest('/punishmentstats');
if (res.raw) return res;
return new WatchdogStats(res);
}
7 changes: 7 additions & 0 deletions src/API/housing/getActiveHouses.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import House from '../../structures/House';
export default async function (this: any) {
// eslint-disable-next-line no-underscore-dangle
const res = await this._makeRequest('/housing/active');
if (res.raw) return res;
return res.length ? res.map((b: any) => new House(b)) : [];
}
9 changes: 9 additions & 0 deletions src/API/housing/getHouse.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import House from '../../structures/House';
import Errors from '../../Errors';
export default async function (this: any, query: string) {
if (!query) throw new Error(Errors.NO_UUID);
// eslint-disable-next-line no-underscore-dangle
const res = await this._makeRequest(`/housing/house?house=${query}`);
if (res.raw) return res;
return new House(res);
}
11 changes: 11 additions & 0 deletions src/API/housing/getPlayerHouses.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import House from '../../structures/House';
import toUuid from '../../utils/toUuid';
import Errors from '../../Errors';
export default async function (this: any, query: string) {
if (!query) throw new Error(Errors.NO_NICKNAME_UUID);
query = await toUuid(query, this.options.mojangCacheTime, this.options.useThirdPartyAPI);
// eslint-disable-next-line no-underscore-dangle
const res = await this._makeRequest(`/housing/houses?player=${query}`);
if (res.raw) return res;
return res.length ? res.map((h: any) => new House(h)) : [];
}
67 changes: 67 additions & 0 deletions src/API/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import getAchievements from './getAchievements';
import getAPIStatus from './getAPIStatus';
import getBoosters from './getBoosters';
import getChallenges from './getChallenges';
import getGameCounts from './getGameCounts';
import getGuild from './getGuild';
import getGuildAchievements from './getGuildAchievements';
import getLeaderboards from './getLeaderboards';
import getPlayer from './getPlayer';
import getQuests from './getQuests';
import getRecentGames from './getRecentGames';
import getStatus from './getStatus';
import getWatchdogStats from './getWatchdogStats';

import getSkyblockAuction from './skyblock/getAuction';
import getSkyblockAuctions from './skyblock/getAuctions';
import getSkyblockAuctionsByPlayer from './skyblock/getAuctionsByPlayer';
import getSkyblockBazaar from './skyblock/getBazaar';
import getSkyblockBingo from './skyblock/getBingo';
import getSkyblockBingoByPlayer from './skyblock/getBingoByPlayer';
import getSkyblockEndedAuctions from './skyblock/getEndedAuctions';
import getSkyblockFireSales from './skyblock/getFireSales';
import getSkyblockGarden from './skyblock/getGarden';
import getSkyblockGovernment from './skyblock/getGovernment';
import getSkyblockMember from './skyblock/getMember';
import getSkyblockMuseum from './skyblock/getMuseum';
import getSkyblockNews from './skyblock/getNews';
import getSkyblockProfiles from './skyblock/getProfiles';

import getActiveHouses from './housing/getActiveHouses';
import getPlayerHouses from './housing/getPlayerHouses';
import getHouse from './housing/getHouse';

export default {
getAchievements,
getAPIStatus,
getBoosters,
getChallenges,
getGameCounts,
getGuild,
getGuildAchievements,
getLeaderboards,
getPlayer,
getQuests,
getRecentGames,
getStatus,
getWatchdogStats,

getSkyblockAuction,
getSkyblockAuctions,
getSkyblockAuctionsByPlayer,
getSkyblockBazaar,
getSkyblockBingo,
getSkyblockBingoByPlayer,
getSkyblockEndedAuctions,
getSkyblockFireSales,
getSkyblockGarden,
getSkyblockGovernment,
getSkyblockMember,
getSkyblockMuseum,
getSkyblockNews,
getSkyblockProfiles,

getActiveHouses,
getPlayerHouses,
getHouse
};
23 changes: 23 additions & 0 deletions src/API/skyblock/getAuction.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import Auction from '../../structures/SkyBlock/Auctions/Auction';
import { ActionFilterType } from '../../typings';
import toUuid from '../../utils/toUuid';
import Errors from '../../Errors';

export default async function (this: any, type: ActionFilterType, query: string, includeItemBytes = false) {
if (!query) throw new Error(Errors.NO_NICKNAME_UUID);
let filter;
if ('PROFILE' === type) {
filter = 'profile';
} else if ('PLAYER' === type) {
query = await toUuid(query, this.options.mojangCacheTime, this.options.useThirdPartyAPI);
filter = 'player';
} else if ('AUCTION' === type) {
filter = 'uuid';
} else {
throw new Error(Errors.BAD_AUCTION_FILTER);
}
// eslint-disable-next-line no-underscore-dangle
const res = await this._makeRequest(`/skyblock/auction?${filter}=${query}`);
if (res.raw) return res;
return res.auctions.length ? res.auctions.map((a: any) => new Auction(a, includeItemBytes)) : [];
}
71 changes: 71 additions & 0 deletions src/API/skyblock/getAuctions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import AuctionInfo from '../../structures/SkyBlock/Auctions/AuctionInfo';
import Auction from '../../structures/SkyBlock/Auctions/Auction';
import Errors from '../../Errors';
async function getPage(this: any, page: any = 0, options: any = {}) {
// eslint-disable-next-line no-underscore-dangle
const content = await this._makeRequest(`/skyblock/auctions?page=${page}`, false);
const result: any = {};
if (!options.noInfo) result.info = new AuctionInfo(content);
if (options.raw) result.auctions = content.auctions;
else if (options.noAuctions) result.auctions = [];
else result.auctions = content.auctions.map((x: any) => new Auction(x, options.includeItemBytes));
return result;
}
async function noReject(promise: any, args: any = [], retries: any = 3, cooldown: any = 100) {
try {
const result = await promise.call(null, ...args);
return result;
} catch {
if (retries) {
await new Promise((resolve) => setTimeout(resolve, cooldown));
return await noReject(promise, args, retries - 1, cooldown);
}
return null;
}
}

export default async function (range: any, options: any = {}) {
options.retries ||= 3;
options.cooldown ||= 100;
if (null === range || '*' === range) range = [0, (await getPage(0, { noAuctions: true })).info.totalPages];
if (!Array.isArray(range)) range = [parseInt(range), parseInt(range)];
if (isNaN(range[0])) throw new Error(Errors.PAGE_INDEX_ERROR);
if (parseInt(options.retries) !== options.retries || 10 < options.retries || 0 > options.retries) {
throw new Error(Errors.INVALID_OPTION_VALUE);
}
if (parseInt(options.cooldown) !== options.cooldown || 3000 < options.cooldown || 0 > options.cooldown) {
throw new Error(Errors.INVALID_OPTION_VALUE);
}
range = range.sort();
const result: any = { auctions: [] };
const fetches = [];
const failedPages = [];
if (options.noAuctions) return { info: options.noInfo ? null : (await getPage(range[1], { noAuctions: true })).info };
for (let i = range[0]; i <= range[1]; i++) {
if (options.race) {
fetches.push(noReject(getPage, [i, options], options.retries, options.cooldown));
} else {
const resp = await noReject(getPage, [i, options], options.retries, options.cooldown);
if (resp) {
result.auctions = result.auctions.concat(resp.auctions);
if (resp.info) result.info = resp.info;
} else {
failedPages.push(i);
}
}
}
if (fetches.length) {
result.auctions = (await Promise.all(fetches)).reduce((pV, cV, index) => {
if (!cV) {
failedPages.push(index + range[0]);
return pV;
}
if (cV.info) result.info = cV.info;
if (cV.auctions.length) return pV.concat(cV.auctions);
return pV;
}, []);
}
// eslint-disable-next-line no-underscore-dangle
result.info = result.info ? result.info._extend('failedPages', failedPages) : { failedPages };
return result;
}
11 changes: 11 additions & 0 deletions src/API/skyblock/getAuctionsByPlayer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import Auction from '../../structures/SkyBlock/Auctions/Auction';
import toUuid from '../../utils/toUuid';
import Errors from '../../Errors';
export default async function (this: any, query: string, includeItemBytes = false) {
if (!query) throw new Error(Errors.NO_NICKNAME_UUID);
query = await toUuid(query, this.options.mojangCacheTime, this.options.useThirdPartyAPI);
// eslint-disable-next-line no-underscore-dangle
const res = await this._makeRequest(`/skyblock/auction?player=${query}`);
if (res.raw) return res;
return res.auctions.length ? res.auctions.map((a: any) => new Auction(a, includeItemBytes)) : [];
}
8 changes: 8 additions & 0 deletions src/API/skyblock/getBazaar.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import Product from '../../structures/SkyBlock/Bazzar/Product';
export default async function (this: any) {
// eslint-disable-next-line no-underscore-dangle
const res = await this._makeRequest('/skyblock/bazaar');
if (res.raw) return res;
const productsKeys = Object.keys(res.products);
return productsKeys.map((x) => new Product(res.products[x]));
}
7 changes: 7 additions & 0 deletions src/API/skyblock/getBingo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import BingoData from '../../structures/SkyBlock/Static/BingoData';
export default async function (this: any) {
// eslint-disable-next-line no-underscore-dangle
const res = await this._makeRequest('/resources/skyblock/bingo');
if (res.raw) return res;
return new BingoData(res);
}
14 changes: 14 additions & 0 deletions src/API/skyblock/getBingoByPlayer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import PlayerBingo from '../../structures/SkyBlock/PlayerBingo';
import toUuid from '../../utils/toUuid';
import getBingo from './getBingo';
import Errors from '../../Errors';
export default async function (this: any, query: string, { fetchBingoData = false }) {
if (!query) throw new Error(Errors.NO_NICKNAME_UUID);
query = await toUuid(query, this.options.mojangCacheTime, this.options.useThirdPartyAPI);
// eslint-disable-next-line no-underscore-dangle
const res = await this._makeRequest(`/skyblock/uuid?player=${query}`);
if (res.raw) return res;
let bingoData = null;
if (fetchBingoData) bingoData = await getBingo.call(this);
return new PlayerBingo(res, bingoData);
}
Loading

0 comments on commit d74cf02

Please sign in to comment.