Added callbacks support

This commit is contained in:
Makashov Nurbol 2018-12-26 16:56:22 +06:00
parent cfb6f0fde0
commit f1eb03fd66
14 changed files with 244 additions and 69 deletions

View File

@ -44,8 +44,7 @@ export class ApiInfo extends React.Component<ApiInfoProps> {
null;
const website =
(info.contact &&
info.contact.url && (
(info.contact && info.contact.url && (
<InfoSpan>
URL: <a href={info.contact.url}>{info.contact.url}</a>
</InfoSpan>
@ -53,8 +52,7 @@ export class ApiInfo extends React.Component<ApiInfoProps> {
null;
const email =
(info.contact &&
info.contact.email && (
(info.contact && info.contact.email && (
<InfoSpan>
{info.contact.name || 'E-mail'}:{' '}
<a href={'mailto:' + info.contact.email}>{info.contact.email}</a>

View File

@ -0,0 +1,26 @@
import { observer } from 'mobx-react';
import * as React from 'react';
import { CallbackModel } from '../../services/models';
import { CallbackDetailsWrap, StyledCallbackTitle } from '../Callbacks/styled.elements';
@observer
export class CallbackView extends React.Component<{ callback: CallbackModel }> {
toggle = () => {
this.props.callback.toggle();
};
render() {
const { name, expanded } = this.props.callback;
return (
<div>
<StyledCallbackTitle onClick={this.toggle} name={name} opened={expanded} />
{expanded && (
<CallbackDetailsWrap>
<span>{name}</span>
</CallbackDetailsWrap>
)}
</div>
);
}
}

View File

@ -0,0 +1,22 @@
import * as React from 'react';
import { ShelfIcon } from '../../common-elements';
export interface CallbackTitleProps {
name: string;
opened?: boolean;
className?: string;
onClick?: () => void;
}
export class CallbackTitle extends React.PureComponent<CallbackTitleProps> {
render() {
const { name, opened, className, onClick } = this.props;
return (
<div className={className} onClick={onClick || undefined}>
<ShelfIcon size={'1.5em'} direction={opened ? 'up' : 'down'} float={'left'} />
<strong>{name} </strong>
</div>
);
}
}

View File

@ -0,0 +1,35 @@
import * as React from 'react';
import { CallbackModel } from '../../services/models';
import styled from '../../styled-components';
import { CallbackView } from './Callback';
const CallbacksHeader = styled.h3`
font-size: 18px;
padding: 0.2em 0;
margin: 3em 0 1.1em;
color: #253137;
font-weight: normal;
`;
export interface CallbacksListProps {
callbacks: CallbackModel[];
}
export class CallbacksList extends React.PureComponent<CallbacksListProps> {
render() {
const { callbacks } = this.props;
if (!callbacks || callbacks.length === 0) {
return null;
}
return (
<div>
<CallbacksHeader> Callbacks </CallbacksHeader>
{callbacks.map(callback => {
return <CallbackView key={callback.name} callback={callback} />;
})}
</div>
);
}
}

View File

@ -0,0 +1,17 @@
// import { transparentize } from 'polished';
import styled from '../../styled-components';
import { CallbackTitle } from './CallbackTitle';
export const StyledCallbackTitle = styled(CallbackTitle)`
padding: 10px;
border-radius: 2px;
margin-bottom: 4px;
line-height: 1.5em;
background-color: #f2f2f2;
cursor: pointer;
`;
export const CallbackDetailsWrap = styled.div`
padding: 10px;
`;

View File

@ -4,10 +4,19 @@ import * as React from 'react';
import { ExternalDocumentation } from '../ExternalDocumentation/ExternalDocumentation';
import { AdvancedMarkdown } from '../Markdown/AdvancedMarkdown';
import { Operation } from '..';
import { H1, H2, MiddlePanel, Row, Section, ShareLink } from '../../common-elements';
import { ContentItemModel } from '../../services/MenuBuilder';
import { ContentItemModel } from '../../services';
import { GroupModel, OperationModel } from '../../services/models';
import { Operation } from '../Operation/Operation';
import styled from '../../styled-components';
const CallbacksHeader = styled.h3`
font-size: 18px;
padding: 0 40px;
margin: 3em 0 1.1em;
color: #253137;
font-weight: normal;
`;
@observer
export class ContentItems extends React.Component<{
@ -18,7 +27,22 @@ export class ContentItems extends React.Component<{
if (items.length === 0) {
return null;
}
return items.map(item => <ContentItem item={item} key={item.id} />);
return items.map(item => {
if (item.type === 'operation' && item.callbacks.length > 0) {
return (
<React.Fragment key={item.id}>
<ContentItem item={item} />
<CallbacksHeader>Callbacks:</CallbacksHeader>
{item.callbacks.map((callbackIndex, idx) => {
return <ContentItems key={idx} items={callbackIndex.operations} />;
})}
</React.Fragment>
);
}
return <ContentItem key={item.id} item={item} />;
});
}
}

View File

@ -65,8 +65,7 @@ export class Field extends React.Component<FieldProps> {
<FieldDetails {...this.props} />
</PropertyDetailsCell>
</tr>
{field.expanded &&
withSubSchema && (
{field.expanded && withSubSchema && (
<tr key={field.name + 'inner'}>
<PropertyCellWithInner colSpan={2}>
<InnerPropertiesWrap>

View File

@ -28,8 +28,7 @@ export class ResponseView extends React.Component<{ response: ResponseModel }> {
code={code}
opened={expanded}
/>
{expanded &&
!empty && (
{expanded && !empty && (
<ResponseDetailsWrap>
<ResponseDetails response={this.props.response} />
</ResponseDetailsWrap>

View File

@ -93,8 +93,7 @@ export class SecurityDefs extends React.PureComponent<SecurityDefsProps> {
<th> HTTP Authorization Scheme </th>
<td> {scheme.http.scheme} </td>
</tr>,
scheme.http.scheme === 'bearer' &&
scheme.http.bearerFormat && (
scheme.http.scheme === 'bearer' && scheme.http.bearerFormat && (
<tr key="bearer">
<th> Bearer format </th>
<td> "{scheme.http.bearerFormat}" </td>

View File

@ -57,16 +57,13 @@ export class MenuItem extends React.Component<MenuItemProps> {
{item.name}
{this.props.children}
</MenuItemTitle>
{(item.depth > 0 &&
item.items.length > 0 && (
{(item.depth > 0 && item.items.length > 0 && (
<ShelfIcon float={'right'} direction={item.expanded ? 'down' : 'right'} />
)) ||
null}
</MenuItemLabel>
)}
{!withoutChildren &&
item.items &&
item.items.length > 0 && (
{!withoutChildren && item.items && item.items.length > 0 && (
<MenuItems
expanded={item.expanded}
items={item.items}

View File

@ -0,0 +1,53 @@
import { action, observable } from 'mobx';
import { OpenAPICallback, Referenced } from '../../types';
import { isOperationName } from '../../utils';
import { OpenAPIParser } from '../OpenAPIParser';
import { OperationModel } from './Operation';
export class CallbackModel {
@observable
expanded: boolean;
name: string;
paths: Referenced<OpenAPICallback>;
operations: OperationModel[] = [];
constructor(
parser: OpenAPIParser,
name: string,
infoOrRef: Referenced<OpenAPICallback>,
options,
) {
this.name = name;
this.paths = parser.deref(infoOrRef);
parser.exitRef(infoOrRef);
for (const pathName of Object.keys(this.paths)) {
const path = this.paths[pathName];
const operations = Object.keys(path).filter(isOperationName);
for (const operationName of operations) {
const operationInfo = path[operationName];
const operation = new OperationModel(
parser,
{
...operationInfo,
pathName,
httpVerb: operationName,
pathParameters: path.parameters || [],
},
undefined,
options,
);
this.operations.push(operation);
}
}
console.log(this.operations);
}
@action
toggle() {
this.expanded = !this.expanded;
}
}

View File

@ -49,11 +49,7 @@ export class MediaTypeModel {
if (this.schema && this.schema.oneOf) {
this.examples = {};
for (const subSchema of this.schema.oneOf) {
const sample = Sampler.sample(
subSchema.rawSchema,
samplerOptions,
parser.spec,
);
const sample = Sampler.sample(subSchema.rawSchema, samplerOptions, parser.spec);
if (this.schema.discriminatorProp && typeof sample === 'object' && sample) {
sample[this.schema.discriminatorProp] = subSchema.title;
@ -66,11 +62,7 @@ export class MediaTypeModel {
} else if (this.schema) {
this.examples = {
default: new ExampleModel(parser, {
value: Sampler.sample(
info.schema,
samplerOptions,
parser.spec,
),
value: Sampler.sample(info.schema, samplerOptions, parser.spec),
}),
};
}

View File

@ -26,6 +26,7 @@ import {
import { ContentItemModel, ExtendedOpenAPIOperation } from '../MenuBuilder';
import { OpenAPIParser } from '../OpenAPIParser';
import { RedocNormalizedOptions } from '../RedocNormalizedOptions';
import { CallbackModel } from './Callback';
import { FieldModel } from './Field';
import { RequestBodyModel } from './RequestBody';
import { ResponseModel } from './Response';
@ -187,4 +188,16 @@ export class OperationModel implements IMenuItem {
);
});
}
@memoize
get callbacks() {
return Object.keys(this.operationSpec.callbacks || []).map(callbackName => {
return new CallbackModel(
this.parser,
callbackName,
this.operationSpec.callbacks![callbackName],
this.options,
);
});
}
}

View File

@ -1,3 +1,4 @@
export * from './Callback';
export * from '../SpecStore';
export * from './Group.model';
export * from './Operation';