fumadocs-openapi
Version:
Generate MDX docs for your OpenAPI spec
105 lines (103 loc) • 2.8 kB
JavaScript
import { idToTitle } from "../id-to-title.js";
import { getTagDisplayName, methodKeys } from "../schema.js";
//#region src/utils/pages/builder.ts
async function fromServer(server, config) {
const schemas = await server.getSchemas();
const generated = {};
const entries = Object.entries(schemas);
if (entries.length === 0) throw new Error("No input files found.");
for (const [id, schema] of entries) generated[id] = fromSchema(id, schema, config);
return generated;
}
function fromSchema(schemaId, processed, config) {
const files = [];
const { toPages } = config;
const { dereferenced } = processed;
toPages({
id: schemaId,
document: processed,
create(entry) {
files.push(entry);
},
extract: () => extractInfo(dereferenced),
routePathToFilePath(path) {
return path.toLowerCase().replaceAll(".", "-").split("/").flatMap((v) => {
if (v.startsWith("{") && v.endsWith("}")) return v.slice(1, -1);
if (v.length === 0) return [];
return v;
}).join("/");
},
fromExtractedWebhook(item) {
const pathItem = dereferenced.webhooks?.[item.name];
if (!pathItem) return;
const operation = pathItem?.[item.method];
if (!operation) return;
return {
pathItem,
operation,
get displayName() {
return operation.summary || pathItem.summary || idToTitle(item.name);
}
};
},
fromExtractedOperation(item) {
const pathItem = dereferenced.paths?.[item.path];
if (!pathItem) return;
const operation = pathItem?.[item.method];
if (!operation) return;
return {
pathItem,
operation,
get displayName() {
return operation.summary || pathItem.summary || (operation.operationId ? idToTitle(operation.operationId) : item.path);
}
};
},
fromTag(tag) {
return { get displayName() {
return getTagDisplayName(tag);
} };
},
fromTagName(name) {
const tag = dereferenced.tags?.find((item) => item.name === name);
if (!tag) return;
return {
info: tag,
...this.fromTag(tag)
};
}
});
return files;
}
function extractInfo(document) {
const result = {
webhooks: [],
operations: []
};
for (const [path, pathItem] of Object.entries(document.paths ?? {})) {
if (!pathItem) continue;
for (const methodKey of methodKeys) {
if (!pathItem[methodKey]) continue;
result.operations.push({
method: methodKey,
path,
tags: pathItem[methodKey]?.tags
});
}
}
for (const [name, pathItem] of Object.entries(document.webhooks ?? {})) {
if (!pathItem) continue;
for (const methodKey of methodKeys) {
if (!pathItem[methodKey]) continue;
result.webhooks.push({
method: methodKey,
name,
tags: pathItem[methodKey]?.tags
});
}
}
return result;
}
//#endregion
export { fromSchema, fromServer };
//# sourceMappingURL=builder.js.map