redoc/src/services/models/ApiInfo.ts

49 lines
1.4 KiB
TypeScript
Raw Normal View History

2017-10-12 00:01:37 +03:00
import { OpenAPIContact, OpenAPIInfo, OpenAPILicense } from '../../types';
import { IS_BROWSER } from '../../utils/';
2018-01-22 21:30:53 +03:00
import { OpenAPIParser } from '../OpenAPIParser';
2017-10-12 00:01:37 +03:00
export class ApiInfoModel implements OpenAPIInfo {
title: string;
version: string;
description: string;
2017-10-12 00:01:37 +03:00
termsOfService?: string;
contact?: OpenAPIContact;
license?: OpenAPILicense;
2018-10-05 16:43:59 +03:00
downloadLink?: string;
downloadFileName?: string;
constructor(private parser: OpenAPIParser) {
Object.assign(this, parser.spec.info);
this.description = parser.spec.info.description || '';
const firstHeadingLinePos = this.description.search(/^##?\s+/m);
if (firstHeadingLinePos > -1) {
this.description = this.description.substring(0, firstHeadingLinePos);
}
2018-10-05 16:43:59 +03:00
this.downloadLink = this.getDownloadLink();
this.downloadFileName = this.getDownloadFileName();
2017-10-12 00:01:37 +03:00
}
2018-10-05 16:43:59 +03:00
private getDownloadLink(): string | undefined {
2018-03-07 18:21:17 +03:00
if (this.parser.specUrl) {
return this.parser.specUrl;
}
if (IS_BROWSER && window.Blob && window.URL && window.URL.createObjectURL) {
2017-10-12 00:01:37 +03:00
const blob = new Blob([JSON.stringify(this.parser.spec, null, 2)], {
type: 'application/json',
});
return window.URL.createObjectURL(blob);
}
}
2018-10-05 16:43:59 +03:00
private getDownloadFileName(): string | undefined {
if (!this.parser.specUrl) {
2017-10-12 00:01:37 +03:00
return 'swagger.json';
}
return undefined;
}
}