-
-
Notifications
You must be signed in to change notification settings - Fork 433
Expand file tree
/
Copy pathnpm-website-versions.ts
More file actions
37 lines (30 loc) · 954 Bytes
/
npm-website-versions.ts
File metadata and controls
37 lines (30 loc) · 954 Bytes
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
export interface NpmWebsiteVersionDownload {
version: string
downloads: number
}
interface NpmApiVersionDownloadsResponse {
downloads: Record<string, number>
}
export async function fetchNpmVersionDownloadsFromApi(
packageName: string,
): Promise<NpmWebsiteVersionDownload[]> {
const encodedName = encodePackageName(packageName)
const versionsResponse = await fetch(`https://api.npmjs.org/versions/${encodedName}/last-week`)
if (!versionsResponse.ok) {
if (versionsResponse.status === 404) {
throw createError({
statusCode: 404,
message: 'Package not found',
})
}
throw createError({
statusCode: 502,
message: 'Failed to fetch version download data from npm API',
})
}
const versionsData = (await versionsResponse.json()) as NpmApiVersionDownloadsResponse
return Object.entries(versionsData.downloads).map(([version, downloads]) => ({
version,
downloads,
}))
}