fumadocs-openapi
Version:
Generate MDX docs for your OpenAPI spec
88 lines (86 loc) • 2.63 kB
JavaScript
import { MethodLabel } from "../ui/components/method-label.js";
import { Fragment, jsx, jsxs } from "react/jsx-runtime";
//#region src/server/source-api.tsx
/**
* Fumadocs Source API integration, pass this to `plugins` array in `loader()`.
*/
function openapiPlugin() {
return {
name: "fumadocs:openapi",
enforce: "pre",
transformPageTree: { file(node, filePath) {
if (!filePath) return node;
const file = this.storage.read(filePath);
if (!file || file.format !== "page") return node;
const openApiData = file.data._openapi;
if (!openApiData || typeof openApiData !== "object") return node;
if (openApiData.webhook) node.name = /* @__PURE__ */ jsxs(Fragment, { children: [
node.name,
" ",
/* @__PURE__ */ jsx("span", {
className: "ms-auto border border-current px-1 rounded-lg text-xs text-nowrap font-mono",
children: "Webhook"
})
] });
else if (openApiData.method) node.name = /* @__PURE__ */ jsxs(Fragment, { children: [
node.name,
" ",
/* @__PURE__ */ jsx(MethodLabel, {
className: "ms-auto text-xs text-nowrap",
children: openApiData.method
})
] });
return node;
} }
};
}
/**
* Generate virtual pages for Fumadocs Source API
*/
async function openapiSource(server, options = {}) {
const { baseDir = "" } = options;
const { createAutoPreset } = await import("../utils/pages/preset-auto.js");
const { fromServer } = await import("../utils/pages/builder.js");
const { toBody } = await import("../utils/pages/to-body.js");
const { toStaticData } = await import("../utils/pages/to-static-data.js");
const files = [];
const entries = await fromServer(server, createAutoPreset(options));
for (const [schemaId, list] of Object.entries(entries)) {
const processed = await server.getSchema(schemaId);
for (const entry of list) {
const props = toBody(entry);
props.showDescription ??= true;
files.push({
type: "page",
path: `${baseDir}/${entry.path}`,
data: {
...entry.info,
getAPIPageProps() {
return props;
},
getSchema() {
return {
id: schemaId,
...processed
};
},
...toStaticData(props, processed.dereferenced),
_openapi: {
method: entry.type === "operation" || entry.type === "webhook" ? entry.item.method : void 0,
webhook: entry.type === "webhook"
}
}
});
}
}
return { files };
}
/**
* @deprecated use `openapiPlugin()`
*/
function transformerOpenAPI() {
return openapiPlugin().transformPageTree;
}
//#endregion
export { openapiPlugin, openapiSource, transformerOpenAPI };
//# sourceMappingURL=source-api.js.map