separated server calls

This commit is contained in:
Ronja Spiegelberg 2024-04-13 17:56:33 +02:00
commit 45b4c7f7fc

View file

@ -0,0 +1,23 @@
export type PlayerResponse = {
readonly name: string;
readonly id: string;
};
export async function fetchTyped<T>({ url, method }: { url: URL; method: string; }) {
const response = await fetch(url, { method });
if (!response.ok)
return null;
return await response.json() as T;
}
export function postPlayer(name: string) {
const url = new URL(import.meta.env.VITE_TANK_PLAYER_URL);
url.searchParams.set('name', name);
return fetchTyped<PlayerResponse>({ url, method: 'POST' });
}
export function getPlayer(id: string) {
const url = new URL(import.meta.env.VITE_TANK_PLAYER_URL);
url.searchParams.set('id', id);
return fetchTyped<PlayerResponse>({ url, method: 'GET' });
}