diff --git a/src/app/(pages)/player/[id]/page.tsx b/src/app/(pages)/player/[id]/page.tsx index 1d5074c..b50636a 100644 --- a/src/app/(pages)/player/[id]/page.tsx +++ b/src/app/(pages)/player/[id]/page.tsx @@ -18,16 +18,10 @@ type Params = { }; export async function generateMetadata({ params: { id } }: Params): Promise { - let error: string | undefined = undefined; - let player: Player | undefined = undefined; + const { error, player } = await fetchPlayer(id); - try { - player = (await getData(id))?.player; - } catch (err) { - error = (err as McUtilsAPIError).message; - } if (error && player == undefined) { - return generateEmbed({ title: "Unknown Player", description: "Invalid UUID / Username" }); + return generateEmbed({ title: "Unknown Player", description: error }); } const { username, uniqueId, skin } = player as Player; @@ -54,16 +48,28 @@ async function getData(id: string): Promise { return await getPlayer(id); } -export default async function Page({ params }: Params): Promise { +/** + * Fetches the player's data from the uuid or username + * + * @param id the player's uuid or username + * @returns the player's data or an error message + */ +async function fetchPlayer(id: string): Promise<{ error: string | undefined; player: Player | undefined }> { let error: string | undefined = undefined; let player: Player | undefined = undefined; try { - player = (await getData(params.id))?.player; + player = (await getData(id))?.player; } catch (err) { error = (err as McUtilsAPIError).message; } + return { error, player }; +} + +export default async function Page({ params: { id } }: Params): Promise { + const { error, player } = await fetchPlayer(id); + return (
diff --git a/src/app/(pages)/server/[platform]/[hostname]/page.tsx b/src/app/(pages)/server/[platform]/[hostname]/page.tsx index 56671f9..78872d0 100644 --- a/src/app/(pages)/server/[platform]/[hostname]/page.tsx +++ b/src/app/(pages)/server/[platform]/[hostname]/page.tsx @@ -20,17 +20,10 @@ type Params = { }; export async function generateMetadata({ params: { platform, hostname } }: Params): Promise { - let error: string | undefined = undefined; - let server: JavaMinecraftServer | BedrockMinecraftServer | undefined = undefined; - - try { - server = (await getServer(platform, hostname))?.server; - } catch (err) { - error = (err as McUtilsAPIError).message; - } + const { error, server } = await fetchServer(platform, hostname); if (error && server == undefined) { - return generateEmbed({ title: "Unknown Server", description: "Server not responding" }); + return generateEmbed({ title: "Unknown Server", description: error }); } const { hostname: serverHostname, players } = server as JavaMinecraftServer | BedrockMinecraftServer; @@ -69,16 +62,32 @@ async function getData( return (await getServer(platform, hostname)).server; } -export default async function Page({ params: { platform, hostname } }: Params): Promise { +/** + * Fetches the server's data from the hostname + * + * @param platform the server's platform + * @param hostname the server's hostname + * @returns the server's data or an error message + */ +async function fetchServer( + platform: ServerPlatform, + hostname: string +): Promise<{ error: string | undefined; server: JavaMinecraftServer | BedrockMinecraftServer | undefined }> { let error: string | undefined = undefined; let server: JavaMinecraftServer | BedrockMinecraftServer | undefined = undefined; try { - server = (await getServer(platform, hostname))?.server; + server = (await getData(platform, hostname)) as JavaMinecraftServer | BedrockMinecraftServer; } catch (err) { error = (err as McUtilsAPIError).message; } + return { error, server }; +} + +export default async function Page({ params: { platform, hostname } }: Params): Promise { + const { error, server } = await fetchServer(platform, hostname); + let favicon = null; // Server favicon // Java specific