2024-04-14 14:35:55 -04:00
|
|
|
import { ErrorResponse } from "../types/generic";
|
2024-04-14 13:34:34 -04:00
|
|
|
import type { Player } from "../types/player";
|
|
|
|
|
2024-04-14 14:03:02 -04:00
|
|
|
const ENDPOINT = "https://mc.rainnny.club"; // The API endpoint to use
|
|
|
|
|
2024-04-14 13:34:34 -04:00
|
|
|
/**
|
|
|
|
* Get a player by their username or UUID.
|
|
|
|
*
|
|
|
|
* @param query the query to search for the player by
|
|
|
|
* @returns the promised player
|
|
|
|
*/
|
|
|
|
export const getPlayer = (query: string): Promise<Player> => {
|
2024-04-14 14:03:02 -04:00
|
|
|
return new Promise(async (resolve, reject) => {
|
|
|
|
const response: Response = await fetch(`${ENDPOINT}/player/${query}`); // Request the player
|
2024-04-14 14:35:55 -04:00
|
|
|
const json: any = await response.json();
|
|
|
|
|
|
|
|
// Resolve the player
|
|
|
|
if (response.ok) {
|
|
|
|
resolve(json as Player);
|
|
|
|
} else {
|
|
|
|
reject(json as ErrorResponse); // The request failed
|
|
|
|
}
|
2024-04-14 13:34:34 -04:00
|
|
|
});
|
|
|
|
};
|