Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

read all repos in org with pagination #76

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions routes/orgs/[owner]/repos.ts
Original file line number Diff line number Diff line change
@@ -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;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can handle the pagination once and return theall pages instead of asking API consumers to do it.


const owner = getRouterParam(event, "owner");

const { _data: rawRepos, headers } = await ghPagination(
`orgs/${owner}/repos`,
page,
perPage,
);

const repos = rawRepos.map(
Expand All @@ -23,6 +30,7 @@ export default eventHandler(async (event) => {
},
);

setResponseHeader(event, "Link", headers.Link);
return {
repos,
};
Expand Down
28 changes: 28 additions & 0 deletions utils/github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`,
},
);