Files
Papo/public/ts/services/api.ts
Pascal Prießnitz 22caa79b54
All checks were successful
Deploy Discord Bot / deploy (push) Successful in 37s
[deploy]
2025-12-04 16:43:38 +01:00

80 lines
3.9 KiB
TypeScript

import { getConfig } from '../state/store.js';
type FetchOptions = RequestInit & { query?: Record<string, string | number | boolean | undefined> };
function buildUrl(path: string, query?: Record<string, string | number | boolean | undefined>) {
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<T = unknown>(path: string, options: FetchOptions = {}): Promise<T> {
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()) as T;
}
return (await res.text()) as unknown as T;
}
export const api = {
me: () => request<{ user?: { username: string; discriminator: string; isAdmin?: boolean } }>('/me'),
guilds: () => request<{ guilds: Array<{ id: string; name: string; icon?: string }> }>('/guilds'),
overview: (guildId: string) => request(`/overview`, { query: { guildId } }),
settings: (guildId: string) => request(`/settings`, { query: { guildId } }),
saveSettings: (payload: Record<string, unknown>) => request('/settings', { method: 'POST', body: JSON.stringify(payload) }),
modules: (guildId: string) => request(`/modules`, { query: { guildId } }),
tickets: (guildId: string) => request(`/tickets`, { query: { guildId } }),
pipeline: (guildId: string, filter?: string) => request(`/tickets/pipeline`, { query: { guildId, filter } }),
sla: (guildId: string, range?: number) => request(`/tickets/sla`, { query: { guildId, range } }),
automations: (guildId: string) => request(`/automations`, { query: { guildId } }),
saveAutomation: (payload: Record<string, unknown>) =>
request(payload['id'] ? `/automations/${payload['id']}` : '/automations', {
method: payload['id'] ? 'PUT' : 'POST',
body: JSON.stringify(payload)
}),
kb: (guildId: string) => request(`/kb`, { query: { guildId } }),
saveKb: (payload: Record<string, unknown>) =>
request(payload['id'] ? `/kb/${payload['id']}` : '/kb', {
method: payload['id'] ? 'PUT' : 'POST',
body: JSON.stringify(payload)
}),
reactionRoles: (guildId: string) => request(`/reactionroles`, { query: { guildId } }),
saveReactionRole: (payload: Record<string, unknown> & { id?: string }) =>
request(payload.id ? `/reactionroles/${payload.id}` : '/reactionroles', {
method: payload.id ? 'PUT' : 'POST',
body: JSON.stringify(payload)
}),
events: (guildId: string) => request(`/events`, { query: { guildId } }),
saveEvent: (payload: Record<string, unknown> & { id?: string }) =>
request(payload.id ? `/events/${payload.id}` : '/events', {
method: payload.id ? 'PUT' : 'POST',
body: JSON.stringify(payload)
}),
statuspage: (guildId: string) => request(`/statuspage`, { query: { guildId } }),
saveStatuspage: (payload: Record<string, unknown>) => request('/statuspage', { method: 'POST', body: JSON.stringify(payload) }),
serverStats: (guildId: string) => request(`/serverstats`, { query: { guildId } }),
saveServerStats: (payload: Record<string, unknown>) => request('/serverstats', { method: 'POST', body: JSON.stringify(payload) }),
dynamicVoice: (guildId: string) => request(`/dynamicvoice`, { query: { guildId } }),
saveDynamicVoice: (payload: Record<string, unknown>) => request('/dynamicvoice', { method: 'POST', body: JSON.stringify(payload) })
};