@@ -13,45 +13,12 @@ import type {
1313 BinanceInterval ,
1414 MarketType ,
1515} from './types' ;
16-
17- // ============================================================================
18- // API 端点配置
19- // ============================================================================
20-
21- /**
22- * 检测是否是本地开发环境
23- */
24- const isDev = typeof window !== 'undefined' &&
25- ( window . location . hostname === 'localhost' || window . location . hostname === '127.0.0.1' ) ;
26-
27- /**
28- * 获取 API 基础 URL
29- * 本地开发使用 Vite 代理,生产环境直连 Binance
30- */
31- function getBaseUrl ( market : 'spot' | 'futures' ) : string {
32- if ( isDev ) {
33- // 本地开发: 使用 Vite 代理路径
34- return market === 'spot' ? '/binance-spot' : '/binance-futures' ;
35- }
36-
37- // 生产环境: 直连 Binance API
38- return market === 'spot'
39- ? 'https://api.binance.com'
40- : 'https://fapi.binance.com' ;
41- }
42-
43- const ENDPOINTS = {
44- spot : {
45- klines : '/api/v3/klines' ,
46- depth : '/api/v3/depth' ,
47- ticker24h : '/api/v3/ticker/24hr' ,
48- } ,
49- futures : {
50- klines : '/fapi/v1/klines' ,
51- depth : '/fapi/v1/depth' ,
52- ticker24h : '/fapi/v1/ticker/24hr' ,
53- } ,
54- } as const ;
16+ import {
17+ DEFAULT_MARKET ,
18+ DEFAULT_SYMBOL ,
19+ REST_ENDPOINTS as ENDPOINTS ,
20+ getBaseUrl ,
21+ } from './constants' ;
5522
5623// ============================================================================
5724// 工具函数
@@ -111,10 +78,10 @@ function buildUrl(
11178 * @param endTime - 结束时间 (毫秒)
11279 */
11380export async function getKlines (
114- symbol : string = 'BTCUSDT' ,
81+ symbol : string = DEFAULT_SYMBOL ,
11582 interval : BinanceInterval = '1h' ,
11683 limit : number = 500 ,
117- market : MarketType = 'spot' ,
84+ market : MarketType = DEFAULT_MARKET ,
11885 startTime ?: number ,
11986 endTime ?: number ,
12087) : Promise < BinanceKline [ ] > {
@@ -159,10 +126,10 @@ export async function getKlines(
159126 * @param market - 市场类型
160127 */
161128export async function getKlinesBatch (
162- symbol : string = 'BTCUSDT' ,
129+ symbol : string = DEFAULT_SYMBOL ,
163130 interval : BinanceInterval = '1h' ,
164131 totalCount : number = 2000 ,
165- market : MarketType = 'spot' ,
132+ market : MarketType = DEFAULT_MARKET ,
166133) : Promise < BinanceKline [ ] > {
167134 const allKlines : BinanceKline [ ] = [ ] ;
168135 const batchSize = 1500 ; // Binance 单次最大限制
@@ -225,9 +192,9 @@ export async function getKlinesBatch(
225192 * @param market - 市场类型
226193 */
227194export async function getDepth (
228- symbol : string = 'BTCUSDT' ,
229- limit : number = 100 , // 默认获取 100 档深度数据(Binance 支持: 5, 10, 20, 50, 100, 500, 1000, 5000)
230- market : MarketType = 'spot' ,
195+ symbol : string = DEFAULT_SYMBOL ,
196+ limit : number = 100 ,
197+ market : MarketType = DEFAULT_MARKET ,
231198) : Promise < BinanceDepth > {
232199 // 验证 limit 参数(Binance 有效值)
233200 const validLimits = [ 5 , 10 , 20 , 50 , 100 , 500 , 1000 , 5000 ] ;
0 commit comments