@kubb/plugin-zod 3.0.0-alpha.3 → 3.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.
- package/README.md +13 -4
- package/dist/chunk-FLFW72FT.cjs +344 -0
- package/dist/chunk-FLFW72FT.cjs.map +1 -0
- package/dist/chunk-G6GLWNC4.cjs +245 -0
- package/dist/chunk-G6GLWNC4.cjs.map +1 -0
- package/dist/chunk-KMSM5GLX.js +236 -0
- package/dist/chunk-KMSM5GLX.js.map +1 -0
- package/dist/chunk-UONLSRDV.js +337 -0
- package/dist/chunk-UONLSRDV.js.map +1 -0
- package/dist/components.cjs +11 -6
- package/dist/components.cjs.map +1 -1
- package/dist/components.d.cts +20 -19
- package/dist/components.d.ts +20 -19
- package/dist/components.js +2 -10
- package/dist/components.js.map +1 -1
- package/dist/generators.cjs +17 -0
- package/dist/generators.cjs.map +1 -0
- package/dist/generators.d.cts +10 -0
- package/dist/generators.d.ts +10 -0
- package/dist/generators.js +4 -0
- package/dist/generators.js.map +1 -0
- package/dist/index.cjs +12 -4
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +4 -137
- package/dist/index.d.ts +4 -137
- package/dist/index.js +3 -8
- package/dist/index.js.map +1 -1
- package/dist/types-ryu2n1NA.d.cts +95 -0
- package/dist/types-ryu2n1NA.d.ts +95 -0
- package/package.json +19 -21
- package/src/components/Operations.tsx +19 -103
- package/src/components/Zod.tsx +66 -0
- package/src/components/index.ts +1 -2
- package/src/generators/__snapshots__/anyof.ts +3 -0
- package/src/generators/__snapshots__/createPet.ts +15 -0
- package/src/generators/__snapshots__/createPetWithUnknownTypeAny.ts +13 -0
- package/src/generators/__snapshots__/createPetWithUnknownTypeUnknown.ts +15 -0
- package/src/generators/__snapshots__/deletePet.ts +3 -0
- package/src/generators/__snapshots__/discriminator.ts +3 -0
- package/src/generators/__snapshots__/enumBooleanLiteral.ts +3 -0
- package/src/generators/__snapshots__/enumNamesType.ts +3 -0
- package/src/generators/__snapshots__/enumNullable.ts +3 -0
- package/src/generators/__snapshots__/enumSingleLiteral.ts +3 -0
- package/src/generators/__snapshots__/enumVarNamesType.ts +3 -0
- package/src/generators/__snapshots__/example.ts +3 -0
- package/src/generators/__snapshots__/getPets.ts +18 -0
- package/src/generators/__snapshots__/mixedValueTypeConst.ts +6 -0
- package/src/generators/__snapshots__/nullableString.ts +3 -0
- package/src/generators/__snapshots__/nullableStringUuid.ts +3 -0
- package/src/generators/__snapshots__/nullableStringWithAnyOf.ts +3 -0
- package/src/generators/__snapshots__/numberValueConst.ts +6 -0
- package/src/generators/__snapshots__/oneof.ts +3 -0
- package/src/generators/__snapshots__/operations.ts +46 -0
- package/src/generators/__snapshots__/optionalPetInfer.ts +5 -0
- package/src/generators/__snapshots__/optionalPetTyped.ts +3 -0
- package/src/generators/__snapshots__/order.ts +3 -0
- package/src/generators/__snapshots__/orderDateTyeString.ts +10 -0
- package/src/generators/__snapshots__/orderDateTypeFalse.ts +3 -0
- package/src/generators/__snapshots__/orderDateTypeString.ts +3 -0
- package/src/generators/__snapshots__/pet.ts +3 -0
- package/src/generators/__snapshots__/petArray.ts +6 -0
- package/src/generators/__snapshots__/petCoercion.ts +3 -0
- package/src/generators/__snapshots__/petTupleObject.ts +6 -0
- package/src/generators/__snapshots__/petWithMapper.ts +3 -0
- package/src/generators/__snapshots__/pets.ts +3 -0
- package/src/generators/__snapshots__/recursive.ts +3 -0
- package/src/generators/__snapshots__/showPetById.ts +18 -0
- package/src/generators/__snapshots__/stringValueConst.ts +6 -0
- package/src/generators/__snapshots__/uuidSchema.ts +3 -0
- package/src/generators/index.ts +2 -0
- package/src/generators/operationsGenerator.tsx +39 -0
- package/src/generators/zodGenerator.tsx +121 -0
- package/src/parser/index.ts +34 -10
- package/src/plugin.ts +28 -46
- package/src/types.ts +42 -88
- package/dist/Operations-BlQtRP31.d.cts +0 -47
- package/dist/Operations-BlQtRP31.d.ts +0 -47
- package/dist/chunk-7X3NWYUN.cjs +0 -1242
- package/dist/chunk-7X3NWYUN.cjs.map +0 -1
- package/dist/chunk-XCGVHLYD.js +0 -1242
- package/dist/chunk-XCGVHLYD.js.map +0 -1
- package/src/SchemaGenerator.tsx +0 -22
- package/src/components/OperationSchema.tsx +0 -64
- package/src/components/Schema.tsx +0 -162
- package/src/components/__snapshots__/operations.ts +0 -50
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
import { type OperationSchema as OperationSchemaType, SchemaGenerator, createReactGenerator, schemaKeywords } from '@kubb/plugin-oas'
|
|
2
|
+
import { Oas } from '@kubb/plugin-oas/components'
|
|
3
|
+
import { useOas, useOperationManager, useSchemaManager } from '@kubb/plugin-oas/hooks'
|
|
4
|
+
import { pluginTsName } from '@kubb/plugin-ts'
|
|
5
|
+
import { File, useApp } from '@kubb/react'
|
|
6
|
+
import { Zod } from '../components'
|
|
7
|
+
import type { PluginZod } from '../types'
|
|
8
|
+
|
|
9
|
+
export const zodGenerator = createReactGenerator<PluginZod>({
|
|
10
|
+
name: 'zod',
|
|
11
|
+
Operation({ operation, options }) {
|
|
12
|
+
const { coercion, inferred, typed, mapper } = options
|
|
13
|
+
|
|
14
|
+
const { plugin, pluginManager, mode } = useApp<PluginZod>()
|
|
15
|
+
const oas = useOas()
|
|
16
|
+
const { getSchemas, getFile } = useOperationManager()
|
|
17
|
+
const schemaManager = useSchemaManager()
|
|
18
|
+
|
|
19
|
+
const file = getFile(operation)
|
|
20
|
+
const schemas = getSchemas(operation)
|
|
21
|
+
const schemaGenerator = new SchemaGenerator(options, {
|
|
22
|
+
oas,
|
|
23
|
+
plugin,
|
|
24
|
+
pluginManager,
|
|
25
|
+
mode,
|
|
26
|
+
override: options.override,
|
|
27
|
+
})
|
|
28
|
+
|
|
29
|
+
const operationSchemas = [schemas.pathParams, schemas.queryParams, schemas.headerParams, schemas.statusCodes, schemas.request, schemas.response]
|
|
30
|
+
.flat()
|
|
31
|
+
.filter(Boolean)
|
|
32
|
+
|
|
33
|
+
const mapOperationSchema = ({ name, schema, description, keysToOmit, ...options }: OperationSchemaType, i: number) => {
|
|
34
|
+
// hack so Params can be optional when needed
|
|
35
|
+
const required = Array.isArray(schema?.required) ? !!schema.required.length : !!schema?.required
|
|
36
|
+
const optional = !required && !!name.includes('Params')
|
|
37
|
+
const tree = [...schemaGenerator.parse({ schema, name }), optional ? { keyword: schemaKeywords.optional } : undefined].filter(Boolean)
|
|
38
|
+
const imports = schemaManager.getImports(tree)
|
|
39
|
+
|
|
40
|
+
const zod = {
|
|
41
|
+
name: schemaManager.getName(name, { type: 'function' }),
|
|
42
|
+
inferTypeName: schemaManager.getName(name, { type: 'type' }),
|
|
43
|
+
file: schemaManager.getFile(name),
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const type = {
|
|
47
|
+
name: schemaManager.getName(name, { type: 'type', pluginKey: [pluginTsName] }),
|
|
48
|
+
file: schemaManager.getFile(options.operationName || name, { pluginKey: [pluginTsName], tag: options.operation?.getTags()[0]?.name }),
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
return (
|
|
52
|
+
<Oas.Schema key={i} name={name} value={schema} tree={tree}>
|
|
53
|
+
{typed && <File.Import isTypeOnly root={file.path} path={type.file.path} name={[type.name]} />}
|
|
54
|
+
{imports.map((imp, index) => (
|
|
55
|
+
<File.Import key={index} root={file.path} path={imp.path} name={imp.name} />
|
|
56
|
+
))}
|
|
57
|
+
<Zod
|
|
58
|
+
name={zod.name}
|
|
59
|
+
typeName={typed ? type.name : undefined}
|
|
60
|
+
inferTypeName={inferred ? zod.inferTypeName : undefined}
|
|
61
|
+
description={description}
|
|
62
|
+
tree={tree}
|
|
63
|
+
mapper={mapper}
|
|
64
|
+
coercion={coercion}
|
|
65
|
+
keysToOmit={keysToOmit}
|
|
66
|
+
/>
|
|
67
|
+
</Oas.Schema>
|
|
68
|
+
)
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
return (
|
|
72
|
+
<File baseName={file.baseName} path={file.path} meta={file.meta} banner={plugin.options.output?.banner} footer={plugin.options.output?.footer}>
|
|
73
|
+
<File.Import name={['z']} path={plugin.options.importPath} />
|
|
74
|
+
{operationSchemas.map(mapOperationSchema)}
|
|
75
|
+
</File>
|
|
76
|
+
)
|
|
77
|
+
},
|
|
78
|
+
Schema({ schema, options }) {
|
|
79
|
+
const { coercion, inferred, typed, mapper, importPath } = options
|
|
80
|
+
|
|
81
|
+
const { getName, getFile, getImports } = useSchemaManager()
|
|
82
|
+
const {
|
|
83
|
+
plugin: {
|
|
84
|
+
options: { output },
|
|
85
|
+
},
|
|
86
|
+
} = useApp<PluginZod>()
|
|
87
|
+
|
|
88
|
+
const imports = getImports(schema.tree)
|
|
89
|
+
|
|
90
|
+
const zod = {
|
|
91
|
+
name: getName(schema.name, { type: 'function' }),
|
|
92
|
+
inferTypeName: getName(schema.name, { type: 'type' }),
|
|
93
|
+
file: getFile(schema.name),
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
const type = {
|
|
97
|
+
name: getName(schema.name, { type: 'type', pluginKey: [pluginTsName] }),
|
|
98
|
+
file: getFile(schema.name, { pluginKey: [pluginTsName] }),
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
return (
|
|
102
|
+
<File baseName={zod.file.baseName} path={zod.file.path} meta={zod.file.meta} banner={output?.banner} footer={output?.footer}>
|
|
103
|
+
<File.Import name={['z']} path={importPath} />
|
|
104
|
+
{typed && <File.Import isTypeOnly root={zod.file.path} path={type.file.path} name={[type.name]} />}
|
|
105
|
+
{imports.map((imp, index) => (
|
|
106
|
+
<File.Import key={index} root={zod.file.path} path={imp.path} name={imp.name} />
|
|
107
|
+
))}
|
|
108
|
+
|
|
109
|
+
<Zod
|
|
110
|
+
name={zod.name}
|
|
111
|
+
typeName={typed ? type.name : undefined}
|
|
112
|
+
inferTypeName={inferred ? zod.inferTypeName : undefined}
|
|
113
|
+
description={schema.value.description}
|
|
114
|
+
tree={schema.tree}
|
|
115
|
+
mapper={mapper}
|
|
116
|
+
coercion={coercion}
|
|
117
|
+
/>
|
|
118
|
+
</File>
|
|
119
|
+
)
|
|
120
|
+
},
|
|
121
|
+
})
|
package/src/parser/index.ts
CHANGED
|
@@ -3,7 +3,7 @@ import { type SchemaKeywordMapper, isKeyword, schemaKeywords } from '@kubb/plugi
|
|
|
3
3
|
|
|
4
4
|
import type { Schema, SchemaKeywordBase, SchemaMapper } from '@kubb/plugin-oas'
|
|
5
5
|
|
|
6
|
-
|
|
6
|
+
const zodKeywordMapper = {
|
|
7
7
|
any: () => 'z.any()',
|
|
8
8
|
unknown: () => 'z.unknown()',
|
|
9
9
|
number: (coercion?: boolean, min?: number, max?: number) => {
|
|
@@ -12,7 +12,11 @@ export const zodKeywordMapper = {
|
|
|
12
12
|
.join('')
|
|
13
13
|
},
|
|
14
14
|
integer: (coercion?: boolean, min?: number, max?: number) => {
|
|
15
|
-
return [
|
|
15
|
+
return [
|
|
16
|
+
coercion ? 'z.coerce.number().int()' : 'z.number().int()',
|
|
17
|
+
min !== undefined ? `.min(${min})` : undefined,
|
|
18
|
+
max !== undefined ? `.max(${max})` : undefined,
|
|
19
|
+
]
|
|
16
20
|
.filter(Boolean)
|
|
17
21
|
.join('')
|
|
18
22
|
},
|
|
@@ -35,7 +39,7 @@ export const zodKeywordMapper = {
|
|
|
35
39
|
tuple: (items: string[] = []) => `z.tuple([${items?.join(', ')}])`,
|
|
36
40
|
enum: (items: string[] = []) => `z.enum([${items?.join(', ')}])`,
|
|
37
41
|
union: (items: string[] = []) => `z.union([${items?.join(', ')}])`,
|
|
38
|
-
const: (value?: string | number) => `z.literal(${value ?? ''})`,
|
|
42
|
+
const: (value?: string | number | boolean) => `z.literal(${value ?? ''})`,
|
|
39
43
|
/**
|
|
40
44
|
* ISO 8601
|
|
41
45
|
*/
|
|
@@ -98,6 +102,7 @@ export const zodKeywordMapper = {
|
|
|
98
102
|
password: undefined,
|
|
99
103
|
phone: undefined,
|
|
100
104
|
readOnly: undefined,
|
|
105
|
+
writeOnly: undefined,
|
|
101
106
|
ref: (value?: string) => (value ? `z.lazy(() => ${value})` : undefined),
|
|
102
107
|
blob: () => 'z.string()',
|
|
103
108
|
deprecated: undefined,
|
|
@@ -201,6 +206,17 @@ export function parse(parent: Schema | undefined, current: Schema, options: Pars
|
|
|
201
206
|
|
|
202
207
|
if (isKeyword(current, schemaKeywords.enum)) {
|
|
203
208
|
if (current.args.asConst) {
|
|
209
|
+
if (current.args.items.length === 1) {
|
|
210
|
+
return parse(
|
|
211
|
+
current,
|
|
212
|
+
{
|
|
213
|
+
keyword: schemaKeywords.const,
|
|
214
|
+
args: current.args.items[0],
|
|
215
|
+
},
|
|
216
|
+
options,
|
|
217
|
+
)
|
|
218
|
+
}
|
|
219
|
+
|
|
204
220
|
return zodKeywordMapper.union(
|
|
205
221
|
current.args.items
|
|
206
222
|
.map((schema) => {
|
|
@@ -219,6 +235,10 @@ export function parse(parent: Schema | undefined, current: Schema, options: Pars
|
|
|
219
235
|
|
|
220
236
|
return zodKeywordMapper.enum(
|
|
221
237
|
current.args.items.map((schema) => {
|
|
238
|
+
if (schema.format === 'boolean') {
|
|
239
|
+
return transformers.stringify(schema.value)
|
|
240
|
+
}
|
|
241
|
+
|
|
222
242
|
if (schema.format === 'number') {
|
|
223
243
|
return transformers.stringify(schema.value)
|
|
224
244
|
}
|
|
@@ -259,7 +279,7 @@ export function parse(parent: Schema | undefined, current: Schema, options: Pars
|
|
|
259
279
|
? current.args.additionalProperties
|
|
260
280
|
.map((schema) => parse(current, schema, options))
|
|
261
281
|
.filter(Boolean)
|
|
262
|
-
.
|
|
282
|
+
.join('')
|
|
263
283
|
: undefined
|
|
264
284
|
|
|
265
285
|
const text = [
|
|
@@ -272,17 +292,17 @@ export function parse(parent: Schema | undefined, current: Schema, options: Pars
|
|
|
272
292
|
}
|
|
273
293
|
|
|
274
294
|
if (isKeyword(current, schemaKeywords.tuple)) {
|
|
275
|
-
return zodKeywordMapper.tuple(
|
|
276
|
-
sort(current.args.items)
|
|
277
|
-
.map((schema) => parse(current, schema, options))
|
|
278
|
-
.filter(Boolean),
|
|
279
|
-
)
|
|
295
|
+
return zodKeywordMapper.tuple(current.args.items.map((schema) => parse(current, schema, options)).filter(Boolean))
|
|
280
296
|
}
|
|
281
297
|
|
|
282
298
|
if (isKeyword(current, schemaKeywords.const)) {
|
|
283
299
|
if (current.args.format === 'number' && current.args.value !== undefined) {
|
|
284
300
|
return zodKeywordMapper.const(Number.parseInt(current.args.value?.toString()))
|
|
285
301
|
}
|
|
302
|
+
|
|
303
|
+
if (current.args.format === 'boolean' && current.args.value !== undefined) {
|
|
304
|
+
return zodKeywordMapper.const(current.args.value)
|
|
305
|
+
}
|
|
286
306
|
return zodKeywordMapper.const(transformers.stringify(current.args.value))
|
|
287
307
|
}
|
|
288
308
|
|
|
@@ -308,10 +328,14 @@ export function parse(parent: Schema | undefined, current: Schema, options: Pars
|
|
|
308
328
|
return zodKeywordMapper.string(options.coercion)
|
|
309
329
|
}
|
|
310
330
|
|
|
311
|
-
if (isKeyword(current, schemaKeywords.number)
|
|
331
|
+
if (isKeyword(current, schemaKeywords.number)) {
|
|
312
332
|
return zodKeywordMapper.number(options.coercion)
|
|
313
333
|
}
|
|
314
334
|
|
|
335
|
+
if (isKeyword(current, schemaKeywords.integer)) {
|
|
336
|
+
return zodKeywordMapper.integer(options.coercion)
|
|
337
|
+
}
|
|
338
|
+
|
|
315
339
|
if (isKeyword(current, schemaKeywords.min)) {
|
|
316
340
|
return zodKeywordMapper.min(current.args)
|
|
317
341
|
}
|
package/src/plugin.ts
CHANGED
|
@@ -1,23 +1,23 @@
|
|
|
1
1
|
import path from 'node:path'
|
|
2
2
|
|
|
3
|
-
import { FileManager, PluginManager, createPlugin } from '@kubb/core'
|
|
3
|
+
import { FileManager, type Group, PluginManager, createPlugin } from '@kubb/core'
|
|
4
4
|
import { camelCase, pascalCase } from '@kubb/core/transformers'
|
|
5
5
|
import { renderTemplate } from '@kubb/core/utils'
|
|
6
|
-
import { OperationGenerator,
|
|
7
|
-
|
|
6
|
+
import { OperationGenerator, SchemaGenerator, pluginOasName } from '@kubb/plugin-oas'
|
|
7
|
+
|
|
8
8
|
import { pluginTsName } from '@kubb/plugin-ts'
|
|
9
9
|
|
|
10
10
|
import type { Plugin } from '@kubb/core'
|
|
11
11
|
import type { PluginOas as SwaggerPluginOptions } from '@kubb/plugin-oas'
|
|
12
|
-
import {
|
|
12
|
+
import { operationsGenerator } from './generators'
|
|
13
|
+
import { zodGenerator } from './generators/zodGenerator.tsx'
|
|
13
14
|
import type { PluginZod } from './types.ts'
|
|
14
|
-
import { zodParser } from './SchemaGenerator.tsx'
|
|
15
15
|
|
|
16
16
|
export const pluginZodName = 'plugin-zod' satisfies PluginZod['name']
|
|
17
17
|
|
|
18
18
|
export const pluginZod = createPlugin<PluginZod>((options) => {
|
|
19
19
|
const {
|
|
20
|
-
output = { path: 'zod' },
|
|
20
|
+
output = { path: 'zod', barrelType: 'named' },
|
|
21
21
|
group,
|
|
22
22
|
exclude = [],
|
|
23
23
|
include,
|
|
@@ -26,39 +26,42 @@ export const pluginZod = createPlugin<PluginZod>((options) => {
|
|
|
26
26
|
dateType = 'string',
|
|
27
27
|
unknownType = 'any',
|
|
28
28
|
typed = false,
|
|
29
|
-
typedSchema = false,
|
|
30
29
|
mapper = {},
|
|
31
|
-
|
|
30
|
+
operations = false,
|
|
32
31
|
importPath = 'zod',
|
|
33
32
|
coercion = false,
|
|
33
|
+
inferred = false,
|
|
34
|
+
generators = [zodGenerator, operations ? operationsGenerator : undefined].filter(Boolean),
|
|
34
35
|
} = options
|
|
35
|
-
const template = group?.output ? group.output : `${output.path}/{{tag}}Controller`
|
|
36
36
|
|
|
37
37
|
return {
|
|
38
38
|
name: pluginZodName,
|
|
39
39
|
options: {
|
|
40
|
-
|
|
40
|
+
output,
|
|
41
41
|
transformers,
|
|
42
42
|
include,
|
|
43
43
|
exclude,
|
|
44
44
|
override,
|
|
45
45
|
typed,
|
|
46
|
-
typedSchema,
|
|
47
46
|
dateType,
|
|
48
47
|
unknownType,
|
|
49
48
|
mapper,
|
|
50
49
|
importPath,
|
|
51
50
|
coercion,
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
...templates,
|
|
55
|
-
},
|
|
51
|
+
operations,
|
|
52
|
+
inferred,
|
|
56
53
|
},
|
|
57
54
|
pre: [pluginOasName, typed ? pluginTsName : undefined].filter(Boolean),
|
|
58
55
|
resolvePath(baseName, pathMode, options) {
|
|
59
56
|
const root = path.resolve(this.config.root, this.config.output.path)
|
|
60
57
|
const mode = pathMode ?? FileManager.getMode(path.resolve(root, output.path))
|
|
61
58
|
|
|
59
|
+
if (options?.tag && group?.type === 'tag') {
|
|
60
|
+
const groupName: Group['name'] = group?.name ? group.name : (ctx) => `${ctx.group}Controller`
|
|
61
|
+
|
|
62
|
+
return path.resolve(root, output.path, groupName({ group: camelCase(options.tag) }), baseName)
|
|
63
|
+
}
|
|
64
|
+
|
|
62
65
|
if (mode === 'single') {
|
|
63
66
|
/**
|
|
64
67
|
* when output is a file then we will always append to the same file(output file), see fileManager.addOrAppend
|
|
@@ -67,12 +70,6 @@ export const pluginZod = createPlugin<PluginZod>((options) => {
|
|
|
67
70
|
return path.resolve(root, output.path)
|
|
68
71
|
}
|
|
69
72
|
|
|
70
|
-
if (options?.tag && group?.type === 'tag') {
|
|
71
|
-
const tag = camelCase(options.tag)
|
|
72
|
-
|
|
73
|
-
return path.resolve(root, renderTemplate(template, { tag }), baseName)
|
|
74
|
-
}
|
|
75
|
-
|
|
76
73
|
return path.resolve(root, output.path, baseName)
|
|
77
74
|
},
|
|
78
75
|
resolveName(name, type) {
|
|
@@ -109,7 +106,7 @@ export const pluginZod = createPlugin<PluginZod>((options) => {
|
|
|
109
106
|
output: output.path,
|
|
110
107
|
})
|
|
111
108
|
|
|
112
|
-
const schemaFiles = await schemaGenerator.build(
|
|
109
|
+
const schemaFiles = await schemaGenerator.build(...generators)
|
|
113
110
|
await this.addFile(...schemaFiles)
|
|
114
111
|
|
|
115
112
|
const operationGenerator = new OperationGenerator(this.plugin.options, {
|
|
@@ -123,36 +120,21 @@ export const pluginZod = createPlugin<PluginZod>((options) => {
|
|
|
123
120
|
mode,
|
|
124
121
|
})
|
|
125
122
|
|
|
126
|
-
const operationFiles = await operationGenerator.build(
|
|
123
|
+
const operationFiles = await operationGenerator.build(...generators)
|
|
127
124
|
await this.addFile(...operationFiles)
|
|
128
|
-
},
|
|
129
|
-
async buildEnd() {
|
|
130
|
-
if (this.config.output.write === false) {
|
|
131
|
-
return
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
const root = path.resolve(this.config.root, this.config.output.path)
|
|
135
|
-
|
|
136
|
-
if (group?.type === 'tag') {
|
|
137
|
-
const rootFiles = await getGroupedByTagFiles({
|
|
138
|
-
logger: this.logger,
|
|
139
|
-
files: this.fileManager.files,
|
|
140
|
-
plugin: this.plugin,
|
|
141
|
-
template,
|
|
142
|
-
exportAs: group.exportAs || '{{tag}}Schemas',
|
|
143
|
-
root,
|
|
144
|
-
output,
|
|
145
|
-
})
|
|
146
|
-
|
|
147
|
-
await this.addFile(...rootFiles)
|
|
148
|
-
}
|
|
149
125
|
|
|
150
|
-
await this.fileManager.
|
|
126
|
+
const barrelFiles = await this.fileManager.getBarrelFiles({
|
|
127
|
+
type: output.barrelType ?? 'named',
|
|
151
128
|
root,
|
|
152
129
|
output,
|
|
153
|
-
|
|
130
|
+
files: this.fileManager.files,
|
|
131
|
+
meta: {
|
|
132
|
+
pluginKey: this.plugin.key,
|
|
133
|
+
},
|
|
154
134
|
logger: this.logger,
|
|
155
135
|
})
|
|
136
|
+
|
|
137
|
+
await this.addFile(...barrelFiles)
|
|
156
138
|
},
|
|
157
139
|
}
|
|
158
140
|
})
|
package/src/types.ts
CHANGED
|
@@ -1,57 +1,16 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
import type * as KubbFile from '@kubb/fs/types'
|
|
1
|
+
import type { Group, Output, PluginFactoryOptions, ResolveNameParams } from '@kubb/core'
|
|
3
2
|
import type { SchemaObject } from '@kubb/oas'
|
|
4
|
-
import type { Exclude, Include, Override, ResolvePathOptions, Schema } from '@kubb/plugin-oas'
|
|
5
|
-
import type { Operations } from './components/Operations'
|
|
6
|
-
|
|
7
|
-
type Templates = {
|
|
8
|
-
operations?: typeof Operations.templates | false
|
|
9
|
-
}
|
|
3
|
+
import type { Exclude, Generator, Include, Override, ResolvePathOptions, Schema } from '@kubb/plugin-oas'
|
|
10
4
|
|
|
11
5
|
export type Options = {
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
* @default 'zod'
|
|
17
|
-
*/
|
|
18
|
-
path: string
|
|
19
|
-
/**
|
|
20
|
-
* Name to be used for the `export * as {{exportAs}} from './'`
|
|
21
|
-
*/
|
|
22
|
-
exportAs?: string
|
|
23
|
-
/**
|
|
24
|
-
* Add an extension to the generated imports and exports, default it will not use an extension
|
|
25
|
-
*/
|
|
26
|
-
extName?: KubbFile.Extname
|
|
27
|
-
/**
|
|
28
|
-
* Define what needs to exported, here you can also disable the export of barrel files
|
|
29
|
-
* @default `'barrel'`
|
|
30
|
-
*/
|
|
31
|
-
exportType?: 'barrel' | 'barrelNamed' | false
|
|
32
|
-
}
|
|
6
|
+
/**
|
|
7
|
+
* @default 'zod'
|
|
8
|
+
*/
|
|
9
|
+
output?: Output
|
|
33
10
|
/**
|
|
34
11
|
* Group the Zod schemas based on the provided name.
|
|
35
12
|
*/
|
|
36
|
-
group?:
|
|
37
|
-
/**
|
|
38
|
-
* Tag will group based on the operation tag inside the Swagger file
|
|
39
|
-
*/
|
|
40
|
-
type: 'tag'
|
|
41
|
-
/**
|
|
42
|
-
* Relative path to save the grouped Zod schemas.
|
|
43
|
-
*
|
|
44
|
-
* `{{tag}}` will be replaced by the current tagName.
|
|
45
|
-
* @example `${output}/{{tag}}Controller` => `zod/PetController`
|
|
46
|
-
* @default `${output}/{{tag}}Controller`
|
|
47
|
-
*/
|
|
48
|
-
output?: string
|
|
49
|
-
/**
|
|
50
|
-
* Name to be used for the `export * as {{exportAs}} from './`
|
|
51
|
-
* @default `"{{tag}}Schemas"`
|
|
52
|
-
*/
|
|
53
|
-
exportAs?: string
|
|
54
|
-
}
|
|
13
|
+
group?: Group
|
|
55
14
|
/**
|
|
56
15
|
* Array containing exclude parameters to exclude/skip tags/operations/methods/paths.
|
|
57
16
|
*/
|
|
@@ -64,30 +23,15 @@ export type Options = {
|
|
|
64
23
|
* Array containing override parameters to override `options` based on tags/operations/methods/paths.
|
|
65
24
|
*/
|
|
66
25
|
override?: Array<Override<ResolvedOptions>>
|
|
67
|
-
transformers?: {
|
|
68
|
-
/**
|
|
69
|
-
* Customize the names based on the type that is provided by the plugin.
|
|
70
|
-
*/
|
|
71
|
-
name?: (name: ResolveNameParams['name'], type?: ResolveNameParams['type']) => string
|
|
72
|
-
/**
|
|
73
|
-
* Receive schema and baseName(propertName) and return FakerMeta array
|
|
74
|
-
* TODO TODO add docs
|
|
75
|
-
* @beta
|
|
76
|
-
*/
|
|
77
|
-
schema?: (
|
|
78
|
-
props: {
|
|
79
|
-
schema?: SchemaObject
|
|
80
|
-
name?: string
|
|
81
|
-
parentName?: string
|
|
82
|
-
},
|
|
83
|
-
defaultSchemas: Schema[],
|
|
84
|
-
) => Schema[] | undefined
|
|
85
|
-
}
|
|
86
|
-
mapper?: Record<string, string>
|
|
87
26
|
/**
|
|
88
|
-
*
|
|
27
|
+
* Path to Zod
|
|
28
|
+
* It will be used as `import { z } from '${importPath}'`.
|
|
29
|
+
* It allows both relative and absolute path.
|
|
30
|
+
* the path will be applied as is, so relative path should be based on the file being generated.
|
|
31
|
+
* @default 'zod'
|
|
89
32
|
*/
|
|
90
|
-
|
|
33
|
+
importPath?: string
|
|
34
|
+
|
|
91
35
|
/**
|
|
92
36
|
* Choose to use `date` or `datetime` as JavaScript `Date` instead of `string`.
|
|
93
37
|
* False will fallback on a simple z.string() format
|
|
@@ -106,40 +50,50 @@ export type Options = {
|
|
|
106
50
|
/**
|
|
107
51
|
* Return Zod generated schema as type with z.infer<TYPE>
|
|
108
52
|
*/
|
|
109
|
-
|
|
53
|
+
inferred?: boolean
|
|
110
54
|
/**
|
|
111
55
|
* Use of z.coerce.string() instead of z.string()
|
|
112
56
|
*/
|
|
113
57
|
coercion?: boolean
|
|
58
|
+
operations?: boolean
|
|
59
|
+
mapper?: Record<string, string>
|
|
60
|
+
transformers?: {
|
|
61
|
+
/**
|
|
62
|
+
* Customize the names based on the type that is provided by the plugin.
|
|
63
|
+
*/
|
|
64
|
+
name?: (name: ResolveNameParams['name'], type?: ResolveNameParams['type']) => string
|
|
65
|
+
/**
|
|
66
|
+
* Receive schema and baseName(propertName) and return FakerMeta array
|
|
67
|
+
* TODO TODO add docs
|
|
68
|
+
* @beta
|
|
69
|
+
*/
|
|
70
|
+
schema?: (
|
|
71
|
+
props: {
|
|
72
|
+
schema?: SchemaObject
|
|
73
|
+
name?: string
|
|
74
|
+
parentName?: string
|
|
75
|
+
},
|
|
76
|
+
defaultSchemas: Schema[],
|
|
77
|
+
) => Schema[] | undefined
|
|
78
|
+
}
|
|
114
79
|
/**
|
|
115
|
-
*
|
|
116
|
-
* It will be used as `import { z } from '${importPath}'`.
|
|
117
|
-
* It allows both relative and absolute path.
|
|
118
|
-
* the path will be applied as is, so relative path should be based on the file being generated.
|
|
119
|
-
* @default 'zod'
|
|
80
|
+
* Define some generators next to the zod generators
|
|
120
81
|
*/
|
|
121
|
-
|
|
82
|
+
generators?: Array<Generator<PluginZod>>
|
|
122
83
|
}
|
|
123
84
|
|
|
124
85
|
type ResolvedOptions = {
|
|
125
|
-
|
|
86
|
+
output: Output
|
|
87
|
+
override: NonNullable<Options['override']>
|
|
126
88
|
transformers: NonNullable<Options['transformers']>
|
|
127
|
-
exclude: Options['exclude']
|
|
128
|
-
include: Options['include']
|
|
129
|
-
override: Options['override']
|
|
130
89
|
dateType: NonNullable<Options['dateType']>
|
|
131
90
|
unknownType: NonNullable<Options['unknownType']>
|
|
132
91
|
typed: NonNullable<Options['typed']>
|
|
133
|
-
|
|
134
|
-
templates: NonNullable<Templates>
|
|
92
|
+
inferred: NonNullable<Options['inferred']>
|
|
135
93
|
mapper: NonNullable<Options['mapper']>
|
|
136
94
|
importPath: NonNullable<Options['importPath']>
|
|
137
95
|
coercion: NonNullable<Options['coercion']>
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
export type FileMeta = {
|
|
141
|
-
pluginKey?: Plugin['key']
|
|
142
|
-
tag?: string
|
|
96
|
+
operations: NonNullable<Options['coercion']>
|
|
143
97
|
}
|
|
144
98
|
|
|
145
99
|
export type PluginZod = PluginFactoryOptions<'plugin-zod', Options, ResolvedOptions, never, ResolvePathOptions>
|
|
@@ -1,47 +0,0 @@
|
|
|
1
|
-
import { Operation } from '@kubb/oas';
|
|
2
|
-
import { KubbNode } from '@kubb/react';
|
|
3
|
-
import { ComponentType, ComponentProps } from 'react';
|
|
4
|
-
|
|
5
|
-
type TemplateProps = {
|
|
6
|
-
/**
|
|
7
|
-
* Name of the function
|
|
8
|
-
*/
|
|
9
|
-
operationsName: string;
|
|
10
|
-
/**
|
|
11
|
-
* Name of the function
|
|
12
|
-
*/
|
|
13
|
-
pathsName: string;
|
|
14
|
-
operations: Operation[];
|
|
15
|
-
};
|
|
16
|
-
declare function Template({ operationsName, pathsName, operations }: TemplateProps): KubbNode;
|
|
17
|
-
type RootTemplateProps = {
|
|
18
|
-
children?: React.ReactNode;
|
|
19
|
-
};
|
|
20
|
-
declare function RootTemplate({ children }: RootTemplateProps): JSX.Element;
|
|
21
|
-
declare const defaultTemplates: {
|
|
22
|
-
readonly default: typeof Template;
|
|
23
|
-
readonly root: typeof RootTemplate;
|
|
24
|
-
};
|
|
25
|
-
type Templates = Partial<typeof defaultTemplates>;
|
|
26
|
-
type Props = {
|
|
27
|
-
/**
|
|
28
|
-
* This will make it possible to override the default behaviour.
|
|
29
|
-
*/
|
|
30
|
-
Template?: ComponentType<ComponentProps<typeof Template>>;
|
|
31
|
-
};
|
|
32
|
-
declare function Operations({ Template }: Props): KubbNode;
|
|
33
|
-
declare namespace Operations {
|
|
34
|
-
var File: (props: FileProps) => KubbNode;
|
|
35
|
-
var templates: {
|
|
36
|
-
readonly default: typeof Template;
|
|
37
|
-
readonly root: typeof RootTemplate;
|
|
38
|
-
};
|
|
39
|
-
}
|
|
40
|
-
type FileProps = {
|
|
41
|
-
/**
|
|
42
|
-
* This will make it possible to override the default behaviour.
|
|
43
|
-
*/
|
|
44
|
-
templates?: Templates;
|
|
45
|
-
};
|
|
46
|
-
|
|
47
|
-
export { Operations as O };
|
|
@@ -1,47 +0,0 @@
|
|
|
1
|
-
import { Operation } from '@kubb/oas';
|
|
2
|
-
import { KubbNode } from '@kubb/react';
|
|
3
|
-
import { ComponentType, ComponentProps } from 'react';
|
|
4
|
-
|
|
5
|
-
type TemplateProps = {
|
|
6
|
-
/**
|
|
7
|
-
* Name of the function
|
|
8
|
-
*/
|
|
9
|
-
operationsName: string;
|
|
10
|
-
/**
|
|
11
|
-
* Name of the function
|
|
12
|
-
*/
|
|
13
|
-
pathsName: string;
|
|
14
|
-
operations: Operation[];
|
|
15
|
-
};
|
|
16
|
-
declare function Template({ operationsName, pathsName, operations }: TemplateProps): KubbNode;
|
|
17
|
-
type RootTemplateProps = {
|
|
18
|
-
children?: React.ReactNode;
|
|
19
|
-
};
|
|
20
|
-
declare function RootTemplate({ children }: RootTemplateProps): JSX.Element;
|
|
21
|
-
declare const defaultTemplates: {
|
|
22
|
-
readonly default: typeof Template;
|
|
23
|
-
readonly root: typeof RootTemplate;
|
|
24
|
-
};
|
|
25
|
-
type Templates = Partial<typeof defaultTemplates>;
|
|
26
|
-
type Props = {
|
|
27
|
-
/**
|
|
28
|
-
* This will make it possible to override the default behaviour.
|
|
29
|
-
*/
|
|
30
|
-
Template?: ComponentType<ComponentProps<typeof Template>>;
|
|
31
|
-
};
|
|
32
|
-
declare function Operations({ Template }: Props): KubbNode;
|
|
33
|
-
declare namespace Operations {
|
|
34
|
-
var File: (props: FileProps) => KubbNode;
|
|
35
|
-
var templates: {
|
|
36
|
-
readonly default: typeof Template;
|
|
37
|
-
readonly root: typeof RootTemplate;
|
|
38
|
-
};
|
|
39
|
-
}
|
|
40
|
-
type FileProps = {
|
|
41
|
-
/**
|
|
42
|
-
* This will make it possible to override the default behaviour.
|
|
43
|
-
*/
|
|
44
|
-
templates?: Templates;
|
|
45
|
-
};
|
|
46
|
-
|
|
47
|
-
export { Operations as O };
|