-
Notifications
You must be signed in to change notification settings - Fork 0
/
github.js
34 lines (30 loc) · 1.31 KB
/
github.js
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
class GitHubAPI {
constructor() {
this.date = new Date();
this.todaysDateMinusOne = new Date(this.date.getFullYear(), this.date.getMonth(), this.date.getDate() - 1);
this.todaysDateMinusSeven = new Date(this.date.getFullYear(), this.date.getMonth(), this.date.getDate() - 7);
}
formatDate(date) {
return `${date.getFullYear()}-${(date.getMonth() + 1).toString().padStart(2, '0')}-${date.getDate().toString().padStart(2, '0')}`;
}
async getMostTrendingRepoDaily() {
try {
const response = await fetch(`https://api.github.com/search/repositories?q=created:>${this.formatDate(this.todaysDateMinusOne)}&sort=stars&order=desc`);
const data = await response.json();
return data.items;
} catch (error) {
console.error('Error fetching data:', error);
}
}
async getMostTrendingRepoWeekly() {
try {
const response = await fetch(`https://api.github.com/search/repositories?q=created:>${this.formatDate(this.todaysDateMinusSeven)}&sort=stars&order=desc`);
const data = await response.json();
return data.items;
} catch (error) {
console.error('Error fetching data:', error);
}
}
}
const gitHubAPI = new GitHubAPI();
export default gitHubAPI;