improved theming, error handling, table sort

This commit is contained in:
Vinzenz Schroeter 2024-04-14 18:26:13 +02:00
commit 7ce0e543ec

View file

@ -1,6 +1,14 @@
import {Guid} from './Guid.ts';
export type PlayerResponse = {
export type ServerResponse<T> = {
ok: boolean;
statusCode: number;
statusText: string;
additionalErrorText?: string;
successResult?: T;
}
export type Player = {
readonly name: string;
readonly id: Guid;
readonly scores: {
@ -14,11 +22,19 @@ export type NameId = {
id: Guid
};
export async function fetchTyped<T>({url, method}: { url: URL; method: string; }) {
export async function fetchTyped<T>({url, method}: { url: URL; method: string; }): Promise<ServerResponse<T>> {
const response = await fetch(url, {method});
if (!response.ok)
return null;
return await response.json() as T;
const result: ServerResponse<T> = {
ok: response.ok,
statusCode: response.status,
statusText: response.statusText
}
if (response.ok)
result.successResult = await response.json();
else
result.additionalErrorText = await response.text();
return result;
}
export function postPlayer({name, id}: NameId) {
@ -33,10 +49,10 @@ export function getPlayer(id: Guid) {
const url = new URL(import.meta.env.VITE_TANK_PLAYER_URL);
url.searchParams.set('id', id);
return fetchTyped<PlayerResponse>({url, method: 'GET'});
return fetchTyped<Player>({url, method: 'GET'});
}
export function getScores() {
const url = new URL('/scores', import.meta.env.VITE_TANK_API);
return fetchTyped<PlayerResponse[]>({url, method: 'GET'});
return fetchTyped<Player[]>({url, method: 'GET'});
}