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

Commit

Permalink
fix option selection and types
Browse files Browse the repository at this point in the history
  • Loading branch information
UCDFiddes committed Jul 17, 2024
1 parent 623580d commit bf18f0b
Show file tree
Hide file tree
Showing 9 changed files with 65 additions and 40 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "hive-bedrock-api",
"version": "2.1.4",
"version": "2.1.5",
"description": "An API wrapper for the Hive Minecraft Bedrock Edition server.",
"main": "lib/index.js",
"types": "lib/index.d.ts",
Expand Down
7 changes: 5 additions & 2 deletions src/methods/getAllTimeLeaderboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,13 @@ import { LeaderboardProcessors } from "../processors";

export default function getAllTimeLeaderboard<G extends Game>(
game_id: G,
options?: Options
options?: Partial<Options>
): Promise<APIResponse<AllTimeProcessedLeaderboard<G>>>;

export default async function getAllTimeLeaderboard<G extends Game>(game_id: G, options?: Options) {
export default async function getAllTimeLeaderboard<G extends Game>(
game_id: G,
options?: Partial<Options>
) {
if (!isGame(game_id))
return {
status: 404,
Expand Down
11 changes: 6 additions & 5 deletions src/methods/getAllTimeStatistics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,23 @@ type AllGameStatisticsPlayer = AllTimeProcessedStatistics & {

export default async function getAllTimeStatistics(
identifier: string,
options?: Options
options?: Partial<Options>
): Promise<APIResponse<AllGameStatisticsPlayer>>;

export default async function getAllTimeStatistics<G extends Game>(
identifier: string,
game_id: G,
options?: Options
options?: Partial<Options>
): Promise<APIResponse<AllTimeProcessedStatistics[G]>>;

export default async function getAllTimeStatistics<G extends Game>(
identifier: string,
game_or_options?: G | Options,
options?: Options
game_or_options?: G | Partial<Options>,
options?: Partial<Options>
) {
let game_id: G | "all" = "all";
let method_options: Options | undefined = game_or_options as Options;
let method_options: Partial<Options> | undefined =
game_or_options as Options;

if (typeof game_or_options === "string") {
game_id = game_or_options as G;
Expand Down
4 changes: 2 additions & 2 deletions src/methods/getGlobalStatistics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ import { APIResponse, Options } from "../types/types";
import fetchEndpoint from "../helpers/fetchEndpoint";

export default function getGlobalStatistics(
options?: Options
options?: Partial<Options>
): Promise<APIResponse<GlobalStatisticsMetadata>>;

export default async function getGlobalStatistics(
options?: Options
options?: Partial<Options>
): Promise<APIResponse<GlobalStatisticsMetadata>> {
let response = await fetchEndpoint(`/global/statistics`, options?.init);
return response;
Expand Down
4 changes: 2 additions & 2 deletions src/methods/getMaps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ import isGame from "../helpers/isGame";

export default function getMaps<G extends Game>(
game_id: G,
options?: Options
options?: Partial<Options>
): Promise<APIResponse<MapMetadata[]>>;

export default async function getMaps<G extends Game>(
game_id: G,
options?: Options
options?: Partial<Options>
): Promise<APIResponse<MapMetadata[]>> {
if (!isGame(game_id))
return {
Expand Down
4 changes: 2 additions & 2 deletions src/methods/getMetadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ import isGame from "../helpers/isGame";

export default function getMetdata<G extends Game>(
game_id: G,
options?: Options
options?: Partial<Options>
): Promise<APIResponse<GameMetadata>>;

export default async function getMetdata<G extends Game>(
game_id: G,
options?: Options
options?: Partial<Options>
): Promise<APIResponse<GameMetadata>> {
if (!isGame(game_id))
return {
Expand Down
53 changes: 33 additions & 20 deletions src/methods/getMonthlyStatistics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,23 @@ interface MonthlyOptions extends Options {

export default async function getMonthlyStatistics(
identifier: string,
options?: MonthlyOptions
options?: Partial<MonthlyOptions>
): Promise<APIResponse<MonthlyProcessedStatistics>>;

export default async function getMonthlyStatistics<G extends Game>(
identifier: string,
game_id: G,
options?: MonthlyOptions
options?: Partial<MonthlyOptions>
): Promise<APIResponse<MonthlyProcessedStatistics[G]>>;

export default async function getMonthlyStatistics<G extends Game>(
identifier: string,
game_or_options?: G | MonthlyOptions,
options?: MonthlyOptions
game_or_options?: G | Partial<MonthlyOptions>,
options?: Partial<MonthlyOptions>
) {
let game_id: G | "all" = "all";
let method_options: MonthlyOptions | undefined = game_or_options as MonthlyOptions;
let method_options: Partial<MonthlyOptions> | undefined =
game_or_options as MonthlyOptions;

if (typeof game_or_options === "string") {
game_id = game_or_options as G;
Expand All @@ -46,29 +47,41 @@ export default async function getMonthlyStatistics<G extends Game>(

let current_date = new Date();
let endpoint = `/game/monthly/player/${game_id}/${identifier}/${
options?.year ?? current_date.getFullYear()
}/${options?.month ?? current_date.getMonth() + 1}` as const;
typeof method_options?.year === "number"
? method_options?.year
: current_date.getFullYear()
}/${
typeof method_options?.month === "number"
? method_options?.month
: current_date.getMonth() + 1
}` as const;
console.log(endpoint);

let response = await fetchEndpoint(endpoint, method_options?.init);
if (response.error) return response;

if (game_id === "all") {
let response_data = Object.entries(response.data);
let processed_data: Partial<MonthlyProcessedStatistics> = Object.fromEntries(
response_data.map(([id, statistics]) => {
if (!statistics || !isGame(id as Game) || Array.isArray(statistics))
return [id, null];
let processed_data: Partial<MonthlyProcessedStatistics> =
Object.fromEntries(
response_data.map(([id, statistics]) => {
if (
!statistics ||
!isGame(id as Game) ||
Array.isArray(statistics)
)
return [id, null];

let e = MonthlyProcessors[Game.ParkourWorlds];
let e = MonthlyProcessors[Game.ParkourWorlds];

return [
id,
typeof MonthlyProcessors[id as Game] === "function"
? MonthlyProcessors[id as Game](statistics)
: null,
];
})
);
return [
id,
typeof MonthlyProcessors[id as Game] === "function"
? MonthlyProcessors[id as Game](statistics)
: null,
];
})
);

return { ...response, data: processed_data };
}
Expand Down
12 changes: 9 additions & 3 deletions src/methods/getPlayerInfomation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,17 @@ import { Processed_Player_Statistics } from "../processors/player";

export default function getPlayerInfomation(
identifier: string,
options?: Options
options?: Partial<Options>
): Promise<APIResponse<Processed_Player_Statistics>>;

export default async function getPlayerInfomation(identifier: string, options?: Options) {
let response = await fetchEndpoint(`/game/all/main/${identifier}`, options?.init);
export default async function getPlayerInfomation(
identifier: string,
options?: Partial<Options>
) {
let response = await fetchEndpoint(
`/game/all/main/${identifier}`,
options?.init
);
if (response.error) return response;

let response_data = response.data.main;
Expand Down
8 changes: 5 additions & 3 deletions src/methods/getSeasonStatistics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ interface SeasonOptions extends Options {
export default async function getSeasonStatistics<G extends Game>(
identifier: string,
game_id: G,
options?: SeasonOptions
options?: Partial<SeasonOptions>
): Promise<APIResponse<MonthlyProcessedStatistics[G]>>;

export default async function getSeasonStatistics<G extends Game>(
identifier: string,
game_id: G,
options?: SeasonOptions
options?: Partial<SeasonOptions>
) {
if (!isGame(game_id as G))
return {
Expand All @@ -32,7 +32,9 @@ export default async function getSeasonStatistics<G extends Game>(
data: null,
};

let endpoint = `/game/season/player/${game_id}/${identifier}/${options?.season ?? 1}`;
let endpoint = `/game/season/player/${game_id}/${identifier}/${
options?.season ?? 1
}`;

let response = await fetchEndpoint(endpoint, options?.init);
if (response.error) return response;
Expand Down

0 comments on commit bf18f0b

Please sign in to comment.