forked from mirrored/github-readme-stats
107 lines
2.6 KiB
JavaScript
107 lines
2.6 KiB
JavaScript
// @ts-check
|
|
import { retryer } from "../common/retryer.js";
|
|
import { MissingParamError, request } from "../common/utils.js";
|
|
|
|
/**
|
|
* Repo data fetcher.
|
|
*
|
|
* @param {import('axios').AxiosRequestHeaders} variables Fetcher variables.
|
|
* @param {string} token GitHub token.
|
|
* @returns {Promise<import('axios').AxiosResponse>} The response.
|
|
*/
|
|
const fetcher = (variables, token) => {
|
|
return request(
|
|
{
|
|
query: `
|
|
fragment RepoInfo on Repository {
|
|
name
|
|
nameWithOwner
|
|
isPrivate
|
|
isArchived
|
|
isTemplate
|
|
stargazers {
|
|
totalCount
|
|
}
|
|
description
|
|
primaryLanguage {
|
|
color
|
|
id
|
|
name
|
|
}
|
|
forkCount
|
|
}
|
|
query getRepo($login: String!, $repo: String!) {
|
|
user(login: $login) {
|
|
repository(name: $repo) {
|
|
...RepoInfo
|
|
}
|
|
}
|
|
organization(login: $login) {
|
|
repository(name: $repo) {
|
|
...RepoInfo
|
|
}
|
|
}
|
|
}
|
|
`,
|
|
variables,
|
|
},
|
|
{
|
|
Authorization: `token ${token}`,
|
|
},
|
|
);
|
|
};
|
|
|
|
const urlExample = "/api/pin?username=USERNAME&repo=REPO_NAME";
|
|
|
|
/**
|
|
* Fetch repository data.
|
|
*
|
|
* @param {string} username GitHub username.
|
|
* @param {string} reponame GitHub repository name.
|
|
* @returns {Promise<import("./types").RepositoryData>} Repository data.
|
|
*/
|
|
const fetchRepo = async (username, reponame) => {
|
|
if (!username && !reponame) {
|
|
throw new MissingParamError(["username", "repo"], urlExample);
|
|
}
|
|
if (!username) throw new MissingParamError(["username"], urlExample);
|
|
if (!reponame) throw new MissingParamError(["repo"], urlExample);
|
|
|
|
let res = await retryer(fetcher, { login: username, repo: reponame });
|
|
|
|
const data = res.data.data;
|
|
|
|
if (!data.user && !data.organization) {
|
|
throw new Error("Not found");
|
|
}
|
|
|
|
const isUser = data.organization === null && data.user;
|
|
const isOrg = data.user === null && data.organization;
|
|
|
|
if (isUser) {
|
|
if (!data.user.repository || data.user.repository.isPrivate) {
|
|
throw new Error("User Repository Not found");
|
|
}
|
|
return {
|
|
...data.user.repository,
|
|
starCount: data.user.repository.stargazers.totalCount,
|
|
};
|
|
}
|
|
|
|
if (isOrg) {
|
|
if (
|
|
!data.organization.repository ||
|
|
data.organization.repository.isPrivate
|
|
) {
|
|
throw new Error("Organization Repository Not found");
|
|
}
|
|
return {
|
|
...data.organization.repository,
|
|
starCount: data.organization.repository.stargazers.totalCount,
|
|
};
|
|
}
|
|
};
|
|
|
|
export { fetchRepo };
|
|
export default fetchRepo;
|