From 869216ecab81bf48e03e6a2a82cb64d7e654794d Mon Sep 17 00:00:00 2001 From: Alex Varchuk Date: Thu, 13 Jul 2023 18:25:25 +0300 Subject: [PATCH] feat: add x-tags --- src/components/SideMenu/MenuItem.tsx | 3 +- src/components/SideMenu/styled.elements.ts | 24 ++++++++------ src/services/MenuBuilder.ts | 38 +++++++++++++++++++++- src/services/types.ts | 2 +- 4 files changed, 54 insertions(+), 13 deletions(-) diff --git a/src/components/SideMenu/MenuItem.tsx b/src/components/SideMenu/MenuItem.tsx index 376e9afa..84bb1783 100644 --- a/src/components/SideMenu/MenuItem.tsx +++ b/src/components/SideMenu/MenuItem.tsx @@ -49,7 +49,8 @@ export class MenuItem extends React.Component { ) : ( - + {item.type === 'schema' && schema} + {item.sidebarLabel} {this.props.children} diff --git a/src/components/SideMenu/styled.elements.ts b/src/components/SideMenu/styled.elements.ts index 9ebeb212..9f137095 100644 --- a/src/components/SideMenu/styled.elements.ts +++ b/src/components/SideMenu/styled.elements.ts @@ -26,43 +26,47 @@ export const OperationBadge = styled.span.attrs((props: { type: string }) => ({ margin-top: 2px; &.get { - background-color: ${props => props.theme.colors.http.get}; + background-color: ${({ theme }) => theme.colors.http.get}; } &.post { - background-color: ${props => props.theme.colors.http.post}; + background-color: ${({ theme }) => theme.colors.http.post}; } &.put { - background-color: ${props => props.theme.colors.http.put}; + background-color: ${({ theme }) => theme.colors.http.put}; } &.options { - background-color: ${props => props.theme.colors.http.options}; + background-color: ${({ theme }) => theme.colors.http.options}; } &.patch { - background-color: ${props => props.theme.colors.http.patch}; + background-color: ${({ theme }) => theme.colors.http.patch}; } &.delete { - background-color: ${props => props.theme.colors.http.delete}; + background-color: ${({ theme }) => theme.colors.http.delete}; } &.basic { - background-color: ${props => props.theme.colors.http.basic}; + background-color: ${({ theme }) => theme.colors.http.basic}; } &.link { - background-color: ${props => props.theme.colors.http.link}; + background-color: ${({ theme }) => theme.colors.http.link}; } &.head { - background-color: ${props => props.theme.colors.http.head}; + background-color: ${({ theme }) => theme.colors.http.head}; } &.hook { - background-color: ${props => props.theme.colors.primary.main}; + background-color: ${({ theme }) => theme.colors.primary.main}; + } + + &.schema { + background-color: ${({ theme }) => theme.colors.http.basic}; } `; diff --git a/src/services/MenuBuilder.ts b/src/services/MenuBuilder.ts index 95b12ee3..4ad28f7b 100644 --- a/src/services/MenuBuilder.ts +++ b/src/services/MenuBuilder.ts @@ -1,4 +1,4 @@ -import type { OpenAPISpec, OpenAPIPaths } from '../types'; +import type { OpenAPISpec, OpenAPIPaths, OpenAPITag, OpenAPISchema } from '../types'; import { isOperationName, JsonPointer, alphabeticallyByProp } from '../utils'; import { MarkdownRenderer } from './MarkdownRenderer'; import { GroupModel, OperationModel } from './models'; @@ -137,7 +137,14 @@ export class MenuBuilder { continue; } + const relatedSchemas = this.getTagRelatedSchema({ + parser, + tag, + parent: item, + }); + item.items = [ + ...relatedSchemas, ...MenuBuilder.addMarkdownItems(tag.description || '', item, item.depth + 1, options), ...this.getOperationsItems(parser, item, tag, item.depth + 1, options), ]; @@ -248,4 +255,33 @@ export class MenuBuilder { } return tags; } + + static getTagRelatedSchema({ + parser, + tag, + parent, + }: { + parser: OpenAPIParser; + tag: TagInfo; + parent: GroupModel; + }): GroupModel[] { + return Object.entries(parser.spec.components?.schemas || {}) + .map(([schemaName, schema]) => { + const schemaTags = schema['x-tags']; + if (!schemaTags?.includes(tag.name)) return null; + + const item = new GroupModel( + 'schema', + { + name: schemaName, + 'x-displayName': `${(schema as OpenAPISchema).title || schemaName}`, + description: ``, + } as OpenAPITag, + parent, + ); + item.depth = parent.depth + 1; + return item; + }) + .filter(Boolean) as GroupModel[]; + } } diff --git a/src/services/types.ts b/src/services/types.ts index 5cba6cc9..3829024b 100644 --- a/src/services/types.ts +++ b/src/services/types.ts @@ -83,7 +83,7 @@ export interface TagGroup { tags: string[]; } -export type MenuItemGroupType = 'group' | 'tag' | 'section'; +export type MenuItemGroupType = 'group' | 'tag' | 'section' | 'schema'; export type MenuItemType = MenuItemGroupType | 'operation'; export interface IMenuItem {