From 715bad6c67b414388ee2a9cbb949659f8e8cde81 Mon Sep 17 00:00:00 2001 From: barbapapazes Date: Mon, 8 Jan 2024 17:25:19 +0100 Subject: [PATCH 1/2] feat: support pagination --- routes/orgs/[owner]/repos.ts | 16 +++++++++++++--- utils/github.ts | 25 +++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 3 deletions(-) diff --git a/routes/orgs/[owner]/repos.ts b/routes/orgs/[owner]/repos.ts index 8fdcf54..e646e0e 100644 --- a/routes/orgs/[owner]/repos.ts +++ b/routes/orgs/[owner]/repos.ts @@ -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) => { @@ -23,6 +31,8 @@ export default eventHandler(async (event) => { }, ); + + setResponseHeader(event, "Link", headers.Link); return { repos, }; diff --git a/utils/github.ts b/utils/github.ts index 3f6f66c..3957fa0 100644 --- a/utils/github.ts +++ b/utils/github.ts @@ -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}`, +}); From 216aea9b52ae0a3747fe2d1371044dfe2157e263 Mon Sep 17 00:00:00 2001 From: "autofix-ci[bot]" <114827586+autofix-ci[bot]@users.noreply.github.com> Date: Mon, 8 Jan 2024 16:27:20 +0000 Subject: [PATCH 2/2] chore: apply automated fixes --- routes/orgs/[owner]/repos.ts | 2 -- utils/github.ts | 17 ++++++++++------- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/routes/orgs/[owner]/repos.ts b/routes/orgs/[owner]/repos.ts index e646e0e..23c6d27 100644 --- a/routes/orgs/[owner]/repos.ts +++ b/routes/orgs/[owner]/repos.ts @@ -13,7 +13,6 @@ export default eventHandler(async (event) => { perPage, ); - const repos = rawRepos.map( (rawRepo) => { @@ -31,7 +30,6 @@ export default eventHandler(async (event) => { }, ); - setResponseHeader(event, "Link", headers.Link); return { repos, diff --git a/utils/github.ts b/utils/github.ts index 3957fa0..26f40f3 100644 --- a/utils/github.ts +++ b/utils/github.ts @@ -85,14 +85,15 @@ export const ghMarkdown = cachedFunction( }, ); -export const ghPagination = cachedFunction(async (url: string, page: number, perPage: number) => { - const { _data: data, headers } = await $fetch.raw(url, { +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', + method: "GET", headers: { "User-Agent": "fetch", Authorization: "token " + runtimeConfig.GH_TOKEN, @@ -105,7 +106,9 @@ export const ghPagination = cachedFunction(async (url: string, page: number, per Link: headers.get("Link"), }, }; -}, { - ...cacheOptions("pagination"), - getKey: (path, page, perPage) => `${path}/${page}/${perPage}`, -}); + }, + { + ...cacheOptions("pagination"), + getKey: (path, page, perPage) => `${path}/${page}/${perPage}`, + }, +);