File size: 1,330 Bytes
d608f5f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
// api/rss.ts - Vercel-style API route
export default async function handler(req: Request): Promise<Response> {
  const { searchParams } = new URL(req.url);
  const url = searchParams.get('url');
  if (!url) return new Response(JSON.stringify({ error: 'Missing url' }), { status: 400 });

  try {
    const upstream = await fetch(url, { 
      headers: { 'user-agent': 'NewsProxy/1.0' },
      signal: AbortSignal.timeout(10000) // 10s timeout
    });
    if (!upstream.ok) return new Response(JSON.stringify({ error: 'Upstream error' }), { status: upstream.status });

    const xml = await upstream.text();
    const items = [...xml.matchAll(/<item>([\s\S]*?)<\/item>/g)].slice(0, 25).map(m => {
      const b = m[1];
      const pick = (tag: string) => (b.match(new RegExp(`<${tag}>([\\s\\S]*?)<\\/${tag}>`, 'i')) || [])[1]?.trim();
      return { title: pick('title'), link: pick('link'), pubDate: pick('pubDate') };
    });

    return Response.json({ items }, { 
      headers: { 
        'Cache-Control': 'max-age=60',
        'Access-Control-Allow-Origin': '*',
        'Access-Control-Allow-Methods': 'GET',
        'Access-Control-Allow-Headers': 'Content-Type'
      } 
    });
  } catch (error) {
    return new Response(JSON.stringify({ error: 'Fetch failed' }), { status: 500 });
  }
}