EndpointRow.tsx 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. import { Table, Tag } from 'antd';
  2. import type { ColumnsType } from 'antd/es/table';
  3. import { methodColors, safeInlineHtml } from './endpoints.js';
  4. import CodeBlock from './CodeBlock';
  5. import './EndpointRow.css';
  6. interface Param {
  7. name: string;
  8. in?: string;
  9. type?: string;
  10. desc?: string;
  11. }
  12. export interface Endpoint {
  13. method: string;
  14. path: string;
  15. summary?: string;
  16. params?: Param[];
  17. body?: string;
  18. response?: string;
  19. errorResponse?: string;
  20. }
  21. const paramColumns: ColumnsType<Param> = [
  22. { title: 'Name', dataIndex: 'name', key: 'name', width: 180 },
  23. { title: 'In', dataIndex: 'in', key: 'in', width: 100 },
  24. { title: 'Type', dataIndex: 'type', key: 'type', width: 120 },
  25. { title: 'Description', dataIndex: 'desc', key: 'desc' },
  26. ];
  27. export default function EndpointRow({ endpoint }: { endpoint: Endpoint }) {
  28. const tagColor = (methodColors as Record<string, string>)[endpoint.method] || 'default';
  29. const hasParams = Array.isArray(endpoint.params) && endpoint.params.length > 0;
  30. return (
  31. <div className="endpoint-row">
  32. <div className="endpoint-header">
  33. <Tag color={tagColor} className="method-tag">{endpoint.method}</Tag>
  34. <code className="endpoint-path">{endpoint.path}</code>
  35. </div>
  36. {endpoint.summary && (
  37. <p
  38. className="endpoint-summary"
  39. dangerouslySetInnerHTML={{ __html: safeInlineHtml(endpoint.summary) }}
  40. />
  41. )}
  42. {hasParams && (
  43. <div className="endpoint-block">
  44. <div className="block-label">Parameters</div>
  45. <Table
  46. columns={paramColumns}
  47. dataSource={endpoint.params}
  48. pagination={false}
  49. size="small"
  50. rowKey="name"
  51. />
  52. </div>
  53. )}
  54. {endpoint.body && (
  55. <div className="endpoint-block">
  56. <div className="block-label">Request body</div>
  57. <CodeBlock code={endpoint.body} lang="json" />
  58. </div>
  59. )}
  60. {endpoint.response && (
  61. <div className="endpoint-block">
  62. <div className="block-label">Response</div>
  63. <CodeBlock code={endpoint.response} lang="json" />
  64. </div>
  65. )}
  66. {endpoint.errorResponse && (
  67. <div className="endpoint-block">
  68. <div className="block-label error-label">Error response</div>
  69. <CodeBlock code={endpoint.errorResponse} lang="json" />
  70. </div>
  71. )}
  72. </div>
  73. );
  74. }