Skip to content

Commit

Permalink
Add Optional label in response schemas
Browse files Browse the repository at this point in the history
  • Loading branch information
baptisteArno committed Nov 28, 2022
1 parent e157c95 commit f5dc5f7
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 70 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ interface Props {
}

export function createRequestBodyTable({ title, body }: Props) {
return createSchemaTable({ title, body });
return createSchemaTable({ title, body, type: "request" });
}
119 changes: 53 additions & 66 deletions packages/docusaurus-plugin-openapi/src/markdown/createSchemaTable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,25 +34,14 @@ function resolveAllOf(allOf: SchemaObject[]) {
return { properties, required };
}

interface RowOptions {
showRequiredLabel?: boolean;
}

const defaultRowOptions: RowOptions = { showRequiredLabel: true };

interface RowProps {
name: string;
schema: SchemaObject;
required: boolean;
options?: RowOptions;
type: "request" | "response";
}

function createRow({
name,
schema,
required,
options = defaultRowOptions,
}: RowProps) {
function createRow({ name, schema, required, type }: RowProps) {
return create("tr", {
children: create("td", {
children: [
Expand All @@ -61,19 +50,7 @@ function createRow({
style: { opacity: "0.6" },
children: ` ${getSchemaName(schema, true)}`,
}),
guard(required && options.showRequiredLabel, () => [
create("span", {
style: { opacity: "0.6" },
children: " — ",
}),
create("strong", {
style: {
fontSize: "var(--ifm-code-font-size)",
color: "var(--openapi-required)",
},
children: " REQUIRED",
}),
]),
...parseTitleLabel({ required, type }),
guard(getQualifierMessage(schema), (message) =>
create("div", {
style: { marginTop: "var(--ifm-table-cell-padding)" },
Expand All @@ -86,21 +63,17 @@ function createRow({
children: createDescription(description),
})
),
createRows({ schema: schema, options }),
createRows({ schema: schema, type }),
],
}),
});
}

interface RowsProps {
interface RowsProps extends Pick<RowProps, "type"> {
schema: SchemaObject;
options?: RowOptions;
}

function createRows({
schema,
options = defaultRowOptions,
}: RowsProps): string | undefined {
function createRows({ schema, type }: RowsProps): string | undefined {
// object
if (schema.properties !== undefined) {
return createFullWidthTable({
Expand All @@ -116,7 +89,7 @@ function createRows({
required: Array.isArray(schema.required)
? schema.required.includes(key)
: false,
options,
type,
})
),
}),
Expand All @@ -137,7 +110,7 @@ function createRows({
name: key,
schema: val,
required: Array.isArray(required) ? required.includes(key) : false,
options,
type,
})
),
}),
Expand All @@ -146,22 +119,18 @@ function createRows({

// array
if (schema.items !== undefined) {
return createRows({ schema: schema.items, options });
return createRows({ schema: schema.items, type });
}

// primitive
return undefined;
}

interface RowsRootProps {
interface RowsRootProps extends Pick<RowProps, "type"> {
schema: SchemaObject;
options?: RowOptions;
}

function createRowsRoot({
schema,
options = defaultRowOptions,
}: RowsRootProps) {
function createRowsRoot({ schema, type }: RowsRootProps) {
// object
if (schema.properties !== undefined) {
return Object.entries(schema.properties).map(([key, val]) =>
Expand All @@ -171,7 +140,7 @@ function createRowsRoot({
required: Array.isArray(schema.required)
? schema.required.includes(key)
: false,
options,
type,
})
);
}
Expand All @@ -184,7 +153,7 @@ function createRowsRoot({
name: key,
schema: val,
required: Array.isArray(required) ? required.includes(key) : false,
options,
type,
})
);
}
Expand All @@ -198,7 +167,7 @@ function createRowsRoot({
style: { opacity: "0.6" },
children: ` ${getSchemaName(schema, true)}`,
}),
createRows({ schema: schema.items, options }),
createRows({ schema: schema.items, type }),
],
}),
});
Expand Down Expand Up @@ -239,15 +208,10 @@ interface Props {
description?: string;
required?: boolean;
};
options?: RowOptions;
type: "request" | "response";
}

export function createSchemaTable({
title,
body,
options = defaultRowOptions,
...rest
}: Props) {
export function createSchemaTable({ title, body, type, ...rest }: Props) {
if (body === undefined || body.content === undefined) {
return undefined;
}
Expand Down Expand Up @@ -279,19 +243,7 @@ export function createSchemaTable({
style: { textAlign: "left" },
children: [
`${title} `,
guard(body.required && options.showRequiredLabel, () => [
create("span", {
style: { opacity: "0.6" },
children: " — ",
}),
create("strong", {
style: {
fontSize: "var(--ifm-code-font-size)",
color: "var(--openapi-required)",
},
children: " REQUIRED",
}),
]),
...parseTitleLabel({ required: body.required, type }),
create("div", {
children: createDescription(body.description),
}),
Expand All @@ -300,8 +252,43 @@ export function createSchemaTable({
}),
}),
create("tbody", {
children: createRowsRoot({ schema: firstBody, options }),
children: createRowsRoot({ schema: firstBody, type }),
}),
],
});
}

const parseTitleLabel = ({
required,
type,
}: {
required?: boolean;
type: Props["type"];
}) => [
guard(required && type === "request", () => [
create("span", {
style: { opacity: "0.6" },
children: " — ",
}),
create("strong", {
style: {
fontSize: "var(--ifm-code-font-size)",
color: "var(--openapi-required)",
},
children: " REQUIRED",
}),
]),
guard(!required && type === "response", () => [
create("span", {
style: { opacity: "0.6" },
children: " — ",
}),
create("strong", {
style: {
fontSize: "var(--ifm-code-font-size)",
color: "var(--openapi-optional)",
},
children: " OPTIONAL",
}),
]),
];
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,7 @@ export function createStatusCodesTable({ responses }: Props) {
body: {
content: responses[code].content,
},
options: {
showRequiredLabel: false,
},
type: "response",
}),
}),
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
--api-sidebar-hidden-width: 30px;

--openapi-required: var(--openapi-code-red);
--openapi-optional: var(--openapi-code-dim);

--openapi-card-background-color: #f6f8fa;
--openapi-card-border-radius: var(--ifm-pre-border-radius);
Expand Down Expand Up @@ -62,6 +63,7 @@ html[data-theme="dark"] {
--openapi-monaco-border-color: #606770;

--openapi-required: var(--openapi-code-red);
--openapi-optional: var(--openapi-code-dim);

--openapi-card-background-color: var(--ifm-card-background-color);

Expand Down

0 comments on commit f5dc5f7

Please sign in to comment.