From c8eb5aa7ec85291ede6f4e2c2b460d2a5c168035 Mon Sep 17 00:00:00 2001 From: Alex Varchuk Date: Wed, 15 Jan 2025 17:01:49 +0100 Subject: [PATCH] fix: add support downloadUrls --- src/components/ApiInfo/ApiInfo.tsx | 7 +++--- src/services/RedocNormalizedOptions.ts | 8 ++++--- src/services/models/ApiInfo.ts | 31 ++++++++++++++++++-------- 3 files changed, 30 insertions(+), 16 deletions(-) diff --git a/src/components/ApiInfo/ApiInfo.tsx b/src/components/ApiInfo/ApiInfo.tsx index 7d1f7586..6c25c695 100644 --- a/src/components/ApiInfo/ApiInfo.tsx +++ b/src/components/ApiInfo/ApiInfo.tsx @@ -27,9 +27,8 @@ export class ApiInfo extends React.Component { 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 && ( @@ -83,11 +82,11 @@ export class ApiInfo extends React.Component { {downloadUrls?.map(({ title, url }) => { return ( {downloadUrls.length > 1 ? title : l('download')} diff --git a/src/services/RedocNormalizedOptions.ts b/src/services/RedocNormalizedOptions.ts index 1a62934e..7bfbc963 100644 --- a/src/services/RedocNormalizedOptions.ts +++ b/src/services/RedocNormalizedOptions.ts @@ -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); diff --git a/src/services/models/ApiInfo.ts b/src/services/models/ApiInfo.ts index 738b98e6..7a7d6db9 100644 --- a/src/services/models/ApiInfo.ts +++ b/src/services/models/ApiInfo.ts @@ -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 {