@adaptic/utils 0.0.99

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 (238) hide show
  1. package/README.md +916 -0
  2. package/dist/index.cjs +71971 -0
  3. package/dist/index.cjs.map +1 -0
  4. package/dist/index.mjs +71677 -0
  5. package/dist/index.mjs.map +1 -0
  6. package/dist/test.js +7799 -0
  7. package/dist/test.js.map +1 -0
  8. package/dist/types/__tests__/alpaca-functions.test.d.ts +2 -0
  9. package/dist/types/__tests__/alpaca-functions.test.d.ts.map +1 -0
  10. package/dist/types/__tests__/api-endpoints.test.d.ts +2 -0
  11. package/dist/types/__tests__/api-endpoints.test.d.ts.map +1 -0
  12. package/dist/types/__tests__/asset-allocation.test.d.ts +2 -0
  13. package/dist/types/__tests__/asset-allocation.test.d.ts.map +1 -0
  14. package/dist/types/__tests__/auth-validator.test.d.ts +2 -0
  15. package/dist/types/__tests__/auth-validator.test.d.ts.map +1 -0
  16. package/dist/types/__tests__/cache.test.d.ts +2 -0
  17. package/dist/types/__tests__/cache.test.d.ts.map +1 -0
  18. package/dist/types/__tests__/errors.test.d.ts +2 -0
  19. package/dist/types/__tests__/errors.test.d.ts.map +1 -0
  20. package/dist/types/__tests__/financial-regression.test.d.ts +2 -0
  21. package/dist/types/__tests__/financial-regression.test.d.ts.map +1 -0
  22. package/dist/types/__tests__/format-tools.test.d.ts +2 -0
  23. package/dist/types/__tests__/format-tools.test.d.ts.map +1 -0
  24. package/dist/types/__tests__/http-keep-alive.test.d.ts +2 -0
  25. package/dist/types/__tests__/http-keep-alive.test.d.ts.map +1 -0
  26. package/dist/types/__tests__/http-timeout.test.d.ts +2 -0
  27. package/dist/types/__tests__/http-timeout.test.d.ts.map +1 -0
  28. package/dist/types/__tests__/logger.test.d.ts +2 -0
  29. package/dist/types/__tests__/logger.test.d.ts.map +1 -0
  30. package/dist/types/__tests__/market-time.test.d.ts +2 -0
  31. package/dist/types/__tests__/market-time.test.d.ts.map +1 -0
  32. package/dist/types/__tests__/misc-utils.test.d.ts +2 -0
  33. package/dist/types/__tests__/misc-utils.test.d.ts.map +1 -0
  34. package/dist/types/__tests__/paginator.test.d.ts +2 -0
  35. package/dist/types/__tests__/paginator.test.d.ts.map +1 -0
  36. package/dist/types/__tests__/performance-metrics.test.d.ts +2 -0
  37. package/dist/types/__tests__/performance-metrics.test.d.ts.map +1 -0
  38. package/dist/types/__tests__/polygon.test.d.ts +2 -0
  39. package/dist/types/__tests__/polygon.test.d.ts.map +1 -0
  40. package/dist/types/__tests__/price-utils.test.d.ts +2 -0
  41. package/dist/types/__tests__/price-utils.test.d.ts.map +1 -0
  42. package/dist/types/__tests__/property-based-financial.test.d.ts +2 -0
  43. package/dist/types/__tests__/property-based-financial.test.d.ts.map +1 -0
  44. package/dist/types/__tests__/rate-limiter.test.d.ts +2 -0
  45. package/dist/types/__tests__/rate-limiter.test.d.ts.map +1 -0
  46. package/dist/types/__tests__/schema-validation.test.d.ts +2 -0
  47. package/dist/types/__tests__/schema-validation.test.d.ts.map +1 -0
  48. package/dist/types/__tests__/technical-analysis.test.d.ts +2 -0
  49. package/dist/types/__tests__/technical-analysis.test.d.ts.map +1 -0
  50. package/dist/types/__tests__/time-utils.test.d.ts +2 -0
  51. package/dist/types/__tests__/time-utils.test.d.ts.map +1 -0
  52. package/dist/types/adaptic.d.ts +52 -0
  53. package/dist/types/adaptic.d.ts.map +1 -0
  54. package/dist/types/alpaca/client.d.ts +95 -0
  55. package/dist/types/alpaca/client.d.ts.map +1 -0
  56. package/dist/types/alpaca/crypto/data.d.ts +281 -0
  57. package/dist/types/alpaca/crypto/data.d.ts.map +1 -0
  58. package/dist/types/alpaca/crypto/index.d.ts +75 -0
  59. package/dist/types/alpaca/crypto/index.d.ts.map +1 -0
  60. package/dist/types/alpaca/crypto/orders.d.ts +221 -0
  61. package/dist/types/alpaca/crypto/orders.d.ts.map +1 -0
  62. package/dist/types/alpaca/index.d.ts +205 -0
  63. package/dist/types/alpaca/index.d.ts.map +1 -0
  64. package/dist/types/alpaca/legacy/account.d.ts +34 -0
  65. package/dist/types/alpaca/legacy/account.d.ts.map +1 -0
  66. package/dist/types/alpaca/legacy/assets.d.ts +13 -0
  67. package/dist/types/alpaca/legacy/assets.d.ts.map +1 -0
  68. package/dist/types/alpaca/legacy/auth.d.ts +18 -0
  69. package/dist/types/alpaca/legacy/auth.d.ts.map +1 -0
  70. package/dist/types/alpaca/legacy/index.d.ts +15 -0
  71. package/dist/types/alpaca/legacy/index.d.ts.map +1 -0
  72. package/dist/types/alpaca/legacy/market-data.d.ts +32 -0
  73. package/dist/types/alpaca/legacy/market-data.d.ts.map +1 -0
  74. package/dist/types/alpaca/legacy/orders.d.ts +84 -0
  75. package/dist/types/alpaca/legacy/orders.d.ts.map +1 -0
  76. package/dist/types/alpaca/legacy/positions.d.ts +66 -0
  77. package/dist/types/alpaca/legacy/positions.d.ts.map +1 -0
  78. package/dist/types/alpaca/legacy/utils.d.ts +18 -0
  79. package/dist/types/alpaca/legacy/utils.d.ts.map +1 -0
  80. package/dist/types/alpaca/market-data/bars.d.ts +142 -0
  81. package/dist/types/alpaca/market-data/bars.d.ts.map +1 -0
  82. package/dist/types/alpaca/market-data/index.d.ts +13 -0
  83. package/dist/types/alpaca/market-data/index.d.ts.map +1 -0
  84. package/dist/types/alpaca/market-data/news.d.ts +87 -0
  85. package/dist/types/alpaca/market-data/news.d.ts.map +1 -0
  86. package/dist/types/alpaca/market-data/quotes.d.ts +85 -0
  87. package/dist/types/alpaca/market-data/quotes.d.ts.map +1 -0
  88. package/dist/types/alpaca/market-data/trades.d.ts +98 -0
  89. package/dist/types/alpaca/market-data/trades.d.ts.map +1 -0
  90. package/dist/types/alpaca/options/contracts.d.ts +279 -0
  91. package/dist/types/alpaca/options/contracts.d.ts.map +1 -0
  92. package/dist/types/alpaca/options/data.d.ts +126 -0
  93. package/dist/types/alpaca/options/data.d.ts.map +1 -0
  94. package/dist/types/alpaca/options/index.d.ts +17 -0
  95. package/dist/types/alpaca/options/index.d.ts.map +1 -0
  96. package/dist/types/alpaca/options/orders.d.ts +366 -0
  97. package/dist/types/alpaca/options/orders.d.ts.map +1 -0
  98. package/dist/types/alpaca/options/strategies.d.ts +224 -0
  99. package/dist/types/alpaca/options/strategies.d.ts.map +1 -0
  100. package/dist/types/alpaca/streams/base-stream.d.ts +143 -0
  101. package/dist/types/alpaca/streams/base-stream.d.ts.map +1 -0
  102. package/dist/types/alpaca/streams/crypto-stream.d.ts +173 -0
  103. package/dist/types/alpaca/streams/crypto-stream.d.ts.map +1 -0
  104. package/dist/types/alpaca/streams/index.d.ts +54 -0
  105. package/dist/types/alpaca/streams/index.d.ts.map +1 -0
  106. package/dist/types/alpaca/streams/option-stream.d.ts +167 -0
  107. package/dist/types/alpaca/streams/option-stream.d.ts.map +1 -0
  108. package/dist/types/alpaca/streams/stock-stream.d.ts +176 -0
  109. package/dist/types/alpaca/streams/stock-stream.d.ts.map +1 -0
  110. package/dist/types/alpaca/streams/stream-manager.d.ts +277 -0
  111. package/dist/types/alpaca/streams/stream-manager.d.ts.map +1 -0
  112. package/dist/types/alpaca/streams/trading-stream.d.ts +186 -0
  113. package/dist/types/alpaca/streams/trading-stream.d.ts.map +1 -0
  114. package/dist/types/alpaca/streams.d.ts +88 -0
  115. package/dist/types/alpaca/streams.d.ts.map +1 -0
  116. package/dist/types/alpaca/test-imports.d.ts +7 -0
  117. package/dist/types/alpaca/test-imports.d.ts.map +1 -0
  118. package/dist/types/alpaca/trading/account.d.ts +198 -0
  119. package/dist/types/alpaca/trading/account.d.ts.map +1 -0
  120. package/dist/types/alpaca/trading/bracket-orders.d.ts +162 -0
  121. package/dist/types/alpaca/trading/bracket-orders.d.ts.map +1 -0
  122. package/dist/types/alpaca/trading/clock.d.ts +99 -0
  123. package/dist/types/alpaca/trading/clock.d.ts.map +1 -0
  124. package/dist/types/alpaca/trading/index.d.ts +15 -0
  125. package/dist/types/alpaca/trading/index.d.ts.map +1 -0
  126. package/dist/types/alpaca/trading/oco-orders.d.ts +203 -0
  127. package/dist/types/alpaca/trading/oco-orders.d.ts.map +1 -0
  128. package/dist/types/alpaca/trading/order-utils.d.ts +404 -0
  129. package/dist/types/alpaca/trading/order-utils.d.ts.map +1 -0
  130. package/dist/types/alpaca/trading/orders.d.ts +199 -0
  131. package/dist/types/alpaca/trading/orders.d.ts.map +1 -0
  132. package/dist/types/alpaca/trading/oto-orders.d.ts +282 -0
  133. package/dist/types/alpaca/trading/oto-orders.d.ts.map +1 -0
  134. package/dist/types/alpaca/trading/positions.d.ts +389 -0
  135. package/dist/types/alpaca/trading/positions.d.ts.map +1 -0
  136. package/dist/types/alpaca/trading/smart-orders.d.ts +301 -0
  137. package/dist/types/alpaca/trading/smart-orders.d.ts.map +1 -0
  138. package/dist/types/alpaca/trading/trailing-stops.d.ts +240 -0
  139. package/dist/types/alpaca/trading/trailing-stops.d.ts.map +1 -0
  140. package/dist/types/alpaca-market-data-api.d.ts +386 -0
  141. package/dist/types/alpaca-market-data-api.d.ts.map +1 -0
  142. package/dist/types/alpaca-trading-api.d.ts +315 -0
  143. package/dist/types/alpaca-trading-api.d.ts.map +1 -0
  144. package/dist/types/alphavantage.d.ts +44 -0
  145. package/dist/types/alphavantage.d.ts.map +1 -0
  146. package/dist/types/asset-allocation-algorithm.d.ts +143 -0
  147. package/dist/types/asset-allocation-algorithm.d.ts.map +1 -0
  148. package/dist/types/cache/stampede-protected-cache.d.ts +565 -0
  149. package/dist/types/cache/stampede-protected-cache.d.ts.map +1 -0
  150. package/dist/types/config/api-endpoints.d.ts +94 -0
  151. package/dist/types/config/api-endpoints.d.ts.map +1 -0
  152. package/dist/types/crypto.d.ts +76 -0
  153. package/dist/types/crypto.d.ts.map +1 -0
  154. package/dist/types/display-manager.d.ts +24 -0
  155. package/dist/types/display-manager.d.ts.map +1 -0
  156. package/dist/types/errors/index.d.ts +130 -0
  157. package/dist/types/errors/index.d.ts.map +1 -0
  158. package/dist/types/examples/asset-allocation-example.d.ts +36 -0
  159. package/dist/types/examples/asset-allocation-example.d.ts.map +1 -0
  160. package/dist/types/examples/rate-limiter-example.d.ts +7 -0
  161. package/dist/types/examples/rate-limiter-example.d.ts.map +1 -0
  162. package/dist/types/format-tools.d.ts +54 -0
  163. package/dist/types/format-tools.d.ts.map +1 -0
  164. package/dist/types/http-timeout.d.ts +37 -0
  165. package/dist/types/http-timeout.d.ts.map +1 -0
  166. package/dist/types/index.d.ts +766 -0
  167. package/dist/types/index.d.ts.map +1 -0
  168. package/dist/types/logger.d.ts +56 -0
  169. package/dist/types/logger.d.ts.map +1 -0
  170. package/dist/types/logging.d.ts +12 -0
  171. package/dist/types/logging.d.ts.map +1 -0
  172. package/dist/types/market-hours.d.ts +24 -0
  173. package/dist/types/market-hours.d.ts.map +1 -0
  174. package/dist/types/market-time.d.ts +246 -0
  175. package/dist/types/market-time.d.ts.map +1 -0
  176. package/dist/types/metrics-calcs.d.ts +6 -0
  177. package/dist/types/metrics-calcs.d.ts.map +1 -0
  178. package/dist/types/misc-utils.d.ts +52 -0
  179. package/dist/types/misc-utils.d.ts.map +1 -0
  180. package/dist/types/performance-metrics.d.ts +88 -0
  181. package/dist/types/performance-metrics.d.ts.map +1 -0
  182. package/dist/types/polygon-indices.d.ts +85 -0
  183. package/dist/types/polygon-indices.d.ts.map +1 -0
  184. package/dist/types/polygon.d.ts +126 -0
  185. package/dist/types/polygon.d.ts.map +1 -0
  186. package/dist/types/price-utils.d.ts +26 -0
  187. package/dist/types/price-utils.d.ts.map +1 -0
  188. package/dist/types/rate-limiter.d.ts +171 -0
  189. package/dist/types/rate-limiter.d.ts.map +1 -0
  190. package/dist/types/schemas/alpaca-schemas.d.ts +779 -0
  191. package/dist/types/schemas/alpaca-schemas.d.ts.map +1 -0
  192. package/dist/types/schemas/alphavantage-schemas.d.ts +255 -0
  193. package/dist/types/schemas/alphavantage-schemas.d.ts.map +1 -0
  194. package/dist/types/schemas/index.d.ts +21 -0
  195. package/dist/types/schemas/index.d.ts.map +1 -0
  196. package/dist/types/schemas/polygon-schemas.d.ts +551 -0
  197. package/dist/types/schemas/polygon-schemas.d.ts.map +1 -0
  198. package/dist/types/schemas/validate-response.d.ts +88 -0
  199. package/dist/types/schemas/validate-response.d.ts.map +1 -0
  200. package/dist/types/technical-analysis.d.ts +90 -0
  201. package/dist/types/technical-analysis.d.ts.map +1 -0
  202. package/dist/types/test.d.ts +2 -0
  203. package/dist/types/test.d.ts.map +1 -0
  204. package/dist/types/testing/options-ws.d.ts +2 -0
  205. package/dist/types/testing/options-ws.d.ts.map +1 -0
  206. package/dist/types/time-utils.d.ts +17 -0
  207. package/dist/types/time-utils.d.ts.map +1 -0
  208. package/dist/types/types/adaptic-types.d.ts +11 -0
  209. package/dist/types/types/adaptic-types.d.ts.map +1 -0
  210. package/dist/types/types/alpaca-types.d.ts +1165 -0
  211. package/dist/types/types/alpaca-types.d.ts.map +1 -0
  212. package/dist/types/types/alphavantage-types.d.ts +66 -0
  213. package/dist/types/types/alphavantage-types.d.ts.map +1 -0
  214. package/dist/types/types/asset-allocation-types.d.ts +328 -0
  215. package/dist/types/types/asset-allocation-types.d.ts.map +1 -0
  216. package/dist/types/types/index.d.ts +21 -0
  217. package/dist/types/types/index.d.ts.map +1 -0
  218. package/dist/types/types/logging-types.d.ts +10 -0
  219. package/dist/types/types/logging-types.d.ts.map +1 -0
  220. package/dist/types/types/market-time-types.d.ts +59 -0
  221. package/dist/types/types/market-time-types.d.ts.map +1 -0
  222. package/dist/types/types/metrics-types.d.ts +33 -0
  223. package/dist/types/types/metrics-types.d.ts.map +1 -0
  224. package/dist/types/types/polygon-indices-types.d.ts +190 -0
  225. package/dist/types/types/polygon-indices-types.d.ts.map +1 -0
  226. package/dist/types/types/polygon-types.d.ts +204 -0
  227. package/dist/types/types/polygon-types.d.ts.map +1 -0
  228. package/dist/types/types/ta-types.d.ts +89 -0
  229. package/dist/types/types/ta-types.d.ts.map +1 -0
  230. package/dist/types/utils/auth-validator.d.ts +32 -0
  231. package/dist/types/utils/auth-validator.d.ts.map +1 -0
  232. package/dist/types/utils/http-keep-alive.d.ts +110 -0
  233. package/dist/types/utils/http-keep-alive.d.ts.map +1 -0
  234. package/dist/types/utils/paginator.d.ts +154 -0
  235. package/dist/types/utils/paginator.d.ts.map +1 -0
  236. package/dist/types/utils/retry.d.ts +78 -0
  237. package/dist/types/utils/retry.d.ts.map +1 -0
  238. package/package.json +78 -0
