임시커밋
This commit is contained in:
82
features/settings/apis/kis-auth.api.ts
Normal file
82
features/settings/apis/kis-auth.api.ts
Normal file
@@ -0,0 +1,82 @@
|
||||
import type { KisRuntimeCredentials } from "@/features/settings/store/use-kis-runtime-store";
|
||||
import type {
|
||||
DashboardKisRevokeResponse,
|
||||
DashboardKisValidateResponse,
|
||||
DashboardKisWsApprovalResponse,
|
||||
} from "@/features/trade/types/trade.types";
|
||||
|
||||
interface KisApiBaseResponse {
|
||||
ok: boolean;
|
||||
message: string;
|
||||
}
|
||||
|
||||
async function postKisAuthApi<T extends KisApiBaseResponse>(
|
||||
endpoint: string,
|
||||
credentials: KisRuntimeCredentials,
|
||||
fallbackErrorMessage: string,
|
||||
): Promise<T> {
|
||||
const response = await fetch(endpoint, {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"content-type": "application/json",
|
||||
},
|
||||
body: JSON.stringify(credentials),
|
||||
cache: "no-store",
|
||||
});
|
||||
|
||||
const payload = (await response.json()) as T;
|
||||
|
||||
if (!response.ok || !payload.ok) {
|
||||
throw new Error(payload.message || fallbackErrorMessage);
|
||||
}
|
||||
|
||||
return payload;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description KIS API 키를 검증합니다.
|
||||
* @see app/api/kis/validate/route.ts
|
||||
*/
|
||||
export async function validateKisCredentials(
|
||||
credentials: KisRuntimeCredentials,
|
||||
): Promise<DashboardKisValidateResponse> {
|
||||
return postKisAuthApi<DashboardKisValidateResponse>(
|
||||
"/api/kis/validate",
|
||||
credentials,
|
||||
"API 키 검증에 실패했습니다.",
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @description KIS 액세스 토큰을 폐기합니다.
|
||||
* @see app/api/kis/revoke/route.ts
|
||||
*/
|
||||
export async function revokeKisCredentials(
|
||||
credentials: KisRuntimeCredentials,
|
||||
): Promise<DashboardKisRevokeResponse> {
|
||||
return postKisAuthApi<DashboardKisRevokeResponse>(
|
||||
"/api/kis/revoke",
|
||||
credentials,
|
||||
"API 토큰 폐기에 실패했습니다.",
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 웹소켓 승인키와 WS URL을 조회합니다.
|
||||
* @see app/api/kis/ws/approval/route.ts
|
||||
*/
|
||||
export async function fetchKisWebSocketApproval(
|
||||
credentials: KisRuntimeCredentials,
|
||||
): Promise<DashboardKisWsApprovalResponse> {
|
||||
const payload = await postKisAuthApi<DashboardKisWsApprovalResponse>(
|
||||
"/api/kis/ws/approval",
|
||||
credentials,
|
||||
"웹소켓 승인키 발급에 실패했습니다.",
|
||||
);
|
||||
|
||||
if (!payload.approvalKey || !payload.wsUrl) {
|
||||
throw new Error(payload.message || "웹소켓 연결 정보가 누락되었습니다.");
|
||||
}
|
||||
|
||||
return payload;
|
||||
}
|
||||
Reference in New Issue
Block a user