refacto: Keeping entrypoints clean and making files by purpose
This commit is contained in:
51
public/src/api.js
Normal file
51
public/src/api.js
Normal file
@@ -0,0 +1,51 @@
|
||||
// ── Raw HTTP wrappers ─────────────────────────────────────────────────────────
|
||||
// These functions only perform fetch() calls and return the raw Response or
|
||||
// parsed JSON. No state mutations, no DOM access.
|
||||
|
||||
export async function apiFetchConfig(team) {
|
||||
const res = await fetch(`/api/config?team=${encodeURIComponent(team)}`);
|
||||
if (!res.ok) throw new Error("config_fetch_failed");
|
||||
return res.json();
|
||||
}
|
||||
|
||||
export async function apiFetchScores() {
|
||||
const res = await fetch("/api/scores");
|
||||
if (!res.ok) throw new Error("scores_fetch_failed");
|
||||
return res.json();
|
||||
}
|
||||
|
||||
/** Returns the raw Response so the caller can inspect status codes (410, etc.). */
|
||||
export async function apiFetchGrid(seed) {
|
||||
return fetch(`/api/grid/${encodeURIComponent(seed)}`);
|
||||
}
|
||||
|
||||
/** Returns the raw Response so the caller can inspect status codes (409, 410, etc.). */
|
||||
export async function apiRevealCell(seed, x, y, team) {
|
||||
return fetch("/api/cell/reveal", {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ seed, x, y, team }),
|
||||
});
|
||||
}
|
||||
|
||||
export async function apiLogin(username, password) {
|
||||
return fetch("/api/auth/login", {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ username, password }),
|
||||
});
|
||||
}
|
||||
|
||||
export async function apiRegister(username, email, password, team) {
|
||||
return fetch("/api/auth/register", {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ username, email, password, team }),
|
||||
});
|
||||
}
|
||||
|
||||
export async function apiGetMe(token) {
|
||||
return fetch("/api/auth/me", {
|
||||
headers: { Authorization: `Bearer ${token}` },
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user