@kubb/plugin-ts 5.0.0-alpha.10 → 5.0.0-alpha.12
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/dist/{components-CRu8IKY3.js → Type-CX1HRooG.js} +377 -365
- package/dist/Type-CX1HRooG.js.map +1 -0
- package/dist/Type-Cat0_htq.cjs +808 -0
- package/dist/Type-Cat0_htq.cjs.map +1 -0
- package/dist/components.cjs +3 -2
- package/dist/components.d.ts +40 -9
- package/dist/components.js +2 -2
- package/dist/generators-CLuCmfUz.js +532 -0
- package/dist/generators-CLuCmfUz.js.map +1 -0
- package/dist/generators-DWBU-MuW.cjs +536 -0
- package/dist/generators-DWBU-MuW.cjs.map +1 -0
- package/dist/generators.cjs +2 -3
- package/dist/generators.d.ts +3 -503
- package/dist/generators.js +2 -2
- package/dist/index.cjs +308 -4
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.ts +26 -21
- package/dist/index.js +305 -2
- package/dist/index.js.map +1 -0
- package/dist/{types-mSXmB8WU.d.ts → types-BA1ZCQ5p.d.ts} +73 -57
- package/package.json +5 -5
- package/src/components/{v2/Enum.tsx → Enum.tsx} +27 -11
- package/src/components/Type.tsx +23 -141
- package/src/components/index.ts +1 -0
- package/src/generators/index.ts +0 -1
- package/src/generators/typeGenerator.tsx +189 -413
- package/src/generators/utils.ts +298 -0
- package/src/index.ts +1 -1
- package/src/plugin.ts +81 -129
- package/src/printer.ts +15 -4
- package/src/resolverTs.ts +109 -1
- package/src/types.ts +68 -52
- package/dist/components-CRu8IKY3.js.map +0 -1
- package/dist/components-DeNDKlzf.cjs +0 -982
- package/dist/components-DeNDKlzf.cjs.map +0 -1
- package/dist/plugin-BZkBwnEA.js +0 -1269
- package/dist/plugin-BZkBwnEA.js.map +0 -1
- package/dist/plugin-Bunz1oGa.cjs +0 -1322
- package/dist/plugin-Bunz1oGa.cjs.map +0 -1
- package/src/components/v2/Type.tsx +0 -59
- package/src/generators/v2/typeGenerator.tsx +0 -167
- package/src/generators/v2/utils.ts +0 -140
- package/src/parser.ts +0 -389
package/src/components/Type.tsx
CHANGED
|
@@ -1,24 +1,22 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { collect } from '@kubb/ast'
|
|
2
|
+
import type { EnumSchemaNode, SchemaNode } from '@kubb/ast/types'
|
|
2
3
|
import { safePrint } from '@kubb/fabric-core/parsers/typescript'
|
|
3
|
-
import type { SchemaObject } from '@kubb/oas'
|
|
4
|
-
import { isKeyword, type Schema, SchemaGenerator, schemaKeywords } from '@kubb/plugin-oas'
|
|
5
4
|
import { File } from '@kubb/react-fabric'
|
|
6
5
|
import type { FabricReactNode } from '@kubb/react-fabric/types'
|
|
7
|
-
import
|
|
8
|
-
import * as factory from '../factory.ts'
|
|
9
|
-
import { parse, typeKeywordMapper } from '../parser.ts'
|
|
6
|
+
import { printerTs } from '../printer.ts'
|
|
10
7
|
import type { PluginTs } from '../types.ts'
|
|
8
|
+
import { Enum, getEnumNames } from './Enum.tsx'
|
|
11
9
|
|
|
12
10
|
type Props = {
|
|
13
11
|
name: string
|
|
14
12
|
typedName: string
|
|
15
|
-
|
|
16
|
-
tree: Array<Schema>
|
|
13
|
+
node: SchemaNode
|
|
17
14
|
optionalType: PluginTs['resolvedOptions']['optionalType']
|
|
18
15
|
arrayType: PluginTs['resolvedOptions']['arrayType']
|
|
19
16
|
enumType: PluginTs['resolvedOptions']['enumType']
|
|
20
17
|
enumKeyCasing: PluginTs['resolvedOptions']['enumKeyCasing']
|
|
21
18
|
syntaxType: PluginTs['resolvedOptions']['syntaxType']
|
|
19
|
+
resolver: PluginTs['resolvedOptions']['resolver']
|
|
22
20
|
description?: string
|
|
23
21
|
keysToOmit?: string[]
|
|
24
22
|
}
|
|
@@ -26,131 +24,34 @@ type Props = {
|
|
|
26
24
|
export function Type({
|
|
27
25
|
name,
|
|
28
26
|
typedName,
|
|
29
|
-
|
|
27
|
+
node,
|
|
30
28
|
keysToOmit,
|
|
31
|
-
schema,
|
|
32
29
|
optionalType,
|
|
33
30
|
arrayType,
|
|
34
31
|
syntaxType,
|
|
35
32
|
enumType,
|
|
36
33
|
enumKeyCasing,
|
|
37
34
|
description,
|
|
35
|
+
resolver,
|
|
38
36
|
}: Props): FabricReactNode {
|
|
39
|
-
const
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
const schemaFromTree = tree.find((item) => item.keyword === schemaKeywords.schema)
|
|
46
|
-
const enumSchemas = SchemaGenerator.deepSearch(tree, schemaKeywords.enum)
|
|
47
|
-
|
|
48
|
-
let type =
|
|
49
|
-
(tree
|
|
50
|
-
.map((current, _index, siblings) =>
|
|
51
|
-
parse(
|
|
52
|
-
{ name, schema, parent: undefined, current, siblings },
|
|
53
|
-
{
|
|
54
|
-
optionalType,
|
|
55
|
-
arrayType,
|
|
56
|
-
enumType,
|
|
57
|
-
},
|
|
58
|
-
),
|
|
59
|
-
)
|
|
60
|
-
.filter(Boolean)
|
|
61
|
-
.at(0) as ts.TypeNode) || typeKeywordMapper.undefined()
|
|
62
|
-
|
|
63
|
-
// Add a "Key" suffix to avoid collisions where necessary
|
|
64
|
-
if (['asConst', 'asPascalConst'].includes(enumType) && enumSchemas.length > 0) {
|
|
65
|
-
const isDirectEnum = schema.type === 'array' && schema.items !== undefined
|
|
66
|
-
const isEnumOnly = 'enum' in schema && schema.enum
|
|
67
|
-
|
|
68
|
-
if (isDirectEnum || isEnumOnly) {
|
|
69
|
-
const enumSchema = enumSchemas[0]!
|
|
70
|
-
const typeNameWithKey = `${enumSchema.args.typeName}Key`
|
|
71
|
-
|
|
72
|
-
type = factory.createTypeReferenceNode(typeNameWithKey)
|
|
73
|
-
|
|
74
|
-
if (schema.type === 'array') {
|
|
75
|
-
if (arrayType === 'generic') {
|
|
76
|
-
type = factory.createTypeReferenceNode(factory.createIdentifier('Array'), [type])
|
|
77
|
-
} else {
|
|
78
|
-
type = factory.createArrayTypeNode(type)
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
if (schemaFromTree && isKeyword(schemaFromTree, schemaKeywords.schema)) {
|
|
85
|
-
const isNullish = tree.some((item) => item.keyword === schemaKeywords.nullish)
|
|
86
|
-
const isNullable = tree.some((item) => item.keyword === schemaKeywords.nullable)
|
|
87
|
-
const isOptional = tree.some((item) => item.keyword === schemaKeywords.optional)
|
|
37
|
+
const resolvedDescription = description || node?.description
|
|
38
|
+
const enumSchemaNodes = collect<EnumSchemaNode>(node, {
|
|
39
|
+
schema(n): EnumSchemaNode | undefined {
|
|
40
|
+
if (n.type === 'enum' && n.name) return n as EnumSchemaNode
|
|
41
|
+
},
|
|
42
|
+
})
|
|
88
43
|
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
nodes: [type, factory.keywordTypeNodes.null],
|
|
92
|
-
}) as ts.TypeNode
|
|
93
|
-
}
|
|
44
|
+
const printer = printerTs({ optionalType, arrayType, enumType, typeName: name, syntaxType, description: resolvedDescription, keysToOmit, resolver })
|
|
45
|
+
const typeNode = printer.print(node)
|
|
94
46
|
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
nodes: [type, factory.keywordTypeNodes.undefined],
|
|
98
|
-
}) as ts.TypeNode
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
if (isOptional && ['undefined', 'questionTokenAndUndefined'].includes(optionalType as string)) {
|
|
102
|
-
type = factory.createUnionDeclaration({
|
|
103
|
-
nodes: [type, factory.keywordTypeNodes.undefined],
|
|
104
|
-
}) as ts.TypeNode
|
|
105
|
-
}
|
|
47
|
+
if (!typeNode) {
|
|
48
|
+
return
|
|
106
49
|
}
|
|
107
50
|
|
|
108
|
-
const
|
|
109
|
-
|
|
110
|
-
typeNodes.push(
|
|
111
|
-
factory.createTypeDeclaration({
|
|
112
|
-
name,
|
|
113
|
-
isExportable: true,
|
|
114
|
-
type: keysToOmit?.length
|
|
115
|
-
? factory.createOmitDeclaration({
|
|
116
|
-
keys: keysToOmit,
|
|
117
|
-
type,
|
|
118
|
-
nonNullable: true,
|
|
119
|
-
})
|
|
120
|
-
: type,
|
|
121
|
-
syntax: useTypeGeneration ? 'type' : 'interface',
|
|
122
|
-
comments: [
|
|
123
|
-
schema.title ? `${jsStringEscape(schema.title)}` : undefined,
|
|
124
|
-
description ? `@description ${jsStringEscape(description)}` : undefined,
|
|
125
|
-
schema.deprecated ? '@deprecated' : undefined,
|
|
126
|
-
schema.minLength ? `@minLength ${schema.minLength}` : undefined,
|
|
127
|
-
schema.maxLength ? `@maxLength ${schema.maxLength}` : undefined,
|
|
128
|
-
schema.pattern ? `@pattern ${schema.pattern}` : undefined,
|
|
129
|
-
schema.default ? `@default ${schema.default}` : undefined,
|
|
130
|
-
schema.example ? `@example ${schema.example}` : undefined,
|
|
131
|
-
],
|
|
132
|
-
}),
|
|
133
|
-
)
|
|
134
|
-
|
|
135
|
-
const enums = [...new Set(enumSchemas)].map((enumSchema) => {
|
|
136
|
-
const name = enumType === 'asPascalConst' ? pascalCase(enumSchema.args.name) : camelCase(enumSchema.args.name)
|
|
137
|
-
const typeName = ['asConst', 'asPascalConst'].includes(enumType) ? `${enumSchema.args.typeName}Key` : enumSchema.args.typeName
|
|
138
|
-
|
|
139
|
-
const [nameNode, typeNode] = factory.createEnumDeclaration({
|
|
140
|
-
name,
|
|
141
|
-
typeName,
|
|
142
|
-
enums: enumSchema.args.items
|
|
143
|
-
.map((item) => (item.value === undefined ? undefined : [trimQuotes(item.name?.toString()), item.value]))
|
|
144
|
-
.filter(Boolean) as unknown as Array<[string, string]>,
|
|
145
|
-
type: enumType,
|
|
146
|
-
enumKeyCasing,
|
|
147
|
-
})
|
|
148
|
-
|
|
51
|
+
const enums = [...new Map(enumSchemaNodes.map((n) => [n.name, n])).values()].map((node) => {
|
|
149
52
|
return {
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
name,
|
|
153
|
-
typeName,
|
|
53
|
+
node,
|
|
54
|
+
...getEnumNames({ node, enumType, resolver }),
|
|
154
55
|
}
|
|
155
56
|
})
|
|
156
57
|
|
|
@@ -160,29 +61,10 @@ export function Type({
|
|
|
160
61
|
|
|
161
62
|
return (
|
|
162
63
|
<>
|
|
163
|
-
{shouldExportEnums &&
|
|
164
|
-
enums.map(({ name, nameNode, typeName, typeNode }) => (
|
|
165
|
-
<>
|
|
166
|
-
{nameNode && (
|
|
167
|
-
<File.Source name={name} isExportable isIndexable isTypeOnly={false}>
|
|
168
|
-
{safePrint(nameNode)}
|
|
169
|
-
</File.Source>
|
|
170
|
-
)}
|
|
171
|
-
{
|
|
172
|
-
<File.Source
|
|
173
|
-
name={typeName}
|
|
174
|
-
isIndexable
|
|
175
|
-
isExportable={['enum', 'asConst', 'asPascalConst', 'constEnum', 'literal', undefined].includes(enumType)}
|
|
176
|
-
isTypeOnly={['asConst', 'asPascalConst', 'literal', undefined].includes(enumType)}
|
|
177
|
-
>
|
|
178
|
-
{safePrint(typeNode)}
|
|
179
|
-
</File.Source>
|
|
180
|
-
}
|
|
181
|
-
</>
|
|
182
|
-
))}
|
|
64
|
+
{shouldExportEnums && enums.map(({ node }) => <Enum node={node} enumType={enumType} enumKeyCasing={enumKeyCasing} resolver={resolver} />)}
|
|
183
65
|
{shouldExportType && (
|
|
184
66
|
<File.Source name={typedName} isTypeOnly isExportable isIndexable>
|
|
185
|
-
{safePrint(
|
|
67
|
+
{safePrint(typeNode)}
|
|
186
68
|
</File.Source>
|
|
187
69
|
)}
|
|
188
70
|
</>
|
package/src/components/index.ts
CHANGED
package/src/generators/index.ts
CHANGED