your personal website on atproto - mirror forked from flo-bit.dev/blento
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657import { json } from '@sveltejs/kit';import type { RequestHandler } from './$types';import type { GitHubContributionsData } from '$lib/cards/GitHubProfileCard/types';
const GithubAPIURL = 'https://edge-function-github-contribution.vercel.app/api/github-data?user=';
export const GET: RequestHandler = async ({ url, platform }) => { const user = url.searchParams.get('user');
if (!user) { return json({ error: 'No user provided' }, { status: 400 }); }
const cachedData = await platform?.env?.USER_DATA_CACHE?.get('#github:' + user);
if (cachedData) { const parsedCache = JSON.parse(cachedData);
const TWELVE_HOURS = 12 * 60 * 60 * 1000; const now = Date.now();
if (now - (parsedCache.updatedAt || 0) < TWELVE_HOURS) { return json(parsedCache); } }
try { const response = await fetch(GithubAPIURL + user); console.log('hello', user);
if (!response.ok) { console.log('error', response.statusText); return json( { error: 'Failed to fetch GitHub data ' + response.statusText }, { status: response.status } ); }
const data = await response.json();
if (!data?.user) { console.log('user not found', response.statusText); return json({ error: 'User not found' }, { status: 404 }); }
const result = data.user as GitHubContributionsData; result.updatedAt = Date.now();
await platform?.env?.USER_DATA_CACHE?.put('#github:' + user, JSON.stringify(result));
return json(result); } catch (error) { console.error('Error fetching GitHub contributions:', error); return json({ error: 'Failed to fetch GitHub data' }, { status: 500 }); }};