@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 (111 loc) • 4.18 kB
JavaScript
import "./chunk--u3MIqq1.js";
import path from "node:path";
import { isFunction } from "remeda";
//#region ../../internals/utils/src/casing.ts
/**
* Shared implementation for camelCase and PascalCase conversion.
* Splits on common word boundaries (spaces, hyphens, underscores, dots, slashes, colons)
* and capitalizes each word according to `pascal`.
*
* When `pascal` is `true` the first word is also capitalized (PascalCase), otherwise only subsequent words are.
*/
function toCamelOrPascal(text, pascal) {
return text.trim().replace(/([a-z\d])([A-Z])/g, "$1 $2").replace(/([A-Z]+)([A-Z][a-z])/g, "$1 $2").replace(/(\d)([a-z])/g, "$1 $2").split(/[\s\-_./\\:]+/).filter(Boolean).map((word, i) => {
if (word.length > 1 && word === word.toUpperCase()) return word;
if (i === 0 && !pascal) return word.charAt(0).toLowerCase() + word.slice(1);
return word.charAt(0).toUpperCase() + word.slice(1);
}).join("").replace(/[^a-zA-Z0-9]/g, "");
}
/**
* Splits `text` on `.` and applies `transformPart` to each segment.
* The last segment receives `isLast = true`, all earlier segments receive `false`.
* Segments are joined with `/` to form a file path.
*/
function applyToFileParts(text, transformPart) {
const parts = text.split(".");
return parts.map((part, i) => transformPart(part, i === parts.length - 1)).join("/");
}
/**
* Converts `text` to camelCase.
* When `isFile` is `true`, dot-separated segments are each cased independently and joined with `/`.
*
* @example
* camelCase('hello-world') // 'helloWorld'
* camelCase('pet.petId', { isFile: true }) // 'pet/petId'
*/
function camelCase(text, { isFile, prefix = "", suffix = "" } = {}) {
if (isFile) return applyToFileParts(text, (part, isLast) => camelCase(part, isLast ? {
prefix,
suffix
} : {}));
return toCamelOrPascal(`${prefix} ${text} ${suffix}`, false);
}
/**
* Converts `text` to PascalCase.
* When `isFile` is `true`, the last dot-separated segment is PascalCased and earlier segments are camelCased.
*
* @example
* pascalCase('hello-world') // 'HelloWorld'
* pascalCase('pet.petId', { isFile: true }) // 'pet/PetId'
*/
function pascalCase(text, { isFile, prefix = "", suffix = "" } = {}) {
if (isFile) return applyToFileParts(text, (part, isLast) => isLast ? pascalCase(part, {
prefix,
suffix
}) : camelCase(part));
return toCamelOrPascal(`${prefix} ${text} ${suffix}`, true);
}
//#endregion
//#region src/utils/getBanner.ts
/**
* Generate a default banner for files created by Kubb
* @returns A string with the default banner
*/
function getDefaultBanner({ title, description, version, config }) {
try {
let source = "";
if ("path" in config.input) source = path.basename(config.input.path);
else if ("data" in config.input) source = "text content";
let banner = "/**\n* Generated by Kubb (https://kubb.dev/).\n* Do not edit manually.\n";
if (config.output.defaultBanner === "simple") {
banner += "*/\n";
return banner;
}
if (source) banner += `* Source: ${source}\n`;
if (title) banner += `* Title: ${title}\n`;
if (description) {
const formattedDescription = description.replace(/\n/gm, "\n* ");
banner += `* Description: ${formattedDescription}\n`;
}
if (version) banner += `* OpenAPI spec version: ${version}\n`;
banner += "*/\n";
return banner;
} catch (_error) {
return "/**\n* Generated by Kubb (https://kubb.dev/).\n* Do not edit manually.\n*/";
}
}
function getBanner({ output, oas, config }) {
let banner = "";
if (config?.output?.defaultBanner !== false && config) {
const { title, description, version } = oas.api?.info || {};
banner = getDefaultBanner({
title,
description,
version,
config
});
}
if (!output.banner) return banner;
if (isFunction(output.banner)) return `${output.banner(oas)}\n${banner}`;
return `${output.banner}\n${banner}`;
}
//#endregion
//#region src/utils/getFooter.ts
function getFooter({ output, oas }) {
if (!output.footer) return;
if (isFunction(output.footer)) return output.footer(oas);
return output.footer;
}
//#endregion
export { pascalCase as i, getBanner as n, camelCase as r, getFooter as t };
//# sourceMappingURL=getFooter-Pw3tLCiV.js.map