@@ -0,0 +1,154 @@
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
@@ -0,0 +1 @@
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"}
@@ -0,0 +1,78 @@
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
@@ -0,0 +1 @@
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"}
package/package.json ADDED
@@ -0,0 +1,78 @@
1
+ {
2
+ "name": "@adaptic/utils",
3
+ "version": "0.0.99",
4
+ "author": "Adaptic.ai",
5
+ "description": "Utility functions used in Adaptic app and Lambda functions",
6
+ "always-build-npm": true,
7
+ "main": "dist/index.cjs",
8
+ "module": "dist/index.mjs",
9
+ "types": "dist/types/index.d.ts",
10
+ "type": "module",
11
+ "exports": {
12
+ ".": {
13
+ "import": "./dist/index.mjs",
14
+ "require": "./dist/index.cjs",
15
+ "types": "./dist/types/index.d.ts"
16
+ }
17
+ },
18
+ "publishConfig": {
19
+ "tag": "stable"
20
+ },
21
+ "files": [
22
+ "dist"
23
+ ],
24
+ "scripts": {
25
+ "clean": "rm -rf dist",
26
+ "build": "npm run clean && rollup -c",
27
+ "prepare": "npm run build",
28
+ "prepublishOnly": "npm run build",
29
+ "test": "vitest run",
30
+ "test:watch": "vitest",
31
+ "test:coverage": "vitest run --coverage",
32
+ "test:legacy": "npm run build && node dist/test.js",
33
+ "build:analyze": "ANALYZE_BUNDLE=true npm run build",
34
+ "docs": "typedoc",
35
+ "docs:watch": "typedoc --watch",
36
+ "changelog": "conventional-changelog -p angular -i CHANGELOG.md -s"
37
+ },
38
+ "dependencies": {
39
+ "@adaptic/backend-legacy": "^0.0.59",
40
+ "@adaptic/lumic-utils": "^1.0.6",
41
+ "@alpacahq/alpaca-trade-api": "^3.1.3",
42
+ "@apollo/client": "^3.13.8",
43
+ "chalk": "^5.4.1",
44
+ "date-fns": "^4.1.0",
45
+ "date-fns-tz": "^3.2.0",
46
+ "lru-cache": "^11.2.2",
47
+ "ms": "^2.1.3",
48
+ "p-limit": "^6.2.0",
49
+ "zod": "^3.25.76"
50
+ },
51
+ "peerDependencies": {
52
+ "react": "^16.8.0 || ^17.0.0 || ^18.0.0 || ^19.0.0"
53
+ },
54
+ "license": "ISC",
55
+ "engines": {
56
+ "node": ">=20.0.0"
57
+ },
58
+ "devDependencies": {
59
+ "@rollup/plugin-commonjs": "^28.0.6",
60
+ "@rollup/plugin-json": "^6.1.0",
61
+ "@rollup/plugin-node-resolve": "^16.0.1",
62
+ "@rollup/plugin-typescript": "^12.1.4",
63
+ "@types/date-fns": "^2.6.3",
64
+ "@types/ms": "^2.1.0",
65
+ "@types/ws": "^8.18.1",
66
+ "@vitest/coverage-v8": "^4.0.18",
67
+ "conventional-changelog-cli": "^5.0.0",
68
+ "fast-check": "^4.5.3",
69
+ "lightweight-charts": "^5.0.8",
70
+ "react": "^19.1.0",
71
+ "rollup": "^4.46.2",
72
+ "rollup-cli": "^1.0.9",
73
+ "rollup-plugin-visualizer": "^6.0.5",
74
+ "typedoc": "^0.28.16",
75
+ "typescript": "^5.8.3",
76
+ "vitest": "^4.0.18"
77
+ }
78
+ }