api-request-builder.tsx 2.7 KB

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