import type { DashboardKisValidateResponse } from "@/features/trade/types/trade.types"; import { normalizeTradingEnv } from "@/lib/kis/config"; import { parseKisCredentialRequest, validateKisCredentialInput, } from "@/lib/kis/request"; import { getKisAccessToken } from "@/lib/kis/token"; import { hasKisApiSession } from "@/app/api/kis/_session"; import { createKisApiErrorResponse, KIS_API_ERROR_CODE, toKisApiErrorMessage, } from "@/app/api/kis/_response"; import { NextRequest, NextResponse } from "next/server"; /** * @file app/api/kis/validate/route.ts * @description 사용자 입력 KIS API 키를 검증합니다. */ /** * @description 액세스 토큰 발급 성공 여부로 API 키를 검증합니다. * @see features/settings/components/KisAuthForm.tsx */ export async function POST(request: NextRequest) { const credentials = await parseKisCredentialRequest(request); const tradingEnv = normalizeTradingEnv(credentials.tradingEnv); const hasSession = await hasKisApiSession(); if (!hasSession) { return createKisApiErrorResponse({ status: 401, code: KIS_API_ERROR_CODE.AUTH_REQUIRED, message: "로그인이 필요합니다.", tradingEnv, }); } const invalidMessage = validateKisCredentialInput(credentials); if (invalidMessage) { return createKisApiErrorResponse({ status: 400, code: KIS_API_ERROR_CODE.INVALID_REQUEST, message: invalidMessage, tradingEnv, }); } try { await getKisAccessToken(credentials); return NextResponse.json({ ok: true, tradingEnv, message: "API 키 검증이 완료되었습니다. (토큰 발급 성공)", } satisfies DashboardKisValidateResponse); } catch (error) { return createKisApiErrorResponse({ status: 401, code: KIS_API_ERROR_CODE.UNAUTHORIZED, message: toKisApiErrorMessage(error, "API 키 검증 중 오류가 발생했습니다."), tradingEnv, }); } }