@kubb/plugin-zod 5.0.0-alpha.9 → 5.0.0-beta.100

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.
Files changed (41) hide show
  1. package/LICENSE +17 -10
  2. package/README.md +38 -23
  3. package/dist/index.cjs +1964 -133
  4. package/dist/index.cjs.map +1 -1
  5. package/dist/index.d.ts +482 -6
  6. package/dist/index.js +1950 -132
  7. package/dist/index.js.map +1 -1
  8. package/package.json +36 -81
  9. package/dist/components-B7zUFnAm.cjs +0 -890
  10. package/dist/components-B7zUFnAm.cjs.map +0 -1
  11. package/dist/components-eECfXVou.js +0 -842
  12. package/dist/components-eECfXVou.js.map +0 -1
  13. package/dist/components.cjs +0 -4
  14. package/dist/components.d.ts +0 -56
  15. package/dist/components.js +0 -2
  16. package/dist/generators-BjPDdJUz.cjs +0 -301
  17. package/dist/generators-BjPDdJUz.cjs.map +0 -1
  18. package/dist/generators-lTWPS6oN.js +0 -290
  19. package/dist/generators-lTWPS6oN.js.map +0 -1
  20. package/dist/generators.cjs +0 -4
  21. package/dist/generators.d.ts +0 -508
  22. package/dist/generators.js +0 -2
  23. package/dist/templates/ToZod.source.cjs +0 -7
  24. package/dist/templates/ToZod.source.cjs.map +0 -1
  25. package/dist/templates/ToZod.source.d.ts +0 -7
  26. package/dist/templates/ToZod.source.js +0 -6
  27. package/dist/templates/ToZod.source.js.map +0 -1
  28. package/dist/types-CoCoOc2u.d.ts +0 -172
  29. package/src/components/Operations.tsx +0 -70
  30. package/src/components/Zod.tsx +0 -142
  31. package/src/components/index.ts +0 -2
  32. package/src/generators/index.ts +0 -2
  33. package/src/generators/operationsGenerator.tsx +0 -50
  34. package/src/generators/zodGenerator.tsx +0 -202
  35. package/src/index.ts +0 -2
  36. package/src/parser.ts +0 -909
  37. package/src/plugin.ts +0 -183
  38. package/src/templates/ToZod.source.ts +0 -4
  39. package/src/types.ts +0 -172
  40. package/templates/ToZod.ts +0 -61
  41. /package/dist/{chunk--u3MIqq1.js → rolldown-runtime-C0LytTxp.js} +0 -0
