redoc/src/services/models/Field.ts

49 lines
1.4 KiB
TypeScript
Raw Normal View History

2017-10-12 00:01:37 +03:00
import { observable, action } from 'mobx';
import { OpenAPIParameter, Referenced } from '../../types';
2017-11-21 14:24:41 +03:00
import { RedocNormalizedOptions } from '../RedocNormalizedOptions';
2017-10-12 00:01:37 +03:00
import { SchemaModel } from './Schema';
import { OpenAPIParser } from '../OpenAPIParser';
/**
* Field or Parameter model ready to be used by components
*/
export class FieldModel {
@observable public expanded: boolean = false;
public schema: SchemaModel;
public name: string;
public required: boolean;
public description: string;
public example?: string;
public deprecated: boolean;
public in?: string;
2017-11-21 14:24:41 +03:00
constructor(
parser: OpenAPIParser,
infoOrRef: Referenced<OpenAPIParameter>,
pointer: string,
options: RedocNormalizedOptions,
) {
2017-10-12 00:01:37 +03:00
const info = parser.deref(infoOrRef);
this.name = info.name;
this.in = info.in;
this.required = !!info.required;
2017-12-07 14:54:34 +03:00
const schemaPointer = (parser.isRef(infoOrRef) ? infoOrRef.$ref : pointer) + '/schema';
this.schema = new SchemaModel(parser, info.schema || {}, schemaPointer, options);
2017-10-12 00:01:37 +03:00
this.description =
info.description === undefined ? this.schema.description || '' : info.description;
this.example = info.example || this.schema.example;
2017-10-12 00:01:37 +03:00
this.deprecated = info.deprecated === undefined ? !!this.schema.deprecated : info.deprecated;
parser.exitRef(infoOrRef);
}
@action
toggle() {
this.expanded = !this.expanded;
}
}