Skip to content

Commit 8eb8032

Browse files
committed
Add compressed sample code responses to frontmatter
1 parent 7aa8265 commit 8eb8032

1 file changed

Lines changed: 49 additions & 4 deletions

File tree

  • packages/docusaurus-plugin-openapi-docs/src

packages/docusaurus-plugin-openapi-docs/src/index.ts

Lines changed: 49 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,11 @@ import chalk from "chalk";
1515
import { render } from "mustache";
1616

1717
import { createApiPageMD, createInfoPageMD, createTagPageMD } from "./markdown";
18-
import { readOpenapiFiles, processOpenapiFiles } from "./openapi";
18+
import {
19+
readOpenapiFiles,
20+
processOpenapiFiles,
21+
sampleResponseFromSchema,
22+
} from "./openapi";
1923
import { OptionsSchema } from "./options";
2024
import generateSidebarSlice from "./sidebars";
2125
import type { PluginOptions, LoadedContent, APIOptions } from "./types";
@@ -210,6 +214,9 @@ hide_table_of_contents: true
210214
api: {{{json}}}
211215
{{/json}}
212216
{{#api.method}}
217+
{{#sampleResponses}}
218+
sample_responses: {{{sampleResponses}}}
219+
{{/sampleResponses}}
213220
sidebar_class_name: "{{{api.method}}} api-method"
214221
{{/api.method}}
215222
{{#infoPath}}
@@ -242,7 +249,7 @@ custom_edit_url: null
242249
{{{markdown}}}
243250
244251
import DocCardList from '@theme/DocCardList';
245-
import {useCurrentSidebarCategory} from '@mxenabled/docusaurus-theme-common';
252+
import {useCurrentSidebarCategory} from '@docusaurus/theme-common';
246253
247254
<DocCardList items={useCurrentSidebarCategory().items}/>
248255
`;
@@ -257,7 +264,7 @@ custom_edit_url: null
257264
{{{markdown}}}
258265
259266
import DocCardList from '@theme/DocCardList';
260-
import {useCurrentSidebarCategory} from '@mxenabled/docusaurus-theme-common';
267+
import {useCurrentSidebarCategory} from '@docusaurus/theme-common';
261268
262269
<DocCardList items={useCurrentSidebarCategory().items}/>
263270
`;
@@ -282,8 +289,46 @@ import {useCurrentSidebarCategory} from '@mxenabled/docusaurus-theme-common';
282289
? infoPageGenerator(item)
283290
: tagPageGenerator(item);
284291
item.markdown = markdown;
292+
285293
if (item.type === "api") {
286-
// item.json = JSON.stringify(item.api);
294+
if (item.type === "api") {
295+
// get sample responses
296+
const { responses } = item.api;
297+
const statusCodes = Object.keys(responses);
298+
299+
const responseExamples = Object.entries(responses).reduce(
300+
(acc, [status, value]) => {
301+
const { content } = value;
302+
303+
for (const mimeType in content) {
304+
// TODO: handle other mime types if needed
305+
if (
306+
mimeType.endsWith("json") &&
307+
content.hasOwnProperty(mimeType)
308+
) {
309+
const bodyContent =
310+
content[mimeType].schema ?? content[mimeType];
311+
312+
const example = sampleResponseFromSchema(bodyContent);
313+
acc[status] = example;
314+
}
315+
}
316+
317+
return acc;
318+
},
319+
{} as { [key: string]: any }
320+
);
321+
322+
if (statusCodes.length) {
323+
// opportunity to compress JSON
324+
item.sampleResponses = {
325+
statusCodes,
326+
responseExamples: zlib
327+
.deflateSync(JSON.stringify(responseExamples))
328+
.toString("base64"),
329+
};
330+
}
331+
}
287332

288333
// opportunity to compress JSON
289334
// const serialize = (o: any) => {

0 commit comments

Comments
 (0)