-
-
Notifications
You must be signed in to change notification settings - Fork 456
Expand file tree
/
Copy path[repo].get.ts
More file actions
55 lines (48 loc) · 1.37 KB
/
[repo].get.ts
File metadata and controls
55 lines (48 loc) · 1.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import { CACHE_MAX_AGE_ONE_HOUR } from '#shared/utils/constants'
interface GitHubSearchResponse {
total_count: number
}
export interface GithubIssueCountResponse {
owner: string
repo: string
issues: number | null
}
export default defineCachedEventHandler(
async (event): Promise<GithubIssueCountResponse> => {
const owner = getRouterParam(event, 'owner')
const repo = getRouterParam(event, 'repo')
if (!owner || !repo) {
throw createError({
statusCode: 400,
statusMessage: 'Owner and repo are required parameters.',
})
}
const query = `repo:${owner}/${repo} is:issue is:open`
const url = `https://api.github.com/search/issues?q=${encodeURIComponent(query)}&per_page=1`
try {
const data = await fetchGitHubWithRetries<GitHubSearchResponse>(url, {
timeout: 10000,
})
return {
owner,
repo,
issues: typeof data?.total_count === 'number' ? data.total_count : null,
}
} catch {
throw createError({
statusCode: 500,
statusMessage: 'Failed to fetch issue count from GitHub',
})
}
},
{
maxAge: CACHE_MAX_AGE_ONE_HOUR,
swr: true,
name: 'github-issue-count',
getKey: event => {
const owner = getRouterParam(event, 'owner')
const repo = getRouterParam(event, 'repo')
return `${owner}/${repo}`
},
},
)