fix: add support downloadUrls

This commit is contained in:
Alex Varchuk 2025-01-15 17:01:49 +01:00
parent 30db4dfe76
commit c8eb5aa7ec
No known key found for this signature in database
GPG Key ID: 8A9260AE529FF454
3 changed files with 30 additions and 16 deletions

View File

@ -27,9 +27,8 @@ export class ApiInfo extends React.Component<ApiInfoProps> {
const { info, externalDocs } = store.spec;
const hideDownloadButtons = store.options.hideDownloadButtons;
// FIXME: use downloadUrls
const downloadUrls = info.downloadUrls;
console.log(downloadUrls);
const downloadFileName = info.downloadFileName;
const license =
(info.license && (
<InfoSpan>
@ -83,11 +82,11 @@ export class ApiInfo extends React.Component<ApiInfoProps> {
{downloadUrls?.map(({ title, url }) => {
return (
<DownloadButton
download={title}
download={downloadFileName || true}
target="_blank"
href={url}
rel="noreferrer"
key={title}
key={url}
>
{downloadUrls.length > 1 ? title : l('download')}
</DownloadButton>

View File

@ -236,6 +236,8 @@ export class RedocNormalizedOptions {
pathInMiddlePanel: boolean;
sanitize: boolean;
hideDownloadButtons: boolean;
downloadFileName?: string;
downloadDefinitionUrl?: string;
downloadUrls?: DownloadUrlsConfig;
disableSearch: boolean;
onlyRequiredInSamples: boolean;
@ -309,9 +311,9 @@ export class RedocNormalizedOptions {
this.pathInMiddlePanel = argValueToBoolean(raw.pathInMiddlePanel);
this.sanitize = argValueToBoolean(raw.sanitize || raw.untrustedSpec);
this.hideDownloadButtons = argValueToBoolean(raw.hideDownloadButtons || raw.hideDownloadButton);
this.downloadUrls =
raw.downloadUrls ||
([{ title: raw.downloadFileName, url: raw.downloadDefinitionUrl }] as DownloadUrlsConfig);
this.downloadFileName = raw.downloadFileName;
this.downloadDefinitionUrl = raw.downloadDefinitionUrl;
this.downloadUrls = raw.downloadUrls;
this.disableSearch = argValueToBoolean(raw.disableSearch);
this.onlyRequiredInSamples = argValueToBoolean(raw.onlyRequiredInSamples);
this.showExtensions = RedocNormalizedOptions.normalizeShowExtensions(raw.showExtensions);

View File

@ -1,7 +1,8 @@
import type { OpenAPIContact, OpenAPIInfo, OpenAPILicense } from '../../types';
import { IS_BROWSER } from '../../utils/';
import { l } from '../Labels';
import type { OpenAPIParser } from '../OpenAPIParser';
import { DownloadUrlsConfig, RedocNormalizedOptions } from '../RedocNormalizedOptions';
import { RedocNormalizedOptions } from '../RedocNormalizedOptions';
export class ApiInfoModel implements OpenAPIInfo {
title: string;
@ -13,7 +14,11 @@ export class ApiInfoModel implements OpenAPIInfo {
contact?: OpenAPIContact;
license?: OpenAPILicense;
downloadUrls?: DownloadUrlsConfig;
downloadUrls: {
title?: string;
url?: string;
}[];
downloadFileName?: string;
constructor(
private parser: OpenAPIParser,
@ -29,14 +34,22 @@ export class ApiInfoModel implements OpenAPIInfo {
}
this.downloadUrls = this.getDownloadUrls();
this.downloadFileName = this.options.downloadFileName || 'openapi.json';
}
private getDownloadUrls(): DownloadUrlsConfig | undefined {
return this.options.downloadUrls
?.map(({ title, url }) => ({
title: title || 'openapi.json',
url: this.getDownloadLink(url) || '',
}))
.filter(({ title, url }) => title && url);
private getDownloadUrls() {
return (
!this.options.downloadUrls
? [
{
title: l('download'),
url: this.getDownloadLink(this.options.downloadDefinitionUrl),
},
]
: this.options.downloadUrls.map(({ title, url }) => ({
title: title || 'Download OpenAPI description',
url: this.getDownloadLink(url),
}))
).filter(({ title, url }) => title && url);
}
private getDownloadLink(url?: string): string | undefined {