1- import { setTimeout } from 'node:timers/promises'
21import { CACHE_MAX_AGE_ONE_HOUR } from '#shared/utils/constants'
32
4- const GITHUB_HEADERS = {
5- 'Accept' : 'application/vnd.github.v3+json' ,
6- 'User-Agent' : 'npmx' ,
7- 'X-GitHub-Api-Version' : '2022-11-28' ,
8- } as const
9-
103interface GitHubSearchResponse {
114 total_count : number
125}
@@ -32,50 +25,23 @@ export default defineCachedEventHandler(
3225 const query = `repo:${ owner } /${ repo } is:issue is:open`
3326 const url = `https://api.github.com/search/issues?q=${ encodeURIComponent ( query ) } &per_page=1`
3427
35- const maxAttempts = 3
36- let delayMs = 1000
37-
38- for ( let attempt = 0 ; attempt < maxAttempts ; attempt += 1 ) {
39- try {
40- const response = await $fetch . raw < GitHubSearchResponse > ( url , {
41- headers : GITHUB_HEADERS ,
42- timeout : 10000 ,
43- } )
44-
45- if ( response . status === 200 ) {
46- return {
47- owner,
48- repo,
49- issues :
50- typeof response . _data ?. total_count === 'number' ? response . _data . total_count : null ,
51- }
52- }
53-
54- if ( response . status === 202 ) {
55- if ( attempt === maxAttempts - 1 ) break
56- await setTimeout ( delayMs )
57- delayMs = Math . min ( delayMs * 2 , 16_000 )
58- continue
59- }
28+ try {
29+ const data = await fetchGitHubWithRetries < GitHubSearchResponse > ( url , {
30+ maxAttempts : 3 ,
31+ timeout : 10000 ,
32+ } )
6033
61- break
62- } catch ( error : any ) {
63- if ( attempt === maxAttempts - 1 ) {
64- throw createError ( {
65- statusCode : error . response ?. status || 500 ,
66- statusMessage :
67- error . response ?. _data ?. message || 'Failed to fetch issue count from GitHub' ,
68- } )
69- }
70- await setTimeout ( delayMs )
71- delayMs = Math . min ( delayMs * 2 , 16_000 )
34+ return {
35+ owner,
36+ repo,
37+ issues : typeof data ?. total_count === 'number' ? data . total_count : null ,
7238 }
39+ } catch {
40+ throw createError ( {
41+ statusCode : 500 ,
42+ statusMessage : 'Failed to fetch issue count from GitHub' ,
43+ } )
7344 }
74-
75- throw createError ( {
76- statusCode : 500 ,
77- statusMessage : 'Failed to fetch issue count from GitHub after retries' ,
78- } )
7945 } ,
8046 {
8147 maxAge : CACHE_MAX_AGE_ONE_HOUR ,
0 commit comments