package/src/plugin.ts DELETED
@@ -1,183 +0,0 @@
1
- import path from 'node:path'
2
- import { camelCase, pascalCase } from '@internals/utils'
3
- import { createPlugin, type Group, getBarrelFiles, getMode, satisfiesDependency } from '@kubb/core'
4
- import { OperationGenerator, pluginOasName, SchemaGenerator } from '@kubb/plugin-oas'
5
- import { pluginTsName } from '@kubb/plugin-ts'
6
- import { operationsGenerator } from './generators'
7
- import { zodGenerator } from './generators/zodGenerator.tsx'
8
- import { source as toZodSource } from './templates/ToZod.source.ts'
9
- import type { PluginZod } from './types.ts'
10
-
11
- export const pluginZodName = 'plugin-zod' satisfies PluginZod['name']
12
-
13
- export const pluginZod = createPlugin<PluginZod>((options) => {
14
- const {
15
- output = { path: 'zod', barrelType: 'named' },
16
- group,
17
- exclude = [],
18
- include,
19
- override = [],
20
- transformers = {},
21
- dateType = 'string',
22
- unknownType = 'any',
23
- emptySchemaType = unknownType,
24
- integerType = 'number',
25
- typed = false,
26
- mapper = {},
27
- operations = false,
28
- mini = false,
29
- version = mini ? '4' : satisfiesDependency('zod', '>=4') ? '4' : '3',
30
- guidType = 'uuid',
31
- importPath = mini ? 'zod/mini' : version === '4' ? 'zod/v4' : 'zod',
32
- coercion = false,
33
- inferred = false,
34
- generators = [zodGenerator, operations ? operationsGenerator : undefined].filter(Boolean),
35
- wrapOutput = undefined,
36
- contentType,
37
- } = options
38
-
39
- // @deprecated Will be removed in v5 when collisionDetection defaults to true
40
- const usedEnumNames = {}
41
-
42
- return {
43
- name: pluginZodName,
44
- options: {
45
- output,
46
- transformers,
47
- include,
48
- exclude,
49
- override,
50
- typed,
51
- dateType,
52
- unknownType,
53
- emptySchemaType,
54
- integerType,
55
- mapper,
56
- importPath,
57
- coercion,
58
- operations,
59
- inferred,
60
- group,
61
- wrapOutput,
62
- version,
63
- guidType,
64
- mini,
65
- usedEnumNames,
66
- },
67
- pre: [pluginOasName, typed ? pluginTsName : undefined].filter(Boolean),
68
- resolvePath(baseName, pathMode, options) {
69
- const root = path.resolve(this.config.root, this.config.output.path)
70
- const mode = pathMode ?? getMode(path.resolve(root, output.path))
71
-
72
- if (mode === 'single') {
73
- /**
74
- * when output is a file then we will always append to the same file(output file), see fileManager.addOrAppend
75
- * Other plugins then need to call addOrAppend instead of just add from the fileManager class
76
- */
77
- return path.resolve(root, output.path)
78
- }
79
-
80
- if (group && (options?.group?.path || options?.group?.tag)) {
81
- const groupName: Group['name'] = group?.name
82
- ? group.name
83
- : (ctx) => {
84
- if (group?.type === 'path') {
85
- return `${ctx.group.split('/')[1]}`
86
- }
87
- return `${camelCase(ctx.group)}Controller`
88
- }
89
-
90
- return path.resolve(
91
- root,
92
- output.path,
93
- groupName({
94
- group: group.type === 'path' ? options.group.path! : options.group.tag!,
95
- }),
96
- baseName,
97
- )
98
- }
99
-
100
- return path.resolve(root, output.path, baseName)
101
- },
102
- resolveName(name, type) {
103
- let resolvedName = camelCase(name, {
104
- suffix: type ? 'schema' : undefined,
105
- isFile: type === 'file',
106
- })
107
-
108
- if (type === 'type') {
109
- resolvedName = pascalCase(resolvedName)
110
- }
111
-
112
- if (type) {
113
- return transformers?.name?.(resolvedName, type) || resolvedName
114
- }
115
-
116
- return resolvedName
117
- },
118
- async install() {
119
- const root = path.resolve(this.config.root, this.config.output.path)
120
- const mode = getMode(path.resolve(root, output.path))
121
- const oas = await this.getOas()
122
-
123
- if (this.plugin.options.typed && this.plugin.options.version === '3') {
124
- // pre add bundled
125
- await this.addFile({
126
- baseName: 'ToZod.ts',
127
- path: path.resolve(root, '.kubb/ToZod.ts'),
128
- sources: [
129
- {
130
- name: 'ToZod',
131
- value: toZodSource,
132
- },
133
- ],
134
- imports: [],
135
- exports: [],
136
- })
137
- }
138
-
139
- const schemaGenerator = new SchemaGenerator(this.plugin.options, {
140
- fabric: this.fabric,
141
- oas,
142
- driver: this.driver,
143
- events: this.events,
144
- plugin: this.plugin,
145
- contentType,
146
- include: undefined,
147
- override,
148
- mode,
149
- output: output.path,
150
- })
151
-
152
- const schemaFiles = await schemaGenerator.build(...generators)
153
- await this.upsertFile(...schemaFiles)
154
-
155
- const operationGenerator = new OperationGenerator(this.plugin.options, {
156
- fabric: this.fabric,
157
- oas,
158
- driver: this.driver,
159
- events: this.events,
160
- plugin: this.plugin,
161
- contentType,
162
- exclude,
163
- include,
164
- override,
165
- mode,
166
- })
167
-
168
- const operationFiles = await operationGenerator.build(...generators)
169
- await this.upsertFile(...operationFiles)
170
-
171
- const barrelFiles = await getBarrelFiles(this.fabric.files, {
172
- type: output.barrelType ?? 'named',
173
- root,
174
- output,
175
- meta: {
176
- pluginName: this.plugin.name,
177
- },
178
- })
179
-
180
- await this.upsertFile(...barrelFiles)
181
- },
182
- }
183
- })
@@ -1,4 +0,0 @@
1
- // @ts-expect-error - import attributes are handled at build time by importAttributeTextPlugin
2
- import content from '../../templates/ToZod.ts' with { type: 'text' }
3
-
4
- export const source = content as string
package/src/types.ts DELETED
@@ -1,172 +0,0 @@
1
- import type { Group, Output, PluginFactoryOptions, ResolveNameParams } from '@kubb/core'
2
- import type { contentType, Oas, SchemaObject } from '@kubb/oas'
3
- import type { Exclude, Include, Override, ResolvePathOptions, Schema } from '@kubb/plugin-oas'
4
- import type { Generator } from '@kubb/plugin-oas/generators'
5
-
6
- export type Options = {
7
- /**
8
- * @default 'zod'
9
- */
10
- output?: Output<Oas>
11
- /**
12
- * Define which contentType should be used.
13
- * By default, the first JSON valid mediaType is used
14
- */
15
- contentType?: contentType
16
- /**
17
- * Group the Zod schemas based on the provided name.
18
- */
19
- group?: Group
20
- /**
21
- * Array containing exclude parameters to exclude/skip tags/operations/methods/paths.
22
- */
23
- exclude?: Array<Exclude>
24
- /**
25
- * Array containing include parameters to include tags/operations/methods/paths.
26
- */
27
- include?: Array<Include>
28
- /**
29
- * Array containing override parameters to override `options` based on tags/operations/methods/paths.
30
- */
31
- override?: Array<Override<ResolvedOptions>>
32
- /**
33
- * Path to Zod
34
- * It used as `import { z } from '${importPath}'`.
35
- * Accepts relative and absolute paths.
36
- * Path is used as-is; relative paths are based on the generated file location.
37
- * @default 'zod'
38
- */
39
- importPath?: string
40
-
41
- /**
42
- * Choose to use date or datetime as JavaScript Date instead of string.
43
- * - false falls back to a simple z.string() format.
44
- * - 'string' uses z.string().datetime() for datetime validation.
45
- * - 'stringOffset' uses z.string().datetime({ offset: true }) for datetime with timezone offset validation.
46
- * - 'stringLocal' uses z.string().datetime({ local: true }) for local datetime validation.
47
- * - 'date' uses z.date() for JavaScript Date objects.
48
- * @default 'string'
49
- * @note 'stringOffset' will become the default in Kubb v3.
50
- */
51
- dateType?: false | 'string' | 'stringOffset' | 'stringLocal' | 'date'
52
- /**
53
- * Choose to use `number` or `bigint` for integer fields with `int64` format.
54
- * - 'number' uses the JavaScript `number` type (matches JSON.parse() runtime behavior).
55
- * - 'bigint' uses the JavaScript `bigint` type (accurate for values exceeding Number.MAX_SAFE_INTEGER).
56
- * @note in v5 of Kubb 'bigint' will become the default to better align with OpenAPI's int64 specification.
57
- * @default 'number'
58
- */
59
- integerType?: 'number' | 'bigint'
60
- /**
61
- * Which type to use when the Swagger/OpenAPI file is not providing more information.
62
- * - 'any' allows any value.
63
- * - 'unknown' requires type narrowing before use.
64
- * - 'void' represents no value.
65
- * @default 'any'
66
- */
67
- unknownType?: 'any' | 'unknown' | 'void'
68
- /**
69
- * Which type to use for empty schema values.
70
- * - 'any' allows any value.
71
- * - 'unknown' requires type narrowing before use.
72
- * - 'void' represents no value.
73
- * @default `unknownType`
74
- */
75
- emptySchemaType?: 'any' | 'unknown' | 'void'
76
- /**
77
- * Use TypeScript(`@kubb/plugin-ts`) to add type annotation.
78
- */
79
- typed?: boolean
80
- /**
81
- * Return Zod generated schema as type with z.infer<TYPE>
82
- */
83
- inferred?: boolean
84
- /**
85
- * Use of z.coerce.string() instead of z.string()
86
- * can also be an object to enable coercion for dates, strings, and numbers
87
- */
88
- coercion?:
89
- | boolean
90
- | {
91
- dates?: boolean
92
- strings?: boolean
93
- numbers?: boolean
94
- }
95
- operations?: boolean
96
- mapper?: Record<string, string>
97
- transformers?: {
98
- /**
99
- * Customize the names based on the type that is provided by the plugin.
100
- */
101
- name?: (name: ResolveNameParams['name'], type?: ResolveNameParams['type']) => string
102
- /**
103
- * Receive schema and baseName(propertyName) and return FakerMeta array
104
- * TODO TODO add docs
105
- * @beta
106
- */
107
- schema?: (
108
- props: {
109
- schema: SchemaObject | null
110
- name: string | null
111
- parentName: string | null
112
- },
113
- defaultSchemas: Schema[],
114
- ) => Schema[] | undefined
115
- }
116
- /**
117
- * Which version of Zod should be used.
118
- * - '3' uses Zod v3.x syntax and features.
119
- * - '4' uses Zod v4.x syntax and features.
120
- * @default '3'
121
- */
122
- version?: '3' | '4'
123
- /**
124
- * Which Zod GUID validator to use for OpenAPI `format: uuid`.
125
- * - 'uuid' uses UUID validation.
126
- * - 'guid' uses GUID validation (Zod v4 only).
127
- * @default 'uuid'
128
- */
129
- guidType?: 'uuid' | 'guid'
130
- /**
131
- * Use Zod Mini's functional API for better tree-shaking support.
132
- * When enabled, generates functional syntax (e.g., `z.optional(z.string())`) instead of chainable methods (e.g., `z.string().optional()`).
133
- * Requires Zod v4 or later. When `mini: true`, `version` is set to '4' and `importPath` will default to 'zod/mini'.
134
- * @default false
135
- */
136
- mini?: boolean
137
- /**
138
- * Callback function to wrap the output of the generated zod schema
139
- *
140
- * This is useful for edge case scenarios where you might leverage something like `z.object({ ... }).openapi({ example: { some: "complex-example" }})`
141
- * or `extendApi(z.object({ ... }), { example: { some: "complex-example", ...otherOpenApiProperties }})`
142
- * while going from openapi -> zod -> openapi
143
- */
144
- wrapOutput?: (arg: { output: string; schema: SchemaObject }) => string | undefined
145
- /**
146
- * Define some generators next to the zod generators
147
- */
148
- generators?: Array<Generator<PluginZod>>
149
- }
150
-
151
- type ResolvedOptions = {
152
- output: Output<Oas>
153
- group: Options['group']
154
- override: NonNullable<Options['override']>
155
- transformers: NonNullable<Options['transformers']>
156
- dateType: NonNullable<Options['dateType']>
157
- integerType: NonNullable<Options['integerType']>
158
- unknownType: NonNullable<Options['unknownType']>
159
- emptySchemaType: NonNullable<Options['emptySchemaType']>
160
- typed: NonNullable<Options['typed']>
161
- inferred: NonNullable<Options['inferred']>
162
- mapper: NonNullable<Options['mapper']>
163
- importPath: NonNullable<Options['importPath']>
164
- coercion: NonNullable<Options['coercion']>
165
- operations: NonNullable<Options['operations']>
166
- wrapOutput: Options['wrapOutput']
167
- version: NonNullable<Options['version']>
168
- guidType: NonNullable<Options['guidType']>
169
- mini: NonNullable<Options['mini']>
170
- }
171
-
172
- export type PluginZod = PluginFactoryOptions<'plugin-zod', Options, ResolvedOptions, never, ResolvePathOptions>
@@ -1,61 +0,0 @@
1
- /**
2
- * See https://github.com/colinhacks/tozod/blob/master/src/index.ts
3
- * Adapted based on https://github.com/colinhacks/zod/issues/372
4
- */
5
-
6
- import type * as z from 'zod'
7
-
8
- type IsAny<T> = [any extends T ? 'true' : 'false'] extends ['true'] ? true : false
9
- type NonOptional<T> = T extends undefined ? never : T
10
- type NonNullable<T> = T extends null ? never : T
11
- type Equals<X, Y> = [X] extends [Y] ? ([Y] extends [X] ? true : false) : false
12
-
13
- type ZodKey<T> =
14
- IsAny<T> extends true
15
- ? 'any'
16
- : Equals<T, boolean> extends true //[T] extends [booleanUtil.Type]
17
- ? 'boolean'
18
- : [undefined] extends [T]
19
- ? 'optional'
20
- : [null] extends [T]
21
- ? 'nullable'
22
- : T extends any[]
23
- ? 'array'
24
- : Equals<T, string> extends true
25
- ? 'string'
26
- : Equals<T, bigint> extends true //[T] extends [bigintUtil.Type]
27
- ? 'bigint'
28
- : Equals<T, number> extends true //[T] extends [numberUtil.Type]
29
- ? 'number'
30
- : Equals<T, Date> extends true //[T] extends [dateUtil.Type]
31
- ? 'date'
32
- : T extends { [k: string]: any } //[T] extends [structUtil.Type]
33
- ? 'object'
34
- : 'rest'
35
-
36
- export type ToZod<T> = {
37
- any: z.ZodAny
38
- optional: z.ZodOptional<ToZod<NonOptional<T>>>
39
- nullable: z.ZodNullable<ToZod<NonNullable<T>>>
40
- array: T extends Array<infer U> ? z.ZodArray<ToZod<U>> : never
41
- string: z.ZodString
42
- bigint: z.ZodBigInt
43
- number: z.ZodNumber
44
- boolean: z.ZodBoolean
45
- date: z.ZodDate
46
- object: z.ZodObject<
47
- // @ts-expect-error cannot convert without Extract but Extract removes the type
48
- {
49
- [K in keyof T]: T[K]
50
- },
51
- 'passthrough',
52
- unknown,
53
- T
54
- >
55
- rest: z.ZodType<T>
56
- }[ZodKey<T>]
57
-
58
- export type ZodShape<T> = {
59
- // Require all the keys from T
60
- [key in keyof T]-?: ToZod<T[key]>
61
- }