github-user-data
Version:
Simple Github API wrapper
183 lines (163 loc) • 3.71 kB
JavaScript
import axios from "axios";
import logSymbols from "log-symbols";
export const GithubUserData = async (username, option) => {
try {
const response = await axios.get(
`https://api.github.com/users/${username}`
);
console.log(
logSymbols.success,
`Successfully gathered user's personal information (User: ${username})`
);
if (option) {
option = option.trim().toLowerCase();
option = validateOption(option);
}
console.log(
logSymbols.success,
`Successfully gathered data within the selected option (Option: ${option})`
);
return option ? response.data[option] : response.data;
} catch (error) {
console.error(
logSymbols.error,
`Unexpected error while getting user's personal information: ${error.message}`
);
}
};
const validateOption = (option) => {
const list = [
"login",
"id",
"node_id",
"avatar_url",
"gravatar_id",
"url",
"type",
"site_admin",
"name",
"company",
"blog",
"location",
"email",
"hireable",
"bio",
"twitter_username",
"public_repos",
"public_gists",
"followers",
"following",
"created_at",
"updated_at",
"all",
];
if (list.includes(option)) {
if (option === "all") {
return "";
} else {
return option;
}
} else {
console.log(logSymbols.error + "Invalid option");
return option;
}
};
export const CountStars = async (username) => {
let totalStars = 0;
let page = 1;
let done = false;
try {
console.log(
logSymbols.success,
`Successfully found user's repositories (User: ${username})`
);
while (!done) {
const stars = await countPageStars(username, page, totalStars);
totalStars += stars;
if (stars === 0) {
done = true;
} else {
page++;
}
}
return totalStars;
} catch (error) {
console.error(
logSymbols.error,
`Unexpected error while getting user's repositories: ${error.message}`
);
}
};
const countPageStars = async (username, page) => {
const response = await axios.get(
`https://api.github.com/users/${username}/repos?per_page=100&page=${page}`
);
const data = response.data;
let starsOnPage = 0;
for (let i = 0; i < data.length; i++) {
starsOnPage = starsOnPage + data[i].stargazers_count;
}
console.log(
logSymbols.success,
`Successfully counted user's stars on page ${page} (User: ${username})`
);
page++;
// count stars until there is no more data
if (data.length === 100) {
return starsOnPage;
} else {
return starsOnPage || 0;
}
};
export const GetNamesOfAllRepos = async (username) => {
let page = 1;
let done = false;
let data = [];
try {
console.log(
logSymbols.success,
`Successfully found user's repositories (User: ${username})`
);
while (!done) {
const repos = await getReposOnPage(username, page);
if(repos.length > 0){
// push each repo name to the data array
for (let i = 0; i < repos.length; i++) {
data.push(repos[i]);
}
}
if (repos.length === 0) {
done = true;
} else {
page++;
}
}
return data;
} catch (error) {
console.error(
logSymbols.error,
`Unexpected error while getting user's repositories: ${error.message}`
);
}
}
const getReposOnPage = async (username, page) => {
const response = await axios.get(
`https://api.github.com/users/${username}/repos?per_page=100&page=${page}`
);
const data = response.data;
let names = [];
for (let i = 0; i < data.length; i++) {
names.push(data[i].name);
}
console.log(
logSymbols.success,
`Successfully gathered user's repositories on page ${page} (User: ${username})`
);
page++;
// count stars until there is no more data
if (data.length === 100) {
return names;
} else {
return names || [];
}
}