@@ -12,22 +12,39 @@ async function jinaFetch(endpoint: string, url: string): Promise<string> {
1212
1313 const jinaApiEndpoint = `https://${ endpoint } .jina.ai/${ url } `
1414 log . info ( `Fetching ${ jinaApiEndpoint } ` )
15- const response = await fetch ( jinaApiEndpoint , {
16- headers : {
17- Authorization : `Bearer ${ getJinaApiKey ( ) } `
18- }
19- } )
15+
16+ const headers = {
17+ Authorization : `Bearer ${ getJinaApiKey ( ) } ` ,
18+ 'X-Return-Format' : 'markdown' ,
19+
20+ }
21+
22+ let response = await fetch ( jinaApiEndpoint , { headers} )
23+ log . info ( { headers, success : response . ok , code : response . status } )
24+
25+ // If we get a 403, retry once with X-No-Cache header
26+ if ( response . status === 403 ) {
27+ log . info ( 'Got 403, retrying with X-No-Cache header' )
28+ response = await fetch ( jinaApiEndpoint , {
29+ headers : {
30+ ...headers ,
31+ 'X-No-Cache' : 'true'
32+ }
33+ } )
34+ console . log ( { headers} )
35+ }
2036
2137 if ( ! response . ok ) {
2238 throw new Error ( `Jina API error: ${ response . statusText } ` )
2339 }
2440 log . info ( `fetched ${ jinaApiEndpoint } ` )
41+ log . info ( { headers, success : response . ok , code : response . status } )
2542
2643 return await response . text ( )
2744}
2845
2946export async function readPage ( url : string ) : Promise < string > {
30- return await jinaFetch ( 'r' , encodeURIComponent ( url ) )
47+ return await jinaFetch ( 'r' , url )
3148}
3249
3350export async function search ( searchTerm : string ) : Promise < string > {
0 commit comments