53 lines
2.0 KiB
TypeScript
53 lines
2.0 KiB
TypeScript
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);
|
|
}
|
|
}
|