your personal website on atproto - mirror blento.app
at switch-grid-layout 54 lines 1.5 kB view raw
1import { json } from '@sveltejs/kit'; 2import type { RequestHandler } from './$types'; 3import type { GitHubContributionsData } from '$lib/cards/social/GitHubProfileCard/types'; 4 5const GithubAPIURL = 'https://edge-function-github-contribution.vercel.app/api/github-data?user='; 6 7export const GET: RequestHandler = async ({ url, platform }) => { 8 const user = url.searchParams.get('user'); 9 10 if (!user) { 11 return json({ error: 'No user provided' }, { status: 400 }); 12 } 13 14 const cachedData = await platform?.env?.USER_DATA_CACHE?.get('#github:' + user); 15 16 if (cachedData) { 17 const parsedCache = JSON.parse(cachedData); 18 19 const TWELVE_HOURS = 12 * 60 * 60 * 1000; 20 const now = Date.now(); 21 22 if (now - (parsedCache.updatedAt || 0) < TWELVE_HOURS) { 23 return json(parsedCache); 24 } 25 } 26 27 try { 28 const response = await fetch(GithubAPIURL + user); 29 30 if (!response.ok) { 31 console.error('error', response.statusText); 32 return json( 33 { error: 'Failed to fetch GitHub data ' + response.statusText }, 34 { status: response.status } 35 ); 36 } 37 38 const data = await response.json(); 39 40 if (!data?.user) { 41 return json({ error: 'User not found' }, { status: 404 }); 42 } 43 44 const result = data.user as GitHubContributionsData; 45 result.updatedAt = Date.now(); 46 47 await platform?.env?.USER_DATA_CACHE?.put('#github:' + user, JSON.stringify(result)); 48 49 return json(result); 50 } catch (error) { 51 console.error('Error fetching GitHub contributions:', error); 52 return json({ error: 'Failed to fetch GitHub data' }, { status: 500 }); 53 } 54};