Created
May 30, 2026 04:57
-
-
Save mentix02/96f417444895e95d94248567fb7f5c26 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { useRef, type SubmitEvent } from "react"; | |
| export function APITester() { | |
| const responseInputRef = useRef<HTMLTextAreaElement>(null); | |
| const testEndpoint = async (e: SubmitEvent<HTMLFormElement>) => { | |
| e.preventDefault(); | |
| try { | |
| const form = e.currentTarget; | |
| const formData = new FormData(form); | |
| const endpoint = formData.get("endpoint") as string; | |
| const url = new URL(endpoint, location.href); | |
| const method = formData.get("method") as string; | |
| const res = await fetch(url, { method }); | |
| const data = await res.json(); | |
| responseInputRef.current!.value = JSON.stringify(data, null, 2); | |
| } catch (error) { | |
| responseInputRef.current!.value = String(error); | |
| } | |
| }; | |
| return ( | |
| <div className="api-tester"> | |
| <form onSubmit={testEndpoint} className="endpoint-row"> | |
| <select name="method" className="method"> | |
| <option value="GET">GET</option> | |
| <option value="PUT">PUT</option> | |
| </select> | |
| <input type="text" name="endpoint" defaultValue="/api/hello" className="url-input" placeholder="/api/hello" /> | |
| <button type="submit" className="send-button"> | |
| Send | |
| </button> | |
| </form> | |
| <textarea ref={responseInputRef} readOnly placeholder="Response will appear here..." className="response-area" /> | |
| </div> | |
| ); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment