-
Notifications
You must be signed in to change notification settings - Fork 0
코인 차트 기능 구현 #16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
코인 차트 기능 구현 #16
Changes from all commits
8b6298f
76a9ba6
5cad1db
1c1f98b
8c1a4b5
8f2a40d
6568ba8
2f26f99
791bd89
492f5bd
7c2e6e5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| import { UPBIT_URL } from "@/shared"; | ||
|
|
||
| import { CandleData, CandleParams, MinutesCandleParams } from "../types"; | ||
|
|
||
| export const candleAPI = async (params: CandleParams): Promise<CandleData[]> => { | ||
| const { market, to, count, type } = params; | ||
|
|
||
| // URL 파라미터 구성 | ||
| const urlParams = new URLSearchParams({ | ||
| market, | ||
| count: count.toString(), | ||
| }); | ||
|
|
||
| if (to) { | ||
| urlParams.append("to", to); | ||
| } | ||
|
|
||
| // 타입별 엔드포인트 구성 | ||
| let endpoint = `${UPBIT_URL}/candles/${type}`; | ||
|
|
||
| // 분 캔들의 경우 unit을 Path 파라미터로 추가 | ||
| if (type === "minutes") { | ||
| const unit = (params as MinutesCandleParams).unit; | ||
| endpoint = `${UPBIT_URL}/candles/minutes/${unit}`; | ||
| } | ||
|
|
||
| const url = `${endpoint}?${urlParams.toString()}`; | ||
|
|
||
| const response = await fetch(url); | ||
| if (!response.ok) { | ||
| throw new Error(`Candle API error: ${response.status} ${response.statusText}`); | ||
| } | ||
|
|
||
| const data: CandleData[] = await response.json(); | ||
| return data; | ||
| }; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| export * from "./useGetMarketOrder"; | ||
| export * from "./useMarketOrderRealtime"; | ||
| export * from "./useMarket"; | ||
| export * from "./useGetCandle"; |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,20 @@ | ||||||
| import { useQuery } from "@tanstack/react-query"; | ||||||
|
|
||||||
| import { candleAPI } from "../apis"; | ||||||
| import type { CandleParams, MinutesCandleParams } from "../types"; | ||||||
|
|
||||||
| export const useGetCandle = (params: CandleParams) => { | ||||||
| const { market, count, type } = params; | ||||||
|
|
||||||
| // 분 캔들일 때만 unit을 queryKey에 포함 | ||||||
| const unit = type === "minutes" ? (params as MinutesCandleParams).unit : undefined; | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
|
||||||
| const queryKey = unit ? ["candle", market, count, type, unit] : ["candle", market, count, type]; | ||||||
|
|
||||||
| return useQuery({ | ||||||
| queryKey, | ||||||
| queryFn: () => candleAPI(params), | ||||||
|
|
||||||
| refetchInterval: type === "seconds" || type === "minutes" ? 500 : false, | ||||||
| }); | ||||||
| }; | ||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| /** | ||
| * 캔들 타입 | ||
| * - seconds: 초 단위 (1초 고정) | ||
| * - minutes: 분 단위 (1, 3, 5, 10, 15, 30, 60, 240분) | ||
| * - days: 일 단위 | ||
| * - weeks: 주 단위 | ||
| * - months: 월 단위 | ||
| */ | ||
| export type CandleType = "seconds" | "minutes" | "days" | "weeks" | "months"; | ||
|
|
||
| /** | ||
| * 분 단위 캔들 옵션 | ||
| * Upbit API에서 지원하는 분 단위 | ||
| */ | ||
| export type MinuteUnit = 1 | 3 | 5 | 10 | 15 | 30 | 60 | 240; | ||
|
|
||
| type SecondsCandleParams = { | ||
| type: "seconds"; | ||
| } & BaseCandleParams; | ||
|
|
||
| export type MinutesCandleParams = { | ||
| type: "minutes"; | ||
| unit: MinuteUnit; | ||
| } & BaseCandleParams; | ||
|
|
||
| type OtherCandleParams = { | ||
| type: "days" | "weeks" | "months"; | ||
| } & BaseCandleParams; | ||
|
|
||
| type BaseCandleParams = { | ||
| market: string; | ||
| to?: string; | ||
| count: number; | ||
| }; | ||
|
|
||
| export type CandleParams = SecondsCandleParams | MinutesCandleParams | OtherCandleParams; |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,23 @@ | ||||||
| export type CandleData = { | ||||||
| market: string; | ||||||
| candle_date_time_utc: string; | ||||||
| candle_date_time_kst: string; | ||||||
| opening_price: number; | ||||||
| high_price: number; | ||||||
| low_price: number; | ||||||
| trade_price: number; | ||||||
| timestamp: number; | ||||||
| candle_acc_trade_price: number; | ||||||
| candle_acc_trade_volume: number; | ||||||
| unit: number; | ||||||
|
||||||
| unit: number; | |
| unit?: number; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,5 @@ | ||
| export * from "./chart.type"; | ||
| export * from "./market-info.type"; | ||
| export * from "./orderbook-units.type"; | ||
| export * from "./trade.type"; | ||
| export * from "./candle.type"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if (type === "minutes")조건문 덕분에 이 블록 안에서params의 타입이MinutesCandleParams로 추론됩니다. 따라서as MinutesCandleParams와 같은 타입 단언 없이params.unit으로 직접 접근할 수 있습니다. 타입 단언을 제거하면 코드가 더 간결해지고 타입 안정성도 높아집니다.