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:
@@ -1,35 +1,15 @@
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import { apiFetch, IndexJobDto } from "@/lib/api";
|
||||
|
||||
export async function GET(
|
||||
request: NextRequest,
|
||||
_request: NextRequest,
|
||||
{ params }: { params: Promise<{ id: string }> }
|
||||
) {
|
||||
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 {
|
||||
const response = await fetch(`${apiBaseUrl}/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();
|
||||
const data = await apiFetch<IndexJobDto>(`/index/jobs/${id}`);
|
||||
return NextResponse.json(data);
|
||||
} catch (error) {
|
||||
console.error("Proxy error:", error);
|
||||
return NextResponse.json({ error: "Failed to fetch job" }, { status: 500 });
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user