/**
* API Route: /api/news-sources
*
* GET: Fetch all news sources with metadata for the visualizer
*/
import { NextResponse } from 'next/server';
import { supabase } from '@/lib/supabase-admin';
export interface NewsSource {
id: string;
name: string;
rss_url: string;
is_active: boolean;
region: string;
website_url: string | null;
description_en: string | null;
description_fr: string | null;
has_paywall: boolean;
credibility_tier: 'premium' | 'standard' | 'aggregator';
logo_url: string | null;
}
export async function GET() {
try {
const { data, error } = await supabase
.from('news_sources')
.select(`
id,
name,
rss_url,
is_active,
region,
website_url,
description_en,
description_fr,
has_paywall,
credibility_tier,
logo_url
`)
.order('name');
if (error) {
console.error('Error fetching news sources:', error);
return NextResponse.json(
{ error: 'Failed to fetch news sources' },
{ status: 500 }
);
}
// Group sources by region for convenience
const sourcesByRegion: Record<string, NewsSource[]> = {};
for (const source of data || []) {
const region = source.region || 'national';
if (!sourcesByRegion[region]) {
sourcesByRegion[region] = [];
}
sourcesByRegion[region].push(source as NewsSource);
}
return NextResponse.json({
sources: data as NewsSource[],
sourcesByRegion,
});
} catch (error) {
console.error('Error in GET /api/news-sources:', error);
return NextResponse.json(
{ error: 'Internal server error' },
{ status: 500 }
);
}
}