@adaptic/utils 0.1.44 → 0.1.45

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.
Files changed (225) hide show
  1. package/README.md +61 -153
  2. package/dist/index.cjs +5356 -60315
  3. package/dist/index.cjs.map +1 -1
  4. package/dist/index.mjs +5356 -60054
  5. package/dist/index.mjs.map +1 -1
  6. package/dist/test.js +361 -824
  7. package/dist/test.js.map +1 -1
  8. package/dist/types/adaptic.d.ts +2 -2
  9. package/dist/types/adaptic.d.ts.map +1 -1
  10. package/dist/types/alpaca-functions.d.ts +233 -0
  11. package/dist/types/alpaca-functions.d.ts.map +1 -0
  12. package/dist/types/alpaca-market-data-api.d.ts +9 -24
  13. package/dist/types/alpaca-market-data-api.d.ts.map +1 -1
  14. package/dist/types/alpaca-trading-api.d.ts +12 -12
  15. package/dist/types/alpaca-trading-api.d.ts.map +1 -1
  16. package/dist/types/alphavantage.d.ts +1 -1
  17. package/dist/types/alphavantage.d.ts.map +1 -1
  18. package/dist/types/asset-allocation-algorithm.d.ts +1 -7
  19. package/dist/types/asset-allocation-algorithm.d.ts.map +1 -1
  20. package/dist/types/cache/stampede-protected-cache.d.ts.map +1 -1
  21. package/dist/types/crypto.d.ts +4 -4
  22. package/dist/types/crypto.d.ts.map +1 -1
  23. package/dist/types/display-manager.d.ts +1 -1
  24. package/dist/types/display-manager.d.ts.map +1 -1
  25. package/dist/types/examples/asset-allocation-example.d.ts +6 -7
  26. package/dist/types/examples/asset-allocation-example.d.ts.map +1 -1
  27. package/dist/types/format-tools.d.ts.map +1 -1
  28. package/dist/types/index.d.ts +55 -393
  29. package/dist/types/index.d.ts.map +1 -1
  30. package/dist/types/logging.d.ts +1 -1
  31. package/dist/types/logging.d.ts.map +1 -1
  32. package/dist/types/market-hours.d.ts.map +1 -1
  33. package/dist/types/market-time.d.ts +13 -75
  34. package/dist/types/market-time.d.ts.map +1 -1
  35. package/dist/types/metrics-calcs.d.ts.map +1 -1
  36. package/dist/types/misc-utils.d.ts +1 -4
  37. package/dist/types/misc-utils.d.ts.map +1 -1
  38. package/dist/types/performance-metrics.d.ts +6 -6
  39. package/dist/types/performance-metrics.d.ts.map +1 -1
  40. package/dist/types/polygon-indices.d.ts +3 -3
  41. package/dist/types/polygon-indices.d.ts.map +1 -1
  42. package/dist/types/polygon.d.ts +1 -1
  43. package/dist/types/polygon.d.ts.map +1 -1
  44. package/dist/types/price-utils.d.ts.map +1 -1
  45. package/dist/types/technical-analysis.d.ts +9 -9
  46. package/dist/types/technical-analysis.d.ts.map +1 -1
  47. package/dist/types/time-utils.d.ts.map +1 -1
  48. package/dist/types/types/adaptic-types.d.ts +1 -1
  49. package/dist/types/types/adaptic-types.d.ts.map +1 -1
  50. package/dist/types/types/alpaca-types.d.ts +89 -252
  51. package/dist/types/types/alpaca-types.d.ts.map +1 -1
  52. package/dist/types/types/alphavantage-types.d.ts +2 -2
  53. package/dist/types/types/asset-allocation-types.d.ts +11 -11
  54. package/dist/types/types/index.d.ts +8 -8
  55. package/dist/types/types/index.d.ts.map +1 -1
  56. package/dist/types/types/logging-types.d.ts +2 -2
  57. package/dist/types/types/logging-types.d.ts.map +1 -1
  58. package/dist/types/types/market-time-types.d.ts +4 -4
  59. package/dist/types/types/market-time-types.d.ts.map +1 -1
  60. package/dist/types/types/metrics-types.d.ts +4 -4
  61. package/dist/types/types/metrics-types.d.ts.map +1 -1
  62. package/dist/types/types/polygon-indices-types.d.ts +6 -6
  63. package/dist/types/types/polygon-types.d.ts +3 -3
  64. package/dist/types/types/ta-types.d.ts +3 -3
  65. package/package.json +6 -24
  66. package/dist/types/__tests__/alpaca-functions.test.d.ts +0 -2
  67. package/dist/types/__tests__/alpaca-functions.test.d.ts.map +0 -1
  68. package/dist/types/__tests__/api-endpoints.test.d.ts +0 -2
  69. package/dist/types/__tests__/api-endpoints.test.d.ts.map +0 -1
  70. package/dist/types/__tests__/asset-allocation.test.d.ts +0 -2
  71. package/dist/types/__tests__/asset-allocation.test.d.ts.map +0 -1
  72. package/dist/types/__tests__/auth-validator.test.d.ts +0 -2
  73. package/dist/types/__tests__/auth-validator.test.d.ts.map +0 -1
  74. package/dist/types/__tests__/cache.test.d.ts +0 -2
  75. package/dist/types/__tests__/cache.test.d.ts.map +0 -1
  76. package/dist/types/__tests__/errors.test.d.ts +0 -2
  77. package/dist/types/__tests__/errors.test.d.ts.map +0 -1
  78. package/dist/types/__tests__/financial-regression.test.d.ts +0 -2
  79. package/dist/types/__tests__/financial-regression.test.d.ts.map +0 -1
  80. package/dist/types/__tests__/format-tools.test.d.ts +0 -2
  81. package/dist/types/__tests__/format-tools.test.d.ts.map +0 -1
  82. package/dist/types/__tests__/http-keep-alive.test.d.ts +0 -2
  83. package/dist/types/__tests__/http-keep-alive.test.d.ts.map +0 -1
  84. package/dist/types/__tests__/http-timeout.test.d.ts +0 -2
  85. package/dist/types/__tests__/http-timeout.test.d.ts.map +0 -1
  86. package/dist/types/__tests__/logger.test.d.ts +0 -2
  87. package/dist/types/__tests__/logger.test.d.ts.map +0 -1
  88. package/dist/types/__tests__/market-time.test.d.ts +0 -2
  89. package/dist/types/__tests__/market-time.test.d.ts.map +0 -1
  90. package/dist/types/__tests__/misc-utils.test.d.ts +0 -2
  91. package/dist/types/__tests__/misc-utils.test.d.ts.map +0 -1
  92. package/dist/types/__tests__/paginator.test.d.ts +0 -2
  93. package/dist/types/__tests__/paginator.test.d.ts.map +0 -1
  94. package/dist/types/__tests__/performance-metrics.test.d.ts +0 -2
  95. package/dist/types/__tests__/performance-metrics.test.d.ts.map +0 -1
  96. package/dist/types/__tests__/polygon.test.d.ts +0 -2
  97. package/dist/types/__tests__/polygon.test.d.ts.map +0 -1
  98. package/dist/types/__tests__/price-utils.test.d.ts +0 -2
  99. package/dist/types/__tests__/price-utils.test.d.ts.map +0 -1
  100. package/dist/types/__tests__/property-based-financial.test.d.ts +0 -2
  101. package/dist/types/__tests__/property-based-financial.test.d.ts.map +0 -1
  102. package/dist/types/__tests__/rate-limiter.test.d.ts +0 -2
  103. package/dist/types/__tests__/rate-limiter.test.d.ts.map +0 -1
  104. package/dist/types/__tests__/schema-validation.test.d.ts +0 -2
  105. package/dist/types/__tests__/schema-validation.test.d.ts.map +0 -1
  106. package/dist/types/__tests__/technical-analysis.test.d.ts +0 -2
  107. package/dist/types/__tests__/technical-analysis.test.d.ts.map +0 -1
  108. package/dist/types/__tests__/time-utils.test.d.ts +0 -2
  109. package/dist/types/__tests__/time-utils.test.d.ts.map +0 -1
  110. package/dist/types/alpaca/client.d.ts +0 -95
  111. package/dist/types/alpaca/client.d.ts.map +0 -1
  112. package/dist/types/alpaca/crypto/data.d.ts +0 -281
  113. package/dist/types/alpaca/crypto/data.d.ts.map +0 -1
  114. package/dist/types/alpaca/crypto/index.d.ts +0 -75
  115. package/dist/types/alpaca/crypto/index.d.ts.map +0 -1
  116. package/dist/types/alpaca/crypto/orders.d.ts +0 -221
  117. package/dist/types/alpaca/crypto/orders.d.ts.map +0 -1
  118. package/dist/types/alpaca/index.d.ts +0 -205
  119. package/dist/types/alpaca/index.d.ts.map +0 -1
  120. package/dist/types/alpaca/legacy/account.d.ts +0 -34
  121. package/dist/types/alpaca/legacy/account.d.ts.map +0 -1
  122. package/dist/types/alpaca/legacy/assets.d.ts +0 -13
  123. package/dist/types/alpaca/legacy/assets.d.ts.map +0 -1
  124. package/dist/types/alpaca/legacy/auth.d.ts +0 -18
  125. package/dist/types/alpaca/legacy/auth.d.ts.map +0 -1
  126. package/dist/types/alpaca/legacy/index.d.ts +0 -15
  127. package/dist/types/alpaca/legacy/index.d.ts.map +0 -1
  128. package/dist/types/alpaca/legacy/market-data.d.ts +0 -32
  129. package/dist/types/alpaca/legacy/market-data.d.ts.map +0 -1
  130. package/dist/types/alpaca/legacy/orders.d.ts +0 -84
  131. package/dist/types/alpaca/legacy/orders.d.ts.map +0 -1
  132. package/dist/types/alpaca/legacy/positions.d.ts +0 -66
  133. package/dist/types/alpaca/legacy/positions.d.ts.map +0 -1
  134. package/dist/types/alpaca/legacy/utils.d.ts +0 -18
  135. package/dist/types/alpaca/legacy/utils.d.ts.map +0 -1
  136. package/dist/types/alpaca/market-data/bars.d.ts +0 -142
  137. package/dist/types/alpaca/market-data/bars.d.ts.map +0 -1
  138. package/dist/types/alpaca/market-data/index.d.ts +0 -13
  139. package/dist/types/alpaca/market-data/index.d.ts.map +0 -1
  140. package/dist/types/alpaca/market-data/news.d.ts +0 -87
  141. package/dist/types/alpaca/market-data/news.d.ts.map +0 -1
  142. package/dist/types/alpaca/market-data/quotes.d.ts +0 -85
  143. package/dist/types/alpaca/market-data/quotes.d.ts.map +0 -1
  144. package/dist/types/alpaca/market-data/trades.d.ts +0 -98
  145. package/dist/types/alpaca/market-data/trades.d.ts.map +0 -1
  146. package/dist/types/alpaca/options/contracts.d.ts +0 -279
  147. package/dist/types/alpaca/options/contracts.d.ts.map +0 -1
  148. package/dist/types/alpaca/options/data.d.ts +0 -126
  149. package/dist/types/alpaca/options/data.d.ts.map +0 -1
  150. package/dist/types/alpaca/options/index.d.ts +0 -17
  151. package/dist/types/alpaca/options/index.d.ts.map +0 -1
  152. package/dist/types/alpaca/options/orders.d.ts +0 -366
  153. package/dist/types/alpaca/options/orders.d.ts.map +0 -1
  154. package/dist/types/alpaca/options/strategies.d.ts +0 -224
  155. package/dist/types/alpaca/options/strategies.d.ts.map +0 -1
  156. package/dist/types/alpaca/streams/base-stream.d.ts +0 -143
  157. package/dist/types/alpaca/streams/base-stream.d.ts.map +0 -1
  158. package/dist/types/alpaca/streams/crypto-stream.d.ts +0 -173
  159. package/dist/types/alpaca/streams/crypto-stream.d.ts.map +0 -1
  160. package/dist/types/alpaca/streams/index.d.ts +0 -54
  161. package/dist/types/alpaca/streams/index.d.ts.map +0 -1
  162. package/dist/types/alpaca/streams/option-stream.d.ts +0 -167
  163. package/dist/types/alpaca/streams/option-stream.d.ts.map +0 -1
  164. package/dist/types/alpaca/streams/stock-stream.d.ts +0 -176
  165. package/dist/types/alpaca/streams/stock-stream.d.ts.map +0 -1
  166. package/dist/types/alpaca/streams/stream-manager.d.ts +0 -277
  167. package/dist/types/alpaca/streams/stream-manager.d.ts.map +0 -1
  168. package/dist/types/alpaca/streams/trading-stream.d.ts +0 -186
  169. package/dist/types/alpaca/streams/trading-stream.d.ts.map +0 -1
  170. package/dist/types/alpaca/streams.d.ts +0 -88
  171. package/dist/types/alpaca/streams.d.ts.map +0 -1
  172. package/dist/types/alpaca/test-imports.d.ts +0 -7
  173. package/dist/types/alpaca/test-imports.d.ts.map +0 -1
  174. package/dist/types/alpaca/trading/account.d.ts +0 -198
  175. package/dist/types/alpaca/trading/account.d.ts.map +0 -1
  176. package/dist/types/alpaca/trading/bracket-orders.d.ts +0 -162
  177. package/dist/types/alpaca/trading/bracket-orders.d.ts.map +0 -1
  178. package/dist/types/alpaca/trading/clock.d.ts +0 -99
  179. package/dist/types/alpaca/trading/clock.d.ts.map +0 -1
  180. package/dist/types/alpaca/trading/index.d.ts +0 -15
  181. package/dist/types/alpaca/trading/index.d.ts.map +0 -1
  182. package/dist/types/alpaca/trading/oco-orders.d.ts +0 -203
  183. package/dist/types/alpaca/trading/oco-orders.d.ts.map +0 -1
  184. package/dist/types/alpaca/trading/order-utils.d.ts +0 -404
  185. package/dist/types/alpaca/trading/order-utils.d.ts.map +0 -1
  186. package/dist/types/alpaca/trading/orders.d.ts +0 -199
  187. package/dist/types/alpaca/trading/orders.d.ts.map +0 -1
  188. package/dist/types/alpaca/trading/oto-orders.d.ts +0 -282
  189. package/dist/types/alpaca/trading/oto-orders.d.ts.map +0 -1
  190. package/dist/types/alpaca/trading/positions.d.ts +0 -389
  191. package/dist/types/alpaca/trading/positions.d.ts.map +0 -1
  192. package/dist/types/alpaca/trading/smart-orders.d.ts +0 -301
  193. package/dist/types/alpaca/trading/smart-orders.d.ts.map +0 -1
  194. package/dist/types/alpaca/trading/trailing-stops.d.ts +0 -240
  195. package/dist/types/alpaca/trading/trailing-stops.d.ts.map +0 -1
  196. package/dist/types/config/api-endpoints.d.ts +0 -94
  197. package/dist/types/config/api-endpoints.d.ts.map +0 -1
  198. package/dist/types/errors/index.d.ts +0 -130
  199. package/dist/types/errors/index.d.ts.map +0 -1
  200. package/dist/types/examples/rate-limiter-example.d.ts +0 -7
  201. package/dist/types/examples/rate-limiter-example.d.ts.map +0 -1
  202. package/dist/types/http-timeout.d.ts +0 -37
  203. package/dist/types/http-timeout.d.ts.map +0 -1
  204. package/dist/types/logger.d.ts +0 -56
  205. package/dist/types/logger.d.ts.map +0 -1
  206. package/dist/types/rate-limiter.d.ts +0 -171
  207. package/dist/types/rate-limiter.d.ts.map +0 -1
  208. package/dist/types/schemas/alpaca-schemas.d.ts +0 -779
  209. package/dist/types/schemas/alpaca-schemas.d.ts.map +0 -1
  210. package/dist/types/schemas/alphavantage-schemas.d.ts +0 -255
  211. package/dist/types/schemas/alphavantage-schemas.d.ts.map +0 -1
  212. package/dist/types/schemas/index.d.ts +0 -21
  213. package/dist/types/schemas/index.d.ts.map +0 -1
  214. package/dist/types/schemas/polygon-schemas.d.ts +0 -551
  215. package/dist/types/schemas/polygon-schemas.d.ts.map +0 -1
  216. package/dist/types/schemas/validate-response.d.ts +0 -88
  217. package/dist/types/schemas/validate-response.d.ts.map +0 -1
  218. package/dist/types/utils/auth-validator.d.ts +0 -32
  219. package/dist/types/utils/auth-validator.d.ts.map +0 -1
  220. package/dist/types/utils/http-keep-alive.d.ts +0 -110
  221. package/dist/types/utils/http-keep-alive.d.ts.map +0 -1
  222. package/dist/types/utils/paginator.d.ts +0 -154
  223. package/dist/types/utils/paginator.d.ts.map +0 -1
  224. package/dist/types/utils/retry.d.ts +0 -78
  225. package/dist/types/utils/retry.d.ts.map +0 -1
