Skip to content

Commit

Permalink
Request Options
Browse files Browse the repository at this point in the history
  • Loading branch information
Kathund committed Aug 14, 2024
1 parent f4eaf64 commit 997e754
Show file tree
Hide file tree
Showing 34 changed files with 142 additions and 70 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "reborn-ts",
"name": "hypixel-api-reborn",
"version": "12.0.0",
"description": "Feature-rich Hypixel API wrapper for Node.js",
"main": "./src/index.ts",
Expand Down
14 changes: 14 additions & 0 deletions src/API/API.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { RequestOptions } from '../Private/Requests';

export interface getPlayerRequestOptions extends RequestOptions {
getGuild?: boolean;
}

export interface AuctionRequestOptions extends RequestOptions {
includeItemBytes?: boolean;
}

export interface SkyblockRequestyOptions extends RequestOptions {
garden?: boolean;
museum?: boolean;
}
5 changes: 3 additions & 2 deletions src/API/getAchievements.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Achievements from '../structures/Static/Achievements';
import { RequestOptions } from '../Private/Requests';
import Endpoint from '../Private/Endpoint';
import Client from '../Client';

Expand All @@ -9,8 +10,8 @@ export default class getAchievements extends Endpoint {
this.client = client;
}

async execute(): Promise<Achievements> {
const res = await this.client.requests.request('/resources/achievements');
async execute(options?: RequestOptions): Promise<Achievements> {
const res = await this.client.requests.request('/resources/achievements', options);
if (res.raw) return res;
return new Achievements(res);
}
Expand Down
5 changes: 3 additions & 2 deletions src/API/getActiveHouses.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { RequestOptions } from '../Private/Requests';
import Endpoint from '../Private/Endpoint';
import House from '../structures/House';
import Client from '../Client';
Expand All @@ -9,8 +10,8 @@ export default class getActiveHouses extends Endpoint {
this.client = client;
}

async execute(): Promise<House[]> {
const res = await this.client.requests.request('/housing/active');
async execute(options?: RequestOptions): Promise<House[]> {
const res = await this.client.requests.request('/housing/active', options);
if (res.raw) return res;
return res.length ? res.map((b: any) => new House(b)) : [];
}
Expand Down
5 changes: 3 additions & 2 deletions src/API/getBoosters.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { RequestOptions } from '../Private/Requests';
import Booster from '../structures/Boosters/Booster';
import Endpoint from '../Private/Endpoint';
import Client from '../Client';
Expand All @@ -9,8 +10,8 @@ export default class getBoosters extends Endpoint {
this.client = client;
}

async execute(): Promise<Booster[]> {
const res = await this.client.requests.request('/boosters');
async execute(options?: RequestOptions): Promise<Booster[]> {
const res = await this.client.requests.request('/boosters', options);
if (res.raw) return res;
return res.boosters.length ? res.boosters.map((b: any) => new Booster(b)).reverse() : [];
}
Expand Down
5 changes: 3 additions & 2 deletions src/API/getChallenges.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Challenges from '../structures/Static/Challenges';
import { RequestOptions } from '../Private/Requests';
import Endpoint from '../Private/Endpoint';
import Client from '../Client';

Expand All @@ -9,8 +10,8 @@ export default class getChallenges extends Endpoint {
this.client = client;
}

async execute(): Promise<Challenges> {
const res = await this.client.requests.request('/resources/challenges');
async execute(options?: RequestOptions): Promise<Challenges> {
const res = await this.client.requests.request('/resources/challenges', options);
if (res.raw) return res;
return new Challenges(res);
}
Expand Down
5 changes: 3 additions & 2 deletions src/API/getGameCounts.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { RequestOptions } from '../Private/Requests';
import GameCounts from '../structures/GameCounts';
import Endpoint from '../Private/Endpoint';
import Client from '../Client';
Expand All @@ -9,8 +10,8 @@ export default class getGameCounts extends Endpoint {
this.client = client;
}

async execute(): Promise<GameCounts> {
const res = await this.client.requests.request('/counts');
async execute(options?: RequestOptions): Promise<GameCounts> {
const res = await this.client.requests.request('/counts', options);
if (res.raw) return res;
return new GameCounts(res);
}
Expand Down
9 changes: 7 additions & 2 deletions src/API/getGuild.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { RequestOptions } from '../Private/Requests';
import Guild from '../structures/Guild/Guild';
import isGuildID from '../utils/isGuildID';
import Endpoint from '../Private/Endpoint';
Expand All @@ -10,15 +11,19 @@ export default class getGuild extends Endpoint {
this.client = client;
}

async execute(searchParameter: 'id' | 'name' | 'player', query: string): Promise<Guild | null> {
async execute(
searchParameter: 'id' | 'name' | 'player',
query: string,
options?: RequestOptions
): Promise<Guild | null> {
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 this.client.requests.toUUID(query);
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)}`);
const res = await this.client.requests.request(`/guild?${searchParameter}=${encodeURI(query)}`, options);
if (res.raw) return res;
if (!res.guild && 'player' !== searchParameter) {
throw new Error(this.client.errors.GUILD_DOES_NOT_EXIST);
Expand Down
5 changes: 3 additions & 2 deletions src/API/getGuildAchievements.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import GuildAchievements from '../structures/Static/GuildAchievements';
import { RequestOptions } from '../Private/Requests';
import Endpoint from '../Private/Endpoint';
import Client from '../Client';

Expand All @@ -9,8 +10,8 @@ export default class getGuildAchievements extends Endpoint {
this.client = client;
}

async execute(): Promise<GuildAchievements> {
const res = await this.client.requests.request('/resources/guilds/achievements');
async execute(options?: RequestOptions): Promise<GuildAchievements> {
const res = await this.client.requests.request('/resources/guilds/achievements', options);
if (res.raw) return res;
return new GuildAchievements(res);
}
Expand Down
5 changes: 3 additions & 2 deletions src/API/getHouse.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { RequestOptions } from '../Private/Requests';
import Endpoint from '../Private/Endpoint';
import House from '../structures/House';
import Client from '../Client';
Expand All @@ -9,9 +10,9 @@ export default class getHouse extends Endpoint {
this.client = client;
}

async execute(query: string): Promise<House> {
async execute(query: string, options?: RequestOptions): Promise<House> {
if (!query) throw new Error(this.client.errors.NO_UUID);
const res = await this.client.requests.request(`/housing/house?house=${query}`);
const res = await this.client.requests.request(`/housing/house?house=${query}`, options);
if (res.raw) return res;
return new House(res);
}
Expand Down
5 changes: 3 additions & 2 deletions src/API/getLeaderboards.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { RequestOptions } from '../Private/Requests';
import Leaderboard from '../structures/Leaderboard';
import Constants from '../utils/Constants';
import Endpoint from '../Private/Endpoint';
Expand All @@ -10,8 +11,8 @@ export default class getLeaderboards extends Endpoint {
this.client = client;
}

async execute(): Promise<any> {
const res = await this.client.requests.request('/leaderboards');
async execute(options?: RequestOptions): Promise<any> {
const res = await this.client.requests.request('/leaderboards', options);
if (res.raw) return res;
if (!res.leaderboards) throw new Error(this.client.errors.SOMETHING_WENT_WRONG.replace(/{cause}/, 'Try again.'));
const lbnames = Object.create(Constants.leaderboardNames);
Expand Down
7 changes: 4 additions & 3 deletions src/API/getPlayer.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { getPlayerRequestOptions } from './API';
import Endpoint from '../Private/Endpoint';
import Player from '../structures/Player';
import Client from '../Client';
Expand All @@ -9,12 +10,12 @@ export default class getPlayer extends Endpoint {
this.client = client;
}

async execute(query: string): Promise<Player> {
async execute(query: string, options?: getPlayerRequestOptions): Promise<Player> {
if (!query) throw new Error(this.client.errors.NO_NICKNAME_UUID);
query = await this.client.requests.toUUID(query);
const res = await this.client.requests.request(`/player?uuid=${query}`);
const res = await this.client.requests.request(`/player?uuid=${query}`, options);
if (res.raw) return res;
if (query && !res.player) throw new Error(this.client.errors.PLAYER_HAS_NEVER_LOGGED);
return new Player(res.player);
return new Player(res.player, options?.getGuild ? await this.client.getGuild('player', query) : undefined);
}
}
5 changes: 3 additions & 2 deletions src/API/getPlayerHouses.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { RequestOptions } from '../Private/Requests';
import Endpoint from '../Private/Endpoint';
import House from '../structures/House';
import Client from '../Client';
Expand All @@ -9,10 +10,10 @@ export default class getPlayerHouses extends Endpoint {
this.client = client;
}

async execute(query: string): Promise<House[]> {
async execute(query: string, options?: RequestOptions): Promise<House[]> {
if (!query) throw new Error(this.client.errors.NO_NICKNAME_UUID);
query = await this.client.requests.toUUID(query);
const res = await this.client.requests.request(`/housing/houses?player=${query}`);
const res = await this.client.requests.request(`/housing/houses?player=${query}`, options);
if (res.raw) return res;
return res.length ? res.map((h: any) => new House(h)) : [];
}
Expand Down
5 changes: 3 additions & 2 deletions src/API/getQuests.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { RequestOptions } from '../Private/Requests';
import Quests from '../structures/Static/Quests';
import Endpoint from '../Private/Endpoint';
import Client from '../Client';
Expand All @@ -9,8 +10,8 @@ export default class getQuests extends Endpoint {
this.client = client;
}

async execute(): Promise<Quests> {
const res = await this.client.requests.request('/resources/quests');
async execute(options?: RequestOptions): Promise<Quests> {
const res = await this.client.requests.request('/resources/quests', options);
if (res.raw) return res;
return new Quests(res);
}
Expand Down
5 changes: 3 additions & 2 deletions src/API/getRecentGames.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { RequestOptions } from '../Private/Requests';
import RecentGame from '../structures/RecentGame';
import Endpoint from '../Private/Endpoint';
import Client from '../Client';
Expand All @@ -9,10 +10,10 @@ export default class getRecentGames extends Endpoint {
this.client = client;
}

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

Expand All @@ -12,7 +13,7 @@ export default class getSkyblockAction extends Endpoint {
async execute(
query: string,
type: 'PROFILE' | 'PLAYER' | 'AUCTION',
includeItemBytes: boolean = false
options?: AuctionRequestOptions
): Promise<Auction[]> {
if (!query) throw new Error(this.client.errors.NO_NICKNAME_UUID);
let filter;
Expand All @@ -26,8 +27,8 @@ export default class getSkyblockAction extends Endpoint {
} else {
throw new Error(this.client.errors.BAD_AUCTION_FILTER);
}
const res = await this.client.requests.request(`/skyblock/auction?${filter}=${query}`);
const res = await this.client.requests.request(`/skyblock/auction?${filter}=${query}`, options);
if (res.raw) return res;
return res.auctions.length ? res.auctions.map((a: any) => new Auction(a, includeItemBytes)) : [];
return res.auctions.length ? res.auctions.map((a: any) => new Auction(a, options?.includeItemBytes ?? false)) : [];
}
}
34 changes: 26 additions & 8 deletions src/API/getSkyblockAuctions.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,35 @@
import AuctionInfo from '../structures/SkyBlock/Auctions/AuctionInfo';
import Auction from '../structures/SkyBlock/Auctions/Auction';
import { AuctionRequestOptions } from './API';
import Endpoint from '../Private/Endpoint';
import Client from '../Client';

export interface getSkyblockAuctionsOptions extends AuctionRequestOptions {
retries?: number;
cooldown?: number;
race?: boolean;
noAuctions?: boolean;
noInfo?: boolean;
}

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) {
async execute(range: any, options?: getSkyblockAuctionsOptions) {
options = this.parasOptions(options);
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) {
if (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) {
if (options.cooldown || 3000 < options.cooldown || 0 > options.cooldown) {
throw new Error(this.client.errors.INVALID_OPTION_VALUE);
}
range = range.sort();
Expand Down Expand Up @@ -60,13 +68,13 @@ export default class getSkyblockAuctions extends Endpoint {
return result;
}

async getPage(page: any = 0, options: any = {}): Promise<any> {
const content = await this.client.requests.request(`/skyblock/auctions?page=${page}`);
async getPage(page: number, options: getSkyblockAuctionsOptions): Promise<any> {
const content = await this.client.requests.request(`/skyblock/auctions?page=${page}`, options);
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));
else result.auctions = content.auctions.map((x: any) => new Auction(x, options?.includeItemBytes ?? false));
return result;
}

Expand All @@ -82,4 +90,14 @@ export default class getSkyblockAuctions extends Endpoint {
return null;
}
}

private parasOptions(options?: getSkyblockAuctionsOptions): getSkyblockAuctionsOptions {
return {
retries: options?.retries ?? 3,
cooldown: options?.cooldown ?? 100,
race: options?.race ?? false,
noAuctions: options?.noAuctions ?? false,
noInfo: options?.noInfo ?? false
};
}
}
5 changes: 3 additions & 2 deletions src/API/getSkyblockAuctionsByPlayer.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Auction from '../structures/SkyBlock/Auctions/Auction';
import { AuctionRequestOptions } from './API';
import Endpoint from '../Private/Endpoint';
import Client from '../Client';

Expand All @@ -9,11 +10,11 @@ export default class getSkyblockActionsByPlayer extends Endpoint {
this.client = client;
}

async execute(query: string, includeItemBytes: boolean): Promise<Auction[]> {
async execute(query: string, options?: AuctionRequestOptions): Promise<Auction[]> {
if (!query) throw new Error(this.client.errors.NO_NICKNAME_UUID);
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)) : [];
return res.auctions.length ? res.auctions.map((a: any) => new Auction(a, options?.includeItemBytes ?? false)) : [];
}
}
5 changes: 3 additions & 2 deletions src/API/getSkyblockBazaar.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Product from '../structures/SkyBlock/Bazzar/Product';
import { RequestOptions } from '../Private/Requests';
import Endpoint from '../Private/Endpoint';
import Client from '../Client';

Expand All @@ -9,8 +10,8 @@ export default class getSkyblockBazaar extends Endpoint {
this.client = client;
}

async execute(): Promise<Product[]> {
const res = await this.client.requests.request('/skyblock/bazaar');
async execute(options?: RequestOptions): Promise<Product[]> {
const res = await this.client.requests.request('/skyblock/bazaar', options);
if (res.raw) return res;
const productsKeys = Object.keys(res.products);
return productsKeys.map((x) => new Product(res.products[x]));
Expand Down
Loading

0 comments on commit 997e754

Please sign in to comment.