'use client'; import React from 'react'; import type { ApiEndpoint } from '../../../types'; import { endpointAnchor } from '../anchor'; import { CodeSamples } from './CodeSamples'; import { EndpointDocProvider } from './context'; import { EndpointHeader } from './Header'; import { Parameters } from './Parameters'; import { RequestBody } from './RequestBody'; import { Responses } from './Responses'; import { Section } from './Section'; import type { SectionId } from './types'; interface EndpointDocProps { endpoint: ApiEndpoint; /** Is this endpoint currently loaded in the sticky playground? */ isLoadedInPlayground: boolean; onTryIt: () => void; /** Scoping prefix for the anchor, so endpoints from different schemas * don't collide on a single page. Falls back to ``endpoint.schemaId``. */ schemaId?: string | null; } /** Card that documents one API endpoint: header + a stack of collapsible * sections (parameters, request body, responses, code samples). The * component itself is a thin orchestrator — each child folder owns its * own rendering concerns and reads the endpoint identity from context. */ export function EndpointDoc({ endpoint, isLoadedInPlayground, onTryIt, schemaId }: EndpointDocProps) { const scopedSchemaId = schemaId ?? endpoint.schemaId ?? null; const anchor = endpointAnchor(endpoint, scopedSchemaId); const pathParams = endpoint.parameters?.filter((p) => endpoint.path.includes(`{${p.name}}`)) ?? []; const queryParams = endpoint.parameters?.filter((p) => !endpoint.path.includes(`{${p.name}}`)) ?? []; const hasParameters = pathParams.length > 0 || queryParams.length > 0; const hasResponses = (endpoint.responses?.length ?? 0) > 0; // Collect section ids that actually render so the header's // expand/collapse-all toggle only touches visible sections. // ``codeSamples`` is always present — it synthesises a snippet // even for parameter-less endpoints. const presentSections: SectionId[] = []; if (hasParameters) presentSections.push('parameters'); if (endpoint.requestBody) presentSections.push('requestBody'); presentSections.push('codeSamples'); if (hasResponses) presentSections.push('responses'); return (
{hasParameters && (
)} {endpoint.requestBody && (
)}
{hasResponses && (
)}
); }