Compare commits

..

2 Commits

2 changed files with 60 additions and 33 deletions

View File

@@ -3,6 +3,7 @@ import { errorToResponse } from "@kri/common/http";
import { createServerApiWithApiKey } from "@/lib/http-server";
import {
internalTarget,
readInternalProjectListQuery,
readInternalQuotaRequest,
readInternalReportBatch,
readInternalShareRequest,
@@ -25,26 +26,33 @@ async function handle(request: NextRequest, context: RouteContext) {
if (!apiKey) return jsonError("X-API-Key는 필수입니다.", 401);
let body: unknown;
if (target.kind !== "list" && target.kind !== "respondent") {
let query: Record<string, string> = {};
if (target.kind === "projects") {
try {
query = readInternalProjectListQuery(request.nextUrl.searchParams);
} catch (error) {
return jsonError(error instanceof Error ? error.message : "조회 조건을 확인해 주세요.", 400);
}
} else if (target.kind !== "list" && target.kind !== "respondent") {
try {
const input: unknown = await request.json();
body =
target.kind === "report"
? readInternalReportBatch(input)
: target.kind === "quota"
? readInternalQuotaRequest(input)
: target.kind === "share"
? readInternalShareRequest(input)
: readExcelRequest(input);
if (target.kind === "report") {
body = readInternalReportBatch(input);
} else if (target.kind === "quota") {
body = readInternalQuotaRequest(input);
} else if (target.kind === "share") {
body = readInternalShareRequest(input);
} else {
body = readExcelRequest(input);
}
} catch (error) {
return jsonError(
error instanceof SyntaxError
? "요청 JSON을 확인해 주세요."
: error instanceof Error
? error.message
: "요청 내용을 확인해 주세요.",
400,
);
let message = "요청 내용을 확인해 주세요.";
if (error instanceof SyntaxError) {
message = "요청 JSON을 확인해 주세요.";
} else if (error instanceof Error) {
message = error.message;
}
return jsonError(message, 400);
}
}
@@ -53,14 +61,19 @@ async function handle(request: NextRequest, context: RouteContext) {
const options = { signal: request.signal, cache: "no-store" as const };
if (
target.kind === "list" ||
target.kind === "projects" ||
target.kind === "quota" ||
target.kind === "respondent" ||
target.kind === "share"
) {
const result =
target.kind === "list"
? await api.get<unknown>(target.path, options)
: await api.post<unknown>(target.path, body, options);
let result: unknown;
if (target.kind === "list") {
result = await api.get<unknown>(target.path, options);
} else if (target.kind === "projects") {
result = await api.get<unknown>(target.path, { ...options, query });
} else {
result = await api.post<unknown>(target.path, body, options);
}
if (
target.kind === "share" &&
(!isShareLink(result) ||
@@ -93,17 +106,16 @@ async function handle(request: NextRequest, context: RouteContext) {
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 }
: {}),
},
});
const headers: Record<string, string> = {
...noStore,
"Content-Type": contentType,
};
if (target.kind === "report") {
headers["X-Accel-Buffering"] = "no";
} else if (disposition) {
headers["Content-Disposition"] = disposition;
}
return new Response(upstream.body, { headers });
} catch (error: unknown) {
const response = errorToResponse(error);
response.headers.set("Cache-Control", "no-store");

View File

@@ -4,6 +4,7 @@ import {
isQuotaResultPath,
isReportStreamPath,
isProjectReadPath,
projectReadQuery,
} from "@/lib/project-proxy-policy";
import { isRecord, validDateRange } from "@/types/kart";
import { readReportBatch } from "@/lib/report-contract";
@@ -12,9 +13,21 @@ import { reportUrl } from "@/types/share";
type Target = {
path: string;
kind: "list" | "quota" | "report" | "excel" | "respondent" | "share";
kind: "list" | "projects" | "quota" | "report" | "excel" | "respondent" | "share";
};
/** Linked lookup only: without all three keys the server would list the API key owner's projects. */
export function readInternalProjectListQuery(search: URLSearchParams) {
const query = projectReadQuery(["list"], search);
if (
!["0", "1"].includes(query.projectType ?? "") ||
!query.intranetCode?.trim() ||
!query.userTeam?.trim()
)
throw new Error("projectType(0|1), intranetCode, userTeam을 함께 입력해 주세요.");
return query;
}
/** URL Copy only: the server resolves the project from the authenticated path. */
export function readInternalShareRequest(value: unknown) {
if (!isRecord(value) || value.mode !== "U")
@@ -39,6 +52,8 @@ export function internalTarget(method: string, slug: string[]): Target | null {
const path = "/" + slug.map(encodeURIComponent).join("/");
if (method === "GET" && area === "quota" && isQuotaListPath(parts))
return { path, kind: "list" };
if (method === "GET" && area === "project" && parts.length === 1 && parts[0] === "list")
return { path, kind: "projects" };
if (method !== "POST") return null;
if (
area === "project" &&