Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hide required labels on response schemas #230

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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" });
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,10 @@ interface RowProps {
name: string;
schema: SchemaObject;
required: boolean;
type: "request" | "response";
}

function createRow({ name, schema, required }: RowProps) {
function createRow({ name, schema, required, type }: RowProps) {
return create("tr", {
children: create("td", {
children: [
Expand All @@ -49,19 +50,7 @@ function createRow({ name, schema, required }: RowProps) {
style: { opacity: "0.6" },
children: ` ${getSchemaName(schema, true)}`,
}),
guard(required, () => [
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 @@ -74,17 +63,17 @@ function createRow({ name, schema, required }: RowProps) {
children: createDescription(description),
})
),
createRows({ schema: schema }),
createRows({ schema: schema, type }),
],
}),
});
}

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

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

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

// primitive
return undefined;
}

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

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

export function createSchemaTable({ title, body, ...rest }: Props) {
export function createSchemaTable({ title, body, type, ...rest }: Props) {
if (body === undefined || body.content === undefined) {
return undefined;
}
Expand Down Expand Up @@ -249,19 +243,7 @@ export function createSchemaTable({ title, body, ...rest }: Props) {
style: { textAlign: "left" },
children: [
`${title} `,
guard(body.required, () => [
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 @@ -270,8 +252,43 @@ export function createSchemaTable({ title, body, ...rest }: Props) {
}),
}),
create("tbody", {
children: createRowsRoot({ schema: firstBody }),
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,6 +64,7 @@ export function createStatusCodesTable({ responses }: Props) {
body: {
content: responses[code].content,
},
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