From b33a3779ccc677a871653f94401dadf4d35f3433 Mon Sep 17 00:00:00 2001 From: d3v1l0n Date: Sun, 29 Dec 2024 04:51:03 +0530 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20fix:=20x=20limit=20api=20will=20?= =?UTF-8?q?use=20LocalStorage?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/lib/github.ts | 79 +++++++++++++++++++++++++++++++---------------- 1 file changed, 52 insertions(+), 27 deletions(-) diff --git a/src/lib/github.ts b/src/lib/github.ts index 7ecf5c96..21ae8ee3 100644 --- a/src/lib/github.ts +++ b/src/lib/github.ts @@ -22,36 +22,61 @@ export interface GithubRepo { updated_at: string; } -export async function fetchGithubUser(username: string): Promise { - 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 { + const cacheKey = `github-user-${username}`; + const url = `${GITHUB_API_URL}/users/${username}`; + + return getFromCacheOrApi(url, cacheKey); } export async function fetchOrgRepos(org: string): Promise { - 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; - } -} \ No newline at end of file + return getFromCacheOrApi(url, cacheKey); +}