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

@@ -0,0 +1,303 @@
// AUTO-GENERATED FILE. DO NOT EDIT.
// Source: scripts/generate-api-client.mjs and openapi/GameList.json
const defaultHeaders = { "Content-Type": "application/json" };
const rawBase = document.querySelector('meta[name="app-base"]')?.content || "";
const basePath = normalizeBase(rawBase);
const withBase = (routePath) => `${basePath}${routePath}`;
function normalizeBase(value) {
if (!value) return "";
if (!value.startsWith("/")) return `/${value}`;
return value.endsWith("/") ? value.slice(0, -1) : value;
}
function toApiError(res, fallbackMessage = `${res.status}`) {
const err = new Error(fallbackMessage);
err.status = res.status;
return err;
}
function buildPath(template, pathParameters = {}) {
return template.replace(/{([^}]+)}/g, (_, key) => {
const value = pathParameters[key];
if (value === undefined || value === null) {
throw new Error(
`Missing path parameter "${key}" for route ${template}`,
);
}
return encodeURIComponent(String(value));
});
}
async function parseApiError(res) {
try {
const data = await res.json();
const message =
data.error || data.detail || data.title || JSON.stringify(data);
return toApiError(res, message);
} catch {
return toApiError(res);
}
}
export const operations = Object.freeze({
CreateSuggestion: {
method: "POST",
path: "/api/suggestions",
hasBody: true,
pathParameters: [],
},
DeletePlayer: {
method: "DELETE",
path: "/api/admin/players/{playerId}",
hasBody: true,
pathParameters: ["playerId"],
},
DeleteSuggestion: {
method: "DELETE",
path: "/api/suggestions/{id}",
hasBody: false,
pathParameters: ["id"],
},
FactoryReset: {
method: "POST",
path: "/api/admin/factory-reset",
hasBody: true,
pathParameters: [],
},
GetAllSuggestions: {
method: "GET",
path: "/api/suggestions/all",
hasBody: false,
pathParameters: [],
},
GetAuthOptions: {
method: "GET",
path: "/api/auth/options",
hasBody: false,
pathParameters: [],
},
GetMe: {
method: "GET",
path: "/api/me",
hasBody: false,
pathParameters: [],
},
GetMySuggestions: {
method: "GET",
path: "/api/suggestions/mine",
hasBody: false,
pathParameters: [],
},
GetMyVotes: {
method: "GET",
path: "/api/votes/mine",
hasBody: false,
pathParameters: [],
},
GetResults: {
method: "GET",
path: "/api/results",
hasBody: false,
pathParameters: [],
},
GetState: {
method: "GET",
path: "/api/state",
hasBody: false,
pathParameters: [],
},
GetStateEvents: {
method: "GET",
path: "/api/events/state",
hasBody: false,
pathParameters: [],
},
GetVoteStatus: {
method: "GET",
path: "/api/admin/vote-status",
hasBody: false,
pathParameters: [],
},
GrantJoker: {
method: "POST",
path: "/api/admin/joker",
hasBody: true,
pathParameters: [],
},
LinkSuggestions: {
method: "POST",
path: "/api/admin/link-suggestions",
hasBody: true,
pathParameters: [],
},
Login: {
method: "POST",
path: "/api/auth/login",
hasBody: true,
pathParameters: [],
},
Logout: {
method: "POST",
path: "/api/auth/logout",
hasBody: false,
pathParameters: [],
},
NextPhase: {
method: "POST",
path: "/api/me/phase/next",
hasBody: false,
pathParameters: [],
},
PrevPhase: {
method: "POST",
path: "/api/me/phase/prev",
hasBody: false,
pathParameters: [],
},
Register: {
method: "POST",
path: "/api/auth/register",
hasBody: true,
pathParameters: [],
},
Reset: {
method: "POST",
path: "/api/admin/reset",
hasBody: true,
pathParameters: [],
},
SetPlayerAdmin: {
method: "POST",
path: "/api/admin/player-admin",
hasBody: true,
pathParameters: [],
},
SetPlayerPhase: {
method: "POST",
path: "/api/admin/player-phase",
hasBody: true,
pathParameters: [],
},
SetResultsOpen: {
method: "POST",
path: "/api/admin/results",
hasBody: true,
pathParameters: [],
},
SetVotesFinalized: {
method: "POST",
path: "/api/votes/finalize",
hasBody: true,
pathParameters: [],
},
UnlinkSuggestions: {
method: "POST",
path: "/api/admin/unlink-suggestions",
hasBody: true,
pathParameters: [],
},
UpdateSuggestion: {
method: "PUT",
path: "/api/suggestions/{id}",
hasBody: true,
pathParameters: ["id"],
},
UpsertVote: {
method: "POST",
path: "/api/votes",
hasBody: true,
pathParameters: [],
},
});
export function resolveOperationPath(operationId, pathParameters = {}) {
const operation = operations[operationId];
if (!operation) {
throw new Error(`Unknown operationId "${operationId}"`);
}
return withBase(buildPath(operation.path, pathParameters));
}
export async function requestOperation(
operationId,
{
pathParameters = {},
body,
headers = {},
raw = false,
acceptStatuses = [],
} = {},
) {
const operation = operations[operationId];
if (!operation) {
throw new Error(`Unknown operationId "${operationId}"`);
}
const response = await fetch(
resolveOperationPath(operationId, pathParameters),
{
method: operation.method,
credentials: "same-origin",
headers: { ...defaultHeaders, ...headers },
body: body === undefined ? undefined : JSON.stringify(body),
},
);
const acceptedStatusSet = new Set(acceptStatuses);
if (!response.ok && !acceptedStatusSet.has(response.status)) {
throw await parseApiError(response);
}
if (raw) return response;
if (response.status === 204) return null;
return response.json();
}
export const apiClient = Object.freeze({
getAuthOptions: (options = {}) =>
requestOperation("GetAuthOptions", options),
register: (options = {}) => requestOperation("Register", options),
login: (options = {}) => requestOperation("Login", options),
logout: (options = {}) => requestOperation("Logout", options),
getState: (options = {}) => requestOperation("GetState", options),
getStateEvents: (options = {}) =>
requestOperation("GetStateEvents", options),
getMe: (options = {}) => requestOperation("GetMe", options),
nextPhase: (options = {}) => requestOperation("NextPhase", options),
prevPhase: (options = {}) => requestOperation("PrevPhase", options),
getMySuggestions: (options = {}) =>
requestOperation("GetMySuggestions", options),
createSuggestion: (options = {}) =>
requestOperation("CreateSuggestion", options),
deleteSuggestion: (options = {}) =>
requestOperation("DeleteSuggestion", options),
updateSuggestion: (options = {}) =>
requestOperation("UpdateSuggestion", options),
getAllSuggestions: (options = {}) =>
requestOperation("GetAllSuggestions", options),
getMyVotes: (options = {}) => requestOperation("GetMyVotes", options),
upsertVote: (options = {}) => requestOperation("UpsertVote", options),
setVotesFinalized: (options = {}) =>
requestOperation("SetVotesFinalized", options),
getResults: (options = {}) => requestOperation("GetResults", options),
setResultsOpen: (options = {}) =>
requestOperation("SetResultsOpen", options),
getVoteStatus: (options = {}) => requestOperation("GetVoteStatus", options),
grantJoker: (options = {}) => requestOperation("GrantJoker", options),
setPlayerPhase: (options = {}) =>
requestOperation("SetPlayerPhase", options),
setPlayerAdmin: (options = {}) =>
requestOperation("SetPlayerAdmin", options),
deletePlayer: (options = {}) => requestOperation("DeletePlayer", options),
linkSuggestions: (options = {}) =>
requestOperation("LinkSuggestions", options),
unlinkSuggestions: (options = {}) =>
requestOperation("UnlinkSuggestions", options),
reset: (options = {}) => requestOperation("Reset", options),
factoryReset: (options = {}) => requestOperation("FactoryReset", options),
});

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 },
}),
};