-
Notifications
You must be signed in to change notification settings - Fork 4k
/
Copy pathOpenAPISchemaName.tsx
61 lines (51 loc) · 2 KB
/
OpenAPISchemaName.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
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 (
<div 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}
{required ? <span className="openapi-schema-required">required</span> : null}
{schema?.deprecated ? <span className="openapi-deprecated">Deprecated</span> : null}
</div>
);
}
function getAdditionalItems(schema: OpenAPIV3.SchemaObject): string {
let additionalItems = '';
if (schema.minimum || schema.minLength) {
additionalItems += ` · min: ${schema.minimum || schema.minLength}`;
}
if (schema.maximum || schema.maxLength) {
additionalItems += ` · max: ${schema.maximum || schema.maxLength}`;
}
// If the schema has a default value, we display it
if (typeof schema.default !== 'undefined') {
additionalItems += ` · default: ${schema.default}`;
}
if (schema.nullable) {
additionalItems = ' | nullable';
}
return additionalItems;
}