Skip to content

Commit

Permalink
feat: add pages query
Browse files Browse the repository at this point in the history
  • Loading branch information
xhofe committed Oct 16, 2023
1 parent 02eb0e9 commit 8e647f0
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 8 deletions.
1 change: 0 additions & 1 deletion app/(empty)/iframe/page.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
"use client"
import { fetchRepos } from "@/app/api/github"
import { GhUserUse } from "@/app/api/types"
import { calParams } from "@/utils/svg"
import { Spinner } from "@nextui-org/react"
Expand Down
8 changes: 4 additions & 4 deletions app/api/github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const avatarCache = new LRUCache<string, string>({
ttl: 1000 * 60 * 60 * 24,
})

export async function fetchRepo(repo: string) {
export async function fetchRepo(repo: string, maxPages: number = 1) {
// validate repo
const repoRegex = /^[\w-]+\/[\w-]+$/
if (!repoRegex.test(repo)) {
Expand All @@ -31,7 +31,7 @@ export async function fetchRepo(repo: string) {
console.log(`fetching ${repo}`)
const users = []
let page = 1
while (true) {
while (page <= maxPages) {
const res = await fetch(
`https://api.github.com/repos/${repo}/contributors?per_page=100&page=${page}`
)
Expand Down Expand Up @@ -59,11 +59,11 @@ export async function fetchRepo(repo: string) {
return usersUse
}

export async function fetchRepos(repos: string[]) {
export async function fetchRepos(repos: string[], maxPages?: number) {
const users = (
await Promise.all(
repos.map(async (repo) => {
const users = await fetchRepo(repo)
const users = await fetchRepo(repo, maxPages)
return users
})
)
Expand Down
3 changes: 2 additions & 1 deletion app/api/json/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ export async function GET(req: NextRequest) {
try {
const searchParams = req.nextUrl.searchParams
const repos = searchParams.getAll("repo")
const maxPages = parseInt(searchParams.get("pages") || "1")

if (repos.length === 0) {
throw new Error("repo is required")
}

const users = await fetchRepos(repos)
const users = await fetchRepos(repos, maxPages)
return NextResponse.json(users)
} catch (e: any) {
return NextResponse.json({ error: e.message }, { status: 500 })
Expand Down
2 changes: 2 additions & 0 deletions app/api/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ export async function GET(req: NextRequest): Promise<NextResponse> {
if (repos.length === 0) {
throw new Error("repo is required")
}
const maxPages = parseInt(searchParams.get("pages") || "1")
const svg = await generateSVG({
repos,
maxPages,
cols: searchParams.get("cols"),
radius: searchParams.get("radius"),
space: searchParams.get("space"),
Expand Down
4 changes: 2 additions & 2 deletions app/api/svg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ export function getUser(
}

export async function generateSVG(
conf: { repos: string[] } & Omit<calParamsArg, "total">
conf: { repos: string[]; maxPages?: number } & Omit<calParamsArg, "total">
) {
const users = (await fetchRepos(conf.repos)) as GhUser[]
const users = (await fetchRepos(conf.repos, conf.maxPages)) as GhUser[]
const avatars = await Promise.all(
users.map(async (user: GhUserUse) => await fetchAvatar(user.avatar_url))
)
Expand Down

0 comments on commit 8e647f0

Please sign in to comment.