portfolio/lib/github.ts

152 lines
3.8 KiB
TypeScript
Raw Normal View History

import fetch from 'node-fetch'
import type {Repo} from '@/types'
const GITHUB_ACCESS_TOKEN = process.env.GITHUB_ACCESS_TOKEN
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
}
}
]
}
}
}
}
export async function getPinnedRepos() {
const response = await fetch(GITHUB_GRAPHQL, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${GITHUB_ACCESS_TOKEN}`
},
body: JSON.stringify({
query: `{
user(login: "r-freeman") {
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-27 23:33:49 +00:00
}).then(r => r.json()) as PinnedReposResponse
return response.data.user.pinnedItems.nodes
2023-01-27 23:33:49 +00:00
}
export async function getTotalRepos() {
const response = await fetch(GITHUB_GRAPHQL, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${GITHUB_ACCESS_TOKEN}`
},
body: JSON.stringify({
query: `{
user(login: "r-freeman") {
repositories {
totalCount
}
}
}`
})
}).then(r => r.json()) as TotalReposResponse
return response.data.user.repositories.totalCount
}
export async function getTotalFollowers() {
const response = await fetch(GITHUB_GRAPHQL, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${GITHUB_ACCESS_TOKEN}`
},
body: JSON.stringify({
query: `{
user(login: "r-freeman") {
followers {
totalCount
}
}
}`
})
}).then(r => r.json()) as TotalFollowersResponse
return response.data.user.followers.totalCount
2023-01-28 15:01:32 +00:00
}
export async function getTotalStars(totalRepos: number) {
const response = await fetch(GITHUB_GRAPHQL, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${GITHUB_ACCESS_TOKEN}`
},
body: JSON.stringify({
query: `{
user(login: "r-freeman") {
repositories(first: ${totalRepos}) {
nodes {
stargazers {
totalCount
}
}
}
}
}`
})
}).then(r => r.json()) as TotalStarsResponse
return response.data.user.repositories.nodes
.reduce((acc, node) => acc + node.stargazers.totalCount, 0)
}