@@ -20,19 +20,13 @@ export type OnPageCallback = (
2020 disclosedAt : string
2121) => Promise < void > ;
2222
23- /**
24- * Formats a date as YYYYMMDD string.
25- */
2623function formatDateYYYYMMDD ( date : Date ) : string {
2724 const year = date . getFullYear ( ) ;
2825 const month = String ( date . getMonth ( ) + 1 ) . padStart ( 2 , "0" ) ;
2926 const day = String ( date . getDate ( ) ) . padStart ( 2 , "0" ) ;
3027 return `${ year } ${ month } ${ day } ` ;
3128}
3229
33- /**
34- * Builds request headers for Quiver API.
35- */
3630function buildHeaders ( ) : Record < string , string > {
3731 return {
3832 Accept : "application/json" ,
@@ -46,21 +40,16 @@ function buildHeaders(): Record<string, string> {
4640 } ;
4741}
4842
49- /**
50- * Handles HTTP error responses from Quiver API.
51- * @returns true if the error was handled and we should retry, false otherwise
52- */
5343async function handleErrorResponse (
5444 response : Response ,
5545 page : number ,
5646 errorText : string
5747) : Promise < boolean > {
5848 if ( response . status === 429 ) {
59- let waitSeconds = 30 ; // Default fallback
49+ let waitSeconds = 30 ;
6050 try {
6151 const errorJson = JSON . parse ( errorText ) as { detail ?: string } ;
6252 const detail = errorJson . detail || "" ;
63- // Extract "Expected available in X seconds" from the detail message
6453 const match = detail . match ( / E x p e c t e d a v a i l a b l e i n ( \d + ) \s * s e c o n d s ? / i) ;
6554 if ( match && match [ 1 ] ) {
6655 const parsedSeconds = parseInt ( match [ 1 ] , 10 ) ;
@@ -69,14 +58,14 @@ async function handleErrorResponse(
6958 }
7059 }
7160 } catch {
72- // If parsing fails, use default wait time
61+ // Use default wait time if parsing fails
7362 }
7463
7564 console . error (
7665 `[QUIVER] Page ${ page } : Rate limited (429). Waiting ${ waitSeconds } seconds before retry...`
7766 ) ;
7867 await sleep ( waitSeconds * 1000 ) ;
79- return true ; // Indicates we should retry
68+ return true ;
8069 }
8170
8271 if ( response . status === 500 ) {
@@ -94,14 +83,6 @@ async function handleErrorResponse(
9483 throw new Error ( `HTTP ${ response . status } ${ response . statusText } : ${ errorText } ` ) ;
9584}
9685
97- /**
98- * Fetches all congressional trades from Quiver Quant API.
99- * Uses the bulk congressional trading endpoint.
100- * See: https://api.quiverquant.com/docs/#/operations/beta_bulk_congresstrading_retrieve
101- *
102- * @param onPage Optional callback to process each page as it's fetched (for memory efficiency)
103- * @returns Promise with total trades fetched and pages fetched
104- */
10586export async function fetchCongressionalTradesPaginated (
10687 onPage ?: OnPageCallback
10788) : Promise < FetchCongressionalTradesResult > {
@@ -116,7 +97,6 @@ export async function fetchCongressionalTradesPaginated(
11697 const pageSize = CONFIG . quiverPageSize || 100 ;
11798 const headers = buildHeaders ( ) ;
11899
119- // Use yesterday's date (ensures complete data for the day)
120100 const targetDate = new Date ( ) ;
121101 targetDate . setDate ( targetDate . getDate ( ) - 1 ) ;
122102 const dateStr = formatDateYYYYMMDD ( targetDate ) ;
@@ -157,7 +137,6 @@ export async function fetchCongressionalTradesPaginated(
157137
158138 totalTradesFetched += data . length ;
159139
160- // Process page immediately if callback provided (memory efficient)
161140 if ( onPage ) {
162141 await onPage ( data , page , dateStr ) ;
163142 } else {
@@ -176,16 +155,13 @@ export async function fetchCongressionalTradesPaginated(
176155 `[QUIVER] Page ${ page } : Fetched ${ data . length } trades (${ totalTradesFetched } total)`
177156 ) ;
178157
179- // Add delay between requests to respect rate limits (1-3 seconds)
180158 await sleep ( Math . floor ( Math . random ( ) * 2000 ) + 1000 ) ;
181159 } catch ( error ) {
182160 const errorMessage = error instanceof Error ? error . message : String ( error ) ;
183- // If it's a 404 or empty response, we've reached the end
184161 if ( errorMessage . includes ( "404" ) || errorMessage . includes ( "No data" ) ) {
185162 console . error ( `[QUIVER] Page ${ page } : No more data available, stopping pagination` ) ;
186163 break ;
187164 }
188- // For other errors, log and stop (could be rate limit or API issue)
189165 console . error ( `[QUIVER] Page ${ page } : Error fetching data: ${ errorMessage } ` ) ;
190166 throw error ;
191167 }
0 commit comments