Skip to content

Commit

Permalink
Add option to disable frontmatter api prop compression (#800)
Browse files Browse the repository at this point in the history
* add option to disable frontmatter openapi snippet compression

* disable compression for petstore

* explicitly check for true

* handle both compressed and uncompression api prop
  • Loading branch information
sserrata committed May 6, 2024
1 parent d2ff97b commit add373b
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 7 deletions.
1 change: 1 addition & 0 deletions demo/docusaurus.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,7 @@ const config = {
"https://raw.githubusercontent.com/PaloAltoNetworks/docusaurus-openapi-docs/main/demo/examples/petstore.yaml",
hideSendButton: false,
showSchemas: true,
disableCompression: true,
},
cos: {
specPath: "examples/openapi-cos.json",
Expand Down
9 changes: 6 additions & 3 deletions packages/docusaurus-plugin-openapi-docs/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ export default function pluginOpenAPIDocs(
markdownGenerators,
downloadUrl,
sidebarOptions,
disableCompression,
} = options;

// Remove trailing slash before proceeding
Expand Down Expand Up @@ -319,9 +320,11 @@ custom_edit_url: null
// const deserialize = (s: any) => {
// return zlib.inflateSync(Buffer.from(s, "base64")).toString();
// };
item.json = zlib
.deflateSync(JSON.stringify(item.api))
.toString("base64");
disableCompression === true
? (item.json = JSON.stringify(item.api))
: (item.json = zlib
.deflateSync(JSON.stringify(item.api))
.toString("base64"));
let infoBasePath = `${outputDir}/${item.infoId}`;
if (docRouteBasePath) {
infoBasePath = `${docRouteBasePath}/${outputDir
Expand Down
1 change: 1 addition & 0 deletions packages/docusaurus-plugin-openapi-docs/src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export const OptionsSchema = Joi.object({
sidebarOptions: sidebarOptions,
markdownGenerators: markdownGenerators,
showSchemas: Joi.boolean(),
disableCompression: Joi.boolean(),
version: Joi.string().when("versions", {
is: Joi.exist(),
then: Joi.required(),
Expand Down
1 change: 1 addition & 0 deletions packages/docusaurus-plugin-openapi-docs/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export interface APIOptions {
proxy?: string;
markdownGenerators?: MarkdownGenerator;
showSchemas?: boolean;
disableCompression?: boolean;
}

export interface MarkdownGenerator {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,12 @@ export default function ApiItem(props: Props): JSX.Element {
const { schema } = frontMatter as SchemaFrontMatter;
// decompress and parse
if (api) {
api = JSON.parse(
zlib.inflateSync(Buffer.from(api as any, "base64")).toString()
);
try {
api = JSON.parse(
zlib.inflateSync(Buffer.from(api as any, "base64")).toString()
);
} catch {}
}

const { siteConfig } = useDocusaurusContext();
const themeConfig = siteConfig.themeConfig as ThemeConfig;
const options = themeConfig.api;
Expand Down

0 comments on commit add373b

Please sign in to comment.