@kubb/plugin-zod
Version:
Zod schema generator plugin for Kubb, creating type-safe validation schemas from OpenAPI specifications for runtime data validation.
181 lines (179 loc) • 7.66 kB
JavaScript
import { Zod, Operations } from './chunk-4QMHDKCS.js';
import { createReactGenerator, SchemaGenerator, schemaKeywords } from '@kubb/plugin-oas';
import { Oas } from '@kubb/plugin-oas/components';
import { useSchemaManager, useOas, useOperationManager } from '@kubb/plugin-oas/hooks';
import { getFooter, getBanner } from '@kubb/plugin-oas/utils';
import { pluginTsName } from '@kubb/plugin-ts';
import { useApp, File } from '@kubb/react';
import { jsxs, jsx } from '@kubb/react/jsx-runtime';
var zodGenerator = createReactGenerator({
name: "zod",
Operation({ operation, options }) {
const { coercion: globalCoercion, inferred, typed, mapper, wrapOutput } = options;
const { plugin, pluginManager, mode } = useApp();
const oas = useOas();
const { getSchemas, getFile, getGroup } = useOperationManager();
const schemaManager = useSchemaManager();
const file = getFile(operation);
const schemas = getSchemas(operation);
const schemaGenerator = new 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: 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: [pluginTsName]
}),
file: schemaManager.getFile(options2.operationName || name, {
pluginKey: [pluginTsName],
group
})
};
return /* @__PURE__ */ jsxs(Oas.Schema, { name, schemaObject, tree, children: [
typed && /* @__PURE__ */ jsx(File.Import, { isTypeOnly: true, root: file.path, path: type.file.path, name: [type.name] }),
typed && /* @__PURE__ */ jsx(File.Import, { isTypeOnly: true, path: "@kubb/plugin-zod/utils", name: ["ToZod"] }),
imports.map((imp) => /* @__PURE__ */ jsx(File.Import, { root: file.path, path: imp.path, name: imp.name }, [imp.path, imp.name, imp.isTypeOnly].join("-"))),
/* @__PURE__ */ jsx(
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__ */ jsxs(
File,
{
baseName: file.baseName,
path: file.path,
meta: file.meta,
banner: getBanner({ oas, output: plugin.options.output, config: pluginManager.config }),
footer: getFooter({ oas, output: plugin.options.output }),
children: [
/* @__PURE__ */ jsx(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 } = useSchemaManager();
const {
pluginManager,
plugin: {
options: { output, emptySchemaType }
}
} = useApp();
const oas = 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: [pluginTsName] }),
file: getFile(schema.name, { pluginKey: [pluginTsName] })
};
return /* @__PURE__ */ jsxs(
File,
{
baseName: zod.file.baseName,
path: zod.file.path,
meta: zod.file.meta,
banner: getBanner({ oas, output, config: pluginManager.config }),
footer: getFooter({ oas, output }),
children: [
/* @__PURE__ */ jsx(File.Import, { name: ["z"], path: importPath }),
typed && /* @__PURE__ */ jsx(File.Import, { isTypeOnly: true, root: zod.file.path, path: type.file.path, name: [type.name] }),
typed && /* @__PURE__ */ jsx(File.Import, { isTypeOnly: true, path: "@kubb/plugin-zod/utils", name: ["ToZod"] }),
imports.map((imp) => /* @__PURE__ */ jsx(File.Import, { root: zod.file.path, path: imp.path, name: imp.name }, [imp.path, imp.name, imp.isTypeOnly].join("-"))),
/* @__PURE__ */ jsx(
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 = createReactGenerator({
name: "operations",
Operations({ operations }) {
const {
pluginManager,
plugin: {
key: pluginKey,
options: { output }
}
} = useApp();
const oas = useOas();
const { getFile, groupSchemasByName } = 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__ */ jsx(File.Import, { name: names, root: file.path, path: getFile(operation).path }, key);
}).filter(Boolean);
return /* @__PURE__ */ jsxs(
File,
{
baseName: file.baseName,
path: file.path,
meta: file.meta,
banner: getBanner({ oas, output, config: pluginManager.config }),
footer: getFooter({ oas, output }),
children: [
imports,
/* @__PURE__ */ jsx(Operations, { name, operations: transformedOperations })
]
}
);
}
});
export { operationsGenerator, zodGenerator };
//# sourceMappingURL=chunk-I74P26CO.js.map
//# sourceMappingURL=chunk-I74P26CO.js.map