chore: refactor + jsxify md tags

This commit is contained in:
Roman Hotsiy 2019-07-29 13:12:12 +03:00
parent 8c27d31396
commit 00a0abf4a4
No known key found for this signature in database
GPG Key ID: 5CB7B3ACABA57CB0
5 changed files with 54 additions and 36 deletions

View File

@ -38,7 +38,7 @@ info:
OAuth2 - an open protocol to allow secure authorization in a simple OAuth2 - an open protocol to allow secure authorization in a simple
and standard method from web, mobile and desktop applications. and standard method from web, mobile and desktop applications.
<security-definitions /> <SecurityDefinitions />
version: 1.0.0 version: 1.0.0
title: Swagger Petstore title: Swagger Petstore
@ -78,11 +78,11 @@ tags:
- name: pet_model - name: pet_model
x-displayName: The Pet Model x-displayName: The Pet Model
description: | description: |
<object-description schemaRef="#/components/schemas/Pet" /> <ObjectDescription schemaRef="#/components/schemas/Pet" />
- name: store_model - name: store_model
x-displayName: The Order Model x-displayName: The Order Model
description: | description: |
<object-description schemaRef="#/components/schemas/Order" examplesRef="#/components/examples/Order" showReadOnly={true} showWriteOnly={true} /> <ObjectDescription schemaRef="#/components/schemas/Order" exampleRef="#/components/examples/Order" showReadOnly={true} showWriteOnly={true} />
x-tagGroups: x-tagGroups:
- name: General - name: General
tags: tags:

View File

@ -1,14 +1,15 @@
import * as React from 'react'; import * as React from 'react';
import { Schema } from '../Schema';
import { DarkRightPanel, MiddlePanel, Row, Section } from '../../common-elements'; import { DarkRightPanel, MiddlePanel, Row, Section } from '../../common-elements';
import { MediaTypeModel, OpenAPIParser, RedocNormalizedOptions } from '../../services'; import { MediaTypeModel, OpenAPIParser, RedocNormalizedOptions } from '../../services';
import styled from '../../styled-components';
import { OpenAPIMediaType } from '../../types'; import { OpenAPIMediaType } from '../../types';
import { MediaTypeSamples } from '../PayloadSamples/MediaTypeSamples'; import { MediaTypeSamples } from '../PayloadSamples/MediaTypeSamples';
import { Schema } from '../Schema';
export interface ObjectDescriptionProps { export interface ObjectDescriptionProps {
schemaRef: string; schemaRef: string;
examplesRef?: string; exampleRef?: string;
showReadOnly?: boolean; showReadOnly?: boolean;
showWriteOnly?: boolean; showWriteOnly?: boolean;
parser: OpenAPIParser; parser: OpenAPIParser;
@ -16,7 +17,7 @@ export interface ObjectDescriptionProps {
} }
export class ObjectDescription extends React.PureComponent<ObjectDescriptionProps> { export class ObjectDescription extends React.PureComponent<ObjectDescriptionProps> {
private static getMediaType(schemaRef, examplesRef): OpenAPIMediaType { private static getMediaType(schemaRef: string, exampleRef?: string): OpenAPIMediaType {
if (!schemaRef) { if (!schemaRef) {
return {}; return {};
} }
@ -25,33 +26,28 @@ export class ObjectDescription extends React.PureComponent<ObjectDescriptionProp
schema: { $ref: schemaRef }, schema: { $ref: schemaRef },
}; };
if (examplesRef) { if (exampleRef) {
info.examples = { object: { $ref: examplesRef } }; info.examples = { example: { $ref: exampleRef } };
} }
return info; return info;
} }
private static getMediaModel({ private _mediaModel: MediaTypeModel;
schemaRef,
examplesRef, private get mediaModel() {
parser, const { parser, schemaRef, exampleRef, options } = this.props;
options, if (!this._mediaModel) {
}: ObjectDescriptionProps) { this._mediaModel = new MediaTypeModel(
return new MediaTypeModel(
parser, parser,
'json', 'json',
false, false,
ObjectDescription.getMediaType(schemaRef, examplesRef), ObjectDescription.getMediaType(schemaRef, exampleRef),
options, options,
); );
} }
private mediaModel: MediaTypeModel; return this._mediaModel;
constructor(props: ObjectDescriptionProps) {
super(props);
this.mediaModel = ObjectDescription.getMediaModel(this.props);
} }
render() { render() {
@ -63,15 +59,29 @@ export class ObjectDescription extends React.PureComponent<ObjectDescriptionProp
<Schema <Schema
skipWriteOnly={!showWriteOnly} skipWriteOnly={!showWriteOnly}
skipReadOnly={!showReadOnly} skipReadOnly={!showReadOnly}
key="schema"
schema={this.mediaModel.schema} schema={this.mediaModel.schema}
/> />
</MiddlePanel> </MiddlePanel>
<DarkRightPanel> <DarkRightPanel>
<MediaSamplesWrap>
<MediaTypeSamples mediaType={this.mediaModel} /> <MediaTypeSamples mediaType={this.mediaModel} />
</MediaSamplesWrap>
</DarkRightPanel> </DarkRightPanel>
</Row> </Row>
</Section> </Section>
); );
} }
} }
const MediaSamplesWrap = styled.div`
background: ${({ theme }) => theme.codeSample.backgroundColor};
& > div,
& > pre {
padding: ${props => props.theme.spacing.unit * 4}px;
margin: 0;
}
& > div > pre {
padding: 0;
}
`;

