@kubb/plugin-ts 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.
- package/LICENSE +17 -10
- package/README.md +38 -23
- package/dist/index.cjs +2376 -5
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.ts +941 -40
- package/dist/index.js +2321 -2
- package/dist/index.js.map +1 -0
- package/dist/rolldown-runtime-CNktS9qV.js +17 -0
- package/package.json +38 -74
- package/dist/chunk--u3MIqq1.js +0 -8
- package/dist/components-CRu8IKY3.js +0 -729
- 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/components.cjs +0 -3
- package/dist/components.d.ts +0 -36
- package/dist/components.js +0 -2
- package/dist/generators.cjs +0 -4
- package/dist/generators.d.ts +0 -509
- package/dist/generators.js +0 -2
- 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/dist/types-mSXmB8WU.d.ts +0 -298
- package/src/components/Type.tsx +0 -190
- package/src/components/index.ts +0 -1
- package/src/components/v2/Enum.tsx +0 -67
- package/src/components/v2/Type.tsx +0 -59
- package/src/constants.ts +0 -29
- package/src/factory.ts +0 -726
- package/src/generators/index.ts +0 -2
- package/src/generators/typeGenerator.tsx +0 -468
- package/src/generators/v2/typeGenerator.tsx +0 -167
- package/src/generators/v2/utils.ts +0 -140
- package/src/index.ts +0 -3
- package/src/parser.ts +0 -389
- package/src/plugin.ts +0 -232
- package/src/printer.ts +0 -368
- package/src/resolverTs.ts +0 -77
- package/src/types.ts +0 -296
package/src/components/Type.tsx
DELETED
|
@@ -1,190 +0,0 @@
|
|
|
1
|
-
import { camelCase, jsStringEscape, pascalCase, trimQuotes } from '@internals/utils'
|
|
2
|
-
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
|
-
import { File } from '@kubb/react-fabric'
|
|
6
|
-
import type { FabricReactNode } from '@kubb/react-fabric/types'
|
|
7
|
-
import type ts from 'typescript'
|
|
8
|
-
import * as factory from '../factory.ts'
|
|
9
|
-
import { parse, typeKeywordMapper } from '../parser.ts'
|
|
10
|
-
import type { PluginTs } from '../types.ts'
|
|
11
|
-
|
|
12
|
-
type Props = {
|
|
13
|
-
name: string
|
|
14
|
-
typedName: string
|
|
15
|
-
schema: SchemaObject
|
|
16
|
-
tree: Array<Schema>
|
|
17
|
-
optionalType: PluginTs['resolvedOptions']['optionalType']
|
|
18
|
-
arrayType: PluginTs['resolvedOptions']['arrayType']
|
|
19
|
-
enumType: PluginTs['resolvedOptions']['enumType']
|
|
20
|
-
enumKeyCasing: PluginTs['resolvedOptions']['enumKeyCasing']
|
|
21
|
-
syntaxType: PluginTs['resolvedOptions']['syntaxType']
|
|
22
|
-
description?: string
|
|
23
|
-
keysToOmit?: string[]
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
export function Type({
|
|
27
|
-
name,
|
|
28
|
-
typedName,
|
|
29
|
-
tree,
|
|
30
|
-
keysToOmit,
|
|
31
|
-
schema,
|
|
32
|
-
optionalType,
|
|
33
|
-
arrayType,
|
|
34
|
-
syntaxType,
|
|
35
|
-
enumType,
|
|
36
|
-
enumKeyCasing,
|
|
37
|
-
description,
|
|
38
|
-
}: Props): FabricReactNode {
|
|
39
|
-
const typeNodes: ts.Node[] = []
|
|
40
|
-
|
|
41
|
-
if (!tree.length) {
|
|
42
|
-
return ''
|
|
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)
|
|
88
|
-
|
|
89
|
-
if (isNullable) {
|
|
90
|
-
type = factory.createUnionDeclaration({
|
|
91
|
-
nodes: [type, factory.keywordTypeNodes.null],
|
|
92
|
-
}) as ts.TypeNode
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
if (isNullish && ['undefined', 'questionTokenAndUndefined'].includes(optionalType as string)) {
|
|
96
|
-
type = factory.createUnionDeclaration({
|
|
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
|
-
}
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
const useTypeGeneration = syntaxType === 'type' || [factory.syntaxKind.union].includes(type.kind as typeof factory.syntaxKind.union) || !!keysToOmit?.length
|
|
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
|
-
|
|
149
|
-
return {
|
|
150
|
-
nameNode,
|
|
151
|
-
typeNode,
|
|
152
|
-
name,
|
|
153
|
-
typeName,
|
|
154
|
-
}
|
|
155
|
-
})
|
|
156
|
-
|
|
157
|
-
// Skip enum exports when using inlineLiteral
|
|
158
|
-
const shouldExportEnums = enumType !== 'inlineLiteral'
|
|
159
|
-
const shouldExportType = enumType === 'inlineLiteral' || enums.every((item) => item.typeName !== name)
|
|
160
|
-
|
|
161
|
-
return (
|
|
162
|
-
<>
|
|
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
|
-
))}
|
|
183
|
-
{shouldExportType && (
|
|
184
|
-
<File.Source name={typedName} isTypeOnly isExportable isIndexable>
|
|
185
|
-
{safePrint(...typeNodes)}
|
|
186
|
-
</File.Source>
|
|
187
|
-
)}
|
|
188
|
-
</>
|
|
189
|
-
)
|
|
190
|
-
}
|
package/src/components/index.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export { Type } from './Type.tsx'
|
|
@@ -1,67 +0,0 @@
|
|
|
1
|
-
import { camelCase, pascalCase, trimQuotes } from '@internals/utils'
|
|
2
|
-
import type { EnumSchemaNode } from '@kubb/ast/types'
|
|
3
|
-
import { safePrint } from '@kubb/fabric-core/parsers/typescript'
|
|
4
|
-
import { File } from '@kubb/react-fabric'
|
|
5
|
-
import type { FabricReactNode } from '@kubb/react-fabric/types'
|
|
6
|
-
import { ENUM_TYPES_WITH_KEY_SUFFIX, ENUM_TYPES_WITH_RUNTIME_VALUE, ENUM_TYPES_WITH_TYPE_ONLY } from '../../constants.ts'
|
|
7
|
-
import * as factory from '../../factory.ts'
|
|
8
|
-
import type { PluginTs } from '../../types.ts'
|
|
9
|
-
|
|
10
|
-
type Props = {
|
|
11
|
-
node: EnumSchemaNode
|
|
12
|
-
enumType: PluginTs['resolvedOptions']['enumType']
|
|
13
|
-
enumKeyCasing: PluginTs['resolvedOptions']['enumKeyCasing']
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
/**
|
|
17
|
-
* Resolves the runtime identifier name and the TypeScript type name for an enum schema node.
|
|
18
|
-
*
|
|
19
|
-
* The raw `node.name` may be a YAML key such as `"enumNames.Type"` which is not a
|
|
20
|
-
* valid TypeScript identifier. `pascalCase` normalizes it unconditionally; for inline enum
|
|
21
|
-
* properties the adapter already emits a PascalCase+suffix name so `pascalCase` is a no-op.
|
|
22
|
-
*/
|
|
23
|
-
export function getEnumNames(node: EnumSchemaNode, enumType: PluginTs['resolvedOptions']['enumType']): { enumName: string; typeName: string } {
|
|
24
|
-
const resolved = pascalCase(node.name!)
|
|
25
|
-
const enumName = enumType === 'asPascalConst' ? resolved : camelCase(node.name!)
|
|
26
|
-
const typeName = ENUM_TYPES_WITH_KEY_SUFFIX.has(enumType) ? `${resolved}Key` : resolved
|
|
27
|
-
|
|
28
|
-
return { enumName, typeName }
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
/**
|
|
32
|
-
* Renders the enum declaration(s) for a single named `EnumSchemaNode`.
|
|
33
|
-
*
|
|
34
|
-
* Depending on `enumType` this may emit:
|
|
35
|
-
* - A runtime object (`asConst` / `asPascalConst`) plus a `typeof` type alias
|
|
36
|
-
* - A `const enum` or plain `enum` declaration (`constEnum` / `enum`)
|
|
37
|
-
* - A union literal type alias (`literal`)
|
|
38
|
-
*
|
|
39
|
-
* The emitted `File.Source` nodes carry the resolved names so that the barrel
|
|
40
|
-
* index picks up the correct export identifiers.
|
|
41
|
-
*/
|
|
42
|
-
export function Enum({ node, enumType, enumKeyCasing }: Props): FabricReactNode {
|
|
43
|
-
const { enumName, typeName } = getEnumNames(node, enumType)
|
|
44
|
-
|
|
45
|
-
const [nameNode, typeNode] = factory.createEnumDeclaration({
|
|
46
|
-
name: enumName,
|
|
47
|
-
typeName,
|
|
48
|
-
enums: (node.namedEnumValues?.map((v) => [trimQuotes(v.name.toString()), v.value]) ??
|
|
49
|
-
node.enumValues?.filter((v): v is NonNullable<typeof v> => v !== null && v !== undefined).map((v) => [trimQuotes(v.toString()), v]) ??
|
|
50
|
-
[]) as unknown as Array<[string, string]>,
|
|
51
|
-
type: enumType,
|
|
52
|
-
enumKeyCasing,
|
|
53
|
-
})
|
|
54
|
-
|
|
55
|
-
return (
|
|
56
|
-
<>
|
|
57
|
-
{nameNode && (
|
|
58
|
-
<File.Source name={enumName} isExportable isIndexable isTypeOnly={false}>
|
|
59
|
-
{safePrint(nameNode)}
|
|
60
|
-
</File.Source>
|
|
61
|
-
)}
|
|
62
|
-
<File.Source name={typeName} isIndexable isExportable={ENUM_TYPES_WITH_RUNTIME_VALUE.has(enumType)} isTypeOnly={ENUM_TYPES_WITH_TYPE_ONLY.has(enumType)}>
|
|
63
|
-
{safePrint(typeNode)}
|
|
64
|
-
</File.Source>
|
|
65
|
-
</>
|
|
66
|
-
)
|
|
67
|
-
}
|
|
@@ -1,59 +0,0 @@
|
|
|
1
|
-
import { collect } from '@kubb/ast'
|
|
2
|
-
import type { EnumSchemaNode, SchemaNode } from '@kubb/ast/types'
|
|
3
|
-
import { safePrint } from '@kubb/fabric-core/parsers/typescript'
|
|
4
|
-
import { File } from '@kubb/react-fabric'
|
|
5
|
-
import type { FabricReactNode } from '@kubb/react-fabric/types'
|
|
6
|
-
import { printerTs } from '../../printer.ts'
|
|
7
|
-
import type { PluginTs } from '../../types.ts'
|
|
8
|
-
import { Enum, getEnumNames } from './Enum.tsx'
|
|
9
|
-
|
|
10
|
-
type Props = {
|
|
11
|
-
name: string
|
|
12
|
-
typedName: string
|
|
13
|
-
node: SchemaNode
|
|
14
|
-
optionalType: PluginTs['resolvedOptions']['optionalType']
|
|
15
|
-
arrayType: PluginTs['resolvedOptions']['arrayType']
|
|
16
|
-
enumType: PluginTs['resolvedOptions']['enumType']
|
|
17
|
-
enumKeyCasing: PluginTs['resolvedOptions']['enumKeyCasing']
|
|
18
|
-
syntaxType: PluginTs['resolvedOptions']['syntaxType']
|
|
19
|
-
description?: string
|
|
20
|
-
keysToOmit?: string[]
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
export function Type({ name, typedName, node, keysToOmit, optionalType, arrayType, syntaxType, enumType, enumKeyCasing, description }: Props): FabricReactNode {
|
|
24
|
-
const resolvedDescription = description || node?.description
|
|
25
|
-
const enumSchemaNodes = collect<EnumSchemaNode>(node, {
|
|
26
|
-
schema(n): EnumSchemaNode | undefined {
|
|
27
|
-
if (n.type === 'enum' && n.name) return n as EnumSchemaNode
|
|
28
|
-
},
|
|
29
|
-
})
|
|
30
|
-
|
|
31
|
-
const printer = printerTs({ optionalType, arrayType, enumType, typeName: name, syntaxType, description: resolvedDescription, keysToOmit })
|
|
32
|
-
const typeNode = printer.print(node)
|
|
33
|
-
|
|
34
|
-
if (!typeNode) {
|
|
35
|
-
return
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
const enums = [...new Map(enumSchemaNodes.map((n) => [n.name, n])).values()].map((node) => {
|
|
39
|
-
return {
|
|
40
|
-
node,
|
|
41
|
-
...getEnumNames(node, enumType),
|
|
42
|
-
}
|
|
43
|
-
})
|
|
44
|
-
|
|
45
|
-
// Skip enum exports when using inlineLiteral
|
|
46
|
-
const shouldExportEnums = enumType !== 'inlineLiteral'
|
|
47
|
-
const shouldExportType = enumType === 'inlineLiteral' || enums.every((item) => item.typeName !== name)
|
|
48
|
-
|
|
49
|
-
return (
|
|
50
|
-
<>
|
|
51
|
-
{shouldExportEnums && enums.map(({ node }) => <Enum node={node} enumType={enumType} enumKeyCasing={enumKeyCasing} />)}
|
|
52
|
-
{shouldExportType && (
|
|
53
|
-
<File.Source name={typedName} isTypeOnly isExportable isIndexable>
|
|
54
|
-
{safePrint(typeNode)}
|
|
55
|
-
</File.Source>
|
|
56
|
-
)}
|
|
57
|
-
</>
|
|
58
|
-
)
|
|
59
|
-
}
|
package/src/constants.ts
DELETED
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
import type { PluginTs } from './types.ts'
|
|
2
|
-
|
|
3
|
-
type OptionalType = PluginTs['resolvedOptions']['optionalType']
|
|
4
|
-
type EnumType = PluginTs['resolvedOptions']['enumType']
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* `optionalType` values that cause a property's type to include `| undefined`.
|
|
8
|
-
*/
|
|
9
|
-
export const OPTIONAL_ADDS_UNDEFINED = new Set<OptionalType>(['undefined', 'questionTokenAndUndefined'] as const)
|
|
10
|
-
|
|
11
|
-
/**
|
|
12
|
-
* `optionalType` values that render the property key with a `?` token.
|
|
13
|
-
*/
|
|
14
|
-
export const OPTIONAL_ADDS_QUESTION_TOKEN = new Set<OptionalType>(['questionToken', 'questionTokenAndUndefined'] as const)
|
|
15
|
-
|
|
16
|
-
/**
|
|
17
|
-
* `enumType` values that append a `Key` suffix to the generated enum type alias.
|
|
18
|
-
*/
|
|
19
|
-
export const ENUM_TYPES_WITH_KEY_SUFFIX = new Set<EnumType>(['asConst', 'asPascalConst'] as const)
|
|
20
|
-
|
|
21
|
-
/**
|
|
22
|
-
* `enumType` values that require a runtime value declaration (object, enum, or literal).
|
|
23
|
-
*/
|
|
24
|
-
export const ENUM_TYPES_WITH_RUNTIME_VALUE = new Set<EnumType | undefined>(['enum', 'asConst', 'asPascalConst', 'constEnum', 'literal', undefined] as const)
|
|
25
|
-
|
|
26
|
-
/**
|
|
27
|
-
* `enumType` values whose type declaration is type-only (no runtime value emitted for the type alias).
|
|
28
|
-
*/
|
|
29
|
-
export const ENUM_TYPES_WITH_TYPE_ONLY = new Set<EnumType | undefined>(['asConst', 'asPascalConst', 'literal', undefined] as const)
|