|
| 1 | +import { Button } from "../../components/ui/button"; |
| 2 | +import { Textarea } from "../../components/ui/textarea"; |
| 3 | +import fetchData from "../../api/fetchData"; |
| 4 | +import { useState } from "react"; |
| 5 | + |
| 6 | +type MethodType = "GET" | "POST" | "PUT" | "DELETE"; |
| 7 | + |
| 8 | +export default function ApiTestForm() { |
| 9 | + const [url, setUrl] = useState(""); |
| 10 | + const [method, setMethod] = useState<MethodType>("GET"); |
| 11 | + const [body, setBody] = useState("{}"); |
| 12 | + |
| 13 | + const handleSubmit = async (e: React.FormEvent<HTMLFormElement>) => { |
| 14 | + e.preventDefault(); |
| 15 | + |
| 16 | + if (method !== "GET") { |
| 17 | + const confirmChange = window.confirm( |
| 18 | + "⚠️ 이 요청은 실제 DB에 영향을 줄 수 있습니다. 진행하시겠습니까?" |
| 19 | + ); |
| 20 | + if (!confirmChange) { |
| 21 | + console.log("%c요청이 취소되었습니다.", "color: orange;"); |
| 22 | + return; |
| 23 | + } |
| 24 | + } |
| 25 | + |
| 26 | + try { |
| 27 | + const parsedBody = body ? JSON.parse(body) : undefined; |
| 28 | + |
| 29 | + console.log( |
| 30 | + "%cAPI 요청 정보:\n", |
| 31 | + "background: #006980; color: white; padding: 4px; border-radius: 4px;", |
| 32 | + { url, method, parsedBody } |
| 33 | + ); |
| 34 | + const res = await fetchData({ |
| 35 | + url, |
| 36 | + method, |
| 37 | + body: parsedBody, |
| 38 | + }); |
| 39 | + |
| 40 | + console.log( |
| 41 | + "%cAPI 요청 결과:\n", |
| 42 | + "background: green; color: white; padding: 4px; border-radius: 4px;", |
| 43 | + res |
| 44 | + ); |
| 45 | + } catch (err) { |
| 46 | + console.log("에러 발생:", err); |
| 47 | + } |
| 48 | + }; |
| 49 | + |
| 50 | + return ( |
| 51 | + <form onSubmit={handleSubmit} className="flex flex-col gap-[10px]"> |
| 52 | + <div> |
| 53 | + <label className="block font-semibold mb-2">URL</label> |
| 54 | + <input |
| 55 | + type="text" |
| 56 | + value={url} |
| 57 | + onChange={(e) => setUrl(e.target.value)} |
| 58 | + className="w-full p-2 border rounded" |
| 59 | + placeholder="/api 이하의 엔드포인트를 전부 입력해주세요(예: /admin-support/releases?page=0&perPage=10)" |
| 60 | + required |
| 61 | + /> |
| 62 | + </div> |
| 63 | + <div> |
| 64 | + <label className="block font-semibold mb-2">HTTP Method</label> |
| 65 | + <select |
| 66 | + value={method} |
| 67 | + onChange={(e) => setMethod(e.target.value as MethodType)} |
| 68 | + className="w-full p-2 border rounded" |
| 69 | + > |
| 70 | + <option value="GET">GET</option> |
| 71 | + <option value="POST">POST</option> |
| 72 | + <option value="PUT">PUT</option> |
| 73 | + <option value="DELETE">DELETE</option> |
| 74 | + </select> |
| 75 | + </div> |
| 76 | + <div> |
| 77 | + <label className="block font-semibold mb-2">Body (JSON)</label> |
| 78 | + <Textarea |
| 79 | + value={body} |
| 80 | + onChange={(e) => setBody(e.target.value)} |
| 81 | + className="w-full p-2 border rounded" |
| 82 | + rows={5} |
| 83 | + placeholder='json 형식으로 입력해주세요 {"key": "value"}' |
| 84 | + disabled={method === "GET" || method === "DELETE"} |
| 85 | + /> |
| 86 | + </div> |
| 87 | + <Button type="submit">API 보내기</Button> |
| 88 | + </form> |
| 89 | + ); |
| 90 | +} |
0 commit comments