interface ApiExplorerRequest { method: string; endpoint: string; headers: Record; body?: string; } const requestHistory: ApiExplorerRequest[] = []; function sendApiRequest(): void { const method = (document.getElementById('api-method') as HTMLSelectElement)?.value || 'GET'; const endpoint = (document.getElementById('api-endpoint') as HTMLInputElement)?.value || ''; const bodyText = (document.getElementById('api-body') as HTMLTextAreaElement)?.value || ''; const token = localStorage.getItem('token'); const headers: Record = { 'Content-Type': 'application/json' }; if (token) { headers['Authorization'] = `Bearer ${token}`; } const request: ApiExplorerRequest = { method, endpoint, headers, body: bodyText || undefined }; addToHistory(request); const startTime = performance.now(); fetch(endpoint, { method, headers, body: bodyText || undefined }) .then(async response => { const endTime = performance.now(); const duration = Math.round(endTime - startTime); const responseText = await response.text(); let responseData: unknown; try { responseData = JSON.parse(responseText); } catch { responseData = responseText; } displayResponse(response, responseData, duration); generateCurl(request); }) .catch(error => { displayError(error); }); } function displayResponse(response: Response, data: unknown, duration: number): void { const container = document.getElementById('api-response'); if (!container) return; const statusColor = response.ok ? 'var(--accent)' : 'var(--error)'; container.innerHTML = `
${response.status} ${response.statusText} ${duration}ms
${JSON.stringify(data, null, 2)}
`; } function displayError(error: Error): void { const container = document.getElementById('api-response'); if (!container) return; container.innerHTML = `

Error: ${error.message}

`; } function generateCurl(request: ApiExplorerRequest): void { const container = document.getElementById('curl-command'); if (!container) return; let curl = `curl -X ${request.method} '${request.endpoint}'`; Object.entries(request.headers).forEach(([key, value]) => { curl += ` \\\n -H '${key}: ${value}'`; }); if (request.body) { curl += ` \\\n -d '${request.body}'`; } container.textContent = curl; } function addToHistory(request: ApiExplorerRequest): void { requestHistory.unshift(request); if (requestHistory.length > 20) { requestHistory.pop(); } renderHistory(); } function renderHistory(): void { const container = document.getElementById('request-history'); if (!container) return; if (requestHistory.length === 0) { container.innerHTML = '

No requests yet

'; return; } container.innerHTML = requestHistory.slice(0, 10).map((req, i) => `
${req.method} ${req.endpoint}
`).join(''); } function loadFromHistory(index: number): void { const request = requestHistory[index]; if (!request) return; const methodSelect = document.getElementById('api-method') as HTMLSelectElement; const endpointInput = document.getElementById('api-endpoint') as HTMLInputElement; const bodyInput = document.getElementById('api-body') as HTMLTextAreaElement; if (methodSelect) methodSelect.value = request.method; if (endpointInput) endpointInput.value = request.endpoint; if (bodyInput) bodyInput.value = request.body || ''; } function copyCurl(): void { const curl = document.getElementById('curl-command')?.textContent; if (curl) { navigator.clipboard.writeText(curl); if ((window as any).showToast?.success) { (window as any).showToast.success('cURL copied to clipboard'); } } } function formatJson(): void { const bodyInput = document.getElementById('api-body') as HTMLTextAreaElement; if (!bodyInput) return; try { const parsed = JSON.parse(bodyInput.value); bodyInput.value = JSON.stringify(parsed, null, 2); } catch { if ((window as any).showToast?.error) { (window as any).showToast.error('Invalid JSON'); } } } (window as any).sendApiRequest = sendApiRequest; (window as any).loadFromHistory = loadFromHistory; (window as any).copyCurl = copyCurl; (window as any).formatJson = formatJson;