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

Commit

Permalink
Rewrite toUUID
Browse files Browse the repository at this point in the history
  • Loading branch information
Kathund committed Aug 14, 2024
1 parent 330f713 commit 49093ef
Show file tree
Hide file tree
Showing 13 changed files with 47 additions and 47 deletions.
3 changes: 1 addition & 2 deletions src/API/getGuild.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import Guild from '../structures/Guild/Guild';
import isGuildID from '../utils/isGuildID';
import Endpoint from '../Private/Endpoint';
import toUuid from '../utils/toUuid';
import Client from '../Client';

export default class getGuild extends Endpoint {
Expand All @@ -15,7 +14,7 @@ export default class getGuild extends Endpoint {
if (!query) throw new Error(this.client.errors.NO_GUILD_QUERY);
if ('id' === searchParameter && !isGuildID(query)) throw new Error(this.client.errors.INVALID_GUILD_ID);
const isPlayerQuery = 'player' === searchParameter;
if (isPlayerQuery) query = await toUuid(query);
if (isPlayerQuery) query = await this.client.requests.toUUID(query);
if (!['id', 'name', 'player'].includes(searchParameter)) {
throw new Error(this.client.errors.INVALID_GUILD_SEARCH_PARAMETER);
}
Expand Down
3 changes: 1 addition & 2 deletions src/API/getPlayer.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import Endpoint from '../Private/Endpoint';
import Player from '../structures/Player';
import toUuid from '../utils/toUuid';
import Client from '../Client';

export default class getPlayer extends Endpoint {
Expand All @@ -12,7 +11,7 @@ export default class getPlayer extends Endpoint {

async execute(query: string): Promise<Player> {
if (!query) throw new Error(this.client.errors.NO_NICKNAME_UUID);
query = await toUuid(query);
query = await this.client.requests.toUUID(query);
const res = await this.client.requests.request(`/player?uuid=${query}`);
if (res.raw) return res;
if (query && !res.player) throw new Error(this.client.errors.PLAYER_HAS_NEVER_LOGGED);
Expand Down
3 changes: 1 addition & 2 deletions src/API/getPlayerHouses.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import Endpoint from '../Private/Endpoint';
import House from '../structures/House';
import toUuid from '../utils/toUuid';
import Client from '../Client';

export default class getPlayerHouses extends Endpoint {
Expand All @@ -12,7 +11,7 @@ export default class getPlayerHouses extends Endpoint {

async execute(query: string): Promise<House[]> {
if (!query) throw new Error(this.client.errors.NO_NICKNAME_UUID);
query = await toUuid(query);
query = await this.client.requests.toUUID(query);
const res = await this.client.requests.request(`/housing/houses?player=${query}`);
if (res.raw) return res;
return res.length ? res.map((h: any) => new House(h)) : [];
Expand Down
3 changes: 1 addition & 2 deletions src/API/getRecentGames.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import RecentGame from '../structures/RecentGame';
import Endpoint from '../Private/Endpoint';
import toUuid from '../utils/toUuid';
import Client from '../Client';

export default class getRecentGames extends Endpoint {
Expand All @@ -12,7 +11,7 @@ export default class getRecentGames extends Endpoint {

async execute(query: string): Promise<RecentGame[]> {
if (!query) throw new Error(this.client.errors.NO_NICKNAME_UUID);
query = await toUuid(query);
query = await this.client.requests.toUUID(query);
const res = await this.client.requests.request(`/recentgames?uuid=${query}`);
if (res.raw) return res;
if (0 === res.games.length) {
Expand Down
3 changes: 1 addition & 2 deletions src/API/getSkyblockAuction.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import Auction from '../structures/SkyBlock/Auctions/Auction';
import Endpoint from '../Private/Endpoint';
import toUuid from '../utils/toUuid';
import Client from '../Client';

export default class getSkyblockAction extends Endpoint {
Expand All @@ -20,7 +19,7 @@ export default class getSkyblockAction extends Endpoint {
if ('PROFILE' === type) {
filter = 'profile';
} else if ('PLAYER' === type) {
query = await toUuid(query);
query = await this.client.requests.toUUID(query);
filter = 'player';
} else if ('AUCTION' === type) {
filter = 'uuid';
Expand Down
3 changes: 1 addition & 2 deletions src/API/getSkyblockAuctionsByPlayer.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import Auction from '../structures/SkyBlock/Auctions/Auction';
import Endpoint from '../Private/Endpoint';
import toUuid from '../utils/toUuid';
import Client from '../Client';

export default class getSkyblockActionsByPlayer extends Endpoint {
Expand All @@ -12,7 +11,7 @@ export default class getSkyblockActionsByPlayer extends Endpoint {

async execute(query: string, includeItemBytes: boolean): Promise<Auction[]> {
if (!query) throw new Error(this.client.errors.NO_NICKNAME_UUID);
query = await toUuid(query);
query = await this.client.requests.toUUID(query);
const res = await this.client.requests.request(`/skyblock/auction?player=${query}`);
if (res.raw) return res;
return res.auctions.length ? res.auctions.map((a: any) => new Auction(a, includeItemBytes)) : [];
Expand Down
3 changes: 1 addition & 2 deletions src/API/getSkyblockBingoByPlayer.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import PlayerBingo from '../structures/SkyBlock/PlayerBingo';
import Endpoint from '../Private/Endpoint';
import toUuid from '../utils/toUuid';
import Client from '../Client';

export default class getBingoByPlayer extends Endpoint {
Expand All @@ -12,7 +11,7 @@ export default class getBingoByPlayer extends Endpoint {

async execute(query: string): Promise<PlayerBingo> {
if (!query) throw new Error(this.client.errors.NO_NICKNAME_UUID);
query = await toUuid(query);
query = await this.client.requests.toUUID(query);
const res = await this.client.requests.request(`/skyblock/uuid?player=${query}`);
if (res.raw) return res;
return new PlayerBingo(res);
Expand Down
3 changes: 1 addition & 2 deletions src/API/getSkyblockMember.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import SkyblockMember from '../structures/SkyBlock/SkyblockMember';
import Endpoint from '../Private/Endpoint';
import toUuid from '../utils/toUuid';
import Client from '../Client';

export default class getSkyblockMember extends Endpoint {
Expand All @@ -12,7 +11,7 @@ export default class getSkyblockMember extends Endpoint {

async execute(query: string): Promise<Map<string, SkyblockMember>> {
if (!query) throw new Error(this.client.errors.NO_NICKNAME_UUID);
query = await toUuid(query);
query = await this.client.requests.toUUID(query);
const res = await this.client.requests.request(`/skyblock/profiles?uuid=${query}`);
if (res.raw) return res;
if (!res.profiles || !res.profiles.length) throw new Error(this.client.errors.NO_SKYBLOCK_PROFILES);
Expand Down
3 changes: 1 addition & 2 deletions src/API/getSkyblockMuseum.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import SkyblockMuseum from '../structures/SkyBlock/SkyblockMuseum';
import Endpoint from '../Private/Endpoint';
import toUuid from '../utils/toUuid';
import Client from '../Client';

export default class getSkyblockMuseum extends Endpoint {
Expand All @@ -12,7 +11,7 @@ export default class getSkyblockMuseum extends Endpoint {

async execute(query: string, profileId: string): Promise<SkyblockMuseum> {
if (!query) throw new Error(this.client.errors.NO_NICKNAME_UUID);
query = await toUuid(query);
query = await this.client.requests.toUUID(query);
const res = await this.client.requests.request(`/skyblock/museum?uuid=${query}&profile=${profileId}`);
if (res.raw) return res;
return new SkyblockMuseum({
Expand Down
3 changes: 1 addition & 2 deletions src/API/getSkyblockProfiles.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import SkyblockProfile from '../structures/SkyBlock/SkyblockProfile';
import Endpoint from '../Private/Endpoint';
import toUuid from '../utils/toUuid';
import Client from '../Client';
export default class getSkyblockProfiles extends Endpoint {
readonly client: Client;
Expand All @@ -11,7 +10,7 @@ export default class getSkyblockProfiles extends Endpoint {

async execute(query: string): Promise<SkyblockProfile[]> {
if (!query) throw new Error(this.client.errors.NO_NICKNAME_UUID);
query = await toUuid(query);
query = await this.client.requests.toUUID(query);
const res = await this.client.requests.request(`/skyblock/profiles?uuid=${query}`);
if (res.raw) return res;
if (!res.profiles || !res.profiles.length) throw new Error(this.client.errors.NO_SKYBLOCK_PROFILES);
Expand Down
3 changes: 1 addition & 2 deletions src/API/getStatus.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import Endpoint from '../Private/Endpoint';
import Status from '../structures/Status';
import toUuid from '../utils/toUuid';
import Client from '../Client';

export default class getStatus extends Endpoint {
Expand All @@ -11,7 +10,7 @@ export default class getStatus extends Endpoint {
}

async execute(query: string): Promise<Status> {
query = await toUuid(query);
query = await this.client.requests.toUUID(query);
const res = await this.client.requests.request(`/status?uuid=${query}`);
if (res.raw) return res;
return new Status(res.session);
Expand Down
36 changes: 36 additions & 0 deletions src/Private/Requests.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const BASE_URL = 'https://api.hypixel.net/v2';
import isUUID from '../utils/isUUID';
import Client from '../Client';
import axios from 'axios';

Expand Down Expand Up @@ -56,6 +57,41 @@ class Requests {

return parsedRes;
}

async toUUID(input: string): Promise<string> {
if (!input) throw new Error(this.client.errors.NO_NICKNAME_UUID);
if ('string' !== typeof input) throw new Error(this.client.errors.UUID_NICKNAME_MUST_BE_A_STRING);
if (isUUID(input)) return input.replace(/-/g, '');
const url = `https://mowojang.matdoes.dev/${input}`;
if (this.client.cacheHandler.has(url)) {
return this.client.cacheHandler.get(url);
}
const res = await axios.get(url);
if (500 <= res.status && 528 > res.status) {
throw new Error(
this.client.errors.ERROR_STATUSTEXT.replace(/{statustext}/, `Server Error : ${res.status} ${res.statusText}`)
);
}
const parsedRes = await res.data;
if (400 === res.status) {
throw new Error(
this.client.errors.ERROR_CODE_CAUSE.replace(/{code}/, '400 Bad Request').replace(
/{cause}/,
parsedRes.cause || ''
)
);
}
if (200 !== res.status) {
throw new Error(this.client.errors.ERROR_STATUSTEXT.replace(/{statustext}/, res.statusText));
}
if ('string' !== typeof parsedRes.id || 'string' !== typeof parsedRes.name) {
throw new Error(this.client.errors.MALFORMED_UUID);
}
if (this.client.options.cache) {
this.client.cacheHandler.set(url, parsedRes.id);
}
return parsedRes.id;
}
}

export default Requests;
25 changes: 0 additions & 25 deletions src/utils/toUuid.ts

This file was deleted.

0 comments on commit 49093ef

Please sign in to comment.