@adaptic/utils 0.1.2 → 0.1.3

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/test.js CHANGED
@@ -6336,23 +6336,42 @@ requireWebsocketServer();
6336
6336
  /**
6337
6337
  * Validates Alpaca API credentials
6338
6338
  * @param auth - Authentication object containing API key and secret
6339
- * @throws {Error} If credentials are invalid or missing
6339
+ * @param options - Validation options
6340
+ * @param options.throwOnMissing - If false, missing credentials will log a warning instead of throwing (default: true)
6341
+ * @throws {Error} If credentials are invalid (when throwOnMissing is true)
6342
+ * @returns {boolean} True if credentials are valid, false if missing (when throwOnMissing is false)
6340
6343
  */
6341
- function validateAlpacaCredentials(auth) {
6344
+ function validateAlpacaCredentials(auth, options = { throwOnMissing: true }) {
6345
+ const { throwOnMissing = true } = options;
6346
+ // Check for missing or empty API key
6342
6347
  if (!auth.apiKey ||
6343
6348
  typeof auth.apiKey !== "string" ||
6344
6349
  auth.apiKey.trim().length === 0) {
6345
- throw new Error("Invalid Alpaca API key: must be a non-empty string");
6350
+ if (throwOnMissing) {
6351
+ throw new Error("Invalid Alpaca API key: must be a non-empty string");
6352
+ }
6353
+ console.warn("[AlpacaAPI] API key not configured. Market data features will be unavailable.");
6354
+ return false;
6346
6355
  }
6356
+ // Check for missing or empty API secret
6347
6357
  if (!auth.apiSecret ||
6348
6358
  typeof auth.apiSecret !== "string" ||
6349
6359
  auth.apiSecret.trim().length === 0) {
6350
- throw new Error("Invalid Alpaca API secret: must be a non-empty string");
6360
+ if (throwOnMissing) {
6361
+ throw new Error("Invalid Alpaca API secret: must be a non-empty string");
6362
+ }
6363
+ console.warn("[AlpacaAPI] API secret not configured. Market data features will be unavailable.");
6364
+ return false;
6351
6365
  }
6352
6366
  // Alpaca keys are typically 20+ characters
6353
6367
  if (auth.apiKey.length < 10) {
6354
- throw new Error("Alpaca API key appears to be too short");
6368
+ if (throwOnMissing) {
6369
+ throw new Error("Alpaca API key appears to be too short");
6370
+ }
6371
+ console.warn("[AlpacaAPI] API key appears to be too short.");
6372
+ return false;
6355
6373
  }
6374
+ return true;
6356
6375
  }
6357
6376
 
6358
6377
  /**
@@ -6396,6 +6415,8 @@ class AlpacaMarketDataAPI extends EventEmitter {
6396
6415
  dataURL;
6397
6416
  apiURL;
6398
6417
  v1beta1url;
6418
+ /** Whether API credentials are valid and available. False during build time when env vars are missing. */
6419
+ credentialsValid = false;
6399
6420
  stockStreamUrl = getStockStreamUrl("PRODUCTION"); // production values
6400
6421
  optionStreamUrl = getOptionsStreamUrl("PRODUCTION"); // production values
6401
6422
  cryptoStreamUrl = getCryptoStreamUrl("PRODUCTION"); // production values
@@ -6451,13 +6472,16 @@ class AlpacaMarketDataAPI extends EventEmitter {
6451
6472
  constructor() {
6452
6473
  super();
6453
6474
  // Validate credentials from environment variables before initializing
6454
- const apiKey = process.env.ALPACA_API_KEY;
6455
- const apiSecret = process.env.ALPACA_SECRET_KEY;
6456
- validateAlpacaCredentials({
6475
+ // Use throwOnMissing: false to allow initialization during build time
6476
+ // when env vars are not available. Features will be unavailable until
6477
+ // credentials are provided at runtime.
6478
+ const apiKey = process.env.ALPACA_API_KEY || "";
6479
+ const apiSecret = process.env.ALPACA_SECRET_KEY || "";
6480
+ this.credentialsValid = validateAlpacaCredentials({
6457
6481
  apiKey,
6458
6482
  apiSecret,
6459
6483
  isPaper: process.env.ALPACA_ACCOUNT_TYPE === "PAPER",
6460
- });
6484
+ }, { throwOnMissing: false });
6461
6485
  this.dataURL = MARKET_DATA_API.STOCKS;
6462
6486
  this.apiURL =
6463
6487
  process.env.ALPACA_ACCOUNT_TYPE === "PAPER"