Files
contact-front/src/app/api/message-template/[[...slug]]/route.ts

106 lines
3.1 KiB
TypeScript

import {NextRequest, NextResponse} from "next/server";
import {RouteContext} from "@/types/common";
import {createNotihubApi} from "@/lib/http-server";
import {errorToResponse} from "@kri/common/http";
import type { Template, TemplateSummary } from "@kri/common/noti-hub";
export const GET = async (req: NextRequest, context: RouteContext) => {
try{
const { searchParams } = new URL(req.url);
const {slug = []} = await context.params;
const [action] = slug;
const notihubApi = await createNotihubApi();
if(slug.length === 1){
const result = await notihubApi.get<Template>(
`/template/${slug[0]}`,
);
return NextResponse.json(result, { status: 200 });
}
if(action === "list" && slug.length === 3){
const result = await notihubApi.get<TemplateSummary[]>(
`/template/list/${slug[1]}/${slug[2]}`,
);
return NextResponse.json(result, { status: 200 });
}
return NextResponse.json({ message: "Not found" }, { status: 404 });
} catch (error: unknown) {
return errorToResponse(error);
}
}
export const PUT = async (req: NextRequest, context: RouteContext) => {
try{
const { searchParams } = new URL(req.url);
const {slug = []} = await context.params;
const [action] = slug;
const notihubApi = await createNotihubApi();
if(action === "body-image" && slug.length === 2){
const itrntPrjctCd = slug[1];
const formData = await req.formData();
const file = formData.get("file") as File | null;
if (!file) {
return NextResponse.json(
{ message: "file 필드가 없습니다." },
{ status: 400 }
);
}
const result = await notihubApi.putForm<string>(
`/template/body-image/${itrntPrjctCd}`,
formData
);
return NextResponse.json(result, { status: 200 });
}
if(!action){
const body = await req.json().catch(() => null);
if (!body) {
return NextResponse.json(
{ message: "요청 바디가 없습니다." },
{ status: 400 }
);
}
const result = await notihubApi.put<Template>(`/template`, body);
return NextResponse.json(result, { status: 200 });
}
return NextResponse.json({ message: "Not found" }, { status: 404 });
} catch (error: unknown) {
return errorToResponse(error);
}
}
export const PATCH = async (req: NextRequest, context: RouteContext) => {
try{
const { searchParams } = new URL(req.url);
const {slug = []} = await context.params;
const [action] = slug;
const notihubApi = await createNotihubApi();
if(!action){
const body = await req.json().catch(() => null);
if (!body) {
return NextResponse.json(
{ message: "요청 바디가 없습니다." },
{ status: 400 }
);
}
const result = await notihubApi.patch<Template>(`/template`, body);
return NextResponse.json(result, { status: 200 });
}
return NextResponse.json({ message: "Not found" }, { status: 404 });
} catch (error: unknown) {
return errorToResponse(error);
}
}