mirror of
https://github.com/Snigdha-OS/Snigdha-OS.github.io.git
synced 2025-09-06 20:55:18 +02:00
🐛 fix: x limit api will use LocalStorage
This commit is contained in:
@@ -22,36 +22,61 @@ export interface GithubRepo {
|
|||||||
updated_at: string;
|
updated_at: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function fetchGithubUser(username: string): Promise<GithubUser> {
|
// Helper function to check cache expiration
|
||||||
try {
|
function isCacheExpired(cacheKey: string): boolean {
|
||||||
const response = await fetch(`${GITHUB_API_URL}/users/${username}`);
|
const cachedData = localStorage.getItem(cacheKey);
|
||||||
|
if (!cachedData) return true;
|
||||||
if (!response.ok) {
|
|
||||||
const errorData = await response.json().catch(() => ({}));
|
const { timestamp } = JSON.parse(cachedData);
|
||||||
console.error(`GitHub API Error (${response.status}):`, errorData);
|
const currentTime = Date.now();
|
||||||
throw new Error(`Failed to fetch user ${username}: ${response.statusText}`);
|
const cacheExpirationTime = 3600000; // Cache expiration time in ms (1 hour)
|
||||||
}
|
|
||||||
|
return currentTime - timestamp > cacheExpirationTime;
|
||||||
|
}
|
||||||
|
|
||||||
return await response.json();
|
// Function to get from cache or make an API request
|
||||||
} catch (error) {
|
async function getFromCacheOrApi(url: string, cacheKey: string) {
|
||||||
console.error(`Error fetching GitHub user ${username}:`, error);
|
// Check if cached data exists and is valid
|
||||||
throw error;
|
if (!isCacheExpired(cacheKey)) {
|
||||||
|
const cachedData = localStorage.getItem(cacheKey);
|
||||||
|
if (cachedData) {
|
||||||
|
console.log('Serving from cache');
|
||||||
|
return JSON.parse(cachedData).data;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// If not cached or cache expired, make the API request
|
||||||
|
console.log('Fetching from GitHub API');
|
||||||
|
const response = await fetch(url);
|
||||||
|
|
||||||
|
if (!response.ok) {
|
||||||
|
const errorData = await response.json().catch(() => ({}));
|
||||||
|
console.error(`GitHub API Error (${response.status}):`, errorData);
|
||||||
|
throw new Error(`Failed to fetch data: ${response.statusText}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
const data = await response.json();
|
||||||
|
|
||||||
|
// Cache the response with a timestamp
|
||||||
|
const cacheData = {
|
||||||
|
data,
|
||||||
|
timestamp: Date.now(),
|
||||||
|
};
|
||||||
|
|
||||||
|
localStorage.setItem(cacheKey, JSON.stringify(cacheData));
|
||||||
|
return data;
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function fetchGithubUser(username: string): Promise<GithubUser> {
|
||||||
|
const cacheKey = `github-user-${username}`;
|
||||||
|
const url = `${GITHUB_API_URL}/users/${username}`;
|
||||||
|
|
||||||
|
return getFromCacheOrApi(url, cacheKey);
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function fetchOrgRepos(org: string): Promise<GithubRepo[]> {
|
export async function fetchOrgRepos(org: string): Promise<GithubRepo[]> {
|
||||||
try {
|
const cacheKey = `github-org-repos-${org}`;
|
||||||
const response = await fetch(`${GITHUB_API_URL}/orgs/${org}/repos?sort=updated&per_page=100`);
|
const url = `${GITHUB_API_URL}/orgs/${org}/repos?sort=updated&per_page=100`;
|
||||||
|
|
||||||
if (!response.ok) {
|
|
||||||
const errorData = await response.json().catch(() => ({}));
|
|
||||||
console.error(`GitHub API Error (${response.status}):`, errorData);
|
|
||||||
throw new Error(`Failed to fetch repositories: ${response.statusText}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
return await response.json();
|
return getFromCacheOrApi(url, cacheKey);
|
||||||
} catch (error) {
|
}
|
||||||
console.error('Error fetching repositories:', error);
|
|
||||||
throw error;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
Reference in New Issue
Block a user