Branchwell

AI Resource Pro / website / scripts / test-news-api.ts

1import fetch from 'node-fetch';
2
3interface NewsItem {
4 id: string;
5 title: string;
6 content: string;
7 imageUrl?: string;
8 articleDate: string;
9 url: string;
10 source: string;
11 primary_category_id?: number | null;
12 categories?: Array<{
13 id: number;
14 name: string;
15 icon?: string;
16 }>;
17 createdAt: string;
18 updatedAt: string;
19}
20
21async function testNewsAPI() {
22 console.log('Testing news API with different limit values...');
23
24 const limits = [10, 20, 50, 100, 1000];
25
26 for (const limit of limits) {
27 try {
28 console.log(`\nTesting with limit=${limit}:`);
29 const response = await fetch(`http://localhost:3000/api/news?limit=${limit}`);
30
31 if (!response.ok) {
32 console.error(`Error: ${response.status} ${response.statusText}`);
33 continue;
34 }
35
36 const data = await response.json() as NewsItem[];
37 console.log(`Received ${data.length} news items`);
38
39 // Print the first and last item titles
40 if (data.length > 0) {
41 console.log(`First item: "${data[0].title}"`);
42 if (data.length > 1) {
43 console.log(`Last item: "${data[data.length - 1].title}"`);
44 }
45 }
46 } catch (error) {
47 console.error(`Error testing with limit=${limit}:`, error instanceof Error ? error.message : String(error));
48 }
49 }
50}
51
52testNewsAPI().catch(err => {
53 console.error('Unhandled error:', err);
54 process.exit(1);
55});