MCPcopy Create free account
hub / github.com/anomalyco/opencode / fetchNpmDownloads

Function fetchNpmDownloads

script/stats.ts:52–68  ·  view source on GitHub ↗
(packageName: string)

Source from the content-addressed store, hash-verified

50}
51
52async function fetchNpmDownloads(packageName: string): Promise<number> {
53 try {
54 // Use a range from 2020 to current year + 5 years to ensure it works forever
55 const currentYear = new Date().getFullYear()
56 const endYear = currentYear + 5
57 const response = await fetch(`https://api.npmjs.org/downloads/range/2020-01-01:${endYear}-12-31/${packageName}`)
58 if (!response.ok) {
59 console.warn(`Failed to fetch npm downloads for ${packageName}: ${response.status}`)
60 return 0
61 }
62 const data: NpmDownloadsRange = await response.json()
63 return data.downloads.reduce((total, day) => total + day.downloads, 0)
64 } catch (error) {
65 console.warn(`Error fetching npm downloads for ${packageName}:`, error)
66 return 0
67 }
68}
69
70async function fetchReleases(): Promise<Release[]> {
71 const releases: Release[] = []

Callers 1

stats.tsFile · 0.85

Calls 2

jsonMethod · 0.65
fetchFunction · 0.50

Tested by

no test coverage detected