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 {
item: OperationModel;
className?: string;
}
@observer
class OperationMenuItemContent extends React.Component<OperationMenuItemContentProps> {
render() {
const { item } = this.props;
const { item, className } = this.props;
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} />
<MenuItemTitle width="calc(100% - 32px)">{item.name}</MenuItemTitle>
</MenuItemLabel>

View File

@ -8,7 +8,13 @@ import { SecurityRequirementModel } from './SecurityRequirement';
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 { OpenAPIParser } from '../OpenAPIParser';
import { RedocNormalizedOptions } from '../RedocNormalizedOptions';
@ -77,6 +83,10 @@ export class OperationModel implements IMenuItem {
.concat(operationSpec.parameters || [])
.map(paramOrRef => new FieldModel(parser, paramOrRef, this._$ref, options));
if (options.requiredPropsFirst) {
sortByRequired(this.parameters);
}
let hasSuccessResponses = false;
this.responses = Object.keys(operationSpec.responses || [])
.filter(code => {

View File

@ -13,6 +13,7 @@ import {
isNamedDefinition,
isPrimitiveType,
JsonPointer,
sortByRequired,
} from '../../utils/';
// TODO: refactor this model, maybe use getters instead of copying all the values
@ -226,7 +227,7 @@ function buildFields(
});
if (options.requiredPropsFirst) {
sortFields(fields, schema.required);
sortByRequired(fields, schema.required);
}
if (typeof additionalProps === 'object') {
@ -246,17 +247,3 @@ function buildFields(
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;
}
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/';