import type { DailyResponse } from '@wordle/shared'; import { getApiBase } from './messages/index.js'; function base() { return getApiBase(); } async function fetchJSON(url: string, options?: RequestInit): Promise { const res = await fetch(url, options); if (!res.ok) { const body = await res.json().catch(() => ({})); throw new Error((body as { error?: string }).error ?? `HTTP ${res.status}`); } return res.json() as Promise; } export function getValidateWord(word: string): Promise<{ word: string; valid: boolean }> { return fetchJSON(`${base()}/validate/${encodeURIComponent(word)}`); } export function getDaily(): Promise { return fetchJSON(`${base()}/daily`); } export function postGuess(body: { wordId: number; guess: string; tryNo: number }): Promise { return fetchJSON(`${base()}/guess`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(body), }); } export function postPlayAgain(body: { skillMetric: number }): Promise { return fetchJSON(`${base()}/play-again`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(body), }); } export function postSolver(body: import('@wordle/shared').SolverRequest): Promise { return fetchJSON(`${base()}/solver`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(body), }); }