Files
FWS-DiscordBot/src/web/public/core/api.js
T
nocodeandClaude Opus 5 06a9be0cba feat(web): serve the dashboard and its interface
The front-end is plain JavaScript served as is: no build step to run before
the bot starts, and no second toolchain to keep alive. A simple/advanced
switch hides the technical panels without ever standing in for a permission.

`npm run setup` starts the wizard alone, because the bot refuses to boot
without a token and the `.env` has to be writable from somewhere. When the
webhook and the dashboard share a port, a single HTTP server is started.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-09-23 20:09:14 +02:00

52 lines
1.6 KiB
JavaScript

/** Typed-ish client for the dashboard API (M16-T11). */
export class ApiError extends Error {
constructor(status, message, details) {
super(message);
this.name = 'ApiError';
this.status = status;
this.details = details;
}
}
async function request(method, path, body) {
const response = await fetch(path, {
method,
headers: {
Accept: 'application/json',
...(body !== undefined && { 'Content-Type': 'application/json' }),
// Required by the server for every write: a cross-site form cannot set it.
'X-Requested-With': 'fws-dashboard',
},
body: body === undefined ? undefined : JSON.stringify(body),
credentials: 'same-origin',
});
const payload = await response.json().catch(() => null);
if (!response.ok) {
throw new ApiError(response.status, payload?.error ?? `Erreur ${response.status}`, payload?.details);
}
return payload;
}
const withQuery = (path, params) => {
const search = new URLSearchParams(
Object.entries(params ?? {}).filter(([, value]) => value !== undefined && value !== null && value !== ''),
).toString();
return search ? `${path}?${search}` : path;
};
export const api = {
get: (path, params) => request('GET', withQuery(path, params)),
post: (path, body = {}) => request('POST', path, body),
put: (path, body = {}) => request('PUT', path, body),
patch: (path, body = {}) => request('PATCH', path, body),
delete: (path, body = {}) => request('DELETE', path, body),
};
export const encodeRepo = (fullName) =>
String(fullName)
.split('/')
.map((part) => encodeURIComponent(part))
.join('/');