@kubb/plugin-zod 5.0.0-alpha.3 → 5.0.0-alpha.30
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/dist/index.cjs +1619 -100
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +418 -4
- package/dist/index.js +1614 -100
- package/dist/index.js.map +1 -1
- package/package.json +6 -34
- package/src/components/Operations.tsx +22 -15
- package/src/components/Zod.tsx +20 -119
- package/src/constants.ts +5 -0
- package/src/generators/zodGenerator.tsx +129 -159
- package/src/generators/zodGeneratorLegacy.tsx +365 -0
- package/src/index.ts +12 -1
- package/src/plugin.ts +102 -148
- package/src/presets.ts +30 -0
- package/src/printers/printerZod.ts +298 -0
- package/src/printers/printerZodMini.ts +273 -0
- package/src/resolvers/resolverZod.ts +61 -0
- package/src/resolvers/resolverZodLegacy.ts +60 -0
- package/src/types.ts +172 -93
- package/src/utils.ts +248 -0
- package/dist/components-B7zUFnAm.cjs +0 -890
- package/dist/components-B7zUFnAm.cjs.map +0 -1
- package/dist/components-eECfXVou.js +0 -842
- package/dist/components-eECfXVou.js.map +0 -1
- package/dist/components.cjs +0 -4
- package/dist/components.d.ts +0 -56
- package/dist/components.js +0 -2
- package/dist/generators-CRKtFRi1.js +0 -290
- package/dist/generators-CRKtFRi1.js.map +0 -1
- package/dist/generators-CzSLRVqQ.cjs +0 -301
- package/dist/generators-CzSLRVqQ.cjs.map +0 -1
- package/dist/generators.cjs +0 -4
- package/dist/generators.d.ts +0 -503
- package/dist/generators.js +0 -2
- package/dist/templates/ToZod.source.cjs +0 -7
- package/dist/templates/ToZod.source.cjs.map +0 -1
- package/dist/templates/ToZod.source.d.ts +0 -7
- package/dist/templates/ToZod.source.js +0 -6
- package/dist/templates/ToZod.source.js.map +0 -1
- package/dist/types-D0wsPC6Y.d.ts +0 -172
- package/src/components/index.ts +0 -2
- package/src/generators/index.ts +0 -2
- package/src/generators/operationsGenerator.tsx +0 -50
- package/src/parser.ts +0 -909
- package/src/templates/ToZod.source.ts +0 -4
- package/templates/ToZod.ts +0 -61
|
@@ -1,201 +1,171 @@
|
|
|
1
|
-
import
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import { createReactGenerator } from '@kubb/plugin-oas/generators'
|
|
5
|
-
import { useOas, useOperationManager, useSchemaManager } from '@kubb/plugin-oas/hooks'
|
|
6
|
-
import { getBanner, getFooter, getImports } from '@kubb/plugin-oas/utils'
|
|
7
|
-
import { pluginTsName } from '@kubb/plugin-ts'
|
|
1
|
+
import { caseParams } from '@kubb/ast'
|
|
2
|
+
import type { SchemaNode } from '@kubb/ast/types'
|
|
3
|
+
import { defineGenerator } from '@kubb/core'
|
|
8
4
|
import { File } from '@kubb/react-fabric'
|
|
9
|
-
import {
|
|
5
|
+
import { Operations } from '../components/Operations.tsx'
|
|
6
|
+
import { Zod } from '../components/Zod.tsx'
|
|
7
|
+
import { ZOD_NAMESPACE_IMPORTS } from '../constants.ts'
|
|
8
|
+
import { printerZod } from '../printers/printerZod.ts'
|
|
9
|
+
import { printerZodMini } from '../printers/printerZodMini.ts'
|
|
10
10
|
import type { PluginZod } from '../types'
|
|
11
|
+
import { buildSchemaNames } from '../utils.ts'
|
|
11
12
|
|
|
12
|
-
export const zodGenerator =
|
|
13
|
+
export const zodGenerator = defineGenerator<PluginZod>({
|
|
13
14
|
name: 'zod',
|
|
14
|
-
|
|
15
|
-
const {
|
|
16
|
-
|
|
17
|
-
options: { coercion: globalCoercion, inferred, typed, mapper, wrapOutput, version, guidType, mini },
|
|
18
|
-
} = plugin
|
|
19
|
-
|
|
20
|
-
const mode = useMode()
|
|
21
|
-
const pluginManager = usePluginManager()
|
|
22
|
-
|
|
23
|
-
const oas = useOas()
|
|
24
|
-
const { getSchemas, getFile, getGroup } = useOperationManager(generator)
|
|
25
|
-
const schemaManager = useSchemaManager()
|
|
26
|
-
|
|
27
|
-
const file = getFile(operation)
|
|
28
|
-
const schemas = getSchemas(operation)
|
|
29
|
-
const schemaGenerator = new SchemaGenerator(options, {
|
|
30
|
-
fabric: generator.context.fabric,
|
|
31
|
-
oas,
|
|
32
|
-
plugin,
|
|
33
|
-
pluginManager,
|
|
34
|
-
events: generator.context.events,
|
|
35
|
-
mode,
|
|
36
|
-
override: options.override,
|
|
37
|
-
})
|
|
15
|
+
schema(node, options) {
|
|
16
|
+
const { adapter, config, resolver, root } = this
|
|
17
|
+
const { output, coercion, guidType, mini, wrapOutput, inferred, importPath, group, printer } = options
|
|
38
18
|
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
const toZodPath = path.resolve(config.root, config.output.path, '.kubb/ToZod.ts')
|
|
19
|
+
if (!node.name) {
|
|
20
|
+
return
|
|
21
|
+
}
|
|
43
22
|
|
|
44
|
-
const
|
|
45
|
-
|
|
46
|
-
let keysToOmit = keysToOmitOriginal
|
|
23
|
+
const mode = this.getMode(output)
|
|
24
|
+
const isZodImport = ZOD_NAMESPACE_IMPORTS.has(importPath as 'zod' | 'zod/mini')
|
|
47
25
|
|
|
48
|
-
|
|
49
|
-
|
|
26
|
+
const imports = adapter.getImports(node, (schemaName) => ({
|
|
27
|
+
name: resolver.resolveSchemaName(schemaName),
|
|
28
|
+
path: resolver.resolveFile({ name: schemaName, extname: '.ts' }, { root, output, group }).path,
|
|
29
|
+
}))
|
|
50
30
|
|
|
51
|
-
|
|
52
|
-
|
|
31
|
+
const meta = {
|
|
32
|
+
name: resolver.resolveSchemaName(node.name),
|
|
33
|
+
file: resolver.resolveFile({ name: node.name, extname: '.ts' }, { root, output, group }),
|
|
34
|
+
} as const
|
|
53
35
|
|
|
54
|
-
|
|
55
|
-
delete schemaObject.properties?.[key]
|
|
56
|
-
}
|
|
36
|
+
const inferTypeName = inferred ? resolver.resolveSchemaTypeName(node.name) : undefined
|
|
57
37
|
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
38
|
+
const schemaPrinter = mini
|
|
39
|
+
? printerZodMini({ guidType, wrapOutput, resolver, schemaName: meta.name, nodes: printer?.nodes })
|
|
40
|
+
: printerZod({ coercion, guidType, wrapOutput, resolver, schemaName: meta.name, nodes: printer?.nodes })
|
|
61
41
|
|
|
62
|
-
|
|
63
|
-
|
|
42
|
+
return (
|
|
43
|
+
<File
|
|
44
|
+
baseName={meta.file.baseName}
|
|
45
|
+
path={meta.file.path}
|
|
46
|
+
meta={meta.file.meta}
|
|
47
|
+
banner={resolver.resolveBanner(adapter.rootNode, { output, config })}
|
|
48
|
+
footer={resolver.resolveFooter(adapter.rootNode, { output, config })}
|
|
49
|
+
>
|
|
50
|
+
<File.Import name={isZodImport ? 'z' : ['z']} path={importPath} isNameSpace={isZodImport} />
|
|
51
|
+
{mode === 'split' && imports.map((imp) => <File.Import key={[node.name, imp.path].join('-')} root={meta.file.path} path={imp.path} name={imp.name} />)}
|
|
64
52
|
|
|
65
|
-
|
|
66
|
-
|
|
53
|
+
<Zod name={meta.name} node={node} printer={schemaPrinter} inferTypeName={inferTypeName} />
|
|
54
|
+
</File>
|
|
55
|
+
)
|
|
56
|
+
},
|
|
57
|
+
operation(node, options) {
|
|
58
|
+
const { adapter, config, resolver, root } = this
|
|
59
|
+
const { output, coercion, guidType, mini, wrapOutput, inferred, importPath, group, paramsCasing, printer } = options
|
|
67
60
|
|
|
68
|
-
|
|
69
|
-
|
|
61
|
+
const mode = this.getMode(output)
|
|
62
|
+
const isZodImport = ZOD_NAMESPACE_IMPORTS.has(importPath as 'zod' | 'zod/mini')
|
|
70
63
|
|
|
71
|
-
|
|
72
|
-
schemaObject.required = Object.entries(schemaObject.properties || {})
|
|
73
|
-
.filter(([_key, value]) => value && Object.hasOwn(value, 'default'))
|
|
74
|
-
.map(([key]) => key)
|
|
75
|
-
}
|
|
64
|
+
const params = caseParams(node.parameters, paramsCasing)
|
|
76
65
|
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
].filter(Boolean)
|
|
81
|
-
const imports = getImports(tree)
|
|
82
|
-
const group = options.operation ? getGroup(options.operation) : undefined
|
|
66
|
+
const meta = {
|
|
67
|
+
file: resolver.resolveFile({ name: node.operationId, extname: '.ts', tag: node.tags[0] ?? 'default', path: node.path }, { root, output, group }),
|
|
68
|
+
} as const
|
|
83
69
|
|
|
84
|
-
|
|
70
|
+
function renderSchemaEntry({ schema, name, keysToOmit }: { schema: SchemaNode | null; name: string; keysToOmit?: Array<string> }) {
|
|
71
|
+
if (!schema) return null
|
|
85
72
|
|
|
86
|
-
const
|
|
87
|
-
name: schemaManager.getName(name, { type: 'function' }),
|
|
88
|
-
inferTypeName: schemaManager.getName(name, { type: 'type' }),
|
|
89
|
-
file: schemaManager.getFile(name),
|
|
90
|
-
}
|
|
73
|
+
const inferTypeName = inferred ? resolver.resolveTypeName(name) : undefined
|
|
91
74
|
|
|
92
|
-
const
|
|
93
|
-
name:
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
}),
|
|
101
|
-
}
|
|
75
|
+
const imports = adapter.getImports(schema, (schemaName) => ({
|
|
76
|
+
name: resolver.resolveSchemaName(schemaName),
|
|
77
|
+
path: resolver.resolveFile({ name: schemaName, extname: '.ts' }, { root, output, group }).path,
|
|
78
|
+
}))
|
|
79
|
+
|
|
80
|
+
const schemaPrinter = mini
|
|
81
|
+
? printerZodMini({ guidType, wrapOutput, resolver, schemaName: name, keysToOmit, nodes: printer?.nodes })
|
|
82
|
+
: printerZod({ coercion, guidType, wrapOutput, resolver, schemaName: name, keysToOmit, nodes: printer?.nodes })
|
|
102
83
|
|
|
103
84
|
return (
|
|
104
85
|
<>
|
|
105
|
-
{
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
))}
|
|
109
|
-
<Zod
|
|
110
|
-
name={zod.name}
|
|
111
|
-
typeName={typed ? type.name : undefined}
|
|
112
|
-
inferTypeName={inferred ? zod.inferTypeName : undefined}
|
|
113
|
-
description={description}
|
|
114
|
-
tree={tree}
|
|
115
|
-
schema={schemaObject}
|
|
116
|
-
mapper={mapper}
|
|
117
|
-
coercion={coercion}
|
|
118
|
-
keysToOmit={keysToOmit}
|
|
119
|
-
wrapOutput={wrapOutput}
|
|
120
|
-
version={plugin.options.version}
|
|
121
|
-
guidType={guidType}
|
|
122
|
-
emptySchemaType={plugin.options.emptySchemaType}
|
|
123
|
-
mini={mini}
|
|
124
|
-
/>
|
|
86
|
+
{mode === 'split' &&
|
|
87
|
+
imports.map((imp) => <File.Import key={[name, imp.path, imp.name].join('-')} root={meta.file.path} path={imp.path} name={imp.name} />)}
|
|
88
|
+
<Zod name={name} node={schema} printer={schemaPrinter} inferTypeName={inferTypeName} />
|
|
125
89
|
</>
|
|
126
90
|
)
|
|
127
91
|
}
|
|
128
92
|
|
|
129
|
-
const
|
|
93
|
+
const paramSchemas = params.map((param) => renderSchemaEntry({ schema: param.schema, name: resolver.resolveParamName(node, param) }))
|
|
94
|
+
|
|
95
|
+
const responseSchemas = node.responses.map((res) =>
|
|
96
|
+
renderSchemaEntry({
|
|
97
|
+
schema: res.schema,
|
|
98
|
+
name: resolver.resolveResponseStatusName(node, res.statusCode),
|
|
99
|
+
keysToOmit: res.keysToOmit,
|
|
100
|
+
}),
|
|
101
|
+
)
|
|
102
|
+
|
|
103
|
+
const requestSchema = node.requestBody?.schema
|
|
104
|
+
? renderSchemaEntry({
|
|
105
|
+
schema: {
|
|
106
|
+
...node.requestBody.schema,
|
|
107
|
+
description: node.requestBody.description ?? node.requestBody.schema.description,
|
|
108
|
+
},
|
|
109
|
+
name: resolver.resolveDataName(node),
|
|
110
|
+
keysToOmit: node.requestBody.keysToOmit,
|
|
111
|
+
})
|
|
112
|
+
: null
|
|
130
113
|
|
|
131
114
|
return (
|
|
132
115
|
<File
|
|
133
|
-
baseName={file.baseName}
|
|
134
|
-
path={file.path}
|
|
135
|
-
meta={file.meta}
|
|
136
|
-
banner={
|
|
137
|
-
footer={
|
|
116
|
+
baseName={meta.file.baseName}
|
|
117
|
+
path={meta.file.path}
|
|
118
|
+
meta={meta.file.meta}
|
|
119
|
+
banner={resolver.resolveBanner(adapter.rootNode, { output, config })}
|
|
120
|
+
footer={resolver.resolveFooter(adapter.rootNode, { output, config })}
|
|
138
121
|
>
|
|
139
|
-
<File.Import name={isZodImport ? 'z' : ['z']} path={
|
|
140
|
-
{
|
|
141
|
-
{
|
|
122
|
+
<File.Import name={isZodImport ? 'z' : ['z']} path={importPath} isNameSpace={isZodImport} />
|
|
123
|
+
{paramSchemas}
|
|
124
|
+
{responseSchemas}
|
|
125
|
+
{requestSchema}
|
|
142
126
|
</File>
|
|
143
127
|
)
|
|
144
128
|
},
|
|
145
|
-
|
|
146
|
-
const {
|
|
147
|
-
const {
|
|
148
|
-
options: { output, emptySchemaType, coercion, inferred, typed, mapper, importPath, wrapOutput, version, guidType, mini },
|
|
149
|
-
} = plugin
|
|
150
|
-
const pluginManager = usePluginManager()
|
|
151
|
-
const oas = useOas()
|
|
152
|
-
|
|
153
|
-
const imports = getImports(schema.tree)
|
|
154
|
-
|
|
155
|
-
const zod = {
|
|
156
|
-
name: getName(schema.name, { type: 'function' }),
|
|
157
|
-
inferTypeName: getName(schema.name, { type: 'type' }),
|
|
158
|
-
file: getFile(schema.name),
|
|
159
|
-
}
|
|
129
|
+
operations(nodes, options) {
|
|
130
|
+
const { adapter, config, resolver, root } = this
|
|
131
|
+
const { output, importPath, group, operations, paramsCasing } = options
|
|
160
132
|
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
file: getFile(schema.name, { pluginName: pluginTsName }),
|
|
133
|
+
if (!operations) {
|
|
134
|
+
return
|
|
164
135
|
}
|
|
136
|
+
const isZodImport = ZOD_NAMESPACE_IMPORTS.has(importPath as 'zod' | 'zod/mini')
|
|
137
|
+
|
|
138
|
+
const meta = {
|
|
139
|
+
file: resolver.resolveFile({ name: 'operations', extname: '.ts' }, { root, output, group }),
|
|
140
|
+
} as const
|
|
165
141
|
|
|
166
|
-
const
|
|
167
|
-
|
|
142
|
+
const transformedOperations = nodes.map((node) => {
|
|
143
|
+
const params = caseParams(node.parameters, paramsCasing)
|
|
144
|
+
|
|
145
|
+
return {
|
|
146
|
+
node,
|
|
147
|
+
data: buildSchemaNames(node, { params, resolver }),
|
|
148
|
+
}
|
|
149
|
+
})
|
|
150
|
+
|
|
151
|
+
const imports = transformedOperations.flatMap(({ node, data }) => {
|
|
152
|
+
const names = [data.request, ...Object.values(data.responses), ...Object.values(data.parameters)].filter(Boolean) as string[]
|
|
153
|
+
const opFile = resolver.resolveFile({ name: node.operationId, extname: '.ts', tag: node.tags[0] ?? 'default', path: node.path }, { root, output, group })
|
|
154
|
+
|
|
155
|
+
return names.map((name) => <File.Import key={[name, opFile.path].join('-')} name={[name]} root={meta.file.path} path={opFile.path} />)
|
|
156
|
+
})
|
|
168
157
|
|
|
169
158
|
return (
|
|
170
159
|
<File
|
|
171
|
-
baseName={
|
|
172
|
-
path={
|
|
173
|
-
meta={
|
|
174
|
-
banner={
|
|
175
|
-
footer={
|
|
160
|
+
baseName={meta.file.baseName}
|
|
161
|
+
path={meta.file.path}
|
|
162
|
+
meta={meta.file.meta}
|
|
163
|
+
banner={resolver.resolveBanner(adapter.rootNode, { output, config })}
|
|
164
|
+
footer={resolver.resolveFooter(adapter.rootNode, { output, config })}
|
|
176
165
|
>
|
|
177
|
-
<File.Import name={isZodImport ? 'z' : ['z']} path={importPath} isNameSpace={isZodImport} />
|
|
178
|
-
{
|
|
179
|
-
|
|
180
|
-
{imports.map((imp) => (
|
|
181
|
-
<File.Import key={[imp.path, imp.name, imp.isTypeOnly].join('-')} root={zod.file.path} path={imp.path} name={imp.name} />
|
|
182
|
-
))}
|
|
183
|
-
|
|
184
|
-
<Zod
|
|
185
|
-
name={zod.name}
|
|
186
|
-
typeName={typed ? type.name : undefined}
|
|
187
|
-
inferTypeName={inferred ? zod.inferTypeName : undefined}
|
|
188
|
-
description={schema.value.description}
|
|
189
|
-
tree={schema.tree}
|
|
190
|
-
schema={schema.value}
|
|
191
|
-
mapper={mapper}
|
|
192
|
-
coercion={coercion}
|
|
193
|
-
wrapOutput={wrapOutput}
|
|
194
|
-
version={version}
|
|
195
|
-
guidType={guidType}
|
|
196
|
-
emptySchemaType={emptySchemaType}
|
|
197
|
-
mini={mini}
|
|
198
|
-
/>
|
|
166
|
+
<File.Import isTypeOnly name={isZodImport ? 'z' : ['z']} path={importPath} isNameSpace={isZodImport} />
|
|
167
|
+
{imports}
|
|
168
|
+
<Operations name="operations" operations={transformedOperations} />
|
|
199
169
|
</File>
|
|
200
170
|
)
|
|
201
171
|
},
|