infoway-sdk 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,317 @@
1
+ /**
2
+ * Low-level HTTP client with retry and error handling.
3
+ * Uses native fetch (Node 18+).
4
+ */
5
+ declare class HttpClient {
6
+ private readonly _apiKey;
7
+ private readonly _baseUrl;
8
+ private readonly _timeout;
9
+ private readonly _maxRetries;
10
+ constructor(options?: {
11
+ apiKey?: string;
12
+ baseUrl?: string;
13
+ timeout?: number;
14
+ maxRetries?: number;
15
+ });
16
+ /** Expose apiKey for testing. */
17
+ get apiKey(): string;
18
+ get(path: string, params?: Record<string, string | number | undefined>): Promise<unknown>;
19
+ post(path: string, body?: Record<string, unknown>): Promise<unknown>;
20
+ private _buildUrl;
21
+ private _request;
22
+ private _handleResponse;
23
+ private _sleep;
24
+ }
25
+
26
+ /**
27
+ * Shared market data methods for trade/depth/kline.
28
+ * Base class for StockClient, CryptoClient, JapanClient, IndiaClient, CommonClient.
29
+ */
30
+
31
+ declare abstract class MarketDataClient {
32
+ protected abstract readonly _prefix: string;
33
+ protected readonly _http: HttpClient;
34
+ constructor(http: HttpClient);
35
+ /**
36
+ * Get real-time trade data.
37
+ * @param codes - Comma-separated symbol codes (e.g. "AAPL.US" or "AAPL.US,TSLA.US")
38
+ */
39
+ getTrade(codes: string): Promise<unknown>;
40
+ /**
41
+ * Get real-time order book depth.
42
+ * @param codes - Comma-separated symbol codes
43
+ */
44
+ getDepth(codes: string): Promise<unknown>;
45
+ /**
46
+ * Get candlestick/K-line data.
47
+ * @param codes - Comma-separated symbol codes
48
+ * @param klineType - K-line interval (use KlineType enum or number 1-12)
49
+ * @param count - Number of candles to return
50
+ */
51
+ getKline(codes: string, klineType: number, count: number): Promise<unknown>;
52
+ }
53
+
54
+ /** Stock market data client (HK, US, CN). */
55
+
56
+ declare class StockClient extends MarketDataClient {
57
+ protected readonly _prefix = "stock";
58
+ }
59
+
60
+ /** Crypto market data client. */
61
+
62
+ declare class CryptoClient extends MarketDataClient {
63
+ protected readonly _prefix = "crypto";
64
+ }
65
+
66
+ /** Japan market data client. */
67
+
68
+ declare class JapanClient extends MarketDataClient {
69
+ protected readonly _prefix = "japan";
70
+ }
71
+
72
+ /** India market data client. */
73
+
74
+ declare class IndiaClient extends MarketDataClient {
75
+ protected readonly _prefix = "india";
76
+ }
77
+
78
+ /** Common market data client. */
79
+
80
+ declare class CommonClient extends MarketDataClient {
81
+ protected readonly _prefix = "common";
82
+ }
83
+
84
+ /** Basic information client (symbols, trading days, hours). */
85
+
86
+ declare class BasicClient {
87
+ private readonly _http;
88
+ constructor(http: HttpClient);
89
+ getSymbols(market?: string): Promise<unknown>;
90
+ getSymbolInfo(codes: string): Promise<unknown>;
91
+ getAdjustmentFactors(codes: string): Promise<unknown>;
92
+ getTradingDays(market: string): Promise<unknown>;
93
+ getTradingHours(market?: string): Promise<unknown>;
94
+ }
95
+
96
+ /** Market overview client (temperature, breadth, indexes, leaders). */
97
+
98
+ declare class MarketClient {
99
+ private readonly _http;
100
+ constructor(http: HttpClient);
101
+ getTemperature(market?: string): Promise<unknown>;
102
+ getBreadth(market: string): Promise<unknown>;
103
+ getIndexes(): Promise<unknown>;
104
+ getLeaders(market: string, limit?: number): Promise<unknown>;
105
+ getRankConfig(market: string): Promise<unknown>;
106
+ }
107
+
108
+ /** Plate (sector) data client. */
109
+
110
+ declare class PlateClient {
111
+ private readonly _http;
112
+ constructor(http: HttpClient);
113
+ getIndustry(market: string, limit?: number): Promise<unknown>;
114
+ getConcept(market: string, limit?: number): Promise<unknown>;
115
+ getMembers(plateSymbol: string, offset?: number, limit?: number): Promise<unknown>;
116
+ getIntro(plateSymbol: string): Promise<unknown>;
117
+ getChart(market: string, limit?: number): Promise<unknown>;
118
+ }
119
+
120
+ /** Stock fundamental data client. */
121
+
122
+ declare class StockInfoClient {
123
+ private readonly _http;
124
+ constructor(http: HttpClient);
125
+ getValuation(symbol: string): Promise<unknown>;
126
+ getRatings(symbol: string): Promise<unknown>;
127
+ getCompany(symbol: string): Promise<unknown>;
128
+ getPanorama(symbol: string): Promise<unknown>;
129
+ getConcepts(symbol: string): Promise<unknown>;
130
+ getEvents(symbol: string, limit?: number): Promise<unknown>;
131
+ getDrivers(symbol: string): Promise<unknown>;
132
+ }
133
+
134
+ /**
135
+ * Shared type definitions and enums for the Infoway SDK.
136
+ */
137
+ /** K-line (candlestick) interval types. */
138
+ declare enum KlineType {
139
+ MIN_1 = 1,
140
+ MIN_5 = 2,
141
+ MIN_15 = 3,
142
+ MIN_30 = 4,
143
+ HOUR_1 = 5,
144
+ HOUR_2 = 6,
145
+ HOUR_4 = 7,
146
+ DAY = 8,
147
+ WEEK = 9,
148
+ MONTH = 10,
149
+ QUARTER = 11,
150
+ YEAR = 12
151
+ }
152
+ /** Business types for WebSocket connections. */
153
+ declare const Business: {
154
+ readonly STOCK: "stock";
155
+ readonly JAPAN: "japan";
156
+ readonly INDIA: "india";
157
+ readonly CRYPTO: "crypto";
158
+ readonly COMMON: "common";
159
+ };
160
+ type Business = (typeof Business)[keyof typeof Business];
161
+ /** WebSocket message codes. */
162
+ declare enum WsCode {
163
+ SUB_TRADE = 10000,
164
+ PUSH_TRADE = 10001,
165
+ UNSUB_TRADE = 10002,
166
+ SUB_DEPTH = 10003,
167
+ PUSH_DEPTH = 10004,
168
+ UNSUB_DEPTH = 10005,
169
+ SUB_KLINE = 10006,
170
+ PUSH_KLINE = 10007,
171
+ UNSUB_KLINE = 10008,
172
+ HEARTBEAT = 10010
173
+ }
174
+ /** Options for creating an InfowayClient. */
175
+ interface InfowayClientOptions {
176
+ /** Your Infoway API key. Falls back to INFOWAY_API_KEY env var. */
177
+ apiKey?: string;
178
+ /** Base URL for REST API. Default: https://data.infoway.io */
179
+ baseUrl?: string;
180
+ /** Request timeout in milliseconds. Default: 15000 */
181
+ timeout?: number;
182
+ /** Max retry attempts for failed requests. Default: 3 */
183
+ maxRetries?: number;
184
+ }
185
+ /** Options for creating an InfowayWebSocket. */
186
+ interface InfowayWebSocketOptions {
187
+ /** Your Infoway API key. */
188
+ apiKey: string;
189
+ /** Business type (stock, crypto, japan, india, common). */
190
+ business: Business;
191
+ /** Base WebSocket URL. Default: wss://data.infoway.io/ws */
192
+ baseUrl?: string;
193
+ /** Maximum reconnect attempts. Unlimited if not set. */
194
+ maxReconnectAttempts?: number;
195
+ }
196
+ /** Standard API response envelope. */
197
+ interface ApiResponse<T = unknown> {
198
+ ret: number;
199
+ /** Some endpoints return `code` instead of `ret`. */
200
+ code?: number;
201
+ msg: string;
202
+ traceId?: string;
203
+ data: T;
204
+ }
205
+ /** WebSocket message structure. */
206
+ interface WsMessage {
207
+ code: number;
208
+ trace: string;
209
+ data?: Record<string, unknown>;
210
+ }
211
+
212
+ /**
213
+ * Main entry point for the Infoway SDK.
214
+ */
215
+
216
+ /**
217
+ * Infoway API client.
218
+ *
219
+ * @example
220
+ * ```ts
221
+ * import { InfowayClient } from "infoway-sdk";
222
+ *
223
+ * const client = new InfowayClient({ apiKey: "YOUR_API_KEY" });
224
+ * const trades = await client.stock.getTrade("AAPL.US");
225
+ * const klines = await client.crypto.getKline("BTCUSDT", 8, 100);
226
+ * const temp = await client.market.getTemperature("HK,US");
227
+ * ```
228
+ */
229
+ declare class InfowayClient {
230
+ /** @internal */
231
+ readonly _http: HttpClient;
232
+ readonly stock: StockClient;
233
+ readonly crypto: CryptoClient;
234
+ readonly japan: JapanClient;
235
+ readonly india: IndiaClient;
236
+ readonly common: CommonClient;
237
+ readonly basic: BasicClient;
238
+ readonly market: MarketClient;
239
+ readonly plate: PlateClient;
240
+ readonly stockInfo: StockInfoClient;
241
+ constructor(options?: InfowayClientOptions);
242
+ }
243
+
244
+ /**
245
+ * WebSocket client with auto-reconnect, heartbeat, and subscription management.
246
+ */
247
+
248
+ type MessageHandler = (msg: Record<string, unknown>) => void;
249
+ type ErrorHandler = (err: Error) => void;
250
+ type VoidHandler = () => void;
251
+ declare class InfowayWebSocket {
252
+ /** @internal */
253
+ readonly _url: string;
254
+ private readonly _maxReconnect;
255
+ private _ws;
256
+ /** @internal */
257
+ readonly _subscriptions: Set<string>;
258
+ private _running;
259
+ private _backoff;
260
+ private _heartbeatTimer;
261
+ /** Called when a trade push message is received. */
262
+ onTrade: MessageHandler | null;
263
+ /** Called when a depth push message is received. */
264
+ onDepth: MessageHandler | null;
265
+ /** Called when a kline push message is received. */
266
+ onKline: MessageHandler | null;
267
+ /** Called when an error occurs. */
268
+ onError: ErrorHandler | null;
269
+ /** Called after a successful reconnection. */
270
+ onReconnect: VoidHandler | null;
271
+ /** Called when the connection is lost. */
272
+ onDisconnect: VoidHandler | null;
273
+ constructor(options: InfowayWebSocketOptions);
274
+ /**
275
+ * Build a WebSocket message JSON string.
276
+ * @internal
277
+ */
278
+ _buildMessage(code: WsCode | number, codes?: string): string;
279
+ /**
280
+ * Connect and start listening. Returns a Promise that resolves
281
+ * when the connection is closed intentionally or max reconnects exceeded.
282
+ */
283
+ connect(): Promise<void>;
284
+ private _connectOnce;
285
+ private _startHeartbeat;
286
+ private _stopHeartbeat;
287
+ private _resubscribe;
288
+ subscribeTrade(codes: string): Promise<void>;
289
+ subscribeDepth(codes: string): Promise<void>;
290
+ subscribeKline(codes: string): Promise<void>;
291
+ unsubscribeTrade(codes: string): Promise<void>;
292
+ unsubscribeDepth(codes: string): Promise<void>;
293
+ unsubscribeKline(codes: string): Promise<void>;
294
+ close(): Promise<void>;
295
+ private _sleep;
296
+ }
297
+
298
+ /**
299
+ * Infoway SDK exception types.
300
+ */
301
+ /** Raised when a request times out. */
302
+ declare class InfowayTimeoutError extends Error {
303
+ constructor(message?: string);
304
+ }
305
+ /** Raised when the API returns a non-success response. */
306
+ declare class InfowayAPIError extends Error {
307
+ readonly ret: number;
308
+ readonly msg: string;
309
+ readonly traceId?: string;
310
+ constructor(ret: number, msg: string, traceId?: string);
311
+ }
312
+ /** Raised on 401 Unauthorized. */
313
+ declare class InfowayAuthError extends InfowayAPIError {
314
+ constructor(msg?: string);
315
+ }
316
+
317
+ export { type ApiResponse, BasicClient, Business, CommonClient, CryptoClient, HttpClient, IndiaClient, InfowayAPIError, InfowayAuthError, InfowayClient, type InfowayClientOptions, InfowayTimeoutError, InfowayWebSocket, type InfowayWebSocketOptions, JapanClient, KlineType, MarketClient, PlateClient, StockClient, StockInfoClient, WsCode, type WsMessage };