feat: reqired-first sort order for params

This commit is contained in:
Roman Hotsiy 2018-02-19 13:31:18 +02:00
parent 8bd71deebe
commit ecf33d2dca
No known key found for this signature in database
GPG Key ID: 5CB7B3ACABA57CB0
4 changed files with 38 additions and 18 deletions

View File

@ -47,14 +47,20 @@ export class MenuItem extends React.Component<MenuItemProps> {
export interface OperationMenuItemContentProps { export interface OperationMenuItemContentProps {
item: OperationModel; item: OperationModel;
className?: string;
} }
@observer @observer
class OperationMenuItemContent extends React.Component<OperationMenuItemContentProps> { class OperationMenuItemContent extends React.Component<OperationMenuItemContentProps> {
render() { render() {
const { item } = this.props; const { item, className } = this.props;
return ( return (
<MenuItemLabel depth={item.depth} active={item.active} deprecated={item.deprecated}> <MenuItemLabel
className={className}
depth={item.depth}
active={item.active}
deprecated={item.deprecated}
>
<OperationBadge type={item.httpVerb} /> <OperationBadge type={item.httpVerb} />
<MenuItemTitle width="calc(100% - 32px)">{item.name}</MenuItemTitle> <MenuItemTitle width="calc(100% - 32px)">{item.name}</MenuItemTitle>
</MenuItemLabel> </MenuItemLabel>

View File

@ -8,7 +8,13 @@ import { SecurityRequirementModel } from './SecurityRequirement';
import { OpenAPIExternalDocumentation, OpenAPIServer } from '../../types'; import { OpenAPIExternalDocumentation, OpenAPIServer } from '../../types';
import { getOperationSummary, isAbsolutePath, JsonPointer, stripTrailingSlash } from '../../utils'; import {
getOperationSummary,
isAbsolutePath,
JsonPointer,
stripTrailingSlash,
sortByRequired,
} from '../../utils';
import { ContentItemModel, ExtendedOpenAPIOperation } from '../MenuBuilder'; import { ContentItemModel, ExtendedOpenAPIOperation } from '../MenuBuilder';
import { OpenAPIParser } from '../OpenAPIParser'; import { OpenAPIParser } from '../OpenAPIParser';
import { RedocNormalizedOptions } from '../RedocNormalizedOptions'; import { RedocNormalizedOptions } from '../RedocNormalizedOptions';
@ -77,6 +83,10 @@ export class OperationModel implements IMenuItem {
.concat(operationSpec.parameters || []) .concat(operationSpec.parameters || [])
.map(paramOrRef => new FieldModel(parser, paramOrRef, this._$ref, options)); .map(paramOrRef => new FieldModel(parser, paramOrRef, this._$ref, options));
if (options.requiredPropsFirst) {
sortByRequired(this.parameters);
}
let hasSuccessResponses = false; let hasSuccessResponses = false;
this.responses = Object.keys(operationSpec.responses || []) this.responses = Object.keys(operationSpec.responses || [])
.filter(code => { .filter(code => {

View File

@ -13,6 +13,7 @@ import {
isNamedDefinition, isNamedDefinition,
isPrimitiveType, isPrimitiveType,
JsonPointer, JsonPointer,
sortByRequired,
} from '../../utils/'; } from '../../utils/';
// TODO: refactor this model, maybe use getters instead of copying all the values // TODO: refactor this model, maybe use getters instead of copying all the values
@ -226,7 +227,7 @@ function buildFields(
}); });
if (options.requiredPropsFirst) { if (options.requiredPropsFirst) {
sortFields(fields, schema.required); sortByRequired(fields, schema.required);
} }
if (typeof additionalProps === 'object') { if (typeof additionalProps === 'object') {
@ -246,17 +247,3 @@ function buildFields(
return fields; return fields;
} }
function sortFields(fields: FieldModel[], order: string[] = []) {
fields.sort((a, b) => {
if (!a.required && b.required) {
return 1;
} else if (a.required && !b.required) {
return -1;
} else if (a.required && b.required) {
return order.indexOf(a.name) > order.indexOf(b.name) ? 1 : -1;
} else {
return 0;
}
});
}

View File

@ -161,4 +161,21 @@ export function humanizeConstraints(schema: OpenAPISchema): string[] {
return res; return res;
} }
export function sortByRequired(
fields: { required: boolean; name: string }[],
order: string[] = [],
) {
fields.sort((a, b) => {
if (!a.required && b.required) {
return 1;
} else if (a.required && !b.required) {
return -1;
} else if (a.required && b.required) {
return order.indexOf(a.name) > order.indexOf(b.name) ? 1 : -1;
} else {
return 0;
}
});
}
export const SECURITY_SCHEMES_SECTION = 'section/Authentication/'; export const SECURITY_SCHEMES_SECTION = 'section/Authentication/';