separated server calls
This commit is contained in:
parent
7cc159edd7
commit
45b4c7f7fc
1 changed files with 99 additions and 31 deletions
23
tank-frontend/src/serverCalls.tsx
Normal file
23
tank-frontend/src/serverCalls.tsx
Normal 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' });
|
||||
}
|
||||
Reference in a new issue