261027_02_ENH: kric-internal에 project-list 추가 #3
@@ -3,6 +3,7 @@ import { errorToResponse } from "@kri/common/http";
|
|||||||
import { createServerApiWithApiKey } from "@/lib/http-server";
|
import { createServerApiWithApiKey } from "@/lib/http-server";
|
||||||
import {
|
import {
|
||||||
internalTarget,
|
internalTarget,
|
||||||
|
readInternalProjectListQuery,
|
||||||
readInternalQuotaRequest,
|
readInternalQuotaRequest,
|
||||||
readInternalReportBatch,
|
readInternalReportBatch,
|
||||||
readInternalShareRequest,
|
readInternalShareRequest,
|
||||||
@@ -25,26 +26,33 @@ async function handle(request: NextRequest, context: RouteContext) {
|
|||||||
if (!apiKey) return jsonError("X-API-Key는 필수입니다.", 401);
|
if (!apiKey) return jsonError("X-API-Key는 필수입니다.", 401);
|
||||||
|
|
||||||
let body: unknown;
|
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 {
|
try {
|
||||||
const input: unknown = await request.json();
|
const input: unknown = await request.json();
|
||||||
body =
|
if (target.kind === "report") {
|
||||||
target.kind === "report"
|
body = readInternalReportBatch(input);
|
||||||
? readInternalReportBatch(input)
|
} else if (target.kind === "quota") {
|
||||||
: target.kind === "quota"
|
body = readInternalQuotaRequest(input);
|
||||||
? readInternalQuotaRequest(input)
|
} else if (target.kind === "share") {
|
||||||
: target.kind === "share"
|
body = readInternalShareRequest(input);
|
||||||
? readInternalShareRequest(input)
|
} else {
|
||||||
: readExcelRequest(input);
|
body = readExcelRequest(input);
|
||||||
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
return jsonError(
|
let message = "요청 내용을 확인해 주세요.";
|
||||||
error instanceof SyntaxError
|
if (error instanceof SyntaxError) {
|
||||||
? "요청 JSON을 확인해 주세요."
|
message = "요청 JSON을 확인해 주세요.";
|
||||||
: error instanceof Error
|
} else if (error instanceof Error) {
|
||||||
? error.message
|
message = error.message;
|
||||||
: "요청 내용을 확인해 주세요.",
|
}
|
||||||
400,
|
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 };
|
const options = { signal: request.signal, cache: "no-store" as const };
|
||||||
if (
|
if (
|
||||||
target.kind === "list" ||
|
target.kind === "list" ||
|
||||||
|
target.kind === "projects" ||
|
||||||
target.kind === "quota" ||
|
target.kind === "quota" ||
|
||||||
target.kind === "respondent" ||
|
target.kind === "respondent" ||
|
||||||
target.kind === "share"
|
target.kind === "share"
|
||||||
) {
|
) {
|
||||||
const result =
|
let result: unknown;
|
||||||
target.kind === "list"
|
if (target.kind === "list") {
|
||||||
? await api.get<unknown>(target.path, options)
|
result = await api.get<unknown>(target.path, options);
|
||||||
: await api.post<unknown>(target.path, body, 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 (
|
if (
|
||||||
target.kind === "share" &&
|
target.kind === "share" &&
|
||||||
(!isShareLink(result) ||
|
(!isShareLink(result) ||
|
||||||
@@ -93,17 +106,16 @@ async function handle(request: NextRequest, context: RouteContext) {
|
|||||||
await upstream.body?.cancel();
|
await upstream.body?.cancel();
|
||||||
return jsonError("서버 응답 형식을 확인해 주세요.", 502);
|
return jsonError("서버 응답 형식을 확인해 주세요.", 502);
|
||||||
}
|
}
|
||||||
return new Response(upstream.body, {
|
const headers: Record<string, string> = {
|
||||||
headers: {
|
|
||||||
...noStore,
|
...noStore,
|
||||||
"Content-Type": contentType,
|
"Content-Type": contentType,
|
||||||
...(target.kind === "report"
|
};
|
||||||
? { "X-Accel-Buffering": "no" }
|
if (target.kind === "report") {
|
||||||
: disposition
|
headers["X-Accel-Buffering"] = "no";
|
||||||
? { "Content-Disposition": disposition }
|
} else if (disposition) {
|
||||||
: {}),
|
headers["Content-Disposition"] = disposition;
|
||||||
},
|
}
|
||||||
});
|
return new Response(upstream.body, { headers });
|
||||||
} catch (error: unknown) {
|
} catch (error: unknown) {
|
||||||
const response = errorToResponse(error);
|
const response = errorToResponse(error);
|
||||||
response.headers.set("Cache-Control", "no-store");
|
response.headers.set("Cache-Control", "no-store");
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import {
|
|||||||
isQuotaResultPath,
|
isQuotaResultPath,
|
||||||
isReportStreamPath,
|
isReportStreamPath,
|
||||||
isProjectReadPath,
|
isProjectReadPath,
|
||||||
|
projectReadQuery,
|
||||||
} from "@/lib/project-proxy-policy";
|
} from "@/lib/project-proxy-policy";
|
||||||
import { isRecord, validDateRange } from "@/types/kart";
|
import { isRecord, validDateRange } from "@/types/kart";
|
||||||
import { readReportBatch } from "@/lib/report-contract";
|
import { readReportBatch } from "@/lib/report-contract";
|
||||||
@@ -12,9 +13,21 @@ import { reportUrl } from "@/types/share";
|
|||||||
|
|
||||||
type Target = {
|
type Target = {
|
||||||
path: string;
|
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. */
|
/** URL Copy only: the server resolves the project from the authenticated path. */
|
||||||
export function readInternalShareRequest(value: unknown) {
|
export function readInternalShareRequest(value: unknown) {
|
||||||
if (!isRecord(value) || value.mode !== "U")
|
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("/");
|
const path = "/" + slug.map(encodeURIComponent).join("/");
|
||||||
if (method === "GET" && area === "quota" && isQuotaListPath(parts))
|
if (method === "GET" && area === "quota" && isQuotaListPath(parts))
|
||||||
return { path, kind: "list" };
|
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 (method !== "POST") return null;
|
||||||
if (
|
if (
|
||||||
area === "project" &&
|
area === "project" &&
|
||||||
|
|||||||
Reference in New Issue
Block a user