[READ-ONLY] a fast, modern browser for the npm registry
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

fix: use pagination to fetch all contributors

+36 -19
+36 -19
server/api/contributors.get.ts
··· 1 - import type { H3Event } from 'h3' 2 - 3 1 export interface GitHubContributor { 4 2 login: string 5 3 id: number ··· 9 7 } 10 8 11 9 export default defineCachedEventHandler( 12 - async (_event: H3Event): Promise<GitHubContributor[]> => { 13 - const response = await fetch( 14 - 'https://api.github.com/repos/npmx-dev/npmx.dev/contributors?per_page=50', 15 - { 16 - headers: { 17 - 'Accept': 'application/vnd.github.v3+json', 18 - 'User-Agent': 'npmx', 10 + async (): Promise<GitHubContributor[]> => { 11 + const allContributors: GitHubContributor[] = [] 12 + let page = 1 13 + const perPage = 100 14 + 15 + while (true) { 16 + const response = await fetch( 17 + `https://api.github.com/repos/npmx-dev/npmx.dev/contributors?per_page=${perPage}&page=${page}`, 18 + { 19 + headers: { 20 + 'Accept': 'application/vnd.github.v3+json', 21 + 'User-Agent': 'npmx', 22 + }, 19 23 }, 20 - }, 21 - ) 24 + ) 25 + 26 + if (!response.ok) { 27 + throw createError({ 28 + statusCode: response.status, 29 + message: 'Failed to fetch contributors', 30 + }) 31 + } 32 + 33 + const contributors = (await response.json()) as GitHubContributor[] 34 + 35 + if (contributors.length === 0) { 36 + break 37 + } 38 + 39 + allContributors.push(...contributors) 40 + 41 + // If we got fewer than perPage results, we've reached the end 42 + if (contributors.length < perPage) { 43 + break 44 + } 22 45 23 - if (!response.ok) { 24 - throw createError({ 25 - statusCode: response.status, 26 - message: 'Failed to fetch contributors', 27 - }) 46 + page++ 28 47 } 29 48 30 - const contributors = (await response.json()) as GitHubContributor[] 31 - 32 49 // Filter out bots 33 - return contributors.filter(c => !c.login.includes('[bot]')) 50 + return allContributors.filter(c => !c.login.includes('[bot]')) 34 51 }, 35 52 { 36 53 maxAge: 3600, // Cache for 1 hour