'use client' import { useEffect, useState } from 'react' import { notFound } from 'next/navigation' import { AxiosError } from 'axios' import api from '@/services/api' /** Result of probing a single backend endpoint. */ interface EndpointResult { status?: number data?: unknown error?: unknown } type Results = Record function getHealthUrl(): string { const apiUrl = process.env.NEXT_PUBLIC_API_URL || 'http://localhost:4000/api' return `${apiUrl.replace(/\/?api\/?$/, '')}/health` } function toErrorResult(error: unknown): EndpointResult { if (error instanceof AxiosError) { return { status: error.response?.status, error: error.response?.data ?? error.message } } if (error instanceof Error) { return { error: error.message } } return { error: 'Unknown error' } } /** * Developer-only diagnostic page that probes core backend endpoints. * Not linked from navigation and gated to non-production builds so it is * never a publicly routable page in production. */ export default function ApiTestPage() { const [results, setResults] = useState({}) const isEnabled = process.env.NODE_ENV !== 'production' useEffect(() => { if (!isEnabled) return const testEndpoints = async () => { const tests: Results = {} try { const res = await fetch(getHealthUrl()) tests.health = { status: res.status, data: await res.json() } } catch (error: unknown) { tests.health = toErrorResult(error) } try { const res = await api.get('/auth/me') tests.authMe = { status: res.status, data: res.data } } catch (error: unknown) { tests.authMe = toErrorResult(error) } try { const res = await api.get('/posts') tests.posts = { status: res.status, data: res.data } } catch (error: unknown) { tests.posts = toErrorResult(error) } try { const res = await api.get('/github/popular') tests.githubPopular = { status: res.status, data: res.data } } catch (error: unknown) { tests.githubPopular = toErrorResult(error) } setResults(tests) } void testEndpoints() }, [isEnabled]) if (!isEnabled) { notFound() } return (

API Connection Test

Developer diagnostic page (disabled in production).

API Base URL: {process.env.NEXT_PUBLIC_API_URL || 'Not set'}
Results:
            {JSON.stringify(results, null, 2)}
          
) }