import type { KisRuntimeCredentials } from "@/features/settings/store/use-kis-runtime-store"; import type { DashboardActivityResponse, DashboardBalanceResponse, DashboardIndicesResponse, } from "@/features/dashboard/types/dashboard.types"; /** * @file features/dashboard/apis/dashboard.api.ts * @description 대시보드 잔고/지수 API 클라이언트 */ /** * 계좌 잔고/보유종목을 조회합니다. * @param credentials KIS 인증 정보 * @returns 잔고 응답 * @see app/api/kis/domestic/balance/route.ts 서버 라우트 */ export async function fetchDashboardBalance( credentials: KisRuntimeCredentials, ): Promise { const response = await fetch("/api/kis/domestic/balance", { method: "GET", headers: buildKisRequestHeaders(credentials), cache: "no-store", }); const payload = (await response.json()) as | DashboardBalanceResponse | { error?: string }; if (!response.ok) { throw new Error( "error" in payload ? payload.error : "잔고 조회 중 오류가 발생했습니다.", ); } return payload as DashboardBalanceResponse; } /** * 시장 지수(KOSPI/KOSDAQ)를 조회합니다. * @param credentials KIS 인증 정보 * @returns 지수 응답 * @see app/api/kis/domestic/indices/route.ts 서버 라우트 */ export async function fetchDashboardIndices( credentials: KisRuntimeCredentials, ): Promise { const response = await fetch("/api/kis/domestic/indices", { method: "GET", headers: buildKisRequestHeaders(credentials), cache: "no-store", }); const payload = (await response.json()) as | DashboardIndicesResponse | { error?: string }; if (!response.ok) { throw new Error( "error" in payload ? payload.error : "지수 조회 중 오류가 발생했습니다.", ); } return payload as DashboardIndicesResponse; } /** * 주문내역/매매일지(활동 데이터)를 조회합니다. * @param credentials KIS 인증 정보 * @returns 활동 데이터 응답 * @see app/api/kis/domestic/activity/route.ts 서버 라우트 */ export async function fetchDashboardActivity( credentials: KisRuntimeCredentials, ): Promise { const response = await fetch("/api/kis/domestic/activity", { method: "GET", headers: buildKisRequestHeaders(credentials), cache: "no-store", }); const payload = (await response.json()) as | DashboardActivityResponse | { error?: string }; if (!response.ok) { throw new Error( "error" in payload ? payload.error : "활동 데이터 조회 중 오류가 발생했습니다.", ); } return payload as DashboardActivityResponse; } /** * 대시보드 API 공통 헤더를 구성합니다. * @param credentials KIS 인증 정보 * @returns KIS 전달 헤더 * @see features/dashboard/apis/dashboard.api.ts fetchDashboardBalance/fetchDashboardIndices */ function buildKisRequestHeaders(credentials: KisRuntimeCredentials) { const headers: Record = { "x-kis-app-key": credentials.appKey, "x-kis-app-secret": credentials.appSecret, "x-kis-trading-env": credentials.tradingEnv, }; if (credentials.accountNo?.trim()) { headers["x-kis-account-no"] = credentials.accountNo.trim(); } return headers; }