import { NextResponse } from "next/server"; import type { DashboardIndicesResponse } from "@/features/dashboard/types/dashboard.types"; import { hasKisConfig, normalizeTradingEnv } from "@/lib/kis/config"; import { getDomesticDashboardIndices } from "@/lib/kis/dashboard"; import { hasKisApiSession } from "@/app/api/kis/_session"; import { readKisCredentialsFromHeaders } from "@/app/api/kis/domestic/_shared"; /** * @file app/api/kis/domestic/indices/route.ts * @description 국내 주요 지수(KOSPI/KOSDAQ) 조회 API */ /** * 대시보드 지수 조회 API * @returns 코스피/코스닥 지수 목록 * @see features/dashboard/hooks/use-dashboard-data.ts 대시보드 초기 로드/주기 갱신에서 호출합니다. */ export async function GET(request: Request) { const hasSession = await hasKisApiSession(); if (!hasSession) { return NextResponse.json({ error: "로그인이 필요합니다." }, { status: 401 }); } const credentials = readKisCredentialsFromHeaders(request.headers); if (!hasKisConfig(credentials)) { return NextResponse.json( { error: "KIS API 키 설정이 필요합니다.", }, { status: 400 }, ); } try { const items = await getDomesticDashboardIndices(credentials); const response: DashboardIndicesResponse = { source: "kis", tradingEnv: normalizeTradingEnv(credentials.tradingEnv), items, fetchedAt: new Date().toISOString(), }; return NextResponse.json(response, { headers: { "cache-control": "no-store", }, }); } catch (error) { const message = error instanceof Error ? error.message : "지수 조회 중 오류가 발생했습니다."; return NextResponse.json({ error: message }, { status: 500 }); } }