@adaptic/utils 0.0.903 → 0.0.904

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
@@ -622,7 +622,7 @@ async function createOrder$1(auth, params) {
622
622
  try {
623
623
  const { APIKey, APISecret, type } = await validateAuth(auth);
624
624
  const apiBaseUrl = getTradingApiUrl(type);
625
- const response = await fetch(`${apiBaseUrl}/v2/orders`, {
625
+ const response = await fetch(`${apiBaseUrl}/orders`, {
626
626
  method: "POST",
627
627
  headers: {
628
628
  "APCA-API-KEY-ID": APIKey,
@@ -671,7 +671,7 @@ async function getOrders$1(auth, params = {}) {
671
671
  queryParams.append("symbols", params.symbols.join(","));
672
672
  if (params.side)
673
673
  queryParams.append("side", params.side);
674
- const response = await fetch(`${apiBaseUrl}/v2/orders?${queryParams}`, {
674
+ const response = await fetch(`${apiBaseUrl}/orders?${queryParams}`, {
675
675
  method: "GET",
676
676
  headers: {
677
677
  "APCA-API-KEY-ID": APIKey,
@@ -709,7 +709,7 @@ async function cancelAllOrders$1(auth) {
709
709
  try {
710
710
  const { APIKey, APISecret, type } = await validateAuth(auth);
711
711
  const apiBaseUrl = getTradingApiUrl(type);
712
- const response = await fetch(`${apiBaseUrl}/v2/orders`, {
712
+ const response = await fetch(`${apiBaseUrl}/orders`, {
713
713
  method: "DELETE",
714
714
  headers: {
715
715
  "APCA-API-KEY-ID": APIKey,
@@ -742,7 +742,7 @@ async function getOrder$1(auth, orderId, nested) {
742
742
  const queryParams = new URLSearchParams();
743
743
  if (nested)
744
744
  queryParams.append("nested", "true");
745
- const response = await fetch(`${apiBaseUrl}/v2/orders/${orderId}?${queryParams}`, {
745
+ const response = await fetch(`${apiBaseUrl}/orders/${orderId}?${queryParams}`, {
746
746
  method: "GET",
747
747
  headers: {
748
748
  "APCA-API-KEY-ID": APIKey,
@@ -772,7 +772,7 @@ async function replaceOrder$1(auth, orderId, params) {
772
772
  try {
773
773
  const { APIKey, APISecret, type } = await validateAuth(auth);
774
774
  const apiBaseUrl = getTradingApiUrl(type);
775
- const response = await fetch(`${apiBaseUrl}/v2/orders/${orderId}`, {
775
+ const response = await fetch(`${apiBaseUrl}/orders/${orderId}`, {
776
776
  method: "PATCH",
777
777
  headers: {
778
778
  "APCA-API-KEY-ID": APIKey,
@@ -804,7 +804,7 @@ async function cancelOrder$1(auth, orderId) {
804
804
  try {
805
805
  const { APIKey, APISecret, type } = await validateAuth(auth);
806
806
  const apiBaseUrl = getTradingApiUrl(type);
807
- const response = await fetch(`${apiBaseUrl}/v2/orders/${orderId}`, {
807
+ const response = await fetch(`${apiBaseUrl}/orders/${orderId}`, {
808
808
  method: "DELETE",
809
809
  headers: {
810
810
  "APCA-API-KEY-ID": APIKey,
@@ -863,7 +863,7 @@ async function createLimitOrder(auth, params = {
863
863
  body.client_order_id = client_order_id;
864
864
  }
865
865
  return makeRequest(auth, {
866
- endpoint: "/v2/orders",
866
+ endpoint: "/orders",
867
867
  method: "POST",
868
868
  body,
869
869
  });
@@ -1492,7 +1492,7 @@ async function fetchAllPositions(auth) {
1492
1492
  try {
1493
1493
  const { APIKey, APISecret, type } = await validateAuth(auth);
1494
1494
  const apiBaseUrl = getTradingApiUrl(type);
1495
- const apiUrl = `${apiBaseUrl}/v2/positions`;
1495
+ const apiUrl = `${apiBaseUrl}/positions`;
1496
1496
  const response = await fetch(apiUrl, {
1497
1497
  method: "GET",
1498
1498
  headers: {
@@ -1523,7 +1523,7 @@ async function fetchPosition(auth, symbolOrAssetId) {
1523
1523
  try {
1524
1524
  const { APIKey, APISecret, type } = await validateAuth(auth);
1525
1525
  const apiBaseUrl = getTradingApiUrl(type);
1526
- const response = await fetch(`${apiBaseUrl}/v2/positions/${symbolOrAssetId}`, {
1526
+ const response = await fetch(`${apiBaseUrl}/positions/${symbolOrAssetId}`, {
1527
1527
  method: "GET",
1528
1528
  headers: {
1529
1529
  "APCA-API-KEY-ID": APIKey,
@@ -1640,7 +1640,7 @@ async function closePosition$1(auth, symbolOrAssetId, params) {
1640
1640
  queryParams.append("percentage", params.percentage.toString());
1641
1641
  }
1642
1642
  const queryString = queryParams.toString();
1643
- const url = `${apiBaseUrl}/v2/positions/${encodeURIComponent(symbolOrAssetId)}${queryString ? `?${queryString}` : ""}`;
1643
+ const url = `${apiBaseUrl}/positions/${encodeURIComponent(symbolOrAssetId)}${queryString ? `?${queryString}` : ""}`;
1644
1644
  const response = await fetch(url, {
1645
1645
  method: "DELETE",
1646
1646
  headers: {
@@ -1746,7 +1746,7 @@ async function closeAllPositions$1(auth, params = { cancel_orders: true, useLimi
1746
1746
  }
1747
1747
  else {
1748
1748
  const response = await makeRequest(auth, {
1749
- endpoint: "/v2/positions",
1749
+ endpoint: "/positions",
1750
1750
  method: "DELETE",
1751
1751
  queryString: cancel_orders ? "?cancel_orders=true" : "",
1752
1752
  });
@@ -1912,7 +1912,7 @@ async function fetchPortfolioHistory({ params, accountId, client, alpacaAccount,
1912
1912
  }
1913
1913
  const { APIKey, APISecret, type } = alpacaAccountObj;
1914
1914
  const apiBaseUrl = getTradingApiUrl(type);
1915
- const apiUrl = `${apiBaseUrl}/v2/account/portfolio/history`;
1915
+ const apiUrl = `${apiBaseUrl}/account/portfolio/history`;
1916
1916
  const { start, end, period } = params;
1917
1917
  // Validate date formats
1918
1918
  if (start) {
@@ -2219,7 +2219,7 @@ async function getAsset(auth, symbolOrAssetId) {
2219
2219
  const apiBaseUrl = getTradingApiUrl(type);
2220
2220
  // Use encodeURIComponent to handle special characters in symbols (e.g., BTC/USDT)
2221
2221
  const encodedSymbolOrAssetId = encodeURIComponent(symbolOrAssetId);
2222
- const response = await fetch(`${apiBaseUrl}/v2/assets/${encodedSymbolOrAssetId}`, {
2222
+ const response = await fetch(`${apiBaseUrl}/assets/${encodedSymbolOrAssetId}`, {
2223
2223
  method: "GET",
2224
2224
  headers: {
2225
2225
  "APCA-API-KEY-ID": APIKey,
@@ -5163,6 +5163,11 @@ function validateConcurrency(concurrency) {
5163
5163
  /**********************************************************************************
5164
5164
  * Polygon.io calls
5165
5165
  **********************************************************************************/
5166
+ /**
5167
+ * Set of Polygon API response statuses that indicate valid, usable data.
5168
+ * "OK" = real-time data, "DELAYED" = delayed data (still valid, e.g. free-tier plans).
5169
+ */
5170
+ const POLYGON_VALID_STATUSES = new Set(["OK", "DELAYED"]);
5166
5171
  // Constants from environment variables
5167
5172
  const POLYGON_API_KEY = process.env.POLYGON_API_KEY;
5168
5173
  // Define concurrency limits per API
@@ -5277,7 +5282,7 @@ const fetchLastTrade = async (symbol, options) => {
5277
5282
  try {
5278
5283
  const response = await fetchWithRetry(`${baseUrl}?${params.toString()}`, {}, 3, 1000);
5279
5284
  const data = await response.json();
5280
- if (data.status !== "OK" || !data.results) {
5285
+ if (!POLYGON_VALID_STATUSES.has(data.status) || !data.results) {
5281
5286
  throw new Error(`Polygon.io API error: ${data.status || "No results"} ${data.error || ""}`);
5282
5287
  }
5283
5288
  const { p: price, s: vol, t: timestamp } = data.results;
@@ -5349,9 +5354,12 @@ const fetchPrices = async (params, options) => {
5349
5354
  //getLogger().info(`Debug: Fetching ${nextUrl}`);
5350
5355
  const response = await fetchWithRetry(nextUrl, {}, 3, 1000);
5351
5356
  const data = await response.json();
5352
- if (data.status !== "OK") {
5357
+ if (!POLYGON_VALID_STATUSES.has(data.status)) {
5353
5358
  throw new Error(`Polygon.io API responded with status: ${data.status}`);
5354
5359
  }
5360
+ if (data.status === "DELAYED") {
5361
+ getLogger().warn(`Polygon.io returned DELAYED data for ${params.ticker} — using delayed results`, { ticker: params.ticker, source: "PolygonAPI.fetchPrices" });
5362
+ }
5355
5363
  if (data.results) {
5356
5364
  allResults = [...allResults, ...data.results];
5357
5365
  }
@@ -5463,7 +5471,7 @@ const fetchGroupedDaily = async (date, options) => {
5463
5471
  try {
5464
5472
  const response = await fetchWithRetry(`${baseUrl}?${params.toString()}`, {}, 3, 1000);
5465
5473
  const data = await response.json();
5466
- if (data.status !== "OK") {
5474
+ if (!POLYGON_VALID_STATUSES.has(data.status)) {
5467
5475
  throw new Error(`Polygon.io API responded with status: ${data.status}`);
5468
5476
  }
5469
5477
  return {
@@ -5554,7 +5562,7 @@ symbol, date = new Date(), options) => {
5554
5562
  return polygonLimit(async () => {
5555
5563
  const response = await fetchWithRetry(`${baseUrl}?${params.toString()}`, {}, 3, 1000);
5556
5564
  const data = await response.json();
5557
- if (data.status !== "OK") {
5565
+ if (!POLYGON_VALID_STATUSES.has(data.status)) {
5558
5566
  throw new Error(`Failed to fetch daily open/close data for ${symbol}: ${data.status}`);
5559
5567
  }
5560
5568
  return data;