rename to getGame & improve error handling

This commit is contained in:
Nathaniel Tampus
2023-03-05 00:33:44 +08:00
parent 997cbacc66
commit b7ee6f4371
2 changed files with 31 additions and 20 deletions

View File

@@ -2,24 +2,35 @@ import type { Game } from "@chessu/types";
import { API_URL } from "@/config";
export const createGame = async (side: string, unlisted: boolean) => {
const res = await fetch(`${API_URL}/v1/games`, {
method: "POST",
credentials: "include",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify({ side, unlisted })
});
try {
const res = await fetch(`${API_URL}/v1/games`, {
method: "POST",
credentials: "include",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify({ side, unlisted }),
cache: "no-store"
});
const game: Game | undefined = await res.json();
return game;
};
export const findGame = async (code: string) => {
const res = await fetch(`${API_URL}/v1/games/${code}`);
if (res && res.status === 200) {
const game: Game = await res.json();
return game;
if (res && res.status === 201) {
const game: Game = await res.json();
return game;
}
} catch (err) {
console.error(err);
}
};
export const getGame = async (code: string) => {
try {
const res = await fetch(`${API_URL}/v1/games/${code}`, { cache: "no-store" });
if (res && res.status === 200) {
const game: Game = await res.json();
return game;
}
} catch (err) {
console.error(err);
}
};