-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgithub-api.js
More file actions
114 lines (107 loc) · 3.69 KB
/
Copy pathgithub-api.js
File metadata and controls
114 lines (107 loc) · 3.69 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
// ✅ Utility: Handle API Errors
function handleAPIError(response) {
if (response.status === 404) {
throw new Error("User not found! Please check the username.");
} else if (response.status === 403) {
throw new Error("API rate limit exceeded. Please try again later.");
} else if (response.status === 429) {
throw new Error("Too many requests! Please wait a moment.");
} else if (response.status >= 500) {
throw new Error("Server error! GitHub API might be down.");
} else if (!response.ok) {
throw new Error(`Unexpected error: ${response.statusText}`);
}
}
// ✅ Utility: Format Date
function formatDate(dateString) {
const date = new Date(dateString);
return date.toLocaleDateString('en-US', {
year: 'numeric',
month: 'long',
day: 'numeric'
});
}
// ✅ 1️⃣ Get GitHub User Profile
async function getGitHubUser(username) {
try {
const response = await fetch(`https://api.github.com/users/${username}`, {
headers: { 'Accept': 'application/vnd.github+json' }
});
handleAPIError(response);
const data = await response.json();
return data;
} catch (error) {
console.error("❌ Error fetching user:", error.message);
throw error;
}
}
// ✅ 2️⃣ Get User Repositories
async function getUserRepositories(username) {
try {
const response = await fetch(`https://api.github.com/users/${username}/repos?per_page=100`, {
headers: { 'Accept': 'application/vnd.github+json' }
});
handleAPIError(response);
const data = await response.json();
return data;
} catch (error) {
console.error("❌ Error fetching repositories:", error.message);
throw error;
}
}
// ✅ 3️⃣ Get User Events
async function getUserEvents(username) {
try {
const response = await fetch(`https://api.github.com/users/${username}/events/public`, {
headers: { 'Accept': 'application/vnd.github+json' }
});
handleAPIError(response);
const data = await response.json();
return data.slice(0, 10); // limit for performance
} catch (error) {
console.error("❌ Error fetching events:", error.message);
throw error;
}
}
// ✅ 4️⃣ Get User Followers
async function getUserFollowers(username) {
try {
const response = await fetch(`https://api.github.com/users/${username}/followers`, {
headers: { 'Accept': 'application/vnd.github+json' }
});
handleAPIError(response);
const data = await response.json();
return data;
} catch (error) {
console.error("❌ Error fetching followers:", error.message);
throw error;
}
}
// ✅ 5️⃣ Get User Following
async function getUserFollowing(username) {
try {
const response = await fetch(`https://api.github.com/users/${username}/following`, {
headers: { 'Accept': 'application/vnd.github+json' }
});
handleAPIError(response);
const data = await response.json();
return data;
} catch (error) {
console.error("❌ Error fetching following:", error.message);
throw error;
}
}
// ✅ 6️⃣ Get User Organizations
async function getUserOrganizations(username) {
try {
const response = await fetch(`https://api.github.com/users/${username}/orgs`, {
headers: { 'Accept': 'application/vnd.github+json' }
});
handleAPIError(response);
const data = await response.json();
return data;
} catch (error) {
console.error("❌ Error fetching organizations:", error.message);
throw error;
}
}