-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgithub.js
More file actions
102 lines (94 loc) · 2.35 KB
/
github.js
File metadata and controls
102 lines (94 loc) · 2.35 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
const GITHUB_API_URL = "https://api.github.com/graphql";
const GITHUB_TOKEN = process.env.NEXT_PUBLIC_GITHUB_TOKEN;
export async function fetchGitHubData() {
const query = `
{
viewer {
login
name
avatarUrl
repositories(first: 5, orderBy: {field: UPDATED_AT, direction: DESC}) {
nodes {
name
description
url
updatedAt
stargazerCount
}
}
}
}
`;
const response = await fetch(GITHUB_API_URL, {
method: "POST",
headers: {
"Authorization": `Bearer ${GITHUB_TOKEN}`,
"Content-Type": "application/json"
},
body: JSON.stringify({ query })
});
console.log(response);
const data = await response.json();
return data.data.viewer;
}
export async function getPinnedRepos() {
try {
const query = `
query {
user(login: "rishiparihar") {
pinnedItems(first: 6, types: REPOSITORY) {
nodes {
... on Repository {
name
description
url
homepageUrl
stargazerCount
forkCount
primaryLanguage {
name
color
}
repositoryTopics(first: 5) {
nodes {
topic {
name
}
}
}
defaultBranchRef {
target {
... on Commit {
history(first: 1) {
totalCount
}
}
}
}
}
}
}
}
}
`;
const response = await fetch(GITHUB_API_URL, {
method: 'POST',
headers: {
'Authorization': `Bearer ${process.env.NEXT_PUBLIC_GITHUB_TOKEN}`,
'Content-Type': 'application/json',
},
body: JSON.stringify({ query }),
});
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const data = await response.json();
if (data.errors) {
throw new Error(data.errors[0].message);
}
return data.data.user.pinnedItems.nodes;
} catch (error) {
console.error('Error:', error);
return [];
}
}