@kubb/plugin-zod 5.0.0-alpha.25 → 5.0.0-alpha.27

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.
package/src/types.ts CHANGED
@@ -13,6 +13,8 @@ import type {
13
13
  Resolver,
14
14
  UserGroup,
15
15
  } from '@kubb/core'
16
+ import type { PrinterZodNodes } from './printers/printerZod.ts'
17
+ import type { PrinterZodMiniNodes } from './printers/printerZodMini.ts'
16
18
 
17
19
  /**
18
20
  * The concrete resolver type for `@kubb/plugin-zod`.
@@ -23,61 +25,73 @@ export type ResolverZod = Resolver &
23
25
  /**
24
26
  * Resolves a camelCase schema function name with a `Schema` suffix.
25
27
  */
26
- resolveName(name: string): string
28
+ resolveSchemaName(this: ResolverZod, name: string): string
27
29
  /**
28
- * Resolves the name for a `z.infer<typeof ...>` type export from an already-resolved function name.
30
+ * Resolves the name for a `z.infer<typeof ...>` type export.
31
+ * Strips the trailing `Schema` suffix (added by `resolveSchemaName`) before PascalCasing.
29
32
  *
30
33
  * @example
31
- * resolver.resolveInferName('petSchema') // → 'PetSchema'
32
- * resolver.resolveInferName('addPet200Schema') // → 'AddPet200Schema'
34
+ * resolver.resolveSchemaTypeName('pet) // → 'PetSchema'
35
+ * resolver.resolveSchemaTypeName('addPet200') // → 'AddPet200Schema'
36
+ * resolver.resolveSchemaTypeName('PetName') // → 'PetNameSchema'
33
37
  */
34
- resolveInferName(name: string): string
38
+ resolveSchemaTypeName(this: ResolverZod, name: string): string
39
+ /**
40
+ * Resolves the name for a `z.infer<typeof ...>` type export.
41
+ * Strips the trailing `Schema` suffix (added by `resolveSchemaName`) before PascalCasing.
42
+ *
43
+ * @example
44
+ * resolver.resolveTypeName('pet') // → 'Pet'
45
+ * resolver.resolveTypeName('addPet200') // → 'AddPet200'
46
+ * resolver.resolveTypeName('PetName') // → 'PetName'
47
+ */
48
+ resolveTypeName(this: ResolverZod, name: string): string
35
49
  /**
36
50
  * Resolves a PascalCase path/file name for the generated output.
37
51
  */
38
- resolvePathName(name: string, type?: 'file' | 'function' | 'type' | 'const'): string
52
+ resolvePathName(this: ResolverZod, name: string, type?: 'file' | 'function' | 'type' | 'const'): string
39
53
  /**
40
54
  * Resolves the name for an operation response by status code.
41
55
  *
42
56
  * @example
43
57
  * resolver.resolveResponseStatusName(node, 200) // → 'listPetsStatus200Schema'
44
58
  */
45
- resolveResponseStatusName(node: OperationNode, statusCode: StatusCode): string
59
+ resolveResponseStatusName(this: ResolverZod, node: OperationNode, statusCode: StatusCode): string
46
60
  /**
47
61
  * Resolves the name for the collection of all operation responses.
48
62
  *
49
63
  * @example
50
64
  * resolver.resolveResponsesName(node) // → 'listPetsResponsesSchema'
51
65
  */
52
- resolveResponsesName(node: OperationNode): string
66
+ resolveResponsesName(this: ResolverZod, node: OperationNode): string
53
67
  /**
54
68
  * Resolves the name for the union of all operation responses.
55
69
  *
56
70
  * @example
57
71
  * resolver.resolveResponseName(node) // → 'listPetsResponseSchema'
58
72
  */
59
- resolveResponseName(node: OperationNode): string
73
+ resolveResponseName(this: ResolverZod, node: OperationNode): string
60
74
  /**
61
75
  * Resolves the name for an operation's grouped path parameters type.
62
76
  *
63
77
  * @example
64
78
  * resolver.resolvePathParamsName(node, param) // → 'deletePetPathPetIdSchema'
65
79
  */
66
- resolvePathParamsName(node: OperationNode, param: ParameterNode): string
80
+ resolvePathParamsName(this: ResolverZod, node: OperationNode, param: ParameterNode): string
67
81
  /**
68
82
  * Resolves the name for an operation's grouped query parameters type.
69
83
  *
70
84
  * @example
71
85
  * resolver.resolveQueryParamsName(node, param) // → 'findPetsByStatusQueryStatusSchema'
72
86
  */
