260805_01_ENH: 이메일 템플릿 저장 시 빈 칸 유지, 외부 설문에서 리스트 단건 조회, 리스트 헤더조회, 리스트 단건 수정 기능 사용할수 있도록 api 라우트 생성 및 cors 완화
This commit is contained in:
115
src/app/api/list-external/[[...slug]]/route.ts
Normal file
115
src/app/api/list-external/[[...slug]]/route.ts
Normal file
@@ -0,0 +1,115 @@
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import { RouteContext } from "@/types/common";
|
||||
import { createContactApiWithApiKey } from "@/lib/http-server";
|
||||
import { errorToResponse } from "@kri/common/http";
|
||||
import { ListSearchExternal } from "@/types/list";
|
||||
|
||||
const getApiKey = (req: NextRequest) => req.headers.get("x-api-key");
|
||||
|
||||
// backend가 제공하는 메소드/경로 그대로 사용 (path variable)
|
||||
export const GET = async (req: NextRequest, context: RouteContext) => {
|
||||
try {
|
||||
const apiKey = getApiKey(req);
|
||||
if (!apiKey) {
|
||||
return NextResponse.json({ message: "X-API-Key는 필수입니다." }, { status: 401 });
|
||||
}
|
||||
|
||||
const { slug = [] } = await context.params;
|
||||
const [action, prjctIdx, sid] = slug;
|
||||
const contactApi = await createContactApiWithApiKey(apiKey);
|
||||
|
||||
if (action === "header" && prjctIdx) {
|
||||
const result = await contactApi.get<string[]>(`/list-external/header/${prjctIdx}`);
|
||||
return NextResponse.json(result, { status: 200 });
|
||||
}
|
||||
|
||||
if (action === "search" && prjctIdx && sid) {
|
||||
const result = await contactApi.get<ListSearchExternal>(
|
||||
`/list-external/search/${prjctIdx}/${encodeURIComponent(sid)}`,
|
||||
);
|
||||
return NextResponse.json(result, { status: 200 });
|
||||
}
|
||||
|
||||
return NextResponse.json({ message: "Not found" }, { status: 404 });
|
||||
} catch (error: unknown) {
|
||||
return errorToResponse(error);
|
||||
}
|
||||
};
|
||||
|
||||
// backend가 제공하는 메소드/경로 그대로 사용 (path variable)
|
||||
export const PATCH = async (req: NextRequest, context: RouteContext) => {
|
||||
try {
|
||||
const apiKey = getApiKey(req);
|
||||
if (!apiKey) {
|
||||
return NextResponse.json({ message: "X-API-Key는 필수입니다." }, { status: 401 });
|
||||
}
|
||||
|
||||
const { slug = [] } = await context.params;
|
||||
const [action, prjctIdx, sid] = slug;
|
||||
|
||||
if (action === "update" && prjctIdx && sid) {
|
||||
const body = await req.json().catch(() => null);
|
||||
const contactApi = await createContactApiWithApiKey(apiKey);
|
||||
const result = await contactApi.patch<number>(
|
||||
`/list-external/update/${prjctIdx}/${encodeURIComponent(sid)}`,
|
||||
body,
|
||||
);
|
||||
return NextResponse.json(result, { status: 200 });
|
||||
}
|
||||
|
||||
return NextResponse.json({ message: "Not found" }, { status: 404 });
|
||||
} catch (error: unknown) {
|
||||
return errorToResponse(error);
|
||||
}
|
||||
};
|
||||
|
||||
// 외부 연동 호환용: 항상 POST, path variable을 모두 url 쿼리 파람으로 받아 backend path variable로 전달
|
||||
export const POST = async (req: NextRequest, context: RouteContext) => {
|
||||
try {
|
||||
const apiKey = getApiKey(req);
|
||||
if (!apiKey) {
|
||||
return NextResponse.json({ message: "X-API-Key는 필수입니다." }, { status: 401 });
|
||||
}
|
||||
|
||||
const { slug = [] } = await context.params;
|
||||
const [action] = slug;
|
||||
const { searchParams } = req.nextUrl;
|
||||
const prjctIdx = searchParams.get("prjctIdx");
|
||||
const sid = searchParams.get("sid");
|
||||
const contactApi = await createContactApiWithApiKey(apiKey);
|
||||
|
||||
if (action === "header") {
|
||||
if (!prjctIdx) {
|
||||
return NextResponse.json({ message: "prjctIdx는 필수입니다." }, { status: 400 });
|
||||
}
|
||||
const result = await contactApi.get<string[]>(`/list-external/header/${prjctIdx}`);
|
||||
return NextResponse.json(result, { status: 200 });
|
||||
}
|
||||
|
||||
if (action === "search") {
|
||||
if (!prjctIdx || !sid) {
|
||||
return NextResponse.json({ message: "prjctIdx, sid는 필수입니다." }, { status: 400 });
|
||||
}
|
||||
const result = await contactApi.get<ListSearchExternal>(
|
||||
`/list-external/search/${prjctIdx}/${encodeURIComponent(sid)}`,
|
||||
);
|
||||
return NextResponse.json(result, { status: 200 });
|
||||
}
|
||||
|
||||
if (action === "update") {
|
||||
if (!prjctIdx || !sid) {
|
||||
return NextResponse.json({ message: "prjctIdx, sid는 필수입니다." }, { status: 400 });
|
||||
}
|
||||
const body = await req.json().catch(() => null);
|
||||
const result = await contactApi.patch<number>(
|
||||
`/list-external/update/${prjctIdx}/${encodeURIComponent(sid)}`,
|
||||
body,
|
||||
);
|
||||
return NextResponse.json(result, { status: 200 });
|
||||
}
|
||||
|
||||
return NextResponse.json({ message: "Not found" }, { status: 404 });
|
||||
} catch (error: unknown) {
|
||||
return errorToResponse(error);
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user