mirror of
https://github.com/GitbookIO/gitbook.git
synced 2026-09-23 19:06:31 +00:00
64 lines
2.2 KiB
TypeScript
64 lines
2.2 KiB
TypeScript
import type { OpenAPIV3 } from '@gitbook/openapi-parser';
|
|
import type React from 'react';
|
|
|
|
interface OpenAPISchemaNameProps {
|
|
schema?: OpenAPIV3.SchemaObject;
|
|
propertyName?: string | React.JSX.Element;
|
|
required?: boolean;
|
|
type?: string;
|
|
}
|
|
|
|
/**
|
|
* Display the schema name row.
|
|
* It includes the property name, type, required and deprecated status.
|
|
*/
|
|
export function OpenAPISchemaName(props: OpenAPISchemaNameProps) {
|
|
const { schema, type, propertyName, required } = props;
|
|
|
|
const additionalItems = schema && getAdditionalItems(schema);
|
|
|
|
return (
|
|
<span className="openapi-schema-name">
|
|
{propertyName ? (
|
|
<span data-deprecated={schema?.deprecated} className="openapi-schema-propertyname">
|
|
{propertyName}
|
|
</span>
|
|
) : null}
|
|
<span>
|
|
{type ? <span className="openapi-schema-type">{type}</span> : null}
|
|
{additionalItems ? (
|
|
<span className="openapi-schema-type">{additionalItems}</span>
|
|
) : null}
|
|
</span>
|
|
{schema?.readOnly ? <span className="openapi-schema-readonly">read-only</span> : null}
|
|
{schema?.writeOnly ? (
|
|
<span className="openapi-schema-writeonly">write-only</span>
|
|
) : null}
|
|
{required ? (
|
|
<span className="openapi-schema-required">required</span>
|
|
) : (
|
|
<span className="openapi-schema-optional">optional</span>
|
|
)}
|
|
{schema?.deprecated ? <span className="openapi-deprecated">Deprecated</span> : null}
|
|
</span>
|
|
);
|
|
}
|
|
|
|
function getAdditionalItems(schema: OpenAPIV3.SchemaObject): string {
|
|
let additionalItems = '';
|
|
|
|
if (schema.minimum || schema.minLength || schema.minItems) {
|
|
additionalItems += ` · min: ${schema.minimum || schema.minLength || schema.minItems}`;
|
|
}
|
|
|
|
if (schema.maximum || schema.maxLength || schema.maxItems) {
|
|
additionalItems += ` · max: ${schema.maximum || schema.maxLength || schema.maxItems}`;
|
|
}
|
|
|
|
if (schema.nullable) {
|
|
additionalItems = ' | nullable';
|
|
}
|
|
|
|
return additionalItems;
|
|
}
|