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