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

Commit

Permalink
Rewrite (#6)
Browse files Browse the repository at this point in the history
* clear

* Rewrite Info

* Fix endpoint importing

* Rewrite types

* fix

* better exports
  • Loading branch information
Kathund authored Aug 2, 2024
1 parent 17b90b6 commit a72d54a
Show file tree
Hide file tree
Showing 89 changed files with 1,618 additions and 1,802 deletions.
85 changes: 85 additions & 0 deletions API/getSkyblockAuctions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import AuctionInfo from '../src/structures/SkyBlock/Auctions/AuctionInfo';
import Auction from '../src/structures/SkyBlock/Auctions/Auction';
import Errors from '../src/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;
}
}

import Endpoint from '../src/Private/Endpoint';
import Client from '../src/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 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;
}
}
8 changes: 0 additions & 8 deletions src/API/getAPIStatus.ts

This file was deleted.

21 changes: 16 additions & 5 deletions src/API/getAchievements.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@
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);
import Endpoint from '../Private/Endpoint';
import Client from '../Client';
export default class getAchievements extends Endpoint {
readonly client: Client;
readonly name: string;
constructor(client: Client) {
super(client);
this.client = client;
this.name = 'getAchievements';
}

async execute() {
const res = await this.client.requests.request('/resources/achievements');
if (res.raw) return res;
return new Achievements(res);
}
}
18 changes: 18 additions & 0 deletions src/API/getActiveHouses.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import Endpoint from '../Private/Endpoint';
import House from '../structures/House';
import Client from '../Client';
export default class getActiveHouses extends Endpoint {
readonly client: Client;
readonly name: string;
constructor(client: Client) {
super(client);
this.client = client;
this.name = 'getActiveHouses';
}

async execute() {
const res = await this.client.requests.request('/housing/active');
if (res.raw) return res;
return res.length ? res.map((b: any) => new House(b)) : [];
}
}
21 changes: 16 additions & 5 deletions src/API/getBoosters.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@
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() : [];
import Endpoint from '../Private/Endpoint';
import Client from '../Client';
export default class getBoosters extends Endpoint {
readonly client: Client;
readonly name: string;
constructor(client: Client) {
super(client);
this.client = client;
this.name = 'getBoosters';
}

async execute() {
const res = await this.client.requests.request('/boosters');
if (res.raw) return res;
return res.boosters.length ? res.boosters.map((b: any) => new Booster(b)).reverse() : [];
}
}
21 changes: 16 additions & 5 deletions src/API/getChallenges.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@
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);
import Endpoint from '../Private/Endpoint';
import Client from '../Client';
export default class getChallenges extends Endpoint {
readonly client: Client;
readonly name: string;
constructor(client: Client) {
super(client);
this.client = client;
this.name = 'getChallenges';
}

async execute() {
const res = await this.client.requests.request('/resources/challenges');
if (res.raw) return res;
return new Challenges(res);
}
}
21 changes: 16 additions & 5 deletions src/API/getGameCounts.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@
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);
import Endpoint from '../Private/Endpoint';
import Client from '../Client';
export default class getGameCounts extends Endpoint {
readonly client: Client;
readonly name: string;
constructor(client: Client) {
super(client);
this.client = client;
this.name = 'getGameCounts';
}

async execute() {
const res = await this.client.requests.request('/counts');
if (res.raw) return res;
return new GameCounts(res);
}
}
36 changes: 23 additions & 13 deletions src/API/getGuild.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,30 @@
import { GuildSearchParameter } from '../typings';
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';
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);
import Client from '../Client';
export default class getGuild extends Endpoint {
readonly client: Client;
readonly name: string;
constructor(client: Client) {
super(client);
this.client = client;
this.name = 'getGuild';
}

return res.guild ? new Guild(res.guild, isPlayerQuery ? query : undefined) : null;
async execute(searchParameter: 'id' | 'name' | 'player', 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);
if (!['id', 'name', 'player'].includes(searchParameter)) throw new Error(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);
}

return res.guild ? new Guild(res.guild, isPlayerQuery ? query : undefined) : null;
}
}
21 changes: 16 additions & 5 deletions src/API/getGuildAchievements.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@
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);
import Endpoint from '../Private/Endpoint';
import Client from '../Client';
export default class getGuildAchievements extends Endpoint {
readonly client: Client;
readonly name: string;
constructor(client: Client) {
super(client);
this.client = client;
this.name = 'getGuildAchievements';
}

async execute() {
const res = await this.client.requests.request('/resources/guilds/achievements');
if (res.raw) return res;
return new GuildAchievements(res);
}
}
20 changes: 20 additions & 0 deletions src/API/getHouse.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import Endpoint from '../Private/Endpoint';
import House from '../structures/House';
import Errors from '../Errors';
import Client from '../Client';
export default class getHouse extends Endpoint {
readonly client: Client;
readonly name: string;
constructor(client: Client) {
super(client);
this.client = client;
this.name = 'getHouse';
}

async execute(query: string) {
if (!query) throw new Error(Errors.NO_UUID);
const res = await this.client.requests.request(`/housing/house?house=${query}`);
if (res.raw) return res;
return new House(res);
}
}
33 changes: 22 additions & 11 deletions src/API/getLeaderboards.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,27 @@
import Leaderboard from '../structures/Leaderboard';
import Constants from '../utils/Constants';
import Endpoint from '../Private/Endpoint';
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))
: [];
import Client from '../Client';
export default class getLeaderboards extends Endpoint {
readonly client: Client;
readonly name: string;
constructor(client: Client) {
super(client);
this.client = client;
this.name = 'getLeaderboards';
}

async execute() {
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.'));
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;
}
return lbnames;
}
38 changes: 19 additions & 19 deletions src/API/getPlayer.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
import Errors from '../Errors';
import toUuid from '../utils/toUuid';
import getGuild from './getGuild';
import getRecentGames from './getRecentGames';
import Endpoint from '../Private/Endpoint';
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);
import toUuid from '../utils/toUuid';
import Errors from '../Errors';
import Client from '../Client';
export default class getPlayer extends Endpoint {
readonly client: Client;
readonly name: string;
constructor(client: Client) {
super(client);
this.client = client;
this.name = 'getPlayer';
}
if (options.recentGames) {
recentGames = getRecentGames.call(this, query);

async execute(query: string) {
if (!query) throw new Error(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);
return new Player(res.player);
}
[guild, recentGames] = await Promise.all([guild, recentGames]);
return new Player(res.player, { guild, recentGames });
}
Loading

0 comments on commit a72d54a

Please sign in to comment.