대시보드
This commit is contained in:
65
app/api/kis/validate/route.ts
Normal file
65
app/api/kis/validate/route.ts
Normal file
@@ -0,0 +1,65 @@
|
||||
import type { DashboardKisValidateResponse } from "@/features/dashboard/types/dashboard.types";
|
||||
import type { KisCredentialInput } from "@/lib/kis/config";
|
||||
import { hasKisConfig, normalizeTradingEnv } from "@/lib/kis/config";
|
||||
import { getKisAccessToken } from "@/lib/kis/token";
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
|
||||
/**
|
||||
* @file app/api/kis/validate/route.ts
|
||||
* @description 사용자 입력 KIS API 키 검증 라우트
|
||||
*/
|
||||
|
||||
/**
|
||||
* KIS API 키 검증
|
||||
* @param request appKey/appSecret/tradingEnv JSON 본문
|
||||
* @returns 검증 성공/실패 정보
|
||||
* @see features/dashboard/components/dashboard-main.tsx handleValidateKis - 검증 버튼 클릭 시 호출
|
||||
*/
|
||||
export async function POST(request: NextRequest) {
|
||||
const body = (await request.json()) as Partial<DashboardKisValidateRequest>;
|
||||
|
||||
const credentials: KisCredentialInput = {
|
||||
appKey: body.appKey?.trim(),
|
||||
appSecret: body.appSecret?.trim(),
|
||||
tradingEnv: normalizeTradingEnv(body.tradingEnv),
|
||||
};
|
||||
|
||||
if (!hasKisConfig(credentials)) {
|
||||
return NextResponse.json(
|
||||
{
|
||||
ok: false,
|
||||
tradingEnv: normalizeTradingEnv(credentials.tradingEnv),
|
||||
message: "앱 키와 앱 시크릿을 모두 입력해 주세요.",
|
||||
} satisfies DashboardKisValidateResponse,
|
||||
{ status: 400 },
|
||||
);
|
||||
}
|
||||
|
||||
try {
|
||||
// 검증 단계는 토큰 발급 성공 여부만 확인합니다.
|
||||
await getKisAccessToken(credentials);
|
||||
|
||||
return NextResponse.json({
|
||||
ok: true,
|
||||
tradingEnv: normalizeTradingEnv(credentials.tradingEnv),
|
||||
message: "API 키 검증이 완료되었습니다. (토큰 발급 성공)",
|
||||
} satisfies DashboardKisValidateResponse);
|
||||
} catch (error) {
|
||||
const message = error instanceof Error ? error.message : "API 키 검증 중 오류가 발생했습니다.";
|
||||
|
||||
return NextResponse.json(
|
||||
{
|
||||
ok: false,
|
||||
tradingEnv: normalizeTradingEnv(credentials.tradingEnv),
|
||||
message,
|
||||
} satisfies DashboardKisValidateResponse,
|
||||
{ status: 401 },
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
interface DashboardKisValidateRequest {
|
||||
appKey: string;
|
||||
appSecret: string;
|
||||
tradingEnv: "real" | "mock";
|
||||
}
|
||||
Reference in New Issue
Block a user