|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import { useState } from 'react'; |
| 4 | + |
| 5 | +export default function ApiPage() { |
| 6 | + const [baseUrl, setBaseUrl] = useState<string>( |
| 7 | + process.env.NEXT_PUBLIC_API_BASE || 'http://localhost:8000' |
| 8 | + ); |
| 9 | + const [query, setQuery] = useState<string>('/collections/addresses/items?limit=5'); |
| 10 | + const [resp, setResp] = useState<any>(null); |
| 11 | + const [loading, setLoading] = useState(false); |
| 12 | + const [error, setError] = useState<string | null>(null); |
| 13 | + |
| 14 | + const run = async () => { |
| 15 | + setLoading(true); |
| 16 | + setError(null); |
| 17 | + setResp(null); |
| 18 | + try { |
| 19 | + const url = new URL(query, baseUrl).toString(); |
| 20 | + const r = await fetch(url); |
| 21 | + const data = await r.json(); |
| 22 | + if (!r.ok) throw new Error(data?.error || `HTTP ${r.status}`); |
| 23 | + setResp(data); |
| 24 | + } catch (e: any) { |
| 25 | + setError(e.message || String(e)); |
| 26 | + } finally { |
| 27 | + setLoading(false); |
| 28 | + } |
| 29 | + }; |
| 30 | + |
| 31 | + return ( |
| 32 | + <section> |
| 33 | + <h1>API</h1> |
| 34 | + <p>Try the reference API endpoints.</p> |
| 35 | + |
| 36 | + <div style={{ display: 'grid', gap: 8, maxWidth: 720 }}> |
| 37 | + <label> |
| 38 | + API Base URL |
| 39 | + <input |
| 40 | + style={{ width: '100%', padding: 8, marginTop: 4 }} |
| 41 | + value={baseUrl} |
| 42 | + onChange={(e) => setBaseUrl(e.target.value)} |
| 43 | + placeholder="https://api.example.org" |
| 44 | + /> |
| 45 | + </label> |
| 46 | + <label> |
| 47 | + Path/Query |
| 48 | + <input |
| 49 | + style={{ width: '100%', padding: 8, marginTop: 4 }} |
| 50 | + value={query} |
| 51 | + onChange={(e) => setQuery(e.target.value)} |
| 52 | + placeholder="/collections/addresses/items?limit=5" |
| 53 | + /> |
| 54 | + </label> |
| 55 | + <div> |
| 56 | + <button className="btn" onClick={run} disabled={loading}> |
| 57 | + {loading ? 'Fetching…' : 'Fetch'} |
| 58 | + </button> |
| 59 | + </div> |
| 60 | + </div> |
| 61 | + |
| 62 | + <div style={{ marginTop: 24 }}> |
| 63 | + {error && <div style={{ color: 'crimson' }}>Error: {error}</div>} |
| 64 | + {resp && ( |
| 65 | + <div> |
| 66 | + <div>Type: {resp.type || '—'}</div> |
| 67 | + {Array.isArray(resp.features) && ( |
| 68 | + <div>Features: {resp.features.length}</div> |
| 69 | + )} |
| 70 | + <pre style={{ background: '#f6f8fa', padding: 12, borderRadius: 8, overflowX: 'auto' }}> |
| 71 | +{JSON.stringify(resp, null, 2)} |
| 72 | + </pre> |
| 73 | + </div> |
| 74 | + )} |
| 75 | + </div> |
| 76 | + |
| 77 | + <h2>Usage</h2> |
| 78 | + <p>JavaScript:</p> |
| 79 | + <pre style={{ background: '#f6f8fa', padding: 12, borderRadius: 8, overflowX: 'auto' }}> |
| 80 | +{`const url = 'https://bharataddress.github.io/collections/addresses/items?limit=5'; |
| 81 | +const resp = await fetch(url); |
| 82 | +const data = await resp.json();`} |
| 83 | + </pre> |
| 84 | + |
| 85 | + <p>Python (requests):</p> |
| 86 | + <pre style={{ background: '#f6f8fa', padding: 12, borderRadius: 8, overflowX: 'auto' }}> |
| 87 | +{`import requests |
| 88 | +url = 'https://bharataddress.github.io/collections/addresses/items?limit=5' |
| 89 | +r = requests.get(url, timeout=30) |
| 90 | +r.raise_for_status() |
| 91 | +data = r.json()`} |
| 92 | + </pre> |
| 93 | + </section> |
| 94 | + ); |
| 95 | +} |
| 96 | + |
0 commit comments