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

Commit

Permalink
Rewrite Errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Kathund committed Aug 14, 2024
1 parent d41601f commit 330f713
Show file tree
Hide file tree
Showing 19 changed files with 207 additions and 199 deletions.
85 changes: 0 additions & 85 deletions API/getSkyblockAuctions.ts

This file was deleted.

11 changes: 6 additions & 5 deletions src/API/getGuild.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import Guild from '../structures/Guild/Guild';
import isGuildID from '../utils/isGuildID';
import Endpoint from '../Private/Endpoint';
import toUuid from '../utils/toUuid';
import Errors from '../Errors';
import Client from '../Client';

export default class getGuild extends Endpoint {
Expand All @@ -13,15 +12,17 @@ export default class getGuild extends Endpoint {
}

async execute(searchParameter: 'id' | 'name' | 'player', query: string): Promise<Guild | null> {
if (!query) throw new Error(Errors.NO_GUILD_QUERY);
if ('id' === searchParameter && !isGuildID(query)) throw new Error(Errors.INVALID_GUILD_ID);
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 (!['id', 'name', 'player'].includes(searchParameter)) throw new Error(Errors.INVALID_GUILD_SEARCH_PARAMETER);
if (!['id', 'name', 'player'].includes(searchParameter)) {
throw new Error(this.client.errors.INVALID_GUILD_SEARCH_PARAMETER);
}
const res = await this.client.requests.request(`/guild?${searchParameter}=${encodeURI(query)}`);
if (res.raw) return res;
if (!res.guild && 'player' !== searchParameter) {
throw new Error(Errors.GUILD_DOES_NOT_EXIST);
throw new Error(this.client.errors.GUILD_DOES_NOT_EXIST);
}

return res.guild ? new Guild(res.guild, isPlayerQuery ? query : undefined) : null;
Expand Down
3 changes: 1 addition & 2 deletions src/API/getHouse.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 Errors from '../Errors';
import Client from '../Client';

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

async execute(query: string): Promise<House> {
if (!query) throw new Error(Errors.NO_UUID);
if (!query) throw new Error(this.client.errors.NO_UUID);
const res = await this.client.requests.request(`/housing/house?house=${query}`);
if (res.raw) return res;
return new House(res);
Expand Down
3 changes: 1 addition & 2 deletions src/API/getLeaderboards.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import Leaderboard from '../structures/Leaderboard';
import Constants from '../utils/Constants';
import Endpoint from '../Private/Endpoint';
import Errors from '../Errors';
import Client from '../Client';

export default class getLeaderboards extends Endpoint {
Expand All @@ -14,7 +13,7 @@ export default class getLeaderboards extends Endpoint {
async execute(): Promise<any> {
const res = await this.client.requests.request('/leaderboards');
if (res.raw) return res;
if (!res.leaderboards) throw new Error(Errors.SOMETHING_WENT_WRONG.replace(/{cause}/, 'Try again.'));
if (!res.leaderboards) throw new Error(this.client.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
Expand Down
5 changes: 2 additions & 3 deletions src/API/getPlayer.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import Endpoint from '../Private/Endpoint';
import Player from '../structures/Player';
import toUuid from '../utils/toUuid';
import Errors from '../Errors';
import Client from '../Client';

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

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

export default class getSkyblockAction extends Endpoint {
Expand All @@ -16,7 +15,7 @@ export default class getSkyblockAction extends Endpoint {
type: 'PROFILE' | 'PLAYER' | 'AUCTION',
includeItemBytes: boolean = false
): Promise<Auction[]> {
if (!query) throw new Error(Errors.NO_NICKNAME_UUID);
if (!query) throw new Error(this.client.errors.NO_NICKNAME_UUID);
let filter;
if ('PROFILE' === type) {
filter = 'profile';
Expand All @@ -26,7 +25,7 @@ export default class getSkyblockAction extends Endpoint {
} else if ('AUCTION' === type) {
filter = 'uuid';
} else {
throw new Error(Errors.BAD_AUCTION_FILTER);
throw new Error(this.client.errors.BAD_AUCTION_FILTER);
}
const res = await this.client.requests.request(`/skyblock/auction?${filter}=${query}`);
if (res.raw) return res;
Expand Down
85 changes: 85 additions & 0 deletions src/API/getSkyblockAuctions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import AuctionInfo from '../structures/SkyBlock/Auctions/AuctionInfo';
import Auction from '../structures/SkyBlock/Auctions/Auction';
import Endpoint from '../Private/Endpoint';
import Client from '../Client';

export default class getSkyblockAuctions extends Endpoint {
readonly client: Client;
readonly name: string;
constructor(client: Client) {
super(client);
this.client = client;
this.name = 'getSkyblockAuctions';
}

async execute(range: any, options: any) {
options.retries ||= 3;
options.cooldown ||= 100;
if (null === range || '*' === range) range = [0, (await this.getPage(0, { noAuctions: true })).info.totalPages];
if (!Array.isArray(range)) range = [parseInt(range), parseInt(range)];
if (isNaN(range[0])) throw new Error(this.client.errors.PAGE_INDEX_ERROR);
if (parseInt(options.retries) !== options.retries || 10 < options.retries || 0 > options.retries) {
throw new Error(this.client.errors.INVALID_OPTION_VALUE);
}
if (parseInt(options.cooldown) !== options.cooldown || 3000 < options.cooldown || 0 > options.cooldown) {
throw new Error(this.client.errors.INVALID_OPTION_VALUE);
}
range = range.sort();
const result: any = { auctions: [] };
const fetches = [];
const failedPages = [];
if (options.noAuctions) {
return { info: options.noInfo ? null : (await this.getPage(range[1], { noAuctions: true })).info };
}
for (let i = range[0]; i <= range[1]; i++) {
if (options.race) {
fetches.push(this.noReject(this.getPage, [i, options], options.retries, options.cooldown));
} else {
const resp = await this.noReject(this.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;
}

async getPage(page: any = 0, options: any = {}): Promise<any> {
const content = await this.client.requests.request(`/skyblock/auctions?page=${page}`);
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 noReject(promise: any, args: any = [], retries: any = 3, cooldown: any = 100): Promise<any> {
try {
const result = await promise.call(null, ...args);
return result;
} catch {
if (retries) {
await new Promise((resolve) => setTimeout(resolve, cooldown));
return await this.noReject(promise, args, retries - 1, cooldown);
}
return null;
}
}
}
3 changes: 1 addition & 2 deletions src/API/getSkyblockAuctionsByPlayer.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import Auction from '../structures/SkyBlock/Auctions/Auction';
import Endpoint from '../Private/Endpoint';
import toUuid from '../utils/toUuid';
import Errors from '../Errors';
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(Errors.NO_NICKNAME_UUID);
if (!query) throw new Error(this.client.errors.NO_NICKNAME_UUID);
query = await toUuid(query);
const res = await this.client.requests.request(`/skyblock/auction?player=${query}`);
if (res.raw) return res;
Expand Down
3 changes: 1 addition & 2 deletions src/API/getSkyblockBingoByPlayer.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import PlayerBingo from '../structures/SkyBlock/PlayerBingo';
import Endpoint from '../Private/Endpoint';
import toUuid from '../utils/toUuid';
import Errors from '../Errors';
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(Errors.NO_NICKNAME_UUID);
if (!query) throw new Error(this.client.errors.NO_NICKNAME_UUID);
query = await toUuid(query);
const res = await this.client.requests.request(`/skyblock/uuid?player=${query}`);
if (res.raw) return res;
Expand Down
5 changes: 2 additions & 3 deletions src/API/getSkyblockMember.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import SkyblockMember from '../structures/SkyBlock/SkyblockMember';
import Endpoint from '../Private/Endpoint';
import toUuid from '../utils/toUuid';
import Errors from '../Errors';
import Client from '../Client';

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

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

0 comments on commit 330f713

Please sign in to comment.