fumadocs-openapi
Version:
Generate MDX docs for your OpenAPI spec
98 lines (96 loc) • 3.49 kB
JavaScript
import { removeUndefined } from "../remove-undefined.js";
import { toStaticData } from "./to-static-data.js";
import { dump } from "js-yaml";
//#region src/utils/pages/to-text.ts
function toText(entry, processed, options = {}) {
switch (entry.type) {
case "operation": return generatePage(entry.schemaId, processed, { operations: [entry.item] }, {
...options,
...entry.info
}, { type: "operation" });
case "group": return generatePage(entry.schemaId, processed, {
operations: entry.operations,
webhooks: entry.webhooks,
showTitle: true
}, {
...options,
...entry.info
}, { type: "file" });
case "tag": return generatePage(entry.schemaId, processed, {
operations: entry.operations,
webhooks: entry.webhooks,
showTitle: true
}, {
...options,
...entry.info
}, {
type: "tag",
tag: entry.rawTag
});
case "webhook": return generatePage(entry.schemaId, processed, { webhooks: [entry.item] }, {
...options,
...entry.info
}, { type: "operation" });
}
}
function generateDocument(frontmatter, content, options) {
const { addGeneratedComment = true, imports } = options;
const out = [];
const banner = dump(removeUndefined(frontmatter)).trimEnd();
if (banner.length > 0) out.push(`---\n${banner}\n---`);
if (addGeneratedComment) {
let commentContent = "This file was generated by Fumadocs. Do not edit this file directly. Any changes should be made by running the generation command again.";
if (typeof addGeneratedComment === "string") commentContent = addGeneratedComment;
commentContent = commentContent.replaceAll("/", "\\/");
out.push(`{/* ${commentContent} */}`);
}
if (imports) out.push(...imports.map((item) => `import { ${item.names.join(", ")} } from ${JSON.stringify(item.from)};`).join("\n"));
out.push(content);
return out.join("\n\n");
}
function generatePage(schemaId, processed, pageProps, options, context) {
const { frontmatter, includeDescription = false } = options;
const extend = frontmatter?.(options.title, options.description, context);
const page = {
...pageProps,
document: schemaId
};
let meta;
if (page.operations?.length === 1) meta = { method: page.operations[0].method.toUpperCase() };
else if (page.webhooks?.length === 1) meta = {
method: page.webhooks[0].method.toUpperCase(),
webhook: true
};
const data = toStaticData(page, processed.dereferenced);
const content = [];
if (options.description && includeDescription) content.push(options.description);
content.push(pageContent(page));
return generateDocument({
title: options.title,
description: !includeDescription ? options.description : void 0,
full: true,
...extend,
_openapi: {
...meta,
...data,
...extend?._openapi
}
}, content.join("\n\n"), options);
}
function pageContent({ showTitle, showDescription, document, webhooks, operations }) {
const propStrs = [`document={${JSON.stringify(document)}}`];
if (webhooks) propStrs.push(`webhooks={${JSON.stringify(webhooks.map((item) => ({
name: item.name,
method: item.method
})))}}`);
if (operations) propStrs.push(`operations={${JSON.stringify(operations.map((item) => ({
path: item.path,
method: item.method
})))}}`);
if (showTitle) propStrs.push(`showTitle={${JSON.stringify(showTitle)}}`);
if (showDescription) propStrs.push(`showDescription={${JSON.stringify(showDescription)}}`);
return `<APIPage ${propStrs.join(" ")} />`;
}
//#endregion
export { generateDocument, toText };
//# sourceMappingURL=to-text.js.map