fix: remove the ability to change the file format

This commit is contained in:
anastasiia-developer 2022-04-22 11:38:15 +03:00
parent 4923037252
commit 16c80fd74b
4 changed files with 6 additions and 42 deletions

View File

@ -232,7 +232,7 @@ You can use all of the following options with standalone version on <redoc> tag
* `expandResponses` - specify which responses to expand by default by response codes. Values should be passed as comma-separated list without spaces e.g. `expandResponses="200,201"`. Special value `"all"` expands all responses by default. Be careful: this option can slow-down documentation rendering time. * `expandResponses` - specify which responses to expand by default by response codes. Values should be passed as comma-separated list without spaces e.g. `expandResponses="200,201"`. Special value `"all"` expands all responses by default. Be careful: this option can slow-down documentation rendering time.
* `maxDisplayedEnumValues` - display only specified number of enum values. hide rest values under spoiler. * `maxDisplayedEnumValues` - display only specified number of enum values. hide rest values under spoiler.
* `hideDownloadButton` - do not show "Download" spec button. **THIS DOESN'T MAKE YOUR SPEC PRIVATE**, it just hides the button. * `hideDownloadButton` - do not show "Download" spec button. **THIS DOESN'T MAKE YOUR SPEC PRIVATE**, it just hides the button.
* `downloadFileName` - set file name and format of downloaded spec file, e.g. `openapi.yaml`, default `swagger.json`. * `downloadFileName` - set file name of downloaded spec file, e.g. `swagger.json`, default `openapi.json`. Spec file in `JSON` format. (only `redoc-cli`)
* `hideHostname` - if set, the protocol and hostname is not shown in the operation definition. * `hideHostname` - if set, the protocol and hostname is not shown in the operation definition.
* `hideLoading` - do not show loading animation. Useful for small docs. * `hideLoading` - do not show loading animation. Useful for small docs.
* `hideSchemaPattern` - if set, the pattern is not shown in the schema. * `hideSchemaPattern` - if set, the pattern is not shown in the schema.

View File

@ -1,4 +1,3 @@
import * as path from 'path';
import defaultTheme, { ResolvedThemeInterface, resolveTheme, ThemeInterface } from '../theme'; import defaultTheme, { ResolvedThemeInterface, resolveTheme, ThemeInterface } from '../theme';
import { querySelector } from '../utils/dom'; import { querySelector } from '../utils/dom';
import { isNumeric, mergeObjects } from '../utils/helpers'; import { isNumeric, mergeObjects } from '../utils/helpers';
@ -155,20 +154,6 @@ export class RedocNormalizedOptions {
return 0; return 0;
} }
static normalizeDownloadFileName(value: RedocRawOptions['downloadFileName']): string {
if (value) {
const extname = path.extname(value);
if (extname === '.json' || extname === '.yaml') {
return value;
} else {
console.warn(`downloadFileName must be a JSON or YAML file.`);
}
}
// Default value
return 'openapi.json';
}
private static normalizeJsonSampleExpandLevel(level?: number | string | 'all'): number { private static normalizeJsonSampleExpandLevel(level?: number | string | 'all'): number {
if (level === 'all') { if (level === 'all') {
return +Infinity; return +Infinity;
@ -249,7 +234,7 @@ export class RedocNormalizedOptions {
this.pathInMiddlePanel = argValueToBoolean(raw.pathInMiddlePanel); this.pathInMiddlePanel = argValueToBoolean(raw.pathInMiddlePanel);
this.untrustedSpec = argValueToBoolean(raw.untrustedSpec); this.untrustedSpec = argValueToBoolean(raw.untrustedSpec);
this.hideDownloadButton = argValueToBoolean(raw.hideDownloadButton); this.hideDownloadButton = argValueToBoolean(raw.hideDownloadButton);
this.downloadFileName = RedocNormalizedOptions.normalizeDownloadFileName(raw.downloadFileName); this.downloadFileName = raw.downloadFileName || 'openapi.json';
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

@ -72,7 +72,7 @@ describe('Models', () => {
} as any; } as any;
const info = new ApiInfoModel(parser); const info = new ApiInfoModel(parser);
expect(info.downloadFileName).toEqual('swagger.json'); expect(info.downloadFileName).toEqual('openapi.json');
}); });
test('should correctly populate download file name', () => { test('should correctly populate download file name', () => {
@ -83,22 +83,9 @@ describe('Models', () => {
}, },
} as any; } as any;
const opts = new RedocNormalizedOptions({ downloadFileName: 'openapi.yaml' }); const opts = new RedocNormalizedOptions({ downloadFileName: 'filename.json' });
const info = new ApiInfoModel(parser, opts); const info = new ApiInfoModel(parser, opts);
expect(info.downloadFileName).toEqual('openapi.yaml'); expect(info.downloadFileName).toEqual('filename.json');
});
test('should correctly populate default download file name if invalid extension is used', () => {
parser.spec = {
openapi: '3.0.0',
info: {
description: 'Test description',
},
} as any;
const opts = new RedocNormalizedOptions({ downloadFileName: 'nope.txt' });
const info = new ApiInfoModel(parser, opts);
expect(info.downloadFileName).toEqual('swagger.json');
}); });
}); });
}); });

View File

@ -1,5 +1,3 @@
import * as path from 'path';
import * as yaml from 'js-yaml';
import { OpenAPIContact, OpenAPIInfo, OpenAPILicense } from '../../types'; import { OpenAPIContact, OpenAPIInfo, OpenAPILicense } from '../../types';
import { IS_BROWSER } from '../../utils/'; import { IS_BROWSER } from '../../utils/';
import { OpenAPIParser } from '../OpenAPIParser'; import { OpenAPIParser } from '../OpenAPIParser';
@ -41,13 +39,7 @@ export class ApiInfoModel implements OpenAPIInfo {
} }
if (IS_BROWSER && window.Blob && window.URL && window.URL.createObjectURL) { if (IS_BROWSER && window.Blob && window.URL && window.URL.createObjectURL) {
let specString: string; const blob = new Blob([JSON.stringify(this.parser.spec, null, 2)], {
if (path.extname(this.options.downloadFileName) === '.yaml') {
specString = yaml.dump(this.parser.spec);
} else {
specString = JSON.stringify(this.parser.spec, null, 2);
}
const blob = new Blob([specString], {
type: 'application/json', type: 'application/json',
}); });
return window.URL.createObjectURL(blob); return window.URL.createObjectURL(blob);