diff --git a/routes/orgs/[owner]/repos.ts b/routes/orgs/[owner]/repos.ts index 8fdcf54..23c6d27 100644 --- a/routes/orgs/[owner]/repos.ts +++ b/routes/orgs/[owner]/repos.ts @@ -1,9 +1,16 @@ import type { GithubRepo } from "~types"; export default eventHandler(async (event) => { - // TODO: Do pagination - const rawRepos = await ghFetch( - `orgs/${event.context.params.owner}/repos?per_page=100`, + const query = getQuery(event); + const page = query.page ? Number(query.page) : 1; + const perPage = query.perPage ? Number(query.perPage) : 100; + + const owner = getRouterParam(event, "owner"); + + const { _data: rawRepos, headers } = await ghPagination( + `orgs/${owner}/repos`, + page, + perPage, ); const repos = rawRepos.map( @@ -23,6 +30,7 @@ export default eventHandler(async (event) => { }, ); + setResponseHeader(event, "Link", headers.Link); return { repos, }; diff --git a/utils/github.ts b/utils/github.ts index 3f6f66c..26f40f3 100644 --- a/utils/github.ts +++ b/utils/github.ts @@ -84,3 +84,31 @@ export const ghMarkdown = cachedFunction( getKey: (_markdown, repo, id) => repo + "/" + id, }, ); + +export const ghPagination = cachedFunction( + async (url: string, page: number, perPage: number) => { + const { _data: data, headers } = await $fetch.raw(url, { + baseURL: "https://api.github.com", + query: { + page, + per_page: perPage, + }, + method: "GET", + headers: { + "User-Agent": "fetch", + Authorization: "token " + runtimeConfig.GH_TOKEN, + }, + }); + + return { + _data: data, + headers: { + Link: headers.get("Link"), + }, + }; + }, + { + ...cacheOptions("pagination"), + getKey: (path, page, perPage) => `${path}/${page}/${perPage}`, + }, +);