-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgithub.js
More file actions
24 lines (21 loc) · 706 Bytes
/
Copy pathgithub.js
File metadata and controls
24 lines (21 loc) · 706 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import axios from "axios";
const BASE_URL = "https://api.github.com/users/";
export const GithubService = {
getUser: async (username) => {
if (!username) throw new Error("Username required");
try {
const headers = {};
const token = import.meta.env.VITE_GITHUB_TOKEN;
if (token) {
headers.Authorization = `Bearer ${token}`;
}
const response = await axios.get(`${BASE_URL}${username}`, { headers });
return response.data;
} catch (error) {
if (error.response?.status === 404) return null;
const msg =
error.response?.data?.message || error.message || "GitHub API request failed";
throw new Error(msg);
}
},
};