Files
auto-trade/app/api/kis/validate/route.ts

66 lines
1.9 KiB
TypeScript
Raw Permalink Normal View History

2026-02-11 16:31:28 +09:00
import type { DashboardKisValidateResponse } from "@/features/trade/types/trade.types";
2026-02-11 15:27:03 +09:00
import { normalizeTradingEnv } from "@/lib/kis/config";
import {
parseKisCredentialRequest,
validateKisCredentialInput,
} from "@/lib/kis/request";
2026-02-06 17:50:35 +09:00
import { getKisAccessToken } from "@/lib/kis/token";
import { hasKisApiSession } from "@/app/api/kis/_session";
2026-02-26 09:05:17 +09:00
import {
createKisApiErrorResponse,
KIS_API_ERROR_CODE,
toKisApiErrorMessage,
} from "@/app/api/kis/_response";
2026-02-06 17:50:35 +09:00
import { NextRequest, NextResponse } from "next/server";
/**
* @file app/api/kis/validate/route.ts
2026-02-11 15:27:03 +09:00
* @description KIS API .
2026-02-06 17:50:35 +09:00
*/
/**
2026-02-11 15:27:03 +09:00
* @description API .
2026-02-11 16:31:28 +09:00
* @see features/settings/components/KisAuthForm.tsx
2026-02-06 17:50:35 +09:00
*/
export async function POST(request: NextRequest) {
2026-02-11 15:27:03 +09:00
const credentials = await parseKisCredentialRequest(request);
const tradingEnv = normalizeTradingEnv(credentials.tradingEnv);
2026-02-06 17:50:35 +09:00
const hasSession = await hasKisApiSession();
if (!hasSession) {
2026-02-26 09:05:17 +09:00
return createKisApiErrorResponse({
status: 401,
code: KIS_API_ERROR_CODE.AUTH_REQUIRED,
message: "로그인이 필요합니다.",
tradingEnv,
});
}
2026-02-11 15:27:03 +09:00
const invalidMessage = validateKisCredentialInput(credentials);
if (invalidMessage) {
2026-02-26 09:05:17 +09:00
return createKisApiErrorResponse({
status: 400,
code: KIS_API_ERROR_CODE.INVALID_REQUEST,
message: invalidMessage,
tradingEnv,
});
2026-02-06 17:50:35 +09:00
}
try {
await getKisAccessToken(credentials);
return NextResponse.json({
ok: true,
2026-02-11 15:27:03 +09:00
tradingEnv,
2026-02-06 17:50:35 +09:00
message: "API 키 검증이 완료되었습니다. (토큰 발급 성공)",
} satisfies DashboardKisValidateResponse);
} catch (error) {
2026-02-26 09:05:17 +09:00
return createKisApiErrorResponse({
status: 401,
code: KIS_API_ERROR_CODE.UNAUTHORIZED,
message: toKisApiErrorMessage(error, "API 키 검증 중 오류가 발생했습니다."),
tradingEnv,
});
2026-02-06 17:50:35 +09:00
}
}