-
Notifications
You must be signed in to change notification settings - Fork 855
Expand file tree
/
Copy pathfetch-npm-stat-data.ts
More file actions
64 lines (50 loc) · 1.97 KB
/
Copy pathfetch-npm-stat-data.ts
File metadata and controls
64 lines (50 loc) · 1.97 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
56
57
58
59
60
61
62
63
64
import { fetch } from 'bun';
import { sleep, REQUEST_SLEEP } from './helpers';
const ATTEMPTS_LIMIT = 2;
export async function fetchNpmStatDataBulk(namesArray: string[], attemptsCount = 0) {
try {
const url = urlForPackages(namesArray.join('&package='));
const response = await fetch(url);
const downloadData = await response.json();
return namesArray.map(name => {
const pkgData = downloadData[name];
if (pkgData && Object.keys(pkgData).length <= 0) {
console.warn(
`[npm-stat] ${name} doesn't exist on npm registry, add npmPkg to its entry or remove it!`
);
return { npm: null };
}
const downloadCountsPerDay = Object.values(pkgData);
return {
name,
npm: {
downloads: downloadCountsPerDay.reduce((sum: number, value: number) => sum + value, 0),
weekDownloads: downloadCountsPerDay
.slice(0, 6)
.reduce((sum: number, value: number) => sum + value, 0),
},
};
});
} catch (error) {
if (attemptsCount >= ATTEMPTS_LIMIT) {
console.error('[npm-stat] Looks like we have reach the npm-stat API rate limit!');
console.error(error);
return namesArray.map(name => ({ name, npm: null }));
}
await sleep(REQUEST_SLEEP, REQUEST_SLEEP * 2);
console.log(`[npm-stat] Retrying fetch for ${namesArray} (${attemptsCount + 1})`);
return await fetchNpmStatDataBulk(namesArray, attemptsCount + 1);
}
}
function formattedDate(date: Date) {
const year = date.getFullYear();
const month = String(date.getMonth() + 1).padStart(2, '0');
const day = String(date.getDate()).padStart(2, '0');
return `${year}-${month}-${day}`;
}
function urlForPackages(packagesList: string) {
const now = new Date();
const fromDate = new Date(now);
fromDate.setMonth(fromDate.getMonth() - 1);
return `https://npm-stat.com/api/download-counts?package=${packagesList}&from=${formattedDate(fromDate)}&until=${formattedDate(now)}`;
}