@kubb/plugin-zod 3.0.0-alpha.9 → 3.0.0-beta.10
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-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 +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-L1fXCZAs.d.cts +100 -0
- package/dist/types-L1fXCZAs.d.ts +100 -0
- package/package.json +19 -21
- package/src/components/Operations.tsx +12 -98
- package/src/components/Zod.tsx +68 -0
- package/src/components/index.ts +1 -2
- 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/parser/index.ts +59 -31
- package/src/plugin.ts +30 -39
- package/src/types.ts +50 -89
- 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
|
@@ -1,60 +0,0 @@
|
|
|
1
|
-
import { SchemaGenerator, schemaKeywords } from '@kubb/plugin-oas'
|
|
2
|
-
import { Oas } from '@kubb/plugin-oas/components'
|
|
3
|
-
import { useOas, useOperation, useOperationManager } from '@kubb/plugin-oas/hooks'
|
|
4
|
-
import { File, useApp } from '@kubb/react'
|
|
5
|
-
|
|
6
|
-
import type { OperationSchema as OperationSchemaType } from '@kubb/plugin-oas'
|
|
7
|
-
import type { ReactNode } from 'react'
|
|
8
|
-
import type { FileMeta, PluginZod } from '../types.ts'
|
|
9
|
-
import { Schema } from './Schema.tsx'
|
|
10
|
-
|
|
11
|
-
type Props = {
|
|
12
|
-
description?: string
|
|
13
|
-
keysToOmit?: string[]
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
export function OperationSchema({ description, keysToOmit }: Props): ReactNode {
|
|
17
|
-
return <Schema keysToOmit={keysToOmit} withTypeAnnotation={false} description={description} />
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
type FileProps = {}
|
|
21
|
-
|
|
22
|
-
OperationSchema.File = function ({}: FileProps): ReactNode {
|
|
23
|
-
const { pluginManager, plugin, mode } = useApp<PluginZod>()
|
|
24
|
-
const { getSchemas, getFile } = useOperationManager()
|
|
25
|
-
const oas = useOas()
|
|
26
|
-
const operation = useOperation()
|
|
27
|
-
|
|
28
|
-
const file = getFile(operation)
|
|
29
|
-
const schemas = getSchemas(operation)
|
|
30
|
-
const generator = new SchemaGenerator(plugin.options, {
|
|
31
|
-
oas,
|
|
32
|
-
plugin,
|
|
33
|
-
pluginManager,
|
|
34
|
-
mode,
|
|
35
|
-
override: plugin.options.override,
|
|
36
|
-
})
|
|
37
|
-
|
|
38
|
-
const items = [schemas.pathParams, schemas.queryParams, schemas.headerParams, schemas.statusCodes, schemas.request, schemas.response].flat().filter(Boolean)
|
|
39
|
-
|
|
40
|
-
const mapItem = ({ name, schema, description, keysToOmit, ...options }: OperationSchemaType, i: number) => {
|
|
41
|
-
// hack so Params can be optional when needed
|
|
42
|
-
const required = Array.isArray(schema?.required) ? !!schema.required.length : !!schema?.required
|
|
43
|
-
const optional = !required && !!name.includes('Params')
|
|
44
|
-
const tree = generator.parse({ schema, name })
|
|
45
|
-
|
|
46
|
-
return (
|
|
47
|
-
<Oas.Schema key={i} name={name} value={schema} tree={[...tree, optional ? { keyword: schemaKeywords.optional } : undefined].filter(Boolean)}>
|
|
48
|
-
{mode === 'split' && <Oas.Schema.Imports isTypeOnly={false} />}
|
|
49
|
-
<OperationSchema description={description} keysToOmit={keysToOmit} />
|
|
50
|
-
</Oas.Schema>
|
|
51
|
-
)
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
return (
|
|
55
|
-
<File<FileMeta> baseName={file.baseName} path={file.path} meta={file.meta}>
|
|
56
|
-
<File.Import name={['z']} path={plugin.options.importPath} />
|
|
57
|
-
{items.map(mapItem)}
|
|
58
|
-
</File>
|
|
59
|
-
)
|
|
60
|
-
}
|
|
@@ -1,166 +0,0 @@
|
|
|
1
|
-
import { Oas } from '@kubb/plugin-oas/components'
|
|
2
|
-
import { Const, File, Type, useApp, useFile } from '@kubb/react'
|
|
3
|
-
import { pluginTsName } from '@kubb/plugin-ts'
|
|
4
|
-
|
|
5
|
-
import transformers from '@kubb/core/transformers'
|
|
6
|
-
import { isKeyword, schemaKeywords } from '@kubb/plugin-oas'
|
|
7
|
-
import { useSchema } from '@kubb/plugin-oas/hooks'
|
|
8
|
-
import type { ReactNode } from 'react'
|
|
9
|
-
import * as parserZod from '../parser/index.ts'
|
|
10
|
-
import { pluginZodName } from '../plugin.ts'
|
|
11
|
-
import type { PluginZod } from '../types.ts'
|
|
12
|
-
|
|
13
|
-
type Props = {
|
|
14
|
-
description?: string
|
|
15
|
-
withTypeAnnotation?: boolean
|
|
16
|
-
keysToOmit?: string[]
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
export function Schema(props: Props): ReactNode {
|
|
20
|
-
const { keysToOmit, withTypeAnnotation, description } = props
|
|
21
|
-
const { tree, name } = useSchema()
|
|
22
|
-
const {
|
|
23
|
-
pluginManager,
|
|
24
|
-
plugin: {
|
|
25
|
-
options: { mapper, typedSchema, coercion },
|
|
26
|
-
},
|
|
27
|
-
} = useApp<PluginZod>()
|
|
28
|
-
|
|
29
|
-
// all checks are also inside this.schema(React)
|
|
30
|
-
const resolvedName = pluginManager.resolveName({
|
|
31
|
-
name,
|
|
32
|
-
pluginKey: [pluginZodName],
|
|
33
|
-
type: 'function',
|
|
34
|
-
})
|
|
35
|
-
const resolvedTypeName = pluginManager.resolveName({
|
|
36
|
-
name,
|
|
37
|
-
pluginKey: [pluginZodName],
|
|
38
|
-
type: 'type',
|
|
39
|
-
})
|
|
40
|
-
|
|
41
|
-
const typeName = pluginManager.resolveName({
|
|
42
|
-
name,
|
|
43
|
-
pluginKey: [pluginTsName],
|
|
44
|
-
type: 'type',
|
|
45
|
-
})
|
|
46
|
-
|
|
47
|
-
if (!tree.length) {
|
|
48
|
-
return (
|
|
49
|
-
<File.Source name={resolvedName} isExportable isIndexable>
|
|
50
|
-
<Const
|
|
51
|
-
name={resolvedName}
|
|
52
|
-
export
|
|
53
|
-
JSDoc={{
|
|
54
|
-
comments: [description ? `@description ${transformers.jsStringEscape(description)}` : undefined].filter(Boolean),
|
|
55
|
-
}}
|
|
56
|
-
>
|
|
57
|
-
undefined
|
|
58
|
-
</Const>
|
|
59
|
-
</File.Source>
|
|
60
|
-
)
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
const hasTuple = tree.some((item) => isKeyword(item, schemaKeywords.tuple))
|
|
64
|
-
|
|
65
|
-
const output = parserZod
|
|
66
|
-
.sort(tree)
|
|
67
|
-
.filter((item) => {
|
|
68
|
-
if (hasTuple && (isKeyword(item, schemaKeywords.min) || isKeyword(item, schemaKeywords.max))) {
|
|
69
|
-
return false
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
return true
|
|
73
|
-
})
|
|
74
|
-
.map((item) => parserZod.parse(undefined, item, { name, typeName, description, mapper, coercion, keysToOmit }))
|
|
75
|
-
.filter(Boolean)
|
|
76
|
-
.join('')
|
|
77
|
-
|
|
78
|
-
const suffix = output.endsWith('.nullable()') ? '.unwrap().and' : '.and'
|
|
79
|
-
|
|
80
|
-
return (
|
|
81
|
-
<>
|
|
82
|
-
<File.Source name={resolvedName} isExportable isIndexable>
|
|
83
|
-
<Const
|
|
84
|
-
export
|
|
85
|
-
name={resolvedName}
|
|
86
|
-
JSDoc={{
|
|
87
|
-
comments: [description ? `@description ${transformers.jsStringEscape(description)}` : undefined].filter(Boolean),
|
|
88
|
-
}}
|
|
89
|
-
>
|
|
90
|
-
{[
|
|
91
|
-
output,
|
|
92
|
-
keysToOmit?.length ? `${suffix}(z.object({ ${keysToOmit.map((key) => `${key}: z.never()`).join(',')} }))` : undefined,
|
|
93
|
-
withTypeAnnotation && typeName ? ` as z.ZodType<${typeName}>` : '',
|
|
94
|
-
]
|
|
95
|
-
.filter(Boolean)
|
|
96
|
-
.join('') || ''}
|
|
97
|
-
</Const>
|
|
98
|
-
</File.Source>
|
|
99
|
-
{typedSchema && (
|
|
100
|
-
<File.Source name={resolvedTypeName} isExportable isIndexable isTypeOnly>
|
|
101
|
-
<Type export name={resolvedTypeName}>
|
|
102
|
-
{`z.infer<typeof ${resolvedName}>`}
|
|
103
|
-
</Type>
|
|
104
|
-
</File.Source>
|
|
105
|
-
)}
|
|
106
|
-
</>
|
|
107
|
-
)
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
type FileProps = {}
|
|
111
|
-
|
|
112
|
-
Schema.File = function ({}: FileProps): ReactNode {
|
|
113
|
-
const {
|
|
114
|
-
pluginManager,
|
|
115
|
-
plugin: {
|
|
116
|
-
options: { typed },
|
|
117
|
-
},
|
|
118
|
-
} = useApp<PluginZod>()
|
|
119
|
-
const { schema } = useSchema()
|
|
120
|
-
|
|
121
|
-
const withTypeAnnotation = !!typed
|
|
122
|
-
|
|
123
|
-
return (
|
|
124
|
-
<Oas.Schema.File output={pluginManager.config.output.path}>
|
|
125
|
-
<Schema.Imports />
|
|
126
|
-
<Schema withTypeAnnotation={withTypeAnnotation} description={schema?.description} />
|
|
127
|
-
</Oas.Schema.File>
|
|
128
|
-
)
|
|
129
|
-
}
|
|
130
|
-
Schema.Imports = (): ReactNode => {
|
|
131
|
-
const {
|
|
132
|
-
pluginManager,
|
|
133
|
-
plugin: {
|
|
134
|
-
options: { typed, importPath },
|
|
135
|
-
},
|
|
136
|
-
} = useApp<PluginZod>()
|
|
137
|
-
const { path: root } = useFile()
|
|
138
|
-
const { name, tree, schema } = useSchema()
|
|
139
|
-
|
|
140
|
-
// used for this.options.typed
|
|
141
|
-
const typeName = pluginManager.resolveName({
|
|
142
|
-
name,
|
|
143
|
-
pluginKey: [pluginTsName],
|
|
144
|
-
type: 'type',
|
|
145
|
-
})
|
|
146
|
-
|
|
147
|
-
const typeFileName = pluginManager.resolveName({
|
|
148
|
-
name: name,
|
|
149
|
-
pluginKey: [pluginTsName],
|
|
150
|
-
type: 'file',
|
|
151
|
-
})
|
|
152
|
-
|
|
153
|
-
const typePath = pluginManager.resolvePath({
|
|
154
|
-
baseName: typeFileName,
|
|
155
|
-
pluginKey: [pluginTsName],
|
|
156
|
-
})
|
|
157
|
-
|
|
158
|
-
const withTypeAnnotation = !!typed
|
|
159
|
-
|
|
160
|
-
return (
|
|
161
|
-
<>
|
|
162
|
-
<File.Import name={['z']} path={importPath} />
|
|
163
|
-
{withTypeAnnotation && typeName && typePath && <File.Import isTypeOnly root={root} path={typePath} name={[typeName]} />}
|
|
164
|
-
</>
|
|
165
|
-
)
|
|
166
|
-
}
|
|
@@ -1,53 +0,0 @@
|
|
|
1
|
-
export const operations = {"get_pets-pet-id": {
|
|
2
|
-
request: undefined,
|
|
3
|
-
parameters: {
|
|
4
|
-
path: undefined,
|
|
5
|
-
query: undefined,
|
|
6
|
-
header: undefined
|
|
7
|
-
},
|
|
8
|
-
responses: {
|
|
9
|
-
200: getPetsPetIdQueryResponse,
|
|
10
|
-
default: getPetsPetIdQueryResponse
|
|
11
|
-
},
|
|
12
|
-
errors: {
|
|
13
|
-
|
|
14
|
-
}
|
|
15
|
-
},
|
|
16
|
-
"listPets": {
|
|
17
|
-
request: undefined,
|
|
18
|
-
parameters: {
|
|
19
|
-
path: undefined,
|
|
20
|
-
query: listPetsQueryParams,
|
|
21
|
-
header: undefined
|
|
22
|
-
},
|
|
23
|
-
responses: {
|
|
24
|
-
200: listPetsQueryResponse,
|
|
25
|
-
default: listPetsQueryResponse
|
|
26
|
-
},
|
|
27
|
-
errors: {
|
|
28
|
-
|
|
29
|
-
}
|
|
30
|
-
},
|
|
31
|
-
"createPets": {
|
|
32
|
-
request: createPetsMutationRequest,
|
|
33
|
-
parameters: {
|
|
34
|
-
path: undefined,
|
|
35
|
-
query: undefined,
|
|
36
|
-
header: undefined
|
|
37
|
-
},
|
|
38
|
-
responses: {
|
|
39
|
-
201: createPetsMutationResponse,
|
|
40
|
-
default: createPetsMutationResponse
|
|
41
|
-
},
|
|
42
|
-
errors: {
|
|
43
|
-
|
|
44
|
-
}
|
|
45
|
-
}} as const
|
|
46
|
-
|
|
47
|
-
export const paths = {"/pets/{pet_id}": {
|
|
48
|
-
get: operations["get_pets-pet-id"]
|
|
49
|
-
},
|
|
50
|
-
"/pets": {
|
|
51
|
-
get: operations["listPets"],
|
|
52
|
-
post: operations["createPets"]
|
|
53
|
-
}} as const
|