Harden gameplay timing and map loading

This commit is contained in:
2026-10-05 15:15:35 +05:00
parent 8b31891776
commit dcc3779262
36 changed files with 1050 additions and 205 deletions

41
lib/client-api.ts Normal file
View File

@@ -0,0 +1,41 @@
export async function api<T = Record<string, unknown>>(
path: string,
body?: unknown,
method = body === undefined ? "GET" : "POST",
): Promise<T> {
let response: Response;
try {
response = await fetch("/api/v1/" + path, {
signal: AbortSignal.timeout(30000),
cache: "no-store",
method,
headers:
body === undefined ? undefined : { "Content-Type": "application/json" },
body: body === undefined ? undefined : JSON.stringify(body),
});
} catch (error) {
if (
error instanceof Error &&
["TimeoutError", "AbortError"].includes(error.name)
)
throw new Error("The request timed out. Reconnect and try again.");
throw new Error(
"Could not reach MineGeo. Check your connection and try again.",
);
}
let data: T & { error?: string };
try {
data = await response.json();
} catch {
throw new Error(
"MineGeo returned an unreadable response. Please try again.",
);
}
if (!response.ok)
throw new Error(
typeof data?.error === "string"
? data.error
: "Request failed. Please try again.",
);
return data;
}