@kubb/plugin-zod 5.0.0-alpha.3 → 5.0.0-alpha.31

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (46) hide show
  1. package/dist/index.cjs +1619 -100
  2. package/dist/index.cjs.map +1 -1
  3. package/dist/index.d.ts +418 -4
  4. package/dist/index.js +1614 -100
  5. package/dist/index.js.map +1 -1
  6. package/package.json +6 -34
  7. package/src/components/Operations.tsx +22 -15
  8. package/src/components/Zod.tsx +20 -119
  9. package/src/constants.ts +5 -0
  10. package/src/generators/zodGenerator.tsx +129 -159
  11. package/src/generators/zodGeneratorLegacy.tsx +365 -0
  12. package/src/index.ts +12 -1
  13. package/src/plugin.ts +102 -148
  14. package/src/presets.ts +30 -0
  15. package/src/printers/printerZod.ts +298 -0
  16. package/src/printers/printerZodMini.ts +273 -0
  17. package/src/resolvers/resolverZod.ts +61 -0
  18. package/src/resolvers/resolverZodLegacy.ts +60 -0
  19. package/src/types.ts +172 -93
  20. package/src/utils.ts +248 -0
  21. package/dist/components-B7zUFnAm.cjs +0 -890
  22. package/dist/components-B7zUFnAm.cjs.map +0 -1
  23. package/dist/components-eECfXVou.js +0 -842
  24. package/dist/components-eECfXVou.js.map +0 -1
  25. package/dist/components.cjs +0 -4
  26. package/dist/components.d.ts +0 -56
  27. package/dist/components.js +0 -2
  28. package/dist/generators-CRKtFRi1.js +0 -290
  29. package/dist/generators-CRKtFRi1.js.map +0 -1
  30. package/dist/generators-CzSLRVqQ.cjs +0 -301
  31. package/dist/generators-CzSLRVqQ.cjs.map +0 -1
  32. package/dist/generators.cjs +0 -4
  33. package/dist/generators.d.ts +0 -503
  34. package/dist/generators.js +0 -2
  35. package/dist/templates/ToZod.source.cjs +0 -7
  36. package/dist/templates/ToZod.source.cjs.map +0 -1
  37. package/dist/templates/ToZod.source.d.ts +0 -7
  38. package/dist/templates/ToZod.source.js +0 -6
  39. package/dist/templates/ToZod.source.js.map +0 -1
  40. package/dist/types-D0wsPC6Y.d.ts +0 -172
  41. package/src/components/index.ts +0 -2
  42. package/src/generators/index.ts +0 -2
  43. package/src/generators/operationsGenerator.tsx +0 -50
  44. package/src/parser.ts +0 -909
  45. package/src/templates/ToZod.source.ts +0 -4
  46. package/templates/ToZod.ts +0 -61
