Archived
feat: implement mobile reader
- implement mobile epub reader
This commit is contained in:
@@ -1,55 +1,31 @@
|
||||
import api from "./client";
|
||||
import type {
|
||||
Bookmark,
|
||||
Note,
|
||||
CreateBookmarkPayload,
|
||||
CreateNotePayload,
|
||||
PaginatedResponse,
|
||||
} from "@cloud-reader/shared";
|
||||
import type { PaginatedResponse } from "@cloud-reader/shared";
|
||||
import type { Bookmark, CreateMarkerPayload } from "../types";
|
||||
|
||||
export function fetchBookmarks(
|
||||
bookId?: string,
|
||||
export async function fetchBookmarks(
|
||||
ebookId?: string | number,
|
||||
): Promise<PaginatedResponse<Bookmark>> {
|
||||
const params = bookId ? { book: bookId } : {};
|
||||
return api
|
||||
.get<PaginatedResponse<Bookmark>>("/api/annotations/bookmarks/", { params })
|
||||
.then((res) => res.data);
|
||||
const params: Record<string, string> = {};
|
||||
if (ebookId != null && ebookId !== "") {
|
||||
params.ebook = String(ebookId);
|
||||
}
|
||||
const { data } = await api.get<PaginatedResponse<Bookmark>>(
|
||||
"/api/annotations/bookmarks/",
|
||||
{ params },
|
||||
);
|
||||
return data;
|
||||
}
|
||||
|
||||
export function createBookmark(
|
||||
payload: CreateBookmarkPayload,
|
||||
export async function createMarker(
|
||||
payload: CreateMarkerPayload,
|
||||
): Promise<Bookmark> {
|
||||
return api
|
||||
.post<Bookmark>("/api/annotations/bookmarks/", payload)
|
||||
.then((res) => res.data);
|
||||
const { data } = await api.post<Bookmark>(
|
||||
"/api/annotations/bookmarks/",
|
||||
payload,
|
||||
);
|
||||
return data;
|
||||
}
|
||||
|
||||
export function deleteBookmark(id: string): Promise<void> {
|
||||
return api.delete(`/api/annotations/bookmarks/${id}/`).then(() => {});
|
||||
export async function deleteBookmark(id: string): Promise<void> {
|
||||
await api.delete(`/api/annotations/bookmarks/${id}/`);
|
||||
}
|
||||
|
||||
export function fetchNotes(bookId?: string): Promise<PaginatedResponse<Note>> {
|
||||
const params = bookId ? { book: bookId } : {};
|
||||
return api
|
||||
.get<PaginatedResponse<Note>>("/api/annotations/notes/", { params })
|
||||
.then((res) => res.data);
|
||||
}
|
||||
|
||||
export function createNote(payload: CreateNotePayload): Promise<Note> {
|
||||
return api
|
||||
.post<Note>("/api/annotations/notes/", payload)
|
||||
.then((res) => res.data);
|
||||
}
|
||||
|
||||
export function updateNote(
|
||||
id: string,
|
||||
content: string,
|
||||
): Promise<Note> {
|
||||
return api
|
||||
.patch<Note>(`/api/annotations/notes/${id}/`, { content })
|
||||
.then((res) => res.data);
|
||||
}
|
||||
|
||||
export function deleteNote(id: string): Promise<void> {
|
||||
return api.delete(`/api/annotations/notes/${id}/`).then(() => {});
|
||||
}
|
||||
+1
-20
@@ -1,31 +1,12 @@
|
||||
import api from "./client";
|
||||
import type { Book, PaginatedResponse } from "@cloud-reader/shared";
|
||||
|
||||
export function fetchBooks(
|
||||
page = 1,
|
||||
pageSize = 20,
|
||||
): Promise<PaginatedResponse<Book>> {
|
||||
return api
|
||||
.get<PaginatedResponse<Book>>("/api/books/", {
|
||||
params: { page, page_size: pageSize },
|
||||
})
|
||||
.then((res) => res.data);
|
||||
}
|
||||
|
||||
export function fetchBook(id: string): Promise<Book> {
|
||||
return api.get<Book>(`/api/books/${id}/`).then((res) => res.data);
|
||||
}
|
||||
|
||||
export function searchBooks(
|
||||
query: string,
|
||||
): Promise<PaginatedResponse<Book>> {
|
||||
return api
|
||||
.get<PaginatedResponse<Book>>("/api/books/search/", {
|
||||
.get<PaginatedResponse<Book>>("/api/books/", {
|
||||
params: { q: query },
|
||||
})
|
||||
.then((res) => res.data);
|
||||
}
|
||||
|
||||
export function deleteBook(id: string): Promise<void> {
|
||||
return api.delete(`/api/books/${id}/`).then(() => {});
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
import axios, { type AxiosError, type InternalAxiosRequestConfig } from "axios";
|
||||
import AsyncStorage from "@react-native-async-storage/async-storage";
|
||||
import type { TokenResponse } from "@cloud-reader/shared";
|
||||
|
||||
const STORAGE_KEYS = {
|
||||
ACCESS_TOKEN: "access_token",
|
||||
@@ -39,6 +40,26 @@ async function clearTokens(): Promise<void> {
|
||||
]);
|
||||
}
|
||||
|
||||
async function saveTokens(tokens: TokenResponse): Promise<void> {
|
||||
await setTokens(tokens.access, tokens.refresh);
|
||||
}
|
||||
|
||||
async function loadTokens(): Promise<TokenResponse | null> {
|
||||
const [access, refresh] = await Promise.all([
|
||||
getAccessToken(),
|
||||
getRefreshToken(),
|
||||
]);
|
||||
if (!access || !refresh) {
|
||||
return null;
|
||||
}
|
||||
return { access, refresh };
|
||||
}
|
||||
|
||||
/** Absolute base URL used for direct (non-axios) requests like file downloads. */
|
||||
function getApiBaseUrl(): string {
|
||||
return api.defaults.baseURL ?? "";
|
||||
}
|
||||
|
||||
// ── Request interceptor ─────────────────────────────────────────────
|
||||
|
||||
api.interceptors.request.use(async (config: InternalAxiosRequestConfig) => {
|
||||
@@ -136,5 +157,14 @@ api.interceptors.response.use(
|
||||
},
|
||||
);
|
||||
|
||||
export { getAccessToken, getRefreshToken, setTokens, clearTokens };
|
||||
const apiClient = api;
|
||||
|
||||
export {
|
||||
apiClient,
|
||||
getAccessToken,
|
||||
saveTokens,
|
||||
loadTokens,
|
||||
clearTokens,
|
||||
getApiBaseUrl,
|
||||
};
|
||||
export default api;
|
||||
+17
-36
@@ -1,54 +1,35 @@
|
||||
import { apiClient } from "./client";
|
||||
import { apiClient, getApiBaseUrl } from "./client";
|
||||
import type {
|
||||
EBookListItem,
|
||||
EBookDetail,
|
||||
ReadingProgress,
|
||||
ReadingSettings,
|
||||
TocResponse,
|
||||
ContentResponse,
|
||||
PaginatedResponse,
|
||||
} from "@cloud-reader/shared";
|
||||
|
||||
export const ebooksApi = {
|
||||
/** List uploaded e-books */
|
||||
/** List the authenticated user's uploaded e-books */
|
||||
list() {
|
||||
return apiClient.get<PaginatedResponse<EBookListItem>>("/api/ebooks/");
|
||||
return apiClient.get<PaginatedResponse<EBookListItem>>(
|
||||
"/api/books/ebooks/",
|
||||
);
|
||||
},
|
||||
|
||||
/** Get e-book detail */
|
||||
/** Get e-book detail (metadata, format, progress) */
|
||||
get(id: number) {
|
||||
return apiClient.get<EBookDetail>(`/api/ebooks/${id}/`);
|
||||
return apiClient.get<EBookDetail>(`/api/books/ebooks/${id}/`);
|
||||
},
|
||||
|
||||
/** Get table of contents */
|
||||
/** Get the table of contents (used as a fallback for chapter navigation) */
|
||||
getToc(id: number) {
|
||||
return apiClient.get<TocResponse>(`/api/ebooks/${id}/toc/`);
|
||||
return apiClient.get<TocResponse>(`/api/books/ebooks/${id}/toc/`);
|
||||
},
|
||||
|
||||
/** Get page content */
|
||||
getContent(id: number, page: number) {
|
||||
return apiClient.get<ContentResponse>(
|
||||
`/api/ebooks/${id}/content/?page=${page}`,
|
||||
);
|
||||
/**
|
||||
* Absolute URL to stream the raw ebook file. The endpoint requires JWT auth,
|
||||
* so callers download it with an Authorization header (e.g. via
|
||||
* expo-file-system) rather than handing the URL to a renderer directly.
|
||||
*/
|
||||
getFileUrl(id: number): string {
|
||||
return `${getApiBaseUrl()}/api/books/ebooks/${id}/file/`;
|
||||
},
|
||||
|
||||
/** Update reading progress */
|
||||
updateProgress(id: number, data: Partial<ReadingProgress>) {
|
||||
return apiClient.patch<ReadingProgress>(
|
||||
`/api/ebooks/${id}/progress/`,
|
||||
data,
|
||||
);
|
||||
},
|
||||
|
||||
/** Get or update reading settings */
|
||||
getSettings(id: number) {
|
||||
return apiClient.get<ReadingSettings>(`/api/ebooks/${id}/settings/`);
|
||||
},
|
||||
|
||||
updateSettings(id: number, data: Partial<ReadingSettings>) {
|
||||
return apiClient.patch<ReadingSettings>(
|
||||
`/api/ebooks/${id}/settings/`,
|
||||
data,
|
||||
);
|
||||
},
|
||||
};
|
||||
};
|
||||
|
||||
@@ -1,4 +0,0 @@
|
||||
export { apiClient, saveTokens, loadTokens, clearTokens } from "./client";
|
||||
export { booksApi } from "./books";
|
||||
export { ebooksApi } from "./ebooks";
|
||||
export { annotationsApi } from "./annotations";
|
||||
@@ -0,0 +1,75 @@
|
||||
/**
|
||||
* Reader API client — reading settings and per-book progress.
|
||||
* Mirrors frontend/src/api/reader.ts. Uses the shared axios client so the
|
||||
* JWT access token is attached automatically.
|
||||
*/
|
||||
|
||||
import api from "./client";
|
||||
import type { ReadingProgress, ReadingSettings } from "../types/reader";
|
||||
|
||||
export async function getReadingSettings(): Promise<ReadingSettings> {
|
||||
const { data } = await api.get<ReadingSettings>("/api/reader/settings/");
|
||||
return data;
|
||||
}
|
||||
|
||||
export async function updateReadingSettings(
|
||||
settings: Partial<ReadingSettings>,
|
||||
): Promise<ReadingSettings> {
|
||||
const { data } = await api.patch<ReadingSettings>(
|
||||
"/api/reader/settings/",
|
||||
settings,
|
||||
);
|
||||
return data;
|
||||
}
|
||||
|
||||
interface ProgressWire {
|
||||
current_position: number;
|
||||
last_page: number;
|
||||
epub_location?: string;
|
||||
updated_at?: string;
|
||||
}
|
||||
|
||||
function toReadingProgress(bookId: number, data: ProgressWire): ReadingProgress {
|
||||
return {
|
||||
id: bookId,
|
||||
book: bookId,
|
||||
current_chapter: data.last_page || 1,
|
||||
current_position: data.current_position ?? 0,
|
||||
percentage: data.current_position ?? 0,
|
||||
epub_location: data.epub_location ?? "",
|
||||
updated_at: data.updated_at ?? "",
|
||||
};
|
||||
}
|
||||
|
||||
export async function getReadingProgress(
|
||||
bookId: number,
|
||||
): Promise<ReadingProgress> {
|
||||
const { data } = await api.get<ProgressWire>(
|
||||
`/api/books/ebooks/${bookId}/progress/`,
|
||||
);
|
||||
return toReadingProgress(bookId, data);
|
||||
}
|
||||
|
||||
export async function updateReadingProgress(
|
||||
bookId: number,
|
||||
progress: Partial<ReadingProgress>,
|
||||
): Promise<ReadingProgress> {
|
||||
const body: Record<string, string | number> = {};
|
||||
if (
|
||||
progress.percentage !== undefined ||
|
||||
progress.current_position !== undefined
|
||||
) {
|
||||
body.current_position = progress.percentage ?? progress.current_position ?? 0;
|
||||
}
|
||||
if (progress.current_chapter !== undefined) {
|
||||
body.last_page = progress.current_chapter;
|
||||
}
|
||||
if (progress.epub_location !== undefined) {
|
||||
body.epub_location = progress.epub_location;
|
||||
}
|
||||
const { data } = await api.patch<ProgressWire>(
|
||||
`/api/books/ebooks/${bookId}/progress/`,
|
||||
body,
|
||||
);
|
||||
return toReadingProgress(bookId, data);
|
||||
}
|
||||
Reference in New Issue
Block a user