import Link from 'next/link'; import { Fragment, type ReactNode } from 'react'; import { createHighlighter } from 'shiki'; import { getApiOperation, getApiVersion, getOperationSnippets, getSdkLanguages, getSecurityScheme, isOperatorOnly, exampleRequestBody, requestBodyChoice, schemaFields, schemaType, unionDiscriminator, unionVariants, unwrapSchema, variantLabels, type ApiParameter, type JsonSchema, } from '@/lib/api-reference'; import { Heading } from './heading'; import { methodStyle } from './method-badge'; import { Tabs } from './tabs'; import { VariantTabs } from './variant-tabs'; const highlighterPromise = createHighlighter({ themes: ['material-theme-palenight'], langs: ['typescript', 'python', 'php', 'csharp', 'go', 'java', 'ruby', 'json'], }); const STATUS_STYLES: Record = { '2': 'border-[#B9E5CB] bg-[#EAF9F0] text-[#167645]', '3': 'border-[#B7D4FF] bg-[#EAF3FF] text-[#1D5FBF]', '4': 'border-[#F1D7A8] bg-[#FFF6E5] text-[#9A5A00]', '5': 'border-[#F2BFC4] bg-[#FFF0F1] text-[#B52D3B]', }; const NEUTRAL_BADGE = 'border-cp-border bg-white text-cp-navy'; const CARD = 'border-cp-border overflow-hidden rounded-[14px] border bg-white'; const CARD_HEAD = 'border-cp-borderSoft flex flex-wrap items-center gap-2.5 border-b bg-[#F8FAFE] px-4 py-3'; const LEAD = 'text-cp-ink mt-4 max-w-[70ch] font-sans text-[16.5px] leading-[1.7]'; const HINT = 'text-cp-muted mt-4 max-w-[70ch] font-sans text-[15px] leading-[1.65]'; /** Nested schemas are inlined to this depth, then reduced to their type name. */ const MAX_FIELD_DEPTH = 3; const SNIPPET_PANEL = '[&_.shiki]:!m-0 [&_.shiki]:overflow-x-auto [&_.shiki]:!bg-[#0E1A40] [&_.shiki]:px-[18px] [&_.shiki]:py-[17px] [&_.shiki]:font-mono [&_.shiki]:text-[13px] [&_.shiki]:leading-[1.8] [&_.snippet-frame]:opacity-60'; /** * The generated SDK examples for one operation, in all seven languages * behind the reference-wide language switcher. Used by every operation * page and embeddable in prose pages (``) * so hand-written pages never fork from the generated examples. */ export async function ApiSnippet({ operationId }: { operationId: string }) { const snippets = getOperationSnippets(operationId); const highlighter = await highlighterPromise; const highlighted = snippets.map((snippet) => ({ ...snippet, html: highlighter.codeToHtml(snippet.source, { lang: snippet.fence, theme: 'material-theme-palenight', transformers: [ { // De-emphasise the shared frame (imports + client construction) // so the operation call stays the visual hero of every example. line(node, line) { if (line <= snippet.frameLines) this.addClassToHast(node, 'snippet-frame'); }, }, ], }), })); return ( snippet.label)} storageKey="cloudpdf-sdk-language"> {highlighted.map((snippet) => (
{snippet.note ? (
{snippet.note}
) : null}
))} ); } /** * The per-language client-construction block alone — for prose pages * that teach setup (authentication) without a specific operation call. */ export async function ApiClientSetup() { const languages = getSdkLanguages(); const highlighter = await highlighterPromise; const blocks = languages.map((language) => ({ ...language, html: highlighter.codeToHtml(language.frame, { lang: language.fence, theme: 'material-theme-palenight', }), })); return ( block.label)} storageKey="cloudpdf-sdk-language"> {blocks.map((block) => (
))} ); } export async function ApiOperation({ operationId }: { operationId: string }) { const { method, path, operation } = getApiOperation(operationId); return ( <>
{method} v{getApiVersion()}

{operation.summary}

{operation.description ? (

{operation.description}

) : null} {isOperatorOnly(operation) ? (

Self-hosted surface. On managed CloudPDF this is operated by the platform — your account is already a tenant. Start at{' '} Tenant documents .

) : null} {operation.parameters?.length ? (
) : null} {operation.requestBody ? (
{operation.requestBody.description ? (

{operation.requestBody.description}

) : null}
{Object.entries(operation.requestBody.content ?? {}).map(([contentType, media]) => (
))}
) : null}

The selected SDK is remembered across the API reference. Values are examples; replace them with identifiers and input from your application.

{Object.entries(operation.responses).map(([status, response]) => { const contents = Object.entries(response.content ?? {}); return (
{status} {response.description ?? 'Response'}
{contents.length ? ( contents.map(([contentType, media]) => ( )) ) : (

No response body.

)}
); })}
); } /** * The body a caller actually sends, one tab per shape when the body offers * a choice. Derived from the schema, so it cannot drift from the field list * above it — and it is the one place the reference spells out the union * branch the generated SDK example did not take. */ async function RequestBodyExample({ operation }: { operation: Operation }) { const choice = requestBodyChoice(operation); const examples = choice ? choice.variants.map((variant, index) => ({ label: choice.labels[index], json: exampleRequestBody(operation, { property: choice.property, variant }), })) : [{ label: 'application/json', json: exampleRequestBody(operation) }]; // Binary and scalar bodies have nothing to spell out. if (examples.every((example) => ['', '{}', 'null'].includes(example.json))) return null; const highlighter = await highlighterPromise; const blocks = examples.map((example) => ({ ...example, html: highlighter.codeToHtml(example.json, { lang: 'json', theme: 'material-theme-palenight' }), })); return ( <>

{choice ? 'A complete body for each shape. Strings stand in for your own values.' : 'A complete body with every required field. Strings stand in for your own values.'}

block.label)}> {blocks.map((block) => (
))} ); } /** * Fern generates one example per operation and takes the first branch of * any union in the body, so an operation whose body offers a choice ships * an example that silently shows one side of it. Say which side — but only * after confirming the generated snippet really took that branch. */ function SdkChoiceNote({ operation, operationId }: { operation: Operation; operationId: string }) { const choice = requestBodyChoice(operation); if (!choice) return null; const [shown, ...rest] = choice.labels; const source = getOperationSnippets(operationId).find( (snippet) => snippet.language === 'typescript', )?.source; if (!source?.includes(`"${shown}"`)) return null; return (

This body offers a choice, and the example takes one branch: it sends{' '} {choice.property ? ( <> {choice.property} as {shown} ) : ( {shown} )} . The{' '} request body {' '} above carries {rest.length === 1 ? 'the other shape' : `the other ${rest.length} shapes`} —{' '} {rest.map((label, index) => ( {index > 0 ? ', ' : ''} {label} ))} .

); } function Section({ id, title, children }: { id: string; title: string; children: ReactNode }) { return (
{title} {children}
); } /** * Path parameters read far better when they stand out from the static * segments. The before each "/" lets a long path wrap between segments * rather than through the middle of an identifier. */ function EndpointPath({ path }: { path: string }) { return ( <> {path .split(/(\{[^}]+\})/g) .filter(Boolean) .map((segment, index) => segment.startsWith('{') ? ( {segment} ) : ( {segment.split('/').map((part, partIndex) => ( {partIndex > 0 ? ( <> / ) : null} {part} ))} ), )} ); } function Authentication({ operation }: { operation: Operation }) { const schemes = [ ...new Set((operation.security ?? []).flatMap((alternative) => Object.keys(alternative))), ]; const scopes = operation['x-required-scope'] ?? []; const capabilities = operation['x-required-capability'] ?? []; if (!schemes.length && !scopes.length && !capabilities.length) return null; return (
{schemes.length > 1 ? (

Any one of these credentials is accepted.

) : null} {schemes.length ? (
{schemes.map((scheme) => (
{getSecurityScheme(scheme)?.['x-docs-title'] ?? scheme}
{getSecurityScheme(scheme)?.description ? (

{getSecurityScheme(scheme)?.description}

) : null}
))}
) : null} {scopes.length ? : null} {capabilities.length ? : null}
); } function ChipRow({ label, values }: { label: string; values: string[] }) { return (
{label} {values.map((value) => ( {value} ))}
); } type Operation = ReturnType['operation']; type Side = 'request' | 'response'; type Field = { key: string; name: string; schema?: JsonSchema; required?: boolean; description?: string; location?: string; }; function parameterField(parameter: ApiParameter): Field { return { key: `${parameter.in}:${parameter.name}`, name: parameter.name, schema: parameter.schema, required: parameter.required, description: parameter.description, location: parameter.in, }; } function fieldsOf(schema?: JsonSchema): Field[] { return schemaFields(schema).map((field) => ({ key: field.name, ...field })); } /** * A union's branches as tabs. The same control serves the body root and * every union-typed field below it, so a choice looks like a choice * wherever the reader meets one. */ function VariantPanels({ variants, side, depth, }: { variants: JsonSchema[]; side: Side; depth: number; }) { const labels = variantLabels(variants); const branches = variants.map((variant, index) => ({ label: labels[index], description: unwrapSchema(variant)?.description, fields: fieldsOf(variant), })); return ( {branches.map((branch) => (
{branch.description ? (

{branch.description}

) : null}
))}
); } function FieldList({ fields, side, depth = 0 }: { fields: Field[]; side: Side; depth?: number }) { return (
{fields.map((field) => ( ))}
); } function FieldRow({ field, side, depth }: { field: Field; side: Side; depth: number }) { // A union nests one visual level (the tabs) but not one schema level: its // branches describe this field, so they spend the same depth budget its // properties would have. Without that, a union inside a union inside a // union — annotation, action target, destination — runs out before it // reaches the fields that matter. const variants = depth < MAX_FIELD_DEPTH ? unionVariants(field.schema) : []; const children = depth < MAX_FIELD_DEPTH && !variants.length ? fieldsOf(field.schema) : []; return (
{field.name} {field.location ? ( {field.location} ) : null} {schemaType(field.schema)} {field.schema?.default !== undefined ? ( default {JSON.stringify(field.schema.default)} ) : null}
{field.description ? (

{field.description}

) : null} {variants.length ? (
) : children.length ? (
) : null}
); } /** * On the way in, the caller needs to know what it must send. On the way out, * every field is normally present, so flagging each one "required" is noise — * the useful signal there is the handful that may be missing. */ function Optionality({ side, required }: { side: Side; required?: boolean }) { if (side === 'request') return required ? : null; return required ? null : ( optional ); } function RequiredPill() { return ( required ); } /** * One content type of a request or response: the media header, then the fields. * Bodies with no properties (binary downloads, scalars) show their type alone. */ function SchemaBlock({ contentType, schema, side, required, }: { contentType: string; schema?: JsonSchema; side: Side; required?: boolean; }) { const resolved = schema ? unwrapSchema(schema) : undefined; const variants = unionVariants(schema); const fields = fieldsOf(schema); return ( <>
{contentType} {schema ? ( {schemaType(schema)} ) : null} {required ? : null}
{resolved?.description ? (

{resolved.description}

) : null} {variants.length ? (
) : fields.length ? ( ) : null} ); }