mirror of
https://github.com/Redocly/redoc.git
synced 2024-11-22 08:36:33 +03:00
feat: reqired-first sort order for params
This commit is contained in:
parent
8bd71deebe
commit
ecf33d2dca
|
@ -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>
|
||||
|
|
|
@ -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 => {
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
|
|
@ -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/';
|
||||
|
|
Loading…
Reference in New Issue
Block a user