File size: 1,143 Bytes
e9ce6e9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import type { AEPOAction, StepResult, StateResult, ResetResult, TaskDifficulty } from "./types";

// Static export has no Next.js server-side rewrite proxy, so API calls go
// directly to FastAPI at the same origin.  FastAPI routes: /reset /step /state
const BASE = "";

export async function fetchState(): Promise<StateResult> {
  const res = await fetch(`${BASE}/state`, { cache: "no-store" });
  if (!res.ok) throw new Error(`GET /state → ${res.status}`);
  return res.json();
}

export async function postReset(task: TaskDifficulty = "easy"): Promise<ResetResult> {
  const res = await fetch(`${BASE}/reset`, {
    method: "POST",
    headers: { "Content-Type": "application/json" },
    body: JSON.stringify({ task }),
  });
  if (!res.ok) throw new Error(`POST /reset → ${res.status}`);
  return res.json();
}

export async function postStep(action: AEPOAction): Promise<StepResult> {
  const res = await fetch(`${BASE}/step`, {
    method: "POST",
    headers: { "Content-Type": "application/json" },
    body: JSON.stringify({ action }),
  });
  if (!res.ok) throw new Error(`POST /step → ${res.status}`);
  return res.json();
}