Skip to content

Commit

Permalink
fix: add support downloadUrls
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexVarchuk committed Jan 15, 2025
1 parent 30db4df commit c8eb5aa
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 16 deletions.
7 changes: 3 additions & 4 deletions src/components/ApiInfo/ApiInfo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down Expand Up @@ -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>
Expand Down
8 changes: 5 additions & 3 deletions src/services/RedocNormalizedOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,8 @@ export class RedocNormalizedOptions {
pathInMiddlePanel: boolean;
sanitize: boolean;
hideDownloadButtons: boolean;
downloadFileName?: string;
downloadDefinitionUrl?: string;
downloadUrls?: DownloadUrlsConfig;
disableSearch: boolean;
onlyRequiredInSamples: boolean;
Expand Down Expand Up @@ -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);
Expand Down
31 changes: 22 additions & 9 deletions src/services/models/ApiInfo.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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,
Expand All @@ -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 {
Expand Down

0 comments on commit c8eb5aa

Please sign in to comment.