import type { TeachingBook } from '../domain/teachingDesign' export interface BookSummary { id: string name: string updatedAt: string lessonCount: number } export interface BookRecord { id: string name: string updatedAt: string data: TeachingBook } export interface BookMeta { id: string name: string updatedAt: string } export interface GenerateResult { filename: string markdown: string } interface ErrorBody { error?: string } async function request(path: string, init?: RequestInit): Promise { const response = await fetch(path, { ...init, headers: { 'Content-Type': 'application/json', ...(init?.headers ?? {}) }, }) if (!response.ok) { const body = (await response.json().catch(() => null)) as ErrorBody | null throw new Error(body?.error ?? `请求失败(${response.status})。`) } return response.json() as Promise } export function listBooks(): Promise { return request('/api/books') } export function createBook(name: string): Promise { return request('/api/books', { method: 'POST', body: JSON.stringify({ name }) }) } export function getBook(id: string): Promise { return request(`/api/books/${id}`) } export function updateBook(id: string, data: TeachingBook): Promise { return request(`/api/books/${id}`, { method: 'PUT', body: JSON.stringify({ data }) }) } export function renameBook(id: string, name: string): Promise { return request(`/api/books/${id}`, { method: 'PATCH', body: JSON.stringify({ name }) }) } export function deleteBook(id: string): Promise<{ ok: true }> { return request(`/api/books/${id}`, { method: 'DELETE' }) } export function generateLesson(topic: string): Promise { return request('/api/generate', { method: 'POST', body: JSON.stringify({ topic }) }) }