UNPKG

@kubb/plugin-zod

Version:

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

184 lines (181 loc) 8.1 kB
'use strict'; var chunkFYI4GRXP_cjs = require('./chunk-FYI4GRXP.cjs'); var pluginOas = require('@kubb/plugin-oas'); var components = require('@kubb/plugin-oas/components'); var hooks = require('@kubb/plugin-oas/hooks'); var utils = require('@kubb/plugin-oas/utils'); var pluginTs = require('@kubb/plugin-ts'); var react = require('@kubb/react'); var jsxRuntime = require('@kubb/react/jsx-runtime'); var zodGenerator = pluginOas.createReactGenerator({ name: "zod", Operation({ operation, options }) { const { coercion: globalCoercion, inferred, typed, mapper, wrapOutput } = options; const { plugin, pluginManager, mode } = react.useApp(); const oas = hooks.useOas(); const { getSchemas, getFile, getGroup } = hooks.useOperationManager(); const schemaManager = hooks.useSchemaManager(); const file = getFile(operation); const schemas = getSchemas(operation); const schemaGenerator = new pluginOas.SchemaGenerator(options, { oas, plugin, pluginManager, mode, override: options.override }); const operationSchemas = [schemas.pathParams, schemas.queryParams, schemas.headerParams, schemas.statusCodes, schemas.request, schemas.response].flat().filter(Boolean); const mapOperationSchema = ({ name, schema: schemaObject, description, keysToOmit, ...options2 }, i) => { const required = Array.isArray(schemaObject?.required) ? !!schemaObject.required.length : !!schemaObject?.required; const someDefaults = Object.values(schemaObject.properties || {}).some((property) => Object.hasOwn(property, "default") && property.default !== void 0); const optional = !required && !someDefaults && name.includes("Params"); const tree = [...schemaGenerator.parse({ schemaObject, name }), optional ? { keyword: pluginOas.schemaKeywords.optional } : void 0].filter(Boolean); const imports = schemaManager.getImports(tree); const group = options2.operation ? getGroup(options2.operation) : void 0; const coercion = name.includes("Params") ? { numbers: true, strings: false, dates: true } : globalCoercion; const zod = { name: schemaManager.getName(name, { type: "function" }), inferTypeName: schemaManager.getName(name, { type: "type" }), file: schemaManager.getFile(name) }; const type = { name: schemaManager.getName(name, { type: "type", pluginKey: [pluginTs.pluginTsName] }), file: schemaManager.getFile(options2.operationName || name, { pluginKey: [pluginTs.pluginTsName], group }) }; return /* @__PURE__ */ jsxRuntime.jsxs(components.Oas.Schema, { name, schemaObject, tree, children: [ typed && /* @__PURE__ */ jsxRuntime.jsx(react.File.Import, { isTypeOnly: true, root: file.path, path: type.file.path, name: [type.name] }), typed && /* @__PURE__ */ jsxRuntime.jsx(react.File.Import, { isTypeOnly: true, path: "@kubb/plugin-zod/utils", name: ["ToZod"] }), imports.map((imp) => /* @__PURE__ */ jsxRuntime.jsx(react.File.Import, { root: file.path, path: imp.path, name: imp.name }, [imp.path, imp.name, imp.isTypeOnly].join("-"))), /* @__PURE__ */ jsxRuntime.jsx( chunkFYI4GRXP_cjs.Zod, { name: zod.name, typeName: typed ? type.name : void 0, inferTypeName: inferred ? zod.inferTypeName : void 0, description, tree, rawSchema: schemaObject, mapper, coercion, keysToOmit, wrapOutput, version: plugin.options.version, emptySchemaType: plugin.options.emptySchemaType } ) ] }, i); }; return /* @__PURE__ */ jsxRuntime.jsxs( react.File, { baseName: file.baseName, path: file.path, meta: file.meta, banner: utils.getBanner({ oas, output: plugin.options.output, config: pluginManager.config }), footer: utils.getFooter({ oas, output: plugin.options.output }), children: [ /* @__PURE__ */ jsxRuntime.jsx(react.File.Import, { name: ["z"], path: plugin.options.importPath }), operationSchemas.map(mapOperationSchema) ] } ); }, Schema({ schema, options }) { const { coercion, inferred, typed, mapper, importPath, wrapOutput, version } = options; const { getName, getFile, getImports } = hooks.useSchemaManager(); const { pluginManager, plugin: { options: { output, emptySchemaType } } } = react.useApp(); const oas = hooks.useOas(); const imports = getImports(schema.tree); const zod = { name: getName(schema.name, { type: "function" }), inferTypeName: getName(schema.name, { type: "type" }), file: getFile(schema.name) }; const type = { name: getName(schema.name, { type: "type", pluginKey: [pluginTs.pluginTsName] }), file: getFile(schema.name, { pluginKey: [pluginTs.pluginTsName] }) }; return /* @__PURE__ */ jsxRuntime.jsxs( react.File, { baseName: zod.file.baseName, path: zod.file.path, meta: zod.file.meta, banner: utils.getBanner({ oas, output, config: pluginManager.config }), footer: utils.getFooter({ oas, output }), children: [ /* @__PURE__ */ jsxRuntime.jsx(react.File.Import, { name: ["z"], path: importPath }), typed && /* @__PURE__ */ jsxRuntime.jsx(react.File.Import, { isTypeOnly: true, root: zod.file.path, path: type.file.path, name: [type.name] }), typed && /* @__PURE__ */ jsxRuntime.jsx(react.File.Import, { isTypeOnly: true, path: "@kubb/plugin-zod/utils", name: ["ToZod"] }), imports.map((imp) => /* @__PURE__ */ jsxRuntime.jsx(react.File.Import, { root: zod.file.path, path: imp.path, name: imp.name }, [imp.path, imp.name, imp.isTypeOnly].join("-"))), /* @__PURE__ */ jsxRuntime.jsx( chunkFYI4GRXP_cjs.Zod, { name: zod.name, typeName: typed ? type.name : void 0, inferTypeName: inferred ? zod.inferTypeName : void 0, description: schema.value.description, tree: schema.tree, rawSchema: schema.value, mapper, coercion, wrapOutput, version, emptySchemaType } ) ] } ); } }); var operationsGenerator = pluginOas.createReactGenerator({ name: "operations", Operations({ operations }) { const { pluginManager, plugin: { key: pluginKey, options: { output } } } = react.useApp(); const oas = hooks.useOas(); const { getFile, groupSchemasByName } = hooks.useOperationManager(); const name = "operations"; const file = pluginManager.getFile({ name, extname: ".ts", pluginKey }); const transformedOperations = operations.map((operation) => ({ operation, data: groupSchemasByName(operation, { type: "function" }) })); const imports = Object.entries(transformedOperations).map(([key, { data, operation }]) => { const names = [data.request, ...Object.values(data.responses), ...Object.values(data.parameters)].filter(Boolean); return /* @__PURE__ */ jsxRuntime.jsx(react.File.Import, { name: names, root: file.path, path: getFile(operation).path }, key); }).filter(Boolean); return /* @__PURE__ */ jsxRuntime.jsxs( react.File, { baseName: file.baseName, path: file.path, meta: file.meta, banner: utils.getBanner({ oas, output, config: pluginManager.config }), footer: utils.getFooter({ oas, output }), children: [ imports, /* @__PURE__ */ jsxRuntime.jsx(chunkFYI4GRXP_cjs.Operations, { name, operations: transformedOperations }) ] } ); } }); exports.operationsGenerator = operationsGenerator; exports.zodGenerator = zodGenerator; //# sourceMappingURL=chunk-6LDDO2JJ.cjs.map //# sourceMappingURL=chunk-6LDDO2JJ.cjs.map