fumadocs-openapi
Version:
Generate MDX docs for your OpenAPI spec
49 lines (47 loc) • 1.34 kB
JavaScript
import { idToTitle } from "./id-to-title.js";
//#region src/utils/schema.ts
const methodKeys = [
"get",
"post",
"patch",
"delete",
"head",
"put"
];
function getPreferredType(body) {
if ("application/json" in body) return "application/json";
return Object.keys(body)[0];
}
function getTagDisplayName(tag) {
return "x-displayName" in tag && typeof tag["x-displayName"] === "string" ? tag["x-displayName"] : idToTitle(tag.name);
}
/**
* Summarize method endpoint information
*/
function createMethod(method, path, operation) {
return {
description: path.description,
summary: path.summary,
...operation,
parameters: [...operation.parameters ?? [], ...path.parameters ?? []],
method: method.toUpperCase()
};
}
function pickExample(value) {
if (value.example !== void 0) return value.example;
if (value.content) {
const type = getPreferredType(value.content);
const content = type ? value.content[type] : void 0;
if (type && content) {
const out = value.examples?.[type].value ?? pickExample(content);
if (out !== void 0) return out;
}
}
if (value.examples) {
const examples = Object.values(value.examples);
if (examples.length > 0) return examples[0].value;
}
}
//#endregion
export { createMethod, getPreferredType, getTagDisplayName, methodKeys, pickExample };
//# sourceMappingURL=schema.js.map