@kubb/plugin-zod 3.0.0-alpha.0
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 +44 -0
- package/dist/Operations-BlQtRP31.d.cts +47 -0
- package/dist/Operations-BlQtRP31.d.ts +47 -0
- package/dist/chunk-7IAXQL4T.cjs +1291 -0
- package/dist/chunk-7IAXQL4T.cjs.map +1 -0
- package/dist/chunk-OXCOZC6T.js +1291 -0
- package/dist/chunk-OXCOZC6T.js.map +1 -0
- package/dist/components.cjs +11 -0
- package/dist/components.cjs.map +1 -0
- package/dist/components.d.cts +28 -0
- package/dist/components.d.ts +28 -0
- package/dist/components.js +11 -0
- package/dist/components.js.map +1 -0
- package/dist/index.cjs +16 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +156 -0
- package/dist/index.d.ts +156 -0
- package/dist/index.js +16 -0
- package/dist/index.js.map +1 -0
- package/package.json +97 -0
- package/src/OperationGenerator.tsx +54 -0
- package/src/SchemaGenerator.tsx +31 -0
- package/src/components/OperationSchema.tsx +66 -0
- package/src/components/Operations.tsx +133 -0
- package/src/components/Schema.tsx +171 -0
- package/src/components/__snapshots__/operations.ts +50 -0
- package/src/components/index.ts +3 -0
- package/src/index.ts +15 -0
- package/src/parser/index.ts +345 -0
- package/src/plugin.ts +167 -0
- package/src/types.ts +151 -0
package/src/plugin.ts
ADDED
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
import path from 'node:path'
|
|
2
|
+
|
|
3
|
+
import { FileManager, PluginManager, createPlugin } from '@kubb/core'
|
|
4
|
+
import { camelCase, pascalCase } from '@kubb/core/transformers'
|
|
5
|
+
import { renderTemplate } from '@kubb/core/utils'
|
|
6
|
+
import { pluginOasName } from '@kubb/plugin-oas'
|
|
7
|
+
import { getGroupedByTagFiles } from '@kubb/plugin-oas/utils'
|
|
8
|
+
import { pluginTsName } from '@kubb/plugin-ts'
|
|
9
|
+
|
|
10
|
+
import { OperationGenerator } from './OperationGenerator.tsx'
|
|
11
|
+
import { SchemaGenerator } from './SchemaGenerator.tsx'
|
|
12
|
+
|
|
13
|
+
import type { Plugin } from '@kubb/core'
|
|
14
|
+
import type { PluginOas as SwaggerPluginOptions } from '@kubb/plugin-oas'
|
|
15
|
+
import { Operations } from './components/Operations.tsx'
|
|
16
|
+
import type { PluginZod } from './types.ts'
|
|
17
|
+
|
|
18
|
+
export const pluginZodName = 'plugin-zod' satisfies PluginZod['name']
|
|
19
|
+
|
|
20
|
+
export const pluginZod = createPlugin<PluginZod>((options) => {
|
|
21
|
+
const {
|
|
22
|
+
output = { path: 'zod' },
|
|
23
|
+
group,
|
|
24
|
+
exclude = [],
|
|
25
|
+
include,
|
|
26
|
+
override = [],
|
|
27
|
+
transformers = {},
|
|
28
|
+
dateType = 'string',
|
|
29
|
+
unknownType = 'any',
|
|
30
|
+
typed = false,
|
|
31
|
+
typedSchema = false,
|
|
32
|
+
mapper = {},
|
|
33
|
+
templates,
|
|
34
|
+
importPath = 'zod',
|
|
35
|
+
coercion = false,
|
|
36
|
+
} = options
|
|
37
|
+
const template = group?.output ? group.output : `${output.path}/{{tag}}Controller`
|
|
38
|
+
|
|
39
|
+
return {
|
|
40
|
+
name: pluginZodName,
|
|
41
|
+
options: {
|
|
42
|
+
extName: output.extName,
|
|
43
|
+
transformers,
|
|
44
|
+
include,
|
|
45
|
+
exclude,
|
|
46
|
+
override,
|
|
47
|
+
typed,
|
|
48
|
+
typedSchema,
|
|
49
|
+
dateType,
|
|
50
|
+
unknownType,
|
|
51
|
+
mapper,
|
|
52
|
+
importPath,
|
|
53
|
+
coercion,
|
|
54
|
+
templates: {
|
|
55
|
+
operations: Operations.templates,
|
|
56
|
+
...templates,
|
|
57
|
+
},
|
|
58
|
+
},
|
|
59
|
+
pre: [pluginOasName, typed ? pluginTsName : undefined].filter(Boolean),
|
|
60
|
+
resolvePath(baseName, pathMode, options) {
|
|
61
|
+
const root = path.resolve(this.config.root, this.config.output.path)
|
|
62
|
+
const mode = pathMode ?? FileManager.getMode(path.resolve(root, output.path))
|
|
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
|
+
if (options?.tag && group?.type === 'tag') {
|
|
73
|
+
const tag = camelCase(options.tag)
|
|
74
|
+
|
|
75
|
+
return path.resolve(root, renderTemplate(template, { tag }), baseName)
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
return path.resolve(root, output.path, baseName)
|
|
79
|
+
},
|
|
80
|
+
resolveName(name, type) {
|
|
81
|
+
let resolvedName = camelCase(name, {
|
|
82
|
+
suffix: type ? 'schema' : undefined,
|
|
83
|
+
isFile: type === 'file',
|
|
84
|
+
})
|
|
85
|
+
|
|
86
|
+
if (type === 'type') {
|
|
87
|
+
resolvedName = pascalCase(resolvedName)
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
if (type) {
|
|
91
|
+
return transformers?.name?.(resolvedName, type) || resolvedName
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
return resolvedName
|
|
95
|
+
},
|
|
96
|
+
async writeFile(path, source) {
|
|
97
|
+
if (!path.endsWith('.ts') || !source) {
|
|
98
|
+
return
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
return this.fileManager.write(path, source, { sanity: false })
|
|
102
|
+
},
|
|
103
|
+
async buildStart() {
|
|
104
|
+
const [swaggerPlugin]: [Plugin<SwaggerPluginOptions>] = PluginManager.getDependedPlugins<SwaggerPluginOptions>(this.plugins, [pluginOasName])
|
|
105
|
+
|
|
106
|
+
const oas = await swaggerPlugin.api.getOas()
|
|
107
|
+
const root = path.resolve(this.config.root, this.config.output.path)
|
|
108
|
+
const mode = FileManager.getMode(path.resolve(root, output.path))
|
|
109
|
+
|
|
110
|
+
const schemaGenerator = new SchemaGenerator(this.plugin.options, {
|
|
111
|
+
oas,
|
|
112
|
+
pluginManager: this.pluginManager,
|
|
113
|
+
plugin: this.plugin,
|
|
114
|
+
contentType: swaggerPlugin.api.contentType,
|
|
115
|
+
include: undefined,
|
|
116
|
+
override,
|
|
117
|
+
mode,
|
|
118
|
+
output: output.path,
|
|
119
|
+
})
|
|
120
|
+
|
|
121
|
+
const schemaFiles = await schemaGenerator.build()
|
|
122
|
+
await this.addFile(...schemaFiles)
|
|
123
|
+
|
|
124
|
+
const operationGenerator = new OperationGenerator(this.plugin.options, {
|
|
125
|
+
oas,
|
|
126
|
+
pluginManager: this.pluginManager,
|
|
127
|
+
plugin: this.plugin,
|
|
128
|
+
contentType: swaggerPlugin.api.contentType,
|
|
129
|
+
exclude,
|
|
130
|
+
include,
|
|
131
|
+
override,
|
|
132
|
+
mode,
|
|
133
|
+
})
|
|
134
|
+
|
|
135
|
+
const operationFiles = await operationGenerator.build()
|
|
136
|
+
await this.addFile(...operationFiles)
|
|
137
|
+
},
|
|
138
|
+
async buildEnd() {
|
|
139
|
+
if (this.config.output.write === false) {
|
|
140
|
+
return
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
const root = path.resolve(this.config.root, this.config.output.path)
|
|
144
|
+
|
|
145
|
+
if (group?.type === 'tag') {
|
|
146
|
+
const rootFiles = await getGroupedByTagFiles({
|
|
147
|
+
logger: this.logger,
|
|
148
|
+
files: this.fileManager.files,
|
|
149
|
+
plugin: this.plugin,
|
|
150
|
+
template,
|
|
151
|
+
exportAs: group.exportAs || '{{tag}}Schemas',
|
|
152
|
+
root,
|
|
153
|
+
output,
|
|
154
|
+
})
|
|
155
|
+
|
|
156
|
+
await this.addFile(...rootFiles)
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
await this.fileManager.addIndexes({
|
|
160
|
+
root,
|
|
161
|
+
output,
|
|
162
|
+
meta: { pluginKey: this.plugin.key },
|
|
163
|
+
logger: this.logger,
|
|
164
|
+
})
|
|
165
|
+
},
|
|
166
|
+
}
|
|
167
|
+
})
|
package/src/types.ts
ADDED
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
import type { Plugin, PluginFactoryOptions, ResolveNameParams } from '@kubb/core'
|
|
2
|
+
import type * as KubbFile from '@kubb/fs/types'
|
|
3
|
+
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
|
+
}
|
|
10
|
+
|
|
11
|
+
export type Options = {
|
|
12
|
+
output?: {
|
|
13
|
+
/**
|
|
14
|
+
* Relative path to save the Zod schemas.
|
|
15
|
+
* When output is a file it will save all models inside that file else it will create a file per schema item.
|
|
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
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Group the Zod schemas based on the provided name.
|
|
35
|
+
*/
|
|
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
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Array containing exclude parameters to exclude/skip tags/operations/methods/paths.
|
|
57
|
+
*/
|
|
58
|
+
exclude?: Array<Exclude>
|
|
59
|
+
/**
|
|
60
|
+
* Array containing include parameters to include tags/operations/methods/paths.
|
|
61
|
+
*/
|
|
62
|
+
include?: Array<Include>
|
|
63
|
+
/**
|
|
64
|
+
* Array containing override parameters to override `options` based on tags/operations/methods/paths.
|
|
65
|
+
*/
|
|
66
|
+
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
|
+
/**
|
|
88
|
+
* Make it possible to override one of the templates
|
|
89
|
+
*/
|
|
90
|
+
templates?: Partial<Templates>
|
|
91
|
+
/**
|
|
92
|
+
* Choose to use `date` or `datetime` as JavaScript `Date` instead of `string`.
|
|
93
|
+
* False will fallback on a simple z.string() format
|
|
94
|
+
* @default 'string' 'stringOffset' will become the default in Kubb v3
|
|
95
|
+
*/
|
|
96
|
+
dateType?: false | 'string' | 'stringOffset' | 'stringLocal' | 'date'
|
|
97
|
+
/**
|
|
98
|
+
* Which type to use when the Swagger/OpenAPI file is not providing more information
|
|
99
|
+
* @default 'any'
|
|
100
|
+
*/
|
|
101
|
+
unknownType?: 'any' | 'unknown'
|
|
102
|
+
/**
|
|
103
|
+
* Use TypeScript(`@kubb/plugin-ts`) to add type annotation.
|
|
104
|
+
*/
|
|
105
|
+
typed?: boolean
|
|
106
|
+
/**
|
|
107
|
+
* Return Zod generated schema as type with z.infer<TYPE>
|
|
108
|
+
*/
|
|
109
|
+
typedSchema?: boolean
|
|
110
|
+
/**
|
|
111
|
+
* Use of z.coerce.string() instead of z.string()
|
|
112
|
+
*/
|
|
113
|
+
coercion?: boolean
|
|
114
|
+
/**
|
|
115
|
+
* Path to Zod
|
|
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'
|
|
120
|
+
*/
|
|
121
|
+
importPath?: string
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
type ResolvedOptions = {
|
|
125
|
+
extName: KubbFile.Extname | undefined
|
|
126
|
+
transformers: NonNullable<Options['transformers']>
|
|
127
|
+
exclude: Options['exclude']
|
|
128
|
+
include: Options['include']
|
|
129
|
+
override: Options['override']
|
|
130
|
+
dateType: NonNullable<Options['dateType']>
|
|
131
|
+
unknownType: NonNullable<Options['unknownType']>
|
|
132
|
+
typed: NonNullable<Options['typed']>
|
|
133
|
+
typedSchema: NonNullable<Options['typedSchema']>
|
|
134
|
+
templates: NonNullable<Templates>
|
|
135
|
+
mapper: NonNullable<Options['mapper']>
|
|
136
|
+
importPath: NonNullable<Options['importPath']>
|
|
137
|
+
coercion: NonNullable<Options['coercion']>
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
export type FileMeta = {
|
|
141
|
+
pluginKey?: Plugin['key']
|
|
142
|
+
tag?: string
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
export type PluginZod = PluginFactoryOptions<'plugin-zod', Options, ResolvedOptions, never, ResolvePathOptions>
|
|
146
|
+
|
|
147
|
+
declare module '@kubb/core' {
|
|
148
|
+
export interface _Register {
|
|
149
|
+
['@kubb/plugin-zod']: PluginZod
|
|
150
|
+
}
|
|
151
|
+
}
|