NeahNew/app/api/news/test-backend/route.ts
2025-05-04 21:01:47 +02:00

64 lines
2.0 KiB
TypeScript

import { NextResponse } from 'next/server';
import { env } from '@/lib/env';
// Test endpoint to check backend behavior with limits
export async function GET(request: Request) {
try {
const url = new URL(request.url);
// Get limit parameter or use default limits for testing
const requestedLimit = url.searchParams.get('limit');
const limits = requestedLimit ? [requestedLimit] : ['5', '10', '50', '100'];
const results: Record<string, any> = {};
// Test each limit
for (const limit of limits) {
console.log(`Testing backend with limit=${limit}...`);
const apiUrl = `${env.NEWS_API_URL}/news?limit=${limit}`;
try {
const response = await fetch(apiUrl, {
method: 'GET',
headers: {
'Accept': 'application/json',
},
signal: AbortSignal.timeout(10000)
});
if (!response.ok) {
results[limit] = {
error: `API returned status ${response.status}`,
status: response.status
};
continue;
}
const articles = await response.json();
results[limit] = {
requested: parseInt(limit),
received: articles.length,
matches: articles.length === parseInt(limit),
firstArticleId: articles.length > 0 ? articles[0].id : null,
lastArticleId: articles.length > 0 ? articles[articles.length - 1].id : null
};
} catch (error) {
results[limit] = {
error: error instanceof Error ? error.message : 'Unknown error'
};
}
}
return NextResponse.json({
timestamp: new Date().toISOString(),
apiUrl: env.NEWS_API_URL,
results
});
} catch (error) {
console.error('Test endpoint error:', error);
return NextResponse.json(
{ error: 'Failed to test backend', details: error instanceof Error ? error.message : 'Unknown error' },
{ status: 500 }
);
}
}