import { useState } from 'react' import { getAuthToken } from '@/services/auth' const API_BASE_URL = import.meta.env.VITE_API_URL || 'http://localhost:4000' export function DemoRoute() { const [method, setMethod] = useState('GET') const [endpoint, setEndpoint] = useState('/api/test') const [headers, setHeaders] = useState('{}') const [body, setBody] = useState('{}') const [response, setResponse] = useState(null) const [error, setError] = useState('') const [loading, setLoading] = useState(false) const [history, setHistory] = useState>([]) const handleSubmit = async () => { setLoading(true) setError('') setResponse(null) try { const token = await getAuthToken() const url = `${API_BASE_URL}${endpoint}` let parsedHeaders: Record = {} try { parsedHeaders = JSON.parse(headers) } catch { throw new Error('Invalid headers JSON') } const fetchOptions: RequestInit = { method, headers: { 'Content-Type': 'application/json', ...(token && { Authorization: `Bearer ${token}` }), ...parsedHeaders, }, } if (method !== 'GET' && method !== 'HEAD') { try { const parsedBody = JSON.parse(body) fetchOptions.body = JSON.stringify(parsedBody) } catch { throw new Error('Invalid body JSON') } } const startTime = performance.now() const res = await fetch(url, fetchOptions) const endTime = performance.now() const duration = Math.round(endTime - startTime) let data const contentType = res.headers.get('content-type') if (contentType && contentType.includes('application/json')) { data = await res.json() } else { data = await res.text() } setResponse({ status: res.status, statusText: res.statusText, duration: `${duration}ms`, headers: Object.fromEntries(res.headers.entries()), data, }) // Add to history setHistory(prev => [ { method, endpoint, timestamp: new Date().toLocaleTimeString() }, ...prev.slice(0, 9), // Keep last 10 ]) } catch (err: any) { setError(err.message || 'An error occurred') } finally { setLoading(false) } } const loadFromHistory = (item: { method: string; endpoint: string }) => { setMethod(item.method) setEndpoint(item.endpoint) } const getStatusColor = (status: number) => { if (status >= 200 && status < 300) return 'bg-green-500' if (status >= 300 && status < 400) return 'bg-yellow-500' if (status >= 400) return 'bg-red-500' return 'bg-gray-500' } return (

API Demo & Testing

{API_BASE_URL}
{/* Request Panel */}

Request

) => setEndpoint(e.target.value)} className="flex-1 px-4 py-2 border rounded-md" />