import type { Question } from '../types/question' import type { Talk } from '../types/talk' import { ofetch } from 'ofetch' import { INALIA_API_KEY, INALIA_ENDPOINT, INALIA_OAUTH, INALIA_OAUTH_SESSION, INALIA_TALK_NUMBER } from './constants' export const api = ofetch.create({ baseURL: INALIA_OAUTH ? '/_inalia' : INALIA_ENDPOINT, headers: { Accept: 'application/json', ...(INALIA_OAUTH ? { 'X-Inalia-Session': INALIA_OAUTH_SESSION } : { Authorization: `Bearer ${INALIA_API_KEY}` }), }, }) export async function fetchUsername(): Promise { const response = await api<{ data?: { username?: string | null } }>('/api/me') const username = response.data?.username if (typeof username !== 'string' || !username.trim()) throw new Error('Inalia account discovery failed: username is missing.') return username } function talkPath(username: string): string { return `/api/${encodeURIComponent(username)}/talks/${INALIA_TALK_NUMBER}` } export async function fetchTalk(username: string): Promise { return api<{ data: Talk }>(talkPath(username)).then(response => response.data) } export async function runTalk(username: string): Promise { await api(`${talkPath(username)}/run`, { method: 'POST', }) } export async function startDemoMode(username: string): Promise { await api(`${talkPath(username)}/demo-mode`, { method: 'POST', }) } export async function stopDemoMode(username: string): Promise { await api(`${talkPath(username)}/demo-mode`, { method: 'DELETE', }) } export async function fetchQuestion(username: string, questionNumber: number): Promise { return api<{ data: Question }>(`${talkPath(username)}/questions/${questionNumber}`).then(response => response.data) }