|
| 1 | +import { NextResponse } from 'next/server'; |
| 2 | +import { fetchLatestArticles } from '@/lib/rss'; |
| 3 | +import { generateArticlesSVG } from '@/lib/svg/articles'; |
| 4 | +import { getNormalizedThemeKey, themes } from '@/lib/svg/themes'; |
| 5 | +import { sanitizeHexColor } from '@/lib/svg/sanitizer'; |
| 6 | + |
| 7 | +export async function GET(request: Request) { |
| 8 | + try { |
| 9 | + const { searchParams } = new URL(request.url); |
| 10 | + const user = searchParams.get('user'); |
| 11 | + const platformParam = searchParams.get('platform'); |
| 12 | + |
| 13 | + if (!user) { |
| 14 | + return new NextResponse('Missing user parameter', { status: 400 }); |
| 15 | + } |
| 16 | + |
| 17 | + const platform = platformParam === 'hashnode' ? 'hashnode' : 'devto'; |
| 18 | + |
| 19 | + // Fetch RSS articles |
| 20 | + const articles = await fetchLatestArticles(platform, user); |
| 21 | + |
| 22 | + // Apply theme |
| 23 | + const themeKey = getNormalizedThemeKey(searchParams.get('theme') || 'default'); |
| 24 | + const theme = themes[themeKey] || themes.default; |
| 25 | + |
| 26 | + // Allow custom colors |
| 27 | + const bg = searchParams.get('bg') |
| 28 | + ? sanitizeHexColor(searchParams.get('bg')!, theme.bg) |
| 29 | + : theme.bg; |
| 30 | + const text = searchParams.get('text') |
| 31 | + ? sanitizeHexColor(searchParams.get('text')!, theme.text) |
| 32 | + : theme.text; |
| 33 | + let accent = theme.accent; |
| 34 | + const accentParam = searchParams.get('accent'); |
| 35 | + if (accentParam) { |
| 36 | + // support comma separated list, just take the first |
| 37 | + const accents = accentParam.split(',').map((c) => sanitizeHexColor(c.trim(), theme.accent)); |
| 38 | + if (accents.length > 0 && accents[0]) { |
| 39 | + accent = accents[0]; |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + const radius = searchParams.get('radius') || '4'; |
| 44 | + const size = searchParams.get('size') || '1'; |
| 45 | + |
| 46 | + const params = { |
| 47 | + user, |
| 48 | + platform, |
| 49 | + bg, |
| 50 | + text, |
| 51 | + accent, |
| 52 | + radius, |
| 53 | + size, |
| 54 | + }; |
| 55 | + |
| 56 | + const svg = generateArticlesSVG(articles, params); |
| 57 | + |
| 58 | + return new NextResponse(svg, { |
| 59 | + headers: { |
| 60 | + 'Content-Type': 'image/svg+xml', |
| 61 | + 'Cache-Control': 'public, s-maxage=3600, stale-while-revalidate=86400', |
| 62 | + }, |
| 63 | + }); |
| 64 | + } catch (error) { |
| 65 | + console.error('Error generating articles SVG:', error); |
| 66 | + return new NextResponse('Internal Server Error', { status: 500 }); |
| 67 | + } |
| 68 | +} |
0 commit comments