Files
duocthu/apps/web/app/api/section-text/route.ts
T

42 lines
1.3 KiB
TypeScript

import { NextResponse } from "next/server";
export const runtime = "nodejs";
const API_GATEWAY_URL =
process.env.API_GATEWAY_URL ?? process.env.AI_SERVICE_URL ?? "http://localhost:8000";
function ragBaseUrl() {
return API_GATEWAY_URL.replace(/\/v1\/rag(?:\/query)?\/?$/, "");
}
export async function GET(request: Request) {
const params = new URL(request.url).searchParams;
const drugId = params.get("drug_id")?.trim() ?? "";
const sectionKey = params.get("section_key")?.trim() ?? "";
if (
!/^[a-z0-9_]{1,160}$/i.test(drugId) ||
!/^[a-z0-9_]{1,80}$/i.test(sectionKey)
) {
return NextResponse.json({ error: "invalid_section_request" }, { status: 400 });
}
try {
const upstream = await fetch(
`${ragBaseUrl()}/v1/rag/section-text?drug_id=${encodeURIComponent(
drugId
)}&section_key=${encodeURIComponent(sectionKey)}`,
{
headers: { "X-Client-Version": "1.0.0" },
cache: "no-store",
signal: AbortSignal.timeout(12_000),
}
);
if (!upstream.ok) {
return NextResponse.json({ error: "section_text_unavailable" }, { status: upstream.status });
}
return NextResponse.json(await upstream.json());
} catch {
return NextResponse.json({ error: "section_text_unavailable" }, { status: 502 });
}
}