Refine GitHub api logic

This commit is contained in:
Sam Becker 2025-02-16 11:13:36 -06:00
parent abe0005f80
commit 29e752bc9b

View File

@ -8,11 +8,6 @@ const DEFAULT_BRANCH = 'main';
const CACHE_GITHUB_REQUESTS = false; const CACHE_GITHUB_REQUESTS = false;
const GITHUB_API_ERROR = 'API rate limit exceeded'; const GITHUB_API_ERROR = 'API rate limit exceeded';
// Cache all results for 2 minutes to avoid rate limiting
// GitHub API requests limited to 60 requests per hour
const FETCH_CONFIG: RequestInit | undefined = CACHE_GITHUB_REQUESTS
? { next: { revalidate: 120 } } : undefined;
interface RepoParams { interface RepoParams {
owner?: string owner?: string
repo?: string repo?: string
@ -20,9 +15,14 @@ interface RepoParams {
commit?: string commit?: string
}; };
const fetchGitHub = async (url: string, cacheRequest?: boolean) => { const fetchGitHub = async (
url: string,
cacheRequest = CACHE_GITHUB_REQUESTS,
) => {
const data = await fetch( const data = await fetch(
url, url,
// Cache all results for 2 minutes to avoid rate limiting
// GitHub API requests limited to 60 requests per hour
cacheRequest ? { next: { revalidate: 120 } } : undefined, cacheRequest ? { next: { revalidate: 120 } } : undefined,
) )
.then(response => response.json()); .then(response => response.json());
@ -97,8 +97,7 @@ const getGitHubApiCompareToCommitUrl = ({ commit }: RepoParams = {}) =>
// Requests // Requests
export const getLatestBaseRepoCommitSha = async () => { export const getLatestBaseRepoCommitSha = async () => {
const response = await fetch(getGitHubApiCommitsUrl(), FETCH_CONFIG); const data = await fetchGitHub(getGitHubApiCommitsUrl());
const data = await response.json();
return data.sha ? data.sha.slice(0, 7) as string : undefined; return data.sha ? data.sha.slice(0, 7) as string : undefined;
}; };