api-request-builder.tsx 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. 'use client';
  2. import { useId, useState } from 'react';
  3. import { buildCurl, buildFetchSnippet, type ApiRequestInput, type HttpMethod } from '@/lib/xray/api-client';
  4. import { ToolFrame } from './tool-frame';
  5. import { TextField, SelectField } from './shared/fields';
  6. import { OutputBlock } from './shared/output-block';
  7. const METHODS: readonly HttpMethod[] = ['GET', 'POST', 'PUT', 'DELETE'];
  8. export function ApiRequestBuilder() {
  9. const [baseUrl, setBaseUrl] = useState('https://panel.example.com:2053');
  10. const [token, setToken] = useState('');
  11. const [path, setPath] = useState('/panel/api/inbounds/list');
  12. const [method, setMethod] = useState<HttpMethod>('GET');
  13. const [body, setBody] = useState('');
  14. const bodyId = useId();
  15. const showBody = method === 'POST' || method === 'PUT';
  16. const input: ApiRequestInput = { baseUrl, token: token || '<token>', path, method, body };
  17. function reset() {
  18. setBaseUrl('https://panel.example.com:2053');
  19. setToken('');
  20. setPath('/panel/api/inbounds/list');
  21. setMethod('GET');
  22. setBody('');
  23. }
  24. return (
  25. <ToolFrame
  26. title="API request builder"
  27. description="Build an authenticated cURL command or fetch() snippet for any 3x-ui panel API endpoint under /panel/api/*."
  28. onReset={reset}
  29. >
  30. <div className="grid grid-cols-1 gap-4 sm:grid-cols-2">
  31. <TextField label="Panel base URL" value={baseUrl} onChange={setBaseUrl} />
  32. <TextField
  33. label="API token (Bearer)"
  34. value={token}
  35. onChange={setToken}
  36. placeholder="Settings → Security → API Token"
  37. />
  38. <TextField label="Endpoint path" value={path} onChange={setPath} />
  39. <SelectField
  40. label="Method"
  41. value={method}
  42. onChange={(v) => setMethod(v as HttpMethod)}
  43. options={METHODS}
  44. />
  45. </div>
  46. {showBody ? (
  47. <div className="mt-4 flex flex-col gap-1.5">
  48. <label htmlFor={bodyId} className="text-sm font-medium">
  49. Request body (JSON)
  50. </label>
  51. <textarea
  52. id={bodyId}
  53. dir="ltr"
  54. value={body}
  55. onChange={(e) => setBody(e.target.value)}
  56. rows={4}
  57. placeholder='{"id": 1}'
  58. className="rounded-lg border bg-fd-background px-3 py-2 font-mono text-sm outline-none transition-colors focus-visible:border-fd-primary focus-visible:ring-2 focus-visible:ring-fd-ring/30"
  59. />
  60. </div>
  61. ) : null}
  62. <div className="mt-4 grid grid-cols-1 gap-4">
  63. <OutputBlock label="cURL" value={buildCurl(input)} />
  64. <OutputBlock label="fetch()" value={buildFetchSnippet(input)} />
  65. </div>
  66. </ToolFrame>
  67. );
  68. }