diff options
| author | nsfisis <nsfisis@gmail.com> | 2025-03-08 10:13:05 +0900 |
|---|---|---|
| committer | nsfisis <nsfisis@gmail.com> | 2025-03-08 10:13:05 +0900 |
| commit | 8dbdf96e674c1e26d7c98af8d0608f30bc1bf166 (patch) | |
| tree | 7c82476f6bbbc71d72ab7e71e39559eca197fd95 /frontend/app/api/client.ts | |
| parent | 54316868c3bec1ff9b04643dfe6c13cf56bf3246 (diff) | |
| parent | 1e6df136d8202c8adf65948527f4c3e7583b338c (diff) | |
| download | phperkaigi-2025-albatross-8dbdf96e674c1e26d7c98af8d0608f30bc1bf166.tar.gz phperkaigi-2025-albatross-8dbdf96e674c1e26d7c98af8d0608f30bc1bf166.tar.zst phperkaigi-2025-albatross-8dbdf96e674c1e26d7c98af8d0608f30bc1bf166.zip | |
Merge branch 'phperkaigi-2025-ws-to-polling' into phperkaigi-2025
Diffstat (limited to 'frontend/app/api/client.ts')
| -rw-r--r-- | frontend/app/api/client.ts | 120 |
1 files changed, 120 insertions, 0 deletions
diff --git a/frontend/app/api/client.ts b/frontend/app/api/client.ts new file mode 100644 index 0000000..3d8b5dd --- /dev/null +++ b/frontend/app/api/client.ts @@ -0,0 +1,120 @@ +import createClient from "openapi-fetch"; +import { createContext } from "react"; +import type { paths } from "./schema"; + +const apiClient = createClient<paths>({ + baseUrl: + process.env.NODE_ENV === "development" + ? "http://localhost:8003/phperkaigi/2025/code-battle/api/" + : "http://api-server/phperkaigi/2025/code-battle/api/", +}); + +export async function apiPostLogin(username: string, password: string) { + const { data, error } = await apiClient.POST("/login", { + body: { + username, + password, + }, + }); + if (error) throw new Error(error.message); + return data; +} + +export async function apiGetGames(token: string) { + const { data, error } = await apiClient.GET("/games", { + params: { + header: { Authorization: `Bearer ${token}` }, + }, + }); + if (error) throw new Error(error.message); + return data; +} + +export async function apiGetGame(token: string, gameId: number) { + const { data, error } = await apiClient.GET("/games/{game_id}", { + params: { + header: { Authorization: `Bearer ${token}` }, + path: { game_id: gameId }, + }, + }); + if (error) throw new Error(error.message); + return data; +} + +export async function apiGetGamePlayLatestState(token: string, gameId: number) { + const { data, error } = await apiClient.GET( + "/games/{game_id}/play/latest_state", + { + params: { + header: { Authorization: `Bearer ${token}` }, + path: { game_id: gameId }, + }, + }, + ); + if (error) throw new Error(error.message); + return data; +} + +export async function apiPostGamePlayCode( + token: string, + gameId: number, + code: string, +) { + const { error } = await apiClient.POST("/games/{game_id}/play/code", { + params: { + header: { Authorization: `Bearer ${token}` }, + path: { game_id: gameId }, + }, + body: { code }, + }); + if (error) throw new Error(error.message); +} + +export async function apiPostGamePlaySubmit( + token: string, + gameId: number, + code: string, +) { + const { data, error } = await apiClient.POST("/games/{game_id}/play/submit", { + params: { + header: { Authorization: `Bearer ${token}` }, + path: { game_id: gameId }, + }, + body: { code }, + }); + if (error) throw new Error(error.message); + return data; +} + +export async function apiGetGameWatchRanking(token: string, gameId: number) { + const { data, error } = await apiClient.GET( + "/games/{game_id}/watch/ranking", + { + params: { + header: { Authorization: `Bearer ${token}` }, + path: { game_id: gameId }, + }, + }, + ); + if (error) throw new Error(error.message); + return data; +} + +export async function apiGetGameWatchLatestStates( + token: string, + gameId: number, +) { + const { data, error } = await apiClient.GET( + "/games/{game_id}/watch/latest_states", + { + params: { + header: { Authorization: `Bearer ${token}` }, + path: { game_id: gameId }, + }, + }, + ); + if (error) throw new Error(error.message); + return data; +} + +export const ApiAuthTokenContext = createContext<string>(""); |
