Files
auto-trade/app/api/kis/domestic/indices/route.ts

65 lines
2.0 KiB
TypeScript
Raw Normal View History

2026-02-12 14:20:07 +09:00
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";
2026-02-26 09:05:17 +09:00
import {
createKisApiErrorResponse,
KIS_API_ERROR_CODE,
toKisApiErrorMessage,
} from "@/app/api/kis/_response";
2026-02-12 14:20:07 +09:00
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) {
2026-02-26 09:05:17 +09:00
return createKisApiErrorResponse({
status: 401,
code: KIS_API_ERROR_CODE.AUTH_REQUIRED,
message: "로그인이 필요합니다.",
});
}
2026-02-12 14:20:07 +09:00
const credentials = readKisCredentialsFromHeaders(request.headers);
if (!hasKisConfig(credentials)) {
2026-02-26 09:05:17 +09:00
return createKisApiErrorResponse({
status: 400,
code: KIS_API_ERROR_CODE.CREDENTIAL_REQUIRED,
message: "KIS API 키 설정이 필요합니다.",
});
2026-02-12 14:20:07 +09:00
}
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) {
2026-02-26 09:05:17 +09:00
return createKisApiErrorResponse({
status: 500,
code: KIS_API_ERROR_CODE.UPSTREAM_FAILURE,
message: toKisApiErrorMessage(error, "지수 조회 중 오류가 발생했습니다."),
});
2026-02-12 14:20:07 +09:00
}
}