@kubb/plugin-zod 0.0.0-canary-20241104172400
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/LICENSE +21 -0
- package/README.md +53 -0
- package/dist/chunk-37OP7TSO.cjs +354 -0
- package/dist/chunk-37OP7TSO.cjs.map +1 -0
- package/dist/chunk-OOAUU32I.js +235 -0
- package/dist/chunk-OOAUU32I.js.map +1 -0
- package/dist/chunk-QEYWJ6VH.cjs +244 -0
- package/dist/chunk-QEYWJ6VH.cjs.map +1 -0
- package/dist/chunk-SSOO3TXR.js +347 -0
- package/dist/chunk-SSOO3TXR.js.map +1 -0
- package/dist/components.cjs +16 -0
- package/dist/components.cjs.map +1 -0
- package/dist/components.d.cts +29 -0
- package/dist/components.d.ts +29 -0
- package/dist/components.js +3 -0
- package/dist/components.js.map +1 -0
- 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 +17 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +9 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.js +4 -0
- package/dist/index.js.map +1 -0
- package/dist/types-L1fXCZAs.d.cts +100 -0
- package/dist/types-L1fXCZAs.d.ts +100 -0
- package/package.json +95 -0
- package/src/components/Operations.tsx +49 -0
- package/src/components/Zod.tsx +68 -0
- package/src/components/index.ts +2 -0
- package/src/generators/__snapshots__/anyof.ts +3 -0
- package/src/generators/__snapshots__/coercion.ts +3 -0
- package/src/generators/__snapshots__/coercionDates.ts +3 -0
- package/src/generators/__snapshots__/coercionNumbers.ts +3 -0
- package/src/generators/__snapshots__/coercionStrings.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 +15 -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 +43 -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 +15 -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/index.ts +2 -0
- package/src/parser/index.ts +381 -0
- package/src/plugin.ts +139 -0
- package/src/types.ts +106 -0
package/src/plugin.ts
ADDED
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
import path from 'node:path'
|
|
2
|
+
|
|
3
|
+
import { FileManager, type Group, PluginManager, createPlugin } from '@kubb/core'
|
|
4
|
+
import { camelCase, pascalCase } from '@kubb/core/transformers'
|
|
5
|
+
import { OperationGenerator, SchemaGenerator, pluginOasName } from '@kubb/plugin-oas'
|
|
6
|
+
|
|
7
|
+
import { pluginTsName } from '@kubb/plugin-ts'
|
|
8
|
+
|
|
9
|
+
import type { Plugin } from '@kubb/core'
|
|
10
|
+
import type { PluginOas as SwaggerPluginOptions } from '@kubb/plugin-oas'
|
|
11
|
+
import { operationsGenerator } from './generators'
|
|
12
|
+
import { zodGenerator } from './generators/zodGenerator.tsx'
|
|
13
|
+
import type { PluginZod } from './types.ts'
|
|
14
|
+
|
|
15
|
+
export const pluginZodName = 'plugin-zod' satisfies PluginZod['name']
|
|
16
|
+
|
|
17
|
+
export const pluginZod = createPlugin<PluginZod>((options) => {
|
|
18
|
+
const {
|
|
19
|
+
output = { path: 'zod', barrelType: 'named' },
|
|
20
|
+
group,
|
|
21
|
+
exclude = [],
|
|
22
|
+
include,
|
|
23
|
+
override = [],
|
|
24
|
+
transformers = {},
|
|
25
|
+
dateType = 'string',
|
|
26
|
+
unknownType = 'any',
|
|
27
|
+
typed = false,
|
|
28
|
+
mapper = {},
|
|
29
|
+
operations = false,
|
|
30
|
+
importPath = 'zod',
|
|
31
|
+
coercion = false,
|
|
32
|
+
inferred = false,
|
|
33
|
+
generators = [zodGenerator, operations ? operationsGenerator : undefined].filter(Boolean),
|
|
34
|
+
} = options
|
|
35
|
+
|
|
36
|
+
return {
|
|
37
|
+
name: pluginZodName,
|
|
38
|
+
options: {
|
|
39
|
+
output,
|
|
40
|
+
transformers,
|
|
41
|
+
include,
|
|
42
|
+
exclude,
|
|
43
|
+
override,
|
|
44
|
+
typed,
|
|
45
|
+
dateType,
|
|
46
|
+
unknownType,
|
|
47
|
+
mapper,
|
|
48
|
+
importPath,
|
|
49
|
+
coercion,
|
|
50
|
+
operations,
|
|
51
|
+
inferred,
|
|
52
|
+
},
|
|
53
|
+
pre: [pluginOasName, typed ? pluginTsName : undefined].filter(Boolean),
|
|
54
|
+
resolvePath(baseName, pathMode, options) {
|
|
55
|
+
const root = path.resolve(this.config.root, this.config.output.path)
|
|
56
|
+
const mode = pathMode ?? FileManager.getMode(path.resolve(root, output.path))
|
|
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
|
+
|
|
64
|
+
if (mode === 'single') {
|
|
65
|
+
/**
|
|
66
|
+
* when output is a file then we will always append to the same file(output file), see fileManager.addOrAppend
|
|
67
|
+
* Other plugins then need to call addOrAppend instead of just add from the fileManager class
|
|
68
|
+
*/
|
|
69
|
+
return path.resolve(root, output.path)
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
return path.resolve(root, output.path, baseName)
|
|
73
|
+
},
|
|
74
|
+
resolveName(name, type) {
|
|
75
|
+
let resolvedName = camelCase(name, {
|
|
76
|
+
suffix: type ? 'schema' : undefined,
|
|
77
|
+
isFile: type === 'file',
|
|
78
|
+
})
|
|
79
|
+
|
|
80
|
+
if (type === 'type') {
|
|
81
|
+
resolvedName = pascalCase(resolvedName)
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
if (type) {
|
|
85
|
+
return transformers?.name?.(resolvedName, type) || resolvedName
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
return resolvedName
|
|
89
|
+
},
|
|
90
|
+
async buildStart() {
|
|
91
|
+
const [swaggerPlugin]: [Plugin<SwaggerPluginOptions>] = PluginManager.getDependedPlugins<SwaggerPluginOptions>(this.plugins, [pluginOasName])
|
|
92
|
+
|
|
93
|
+
const oas = await swaggerPlugin.context.getOas()
|
|
94
|
+
const root = path.resolve(this.config.root, this.config.output.path)
|
|
95
|
+
const mode = FileManager.getMode(path.resolve(root, output.path))
|
|
96
|
+
|
|
97
|
+
const schemaGenerator = new SchemaGenerator(this.plugin.options, {
|
|
98
|
+
oas,
|
|
99
|
+
pluginManager: this.pluginManager,
|
|
100
|
+
plugin: this.plugin,
|
|
101
|
+
contentType: swaggerPlugin.context.contentType,
|
|
102
|
+
include: undefined,
|
|
103
|
+
override,
|
|
104
|
+
mode,
|
|
105
|
+
output: output.path,
|
|
106
|
+
})
|
|
107
|
+
|
|
108
|
+
const schemaFiles = await schemaGenerator.build(...generators)
|
|
109
|
+
await this.addFile(...schemaFiles)
|
|
110
|
+
|
|
111
|
+
const operationGenerator = new OperationGenerator(this.plugin.options, {
|
|
112
|
+
oas,
|
|
113
|
+
pluginManager: this.pluginManager,
|
|
114
|
+
plugin: this.plugin,
|
|
115
|
+
contentType: swaggerPlugin.context.contentType,
|
|
116
|
+
exclude,
|
|
117
|
+
include,
|
|
118
|
+
override,
|
|
119
|
+
mode,
|
|
120
|
+
})
|
|
121
|
+
|
|
122
|
+
const operationFiles = await operationGenerator.build(...generators)
|
|
123
|
+
await this.addFile(...operationFiles)
|
|
124
|
+
|
|
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)
|
|
137
|
+
},
|
|
138
|
+
}
|
|
139
|
+
})
|
package/src/types.ts
ADDED
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
import type { Group, Output, PluginFactoryOptions, ResolveNameParams } from '@kubb/core'
|
|
2
|
+
import type { SchemaObject } from '@kubb/oas'
|
|
3
|
+
import type { Exclude, Generator, Include, Override, ResolvePathOptions, Schema } from '@kubb/plugin-oas'
|
|
4
|
+
|
|
5
|
+
export type Options = {
|
|
6
|
+
/**
|
|
7
|
+
* @default 'zod'
|
|
8
|
+
*/
|
|
9
|
+
output?: Output
|
|
10
|
+
/**
|
|
11
|
+
* Group the Zod schemas based on the provided name.
|
|
12
|
+
*/
|
|
13
|
+
group?: Group
|
|
14
|
+
/**
|
|
15
|
+
* Array containing exclude parameters to exclude/skip tags/operations/methods/paths.
|
|
16
|
+
*/
|
|
17
|
+
exclude?: Array<Exclude>
|
|
18
|
+
/**
|
|
19
|
+
* Array containing include parameters to include tags/operations/methods/paths.
|
|
20
|
+
*/
|
|
21
|
+
include?: Array<Include>
|
|
22
|
+
/**
|
|
23
|
+
* Array containing override parameters to override `options` based on tags/operations/methods/paths.
|
|
24
|
+
*/
|
|
25
|
+
override?: Array<Override<ResolvedOptions>>
|
|
26
|
+
/**
|
|
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'
|
|
32
|
+
*/
|
|
33
|
+
importPath?: string
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Choose to use `date` or `datetime` as JavaScript `Date` instead of `string`.
|
|
37
|
+
* False will fallback on a simple z.string() format
|
|
38
|
+
* @default 'string' 'stringOffset' will become the default in Kubb v3
|
|
39
|
+
*/
|
|
40
|
+
dateType?: false | 'string' | 'stringOffset' | 'stringLocal' | 'date'
|
|
41
|
+
/**
|
|
42
|
+
* Which type to use when the Swagger/OpenAPI file is not providing more information
|
|
43
|
+
* @default 'any'
|
|
44
|
+
*/
|
|
45
|
+
unknownType?: 'any' | 'unknown'
|
|
46
|
+
/**
|
|
47
|
+
* Use TypeScript(`@kubb/plugin-ts`) to add type annotation.
|
|
48
|
+
*/
|
|
49
|
+
typed?: boolean
|
|
50
|
+
/**
|
|
51
|
+
* Return Zod generated schema as type with z.infer<TYPE>
|
|
52
|
+
*/
|
|
53
|
+
inferred?: boolean
|
|
54
|
+
/**
|
|
55
|
+
* Use of z.coerce.string() instead of z.string()
|
|
56
|
+
* can also be an object to enable coercion for dates, strings, and numbers
|
|
57
|
+
*/
|
|
58
|
+
coercion?:
|
|
59
|
+
| boolean
|
|
60
|
+
| {
|
|
61
|
+
dates?: boolean
|
|
62
|
+
strings?: boolean
|
|
63
|
+
numbers?: boolean
|
|
64
|
+
}
|
|
65
|
+
operations?: boolean
|
|
66
|
+
mapper?: Record<string, string>
|
|
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
|
+
/**
|
|
87
|
+
* Define some generators next to the zod generators
|
|
88
|
+
*/
|
|
89
|
+
generators?: Array<Generator<PluginZod>>
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
type ResolvedOptions = {
|
|
93
|
+
output: Output
|
|
94
|
+
override: NonNullable<Options['override']>
|
|
95
|
+
transformers: NonNullable<Options['transformers']>
|
|
96
|
+
dateType: NonNullable<Options['dateType']>
|
|
97
|
+
unknownType: NonNullable<Options['unknownType']>
|
|
98
|
+
typed: NonNullable<Options['typed']>
|
|
99
|
+
inferred: NonNullable<Options['inferred']>
|
|
100
|
+
mapper: NonNullable<Options['mapper']>
|
|
101
|
+
importPath: NonNullable<Options['importPath']>
|
|
102
|
+
coercion: NonNullable<Options['coercion']>
|
|
103
|
+
operations: NonNullable<Options['operations']>
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
export type PluginZod = PluginFactoryOptions<'plugin-zod', Options, ResolvedOptions, never, ResolvePathOptions>
|