fix: use cached endpoint to avoid rate limiting issues
This commit is contained in:
@@ -22,6 +22,10 @@ type ContributorStats = {
|
|||||||
author: { login: string };
|
author: { login: string };
|
||||||
};
|
};
|
||||||
|
|
||||||
|
type ContributorUser = {
|
||||||
|
name: string;
|
||||||
|
}
|
||||||
|
|
||||||
type FetchResult<T> = {
|
type FetchResult<T> = {
|
||||||
data: T;
|
data: T;
|
||||||
dataFetched: Date;
|
dataFetched: Date;
|
||||||
@@ -81,9 +85,12 @@ export class GitHubStatsService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const cachedItem = this.cache[key] as CachedData<T> | undefined;
|
const cachedItem = this.cache[key] as CachedData<T> | undefined;
|
||||||
if (cachedItem && Date.now() - cachedItem.dataFetched < CACHE_TTL) {
|
if (cachedItem) {
|
||||||
|
const timeDifference = Date.now() - cachedItem.dataFetched;
|
||||||
|
if (timeDifference < CACHE_TTL) {
|
||||||
return cachedItem;
|
return cachedItem;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -112,6 +119,16 @@ export class GitHubStatsService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private async fetchGitHub(route: string): Promise<unknown> {
|
||||||
|
try {
|
||||||
|
const response = await fetch(`https://github.cachedapi.com${route}`);
|
||||||
|
return await response.json();
|
||||||
|
} catch (error: unknown) {
|
||||||
|
const response = await fetch(`https://api.github.com${route}`);
|
||||||
|
return await response.json();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private async fetchContributorStats(): Promise<FetchResult<ContributorStats[]>> {
|
private async fetchContributorStats(): Promise<FetchResult<ContributorStats[]>> {
|
||||||
const cacheKey = `contributor_stats_${REPO_OWNER}_${REPO_NAME}`;
|
const cacheKey = `contributor_stats_${REPO_OWNER}_${REPO_NAME}`;
|
||||||
const cachedStats = await this.getCachedData<ContributorStats[]>(cacheKey);
|
const cachedStats = await this.getCachedData<ContributorStats[]>(cacheKey);
|
||||||
@@ -125,11 +142,8 @@ export class GitHubStatsService {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
const { data } = await this.fetchWithRetry(() =>
|
const data = await this.fetchWithRetry(() =>
|
||||||
this.octokit.repos.getContributorsStats({
|
this.fetchGitHub(`/repos/${REPO_OWNER}/${REPO_NAME}/stats/contributors`)
|
||||||
owner: REPO_OWNER,
|
|
||||||
repo: REPO_NAME,
|
|
||||||
})
|
|
||||||
);
|
);
|
||||||
|
|
||||||
if (Array.isArray(data)) {
|
if (Array.isArray(data)) {
|
||||||
@@ -158,10 +172,11 @@ export class GitHubStatsService {
|
|||||||
name = cachedName.data;
|
name = cachedName.data;
|
||||||
} else {
|
} else {
|
||||||
try {
|
try {
|
||||||
const response = await fetch(`https://api.github.com/users/${contributor}`);
|
const data = await this.fetchWithRetry(() =>
|
||||||
const json = await response.json();
|
this.fetchGitHub(`/users/${contributor}`)
|
||||||
if (json.name) {
|
) as ContributorUser;
|
||||||
name = json.name;
|
if (data.name) {
|
||||||
|
name = data.name;
|
||||||
}
|
}
|
||||||
await this.setCachedData(cacheKey, name);
|
await this.setCachedData(cacheKey, name);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
|||||||
Reference in New Issue
Block a user