261021_01_ENH: 1차 마이그레이션 완료
This commit is contained in:
52
src/app/api/quota/[[...slug]]/route.ts
Normal file
52
src/app/api/quota/[[...slug]]/route.ts
Normal file
@@ -0,0 +1,52 @@
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import { cookies } from "next/headers";
|
||||
import { errorToResponse } from "@kri/common/http";
|
||||
import { createServerApi } from "@/lib/http-server";
|
||||
import { isQuotaListPath, isQuotaResultPath } from "@/lib/project-proxy-policy";
|
||||
import { readQuotaRequest } from "@/types/quota";
|
||||
import { CookieKeys } from "@/store/cookie/cookie-keys";
|
||||
import type { RouteContext } from "@/types/common";
|
||||
|
||||
const notFound = () =>
|
||||
NextResponse.json({ message: "지원하지 않는 경로입니다." }, { status: 404 });
|
||||
const unauthorized = () =>
|
||||
NextResponse.json({ message: "로그인이 필요합니다." }, { status: 401 });
|
||||
|
||||
export async function GET(_request: NextRequest, context: RouteContext) {
|
||||
const { slug = [] } = await context.params;
|
||||
if (!isQuotaListPath(slug)) return notFound();
|
||||
if (!(await cookies()).get(CookieKeys.AuthToken)?.value) return unauthorized();
|
||||
try {
|
||||
const serverApi = await createServerApi();
|
||||
const result = await serverApi.get<unknown>(
|
||||
"/quota/" + slug.map(encodeURIComponent).join("/"),
|
||||
{ cache: "no-store" },
|
||||
);
|
||||
return NextResponse.json(result, { headers: { "Cache-Control": "no-store" } });
|
||||
} catch (error: unknown) {
|
||||
return errorToResponse(error);
|
||||
}
|
||||
}
|
||||
|
||||
export async function POST(request: NextRequest, context: RouteContext) {
|
||||
const { slug = [] } = await context.params;
|
||||
if (!isQuotaResultPath(slug)) return notFound();
|
||||
if (!(await cookies()).get(CookieKeys.AuthToken)?.value) return unauthorized();
|
||||
let body;
|
||||
try {
|
||||
body = readQuotaRequest(await request.json());
|
||||
} catch {
|
||||
return NextResponse.json({ message: "쿼터 조회 조건을 확인해 주세요." }, { status: 400 });
|
||||
}
|
||||
try {
|
||||
const serverApi = await createServerApi();
|
||||
const result = await serverApi.post<unknown>(
|
||||
"/quota/" + slug.map(encodeURIComponent).join("/"),
|
||||
body,
|
||||
{ cache: "no-store" },
|
||||
);
|
||||
return NextResponse.json(result, { headers: { "Cache-Control": "no-store" } });
|
||||
} catch (error: unknown) {
|
||||
return errorToResponse(error);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user