@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/index.mjs CHANGED
@@ -300,23 +300,42 @@ const fetchAssetOverview = async (symbol) => {
300
300
  /**
301
301
  * Validates Alpaca API credentials
302
302
  * @param auth - Authentication object containing API key and secret
303
- * @throws {Error} If credentials are invalid or missing
303
+ * @param options - Validation options
304
+ * @param options.throwOnMissing - If false, missing credentials will log a warning instead of throwing (default: true)
305
+ * @throws {Error} If credentials are invalid (when throwOnMissing is true)
306
+ * @returns {boolean} True if credentials are valid, false if missing (when throwOnMissing is false)
304
307
  */
305
- function validateAlpacaCredentials(auth) {
308
+ function validateAlpacaCredentials(auth, options = { throwOnMissing: true }) {
309
+ const { throwOnMissing = true } = options;
310
+ // Check for missing or empty API key
306
311
  if (!auth.apiKey ||
307
312
  typeof auth.apiKey !== "string" ||
308
313
  auth.apiKey.trim().length === 0) {
309
- throw new Error("Invalid Alpaca API key: must be a non-empty string");
314
+ if (throwOnMissing) {
315
+ throw new Error("Invalid Alpaca API key: must be a non-empty string");
316
+ }
317
+ console.warn("[AlpacaAPI] API key not configured. Market data features will be unavailable.");
318
+ return false;
310
319
  }
320
+ // Check for missing or empty API secret
311
321
  if (!auth.apiSecret ||
312
322
  typeof auth.apiSecret !== "string" ||
313
323
  auth.apiSecret.trim().length === 0) {
314
- throw new Error("Invalid Alpaca API secret: must be a non-empty string");
324
+ if (throwOnMissing) {
325
+ throw new Error("Invalid Alpaca API secret: must be a non-empty string");
326
+ }
327
+ console.warn("[AlpacaAPI] API secret not configured. Market data features will be unavailable.");
328
+ return false;
315
329
  }
316
330
  // Alpaca keys are typically 20+ characters
317
331
  if (auth.apiKey.length < 10) {
318
- throw new Error("Alpaca API key appears to be too short");
332
+ if (throwOnMissing) {
333
+ throw new Error("Alpaca API key appears to be too short");
334
+ }
335
+ console.warn("[AlpacaAPI] API key appears to be too short.");
336
+ return false;
319
337
  }
338
+ return true;
320
339
  }
321
340
  /**
322
341
  * Validates Polygon API key
@@ -12263,6 +12282,8 @@ class AlpacaMarketDataAPI extends EventEmitter {
12263
12282
  dataURL;
12264
12283
  apiURL;
12265
12284
  v1beta1url;
12285
+ /** Whether API credentials are valid and available. False during build time when env vars are missing. */
12286
+ credentialsValid = false;
12266
12287
  stockStreamUrl = getStockStreamUrl("PRODUCTION"); // production values
12267
12288
  optionStreamUrl = getOptionsStreamUrl("PRODUCTION"); // production values
12268
12289
  cryptoStreamUrl = getCryptoStreamUrl("PRODUCTION"); // production values
@@ -12318,13 +12339,16 @@ class AlpacaMarketDataAPI extends EventEmitter {
12318
12339
  constructor() {
12319
12340
  super();
12320
12341
  // Validate credentials from environment variables before initializing
12321
- const apiKey = process.env.ALPACA_API_KEY;
12322
- const apiSecret = process.env.ALPACA_SECRET_KEY;
12323
- validateAlpacaCredentials({
12342
+ // Use throwOnMissing: false to allow initialization during build time
12343
+ // when env vars are not available. Features will be unavailable until
12344
+ // credentials are provided at runtime.
12345
+ const apiKey = process.env.ALPACA_API_KEY || "";
12346
+ const apiSecret = process.env.ALPACA_SECRET_KEY || "";
12347
+ this.credentialsValid = validateAlpacaCredentials({
12324
12348
  apiKey,
12325
12349
  apiSecret,
12326
12350
  isPaper: process.env.ALPACA_ACCOUNT_TYPE === "PAPER",
12327
- });
12351
+ }, { throwOnMissing: false });
12328
12352
  this.dataURL = MARKET_DATA_API.STOCKS;
12329
12353
  this.apiURL =
12330
12354
  process.env.ALPACA_ACCOUNT_TYPE === "PAPER"