@kubb/plugin-zod 3.0.0-alpha.9 → 3.0.0-beta.1
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 +14 -5
- package/dist/chunk-5PR4KKK7.js +235 -0
- package/dist/chunk-5PR4KKK7.js.map +1 -0
- package/dist/chunk-6MMPQJME.cjs +244 -0
- package/dist/chunk-6MMPQJME.cjs.map +1 -0
- package/dist/chunk-FLFW72FT.cjs +344 -0
- package/dist/chunk-FLFW72FT.cjs.map +1 -0
- package/dist/chunk-UONLSRDV.js +337 -0
- package/dist/chunk-UONLSRDV.js.map +1 -0
- package/dist/components.cjs +11 -10
- 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 -14
- 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 -8
- 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 -12
- 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 +12 -98
- 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 +24 -8
- package/src/plugin.ts +30 -39
- package/src/types.ts +42 -88
- package/dist/Operations-BG26e_MW.d.cts +0 -47
- package/dist/Operations-BG26e_MW.d.ts +0 -47
- package/dist/chunk-57AHBVYK.cjs +0 -3502
- package/dist/chunk-57AHBVYK.cjs.map +0 -1
- package/dist/chunk-CUPQVLRZ.js +0 -3502
- package/dist/chunk-CUPQVLRZ.js.map +0 -1
- package/src/SchemaGenerator.tsx +0 -22
- package/src/components/OperationSchema.tsx +0 -60
- package/src/components/Schema.tsx +0 -166
- package/src/components/__snapshots__/operations.ts +0 -53
|
@@ -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) => {
|
|
@@ -39,7 +39,7 @@ export const zodKeywordMapper = {
|
|
|
39
39
|
tuple: (items: string[] = []) => `z.tuple([${items?.join(', ')}])`,
|
|
40
40
|
enum: (items: string[] = []) => `z.enum([${items?.join(', ')}])`,
|
|
41
41
|
union: (items: string[] = []) => `z.union([${items?.join(', ')}])`,
|
|
42
|
-
const: (value?: string | number) => `z.literal(${value ?? ''})`,
|
|
42
|
+
const: (value?: string | number | boolean) => `z.literal(${value ?? ''})`,
|
|
43
43
|
/**
|
|
44
44
|
* ISO 8601
|
|
45
45
|
*/
|
|
@@ -102,6 +102,7 @@ export const zodKeywordMapper = {
|
|
|
102
102
|
password: undefined,
|
|
103
103
|
phone: undefined,
|
|
104
104
|
readOnly: undefined,
|
|
105
|
+
writeOnly: undefined,
|
|
105
106
|
ref: (value?: string) => (value ? `z.lazy(() => ${value})` : undefined),
|
|
106
107
|
blob: () => 'z.string()',
|
|
107
108
|
deprecated: undefined,
|
|
@@ -205,6 +206,17 @@ export function parse(parent: Schema | undefined, current: Schema, options: Pars
|
|
|
205
206
|
|
|
206
207
|
if (isKeyword(current, schemaKeywords.enum)) {
|
|
207
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
|
+
|
|
208
220
|
return zodKeywordMapper.union(
|
|
209
221
|
current.args.items
|
|
210
222
|
.map((schema) => {
|
|
@@ -223,6 +235,10 @@ export function parse(parent: Schema | undefined, current: Schema, options: Pars
|
|
|
223
235
|
|
|
224
236
|
return zodKeywordMapper.enum(
|
|
225
237
|
current.args.items.map((schema) => {
|
|
238
|
+
if (schema.format === 'boolean') {
|
|
239
|
+
return transformers.stringify(schema.value)
|
|
240
|
+
}
|
|
241
|
+
|
|
226
242
|
if (schema.format === 'number') {
|
|
227
243
|
return transformers.stringify(schema.value)
|
|
228
244
|
}
|
|
@@ -263,7 +279,7 @@ export function parse(parent: Schema | undefined, current: Schema, options: Pars
|
|
|
263
279
|
? current.args.additionalProperties
|
|
264
280
|
.map((schema) => parse(current, schema, options))
|
|
265
281
|
.filter(Boolean)
|
|
266
|
-
.
|
|
282
|
+
.join('')
|
|
267
283
|
: undefined
|
|
268
284
|
|
|
269
285
|
const text = [
|
|
@@ -276,17 +292,17 @@ export function parse(parent: Schema | undefined, current: Schema, options: Pars
|
|
|
276
292
|
}
|
|
277
293
|
|
|
278
294
|
if (isKeyword(current, schemaKeywords.tuple)) {
|
|
279
|
-
return zodKeywordMapper.tuple(
|
|
280
|
-
sort(current.args.items)
|
|
281
|
-
.map((schema) => parse(current, schema, options))
|
|
282
|
-
.filter(Boolean),
|
|
283
|
-
)
|
|
295
|
+
return zodKeywordMapper.tuple(current.args.items.map((schema) => parse(current, schema, options)).filter(Boolean))
|
|
284
296
|
}
|
|
285
297
|
|
|
286
298
|
if (isKeyword(current, schemaKeywords.const)) {
|
|
287
299
|
if (current.args.format === 'number' && current.args.value !== undefined) {
|
|
288
300
|
return zodKeywordMapper.const(Number.parseInt(current.args.value?.toString()))
|
|
289
301
|
}
|
|
302
|
+
|
|
303
|
+
if (current.args.format === 'boolean' && current.args.value !== undefined) {
|
|
304
|
+
return zodKeywordMapper.const(current.args.value)
|
|
305
|
+
}
|
|
290
306
|
return zodKeywordMapper.const(transformers.stringify(current.args.value))
|
|
291
307
|
}
|
|
292
308
|
|
package/src/plugin.ts
CHANGED
|
@@ -1,23 +1,22 @@
|
|
|
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
|
-
import { renderTemplate } from '@kubb/core/utils'
|
|
6
5
|
import { OperationGenerator, SchemaGenerator, pluginOasName } from '@kubb/plugin-oas'
|
|
7
6
|
|
|
8
7
|
import { pluginTsName } from '@kubb/plugin-ts'
|
|
9
8
|
|
|
10
9
|
import type { Plugin } from '@kubb/core'
|
|
11
10
|
import type { PluginOas as SwaggerPluginOptions } from '@kubb/plugin-oas'
|
|
12
|
-
import {
|
|
13
|
-
import {
|
|
11
|
+
import { operationsGenerator } from './generators'
|
|
12
|
+
import { zodGenerator } from './generators/zodGenerator.tsx'
|
|
14
13
|
import type { PluginZod } from './types.ts'
|
|
15
14
|
|
|
16
15
|
export const pluginZodName = 'plugin-zod' satisfies PluginZod['name']
|
|
17
16
|
|
|
18
17
|
export const pluginZod = createPlugin<PluginZod>((options) => {
|
|
19
18
|
const {
|
|
20
|
-
output = { path: 'zod' },
|
|
19
|
+
output = { path: 'zod', barrelType: 'named' },
|
|
21
20
|
group,
|
|
22
21
|
exclude = [],
|
|
23
22
|
include,
|
|
@@ -26,43 +25,42 @@ export const pluginZod = createPlugin<PluginZod>((options) => {
|
|
|
26
25
|
dateType = 'string',
|
|
27
26
|
unknownType = 'any',
|
|
28
27
|
typed = false,
|
|
29
|
-
typedSchema = false,
|
|
30
28
|
mapper = {},
|
|
31
|
-
|
|
29
|
+
operations = false,
|
|
32
30
|
importPath = 'zod',
|
|
33
31
|
coercion = false,
|
|
32
|
+
inferred = false,
|
|
33
|
+
generators = [zodGenerator, operations ? operationsGenerator : undefined].filter(Boolean),
|
|
34
34
|
} = options
|
|
35
|
-
const template = group?.output ? group.output : `${output.path}/{{tag}}Controller`
|
|
36
35
|
|
|
37
36
|
return {
|
|
38
37
|
name: pluginZodName,
|
|
39
|
-
output: {
|
|
40
|
-
exportType: 'barrelNamed',
|
|
41
|
-
...output,
|
|
42
|
-
},
|
|
43
38
|
options: {
|
|
44
|
-
|
|
39
|
+
output,
|
|
45
40
|
transformers,
|
|
46
41
|
include,
|
|
47
42
|
exclude,
|
|
48
43
|
override,
|
|
49
44
|
typed,
|
|
50
|
-
typedSchema,
|
|
51
45
|
dateType,
|
|
52
46
|
unknownType,
|
|
53
47
|
mapper,
|
|
54
48
|
importPath,
|
|
55
49
|
coercion,
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
...templates,
|
|
59
|
-
},
|
|
50
|
+
operations,
|
|
51
|
+
inferred,
|
|
60
52
|
},
|
|
61
53
|
pre: [pluginOasName, typed ? pluginTsName : undefined].filter(Boolean),
|
|
62
54
|
resolvePath(baseName, pathMode, options) {
|
|
63
55
|
const root = path.resolve(this.config.root, this.config.output.path)
|
|
64
56
|
const mode = pathMode ?? FileManager.getMode(path.resolve(root, output.path))
|
|
65
57
|
|
|
58
|
+
if (options?.tag && group?.type === 'tag') {
|
|
59
|
+
const groupName: Group['name'] = group?.name ? group.name : (ctx) => `${ctx.group}Controller`
|
|
60
|
+
|
|
61
|
+
return path.resolve(root, output.path, groupName({ group: camelCase(options.tag) }), baseName)
|
|
62
|
+
}
|
|
63
|
+
|
|
66
64
|
if (mode === 'single') {
|
|
67
65
|
/**
|
|
68
66
|
* when output is a file then we will always append to the same file(output file), see fileManager.addOrAppend
|
|
@@ -71,12 +69,6 @@ export const pluginZod = createPlugin<PluginZod>((options) => {
|
|
|
71
69
|
return path.resolve(root, output.path)
|
|
72
70
|
}
|
|
73
71
|
|
|
74
|
-
if (options?.tag && group?.type === 'tag') {
|
|
75
|
-
const tag = camelCase(options.tag)
|
|
76
|
-
|
|
77
|
-
return path.resolve(root, renderTemplate(template, { tag }), baseName)
|
|
78
|
-
}
|
|
79
|
-
|
|
80
72
|
return path.resolve(root, output.path, baseName)
|
|
81
73
|
},
|
|
82
74
|
resolveName(name, type) {
|
|
@@ -113,7 +105,7 @@ export const pluginZod = createPlugin<PluginZod>((options) => {
|
|
|
113
105
|
output: output.path,
|
|
114
106
|
})
|
|
115
107
|
|
|
116
|
-
const schemaFiles = await schemaGenerator.build(
|
|
108
|
+
const schemaFiles = await schemaGenerator.build(...generators)
|
|
117
109
|
await this.addFile(...schemaFiles)
|
|
118
110
|
|
|
119
111
|
const operationGenerator = new OperationGenerator(this.plugin.options, {
|
|
@@ -127,22 +119,21 @@ export const pluginZod = createPlugin<PluginZod>((options) => {
|
|
|
127
119
|
mode,
|
|
128
120
|
})
|
|
129
121
|
|
|
130
|
-
const operationFiles = await operationGenerator.build(
|
|
122
|
+
const operationFiles = await operationGenerator.build(...generators)
|
|
131
123
|
await this.addFile(...operationFiles)
|
|
132
124
|
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
}
|
|
125
|
+
const barrelFiles = await this.fileManager.getBarrelFiles({
|
|
126
|
+
type: output.barrelType ?? 'named',
|
|
127
|
+
root,
|
|
128
|
+
output,
|
|
129
|
+
files: this.fileManager.files,
|
|
130
|
+
meta: {
|
|
131
|
+
pluginKey: this.plugin.key,
|
|
132
|
+
},
|
|
133
|
+
logger: this.logger,
|
|
134
|
+
})
|
|
135
|
+
|
|
136
|
+
await this.addFile(...barrelFiles)
|
|
146
137
|
},
|
|
147
138
|
}
|
|
148
139
|
})
|
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/types';
|
|
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/types';
|
|
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 };
|