refactor: replace book details GET route with server action
All checks were successful
Deploy with Docker Compose / deploy (push) Successful in 6m16s

This commit is contained in:
2026-02-28 12:21:07 +01:00
parent 5eba969846
commit 26021ea907
5 changed files with 55 additions and 73 deletions

36
src/app/actions/books.ts Normal file
View File

@@ -0,0 +1,36 @@
"use server";
import { BookService } from "@/lib/services/book.service";
import { AppError } from "@/utils/errors";
import type { KomgaBook } from "@/types/komga";
interface BookDataResult {
success: boolean;
data?: {
book: KomgaBook;
pages: number[];
nextBook: KomgaBook | null;
};
message?: string;
}
export async function getBookData(bookId: string): Promise<BookDataResult> {
try {
const data = await BookService.getBook(bookId);
const nextBook = await BookService.getNextBook(bookId, data.book.seriesId);
return {
success: true,
data: {
...data,
nextBook,
},
};
} catch (error) {
if (error instanceof AppError) {
return { success: false, message: error.code };
}
return { success: false, message: "BOOK_DATA_FETCH_ERROR" };
}
}

View File

@@ -1,54 +0,0 @@
import { NextResponse } from "next/server";
import { BookService } from "@/lib/services/book.service";
import { ERROR_CODES } from "@/constants/errorCodes";
import { getErrorMessage } from "@/utils/errors";
import { AppError } from "@/utils/errors";
import type { KomgaBookWithPages } from "@/types/komga";
import type { NextRequest } from "next/server";
import logger from "@/lib/logger";
type ErrorWithStatusParams = AppError & { params?: { status?: number } };
// Cache handled in service via fetchFromApi options
export async function GET(
request: NextRequest,
{ params }: { params: Promise<{ bookId: string }> }
) {
try {
const bookId: string = (await params).bookId;
const data: KomgaBookWithPages = await BookService.getBook(bookId);
const nextBook = await BookService.getNextBook(bookId, data.book.seriesId);
return NextResponse.json({ ...data, nextBook });
} catch (error) {
logger.error({ err: error }, "API Books - Erreur:");
if (error instanceof AppError) {
const isNotFound =
error.code === ERROR_CODES.BOOK.NOT_FOUND ||
(error.code === ERROR_CODES.KOMGA.HTTP_ERROR &&
(error as ErrorWithStatusParams).params?.status === 404);
return NextResponse.json(
{
error: {
code: error.code,
name: "Book fetch error",
message: getErrorMessage(error.code),
} as AppError,
},
{ status: isNotFound ? 404 : 500 }
);
}
return NextResponse.json(
{
error: {
code: ERROR_CODES.BOOK.NOT_FOUND,
name: "Book fetch error",
message: getErrorMessage(ERROR_CODES.BOOK.NOT_FOUND),
} as AppError,
},
{ status: 500 }
);
}
}