diff --git a/frontend/src/components/TryItPanel.jsx b/frontend/src/components/TryItPanel.jsx new file mode 100644 index 000000000..a0a80c6b9 --- /dev/null +++ b/frontend/src/components/TryItPanel.jsx @@ -0,0 +1,139 @@ +import { useState } from "react"; + +export default function TryItPanel({ endpoint, method = "POST" }) { + const [apiKey, setApiKey] = useState(""); + const [jsonBody, setJsonBody] = useState(`{}`); + const [response, setResponse] = useState(""); + const [status, setStatus] = useState(""); + const [error, setError] = useState(""); + const [params, setParams] = useState({}); + + function buildEndpoint() { + let url = endpoint; + Object.keys(params).forEach(key => { + url = url.replace(`:${key}`, params[key]); + }); + return url; +} + + + async function sendRequest() { + // Stop if API key is missing + if (!apiKey || apiKey.trim() === "") { + setError("You need an API key. Go to Dashboard → Create Project → Copy API key."); + return; + } + + // Clear previous error + setError(""); + + try { + const res = await fetch(`https://api.urbackend.bitbros.in${buildEndpoint()}`, { + + + + method, + headers: { + "Content-Type": "application/json", + "x-api-key": apiKey, + }, + body: method !== "GET" ? jsonBody : null, + }); + + const data = await res.text(); + setStatus(res.status); + setResponse(data); + } catch (err) { + setResponse(err.message); + } + } + + return ( +
+

+ Try It Out +

+ +
+ + setApiKey(e.target.value)} + placeholder="YOUR API KEY" + /> +
+{endpoint.includes(":") && ( +
+ + + {endpoint + .match(/:([a-zA-Z0-9_]+)/g) + ?.map(p => { + const key = p.replace(":", ""); + return ( + setParams({ ...params, [key]: e.target.value })} + style={{ marginBottom: "0.5rem" }} + /> + ); + })} +
+)} + + {method !== "GET" && ( +
+ +