@@ -1,110 +0,0 @@
1
- /**
2
- * HTTP connection pooling and keep-alive configuration.
3
- *
4
- * Node.js (>=20) uses undici as its built-in fetch implementation, which
5
- * automatically maintains an internal connection pool with keep-alive enabled
6
- * by default. This module provides:
7
- *
8
- * 1. Explicit documentation of the connection pooling behavior
9
- * 2. A configured HTTP Agent for use with node:http/node:https if needed
10
- * 3. Verification utilities to confirm keep-alive is active
11
- *
12
- * ## Connection Pooling Behavior
13
- *
14
- * **Native fetch (undici):** The global `fetch()` used throughout this package
15
- * uses Node.js built-in undici which maintains persistent connections by default.
16
- * Connections are kept alive and reused across requests to the same origin.
17
- * No additional configuration is needed for fetch-based API calls.
18
- *
19
- * **Alpaca SDK (@alpacahq/alpaca-trade-api):** The SDK uses its own internal
20
- * HTTP client with connection reuse. The SDK is configured through `AlpacaClient`
21
- * in `src/alpaca/client.ts` via the `createAlpacaClient()` factory which caches
22
- * client instances by API key + account type.
23
- *
24
- * **WebSocket connections:** Trading API and market data WebSocket connections
25
- * are long-lived persistent connections managed by `AlpacaTradingAPI` and
26
- * `AlpacaMarketDataAPI` respectively.
27
- *
28
- * ## API Client Summary
29
- *
30
- * | API Client | Transport | Keep-Alive | Connection Reuse |
31
- * |------------|-----------|------------|------------------|
32
- * | Alpaca Trading (direct) | fetch() | Built-in | Undici pool |
33
- * | Alpaca Market Data | fetch() | Built-in | Undici pool |
34
- * | Alpaca SDK | internal | Built-in | SDK managed |
35
- * | Polygon.io | fetch() via fetchWithRetry | Built-in | Undici pool |
36
- * | Alpha Vantage | fetch() | Built-in | Undici pool |
37
- * | WebSocket (Trading) | ws | Persistent | Long-lived |
38
- * | WebSocket (Market Data) | ws | Persistent | Long-lived |
39
- */
40
- import http from "http";
41
- import https from "https";
42
- /**
43
- * Default keep-alive configuration values.
44
- * These are applied when creating explicit HTTP agents for non-fetch use cases.
45
- */
46
- export declare const KEEP_ALIVE_DEFAULTS: {
47
- /** Enable keep-alive connections */
48
- readonly keepAlive: true;
49
- /** Initial delay before sending keep-alive probes (in milliseconds) */
50
- readonly keepAliveMsecs: 60000;
51
- /** Maximum number of sockets per host */
52
- readonly maxSockets: 50;
53
- /** Maximum total number of sockets */
54
- readonly maxTotalSockets: 256;
55
- /** Maximum number of free (idle) sockets per host */
56
- readonly maxFreeSockets: 10;
57
- /** Socket timeout for idle connections (in milliseconds) */
58
- readonly timeout: 60000;
59
- };
60
- /**
61
- * Pre-configured HTTP agent with keep-alive enabled.
62
- * Use this when making requests through node:http (not needed for fetch).
63
- */
64
- export declare const httpAgent: http.Agent;
65
- /**
66
- * Pre-configured HTTPS agent with keep-alive enabled.
67
- * Use this when making requests through node:https (not needed for fetch).
68
- */
69
- export declare const httpsAgent: https.Agent;
70
- /**
71
- * Connection pool status information
72
- */
73
- export interface ConnectionPoolStatus {
74
- /** Name of the HTTP agent */
75
- name: string;
76
- /** Whether keep-alive is enabled */
77
- keepAlive: boolean;
78
- /** Number of active sockets */
79
- activeSockets: number;
80
- /** Number of free (idle) sockets */
81
- freeSockets: number;
82
- /** Number of pending requests (waiting for a socket) */
83
- pendingRequests: number;
84
- /** Maximum sockets per host */
85
- maxSockets: number;
86
- /** Maximum free sockets per host */
87
- maxFreeSockets: number;
88
- }
89
- /**
90
- * Get the connection pool status for an HTTP/HTTPS agent.
91
- * Useful for monitoring and debugging connection reuse.
92
- *
93
- * @param agent - The HTTP/HTTPS agent to inspect
94
- * @param name - A human-readable name for the agent
95
- * @returns Connection pool status information
96
- */
97
- export declare function getAgentPoolStatus(agent: http.Agent | https.Agent, name?: string): ConnectionPoolStatus;
98
- /**
99
- * Verifies that keep-alive is properly configured for the Node.js built-in fetch.
100
- * Node.js >= 20 uses undici internally, which has keep-alive enabled by default.
101
- *
102
- * @returns Object indicating whether keep-alive is expected to be active
103
- */
104
- export declare function verifyFetchKeepAlive(): {
105
- supported: boolean;
106
- nodeVersion: string;
107
- undiciBuiltIn: boolean;
108
- keepAliveExpected: boolean;
109
- };
110
- //# sourceMappingURL=http-keep-alive.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"http-keep-alive.d.ts","sourceRoot":"","sources":["../../../src/utils/http-keep-alive.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAsCG;AAEH,OAAO,IAAI,MAAM,MAAM,CAAC;AACxB,OAAO,KAAK,MAAM,OAAO,CAAC;AAE1B;;;GAGG;AACH,eAAO,MAAM,mBAAmB;IAC9B,oCAAoC;;IAEpC,uEAAuE;;IAEvE,yCAAyC;;IAEzC,sCAAsC;;IAEtC,qDAAqD;;IAErD,4DAA4D;;CAEpD,CAAC;AAEX;;;GAGG;AACH,eAAO,MAAM,SAAS,YAOpB,CAAC;AAEH;;;GAGG;AACH,eAAO,MAAM,UAAU,aAOrB,CAAC;AAEH;;GAEG;AACH,MAAM,WAAW,oBAAoB;IACnC,6BAA6B;IAC7B,IAAI,EAAE,MAAM,CAAC;IACb,oCAAoC;IACpC,SAAS,EAAE,OAAO,CAAC;IACnB,+BAA+B;IAC/B,aAAa,EAAE,MAAM,CAAC;IACtB,oCAAoC;IACpC,WAAW,EAAE,MAAM,CAAC;IACpB,wDAAwD;IACxD,eAAe,EAAE,MAAM,CAAC;IACxB,+BAA+B;IAC/B,UAAU,EAAE,MAAM,CAAC;IACnB,oCAAoC;IACpC,cAAc,EAAE,MAAM,CAAC;CACxB;AAED;;;;;;;GAOG;AACH,wBAAgB,kBAAkB,CAChC,KAAK,EAAE,IAAI,CAAC,KAAK,GAAG,KAAK,CAAC,KAAK,EAC/B,IAAI,GAAE,MAAkB,GACvB,oBAAoB,CA4BtB;AAED;;;;;GAKG;AACH,wBAAgB,oBAAoB,IAAI;IACtC,SAAS,EAAE,OAAO,CAAC;IACnB,WAAW,EAAE,MAAM,CAAC;IACpB,aAAa,EAAE,OAAO,CAAC;IACvB,iBAAiB,EAAE,OAAO,CAAC;CAC5B,CAeA"}
@@ -1,154 +0,0 @@
1
- /**
2
- * Configuration for cursor-based pagination.
3
- * Used by Alpaca APIs that return `next_page_token`.
4
- */
5
- export interface CursorPaginationConfig<TItem, TResponse> {
6
- /** Pagination strategy type */
7
- type: "cursor";
8
- /**
9
- * Function that fetches a single page of results.
10
- * @param cursor - The cursor/page token for the next page (undefined for first page)
11
- * @returns The API response for that page
12
- */
13
- fetchPage: (cursor: string | undefined) => Promise<TResponse>;
14
- /**
15
- * Function that extracts items from the API response.
16
- * @param response - The raw API response
17
- * @returns Array of items from this page
18
- */
19
- getItems: (response: TResponse) => TItem[];
20
- /**
21
- * Function that extracts the next cursor/page token from the response.
22
- * Returns undefined/null/empty string when there are no more pages.
23
- * @param response - The raw API response
24
- * @returns The next page token, or a falsy value if no more pages
25
- */
26
- getNextCursor: (response: TResponse) => string | undefined | null;
27
- /** Maximum number of items to collect (optional; defaults to no limit) */
28
- maxItems?: number;
29
- /** Maximum number of pages to fetch (safety limit; defaults to 1000) */
30
- maxPages?: number;
31
- /** Label for logging purposes */
32
- label?: string;
33
- }
34
- /**
35
- * Configuration for URL-based pagination.
36
- * Used by Polygon APIs that return `next_url`.
37
- */
38
- export interface UrlPaginationConfig<TItem, TResponse> {
39
- /** Pagination strategy type */
40
- type: "url";
41
- /**
42
- * Function that fetches a page from a given URL.
43
- * For the first page, the URL is the initial request URL.
44
- * For subsequent pages, the URL comes from the previous response.
45
- * @param url - The URL to fetch
46
- * @returns The API response for that page
47
- */
48
- fetchPage: (url: string) => Promise<TResponse>;
49
- /** The initial URL for the first page */
50
- initialUrl: string;
51
- /**
52
- * Function that extracts items from the API response.
53
- * @param response - The raw API response
54
- * @returns Array of items from this page
55
- */
56
- getItems: (response: TResponse) => TItem[];
57
- /**
58
- * Function that extracts the next URL from the response.
59
- * Returns undefined/null/empty string when there are no more pages.
60
- * @param response - The raw API response
61
- * @returns The next page URL, or a falsy value if no more pages
62
- */
63
- getNextUrl: (response: TResponse) => string | undefined | null;
64
- /** Maximum number of items to collect (optional; defaults to no limit) */
65
- maxItems?: number;
66
- /** Maximum number of pages to fetch (safety limit; defaults to 1000) */
67
- maxPages?: number;
68
- /** Label for logging purposes */
69
- label?: string;
70
- }
71
- /**
72
- * Configuration for offset-based pagination.
73
- * Used by APIs that accept page/offset parameters.
74
- */
75
- export interface OffsetPaginationConfig<TItem, TResponse> {
76
- /** Pagination strategy type */
77
- type: "offset";
78
- /**
79
- * Function that fetches a page at a given offset.
80
- * @param offset - The current offset (starts at 0)
81
- * @param limit - The page size
82
- * @returns The API response for that page
83
- */
84
- fetchPage: (offset: number, limit: number) => Promise<TResponse>;
85
- /**
86
- * Function that extracts items from the API response.
87
- * @param response - The raw API response
88
- * @returns Array of items from this page
89
- */
90
- getItems: (response: TResponse) => TItem[];
91
- /** Number of items per page */
92
- pageSize: number;
93
- /**
94
- * Optional function to determine the total number of items.
95
- * If not provided, pagination stops when a page returns fewer items than pageSize.
96
- * @param response - The first API response
97
- * @returns Total number of items available
98
- */
99
- getTotalCount?: (response: TResponse) => number;
100
- /** Maximum number of items to collect (optional; defaults to no limit) */
101
- maxItems?: number;
102
- /** Maximum number of pages to fetch (safety limit; defaults to 1000) */
103
- maxPages?: number;
104
- /** Label for logging purposes */
105
- label?: string;
106
- }
107
- /** Union type of all pagination configurations */
108
- export type PaginationConfig<TItem, TResponse> = CursorPaginationConfig<TItem, TResponse> | UrlPaginationConfig<TItem, TResponse> | OffsetPaginationConfig<TItem, TResponse>;
109
- /**
110
- * Creates an async iterator that paginates through API results.
111
- * Yields individual items one at a time across all pages.
112
- *
113
- * @param config - Pagination configuration
114
- * @returns AsyncGenerator that yields items one at a time
115
- *
116
- * @example
117
- * ```typescript
118
- * // Cursor-based (Alpaca)
119
- * const iterator = paginate({
120
- * type: 'cursor',
121
- * fetchPage: (cursor) => alpacaAPI.getOrders({ page_token: cursor }),
122
- * getItems: (response) => response.orders,
123
- * getNextCursor: (response) => response.next_page_token,
124
- * label: 'getOrders',
125
- * });
126
- *
127
- * for await (const order of iterator) {
128
- * processOrder(order);
129
- * }
130
- * ```
131
- */
132
- export declare function paginate<TItem, TResponse>(config: PaginationConfig<TItem, TResponse>): AsyncGenerator<TItem, void, undefined>;
133
- /**
134
- * Collects all items from a paginated API into a single array.
135
- * This is a convenience wrapper around `paginate()`.
136
- *
137
- * @param config - Pagination configuration
138
- * @returns Array of all items collected across all pages
139
- *
140
- * @example
141
- * ```typescript
142
- * // Collect all orders
143
- * const allOrders = await paginateAll({
144
- * type: 'cursor',
145
- * fetchPage: (cursor) => alpacaAPI.getOrders({ page_token: cursor, limit: 100 }),
146
- * getItems: (response) => response,
147
- * getNextCursor: (response) => response.next_page_token,
148
- * maxItems: 500,
149
- * label: 'getAllOrders',
150
- * });
151
- * ```
152
- */
153
- export declare function paginateAll<TItem, TResponse>(config: PaginationConfig<TItem, TResponse>): Promise<TItem[]>;
154
- //# sourceMappingURL=paginator.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"paginator.d.ts","sourceRoot":"","sources":["../../../src/utils/paginator.ts"],"names":[],"mappings":"AAYA;;;GAGG;AACH,MAAM,WAAW,sBAAsB,CAAC,KAAK,EAAE,SAAS;IACtD,+BAA+B;IAC/B,IAAI,EAAE,QAAQ,CAAC;IACf;;;;OAIG;IACH,SAAS,EAAE,CAAC,MAAM,EAAE,MAAM,GAAG,SAAS,KAAK,OAAO,CAAC,SAAS,CAAC,CAAC;IAC9D;;;;OAIG;IACH,QAAQ,EAAE,CAAC,QAAQ,EAAE,SAAS,KAAK,KAAK,EAAE,CAAC;IAC3C;;;;;OAKG;IACH,aAAa,EAAE,CAAC,QAAQ,EAAE,SAAS,KAAK,MAAM,GAAG,SAAS,GAAG,IAAI,CAAC;IAClE,0EAA0E;IAC1E,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,wEAAwE;IACxE,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,iCAAiC;IACjC,KAAK,CAAC,EAAE,MAAM,CAAC;CAChB;AAED;;;GAGG;AACH,MAAM,WAAW,mBAAmB,CAAC,KAAK,EAAE,SAAS;IACnD,+BAA+B;IAC/B,IAAI,EAAE,KAAK,CAAC;IACZ;;;;;;OAMG;IACH,SAAS,EAAE,CAAC,GAAG,EAAE,MAAM,KAAK,OAAO,CAAC,SAAS,CAAC,CAAC;IAC/C,yCAAyC;IACzC,UAAU,EAAE,MAAM,CAAC;IACnB;;;;OAIG;IACH,QAAQ,EAAE,CAAC,QAAQ,EAAE,SAAS,KAAK,KAAK,EAAE,CAAC;IAC3C;;;;;OAKG;IACH,UAAU,EAAE,CAAC,QAAQ,EAAE,SAAS,KAAK,MAAM,GAAG,SAAS,GAAG,IAAI,CAAC;IAC/D,0EAA0E;IAC1E,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,wEAAwE;IACxE,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,iCAAiC;IACjC,KAAK,CAAC,EAAE,MAAM,CAAC;CAChB;AAED;;;GAGG;AACH,MAAM,WAAW,sBAAsB,CAAC,KAAK,EAAE,SAAS;IACtD,+BAA+B;IAC/B,IAAI,EAAE,QAAQ,CAAC;IACf;;;;;OAKG;IACH,SAAS,EAAE,CAAC,MAAM,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,KAAK,OAAO,CAAC,SAAS,CAAC,CAAC;IACjE;;;;OAIG;IACH,QAAQ,EAAE,CAAC,QAAQ,EAAE,SAAS,KAAK,KAAK,EAAE,CAAC;IAC3C,+BAA+B;IAC/B,QAAQ,EAAE,MAAM,CAAC;IACjB;;;;;OAKG;IACH,aAAa,CAAC,EAAE,CAAC,QAAQ,EAAE,SAAS,KAAK,MAAM,CAAC;IAChD,0EAA0E;IAC1E,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,wEAAwE;IACxE,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,iCAAiC;IACjC,KAAK,CAAC,EAAE,MAAM,CAAC;CAChB;AAED,kDAAkD;AAClD,MAAM,MAAM,gBAAgB,CAAC,KAAK,EAAE,SAAS,IACzC,sBAAsB,CAAC,KAAK,EAAE,SAAS,CAAC,GACxC,mBAAmB,CAAC,KAAK,EAAE,SAAS,CAAC,GACrC,sBAAsB,CAAC,KAAK,EAAE,SAAS,CAAC,CAAC;AAK7C;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,wBAAuB,QAAQ,CAAC,KAAK,EAAE,SAAS,EAC9C,MAAM,EAAE,gBAAgB,CAAC,KAAK,EAAE,SAAS,CAAC,GACzC,cAAc,CAAC,KAAK,EAAE,IAAI,EAAE,SAAS,CAAC,CA8FxC;AAED;;;;;;;;;;;;;;;;;;;GAmBG;AACH,wBAAsB,WAAW,CAAC,KAAK,EAAE,SAAS,EAChD,MAAM,EAAE,gBAAgB,CAAC,KAAK,EAAE,SAAS,CAAC,GACzC,OAAO,CAAC,KAAK,EAAE,CAAC,CAQlB"}
@@ -1,78 +0,0 @@
1
- /**
2
- * Retry utility with exponential backoff for handling transient errors in external API calls.
3
- *
4
- * Features:
5
- * - Configurable retry attempts and delays
6
- * - Exponential backoff with jitter to prevent thundering herd
7
- * - Respects Retry-After headers for rate limiting (429)
8
- * - Fail-fast for non-retryable errors (4xx client errors)
9
- * - Detailed error logging with context
10
- */
11
- export interface RetryConfig {
12
- /** Maximum number of retry attempts (default: 3) */
13
- maxRetries: number;
14
- /** Initial delay in milliseconds before first retry (default: 1000) */
15
- baseDelayMs: number;
16
- /** Maximum delay in milliseconds between retries (default: 30000) */
17
- maxDelayMs: number;
18
- /** HTTP status codes that should trigger a retry (default: [429, 500, 502, 503, 504]) */
19
- retryableStatusCodes: number[];
20
- /** Whether to retry on network errors (default: true) */
21
- retryOnNetworkError: boolean;
22
- /** Optional callback invoked on each retry attempt */
23
- onRetry?: (attempt: number, error: unknown) => void;
24
- }
25
- /**
26
- * Wraps an async function with retry logic and exponential backoff.
27
- *
28
- * This utility handles transient errors in external API calls by automatically retrying
29
- * failed requests with intelligent backoff strategies. It respects rate limit headers,
30
- * fails fast on non-retryable errors, and provides detailed logging.
31
- *
32
- * @template T - The return type of the wrapped function
33
- * @param fn - The async function to wrap with retry logic
34
- * @param config - Retry configuration (merged with defaults)
35
- * @param label - A descriptive label for logging (e.g., 'Polygon.fetchTickerInfo')
36
- * @returns A promise that resolves to the function's return value
37
- * @throws The last error encountered if all retries are exhausted
38
- *
39
- * @example
40
- * ```typescript
41
- * // Basic usage with defaults
42
- * const data = await withRetry(
43
- * async () => fetch('https://api.example.com/data'),
44
- * {},
45
- * 'ExampleAPI.fetchData'
46
- * );
47
- *
48
- * // Custom configuration for rate-limited API
49
- * const result = await withRetry(
50
- * async () => alphaVantageAPI.getQuote(symbol),
51
- * {
52
- * maxRetries: 5,
53
- * baseDelayMs: 5000,
54
- * maxDelayMs: 60000,
55
- * onRetry: (attempt, error) => {
56
- * getLogger().info(`Retry ${attempt} after error:`, error);
57
- * }
58
- * },
59
- * 'AlphaVantage.getQuote'
60
- * );
61
- * ```
62
- */
63
- export declare function withRetry<T>(fn: () => Promise<T>, config?: Partial<RetryConfig>, label?: string): Promise<T>;
64
- /**
65
- * API-specific retry configurations for different external services.
66
- * These configurations are tuned based on each API's rate limits and characteristics.
67
- */
68
- export declare const API_RETRY_CONFIGS: {
69
- /** Polygon.io API - 5 requests/second rate limit */
70
- POLYGON: Partial<RetryConfig>;
71
- /** Alpha Vantage API - 5 requests/minute rate limit (more strict) */
72
- ALPHA_VANTAGE: Partial<RetryConfig>;
73
- /** Alpaca API - generally reliable, shorter retry window */
74
- ALPACA: Partial<RetryConfig>;
75
- /** Generic crypto API configuration */
76
- CRYPTO: Partial<RetryConfig>;
77
- };
78
- //# sourceMappingURL=retry.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"retry.d.ts","sourceRoot":"","sources":["../../../src/utils/retry.ts"],"names":[],"mappings":"AAEA;;;;;;;;;GASG;AAEH,MAAM,WAAW,WAAW;IAC1B,oDAAoD;IACpD,UAAU,EAAE,MAAM,CAAC;IACnB,uEAAuE;IACvE,WAAW,EAAE,MAAM,CAAC;IACpB,qEAAqE;IACrE,UAAU,EAAE,MAAM,CAAC;IACnB,yFAAyF;IACzF,oBAAoB,EAAE,MAAM,EAAE,CAAC;IAC/B,yDAAyD;IACzD,mBAAmB,EAAE,OAAO,CAAC;IAC7B,sDAAsD;IACtD,OAAO,CAAC,EAAE,CAAC,OAAO,EAAE,MAAM,EAAE,KAAK,EAAE,OAAO,KAAK,IAAI,CAAC;CACrD;AA4LD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAqCG;AACH,wBAAsB,SAAS,CAAC,CAAC,EAC/B,EAAE,EAAE,MAAM,OAAO,CAAC,CAAC,CAAC,EACpB,MAAM,GAAE,OAAO,CAAC,WAAW,CAAM,EACjC,KAAK,GAAE,MAAkB,GACxB,OAAO,CAAC,CAAC,CAAC,CAmGZ;AAED;;;GAGG;AACH,eAAO,MAAM,iBAAiB;IAC5B,oDAAoD;aAO/C,OAAO,CAAC,WAAW,CAAC;IAEzB,qEAAqE;mBAOhE,OAAO,CAAC,WAAW,CAAC;IAEzB,4DAA4D;YAOvD,OAAO,CAAC,WAAW,CAAC;IAEzB,uCAAuC;YAOlC,OAAO,CAAC,WAAW,CAAC;CAC1B,CAAC"}