@kubb/plugin-zod
Version:
Zod schema generator plugin for Kubb, creating type-safe validation schemas from OpenAPI specifications for runtime data validation.
136 lines (134 loc) • 4.2 kB
JavaScript
import { zodGenerator, operationsGenerator } from './chunk-I74P26CO.js';
import './chunk-4QMHDKCS.js';
import path from 'path';
import { createPlugin, PluginManager, FileManager } from '@kubb/core';
import { camelCase, pascalCase } from '@kubb/core/transformers';
import { pluginOasName, SchemaGenerator, OperationGenerator } from '@kubb/plugin-oas';
import { pluginTsName } from '@kubb/plugin-ts';
var pluginZodName = "plugin-zod";
var pluginZod = createPlugin((options) => {
const {
output = { path: "zod", barrelType: "named" },
group,
exclude = [],
include,
override = [],
transformers = {},
dateType = "string",
unknownType = "any",
emptySchemaType = unknownType,
typed = false,
mapper = {},
operations = false,
version = "3",
importPath = version === "4" ? "zod/v4" : "zod",
coercion = false,
inferred = false,
generators = [zodGenerator, operations ? operationsGenerator : void 0].filter(Boolean),
wrapOutput = void 0,
contentType
} = options;
return {
name: pluginZodName,
options: {
output,
transformers,
include,
exclude,
override,
typed,
dateType,
unknownType,
emptySchemaType,
mapper,
importPath,
coercion,
operations,
inferred,
group,
wrapOutput,
version
},
pre: [pluginOasName, typed ? pluginTsName : void 0].filter(Boolean),
resolvePath(baseName, pathMode, options2) {
const root = path.resolve(this.config.root, this.config.output.path);
const mode = pathMode ?? FileManager.getMode(path.resolve(root, output.path));
if (mode === "single") {
return path.resolve(root, output.path);
}
if (group && (options2?.group?.path || options2?.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" ? options2.group.path : options2.group.tag
}),
baseName
);
}
return path.resolve(root, output.path, baseName);
},
resolveName(name, type) {
let resolvedName = camelCase(name, {
suffix: type ? "schema" : void 0,
isFile: type === "file"
});
if (type === "type") {
resolvedName = pascalCase(resolvedName);
}
if (type) {
return transformers?.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 = FileManager.getMode(path.resolve(root, output.path));
const schemaGenerator = new SchemaGenerator(this.plugin.options, {
oas,
pluginManager: this.pluginManager,
plugin: this.plugin,
contentType,
include: void 0,
override,
mode,
output: output.path
});
const schemaFiles = await schemaGenerator.build(...generators);
await this.addFile(...schemaFiles);
const operationGenerator = new OperationGenerator(this.plugin.options, {
oas,
pluginManager: this.pluginManager,
plugin: this.plugin,
contentType,
exclude,
include,
override,
mode
});
const operationFiles = await operationGenerator.build(...generators);
await this.addFile(...operationFiles);
const barrelFiles = await this.fileManager.getBarrelFiles({
type: output.barrelType ?? "named",
root,
output,
meta: {
pluginKey: this.plugin.key
},
logger: this.logger
});
await this.addFile(...barrelFiles);
}
};
});
export { pluginZod, pluginZodName };
//# sourceMappingURL=index.js.map
//# sourceMappingURL=index.js.map