@@ -1,172 +0,0 @@
1
- import { t as __name } from "./chunk--u3MIqq1.js";
2
- import { Group, Output, PluginFactoryOptions, ResolveNameParams } from "@kubb/core";
3
- import { Exclude, Include, Override, ResolvePathOptions, Schema } from "@kubb/plugin-oas";
4
- import { Generator } from "@kubb/plugin-oas/generators";
5
- import { Oas, SchemaObject, contentType } from "@kubb/oas";
6
-
7
- //#region src/types.d.ts
8
- type Options = {
9
- /**
10
- * @default 'zod'
11
- */
12
- output?: Output<Oas>;
13
- /**
14
- * Define which contentType should be used.
15
- * By default, the first JSON valid mediaType is used
16
- */
17
- contentType?: contentType;
18
- /**
19
- * Group the Zod schemas based on the provided name.
20
- */
21
- group?: Group;
22
- /**
23
- * Array containing exclude parameters to exclude/skip tags/operations/methods/paths.
24
- */
25
- exclude?: Array<Exclude>;
26
- /**
27
- * Array containing include parameters to include tags/operations/methods/paths.
28
- */
29
- include?: Array<Include>;
30
- /**
31
- * Array containing override parameters to override `options` based on tags/operations/methods/paths.
32
- */
33
- override?: Array<Override<ResolvedOptions>>;
34
- /**
35
- * Path to Zod
36
- * It used as `import { z } from '${importPath}'`.
37
- * Accepts relative and absolute paths.
38
- * Path is used as-is; relative paths are based on the generated file location.
39
- * @default 'zod'
40
- */
41
- importPath?: string;
42
- /**
43
- * Choose to use date or datetime as JavaScript Date instead of string.
44
- * - false falls back to a simple z.string() format.
45
- * - 'string' uses z.string().datetime() for datetime validation.
46
- * - 'stringOffset' uses z.string().datetime({ offset: true }) for datetime with timezone offset validation.
47
- * - 'stringLocal' uses z.string().datetime({ local: true }) for local datetime validation.
48
- * - 'date' uses z.date() for JavaScript Date objects.
49
- * @default 'string'
50
- * @note 'stringOffset' will become the default in Kubb v3.
51
- */
52
- dateType?: false | 'string' | 'stringOffset' | 'stringLocal' | 'date';
53
- /**
54
- * Choose to use `number` or `bigint` for integer fields with `int64` format.
55
- * - 'number' uses the JavaScript `number` type (matches JSON.parse() runtime behavior).
56
- * - 'bigint' uses the JavaScript `bigint` type (accurate for values exceeding Number.MAX_SAFE_INTEGER).
57
- * @note in v5 of Kubb 'bigint' will become the default to better align with OpenAPI's int64 specification.
58
- * @default 'number'
59
- */
60
- integerType?: 'number' | 'bigint';
61
- /**
62
- * Which type to use when the Swagger/OpenAPI file is not providing more information.
63
- * - 'any' allows any value.
64
- * - 'unknown' requires type narrowing before use.
65
- * - 'void' represents no value.
66
- * @default 'any'
67
- */
68
- unknownType?: 'any' | 'unknown' | 'void';
69
- /**
70
- * Which type to use for empty schema values.
71
- * - 'any' allows any value.
72
- * - 'unknown' requires type narrowing before use.
73
- * - 'void' represents no value.
74
- * @default `unknownType`
75
- */
76
- emptySchemaType?: 'any' | 'unknown' | 'void';
77
- /**
78
- * Use TypeScript(`@kubb/plugin-ts`) to add type annotation.
79
- */
80
- typed?: boolean;
81
- /**
82
- * Return Zod generated schema as type with z.infer<TYPE>
83
- */
84
- inferred?: boolean;
85
- /**
86
- * Use of z.coerce.string() instead of z.string()
87
- * can also be an object to enable coercion for dates, strings, and numbers
88
- */
89
- coercion?: boolean | {
90
- dates?: boolean;
91
- strings?: boolean;
92
- numbers?: boolean;
93
- };
94
- operations?: boolean;
95
- mapper?: Record<string, string>;
96
- transformers?: {
97
- /**
98
- * Customize the names based on the type that is provided by the plugin.
99
- */
100
- name?: (name: ResolveNameParams['name'], type?: ResolveNameParams['type']) => string;
101
- /**
102
- * Receive schema and baseName(propertyName) and return FakerMeta array
103
- * TODO TODO add docs
104
- * @beta
105
- */
106
- schema?: (props: {
107
- schema: SchemaObject | null;
108
- name: string | null;
109
- parentName: string | null;
110
- }, defaultSchemas: Schema[]) => Schema[] | undefined;
111
- };
112
- /**
113
- * Which version of Zod should be used.
114
- * - '3' uses Zod v3.x syntax and features.
115
- * - '4' uses Zod v4.x syntax and features.
116
- * @default '3'
117
- */
118
- version?: '3' | '4';
119
- /**
120
- * Which Zod GUID validator to use for OpenAPI `format: uuid`.
121
- * - 'uuid' uses UUID validation.
122
- * - 'guid' uses GUID validation (Zod v4 only).
123
- * @default 'uuid'
124
- */
125
- guidType?: 'uuid' | 'guid';
126
- /**
127
- * Use Zod Mini's functional API for better tree-shaking support.
128
- * When enabled, generates functional syntax (e.g., `z.optional(z.string())`) instead of chainable methods (e.g., `z.string().optional()`).
129
- * Requires Zod v4 or later. When `mini: true`, `version` is set to '4' and `importPath` will default to 'zod/mini'.
130
- * @default false
131
- */
132
- mini?: boolean;
133
- /**
134
- * Callback function to wrap the output of the generated zod schema
135
- *
136
- * This is useful for edge case scenarios where you might leverage something like `z.object({ ... }).openapi({ example: { some: "complex-example" }})`
137
- * or `extendApi(z.object({ ... }), { example: { some: "complex-example", ...otherOpenApiProperties }})`
138
- * while going from openapi -> zod -> openapi
139
- */
140
- wrapOutput?: (arg: {
141
- output: string;
142
- schema: SchemaObject;
143
- }) => string | undefined;
144
- /**
145
- * Define some generators next to the zod generators
146
- */
147
- generators?: Array<Generator<PluginZod>>;
148
- };
149
- type ResolvedOptions = {
150
- output: Output<Oas>;
151
- group: Options['group'];
152
- override: NonNullable<Options['override']>;
153
- transformers: NonNullable<Options['transformers']>;
154
- dateType: NonNullable<Options['dateType']>;
155
- integerType: NonNullable<Options['integerType']>;
156
- unknownType: NonNullable<Options['unknownType']>;
157
- emptySchemaType: NonNullable<Options['emptySchemaType']>;
158
- typed: NonNullable<Options['typed']>;
159
- inferred: NonNullable<Options['inferred']>;
160
- mapper: NonNullable<Options['mapper']>;
161
- importPath: NonNullable<Options['importPath']>;
162
- coercion: NonNullable<Options['coercion']>;
163
- operations: NonNullable<Options['operations']>;
164
- wrapOutput: Options['wrapOutput'];
165
- version: NonNullable<Options['version']>;
166
- guidType: NonNullable<Options['guidType']>;
167
- mini: NonNullable<Options['mini']>;
168
- };
169
- type PluginZod = PluginFactoryOptions<'plugin-zod', Options, ResolvedOptions, never, ResolvePathOptions>;
170
- //#endregion
171
- export { PluginZod as n, Options as t };
172
- //# sourceMappingURL=types-D0wsPC6Y.d.ts.map
@@ -1,2 +0,0 @@
1
- export { Operations } from './Operations.tsx'
2
- export { Zod } from './Zod.tsx'
@@ -1,2 +0,0 @@
1
- export { operationsGenerator } from './operationsGenerator.tsx'
2
- export { zodGenerator } from './zodGenerator.tsx'
@@ -1,50 +0,0 @@
1
- import { usePluginManager } from '@kubb/core/hooks'
2
- import { createReactGenerator } from '@kubb/plugin-oas/generators'
3
- import { useOas, useOperationManager } from '@kubb/plugin-oas/hooks'
4
- import { getBanner, getFooter } from '@kubb/plugin-oas/utils'
5
- import { File } from '@kubb/react-fabric'
6
- import { Operations } from '../components/Operations.tsx'
7
- import type { PluginZod } from '../types'
8
-
9
- export const operationsGenerator = createReactGenerator<PluginZod>({
10
- name: 'operations',
11
- Operations({ operations, generator, plugin }) {
12
- const {
13
- name: pluginName,
14
- options: { output, importPath },
15
- } = plugin
16
- const pluginManager = usePluginManager()
17
-
18
- const oas = useOas()
19
- const { getFile, groupSchemasByName } = useOperationManager(generator)
20
-
21
- const name = 'operations'
22
- const file = pluginManager.getFile({ name, extname: '.ts', pluginName })
23
-
24
- const transformedOperations = operations.map((operation) => ({ operation, data: groupSchemasByName(operation, { type: 'function' }) }))
25
-
26
- const imports = Object.entries(transformedOperations)
27
- .map(([key, { data, operation }]) => {
28
- const names = [data.request, ...Object.values(data.responses), ...Object.values(data.parameters)].filter(Boolean)
29
-
30
- return <File.Import key={key} name={names} root={file.path} path={getFile(operation).path} />
31
- })
32
- .filter(Boolean)
33
-
34
- const isZodImport = importPath === 'zod' || importPath === 'zod/mini'
35
-
36
- return (
37
- <File
38
- baseName={file.baseName}
39
- path={file.path}
40
- meta={file.meta}
41
- banner={getBanner({ oas, output, config: pluginManager.config })}
42
- footer={getFooter({ oas, output })}
43
- >
44
- <File.Import isTypeOnly name={isZodImport ? 'z' : ['z']} path={importPath} isNameSpace={isZodImport} />
45
- {imports}
46
- <Operations name={name} operations={transformedOperations} />
47
- </File>
48
- )
49
- },
50
- })