View File

@ -13,8 +13,9 @@ import { SearchStore } from './SearchStore';
import { ObjectDescription } from '../components/ObjectDescription/ObjectDescription'; import { ObjectDescription } from '../components/ObjectDescription/ObjectDescription';
import { SecurityDefs } from '../components/SecuritySchemes/SecuritySchemes'; import { SecurityDefs } from '../components/SecuritySchemes/SecuritySchemes';
import { import {
OBJECT_DEFINTION_COMPONENT_NAME, OBJECT_DEFINTION_JSX_NAME,
SECURITY_DEFINITIONS_COMPONENT_NAME, SECURITY_DEFINITIONS_COMPONENT_NAME,
SECURITY_DEFINITIONS_JSX_NAME,
} from '../utils/openapi'; } from '../utils/openapi';
export interface StoreState { export interface StoreState {
@ -155,10 +156,15 @@ const DEFAULT_OPTIONS: RedocRawOptions = {
securitySchemes: store.spec.securitySchemes, securitySchemes: store.spec.securitySchemes,
}), }),
}, },
[OBJECT_DEFINTION_COMPONENT_NAME]: { [SECURITY_DEFINITIONS_JSX_NAME]: {
component: ObjectDescription, component: SecurityDefs,
propsSelector: (store: AppStore) => ({ propsSelector: (store: AppStore) => ({
securitySchemes: store.spec.securitySchemes, securitySchemes: store.spec.securitySchemes,
}),
},
[OBJECT_DEFINTION_JSX_NAME]: {
component: ObjectDescription,
propsSelector: (store: AppStore) => ({
parser: store.spec.parser, parser: store.spec.parser,
options: store.options, options: store.options,
}), }),

View File

@ -59,16 +59,16 @@ export class MenuBuilder {
*/ */
static addMarkdownItems( static addMarkdownItems(
description: string, description: string,
grandparent: GroupModel | undefined, parent: GroupModel | undefined,
initialDepth: number, initialDepth: number,
options: RedocNormalizedOptions, options: RedocNormalizedOptions,
): ContentItemModel[] { ): ContentItemModel[] {
const renderer = new MarkdownRenderer(options); const renderer = new MarkdownRenderer(options);
const headings = renderer.extractHeadings(description || ''); const headings = renderer.extractHeadings(description || '');
const mapHeadingsDeep = (parent, items, depth = 1) => const mapHeadingsDeep = (_parent, items, depth = 1) =>
items.map(heading => { items.map(heading => {
const group = new GroupModel('section', heading, parent); const group = new GroupModel('section', heading, _parent);
group.depth = depth; group.depth = depth;
if (heading.items) { if (heading.items) {
group.items = mapHeadingsDeep(group, heading.items, depth + 1); group.items = mapHeadingsDeep(group, heading.items, depth + 1);
@ -84,7 +84,7 @@ export class MenuBuilder {
return group; return group;
}); });
return mapHeadingsDeep(grandparent, headings, initialDepth); return mapHeadingsDeep(parent, headings, initialDepth);
} }
/** /**

View File

@ -495,8 +495,10 @@ export function normalizeServers(
}); });
} }
export const OBJECT_DEFINTION_COMPONENT_NAME = 'object-description';
export const SECURITY_DEFINITIONS_COMPONENT_NAME = 'security-definitions'; export const SECURITY_DEFINITIONS_COMPONENT_NAME = 'security-definitions';
export const SECURITY_DEFINITIONS_JSX_NAME = 'SecurityDefinitions';
export const OBJECT_DEFINTION_JSX_NAME = 'ObjectDescription';
export let SECURITY_SCHEMES_SECTION_PREFIX = 'section/Authentication/'; export let SECURITY_SCHEMES_SECTION_PREFIX = 'section/Authentication/';
export function setSecuritySchemePrefix(prefix: string) { export function setSecuritySchemePrefix(prefix: string) {
SECURITY_SCHEMES_SECTION_PREFIX = prefix; SECURITY_SCHEMES_SECTION_PREFIX = prefix;