Skip to content

Commit 137373e

Browse files
authored
Use redoc options while downloading specs (#264)
1 parent bcbaa54 commit 137373e

File tree

3 files changed

+65
-26
lines changed

3 files changed

+65
-26
lines changed

.changeset/odd-pillows-switch.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'docusaurus-plugin-redoc': patch
3+
---
4+
5+
Use redoc config while loading spec

packages/docusaurus-plugin-redoc/src/index.ts

+32-26
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ import {
1515
bundle,
1616
loadConfig,
1717
stringifyYaml,
18+
Document,
19+
NormalizedProblem,
1820
} from '@redocly/openapi-core';
1921

2022
import {
@@ -24,6 +26,8 @@ import {
2426
DEFAULT_OPTIONS,
2527
} from './options';
2628
import { SpecProps, ApiDocProps } from './types/common';
29+
import { loadSpecWithConfig } from './loadSpec';
30+
2731
// eslint-disable-next-line @typescript-eslint/no-var-requires
2832
const version = require('../package.json').version;
2933

@@ -57,23 +61,6 @@ export default function redocPlugin(
5761
return {
5862
name: 'docusaurus-plugin-redoc',
5963
async loadContent() {
60-
if (!isSpecFile) {
61-
// If spec is a remote url then add it as download url also as a default
62-
url = url || spec;
63-
if (debug) {
64-
console.log('[REDOCUSAURUS_PLUGIN] bundling spec from url', spec);
65-
}
66-
const converted = await loadAndBundleSpec(spec!);
67-
return {
68-
converted,
69-
};
70-
}
71-
72-
// If local file
73-
if (debug) {
74-
console.log('[REDOCUSAURUS_PLUGIN] reading file: ', spec);
75-
}
76-
7764
let redoclyConfig: Config;
7865

7966
if (config) {
@@ -89,16 +76,34 @@ export default function redocPlugin(
8976
redoclyConfig = await loadConfig();
9077
}
9178

92-
const {
93-
bundle: bundledSpec,
94-
problems,
95-
fileDependencies,
96-
} = await bundle({
97-
ref: spec,
98-
config: redoclyConfig,
99-
});
79+
let bundledSpec: Document, problems: NormalizedProblem[];
10080

101-
filesToWatch = [path.resolve(spec), ...fileDependencies];
81+
if (!isSpecFile) {
82+
// If spec is a remote url then add it as download url also as a default
83+
url = url || spec;
84+
if (debug) {
85+
console.log('[REDOCUSAURUS_PLUGIN] bundling spec from url', spec);
86+
}
87+
({ bundle: bundledSpec, problems } = await loadSpecWithConfig(
88+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
89+
spec!,
90+
redoclyConfig,
91+
));
92+
} else {
93+
// If local file
94+
if (debug) {
95+
console.log('[REDOCUSAURUS_PLUGIN] reading file: ', spec);
96+
}
97+
98+
const fileBundle = await bundle({
99+
ref: spec,
100+
config: redoclyConfig,
101+
});
102+
103+
({ bundle: bundledSpec, problems } = fileBundle);
104+
105+
filesToWatch = [path.resolve(spec), ...fileBundle.fileDependencies];
106+
}
102107

103108
if (problems?.length) {
104109
console.error('[REDOCUSAURUS_PLUGIN] errors while bundling spec', spec);
@@ -112,6 +117,7 @@ export default function redocPlugin(
112117
if (debug) {
113118
console.log('[REDOCUSAURUS_PLUGIN] File Bundled');
114119
}
120+
// Pass again to loader to convert swagger to openapi
115121
const converted = await loadAndBundleSpec(bundledSpec.parsed);
116122

117123
// If download url is not provided then use bundled yaml as a static file (see `postBuild`)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import type { Config, Source, Document } from '@redocly/openapi-core';
2+
import { bundle } from '@redocly/openapi-core';
3+
4+
/**
5+
* Based on loadAndBundleSpec from redoc.
6+
* Customized to pass custom loaded config
7+
* @see https://github.com/Redocly/redoc/blob/33be51a7a4068f44fd914314002c058a204ba0c2/src/utils/loadAndBundleSpec.ts
8+
*/
9+
export async function loadSpecWithConfig(
10+
specUrlOrObject: object | string,
11+
config: Config,
12+
) {
13+
const bundleOpts: Parameters<typeof bundle>[0] = {
14+
config,
15+
base: process.cwd(),
16+
};
17+
18+
if (typeof specUrlOrObject === 'object' && specUrlOrObject !== null) {
19+
bundleOpts.doc = {
20+
source: { absoluteRef: '' } as Source,
21+
parsed: specUrlOrObject,
22+
} as Document;
23+
} else {
24+
bundleOpts.ref = specUrlOrObject;
25+
}
26+
27+
return bundle(bundleOpts);
28+
}

0 commit comments

Comments
 (0)