@discomedia/utils 1.0.21 → 1.0.23

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.
package/dist/index.mjs CHANGED
@@ -16037,15 +16037,18 @@ class AlpacaMarketDataAPI extends EventEmitter {
16037
16037
  stockSubscriptions = { trades: [], quotes: [], bars: [] };
16038
16038
  optionSubscriptions = { trades: [], quotes: [], bars: [] };
16039
16039
  setMode(mode = 'production') {
16040
- if (mode === 'sandbox') { // sandbox mode
16040
+ if (mode === 'sandbox') {
16041
+ // sandbox mode
16041
16042
  this.stockStreamUrl = 'wss://stream.data.sandbox.alpaca.markets/v2/sip';
16042
16043
  this.optionStreamUrl = 'wss://stream.data.sandbox.alpaca.markets/v1beta3/options';
16043
16044
  }
16044
- else if (mode === 'test') { // test mode, can only use ticker FAKEPACA
16045
+ else if (mode === 'test') {
16046
+ // test mode, can only use ticker FAKEPACA
16045
16047
  this.stockStreamUrl = 'wss://stream.data.alpaca.markets/v2/test';
16046
16048
  this.optionStreamUrl = 'wss://stream.data.alpaca.markets/v1beta3/options'; // there's no test mode for options
16047
16049
  }
16048
- else { // production
16050
+ else {
16051
+ // production
16049
16052
  this.stockStreamUrl = 'wss://stream.data.alpaca.markets/v2/sip';
16050
16053
  this.optionStreamUrl = 'wss://stream.data.alpaca.markets/v1beta3/options';
16051
16054
  }
@@ -16193,7 +16196,7 @@ class AlpacaMarketDataAPI extends EventEmitter {
16193
16196
  const currentSubscriptions = streamType === 'stock' ? this.stockSubscriptions : this.optionSubscriptions;
16194
16197
  Object.entries(subscriptions).forEach(([key, value]) => {
16195
16198
  if (value) {
16196
- currentSubscriptions[key] = (currentSubscriptions[key] || []).filter(s => !value.includes(s));
16199
+ currentSubscriptions[key] = (currentSubscriptions[key] || []).filter((s) => !value.includes(s));
16197
16200
  }
16198
16201
  });
16199
16202
  const unsubMessage = {
@@ -16256,11 +16259,11 @@ class AlpacaMarketDataAPI extends EventEmitter {
16256
16259
  let pageCount = 0;
16257
16260
  let currency = '';
16258
16261
  // Initialize bar arrays for each symbol
16259
- symbols.forEach(symbol => {
16262
+ symbols.forEach((symbol) => {
16260
16263
  allBars[symbol] = [];
16261
16264
  });
16262
- log(`Starting historical bars fetch for ${symbolsStr} (${params.timeframe}, ${params.start || 'no start'} to ${params.end || 'no end'})`, {
16263
- type: 'info'
16265
+ log(`Starting historical bars fetch for ${symbolsStr.length} symbols (${params.timeframe}, ${params.start || 'no start'} to ${params.end || 'no end'})`, {
16266
+ type: 'info',
16264
16267
  });
16265
16268
  while (hasMorePages) {
16266
16269
  pageCount++;
@@ -16272,7 +16275,7 @@ class AlpacaMarketDataAPI extends EventEmitter {
16272
16275
  };
16273
16276
  const response = await this.makeRequest('/stocks/bars', 'GET', requestParams);
16274
16277
  if (!response.bars) {
16275
- log(`No bars data found in response for ${symbolsStr}`, { type: 'warn' });
16278
+ log(`No bars data found in response for ${symbolsStr.length} symbols`, { type: 'warn' });
16276
16279
  break;
16277
16280
  }
16278
16281
  // Track currency from first response
@@ -16288,7 +16291,7 @@ class AlpacaMarketDataAPI extends EventEmitter {
16288
16291
  allBars[symbol] = [...allBars[symbol], ...bars];
16289
16292
  pageBarsCount += bars.length;
16290
16293
  // Track date range for this page
16291
- bars.forEach(bar => {
16294
+ bars.forEach((bar) => {
16292
16295
  const barDate = new Date(bar.t);
16293
16296
  if (!earliestTimestamp || barDate < earliestTimestamp) {
16294
16297
  earliestTimestamp = barDate;
@@ -16306,9 +16309,7 @@ class AlpacaMarketDataAPI extends EventEmitter {
16306
16309
  const dateRangeStr = earliestTimestamp && latestTimestamp
16307
16310
  ? `${earliestTimestamp.toLocaleDateString('en-US', { timeZone: 'America/New_York' })} to ${latestTimestamp.toLocaleDateString('en-US', { timeZone: 'America/New_York' })}`
16308
16311
  : 'unknown range';
16309
- log(`Page ${pageCount}: Fetched ${pageBarsCount.toLocaleString()} bars (total: ${totalBarsCount.toLocaleString()}) for ${symbolsStr}, date range: ${dateRangeStr}${hasMorePages ? ', more pages available' : ', complete'}`, {
16310
- type: 'info'
16311
- });
16312
+ log(`Page ${pageCount}: Fetched ${pageBarsCount.toLocaleString()} bars (total: ${totalBarsCount.toLocaleString()}) for ${symbols.length} symbols, date range: ${dateRangeStr}${hasMorePages ? ', more pages available' : ', complete'}`);
16312
16313
  // Prevent infinite loops
16313
16314
  if (pageCount > 1000) {
16314
16315
  log(`Stopping pagination after ${pageCount} pages to prevent infinite loop`, { type: 'warn' });
@@ -16316,10 +16317,10 @@ class AlpacaMarketDataAPI extends EventEmitter {
16316
16317
  }
16317
16318
  }
16318
16319
  // Final summary
16319
- const symbolCounts = Object.entries(allBars).map(([symbol, bars]) => `${symbol}: ${bars.length}`).join(', ');
16320
- log(`Historical bars fetch complete: ${totalBarsCount.toLocaleString()} total bars across ${pageCount} pages (${symbolCounts})`, {
16321
- type: 'info'
16322
- });
16320
+ const symbolsJoined = Object.entries(allBars)
16321
+ .map(([symbol, bars]) => `${symbol}: ${bars.length}`)
16322
+ .join(', ');
16323
+ log(`Historical bars fetch complete: ${totalBarsCount.toLocaleString()} total bars across ${pageCount} pages for ${symbolsJoined.length} symbols'}`);
16323
16324
  return {
16324
16325
  bars: allBars,
16325
16326
  next_page_token: null, // Always null since we fetch all pages
@@ -16586,11 +16587,11 @@ class AlpacaMarketDataAPI extends EventEmitter {
16586
16587
  let totalBarsCount = 0;
16587
16588
  let pageCount = 0;
16588
16589
  // Initialize bar arrays for each symbol
16589
- symbols.forEach(symbol => {
16590
+ symbols.forEach((symbol) => {
16590
16591
  allBars[symbol] = [];
16591
16592
  });
16592
- log(`Starting historical options bars fetch for ${symbolsStr} (${params.timeframe}, ${params.start || 'no start'} to ${params.end || 'no end'})`, {
16593
- type: 'info'
16593
+ log(`Starting historical options bars fetch for ${symbolsStr.length} symbols (${params.timeframe}, ${params.start || 'no start'} to ${params.end || 'no end'})`, {
16594
+ type: 'info',
16594
16595
  });
16595
16596
  while (hasMorePages) {
16596
16597
  pageCount++;
@@ -16600,7 +16601,7 @@ class AlpacaMarketDataAPI extends EventEmitter {
16600
16601
  };
16601
16602
  const response = await this.makeRequest('/options/bars', 'GET', requestParams, 'v1beta1');
16602
16603
  if (!response.bars) {
16603
- log(`No options bars data found in response for ${symbolsStr}`, { type: 'warn' });
16604
+ log(`No options bars data found in response for ${symbolsStr.length} symbols`, { type: 'warn' });
16604
16605
  break;
16605
16606
  }
16606
16607
  // Combine bars for each symbol
@@ -16612,7 +16613,7 @@ class AlpacaMarketDataAPI extends EventEmitter {
16612
16613
  allBars[symbol] = [...allBars[symbol], ...bars];
16613
16614
  pageBarsCount += bars.length;
16614
16615
  // Track date range for this page
16615
- bars.forEach(bar => {
16616
+ bars.forEach((bar) => {
16616
16617
  const barDate = new Date(bar.t);
16617
16618
  if (!earliestTimestamp || barDate < earliestTimestamp) {
16618
16619
  earliestTimestamp = barDate;
@@ -16631,7 +16632,7 @@ class AlpacaMarketDataAPI extends EventEmitter {
16631
16632
  ? `${earliestTimestamp.toLocaleDateString('en-US', { timeZone: 'America/New_York' })} to ${latestTimestamp.toLocaleDateString('en-US', { timeZone: 'America/New_York' })}`
16632
16633
  : 'unknown range';
16633
16634
  log(`Page ${pageCount}: Fetched ${pageBarsCount.toLocaleString()} option bars (total: ${totalBarsCount.toLocaleString()}) for ${symbolsStr}, date range: ${dateRangeStr}${hasMorePages ? ', more pages available' : ', complete'}`, {
16634
- type: 'info'
16635
+ type: 'info',
16635
16636
  });
16636
16637
  // Prevent infinite loops
16637
16638
  if (pageCount > 1000) {
@@ -16640,9 +16641,11 @@ class AlpacaMarketDataAPI extends EventEmitter {
16640
16641
  }
16641
16642
  }
16642
16643
  // Final summary
16643
- const symbolCounts = Object.entries(allBars).map(([symbol, bars]) => `${symbol}: ${bars.length}`).join(', ');
16644
+ const symbolCounts = Object.entries(allBars)
16645
+ .map(([symbol, bars]) => `${symbol}: ${bars.length}`)
16646
+ .join(', ');
16644
16647
  log(`Historical options bars fetch complete: ${totalBarsCount.toLocaleString()} total bars across ${pageCount} pages (${symbolCounts})`, {
16645
- type: 'info'
16648
+ type: 'info',
16646
16649
  });
16647
16650
  return {
16648
16651
  bars: allBars,
@@ -16666,11 +16669,11 @@ class AlpacaMarketDataAPI extends EventEmitter {
16666
16669
  let totalTradesCount = 0;
16667
16670
  let pageCount = 0;
16668
16671
  // Initialize trades arrays for each symbol
16669
- symbols.forEach(symbol => {
16672
+ symbols.forEach((symbol) => {
16670
16673
  allTrades[symbol] = [];
16671
16674
  });
16672
- log(`Starting historical options trades fetch for ${symbolsStr} (${params.start || 'no start'} to ${params.end || 'no end'})`, {
16673
- type: 'info'
16675
+ log(`Starting historical options trades fetch for ${symbolsStr.length} symbols (${params.start || 'no start'} to ${params.end || 'no end'})`, {
16676
+ type: 'info',
16674
16677
  });
16675
16678
  while (hasMorePages) {
16676
16679
  pageCount++;
@@ -16680,7 +16683,7 @@ class AlpacaMarketDataAPI extends EventEmitter {
16680
16683
  };
16681
16684
  const response = await this.makeRequest('/options/trades', 'GET', requestParams, 'v1beta1');
16682
16685
  if (!response.trades) {
16683
- log(`No options trades data found in response for ${symbolsStr}`, { type: 'warn' });
16686
+ log(`No options trades data found in response for ${symbolsStr.length} symbols`, { type: 'warn' });
16684
16687
  break;
16685
16688
  }
16686
16689
  // Combine trades for each symbol
@@ -16692,7 +16695,7 @@ class AlpacaMarketDataAPI extends EventEmitter {
16692
16695
  allTrades[symbol] = [...allTrades[symbol], ...trades];
16693
16696
  pageTradesCount += trades.length;
16694
16697
  // Track date range for this page
16695
- trades.forEach(trade => {
16698
+ trades.forEach((trade) => {
16696
16699
  const tradeDate = new Date(trade.t);
16697
16700
  if (!earliestTimestamp || tradeDate < earliestTimestamp) {
16698
16701
  earliestTimestamp = tradeDate;
@@ -16710,8 +16713,8 @@ class AlpacaMarketDataAPI extends EventEmitter {
16710
16713
  const dateRangeStr = earliestTimestamp && latestTimestamp
16711
16714
  ? `${earliestTimestamp.toLocaleDateString('en-US', { timeZone: 'America/New_York' })} to ${latestTimestamp.toLocaleDateString('en-US', { timeZone: 'America/New_York' })}`
16712
16715
  : 'unknown range';
16713
- log(`Page ${pageCount}: Fetched ${pageTradesCount.toLocaleString()} option trades (total: ${totalTradesCount.toLocaleString()}) for ${symbolsStr}, date range: ${dateRangeStr}${hasMorePages ? ', more pages available' : ', complete'}`, {
16714
- type: 'info'
16716
+ log(`Page ${pageCount}: Fetched ${pageTradesCount.toLocaleString()} option trades (total: ${totalTradesCount.toLocaleString()}) for ${symbolsStr.length} symbols, date range: ${dateRangeStr}${hasMorePages ? ', more pages available' : ', complete'}`, {
16717
+ type: 'info',
16715
16718
  });
16716
16719
  // Prevent infinite loops
16717
16720
  if (pageCount > 1000) {
@@ -16720,9 +16723,11 @@ class AlpacaMarketDataAPI extends EventEmitter {
16720
16723
  }
16721
16724
  }
16722
16725
  // Final summary
16723
- const symbolCounts = Object.entries(allTrades).map(([symbol, trades]) => `${symbol}: ${trades.length}`).join(', ');
16726
+ const symbolCounts = Object.entries(allTrades)
16727
+ .map(([symbol, trades]) => `${symbol}: ${trades.length}`)
16728
+ .join(', ');
16724
16729
  log(`Historical options trades fetch complete: ${totalTradesCount.toLocaleString()} total trades across ${pageCount} pages (${symbolCounts})`, {
16725
- type: 'info'
16730
+ type: 'info',
16726
16731
  });
16727
16732
  return {
16728
16733
  trades: allTrades,
@@ -16867,7 +16872,9 @@ class AlpacaMarketDataAPI extends EventEmitter {
16867
16872
  ...(symbol && { symbols: symbol }),
16868
16873
  ...(mergedParams.limit && { limit: Math.min(50, maxLimit - fetchedCount).toString() }),
16869
16874
  ...(mergedParams.sort && { sort: mergedParams.sort }),
16870
- ...(mergedParams.include_content !== undefined ? { include_content: mergedParams.include_content.toString() } : {}),
16875
+ ...(mergedParams.include_content !== undefined
16876
+ ? { include_content: mergedParams.include_content.toString() }
16877
+ : {}),
16871
16878
  ...(pageToken && { page_token: pageToken }),
16872
16879
  });
16873
16880
  const url = `${this.v1beta1url}/news?${queryParams}`;