Skip to content

Commit

Permalink
Use redoc options while downloading specs (#264)
Browse files Browse the repository at this point in the history
  • Loading branch information
rohit-gohri authored Feb 10, 2023
1 parent bcbaa54 commit 137373e
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 26 deletions.
5 changes: 5 additions & 0 deletions .changeset/odd-pillows-switch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'docusaurus-plugin-redoc': patch
---

Use redoc config while loading spec
58 changes: 32 additions & 26 deletions packages/docusaurus-plugin-redoc/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import {
bundle,
loadConfig,
stringifyYaml,
Document,
NormalizedProblem,
} from '@redocly/openapi-core';

import {
Expand All @@ -24,6 +26,8 @@ import {
DEFAULT_OPTIONS,
} from './options';
import { SpecProps, ApiDocProps } from './types/common';
import { loadSpecWithConfig } from './loadSpec';

// eslint-disable-next-line @typescript-eslint/no-var-requires
const version = require('../package.json').version;

Expand Down Expand Up @@ -57,23 +61,6 @@ export default function redocPlugin(
return {
name: 'docusaurus-plugin-redoc',
async loadContent() {
if (!isSpecFile) {
// If spec is a remote url then add it as download url also as a default
url = url || spec;
if (debug) {
console.log('[REDOCUSAURUS_PLUGIN] bundling spec from url', spec);
}
const converted = await loadAndBundleSpec(spec!);
return {
converted,
};
}

// If local file
if (debug) {
console.log('[REDOCUSAURUS_PLUGIN] reading file: ', spec);
}

let redoclyConfig: Config;

if (config) {
Expand All @@ -89,16 +76,34 @@ export default function redocPlugin(
redoclyConfig = await loadConfig();
}

const {
bundle: bundledSpec,
problems,
fileDependencies,
} = await bundle({
ref: spec,
config: redoclyConfig,
});
let bundledSpec: Document, problems: NormalizedProblem[];

filesToWatch = [path.resolve(spec), ...fileDependencies];
if (!isSpecFile) {
// If spec is a remote url then add it as download url also as a default
url = url || spec;
if (debug) {
console.log('[REDOCUSAURUS_PLUGIN] bundling spec from url', spec);
}
({ bundle: bundledSpec, problems } = await loadSpecWithConfig(
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
spec!,
redoclyConfig,
));
} else {
// If local file
if (debug) {
console.log('[REDOCUSAURUS_PLUGIN] reading file: ', spec);
}

const fileBundle = await bundle({
ref: spec,
config: redoclyConfig,
});

({ bundle: bundledSpec, problems } = fileBundle);

filesToWatch = [path.resolve(spec), ...fileBundle.fileDependencies];
}

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

// If download url is not provided then use bundled yaml as a static file (see `postBuild`)
Expand Down
28 changes: 28 additions & 0 deletions packages/docusaurus-plugin-redoc/src/loadSpec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import type { Config, Source, Document } from '@redocly/openapi-core';
import { bundle } from '@redocly/openapi-core';

/**
* Based on loadAndBundleSpec from redoc.
* Customized to pass custom loaded config
* @see https://github.com/Redocly/redoc/blob/33be51a7a4068f44fd914314002c058a204ba0c2/src/utils/loadAndBundleSpec.ts
*/
export async function loadSpecWithConfig(
specUrlOrObject: object | string,
config: Config,
) {
const bundleOpts: Parameters<typeof bundle>[0] = {
config,
base: process.cwd(),
};

if (typeof specUrlOrObject === 'object' && specUrlOrObject !== null) {
bundleOpts.doc = {
source: { absoluteRef: '' } as Source,
parsed: specUrlOrObject,
} as Document;
} else {
bundleOpts.ref = specUrlOrObject;
}

return bundle(bundleOpts);
}

1 comment on commit 137373e

@vercel
Copy link

@vercel vercel bot commented on 137373e Feb 10, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.