redoc/src/services/models/Field.ts

56 lines
1.6 KiB
TypeScript
Raw Normal View History

2018-01-22 21:30:53 +03:00
import { action, observable } from 'mobx';
2017-10-12 00:01:37 +03:00
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 { extractExtensions } from '../../utils/openapi';
2017-10-12 00:01:37 +03:00
import { OpenAPIParser } from '../OpenAPIParser';
2018-01-22 21:30:53 +03:00
import { SchemaModel } from './Schema';
2017-10-12 00:01:37 +03:00
/**
* Field or Parameter model ready to be used by components
*/
export class FieldModel {
2018-08-18 16:23:33 +03:00
@observable
expanded: boolean = false;
2018-01-22 21:30:53 +03:00
schema: SchemaModel;
name: string;
required: boolean;
description: string;
example?: string;
deprecated: boolean;
in?: string;
kind: string;
extensions?: Dict<any>;
2017-10-12 00:01:37 +03:00
2017-11-21 14:24:41 +03:00
constructor(
parser: OpenAPIParser,
infoOrRef: Referenced<OpenAPIParameter> & { name?: string; kind?: string },
2017-11-21 14:24:41 +03:00
pointer: string,
options: RedocNormalizedOptions,
) {
2018-02-12 10:59:16 +03:00
const info = parser.deref<OpenAPIParameter>(infoOrRef);
this.kind = infoOrRef.kind || 'field';
2018-02-12 10:59:16 +03:00
this.name = infoOrRef.name || info.name;
2017-10-12 00:01:37 +03:00
this.in = info.in;
this.required = !!info.required;
2018-07-26 17:34:44 +03:00
this.schema = new SchemaModel(parser, info.schema || {}, pointer, 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);
if (options.showExtensions) {
this.extensions = extractExtensions(info, options.showExtensions);
}
2017-10-12 00:01:37 +03:00
}
@action
toggle() {
this.expanded = !this.expanded;
}
}