UNPKG

@kubb/plugin-faker

Version:

Faker.js data generator plugin for Kubb, creating realistic mock data from OpenAPI specifications for development and testing.

98 lines (96 loc) 3.53 kB
import "./components-C-Ia_yc3.js"; import { t as fakerGenerator } from "./fakerGenerator-CXvRE7-U.js"; import path from "node:path"; import { PluginManager, createPlugin, getBarrelFiles, getMode } from "@kubb/core"; import { camelCase } from "@kubb/core/transformers"; import { OperationGenerator, SchemaGenerator, pluginOasName } from "@kubb/plugin-oas"; import { pluginTsName } from "@kubb/plugin-ts"; //#region src/plugin.ts const pluginFakerName = "plugin-faker"; const pluginFaker = createPlugin((options) => { const { output = { path: "mocks", barrelType: "named" }, seed, group, exclude = [], include, override = [], transformers: transformers$1 = {}, mapper = {}, unknownType = "any", emptySchemaType = unknownType, dateType = "string", dateParser = "faker", generators = [fakerGenerator].filter(Boolean), regexGenerator = "faker", contentType } = options; return { name: pluginFakerName, options: { output, transformers: transformers$1, seed, dateType, unknownType, emptySchemaType, dateParser, mapper, override, regexGenerator, group }, pre: [pluginOasName, pluginTsName], resolvePath(baseName, pathMode, options$1) { 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$1?.group?.path || options$1?.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$1.group.path : options$1.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$1?.name?.(resolvedName, type) || resolvedName; return resolvedName; }, async buildStart() { const [swaggerPlugin] = PluginManager.getDependedPlugins(this.plugins, [pluginOasName]); const oas = await swaggerPlugin.context.getOas(); const root = path.resolve(this.config.root, this.config.output.path); const mode = getMode(path.resolve(root, output.path)); const schemaFiles = await new SchemaGenerator(this.plugin.options, { oas, pluginManager: this.pluginManager, plugin: this.plugin, contentType, include: void 0, override, mode, output: output.path }).build(...generators); await this.addFile(...schemaFiles); const operationFiles = await new OperationGenerator(this.plugin.options, { oas, pluginManager: this.pluginManager, plugin: this.plugin, contentType, exclude, include, override, mode }).build(...generators); await this.addFile(...operationFiles); const barrelFiles = await getBarrelFiles(this.fileManager.files, { type: output.barrelType ?? "named", root, output, meta: { pluginKey: this.plugin.key }, logger: this.logger }); await this.addFile(...barrelFiles); } }; }); //#endregion export { pluginFaker, pluginFakerName }; //# sourceMappingURL=index.js.map