73
- resolveQueryParamsName(node: OperationNode, param: ParameterNode): string
87
+ resolveQueryParamsName(this: ResolverZod, node: OperationNode, param: ParameterNode): string
74
88
  /**
75
89
  * Resolves the name for an operation's grouped header parameters type.
76
90
  *
77
91
  * @example
78
92
  * resolver.resolveHeaderParamsName(node, param) // → 'deletePetHeaderApiKeySchema'
79
93
  */
80
- resolveHeaderParamsName(node: OperationNode, param: ParameterNode): string
94
+ resolveHeaderParamsName(this: ResolverZod, node: OperationNode, param: ParameterNode): string
81
95
  }
82
96
 
83
97
  export type Options = {
@@ -173,13 +187,38 @@ export type Options = {
173
187
  */
174
188
  compatibilityPreset?: CompatibilityPreset
175
189
  /**
176
- * Custom resolver instances for zod-specific name resolution.
190
+ * A single resolver whose methods override the default resolver's naming conventions.
191
+ * When a method returns `null` or `undefined`, the default resolver's result is used instead.
177
192
  */
178
- resolvers?: Array<ResolverZod>
193
+ resolver?: Partial<ResolverZod> & ThisType<ResolverZod>
194
+ /**
195
+ * Override individual printer node handlers to customise rendering of specific schema types.
196
+ *
197
+ * Each key is a `SchemaType` (e.g. `'date'`, `'string'`). The function replaces the
198
+ * built-in handler for that type. Use `this.transform` to recurse into nested schema nodes.
199
+ * When `mini: true`, the overrides apply to the Zod Mini printer.
200
+ *
201
+ * @example Override the `date` node to use `z.string().date()`
202
+ * ```ts
203
+ * pluginZod({
204
+ * printer: {
205
+ * nodes: {
206
+ * date(node) {
207
+ * return 'z.string().date()'
208
+ * },
209
+ * },
210
+ * },
211
+ * })
212
+ * ```
213
+ */
214
+ printer?: {
215
+ nodes?: PrinterZodNodes | PrinterZodMiniNodes
216
+ }
179
217
  /**
180
- * AST visitor transformers applied during code generation.
218
+ * A single AST visitor applied to each SchemaNode/OperationNode before printing.
219
+ * When a visitor method returns `null` or `undefined`, the preset transformer's result is used instead.
181
220
  */
182
- transformers?: Array<Visitor>
221
+ transformer?: Visitor
183
222
  }
184
223
 
185
224
  type ResolvedOptions = {
@@ -195,7 +234,7 @@ type ResolvedOptions = {
195
234
  mini: NonNullable<Options['mini']>
196
235
  wrapOutput: Options['wrapOutput']
197
236
  paramsCasing: Options['paramsCasing']
198
- transformers: Array<Visitor>
237
+ printer: Options['printer']
199
238
  }
200
239
 
201
240
  export type PluginZod = PluginFactoryOptions<'plugin-zod', Options, ResolvedOptions, never, ResolvePathOptions, ResolverZod>
@@ -1,41 +0,0 @@
1
- import type { SchemaNode } from '@kubb/ast/types'
2
- import { Const, File, Type } from '@kubb/react-fabric'
3
- import type { FabricReactNode } from '@kubb/react-fabric/types'
4
- import { printerZodMini } from '../printers/printerZodMini.ts'
5
- import type { PluginZod, ResolverZod } from '../types.ts'
6
-
7
- type Props = {
8
- name: string
9
- node: SchemaNode
10
- guidType: PluginZod['resolvedOptions']['guidType']
11
- wrapOutput: PluginZod['resolvedOptions']['wrapOutput']
12
- inferTypeName?: string
13
- resolver?: ResolverZod
14
- keysToOmit?: Array<string>
15
- }
16
-
17
- export function ZodMini({ name, node, guidType, wrapOutput, inferTypeName, resolver, keysToOmit }: Props): FabricReactNode {
18
- const printer = printerZodMini({ guidType, wrapOutput, resolver, schemaName: name, keysToOmit })
19
- const output = printer.print(node)
20
-
21
- if (!output) {
22
- return
23
- }
24
-
25
- return (
26
- <>
27
- <File.Source name={name} isExportable isIndexable>
28
- <Const export name={name}>
29
- {output}
30
- </Const>
31
- </File.Source>
32
- {inferTypeName && (
33
- <File.Source name={inferTypeName} isExportable isIndexable isTypeOnly>
34
- <Type export name={inferTypeName}>
35
- {`z.infer<typeof ${name}>`}
36
- </Type>
37
- </File.Source>
38
- )}
39
- </>
40
- )
41
- }