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