refactor: streamline API calls and enhance configuration management

- 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.
This commit is contained in:
2026-03-09 14:16:01 +01:00
parent 0f5094575a
commit 85cad1a7e7
15 changed files with 71 additions and 272 deletions

View File

@@ -1,35 +1,25 @@
import { NextRequest, NextResponse } from "next/server"; import { NextRequest, NextResponse } from "next/server";
import { config } from "@/lib/api";
export async function GET( export async function GET(
request: NextRequest, request: NextRequest,
{ params }: { params: Promise<{ bookId: string; pageNum: string }> } { params }: { params: Promise<{ bookId: string; pageNum: string }> }
) { ) {
const { bookId, pageNum } = await params; const { bookId, pageNum } = await params;
// Récupérer les query params (format, width, quality)
const { searchParams } = new URL(request.url);
const format = searchParams.get("format") || "webp";
const width = searchParams.get("width") || "";
const quality = searchParams.get("quality") || "";
// Construire l'URL vers l'API backend
const apiBaseUrl = process.env.API_BASE_URL || "http://api:7080";
const apiUrl = new URL(`${apiBaseUrl}/books/${bookId}/pages/${pageNum}`);
apiUrl.searchParams.set("format", format);
if (width) apiUrl.searchParams.set("width", width);
if (quality) apiUrl.searchParams.set("quality", quality);
// Faire la requête à l'API
const token = process.env.API_BOOTSTRAP_TOKEN;
if (!token) {
return new NextResponse("API token not configured", { status: 500 });
}
try { try {
const { baseUrl, token } = config();
const { searchParams } = new URL(request.url);
const format = searchParams.get("format") || "webp";
const width = searchParams.get("width") || "";
const quality = searchParams.get("quality") || "";
const apiUrl = new URL(`${baseUrl}/books/${bookId}/pages/${pageNum}`);
apiUrl.searchParams.set("format", format);
if (width) apiUrl.searchParams.set("width", width);
if (quality) apiUrl.searchParams.set("quality", quality);
const response = await fetch(apiUrl.toString(), { const response = await fetch(apiUrl.toString(), {
headers: { headers: { Authorization: `Bearer ${token}` },
Authorization: `Bearer ${token}`,
},
}); });
if (!response.ok) { if (!response.ok) {

View File

@@ -1,4 +1,5 @@
import { NextRequest, NextResponse } from "next/server"; import { NextRequest, NextResponse } from "next/server";
import { config } from "@/lib/api";
export async function GET( export async function GET(
request: NextRequest, request: NextRequest,
@@ -6,19 +7,10 @@ export async function GET(
) { ) {
const { bookId } = await params; const { bookId } = await params;
const apiBaseUrl = process.env.API_BASE_URL || "http://api:7080";
const apiUrl = `${apiBaseUrl}/books/${bookId}/thumbnail`;
const token = process.env.API_BOOTSTRAP_TOKEN;
if (!token) {
return new NextResponse("API token not configured", { status: 500 });
}
try { try {
const response = await fetch(apiUrl, { const { baseUrl, token } = config();
headers: { const response = await fetch(`${baseUrl}/books/${bookId}/thumbnail`, {
Authorization: `Bearer ${token}`, headers: { Authorization: `Bearer ${token}` },
},
}); });
if (!response.ok) { if (!response.ok) {

View File

@@ -1,39 +1,13 @@
import { NextRequest, NextResponse } from "next/server"; import { NextRequest, NextResponse } from "next/server";
import { listFolders } from "@/lib/api";
export async function GET(request: NextRequest) { export async function GET(request: NextRequest) {
const apiBaseUrl = process.env.API_BASE_URL || "http://api:7080";
const apiToken = process.env.API_BOOTSTRAP_TOKEN;
if (!apiToken) {
return NextResponse.json({ error: "API token not configured" }, { status: 500 });
}
try { try {
const { searchParams } = new URL(request.url); const { searchParams } = new URL(request.url);
const path = searchParams.get("path"); const path = searchParams.get("path") || undefined;
const data = await listFolders(path);
let apiUrl = `${apiBaseUrl}/folders`;
if (path) {
apiUrl += `?path=${encodeURIComponent(path)}`;
}
const response = await fetch(apiUrl, {
headers: {
Authorization: `Bearer ${apiToken}`,
},
});
if (!response.ok) {
return NextResponse.json(
{ error: `API error: ${response.status}` },
{ status: response.status }
);
}
const data = await response.json();
return NextResponse.json(data); return NextResponse.json(data);
} catch (error) { } catch (error) {
console.error("Proxy error:", error);
return NextResponse.json({ error: "Failed to fetch folders" }, { status: 500 }); return NextResponse.json({ error: "Failed to fetch folders" }, { status: 500 });
} }
} }

View File

@@ -1,36 +1,15 @@
import { NextRequest, NextResponse } from "next/server"; import { NextRequest, NextResponse } from "next/server";
import { cancelJob } from "@/lib/api";
export async function POST( export async function POST(
request: NextRequest, _request: NextRequest,
{ params }: { params: Promise<{ id: string }> } { params }: { params: Promise<{ id: string }> }
) { ) {
const { id } = await params; const { id } = await params;
const apiBaseUrl = process.env.API_BASE_URL || "http://api:7080";
const apiToken = process.env.API_BOOTSTRAP_TOKEN;
if (!apiToken) {
return NextResponse.json({ error: "API token not configured" }, { status: 500 });
}
try { try {
const response = await fetch(`${apiBaseUrl}/index/cancel/${id}`, { const data = await cancelJob(id);
method: "POST",
headers: {
Authorization: `Bearer ${apiToken}`,
},
});
if (!response.ok) {
return NextResponse.json(
{ error: `API error: ${response.status}` },
{ status: response.status }
);
}
const data = await response.json();
return NextResponse.json(data); return NextResponse.json(data);
} catch (error) { } catch (error) {
console.error("Proxy error:", error);
return NextResponse.json({ error: "Failed to cancel job" }, { status: 500 }); return NextResponse.json({ error: "Failed to cancel job" }, { status: 500 });
} }
} }

View File

@@ -1,35 +1,15 @@
import { NextRequest, NextResponse } from "next/server"; import { NextRequest, NextResponse } from "next/server";
import { apiFetch, IndexJobDto } from "@/lib/api";
export async function GET( export async function GET(
request: NextRequest, _request: NextRequest,
{ params }: { params: Promise<{ id: string }> } { params }: { params: Promise<{ id: string }> }
) { ) {
const { id } = await params; const { id } = await params;
const apiBaseUrl = process.env.API_BASE_URL || "http://api:7080";
const apiToken = process.env.API_BOOTSTRAP_TOKEN;
if (!apiToken) {
return NextResponse.json({ error: "API token not configured" }, { status: 500 });
}
try { try {
const response = await fetch(`${apiBaseUrl}/index/jobs/${id}`, { const data = await apiFetch<IndexJobDto>(`/index/jobs/${id}`);
headers: {
Authorization: `Bearer ${apiToken}`,
},
});
if (!response.ok) {
return NextResponse.json(
{ error: `API error: ${response.status}` },
{ status: response.status }
);
}
const data = await response.json();
return NextResponse.json(data); return NextResponse.json(data);
} catch (error) { } catch (error) {
console.error("Proxy error:", error);
return NextResponse.json({ error: "Failed to fetch job" }, { status: 500 }); return NextResponse.json({ error: "Failed to fetch job" }, { status: 500 });
} }
} }

View File

@@ -1,19 +1,12 @@
import { NextRequest } from "next/server"; import { NextRequest } from "next/server";
import { config } from "@/lib/api";
export async function GET( export async function GET(
request: NextRequest, request: NextRequest,
{ params }: { params: Promise<{ id: string }> } { params }: { params: Promise<{ id: string }> }
) { ) {
const { id } = await params; const { id } = await params;
const apiBaseUrl = process.env.API_BASE_URL || "http://api:7080"; const { baseUrl, token } = config();
const apiToken = process.env.API_BOOTSTRAP_TOKEN;
if (!apiToken) {
return new Response(
`data: ${JSON.stringify({ error: "API token not configured" })}\n\n`,
{ status: 500, headers: { "Content-Type": "text/event-stream" } }
);
}
const stream = new ReadableStream({ const stream = new ReadableStream({
async start(controller) { async start(controller) {
@@ -27,10 +20,8 @@ export async function GET(
if (!isActive) return; if (!isActive) return;
try { try {
const response = await fetch(`${apiBaseUrl}/index/jobs/${id}`, { const response = await fetch(`${baseUrl}/index/jobs/${id}`, {
headers: { headers: { Authorization: `Bearer ${token}` },
Authorization: `Bearer ${apiToken}`,
},
}); });
if (response.ok && isActive) { if (response.ok && isActive) {

View File

@@ -0,0 +1,11 @@
import { NextResponse } from "next/server";
import { apiFetch, IndexJobDto } from "@/lib/api";
export async function GET() {
try {
const data = await apiFetch<IndexJobDto[]>("/index/jobs/active");
return NextResponse.json(data);
} catch (error) {
return NextResponse.json({ error: "Failed to fetch active jobs" }, { status: 500 });
}
}

View File

@@ -1,31 +0,0 @@
import { NextRequest, NextResponse } from "next/server";
export async function GET(request: NextRequest) {
const apiBaseUrl = process.env.API_BASE_URL || "http://api:7080";
const apiToken = process.env.API_BOOTSTRAP_TOKEN;
if (!apiToken) {
return NextResponse.json({ error: "API token not configured" }, { status: 500 });
}
try {
const response = await fetch(`${apiBaseUrl}/index/status`, {
headers: {
Authorization: `Bearer ${apiToken}`,
},
});
if (!response.ok) {
return NextResponse.json(
{ error: `API error: ${response.status}` },
{ status: response.status }
);
}
const data = await response.json();
return NextResponse.json(data);
} catch (error) {
console.error("Proxy error:", error);
return NextResponse.json({ error: "Failed to fetch jobs" }, { status: 500 });
}
}

View File

@@ -1,15 +1,8 @@
import { NextRequest } from "next/server"; import { NextRequest } from "next/server";
import { config } from "@/lib/api";
export async function GET(request: NextRequest) { export async function GET(request: NextRequest) {
const apiBaseUrl = process.env.API_BASE_URL || "http://api:7080"; const { baseUrl, token } = config();
const apiToken = process.env.API_BOOTSTRAP_TOKEN;
if (!apiToken) {
return new Response(
`data: ${JSON.stringify({ error: "API token not configured" })}\n\n`,
{ status: 500, headers: { "Content-Type": "text/event-stream" } }
);
}
const stream = new ReadableStream({ const stream = new ReadableStream({
async start(controller) { async start(controller) {
@@ -22,10 +15,8 @@ export async function GET(request: NextRequest) {
if (!isActive) return; if (!isActive) return;
try { try {
const response = await fetch(`${apiBaseUrl}/index/status`, { const response = await fetch(`${baseUrl}/index/status`, {
headers: { headers: { Authorization: `Bearer ${token}` },
Authorization: `Bearer ${apiToken}`,
},
}); });
if (response.ok && isActive) { if (response.ok && isActive) {

View File

@@ -1,29 +1,16 @@
import { NextRequest, NextResponse } from "next/server"; import { NextRequest, NextResponse } from "next/server";
import { apiFetch, updateSetting } from "@/lib/api";
export async function GET( export async function GET(
request: NextRequest, _request: NextRequest,
{ params }: { params: Promise<{ key: string }> } { params }: { params: Promise<{ key: string }> }
) { ) {
const { key } = await params;
try { try {
const { key } = await params; const data = await apiFetch<unknown>(`/settings/${key}`);
const baseUrl = process.env.API_BASE_URL || "http://api:7080";
const token = process.env.API_BOOTSTRAP_TOKEN;
const response = await fetch(`${baseUrl}/settings/${key}`, {
headers: {
Authorization: `Bearer ${token}`,
},
cache: "no-store"
});
if (!response.ok) {
return NextResponse.json({ error: "Failed to fetch setting" }, { status: response.status });
}
const data = await response.json();
return NextResponse.json(data); return NextResponse.json(data);
} catch (error) { } catch (error) {
return NextResponse.json({ error: "Internal server error" }, { status: 500 }); return NextResponse.json({ error: "Failed to fetch setting" }, { status: 500 });
} }
} }
@@ -31,29 +18,12 @@ export async function POST(
request: NextRequest, request: NextRequest,
{ params }: { params: Promise<{ key: string }> } { params }: { params: Promise<{ key: string }> }
) { ) {
const { key } = await params;
try { try {
const { key } = await params; const { value } = await request.json();
const baseUrl = process.env.API_BASE_URL || "http://api:7080"; const data = await updateSetting(key, value);
const token = process.env.API_BOOTSTRAP_TOKEN;
const body = await request.json();
const response = await fetch(`${baseUrl}/settings/${key}`, {
method: "POST",
headers: {
Authorization: `Bearer ${token}`,
"Content-Type": "application/json",
},
body: JSON.stringify(body),
cache: "no-store"
});
if (!response.ok) {
return NextResponse.json({ error: "Failed to update setting" }, { status: response.status });
}
const data = await response.json();
return NextResponse.json(data); return NextResponse.json(data);
} catch (error) { } catch (error) {
return NextResponse.json({ error: "Internal server error" }, { status: 500 }); return NextResponse.json({ error: "Failed to update setting" }, { status: 500 });
} }
} }

View File

@@ -1,25 +1,11 @@
import { NextRequest, NextResponse } from "next/server"; import { NextResponse } from "next/server";
import { clearCache } from "@/lib/api";
export async function POST(request: NextRequest) { export async function POST() {
try { try {
const baseUrl = process.env.API_BASE_URL || "http://api:7080"; const data = await clearCache();
const token = process.env.API_BOOTSTRAP_TOKEN;
const response = await fetch(`${baseUrl}/settings/cache/clear`, {
method: "POST",
headers: {
Authorization: `Bearer ${token}`,
},
cache: "no-store"
});
if (!response.ok) {
return NextResponse.json({ error: "Failed to clear cache" }, { status: response.status });
}
const data = await response.json();
return NextResponse.json(data); return NextResponse.json(data);
} catch (error) { } catch (error) {
return NextResponse.json({ error: "Internal server error" }, { status: 500 }); return NextResponse.json({ error: "Failed to clear cache" }, { status: 500 });
} }
} }

View File

@@ -1,24 +1,11 @@
import { NextRequest, NextResponse } from "next/server"; import { NextResponse } from "next/server";
import { getCacheStats } from "@/lib/api";
export async function GET(request: NextRequest) { export async function GET() {
try { try {
const baseUrl = process.env.API_BASE_URL || "http://api:7080"; const data = await getCacheStats();
const token = process.env.API_BOOTSTRAP_TOKEN;
const response = await fetch(`${baseUrl}/settings/cache/stats`, {
headers: {
Authorization: `Bearer ${token}`,
},
cache: "no-store"
});
if (!response.ok) {
return NextResponse.json({ error: "Failed to fetch cache stats" }, { status: response.status });
}
const data = await response.json();
return NextResponse.json(data); return NextResponse.json(data);
} catch (error) { } catch (error) {
return NextResponse.json({ error: "Internal server error" }, { status: 500 }); return NextResponse.json({ error: "Failed to fetch cache stats" }, { status: 500 });
} }
} }

View File

@@ -1,24 +0,0 @@
import { NextRequest, NextResponse } from "next/server";
export async function GET(request: NextRequest) {
try {
const baseUrl = process.env.API_BASE_URL || "http://api:7080";
const token = process.env.API_BOOTSTRAP_TOKEN;
const response = await fetch(`${baseUrl}/settings`, {
headers: {
Authorization: `Bearer ${token}`,
},
cache: "no-store"
});
if (!response.ok) {
return NextResponse.json({ error: "Failed to fetch settings" }, { status: response.status });
}
const data = await response.json();
return NextResponse.json(data);
} catch (error) {
return NextResponse.json({ error: "Internal server error" }, { status: 500 });
}
}

View File

@@ -89,7 +89,7 @@ export type SeriesDto = {
first_book_id: string; first_book_id: string;
}; };
function config() { export function config() {
const baseUrl = process.env.API_BASE_URL || "http://api:7080"; const baseUrl = process.env.API_BASE_URL || "http://api:7080";
const token = process.env.API_BOOTSTRAP_TOKEN; const token = process.env.API_BOOTSTRAP_TOKEN;
if (!token) { if (!token) {

View File

@@ -21,7 +21,10 @@
{ {
"name": "next" "name": "next"
} }
] ],
"paths": {
"@/*": ["./*"]
}
}, },
"include": [ "include": [
"next-env.d.ts", "next-env.d.ts",