Skip to content

Commit

Permalink
feat: support pagination
Browse files Browse the repository at this point in the history
  • Loading branch information
Barbapapazes committed Jan 8, 2024
1 parent 6473e40 commit 715bad6
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 3 deletions.
16 changes: 13 additions & 3 deletions routes/orgs/[owner]/repos.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
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(
(rawRepo) =>
<GithubRepo>{
Expand All @@ -23,6 +31,8 @@ export default eventHandler(async (event) => {
},
);


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

0 comments on commit 715bad6

Please sign in to comment.