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

View File

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

View File

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