portfolio/lib/github.ts

239 lines
5.8 KiB
TypeScript
Raw Normal View History

import type {Repo} from '@/types'
2023-02-20 20:46:28 +00:00
import fetcher from '@/lib/fetcher'
const GITHUB_ACCESS_TOKEN = process.env.GITHUB_ACCESS_TOKEN
2023-01-28 22:12:58 +00:00
const GITHUB_USERNAME = process.env.GITHUB_USERNAME
const GITHUB_GRAPHQL = "https://api.github.com/graphql"
2023-01-27 23:33:49 +00:00
type PinnedReposResponse = {
data: {
user: {
pinnedItems: {
nodes: Repo[]
}
}
}
}
2023-01-27 23:33:49 +00:00
type TotalReposResponse = {
data: {
user: {
repositories: {
totalCount: number
}
}
}
}
type TotalFollowersResponse = {
data: {
user: {
followers: {
totalCount: number
}
}
}
}
2023-01-28 15:01:32 +00:00
type TotalStarsResponse = {
data: {
user: {
repositories: {
nodes: [
{
stargazers: {
totalCount: number
}
}
]
}
}
}
}
2023-02-21 17:03:38 +00:00
type TotalForksResponse = {
data: {
user: {
repositories: {
nodes: [
{
forks: {
totalCount: number
}
}
]
}
}
}
}
2023-02-21 21:21:11 +00:00
type TopRepoResponse = {
data: {
user: {
repositories: {
nodes: [
{
name: string
url: string
}
]
}
}
}
}
export async function getPinnedRepos() {
2023-01-28 21:24:05 +00:00
const response = await fetcher(GITHUB_GRAPHQL, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${GITHUB_ACCESS_TOKEN}`
},
body: JSON.stringify({
query: `{
2023-01-28 22:12:58 +00:00
user(login: "${GITHUB_USERNAME}") {
pinnedItems(first: 6, types: REPOSITORY) {
nodes {
... on Repository {
name
description
url
stargazerCount
2023-01-26 22:21:15 +00:00
forkCount
primaryLanguage {
name
color
}
}
}
}
}
}`
})
2023-01-28 21:24:05 +00:00
}) as PinnedReposResponse
return response.data.user.pinnedItems.nodes
2023-01-27 23:33:49 +00:00
}
export async function getTotalRepos() {
2023-01-28 21:24:05 +00:00
const response = await fetcher(GITHUB_GRAPHQL, {
2023-01-27 23:33:49 +00:00
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${GITHUB_ACCESS_TOKEN}`
},
body: JSON.stringify({
query: `{
2023-01-28 22:12:58 +00:00
user(login: "${GITHUB_USERNAME}") {
2023-01-27 23:33:49 +00:00
repositories {
totalCount
}
}
}`
})
2023-01-28 21:24:05 +00:00
}) as TotalReposResponse
2023-01-27 23:33:49 +00:00
return response.data.user.repositories.totalCount
}
export async function getTotalFollowers() {
2023-01-28 21:24:05 +00:00
const response = await fetcher(GITHUB_GRAPHQL, {
2023-01-27 23:33:49 +00:00
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${GITHUB_ACCESS_TOKEN}`
},
body: JSON.stringify({
query: `{
2023-01-28 22:12:58 +00:00
user(login: "${GITHUB_USERNAME}") {
2023-01-27 23:33:49 +00:00
followers {
totalCount
}
}
}`
})
2023-01-28 21:24:05 +00:00
}) as TotalFollowersResponse
2023-01-27 23:33:49 +00:00
return response.data.user.followers.totalCount
2023-01-28 15:01:32 +00:00
}
export async function getTotalStars(totalRepos: number) {
2023-01-28 21:24:05 +00:00
const response = await fetcher(GITHUB_GRAPHQL, {
2023-01-28 15:01:32 +00:00
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${GITHUB_ACCESS_TOKEN}`
},
body: JSON.stringify({
query: `{
2023-01-28 22:12:58 +00:00
user(login: "${GITHUB_USERNAME}") {
2023-01-28 15:01:32 +00:00
repositories(first: ${totalRepos}) {
nodes {
stargazers {
totalCount
}
}
}
}
}`
})
2023-01-28 21:24:05 +00:00
}) as TotalStarsResponse
2023-01-28 15:01:32 +00:00
return response.data.user.repositories.nodes
.reduce((acc, node) => acc + node.stargazers.totalCount, 0)
2023-02-21 17:03:38 +00:00
}
export async function getTotalForks(totalRepos: number) {
const response = await fetcher(GITHUB_GRAPHQL, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${GITHUB_ACCESS_TOKEN}`
},
body: JSON.stringify({
query: `{
user(login: "${GITHUB_USERNAME}") {
repositories(first: ${totalRepos}) {
nodes {
forks {
totalCount
}
}
}
}
}`
})
}) as TotalForksResponse
return response.data.user.repositories.nodes
.reduce((acc, node) => acc + node.forks.totalCount, 0)
2023-02-21 21:21:11 +00:00
}
export async function getTopRepo() {
const response = await fetcher(GITHUB_GRAPHQL, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${GITHUB_ACCESS_TOKEN}`
},
body: JSON.stringify({
query: `{
user(login: "${GITHUB_USERNAME}") {
repositories(first: 1, orderBy: {field: STARGAZERS, direction: DESC}) {
nodes {
name
url
}
}
}
}`
})
}) as TopRepoResponse
const {name, url} = response.data.user.repositories.nodes[0]
return {
name,
url
}
}