260908_01_fst: First Commit
This commit is contained in:
73
src/app/api/example/[[...slug]]/route.ts
Normal file
73
src/app/api/example/[[...slug]]/route.ts
Normal file
@@ -0,0 +1,73 @@
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import { errorToResponse } from "@kri/common/http";
|
||||
import { createServerApi } from "@/lib/http-server";
|
||||
import type { RouteContext } from "@/types/common";
|
||||
|
||||
const apiPath = (slug: string[]) => `/example${slug.length ? `/${slug.map(encodeURIComponent).join("/")}` : ""}`;
|
||||
const toResponse = (result: unknown) => result === undefined ? new NextResponse(null, { status: 204 }) : NextResponse.json(result);
|
||||
|
||||
export async function GET(request: NextRequest, context: RouteContext) {
|
||||
try {
|
||||
const { slug = [] } = await context.params;
|
||||
const query = Object.fromEntries(request.nextUrl.searchParams.entries());
|
||||
const serverApi = await createServerApi();
|
||||
return toResponse(await serverApi.get<unknown>(apiPath(slug), { query }));
|
||||
} catch (error: unknown) {
|
||||
return errorToResponse(error);
|
||||
}
|
||||
}
|
||||
|
||||
export async function POST(request: NextRequest, context: RouteContext) {
|
||||
try {
|
||||
const { slug = [] } = await context.params;
|
||||
const serverApi = await createServerApi();
|
||||
const contentType = request.headers.get("content-type") ?? "";
|
||||
if (contentType.includes("multipart/form-data")) {
|
||||
return toResponse(await serverApi.postForm<unknown>(apiPath(slug), await request.formData()));
|
||||
}
|
||||
const body = await request.json().catch(() => undefined);
|
||||
return toResponse(await serverApi.post<unknown>(apiPath(slug), body));
|
||||
} catch (error: unknown) {
|
||||
return errorToResponse(error);
|
||||
}
|
||||
}
|
||||
|
||||
export async function PUT(request: NextRequest, context: RouteContext) {
|
||||
try {
|
||||
const { slug = [] } = await context.params;
|
||||
const serverApi = await createServerApi();
|
||||
const contentType = request.headers.get("content-type") ?? "";
|
||||
if (contentType.includes("multipart/form-data")) {
|
||||
return toResponse(await serverApi.putForm<unknown>(apiPath(slug), await request.formData()));
|
||||
}
|
||||
const body = await request.json().catch(() => undefined);
|
||||
return toResponse(await serverApi.put<unknown>(apiPath(slug), body));
|
||||
} catch (error: unknown) {
|
||||
return errorToResponse(error);
|
||||
}
|
||||
}
|
||||
|
||||
export async function PATCH(request: NextRequest, context: RouteContext) {
|
||||
try {
|
||||
const { slug = [] } = await context.params;
|
||||
const serverApi = await createServerApi();
|
||||
const body = await request.json().catch(() => undefined);
|
||||
return toResponse(await serverApi.patch<unknown>(apiPath(slug), body));
|
||||
} catch (error: unknown) {
|
||||
return errorToResponse(error);
|
||||
}
|
||||
}
|
||||
|
||||
export async function DELETE(request: NextRequest, context: RouteContext) {
|
||||
try {
|
||||
const { slug = [] } = await context.params;
|
||||
const serverApi = await createServerApi();
|
||||
const body = await request.json().catch(() => undefined);
|
||||
return toResponse(await serverApi.del<unknown>(apiPath(slug), body === undefined ? undefined : { json: body }));
|
||||
} catch (error: unknown) {
|
||||
return errorToResponse(error);
|
||||
}
|
||||
}
|
||||
|
||||
// createHttpClient의 FormData 전용 메서드는 postForm과 putForm입니다.
|
||||
// PATCH/DELETE multipart가 필요하면 공용 HTTP Client 정책을 확인한 뒤 별도 구현하세요.
|
||||
Reference in New Issue
Block a user