Files
Michess/client/src/lib/game.ts
Michess 694177c108 feat: Vollständiges Rebranding zu MiChess, Copyright Tom Misch
- Alle @chessu/ → @michess/ (Package-Namen und Imports)
- Theme-Namen chessuDark/Light → michessDark/Light
- Alle Display-Texte: chessu/Michess → MiChess
- Autor: dotnize → Tom Misch
- Copyright: Nathaniel Tampus → Tom Misch
- README komplett neu (Deutsch, MiChess-spezifisch)
- CONTRIBUTING.md neu
- FUNDING.yml + CODE_OF_CONDUCT.md entfernt
- Fork-Hinweis auf chessu bleibt im README erhalten (MIT-Pflicht)
- pnpm-lock.yaml entfernt (wird beim Build neu generiert)

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-13 10:47:16 +02:00

77 lines
2.0 KiB
TypeScript

import { API_URL } from "@/config";
import type { Game } from "@michess/types";
export const createGame = async (side: string, unlisted: boolean) => {
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"
});
if (res && res.status === 201) {
const game: Game = await res.json();
return game;
}
} catch (err) {
console.error(err);
}
};
export const fetchActiveGame = 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);
}
};
export const fetchPublicGames = async () => {
try {
const res = await fetch(`${API_URL}/v1/games`, { cache: "no-store" });
if (res && res.status === 200) {
const games: Game[] = await res.json();
return games;
}
} catch (err) {
console.error(err);
}
};
export const fetchArchivedGame = async ({ id, userid }: { id?: number; userid?: number }) => {
let url = `${API_URL}/v1/games?`;
if (id) {
url += `id=${id}`;
} else {
url += `userid=${userid}`;
}
try {
// TODO: handle caching more efficiently
const res = await fetch(url, {
next: { revalidate: 20 }
});
if (res && res.status === 200) {
if (id) {
const game: Game = await res.json();
if (game.id) return game;
} else {
const games: Game[] = await res.json();
if (games.length && games[0].id) return games;
}
}
} catch (err) {
console.error(err);
}
};