Skip to content

Commit 0ce2b76

Browse files
Retry without cache if received anything in 403
1 parent ace7052 commit 0ce2b76

2 files changed

Lines changed: 23 additions & 7 deletions

File tree

deno.lock

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/utils/jina.ts

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -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

2946
export async function readPage(url: string): Promise<string> {
30-
return await jinaFetch('r', encodeURIComponent(url))
47+
return await jinaFetch('r', url)
3148
}
3249

3350
export async function search(searchTerm: string): Promise<string> {

0 commit comments

Comments
 (0)