UNPKG

@kubb/plugin-zod

Version:

Zod schema generator plugin for Kubb, creating type-safe validation schemas from OpenAPI specifications for runtime data validation.

143 lines (138 loc) 4.58 kB
'use strict'; var chunk6LDDO2JJ_cjs = require('./chunk-6LDDO2JJ.cjs'); require('./chunk-FYI4GRXP.cjs'); var path = require('path'); var core = require('@kubb/core'); var transformers = require('@kubb/core/transformers'); var pluginOas = require('@kubb/plugin-oas'); var pluginTs = require('@kubb/plugin-ts'); function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; } var path__default = /*#__PURE__*/_interopDefault(path); var pluginZodName = "plugin-zod"; var pluginZod = core.createPlugin((options) => { const { output = { path: "zod", barrelType: "named" }, group, exclude = [], include, override = [], transformers: transformers$1 = {}, dateType = "string", unknownType = "any", emptySchemaType = unknownType, typed = false, mapper = {}, operations = false, version = "3", importPath = version === "4" ? "zod/v4" : "zod", coercion = false, inferred = false, generators = [chunk6LDDO2JJ_cjs.zodGenerator, operations ? chunk6LDDO2JJ_cjs.operationsGenerator : void 0].filter(Boolean), wrapOutput = void 0, contentType } = options; return { name: pluginZodName, options: { output, transformers: transformers$1, include, exclude, override, typed, dateType, unknownType, emptySchemaType, mapper, importPath, coercion, operations, inferred, group, wrapOutput, version }, pre: [pluginOas.pluginOasName, typed ? pluginTs.pluginTsName : void 0].filter(Boolean), resolvePath(baseName, pathMode, options2) { const root = path__default.default.resolve(this.config.root, this.config.output.path); const mode = pathMode ?? core.FileManager.getMode(path__default.default.resolve(root, output.path)); if (mode === "single") { return path__default.default.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 `${transformers.camelCase(ctx.group)}Controller`; }; return path__default.default.resolve( root, output.path, groupName({ group: group.type === "path" ? options2.group.path : options2.group.tag }), baseName ); } return path__default.default.resolve(root, output.path, baseName); }, resolveName(name, type) { let resolvedName = transformers.camelCase(name, { suffix: type ? "schema" : void 0, isFile: type === "file" }); if (type === "type") { resolvedName = transformers.pascalCase(resolvedName); } if (type) { return transformers$1?.name?.(resolvedName, type) || resolvedName; } return resolvedName; }, async buildStart() { const [swaggerPlugin] = core.PluginManager.getDependedPlugins(this.plugins, [pluginOas.pluginOasName]); const oas = await swaggerPlugin.context.getOas(); const root = path__default.default.resolve(this.config.root, this.config.output.path); const mode = core.FileManager.getMode(path__default.default.resolve(root, output.path)); const schemaGenerator = new pluginOas.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 pluginOas.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); } }; }); exports.pluginZod = pluginZod; exports.pluginZodName = pluginZodName; //# sourceMappingURL=index.cjs.map //# sourceMappingURL=index.cjs.map