@kubb/plugin-faker
Version:
Faker.js data generator plugin for Kubb, creating realistic mock data from OpenAPI specifications for development and testing.
140 lines (139 loc) • 5.2 kB
JavaScript
import "./chunk--u3MIqq1.js";
import { t as fakerGenerator } from "./fakerGenerator-R2JHUQNn.js";
import path from "node:path";
import { definePlugin, getBarrelFiles, getMode } from "@kubb/core";
import { OperationGenerator, SchemaGenerator, pluginOasName } from "@kubb/plugin-oas";
import { pluginTsName } from "@kubb/plugin-ts";
//#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);
}
//#endregion
//#region src/plugin.ts
const pluginFakerName = "plugin-faker";
const pluginFaker = definePlugin((options) => {
const { output = {
path: "mocks",
barrelType: "named"
}, seed, group, exclude = [], include, override = [], transformers = {}, mapper = {}, unknownType = "any", emptySchemaType = unknownType, dateType = "string", integerType = "number", dateParser = "faker", generators = [fakerGenerator].filter(Boolean), regexGenerator = "faker", paramsCasing, contentType } = options;
return {
name: pluginFakerName,
options: {
output,
transformers,
seed,
dateType,
integerType,
unknownType,
emptySchemaType,
dateParser,
mapper,
override,
regexGenerator,
paramsCasing,
group,
usedEnumNames: {}
},
pre: [pluginOasName, pluginTsName],
resolvePath(baseName, pathMode, options) {
const root = path.resolve(this.config.root, this.config.output.path);
if ((pathMode ?? getMode(path.resolve(root, output.path))) === "single")
/**
* when output is a file then we will always append to the same file(output file), see fileManager.addOrAppend
* Other plugins then need to call addOrAppend instead of just add from the fileManager class
*/
return path.resolve(root, output.path);
if (group && (options?.group?.path || options?.group?.tag)) {
const groupName = group?.name ? group.name : (ctx) => {
if (group?.type === "path") return `${ctx.group.split("/")[1]}`;
return `${camelCase(ctx.group)}Controller`;
};
return path.resolve(root, output.path, groupName({ group: group.type === "path" ? options.group.path : options.group.tag }), baseName);
}
return path.resolve(root, output.path, baseName);
},
resolveName(name, type) {
const resolvedName = camelCase(name, {
prefix: type ? "create" : void 0,
isFile: type === "file"
});
if (type) return transformers?.name?.(resolvedName, type) || resolvedName;
return resolvedName;
},
async install() {
const root = path.resolve(this.config.root, this.config.output.path);
const mode = getMode(path.resolve(root, output.path));
const oas = await this.getOas();
const schemaFiles = await new SchemaGenerator(this.plugin.options, {
fabric: this.fabric,
oas,
pluginManager: this.pluginManager,
events: this.events,
plugin: this.plugin,
contentType,
include: void 0,
override,
mode,
output: output.path
}).build(...generators);
await this.upsertFile(...schemaFiles);
const operationFiles = await new OperationGenerator(this.plugin.options, {
fabric: this.fabric,
oas,
pluginManager: this.pluginManager,
events: this.events,
plugin: this.plugin,
contentType,
exclude,
include,
override,
mode
}).build(...generators);
await this.upsertFile(...operationFiles);
const barrelFiles = await getBarrelFiles(this.fabric.files, {
root,
output,
meta: { pluginKey: this.plugin.key }
});
await this.upsertFile(...barrelFiles);
}
};
});
//#endregion
export { pluginFaker, pluginFakerName };
//# sourceMappingURL=index.js.map