Skip to content

Commit 0eff93b

Browse files
authored
🐛 Fix swagger support (#151)
1 parent f2c522f commit 0eff93b

File tree

5 files changed

+45
-15
lines changed

5 files changed

+45
-15
lines changed

.changeset/lazy-grapes-rest.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'docusaurus-plugin-redoc': minor
3+
---
4+
5+
Add swagger support, create 2 bundles - original and converted

.changeset/orange-donkeys-marry.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'docusaurus-theme-redoc': patch
3+
---
4+
5+
Fix scrollYOffset warning in server build

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

+29-12
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import type {
77
} from '@docusaurus/types';
88
import { normalizeUrl } from '@docusaurus/utils';
99
import { loadAndBundleSpec } from 'redoc';
10+
import type { OpenAPISpec } from 'redoc/typings/types';
1011
import {
1112
formatProblems,
1213
getTotals,
@@ -31,7 +32,10 @@ export { PluginOptions };
3132
export default function redocPlugin(
3233
context: LoadContext,
3334
opts: PluginOptions,
34-
): Plugin<Record<string, unknown>> {
35+
): Plugin<{
36+
converted: OpenAPISpec;
37+
bundle?: Record<string, unknown>;
38+
}> {
3539
const { baseUrl } = context.siteConfig;
3640
const options: PluginOptionsWithDefault = { ...DEFAULT_OPTIONS, ...opts };
3741
const { debug, spec, url: downloadUrl, config } = options;
@@ -56,7 +60,10 @@ export default function redocPlugin(
5660
if (debug) {
5761
console.log('[REDOCUSAURUS_PLUGIN] bundling spec from url', spec);
5862
}
59-
return loadAndBundleSpec(spec!);
63+
const converted = await loadAndBundleSpec(spec!);
64+
return {
65+
converted,
66+
};
6067
}
6168

6269
// If local file
@@ -93,21 +100,26 @@ export default function redocPlugin(
93100
if (debug) {
94101
console.log('[REDOCUSAURUS_PLUGIN] File Bundled');
95102
}
103+
const converted = await loadAndBundleSpec(bundledSpec.parsed);
96104

97105
// If download url is not provided then use bundled yaml as a static file (see `postBuild`)
98106
url = url || fileName;
99-
return bundledSpec.parsed;
107+
108+
return {
109+
converted,
110+
bundle: bundledSpec.parsed,
111+
};
100112
},
101113
async contentLoaded({ content, actions }) {
102114
const { createData, addRoute, setGlobalData } = actions;
103-
if (!content) {
115+
if (!content?.converted) {
104116
throw new Error(`[Redocusaurus] Spec could not be parsed: ${spec}`);
105117
}
106118

107119
const data: SpecProps = {
108120
url,
109121
// eslint-disable-next-line @typescript-eslint/no-explicit-any
110-
spec: content as any,
122+
spec: content.converted as any,
111123
};
112124
setGlobalData(data);
113125

@@ -142,19 +154,24 @@ export default function redocPlugin(
142154
addRoute(routeOptions);
143155
}
144156
},
145-
async postBuild(props) {
157+
async postBuild({ content }) {
146158
if (!isSpecFile || downloadUrl) {
147159
return;
148160
}
149161
// Create a static file from bundled spec
150162
const staticFile = path.join(context.outDir, fileName);
151-
fs.mkdirSync(path.dirname(staticFile));
152-
console.error(
153-
'[REDOCUSAURUS_PLUGIN] creating static bundle copy for download',
154-
staticFile,
155-
);
163+
const dir = path.dirname(staticFile);
164+
if (!fs.existsSync(dir)) {
165+
fs.mkdirSync(dir);
166+
}
167+
if (debug) {
168+
console.error(
169+
'[REDOCUSAURUS_PLUGIN] creating static bundle copy for download',
170+
staticFile,
171+
);
172+
}
156173
// create bundled url
157-
const bundledYaml = stringifyYaml(props.content);
174+
const bundledYaml = stringifyYaml(content.bundle || content.converted);
158175
fs.writeFileSync(staticFile, bundledYaml);
159176
},
160177
getPathsToWatch() {

packages/docusaurus-theme-redoc/src/hooks/useSpec.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { useMemo } from 'react';
22
import useBaseUrl from '@docusaurus/useBaseUrl';
33
import { usePluginData } from '@docusaurus/useGlobalData';
44
import { useColorMode } from '@docusaurus/theme-common';
5-
import { AppStore } from 'redoc';
5+
import { AppStore, RedocRawOptions } from 'redoc';
66
import { SpecProps } from '../types/common';
77
import { GlobalData } from '../types/options';
88

@@ -21,9 +21,12 @@ export function useSpec({ spec, url }: SpecProps) {
2121
const { lightTheme, darkTheme, options: redocOptions } = themeOptions;
2222
const theme = isDarkTheme ? darkTheme : lightTheme;
2323

24-
const options = {
24+
const options: RedocRawOptions = {
2525
...redocOptions,
2626
theme,
27+
// Disable offset when server rendering
28+
scrollYOffset:
29+
typeof window === 'undefined' ? 0 : redocOptions.scrollYOffset,
2730
};
2831
// eslint-disable-next-line @typescript-eslint/no-explicit-any
2932
const store = new AppStore(spec as any, fullUrl, options);

packages/docusaurus-theme-redoc/src/types/options.d.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ export interface ThemeOptions {
3333
}
3434

3535
export type GlobalData = {
36-
options: NonNullable<ThemeOptions['redocOptions']>;
36+
options: NonNullable<ThemeOptions['options']>;
3737
darkTheme: Partial<RedocRawOptions['theme']>;
3838
lightTheme: Partial<RedocRawOptions['theme']>;
3939
};

0 commit comments

Comments
 (0)