Add OpenAPI contract and generated frontend client

This commit is contained in:
2026-02-18 21:25:07 +01:00
parent e55a1b01f4
commit 1802fd6607
19 changed files with 1509 additions and 126 deletions

View File

@@ -1,35 +1,13 @@
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();
}
import { apiClient, resolveOperationPath } from "./api-client.generated.js";
async function requestState(ifNoneMatch) {
const headers = { ...defaultHeaders };
const headers = {};
if (ifNoneMatch) headers["If-None-Match"] = ifNoneMatch;
const res = await fetch(withBase("/api/state"), {
method: "GET",
credentials: "same-origin",
const res = await apiClient.getState({
headers,
raw: true,
acceptStatuses: [304],
});
if (res.status === 304) {
@@ -40,8 +18,6 @@ async function requestState(ifNoneMatch) {
};
}
if (!res.ok) throw await toApiError(res);
return {
notModified: false,
etag: res.headers.get("ETag"),
@@ -49,92 +25,67 @@ async function requestState(ifNoneMatch) {
};
}
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" }),
stateEventsUrl: () => resolveOperationPath("GetStateEvents"),
me: () => apiClient.getMe(),
authOptions: () => apiClient.getAuthOptions(),
register: (payload) => apiClient.register({ body: payload }),
login: (payload) => apiClient.login({ body: payload }),
logout: () => apiClient.logout(),
mySuggestions: () => request("/api/suggestions/mine"),
mySuggestions: () => apiClient.getMySuggestions(),
createSuggestion: (payload) =>
request("/api/suggestions", { method: "POST", body: payload }),
apiClient.createSuggestion({ body: payload }),
deleteSuggestion: (id) =>
request(`/api/suggestions/${id}`, { method: "DELETE" }),
apiClient.deleteSuggestion({ pathParameters: { id } }),
updateSuggestion: (id, payload) =>
request(`/api/suggestions/${id}`, { method: "PUT", body: payload }),
allSuggestions: () => request("/api/suggestions/all"),
apiClient.updateSuggestion({ pathParameters: { id }, body: payload }),
allSuggestions: () => apiClient.getAllSuggestions(),
myVotes: () => request("/api/votes/mine"),
myVotes: () => apiClient.getMyVotes(),
vote: (suggestionId, score) =>
request("/api/votes", {
method: "POST",
apiClient.upsertVote({
body: { suggestionId, score },
}),
finalizeVotes: (final) =>
request("/api/votes/finalize", { method: "POST", body: { final } }),
finalizeVotes: (final) => apiClient.setVotesFinalized({ body: { final } }),
results: () => request("/api/results"),
nextPhase: () => request("/api/me/phase/next", { method: "POST" }),
prevPhase: () => request("/api/me/phase/prev", { method: "POST" }),
results: () => apiClient.getResults(),
nextPhase: () => apiClient.nextPhase(),
prevPhase: () => apiClient.prevPhase(),
};
export const adminApi = {
setResultsOpen: (resultsOpen) =>
request("/api/admin/results", {
method: "POST",
apiClient.setResultsOpen({
body: { resultsOpen },
}),
voteStatus: () => request("/api/admin/vote-status"),
reset: (password) =>
request("/api/admin/reset", { method: "POST", body: { password } }),
voteStatus: () => apiClient.getVoteStatus(),
reset: (password) => apiClient.reset({ body: { password } }),
factoryReset: (password) =>
request("/api/admin/factory-reset", {
method: "POST",
apiClient.factoryReset({
body: { password },
}),
grantJoker: (playerId) =>
request("/api/admin/joker", { method: "POST", body: { playerId } }),
grantJoker: (playerId) => apiClient.grantJoker({ body: { playerId } }),
setPlayerAdmin: (playerId, isAdmin) =>
request("/api/admin/player-admin", {
method: "POST",
apiClient.setPlayerAdmin({
body: { playerId, isAdmin },
}),
setPlayerPhase: (playerId, phase) =>
request("/api/admin/player-phase", {
method: "POST",
apiClient.setPlayerPhase({
body: { playerId, phase },
}),
deletePlayer: (playerId, password) =>
request(`/api/admin/players/${playerId}`, {
method: "DELETE",
apiClient.deletePlayer({
pathParameters: { playerId },
body: { password },
}),
linkSuggestions: (sourceSuggestionId, targetSuggestionId) =>
request("/api/admin/link-suggestions", {
method: "POST",
apiClient.linkSuggestions({
body: { sourceSuggestionId, targetSuggestionId },
}),
unlinkSuggestions: (suggestionId) =>
request("/api/admin/unlink-suggestions", {
method: "POST",
apiClient.unlinkSuggestions({
body: { suggestionId },
}),
};