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