-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.js
More file actions
52 lines (41 loc) · 1.43 KB
/
api.js
File metadata and controls
52 lines (41 loc) · 1.43 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
class GitHubAPI {
constructor() {
this.baseUrl = 'https://api.github.com';
this.cache = new Map();
this.cacheExpiry = 5 * 60 * 1000; // 5 minutes
}
async fetchWithCache(endpoint) {
const cacheKey = endpoint;
const cached = this.cache.get(cacheKey);
if (cached && Date.now() - cached.timestamp < this.cacheExpiry) {
return cached.data;
}
const response = await fetch(`${this.baseUrl}${endpoint}`);
if (!response.ok) {
throw new Error(`GitHub API Error: ${response.statusText}`);
}
const data = await response.json();
this.cache.set(cacheKey, {
data,
timestamp: Date.now()
});
return data;
}
async getUserStats(username) {
return this.fetchWithCache(`/users/${username}`);
}
async getRepoStats(username) {
return this.fetchWithCache(`/users/${username}/repos`);
}
async getLanguageStats(username, repoName) {
return this.fetchWithCache(`/repos/${username}/${repoName}/languages`);
}
async getContributions(username) {
return this.fetchWithCache(`/users/${username}/events`);
}
async getFollowers(username) {
return this.fetchWithCache(`/users/${username}/followers`);
}
}
// Create global API instance
const githubAPI = new GitHubAPI();