@kubb/plugin-ts 5.0.0-alpha.11 → 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.
Files changed (43) hide show
  1. package/dist/{components-CRu8IKY3.js → Type-CX1HRooG.js} +377 -365
  2. package/dist/Type-CX1HRooG.js.map +1 -0
  3. package/dist/Type-Cat0_htq.cjs +808 -0
  4. package/dist/Type-Cat0_htq.cjs.map +1 -0
  5. package/dist/components.cjs +3 -2
  6. package/dist/components.d.ts +40 -9
  7. package/dist/components.js +2 -2
  8. package/dist/generators-CLuCmfUz.js +532 -0
  9. package/dist/generators-CLuCmfUz.js.map +1 -0
  10. package/dist/generators-DWBU-MuW.cjs +536 -0
  11. package/dist/generators-DWBU-MuW.cjs.map +1 -0
  12. package/dist/generators.cjs +2 -3
  13. package/dist/generators.d.ts +3 -503
  14. package/dist/generators.js +2 -2
  15. package/dist/index.cjs +308 -4
  16. package/dist/index.cjs.map +1 -0
  17. package/dist/index.d.ts +26 -21
  18. package/dist/index.js +305 -2
  19. package/dist/index.js.map +1 -0
  20. package/dist/{types-mSXmB8WU.d.ts → types-BA1ZCQ5p.d.ts} +73 -57
  21. package/package.json +5 -5
  22. package/src/components/{v2/Enum.tsx → Enum.tsx} +27 -11
  23. package/src/components/Type.tsx +23 -141
  24. package/src/components/index.ts +1 -0
  25. package/src/generators/index.ts +0 -1
  26. package/src/generators/typeGenerator.tsx +189 -413
  27. package/src/generators/utils.ts +298 -0
  28. package/src/index.ts +1 -1
  29. package/src/plugin.ts +80 -126
  30. package/src/printer.ts +15 -4
  31. package/src/resolverTs.ts +109 -1
  32. package/src/types.ts +68 -52
  33. package/dist/components-CRu8IKY3.js.map +0 -1
  34. package/dist/components-DeNDKlzf.cjs +0 -982
  35. package/dist/components-DeNDKlzf.cjs.map +0 -1
  36. package/dist/plugin-CJ29AwE2.cjs +0 -1320
  37. package/dist/plugin-CJ29AwE2.cjs.map +0 -1
  38. package/dist/plugin-D60XNJSD.js +0 -1267
  39. package/dist/plugin-D60XNJSD.js.map +0 -1
  40. package/src/components/v2/Type.tsx +0 -59
  41. package/src/generators/v2/typeGenerator.tsx +0 -167
  42. package/src/generators/v2/utils.ts +0 -140
  43. package/src/parser.ts +0 -389
@@ -1,24 +1,22 @@
1
- import { camelCase, jsStringEscape, pascalCase, trimQuotes } from '@internals/utils'
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 type ts from 'typescript'
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
- schema: SchemaObject
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
- tree,
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 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)
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
- if (isNullable) {
90
- type = factory.createUnionDeclaration({
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
- 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
- }
47
+ if (!typeNode) {
48
+ return
106
49
  }
107
50
 
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
-
51
+ const enums = [...new Map(enumSchemaNodes.map((n) => [n.name, n])).values()].map((node) => {
149
52
  return {
150
- nameNode,
151
- typeNode,
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(...typeNodes)}
67
+ {safePrint(typeNode)}
186
68
  </File.Source>
187
69
  )}
188
70
  </>
@@ -1 +1,2 @@
1
+ export { Enum } from './Enum.tsx'
1
2
  export { Type } from './Type.tsx'
@@ -1,2 +1 @@
1
1
  export { typeGenerator } from './typeGenerator.tsx'
2
- export { typeGenerator as typeGeneratorV2 } from './v2/typeGenerator.tsx'