261021_01_ENH: 1차 마이그레이션 완료
This commit is contained in:
86
src/app/api/derived/[[...slug]]/route.ts
Normal file
86
src/app/api/derived/[[...slug]]/route.ts
Normal file
@@ -0,0 +1,86 @@
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import { cookies } from "next/headers";
|
||||
import { errorToResponse } from "@kri/common/http";
|
||||
import { createServerApi } from "@/lib/http-server";
|
||||
import {
|
||||
derivedUpstreamPath,
|
||||
isDerivedCreatePath,
|
||||
isDerivedItemPath,
|
||||
isDerivedListPath,
|
||||
} from "@/lib/project-proxy-policy";
|
||||
import { readDerivedSaveRequest } from "@/types/derived";
|
||||
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 });
|
||||
const noStore = { headers: { "Cache-Control": "no-store" } };
|
||||
const loggedIn = async () => !!(await cookies()).get(CookieKeys.AuthToken)?.value;
|
||||
|
||||
export async function GET(_request: NextRequest, context: RouteContext) {
|
||||
const { slug = [] } = await context.params;
|
||||
if (!isDerivedListPath(slug)) return notFound();
|
||||
if (!(await loggedIn())) return unauthorized();
|
||||
try {
|
||||
const serverApi = await createServerApi();
|
||||
const result = await serverApi.get<unknown>(derivedUpstreamPath(slug), {
|
||||
cache: "no-store",
|
||||
});
|
||||
return NextResponse.json(result, noStore);
|
||||
} catch (error: unknown) {
|
||||
return errorToResponse(error);
|
||||
}
|
||||
}
|
||||
|
||||
// Saving recalculates every respondent on the server, so allow a long request.
|
||||
export const maxDuration = 660;
|
||||
|
||||
/** Create (`{type}/{pkey}`) or update (`{type}/{pkey}/{variableId}`); both answer the id as text. */
|
||||
async function save(request: NextRequest, context: RouteContext, create: boolean) {
|
||||
const { slug = [] } = await context.params;
|
||||
if (!(create ? isDerivedCreatePath(slug) : isDerivedItemPath(slug))) return notFound();
|
||||
if (!(await loggedIn())) return unauthorized();
|
||||
let body;
|
||||
try {
|
||||
body = readDerivedSaveRequest(await request.json());
|
||||
} catch {
|
||||
return NextResponse.json(
|
||||
{ message: "파생변수 저장 내용을 확인해 주세요." },
|
||||
{ status: 400 },
|
||||
);
|
||||
}
|
||||
try {
|
||||
const serverApi = await createServerApi();
|
||||
const path = derivedUpstreamPath(slug);
|
||||
const options = { cache: "no-store" as const, expectText: true, timeoutMs: 600_000 };
|
||||
const result = create
|
||||
? await serverApi.post<unknown>(path, body, options)
|
||||
: await serverApi.put<unknown>(path, body, options);
|
||||
if (typeof result !== "string" || result === "")
|
||||
return NextResponse.json(
|
||||
{ message: "파생변수 저장 응답을 확인해 주세요." },
|
||||
{ status: 502 },
|
||||
);
|
||||
return NextResponse.json({ variableId: result }, noStore);
|
||||
} catch (error: unknown) {
|
||||
return errorToResponse(error);
|
||||
}
|
||||
}
|
||||
|
||||
export const POST = (request: NextRequest, context: RouteContext) => save(request, context, true);
|
||||
export const PUT = (request: NextRequest, context: RouteContext) => save(request, context, false);
|
||||
|
||||
export async function DELETE(_request: NextRequest, context: RouteContext) {
|
||||
const { slug = [] } = await context.params;
|
||||
if (!isDerivedItemPath(slug)) return notFound();
|
||||
if (!(await loggedIn())) return unauthorized();
|
||||
try {
|
||||
const serverApi = await createServerApi();
|
||||
const result = await serverApi.del<unknown>(derivedUpstreamPath(slug), { cache: "no-store" });
|
||||
return NextResponse.json(result === true, noStore);
|
||||
} catch (error: unknown) {
|
||||
return errorToResponse(error);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user