fumadocs-openapi
Version:
Generate MDX docs for your OpenAPI spec
91 lines (89 loc) • 2.52 kB
JavaScript
import { getPreferredType } from "../utils/schema.js";
import { ClientLazy } from "./lazy.js";
import { jsx } from "react/jsx-runtime";
//#region src/playground/index.tsx
async function APIPlayground({ path, method, ctx }) {
if (ctx.playground?.render) return ctx.playground.render({
path,
method,
ctx
});
let currentId = 0;
const bodyContent = method.requestBody?.content;
const mediaType = bodyContent ? getPreferredType(bodyContent) : void 0;
const context = {
references: {},
nextId() {
return String(currentId++);
},
registered: /* @__PURE__ */ new WeakMap()
};
return /* @__PURE__ */ jsx(ClientLazy, {
securities: parseSecurities(method, ctx),
method: method.method,
route: path,
parameters: method.parameters,
body: bodyContent && mediaType ? {
schema: writeReferences(bodyContent[mediaType].schema, context),
mediaType
} : void 0,
references: context.references,
proxyUrl: ctx.proxyUrl,
writeOnly: true,
readOnly: false
});
}
function writeReferences(schema, ctx, stack = /* @__PURE__ */ new WeakMap()) {
if (typeof schema !== "object" || !schema) return schema;
if (stack.has(schema)) {
const out = stack.get(schema);
const id = ctx.nextId();
ctx.references[id] = out;
return { $ref: id };
}
const output = { ...schema };
stack.set(schema, output);
for (const _n in output) {
const name = _n;
if (!output[name]) continue;
switch (name) {
case "oneOf":
case "allOf":
case "anyOf":
output[name] = output[name].map((item) => writeReferences(item, ctx, stack));
continue;
case "items":
case "additionalProperties":
case "not":
output[name] = writeReferences(output[name], ctx, stack);
continue;
case "properties":
case "patternProperties":
output[name] = { ...output[name] };
for (const key in output[name]) output[name][key] = writeReferences(output[name][key], ctx, stack);
}
}
return output;
}
function parseSecurities(method, { schema: { dereferenced } }) {
const result = [];
const security = method.security ?? dereferenced.security ?? [];
if (security.length === 0) return result;
for (const map of security) {
const list = [];
for (const [key, scopes] of Object.entries(map)) {
const scheme = dereferenced.components?.securitySchemes?.[key];
if (!scheme) continue;
list.push({
...scheme,
scopes,
id: key
});
}
if (list.length > 0) result.push(list);
}
return result;
}
//#endregion
export { APIPlayground };
//# sourceMappingURL=index.js.map