redoc/src/services/models/MediaContent.ts

57 lines
1.5 KiB
TypeScript
Raw Normal View History

2018-01-22 21:30:53 +03:00
import { action, computed, observable } from 'mobx';
2017-10-12 00:01:37 +03:00
import { OpenAPIMediaType } from '../../types';
import { MediaTypeModel } from './MediaType';
import { mergeSimilarMediaTypes } from '../../utils';
2017-10-12 00:01:37 +03:00
import { OpenAPIParser } from '../OpenAPIParser';
2018-01-22 21:30:53 +03:00
import { RedocNormalizedOptions } from '../RedocNormalizedOptions';
2017-10-12 00:01:37 +03:00
/**
* MediaContent model ready to be sued by React components
* Contains multiple MediaTypes and keeps track of the currently active one
2017-10-12 00:01:37 +03:00
*/
export class MediaContentModel {
mediaTypes: MediaTypeModel[];
@observable activeMimeIdx = 0;
/**
* @param isRequestType needed to know if skipe RO/RW fields in objects
*/
constructor(
public parser: OpenAPIParser,
info: Dict<OpenAPIMediaType>,
2017-11-21 14:24:41 +03:00
public isRequestType: boolean,
options: RedocNormalizedOptions,
2017-10-12 00:01:37 +03:00
) {
if (options.unstable_ignoreMimeParameters) {
info = mergeSimilarMediaTypes(info);
}
this.mediaTypes = Object.keys(info).map(name => {
const mime = info[name];
2017-10-12 00:01:37 +03:00
// reset deref cache just in case something is left there
parser.resetVisited();
2017-11-21 14:24:41 +03:00
return new MediaTypeModel(parser, name, isRequestType, mime, options);
2017-10-12 00:01:37 +03:00
});
}
/**
* Set active media type by index
* @param idx media type index
*/
@action
activate(idx: number) {
this.activeMimeIdx = idx;
}
@computed
get active() {
return this.mediaTypes[this.activeMimeIdx];
}
get hasSample(): boolean {
return this.mediaTypes.filter(mime => !!mime.examples).length > 0;
}
}