Added a Login and Register-Page

This commit is contained in:
CodingPhoenixx
2026-02-12 16:31:47 +01:00
parent 7a69e4a4ea
commit 61ebfec1c9
10 changed files with 311 additions and 15 deletions
+54
View File
@@ -0,0 +1,54 @@
export type HttpMethod = 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH' | 'QUERY';
export interface ApiOptions extends RequestInit {
method?: HttpMethod;
body?: any;
}
/**
* Zentraler API-Wrapper mit typisiertem Rückgabewert und Fehlerhandling.
* Beispiele:
* const tracks = await api<Track[]>('/api/tracks');
* await api<void>('/api/tracks/1', { method: 'DELETE' });
*/
export async function api<T = any>(
path: string,
options: ApiOptions = {}
): Promise<T> {
const config: RequestInit = {
...options,
headers: {
'Content-Type': 'application/json',
...(options.headers || {}),
},
};
if (options.body && typeof options.body !== 'string') {
config.body = JSON.stringify(options.body);
}
const response = await fetch(path, config);
if (!response.ok) {
let msg = `HTTP Error ${response.status}`;
try {
const text = await response.text();
msg += `: ${text}`;
} catch { }
throw new Error(msg);
}
if (response.status === 204) return undefined as T;
try {
return (await response.json()) as T;
} catch {
return undefined as T;
}
}
export const apiGet = <T>(path: string) => api<T>(path, { method: 'GET' });
export const apiPost = <T>(path: string, body: any) => api<T>(path, { method: 'POST', body });
export const apiPut = <T>(path: string, body: any) => api<T>(path, { method: 'PUT', body });
export const apiDelete = <T>(path: string) => api<T>(path, { method: 'DELETE' });