@kubb/plugin-oas
Version:
OpenAPI Specification (OAS) plugin for Kubb, providing core functionality for parsing and processing OpenAPI/Swagger schemas for code generation.
112 lines (109 loc) • 3.58 kB
JavaScript
const require_chunk = require('./chunk-CbDLau6x.cjs');
let __kubb_oas = require("@kubb/oas");
//#region src/utils/getSchemaFactory.ts
/**
* Creates a factory function that generates a versioned OpenAPI schema result.
*
* The returned function accepts an optional schema object and produces a {@link SchemaResult} containing the dereferenced schema and the OpenAPI version ('3.0' or '3.1').
*
* @returns A function that takes an optional schema and returns a versioned schema result.
*/
function getSchemaFactory(oas) {
return (schema) => {
const version = (0, __kubb_oas.isOpenApiV3_1Document)(oas.api) ? "3.1" : "3.0";
return {
schemaObject: oas.dereferenceWithRef(schema),
version
};
};
}
//#endregion
//#region src/utils/getSchemas.ts
/**
* Collect all schema $ref dependencies recursively.
*/
function collectRefs(schema, refs = /* @__PURE__ */ new Set()) {
if (Array.isArray(schema)) {
for (const item of schema) collectRefs(item, refs);
return refs;
}
if (schema && typeof schema === "object") for (const [key, value] of Object.entries(schema)) if (key === "$ref" && typeof value === "string") {
const match = value.match(/^#\/components\/schemas\/(.+)$/);
if (match) refs.add(match[1]);
} else collectRefs(value, refs);
return refs;
}
/**
* Sort schemas topologically so referenced schemas appear first.
*/
function sortSchemas(schemas) {
const deps = /* @__PURE__ */ new Map();
for (const [name, schema] of Object.entries(schemas)) deps.set(name, Array.from(collectRefs(schema)));
const sorted = [];
const visited = /* @__PURE__ */ new Set();
function visit(name, stack = /* @__PURE__ */ new Set()) {
if (visited.has(name)) return;
if (stack.has(name)) return;
stack.add(name);
const children = deps.get(name) || [];
for (const child of children) if (deps.has(child)) visit(child, stack);
stack.delete(name);
visited.add(name);
sorted.push(name);
}
for (const name of Object.keys(schemas)) visit(name);
const sortedSchemas = {};
for (const name of sorted) sortedSchemas[name] = schemas[name];
return sortedSchemas;
}
/**
* Collect schemas from OpenAPI components (schemas, responses, requestBodies)
* and return them in dependency order.
*/
function getSchemas({ oas, contentType, includes = [
"schemas",
"requestBodies",
"responses"
] }) {
const components = oas.getDefinition().components;
let schemas = {};
if (includes.includes("schemas")) schemas = {
...schemas,
...components?.schemas || {}
};
if (includes.includes("responses")) {
const responses = components?.responses || {};
for (const [name, response] of Object.entries(responses)) {
const r = response;
if (r.content && !schemas[name]) {
const firstContentType = Object.keys(r.content)[0] || "application/json";
schemas[name] = r.content?.[contentType || firstContentType]?.schema;
}
}
}
if (includes.includes("requestBodies")) {
const requestBodies = components?.requestBodies || {};
for (const [name, request] of Object.entries(requestBodies)) {
const r = request;
if (r.content && !schemas[name]) {
const firstContentType = Object.keys(r.content)[0] || "application/json";
schemas[name] = r.content?.[contentType || firstContentType]?.schema;
}
}
}
return sortSchemas(schemas);
}
//#endregion
Object.defineProperty(exports, 'getSchemaFactory', {
enumerable: true,
get: function () {
return getSchemaFactory;
}
});
Object.defineProperty(exports, 'getSchemas', {
enumerable: true,
get: function () {
return getSchemas;
}
});
//# sourceMappingURL=getSchemas-BTCpbjet.cjs.map