Skip to content

Commit bb74057

Browse files
mbabkerclaude
andcommitted
Use $fetch in Packagist downloads helper
Switches the Packagist stats request to Nitro's $fetch and surfaces status code, status message, request URL, and response body when a FetchError is caught. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent 8347b2d commit bb74057

1 file changed

Lines changed: 20 additions & 15 deletions

File tree

server/utils/packagist.ts

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,31 @@
1+
import { FetchError } from 'ofetch'
2+
13
export async function fetchPackagistDownloads(packageName: string) {
24
try {
3-
const response = await fetch(`https://packagist.org/packages/${packageName}/stats.json`, {
4-
headers: {
5-
'User-Agent': 'BabDev/2.0',
5+
const data = await $fetch<{ downloads?: { total?: number } }>(
6+
`https://packagist.org/packages/${packageName}/stats.json`,
7+
{
8+
headers: {
9+
'User-Agent': 'BabDev/2.0',
10+
},
611
},
7-
})
8-
9-
if (!response.ok) {
10-
console.error(
11-
`Failed to fetch Packagist data for ${packageName}: API responded with status code ${response.status}`,
12-
)
13-
14-
return null
15-
}
16-
17-
const data = await response.json()
12+
)
1813

1914
return {
2015
downloads: data.downloads?.total || 0,
2116
}
2217
} catch (error) {
23-
console.error(`Failed to fetch Packagist data for ${packageName}:`, error)
18+
if (error instanceof FetchError) {
19+
console.error(
20+
`Failed to fetch Packagist data for ${packageName}: ${error.statusCode ?? 'unknown'} ${error.statusMessage ?? error.message}`,
21+
{
22+
url: error.request,
23+
data: error.data,
24+
},
25+
)
26+
} else {
27+
console.error(`Failed to fetch Packagist data for ${packageName}:`, error)
28+
}
2429

2530
return null
2631
}

0 commit comments

Comments
 (0)