141 lines
4.5 KiB
JavaScript
141 lines
4.5 KiB
JavaScript
const defaultHeaders = { "Content-Type": "application/json" };
|
|
|
|
const rawBase = document.querySelector('meta[name="app-base"]')?.content || "";
|
|
const basePath = normalizeBase(rawBase);
|
|
const withBase = (path) => `${basePath}${path}`;
|
|
|
|
function normalizeBase(value) {
|
|
if (!value) return "";
|
|
if (!value.startsWith("/")) return `/${value}`;
|
|
return value.endsWith("/") ? value.slice(0, -1) : value;
|
|
}
|
|
|
|
async function request(path, { method = "GET", body } = {}) {
|
|
const res = await fetch(withBase(path), {
|
|
method,
|
|
credentials: "same-origin",
|
|
headers: defaultHeaders,
|
|
body: body ? JSON.stringify(body) : undefined,
|
|
});
|
|
|
|
if (!res.ok) throw await toApiError(res);
|
|
return res.status === 204 ? null : res.json();
|
|
}
|
|
|
|
async function requestState(ifNoneMatch) {
|
|
const headers = { ...defaultHeaders };
|
|
if (ifNoneMatch) headers["If-None-Match"] = ifNoneMatch;
|
|
|
|
const res = await fetch(withBase("/api/state"), {
|
|
method: "GET",
|
|
credentials: "same-origin",
|
|
headers,
|
|
});
|
|
|
|
if (res.status === 304) {
|
|
return {
|
|
notModified: true,
|
|
etag: res.headers.get("ETag"),
|
|
data: null,
|
|
};
|
|
}
|
|
|
|
if (!res.ok) throw await toApiError(res);
|
|
|
|
return {
|
|
notModified: false,
|
|
etag: res.headers.get("ETag"),
|
|
data: await res.json(),
|
|
};
|
|
}
|
|
|
|
async function toApiError(res) {
|
|
let msg = `${res.status}`;
|
|
try {
|
|
const data = await res.json();
|
|
msg = data.error || data.detail || data.title || JSON.stringify(data);
|
|
} catch {
|
|
/* ignore */
|
|
}
|
|
const err = new Error(msg);
|
|
err.status = res.status;
|
|
return err;
|
|
}
|
|
|
|
export const api = {
|
|
state: (ifNoneMatch) => requestState(ifNoneMatch),
|
|
stateEventsUrl: () => withBase("/api/events/state"),
|
|
me: () => request("/api/me"),
|
|
authOptions: () => request("/api/auth/options"),
|
|
register: (payload) =>
|
|
request("/api/auth/register", { method: "POST", body: payload }),
|
|
login: (payload) =>
|
|
request("/api/auth/login", { method: "POST", body: payload }),
|
|
logout: () => request("/api/auth/logout", { method: "POST" }),
|
|
|
|
mySuggestions: () => request("/api/suggestions/mine"),
|
|
createSuggestion: (payload) =>
|
|
request("/api/suggestions", { method: "POST", body: payload }),
|
|
deleteSuggestion: (id) =>
|
|
request(`/api/suggestions/${id}`, { method: "DELETE" }),
|
|
updateSuggestion: (id, payload) =>
|
|
request(`/api/suggestions/${id}`, { method: "PUT", body: payload }),
|
|
allSuggestions: () => request("/api/suggestions/all"),
|
|
|
|
myVotes: () => request("/api/votes/mine"),
|
|
vote: (suggestionId, score) =>
|
|
request("/api/votes", {
|
|
method: "POST",
|
|
body: { suggestionId, score },
|
|
}),
|
|
finalizeVotes: (final) =>
|
|
request("/api/votes/finalize", { method: "POST", body: { final } }),
|
|
|
|
results: () => request("/api/results"),
|
|
nextPhase: () => request("/api/me/phase/next", { method: "POST" }),
|
|
prevPhase: () => request("/api/me/phase/prev", { method: "POST" }),
|
|
};
|
|
|
|
export const adminApi = {
|
|
setResultsOpen: (resultsOpen) =>
|
|
request("/api/admin/results", {
|
|
method: "POST",
|
|
body: { resultsOpen },
|
|
}),
|
|
voteStatus: () => request("/api/admin/vote-status"),
|
|
reset: (password) =>
|
|
request("/api/admin/reset", { method: "POST", body: { password } }),
|
|
factoryReset: (password) =>
|
|
request("/api/admin/factory-reset", {
|
|
method: "POST",
|
|
body: { password },
|
|
}),
|
|
grantJoker: (playerId) =>
|
|
request("/api/admin/joker", { method: "POST", body: { playerId } }),
|
|
setPlayerAdmin: (playerId, isAdmin) =>
|
|
request("/api/admin/player-admin", {
|
|
method: "POST",
|
|
body: { playerId, isAdmin },
|
|
}),
|
|
setPlayerPhase: (playerId, phase) =>
|
|
request("/api/admin/player-phase", {
|
|
method: "POST",
|
|
body: { playerId, phase },
|
|
}),
|
|
deletePlayer: (playerId, password) =>
|
|
request(`/api/admin/players/${playerId}`, {
|
|
method: "DELETE",
|
|
body: { password },
|
|
}),
|
|
linkSuggestions: (sourceSuggestionId, targetSuggestionId) =>
|
|
request("/api/admin/link-suggestions", {
|
|
method: "POST",
|
|
body: { sourceSuggestionId, targetSuggestionId },
|
|
}),
|
|
unlinkSuggestions: (suggestionId) =>
|
|
request("/api/admin/unlink-suggestions", {
|
|
method: "POST",
|
|
body: { suggestionId },
|
|
}),
|
|
};
|