forked from anuraghazra/github-readme-stats
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfetchRepo.js
63 lines (56 loc) · 1.32 KB
/
fetchRepo.js
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
const { request } = require("./utils");
async function fetchRepo(username, reponame) {
if (!username || !reponame) {
throw new Error("Invalid username or reponame");
}
const res = await request({
query: `
fragment RepoInfo on Repository {
name
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: {
login: username,
repo: reponame,
},
});
const data = res.data.data;
if (!data.user && !data.organization) {
throw new Error("Not found");
}
if (data.organization === null && data.user) {
if (!data.user.repository) {
throw new Error("User Repository Not found");
}
return data.user.repository;
}
if (data.user === null && data.organization) {
if (!data.organization.repository) {
throw new Error("Organization Repository Not found");
}
return data.organization.repository;
}
}
module.exports = fetchRepo;