@fullstackcraftllc/floe 0.0.1 → 0.0.2

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,352 @@
1
+ import { NormalizedOption, NormalizedTicker } from "../types";
2
+ /**
3
+ * Supported broker integrations for the FloeClient.
4
+ * @enum {string}
5
+ */
6
+ export declare enum Broker {
7
+ /** Tradier brokerage API */
8
+ TRADIER = "tradier"
9
+ }
10
+ /**
11
+ * Event types emitted by the FloeClient.
12
+ * @remarks
13
+ * Used with the {@link FloeClient.on} and {@link FloeClient.off} methods for event-driven updates.
14
+ */
15
+ type FloeEventType = 'tickerUpdate' | 'optionUpdate' | 'error' | 'connected' | 'disconnected';
16
+ /**
17
+ * Event payload map for type-safe event handling.
18
+ */
19
+ interface FloeEventMap {
20
+ tickerUpdate: NormalizedTicker;
21
+ optionUpdate: NormalizedOption;
22
+ error: Error;
23
+ connected: {
24
+ broker: Broker;
25
+ };
26
+ disconnected: {
27
+ broker: Broker;
28
+ reason?: string;
29
+ };
30
+ }
31
+ /**
32
+ * Listener function type for FloeClient events.
33
+ * @template T - The event type from FloeEventType
34
+ */
35
+ type FloeEventListener<T extends FloeEventType> = (data: FloeEventMap[T]) => void;
36
+ /**
37
+ * FloeClient provides a unified, broker-agnostic interface for subscribing to
38
+ * real-time market data including stock tickers and options.
39
+ *
40
+ * @remarks
41
+ * The client normalizes data from various brokers into a consistent format,
42
+ * allowing consumers to switch brokers without changing their application code.
43
+ *
44
+ * @example
45
+ * ```typescript
46
+ * const client = new FloeClient();
47
+ *
48
+ * // Connect to a broker
49
+ * client.connect(Broker.TRADIER, 'your-api-key');
50
+ *
51
+ * // Subscribe to updates using the event emitter pattern
52
+ * client.on('tickerUpdate', (ticker) => {
53
+ * console.log(`${ticker.symbol}: ${ticker.price}`);
54
+ * });
55
+ *
56
+ * // Or use the callback pattern
57
+ * client.onTickerDataChange((ticker) => {
58
+ * console.log(`${ticker.symbol}: ${ticker.price}`);
59
+ * });
60
+ *
61
+ * // Subscribe to specific tickers
62
+ * client.subscribeToTickers(['AAPL', 'GOOGL', 'MSFT']);
63
+ * ```
64
+ */
65
+ export declare class FloeClient {
66
+ /** Currently connected broker, or null if not connected */
67
+ private currentBroker;
68
+ /** List of ticker symbols currently subscribed to */
69
+ private currentSubscribedTickers;
70
+ /** List of option symbols (OCC format) currently subscribed to */
71
+ private currentSubscribedOptions;
72
+ /** Cache of the latest normalized ticker data */
73
+ private normalizedTickers;
74
+ /** Cache of the latest normalized option data */
75
+ private normalizedOptions;
76
+ /** Tradier broker client instance */
77
+ private tradierClient;
78
+ /** Event listeners registry for the EventEmitter pattern */
79
+ private eventListeners;
80
+ /** Callback for ticker data changes (legacy callback pattern) */
81
+ private tickerDataCallback;
82
+ /** Callback for option data changes (legacy callback pattern) */
83
+ private optionDataCallback;
84
+ /**
85
+ * Creates a new FloeClient instance.
86
+ *
87
+ * @remarks
88
+ * The client is created in a disconnected state. Call {@link connect} to
89
+ * establish a connection to a broker before subscribing to data.
90
+ *
91
+ * @example
92
+ * ```typescript
93
+ * const client = new FloeClient();
94
+ * ```
95
+ */
96
+ constructor();
97
+ /**
98
+ * Establishes a connection to a broker's API.
99
+ *
100
+ * @param broker - The broker to connect to (e.g., Broker.TRADIER)
101
+ * @param authKey - The API authentication key or token for the broker
102
+ *
103
+ * @throws {Error} Throws if the specified broker is not supported
104
+ *
105
+ * @remarks
106
+ * Must be called before subscribing to any market data. Only one broker
107
+ * connection is active at a time; calling connect again will switch brokers.
108
+ *
109
+ * @example
110
+ * ```typescript
111
+ * await client.connect(Broker.TRADIER, 'your-tradier-api-key');
112
+ * ```
113
+ */
114
+ connect(broker: Broker, authKey: string): Promise<void>;
115
+ /**
116
+ * Disconnects from the current broker.
117
+ *
118
+ * @remarks
119
+ * Closes the WebSocket connection and clears all subscriptions.
120
+ *
121
+ * @example
122
+ * ```typescript
123
+ * client.disconnect();
124
+ * ```
125
+ */
126
+ disconnect(): void;
127
+ /**
128
+ * Subscribes to real-time updates for the specified stock ticker symbols.
129
+ *
130
+ * @param tickers - Array of ticker symbols to subscribe to (e.g., ['AAPL', 'GOOGL'])
131
+ *
132
+ * @throws {Error} Throws if no broker connection has been established
133
+ *
134
+ * @remarks
135
+ * Ticker updates will be delivered via the 'tickerUpdate' event or through
136
+ * the callback registered with {@link onTickerDataChange}.
137
+ *
138
+ * @example
139
+ * ```typescript
140
+ * client.subscribeToTickers(['AAPL', 'GOOGL', 'MSFT']);
141
+ * ```
142
+ */
143
+ subscribeToTickers(tickers: Array<string>): void;
144
+ /**
145
+ * Subscribes to real-time updates for the specified option contracts.
146
+ *
147
+ * @param symbols - Array of option symbols in OCC format
148
+ * (e.g., ['AAPL230120C00150000'] for AAPL $150 Call expiring Jan 20, 2023)
149
+ *
150
+ * @throws {Error} Throws if no broker connection has been established
151
+ *
152
+ * @remarks
153
+ * Option symbols must be in the standard OCC (Options Clearing Corporation) format:
154
+ * - Root symbol (up to 6 characters, left-padded)
155
+ * - Expiration date (YYMMDD)
156
+ * - Option type (C for call, P for put)
157
+ * - Strike price (8 digits, price × 1000, left-padded with zeros)
158
+ *
159
+ * Option updates will be delivered via the 'optionUpdate' event or through
160
+ * the callback registered with {@link onOptionDataChange}.
161
+ *
162
+ * @example
163
+ * ```typescript
164
+ * // Subscribe to AAPL $150 Call expiring Jan 20, 2023
165
+ * client.subscribeToOptions(['AAPL230120C00150000']);
166
+ * ```
167
+ */
168
+ subscribeToOptions(symbols: Array<string>): void;
169
+ /**
170
+ * Unsubscribes from real-time updates for the specified stock ticker symbols.
171
+ *
172
+ * @param tickers - Array of ticker symbols to unsubscribe from
173
+ *
174
+ * @throws {Error} Throws if no broker connection has been established
175
+ *
176
+ * @remarks
177
+ * After unsubscribing, no further updates will be received for these tickers.
178
+ * Has no effect if the specified tickers were not previously subscribed.
179
+ *
180
+ * @example
181
+ * ```typescript
182
+ * client.unsubscribeFromTickers(['AAPL', 'GOOGL']);
183
+ * ```
184
+ */
185
+ unsubscribeFromTickers(tickers: Array<string>): void;
186
+ /**
187
+ * Unsubscribes from real-time updates for the specified option contracts.
188
+ *
189
+ * @param symbols - Array of option symbols in OCC format to unsubscribe from
190
+ *
191
+ * @throws {Error} Throws if no broker connection has been established
192
+ *
193
+ * @remarks
194
+ * After unsubscribing, no further updates will be received for these options.
195
+ * Has no effect if the specified options were not previously subscribed.
196
+ *
197
+ * @example
198
+ * ```typescript
199
+ * client.unsubscribeFromOptions(['AAPL230120C00150000']);
200
+ * ```
201
+ */
202
+ unsubscribeFromOptions(symbols: Array<string>): void;
203
+ /**
204
+ * Registers an event listener for the specified event type.
205
+ *
206
+ * @template T - The event type
207
+ * @param event - The event type to listen for
208
+ * @param listener - The callback function to invoke when the event occurs
209
+ * @returns The FloeClient instance for method chaining
210
+ *
211
+ * @remarks
212
+ * Multiple listeners can be registered for the same event type.
213
+ * Use {@link off} to remove a listener when it's no longer needed.
214
+ *
215
+ * @example
216
+ * ```typescript
217
+ * client
218
+ * .on('tickerUpdate', (ticker) => console.log(ticker))
219
+ * .on('error', (error) => console.error(error));
220
+ * ```
221
+ */
222
+ on<T extends FloeEventType>(event: T, listener: FloeEventListener<T>): this;
223
+ /**
224
+ * Removes an event listener for the specified event type.
225
+ *
226
+ * @template T - The event type
227
+ * @param event - The event type to stop listening for
228
+ * @param listener - The callback function to remove
229
+ * @returns The FloeClient instance for method chaining
230
+ *
231
+ * @remarks
232
+ * The listener must be the exact same function reference that was passed to {@link on}.
233
+ *
234
+ * @example
235
+ * ```typescript
236
+ * const handler = (ticker) => console.log(ticker);
237
+ * client.on('tickerUpdate', handler);
238
+ * // Later...
239
+ * client.off('tickerUpdate', handler);
240
+ * ```
241
+ */
242
+ off<T extends FloeEventType>(event: T, listener: FloeEventListener<T>): this;
243
+ /**
244
+ * Registers a one-time event listener that automatically removes itself after firing.
245
+ *
246
+ * @template T - The event type
247
+ * @param event - The event type to listen for
248
+ * @param listener - The callback function to invoke once when the event occurs
249
+ * @returns The FloeClient instance for method chaining
250
+ *
251
+ * @example
252
+ * ```typescript
253
+ * client.once('connected', ({ broker }) => {
254
+ * console.log(`Connected to ${broker}`);
255
+ * });
256
+ * ```
257
+ */
258
+ once<T extends FloeEventType>(event: T, listener: FloeEventListener<T>): this;
259
+ /**
260
+ * Emits an event to all registered listeners.
261
+ *
262
+ * @template T - The event type
263
+ * @param event - The event type to emit
264
+ * @param data - The event payload
265
+ *
266
+ * @internal
267
+ * @remarks
268
+ * This method is used internally to dispatch events. It also triggers
269
+ * legacy callback handlers for backwards compatibility.
270
+ */
271
+ private emit;
272
+ /**
273
+ * Registers a callback to be invoked whenever ticker data is updated.
274
+ *
275
+ * @param callback - Function to call with the updated ticker data
276
+ *
277
+ * @deprecated Prefer using {@link on}('tickerUpdate', callback) for new code.
278
+ * The event emitter pattern supports multiple listeners and provides
279
+ * better lifecycle management.
280
+ *
281
+ * @remarks
282
+ * Only one callback can be registered at a time. Calling this method again
283
+ * will replace the previous callback. For multiple listeners, use {@link on}.
284
+ *
285
+ * @example
286
+ * ```typescript
287
+ * client.onTickerDataChange((ticker) => {
288
+ * console.log(`${ticker.symbol} updated: ${ticker.price}`);
289
+ * });
290
+ * ```
291
+ */
292
+ onTickerDataChange(callback: (data: NormalizedTicker) => void): void;
293
+ /**
294
+ * Registers a callback to be invoked whenever option data is updated.
295
+ *
296
+ * @param callback - Function to call with the updated option data
297
+ *
298
+ * @deprecated Prefer using {@link on}('optionUpdate', callback) for new code.
299
+ * The event emitter pattern supports multiple listeners and provides
300
+ * better lifecycle management.
301
+ *
302
+ * @remarks
303
+ * Only one callback can be registered at a time. Calling this method again
304
+ * will replace the previous callback. For multiple listeners, use {@link on}.
305
+ *
306
+ * @example
307
+ * ```typescript
308
+ * client.onOptionDataChange((option) => {
309
+ * console.log(`${option.symbol} bid: ${option.bid}, ask: ${option.ask}`);
310
+ * });
311
+ * ```
312
+ */
313
+ onOptionDataChange(callback: (data: NormalizedOption) => void): void;
314
+ /**
315
+ * Returns the list of currently subscribed ticker symbols.
316
+ *
317
+ * @returns Array of ticker symbols currently subscribed to
318
+ *
319
+ * @example
320
+ * ```typescript
321
+ * const tickers = client.getSubscribedTickers();
322
+ * console.log(`Subscribed to: ${tickers.join(', ')}`);
323
+ * ```
324
+ */
325
+ getSubscribedTickers(): ReadonlyArray<string>;
326
+ /**
327
+ * Returns the list of currently subscribed option symbols.
328
+ *
329
+ * @returns Array of option symbols (OCC format) currently subscribed to
330
+ *
331
+ * @example
332
+ * ```typescript
333
+ * const options = client.getSubscribedOptions();
334
+ * console.log(`Subscribed to ${options.length} options`);
335
+ * ```
336
+ */
337
+ getSubscribedOptions(): ReadonlyArray<string>;
338
+ /**
339
+ * Returns whether the client is currently connected to a broker.
340
+ *
341
+ * @returns True if connected to a broker, false otherwise
342
+ *
343
+ * @example
344
+ * ```typescript
345
+ * if (client.isConnected()) {
346
+ * client.subscribeToTickers(['AAPL']);
347
+ * }
348
+ * ```
349
+ */
350
+ isConnected(): boolean;
351
+ }
352
+ export {};