mhsf-dev/src/lib/api.ts

431 lines
8.6 KiB
TypeScript
Raw Normal View History

2024-08-03 09:51:45 -05:00
/**
* New API file for easier API access
* Could be used for a JavaScript library :eyes:
* @author DeveloLongScript
*/
//
const connector = (
2024-09-03 23:56:15 -05:00
endpoint: string,
options: { version: number; starting?: string },
2024-08-03 09:51:45 -05:00
) =>
2024-09-03 23:56:15 -05:00
`${options.starting == undefined ? "/" : `${options.starting}/`}api/v${options.version}${endpoint}`;
2024-08-03 09:51:45 -05:00
export async function getMOTDFromServer(
2024-09-03 23:56:15 -05:00
list: Array<{ server: string; motd: string }>,
2024-08-03 09:51:45 -05:00
): Promise<Array<{ server: string; motd: string }>> {
2024-09-03 23:56:15 -05:00
const result = await fetch(connector("/motd", { version: 1 }), {
body: JSON.stringify({ motd: list }),
method: "POST",
headers: {
"Content-Type": "application/json",
},
});
let json = await result.json();
return json.result;
2024-08-03 09:51:45 -05:00
}
export async function getCommunityServerFavorites(
2024-09-03 23:56:15 -05:00
server: string,
2024-08-03 09:51:45 -05:00
): Promise<number> {
2024-09-03 23:56:15 -05:00
const result = await fetch(
connector(`/favorites/${server}/community-favorites`, { version: 0 }),
{
method: "POST",
headers: {
"Content-Type": "application/json",
},
},
);
let json = await result.json();
return json.result;
2024-08-03 09:51:45 -05:00
}
/** requires authentication */
export async function favoriteServer(server: string) {
2024-09-03 23:56:15 -05:00
try {
await fetch(
connector(`/favorites/${server}/favorite-server`, { version: 0 }),
{
method: "POST",
headers: {
"Content-Type": "application/json",
},
},
);
} catch {
throw Error("Not authenticated with a user.");
}
2024-08-03 09:51:45 -05:00
}
/** requires authentication */
export async function isFavorited(server: string): Promise<boolean> {
2024-09-03 23:56:15 -05:00
try {
const response = await fetch(
connector(`/favorites/${server}/favorited`, { version: 0 }),
{
method: "POST",
headers: {
"Content-Type": "application/json",
},
},
);
return (await response.json()).result;
} catch {
throw Error("Not authenticated with a user.");
}
2024-08-03 09:51:45 -05:00
}
/** requires authentication */
export async function getAccountFavorites(): Promise<Array<string>> {
2024-09-03 23:56:15 -05:00
try {
const response = await fetch(
connector(`/favorites/account-favorites`, { version: 0 }),
{
method: "POST",
headers: {
"Content-Type": "application/json",
},
},
);
return (await response.json()).result;
} catch {
throw Error("Not authenticated with a user.");
}
2024-08-03 09:51:45 -05:00
}
/**
* currently not used in frontend yet
*/
export async function getHistoricalData(
2024-09-03 23:56:15 -05:00
server: string,
scopes: Array<"player_count" | "favorites" | "server" | "time">,
2024-08-03 09:51:45 -05:00
): Promise<
2024-09-03 23:56:15 -05:00
Array<{
player_count?: number;
favorites?: number;
server?: string;
time?: number;
}>
2024-08-03 09:51:45 -05:00
> {
2024-09-03 23:56:15 -05:00
const response = await fetch(
connector(`/history/${server}/get-historical-data`, { version: 0 }),
{
method: "POST",
body: JSON.stringify({ scopes }),
headers: {
"Content-Type": "application/json",
},
},
);
return (await response.json()).data;
2024-08-03 09:51:45 -05:00
}
export async function getShortTermData(
2024-09-03 23:56:15 -05:00
server: string,
scopes: Array<"player_count" | "favorites" | "server" | "date">,
2024-08-03 09:51:45 -05:00
): Promise<
2024-09-03 23:56:15 -05:00
Array<{
player_count?: number;
favorites?: number;
server?: string;
time?: number;
}>
2024-08-03 09:51:45 -05:00
> {
2024-09-03 23:56:15 -05:00
const response = await fetch(
connector(`/history/${server}/get-short-term-data`, { version: 0 }),
{
method: "POST",
body: JSON.stringify({ scopes }),
headers: {
"Content-Type": "application/json",
},
},
);
return (await response.json()).data;
2024-08-03 09:51:45 -05:00
}
export async function getMetaShortTerm(
2024-09-03 23:56:15 -05:00
scopes: Array<"total_players" | "total_servers" | "date">,
2024-08-03 09:51:45 -05:00
): Promise<
2024-09-03 23:56:15 -05:00
Array<{
total_players?: number;
total_servers?: number;
unix?: number;
}>
2024-08-03 09:51:45 -05:00
> {
2024-09-03 23:56:15 -05:00
const response = await fetch(
connector(`/history/meta-short-term-data`, { version: 0 }),
{
method: "POST",
body: JSON.stringify({ scopes }),
headers: {
"Content-Type": "application/json",
},
},
);
return (await response.json()).data;
2024-08-03 09:51:45 -05:00
}
2024-08-07 16:37:54 -05:00
/** requires authentication */
export async function linkMCAccount(code: string): Promise<string | undefined> {
2024-09-03 23:56:15 -05:00
try {
const response = await fetch(
connector(`/account-linking/claim-account-code`, { version: 0 }),
{
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ code }),
},
);
if (response.status == 400) {
return undefined;
}
return (await response.json()).player;
} catch {
throw Error("Incorrect code");
}
2024-08-07 16:37:54 -05:00
}
/** requires authentication */
export async function unlinkMCAccount(): Promise<boolean> {
2024-09-03 23:56:15 -05:00
try {
const response = await fetch(
connector(`/account-linking/unlink-account`, { version: 0 }),
{
method: "POST",
headers: {
"Content-Type": "application/json",
},
},
);
return true;
} catch {
throw Error(
"Not authenticated with a user or user already linked account.",
);
}
2024-08-07 16:37:54 -05:00
}
export async function serverOwned(server: string): Promise<boolean> {
2024-09-03 23:56:15 -05:00
try {
const response = await fetch(
connector(`/account-linking/is-owned`, { version: 0 }),
{
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ server }),
},
);
return (await response.json()).owned;
} catch {
throw Error("Error while running API");
}
2024-08-07 16:37:54 -05:00
}
/** requires authentication */
export async function userOwnedServer(server: string): Promise<boolean> {
2024-09-03 23:56:15 -05:00
try {
const response = await fetch(
connector(`/account-linking/owned-user`, { version: 0 }),
{
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ server }),
},
);
return (await response.json()).result;
} catch {
throw Error("Error while running API");
}
2024-08-07 16:37:54 -05:00
}
/** requires authentication */
export async function ownServer(server: string): Promise<boolean> {
2024-09-03 23:56:15 -05:00
try {
const response = await fetch(
connector(`/account-linking/own-server`, { version: 0 }),
{
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ server }),
},
);
if (response.status >= 400) {
return false;
}
return true;
} catch {
throw Error("Error while running API");
}
2024-08-07 16:37:54 -05:00
}
/** requires authentication */
export async function unownServer(server: string): Promise<boolean> {
2024-09-03 23:56:15 -05:00
try {
const response = await fetch(
connector(`/account-linking/unown-server`, { version: 0 }),
{
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ server }),
},
);
if (response.status == 400) {
return false;
}
return true;
} catch {
throw Error("Error while running API");
}
2024-08-07 16:37:54 -05:00
}
/** requires authentication */
export async function setCustomization(
2024-09-03 23:56:15 -05:00
server: string,
customization: any,
2024-08-07 16:37:54 -05:00
): Promise<boolean | Error> {
2024-09-03 23:56:15 -05:00
try {
const response = await fetch(
connector(`/customization/${server}/set`, { version: 0 }),
{
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ customization }),
},
);
if (response.status == 400) {
throw Error("Error while running API");
return false;
}
return true;
} catch {
throw Error("Error while running API");
}
2024-08-07 16:37:54 -05:00
}
export async function getCustomization(server: string): Promise<any> {
2024-09-03 23:56:15 -05:00
try {
const response = await fetch(
connector(`/customization/${server}/get`, { version: 0 }),
{
method: "POST",
headers: {
"Content-Type": "application/json",
},
},
);
if (response.status == 400) {
return false;
}
return (await response.json()).results;
} catch {
throw Error("Error while running API");
}
2024-08-07 16:37:54 -05:00
}
2024-08-13 20:57:37 -05:00
export async function sortedFavorites(): Promise<
2024-09-03 23:56:15 -05:00
Array<{ server: string; favorites: number }> | boolean
2024-08-13 20:57:37 -05:00
> {
2024-09-03 23:56:15 -05:00
try {
const response = await fetch(
connector(`/sorting/favorites`, { version: 0 }),
{
method: "POST",
headers: {
"Content-Type": "application/json",
},
},
);
if (response.status == 400) {
return false;
}
return (await response.json()).results;
} catch {
throw Error("Error while running API");
}
2024-08-13 20:57:37 -05:00
}
2024-08-20 21:32:27 -05:00
export async function reportServer(
2024-09-03 23:56:15 -05:00
server: string,
reason: string,
2024-08-20 21:32:27 -05:00
): Promise<boolean> {
2024-09-03 23:56:15 -05:00
try {
const response = await fetch(connector(`/report-server`, { version: 1 }), {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ server, reason }),
});
if (response.status == 400) {
return false;
}
return true;
} catch {
throw Error("Error while running API");
}
}
export async function setAccountSL(
data: number | boolean | null,
type: "srv" | "ipr" | "pad",
): Promise<boolean> {
try {
const response = await fetch(
connector(`/account-sl/change`, { version: 0 }),
{
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ data, type }),
},
);
if (response.status === 400) {
return false;
}
return true;
} catch {
throw Error("Error while running API");
}
2024-08-20 21:32:27 -05:00
}