@kubb/plugin-faker
Version:
Faker.js data generator plugin for Kubb, creating realistic mock data from OpenAPI specifications for development and testing.
143 lines (142 loc) • 5.6 kB
JavaScript
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const require_components = require("./components-cpULzmDp.cjs");
const require_fakerGenerator = require("./fakerGenerator-2H7zW_3z.cjs");
let node_path = require("node:path");
node_path = require_components.__toESM(node_path);
let _kubb_core = require("@kubb/core");
let _kubb_plugin_oas = require("@kubb/plugin-oas");
let _kubb_plugin_ts = require("@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 = (0, _kubb_core.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 = [require_fakerGenerator.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: [_kubb_plugin_oas.pluginOasName, _kubb_plugin_ts.pluginTsName],
resolvePath(baseName, pathMode, options) {
const root = node_path.default.resolve(this.config.root, this.config.output.path);
if ((pathMode ?? (0, _kubb_core.getMode)(node_path.default.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 node_path.default.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 node_path.default.resolve(root, output.path, groupName({ group: group.type === "path" ? options.group.path : options.group.tag }), baseName);
}
return node_path.default.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 = node_path.default.resolve(this.config.root, this.config.output.path);
const mode = (0, _kubb_core.getMode)(node_path.default.resolve(root, output.path));
const oas = await this.getOas();
const schemaFiles = await new _kubb_plugin_oas.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 _kubb_plugin_oas.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 (0, _kubb_core.getBarrelFiles)(this.fabric.files, {
root,
output,
meta: { pluginKey: this.plugin.key }
});
await this.upsertFile(...barrelFiles);
}
};
});
//#endregion
exports.pluginFaker = pluginFaker;
exports.pluginFakerName = pluginFakerName;
//# sourceMappingURL=index.cjs.map