261026_01_ENH: 사내 연동 API 추가 및 SurveyProgress 전환·문자열 필터 지원
This commit is contained in:
97
src/app/api/kric-internal/[[...slug]]/route.ts
Normal file
97
src/app/api/kric-internal/[[...slug]]/route.ts
Normal file
@@ -0,0 +1,97 @@
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import { errorToResponse } from "@kri/common/http";
|
||||
import { createServerApiWithApiKey } from "@/lib/http-server";
|
||||
import {
|
||||
internalTarget,
|
||||
readInternalQuotaRequest,
|
||||
readInternalReportBatch,
|
||||
} from "@/lib/kric-internal-policy";
|
||||
import { readExcelRequest } from "@/types/export";
|
||||
import type { RouteContext } from "@/types/common";
|
||||
|
||||
export const maxDuration = 660;
|
||||
const noStore = { "Cache-Control": "no-store" };
|
||||
const jsonError = (message: string, status: number) =>
|
||||
NextResponse.json({ message }, { status, headers: noStore });
|
||||
|
||||
async function handle(request: NextRequest, context: RouteContext) {
|
||||
const { slug = [] } = await context.params;
|
||||
const target = internalTarget(request.method, slug);
|
||||
if (!target) return jsonError("지원하지 않는 경로입니다.", 404);
|
||||
const apiKey = request.headers.get("x-api-key")?.trim();
|
||||
if (!apiKey) return jsonError("X-API-Key는 필수입니다.", 401);
|
||||
|
||||
let body: unknown;
|
||||
if (target.kind !== "list") {
|
||||
try {
|
||||
const input: unknown = await request.json();
|
||||
body =
|
||||
target.kind === "report"
|
||||
? readInternalReportBatch(input)
|
||||
: target.kind === "quota"
|
||||
? readInternalQuotaRequest(input)
|
||||
: readExcelRequest(input);
|
||||
} catch (error) {
|
||||
return jsonError(
|
||||
error instanceof SyntaxError
|
||||
? "요청 JSON을 확인해 주세요."
|
||||
: error instanceof Error
|
||||
? error.message
|
||||
: "요청 내용을 확인해 주세요.",
|
||||
400,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
const api = createServerApiWithApiKey(apiKey);
|
||||
const options = { signal: request.signal, cache: "no-store" as const };
|
||||
if (target.kind === "list" || target.kind === "quota") {
|
||||
const result =
|
||||
target.kind === "list"
|
||||
? await api.get<unknown>(target.path, options)
|
||||
: await api.post<unknown>(target.path, body, options);
|
||||
return NextResponse.json(result, { headers: noStore });
|
||||
}
|
||||
const upstream = await api.streamPost(target.path, body, {
|
||||
...options,
|
||||
timeoutMs: 600_000,
|
||||
idleTimeoutMs: 600_000,
|
||||
...(target.kind === "report"
|
||||
? { headers: { Accept: "application/x-ndjson" } }
|
||||
: {}),
|
||||
});
|
||||
const contentType =
|
||||
upstream.headers.get("content-type") ?? "application/octet-stream";
|
||||
const disposition = upstream.headers.get("content-disposition");
|
||||
if (
|
||||
!upstream.body ||
|
||||
(target.kind === "report"
|
||||
? !contentType.includes("application/x-ndjson")
|
||||
: !disposition || /json|text\/html/i.test(contentType))
|
||||
) {
|
||||
await upstream.body?.cancel();
|
||||
return jsonError("서버 응답 형식을 확인해 주세요.", 502);
|
||||
}
|
||||
return new Response(upstream.body, {
|
||||
headers: {
|
||||
...noStore,
|
||||
"Content-Type": contentType,
|
||||
...(target.kind === "report"
|
||||
? { "X-Accel-Buffering": "no" }
|
||||
: disposition
|
||||
? { "Content-Disposition": disposition }
|
||||
: {}),
|
||||
},
|
||||
});
|
||||
} catch (error: unknown) {
|
||||
const response = errorToResponse(error);
|
||||
response.headers.set("Cache-Control", "no-store");
|
||||
return response;
|
||||
}
|
||||
}
|
||||
|
||||
export const GET = handle;
|
||||
export const POST = handle;
|
||||
export const OPTIONS = () =>
|
||||
new NextResponse(null, { status: 204, headers: noStore });
|
||||
Reference in New Issue
Block a user