/** * API client for the reader module — reading settings, chapters, and progress. */ import type { ChapterDetail, ChapterSummary, ReadingProgress, ReadingSettings, } from "../types/reader"; const API_BASE = "/api"; /** * Fetch the current user's reading settings. * Auto-creates defaults on the server if none exist. */ export async function getReadingSettings(): Promise { const response = await fetch(`${API_BASE}/reader/settings/`); if (!response.ok) { throw new Error( `Failed to fetch reading settings: ${response.status} ${response.statusText}` ); } return response.json() as Promise; } /** * Update (full or partial) the user's reading settings. */ export async function updateReadingSettings( settings: Partial ): Promise { const method = settings.theme !== undefined ? "PUT" : "PATCH"; const response = await fetch(`${API_BASE}/reader/settings/`, { method, headers: { "Content-Type": "application/json" }, body: JSON.stringify(settings), }); if (!response.ok) { throw new Error( `Failed to update reading settings: ${response.status} ${response.statusText}` ); } return response.json() as Promise; } /** * Fetch the table of contents (chapter list) for a book. */ export async function getChapters(bookId: number): Promise { const response = await fetch(`${API_BASE}/books/${bookId}/chapters/`); if (!response.ok) { throw new Error( `Failed to fetch chapters: ${response.status} ${response.statusText}` ); } return response.json() as Promise; } /** * Fetch a specific chapter with full content for reading. */ export async function getChapterContent( bookId: number, chapterNumber: number ): Promise { const response = await fetch( `${API_BASE}/books/${bookId}/chapters/${chapterNumber}/` ); if (!response.ok) { throw new Error( `Failed to fetch chapter ${chapterNumber}: ${response.status} ${response.statusText}` ); } return response.json() as Promise; } /** * Fetch reading progress for a book. */ export async function getReadingProgress( bookId: number ): Promise { const response = await fetch(`${API_BASE}/books/${bookId}/progress/`); if (!response.ok) { throw new Error( `Failed to fetch reading progress: ${response.status} ${response.statusText}` ); } return response.json() as Promise; } /** * Update reading progress for a book. */ export async function updateReadingProgress( bookId: number, progress: Partial ): Promise { const response = await fetch(`${API_BASE}/books/${bookId}/progress/`, { method: "PUT", headers: { "Content-Type": "application/json" }, body: JSON.stringify(progress), }); if (!response.ok) { throw new Error( `Failed to update reading progress: ${response.status} ${response.statusText}` ); } return response.json() as Promise; }