- Refactor multiple API routes to utilize a centralized configuration function for base URL and token management, improving code consistency and maintainability. - Replace direct environment variable access with a unified config function in the `lib/api.ts` file. - Remove redundant error handling and streamline response handling in various API endpoints. - Delete unused job-related API routes and settings, simplifying the overall API structure.
36 lines
1.0 KiB
TypeScript
36 lines
1.0 KiB
TypeScript
import { NextRequest, NextResponse } from "next/server";
|
|
import { config } from "@/lib/api";
|
|
|
|
export async function GET(
|
|
request: NextRequest,
|
|
{ params }: { params: Promise<{ bookId: string }> }
|
|
) {
|
|
const { bookId } = await params;
|
|
|
|
try {
|
|
const { baseUrl, token } = config();
|
|
const response = await fetch(`${baseUrl}/books/${bookId}/thumbnail`, {
|
|
headers: { Authorization: `Bearer ${token}` },
|
|
});
|
|
|
|
if (!response.ok) {
|
|
return new NextResponse(`Failed to fetch thumbnail: ${response.status}`, {
|
|
status: response.status
|
|
});
|
|
}
|
|
|
|
const contentType = response.headers.get("content-type") || "image/webp";
|
|
const imageBuffer = await response.arrayBuffer();
|
|
|
|
return new NextResponse(imageBuffer, {
|
|
headers: {
|
|
"Content-Type": contentType,
|
|
"Cache-Control": "public, max-age=31536000, immutable",
|
|
},
|
|
});
|
|
} catch (error) {
|
|
console.error("Error fetching thumbnail:", error);
|
|
return new NextResponse("Failed to fetch thumbnail", { status: 500 });
|
|
}
|
|
}
|