Add #getMojangServerStatus

This commit is contained in:
Braydon 2024-04-14 16:06:42 -04:00
parent 18293cc579
commit fe00e7d50b
3 changed files with 35 additions and 2 deletions

@ -67,7 +67,7 @@ export const isMojangBlocked = (hostname: string): Promise<boolean> => {
return new Promise(async (resolve, reject) => {
const response: Response = await fetch(
`${ENDPOINT}/server/blocked/${hostname}`
); // Request the server
); // Request the server's blocked status
const json: any = await response.json();
// Resolve the blocked status
@ -78,3 +78,22 @@ export const isMojangBlocked = (hostname: string): Promise<boolean> => {
}
});
};
/**
* Get the status of Mojang servers.
*
* @returns the promised status
*/
export const getMojangServerStatus = (): Promise<MojangServerStatus> => {
return new Promise(async (resolve, reject) => {
const response: Response = await fetch(`${ENDPOINT}/mojang/status`); // Request the statuses
const json: any = await response.json();
// Resolve the blocked status
if (response.ok) {
resolve(json as MojangServerStatus);
} else {
reject(json as ErrorResponse); // The request failed
}
});
};

7
Lib/src/types/mojang.d.ts vendored Normal file

@ -0,0 +1,7 @@
/**
* Represents the status of
* a service provided by Mojang.
*/
type MojangServerStatus = {
[key: string]: string;
};

@ -1,5 +1,5 @@
import { describe, it } from "bun:test";
import { getMinecraftServer, getPlayer } from "../src";
import { getMinecraftServer, getMojangServerStatus, getPlayer } from "../src";
import { CachedPlayer } from "../src/types/player";
import { BedrockMinecraftServer } from "../src/types/server/bedrock-server";
import { JavaMinecraftServer } from "../src/types/server/java-server";
@ -21,3 +21,10 @@ describe("server", () => {
console.log(server.ip);
});
});
describe("mojang", () => {
it("status", async () => {
const status: MojangServerStatus = await getMojangServerStatus();
console.log(status["https://sessionserver.mojang.com"]);
});
});