Files
Papo/public/ts-build/services/api.js
Pascal Prießnitz 7a296f7b4a
All checks were successful
Deploy Discord Bot / deploy (push) Successful in 38s
[deploy] Emit frontend bundle as ESM for browser
2025-12-04 17:06:27 +01:00

72 lines
3.3 KiB
JavaScript

import { getConfig } from '../state/store.js';
function buildUrl(path, query) {
const cfg = getConfig();
const base = cfg?.baseApi || '';
const url = new URL(path.startsWith('http') ? path : `${base}${path}`, window.location.origin);
if (query) {
Object.entries(query).forEach(([k, v]) => {
if (v === undefined || v === null)
return;
url.searchParams.set(k, String(v));
});
}
return url.toString();
}
async function request(path, options = {}) {
const { query, headers, ...rest } = options;
const url = buildUrl(path, query);
const res = await fetch(url, {
...rest,
headers: {
'Content-Type': 'application/json',
...(headers || {})
}
});
if (!res.ok) {
const text = await res.text().catch(() => res.statusText);
throw new Error(`Request failed: ${res.status} ${text}`);
}
const contentType = res.headers.get('content-type');
if (contentType && contentType.includes('application/json')) {
return (await res.json());
}
return (await res.text());
}
export const api = {
me: () => request('/me'),
guilds: () => request('/guilds'),
overview: (guildId) => request(`/overview`, { query: { guildId } }),
settings: (guildId) => request(`/settings`, { query: { guildId } }),
saveSettings: (payload) => request('/settings', { method: 'POST', body: JSON.stringify(payload) }),
modules: (guildId) => request(`/modules`, { query: { guildId } }),
tickets: (guildId) => request(`/tickets`, { query: { guildId } }),
pipeline: (guildId, filter) => request(`/tickets/pipeline`, { query: { guildId, filter } }),
sla: (guildId, range) => request(`/tickets/sla`, { query: { guildId, range } }),
automations: (guildId) => request(`/automations`, { query: { guildId } }),
saveAutomation: (payload) => request(payload['id'] ? `/automations/${payload['id']}` : '/automations', {
method: payload['id'] ? 'PUT' : 'POST',
body: JSON.stringify(payload)
}),
kb: (guildId) => request(`/kb`, { query: { guildId } }),
saveKb: (payload) => request(payload['id'] ? `/kb/${payload['id']}` : '/kb', {
method: payload['id'] ? 'PUT' : 'POST',
body: JSON.stringify(payload)
}),
reactionRoles: (guildId) => request(`/reactionroles`, { query: { guildId } }),
saveReactionRole: (payload) => request(payload.id ? `/reactionroles/${payload.id}` : '/reactionroles', {
method: payload.id ? 'PUT' : 'POST',
body: JSON.stringify(payload)
}),
events: (guildId) => request(`/events`, { query: { guildId } }),
saveEvent: (payload) => request(payload.id ? `/events/${payload.id}` : '/events', {
method: payload.id ? 'PUT' : 'POST',
body: JSON.stringify(payload)
}),
statuspage: (guildId) => request(`/statuspage`, { query: { guildId } }),
saveStatuspage: (payload) => request('/statuspage', { method: 'POST', body: JSON.stringify(payload) }),
serverStats: (guildId) => request(`/serverstats`, { query: { guildId } }),
saveServerStats: (payload) => request('/serverstats', { method: 'POST', body: JSON.stringify(payload) }),
dynamicVoice: (guildId) => request(`/dynamicvoice`, { query: { guildId } }),
saveDynamicVoice: (payload) => request('/dynamicvoice', { method: 'POST', body: JSON.stringify(payload) })
};