- Add getReaderMetadata() to fetch media item metadata - Add updateReadingProgress() to sync reading progress to backend - Export ReaderMetadata and ReadingProgress types - Enable reader to communicate with Go backend
160 lines
3.8 KiB
TypeScript
160 lines
3.8 KiB
TypeScript
import { Alpine } from "./alpine";
|
|
import { showToast } from "./toast";
|
|
|
|
function getAuthHeader(): string {
|
|
const token = localStorage.getItem("token");
|
|
return token ? `Bearer ${token}` : "";
|
|
}
|
|
|
|
async function apiGet(url: string): Promise<Response> {
|
|
return fetch(`/api${url}`, {
|
|
headers: {
|
|
Authorization: getAuthHeader(),
|
|
"Content-Type": "application/json",
|
|
},
|
|
});
|
|
}
|
|
|
|
async function apiPost(url: string, data?: unknown): Promise<Response> {
|
|
return fetch(`/api${url}`, {
|
|
method: "POST",
|
|
headers: {
|
|
Authorization: getAuthHeader(),
|
|
"Content-Type": "application/json",
|
|
},
|
|
body: data ? JSON.stringify(data) : undefined,
|
|
});
|
|
}
|
|
|
|
async function apiPut(url: string, data?: unknown): Promise<Response> {
|
|
return fetch(`/api${url}`, {
|
|
method: "PUT",
|
|
headers: {
|
|
Authorization: getAuthHeader(),
|
|
"Content-Type": "application/json",
|
|
},
|
|
body: data ? JSON.stringify(data) : undefined,
|
|
});
|
|
}
|
|
|
|
async function apiDelete<T extends object>(
|
|
url: string,
|
|
data?: T,
|
|
): Promise<Response> {
|
|
return fetch(`/api${url}`, {
|
|
method: "DELETE",
|
|
headers: {
|
|
Authorization: getAuthHeader(),
|
|
"Content-Type": "application/json",
|
|
},
|
|
body: data ? JSON.stringify(data) : undefined,
|
|
});
|
|
}
|
|
|
|
async function apiPatch(url: string, data?: unknown): Promise<Response> {
|
|
return fetch(`/api${url}`, {
|
|
method: "PATCH",
|
|
headers: {
|
|
Authorization: getAuthHeader(),
|
|
"Content-Type": "application/json",
|
|
},
|
|
body: data ? JSON.stringify(data) : undefined,
|
|
});
|
|
}
|
|
|
|
async function handleResponse<T>(response: Response): Promise<T> {
|
|
if (!response.ok) {
|
|
const errorData = await response
|
|
.json()
|
|
.catch(() => ({ error: "Unknown error" }));
|
|
throw new Error(errorData.error || `HTTP ${response.status}`);
|
|
}
|
|
return response.json();
|
|
}
|
|
|
|
async function handleVoidResponse(response: Response): Promise<void> {
|
|
if (!response.ok) {
|
|
const errorData = await response
|
|
.json()
|
|
.catch(() => ({ error: "Unknown error" }));
|
|
throw new Error(errorData.error || `HTTP ${response.status}`);
|
|
}
|
|
}
|
|
|
|
function handleError(error: unknown, context: string): void {
|
|
console.error(`${context}:`, error);
|
|
const message =
|
|
error instanceof Error ? error.message : "An unexpected error occurred";
|
|
showToast(message, "error");
|
|
}
|
|
|
|
// ============================================================
|
|
// Reader API Functions
|
|
// ============================================================
|
|
interface ReaderMetadata {
|
|
media_item_id: string;
|
|
title: string;
|
|
author: string;
|
|
cover_image_path: string;
|
|
library_type: "ebook" | "comic" | "manga" | "pdf";
|
|
mime_type: string;
|
|
file_path: string;
|
|
chapter_metadata?: ChapterMetadata;
|
|
total_pages?: number;
|
|
}
|
|
interface ChapterMetadata {
|
|
chapters: Chapter[];
|
|
}
|
|
interface Chapter {
|
|
id: string;
|
|
title: string;
|
|
start_page: number;
|
|
page_count: number;
|
|
}
|
|
interface ReadingProgress {
|
|
current_page: number;
|
|
total_pages: number;
|
|
}
|
|
async function getReaderMetadata(mediaItemId: string): Promise<ReaderMetadata> {
|
|
const response = await apiGet(`/media-items/${mediaItemId}`);
|
|
return handleResponse<ReaderMetadata>(response);
|
|
}
|
|
async function updateReadingProgress(
|
|
mediaItemId: string,
|
|
progress: ReadingProgress,
|
|
): Promise<void> {
|
|
const response = await apiPut(
|
|
`/media-items/${mediaItemId}/progress`,
|
|
progress,
|
|
);
|
|
await handleVoidResponse(response);
|
|
}
|
|
|
|
export {
|
|
apiGet,
|
|
apiPost,
|
|
apiPut,
|
|
apiDelete,
|
|
apiPatch,
|
|
handleResponse,
|
|
handleVoidResponse,
|
|
handleError,
|
|
getReaderMetadata,
|
|
updateReadingProgress,
|
|
ReaderMetadata,
|
|
ReadingProgress,
|
|
ChapterMetadata,
|
|
Chapter,
|
|
};
|
|
|
|
Alpine.data("api", () => ({
|
|
get: apiGet,
|
|
post: apiPost,
|
|
put: apiPut,
|
|
delete: apiDelete,
|
|
patch: apiPatch,
|
|
handleResponse: handleResponse,
|
|
handleVoidResponse: handleVoidResponse,
|
|
handleError: handleError,
|
|
}));
|