@kubb/plugin-ts 5.0.0-alpha.8 → 5.0.0-beta.3

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 (42) hide show
  1. package/LICENSE +17 -10
  2. package/README.md +1 -3
  3. package/dist/index.cjs +1452 -4
  4. package/dist/index.cjs.map +1 -0
  5. package/dist/index.d.ts +574 -4
  6. package/dist/index.js +1418 -2
  7. package/dist/index.js.map +1 -0
  8. package/package.json +44 -64
  9. package/src/components/{v2/Enum.tsx → Enum.tsx} +33 -17
  10. package/src/components/Type.tsx +31 -161
  11. package/src/constants.ts +15 -5
  12. package/src/factory.ts +283 -35
  13. package/src/generators/typeGenerator.tsx +189 -424
  14. package/src/index.ts +9 -2
  15. package/src/plugin.ts +67 -205
  16. package/src/printers/functionPrinter.ts +197 -0
  17. package/src/printers/printerTs.ts +325 -0
  18. package/src/resolvers/resolverTs.ts +66 -0
  19. package/src/types.ts +238 -94
  20. package/src/utils.ts +130 -0
  21. package/dist/components-CRu8IKY3.js +0 -729
  22. package/dist/components-CRu8IKY3.js.map +0 -1
  23. package/dist/components-DeNDKlzf.cjs +0 -982
  24. package/dist/components-DeNDKlzf.cjs.map +0 -1
  25. package/dist/components.cjs +0 -3
  26. package/dist/components.d.ts +0 -36
  27. package/dist/components.js +0 -2
  28. package/dist/generators.cjs +0 -4
  29. package/dist/generators.d.ts +0 -480
  30. package/dist/generators.js +0 -2
  31. package/dist/plugin-D5NGPj0v.js +0 -1232
  32. package/dist/plugin-D5NGPj0v.js.map +0 -1
  33. package/dist/plugin-MLTxoa8p.cjs +0 -1279
  34. package/dist/plugin-MLTxoa8p.cjs.map +0 -1
  35. package/dist/types-CsvB6X5Y.d.ts +0 -167
  36. package/src/components/index.ts +0 -1
  37. package/src/components/v2/Type.tsx +0 -59
  38. package/src/generators/index.ts +0 -2
  39. package/src/generators/v2/typeGenerator.tsx +0 -171
  40. package/src/generators/v2/utils.ts +0 -140
  41. package/src/parser.ts +0 -389
  42. package/src/printer.ts +0 -368
@@ -1,156 +1,42 @@
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'
1
+ import { ast } from '@kubb/core'
2
+ import { File } from '@kubb/renderer-jsx'
3
+ import type { KubbReactNode } from '@kubb/renderer-jsx/types'
4
+ import type { PrinterTsFactory } from '../printers/printerTs.ts'
5
+ import type { PluginTs, ResolverTs } from '../types.ts'
6
+ import { Enum, getEnumNames } from './Enum.tsx'
11
7
 
12
8
  type Props = {
13
9
  name: string
14
- typedName: string
15
- schema: SchemaObject
16
- tree: Array<Schema>
17
- optionalType: PluginTs['resolvedOptions']['optionalType']
18
- arrayType: PluginTs['resolvedOptions']['arrayType']
10
+ node: ast.SchemaNode
11
+ /**
12
+ * Pre-configured printer instance created by the generator.
13
+ * Created with `printerTs({ ..., nodes: options.printer?.nodes })`.
14
+ */
15
+ printer: ast.Printer<PrinterTsFactory>
19
16
  enumType: PluginTs['resolvedOptions']['enumType']
17
+ enumTypeSuffix: PluginTs['resolvedOptions']['enumTypeSuffix']
20
18
  enumKeyCasing: PluginTs['resolvedOptions']['enumKeyCasing']
21
- syntaxType: PluginTs['resolvedOptions']['syntaxType']
22
- description?: string
23
- keysToOmit?: string[]
19
+ resolver: ResolverTs
24
20
  }
25
21
 
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)
22
+ export function Type({ name, node, printer, enumType, enumTypeSuffix, enumKeyCasing, resolver }: Props): KubbReactNode {
23
+ const enumSchemaNodes = ast.collect<ast.EnumSchemaNode>(node, {
24
+ schema(n): ast.EnumSchemaNode | undefined {
25
+ const enumNode = ast.narrowSchema(n, ast.schemaTypes.enum)
26
+ if (enumNode?.name) return enumNode
27
+ },
28
+ })
88
29
 
89
- if (isNullable) {
90
- type = factory.createUnionDeclaration({
91
- nodes: [type, factory.keywordTypeNodes.null],
92
- }) as ts.TypeNode
93
- }
30
+ const output = printer.print(node)
94
31
 
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
- }
32
+ if (!output) {
33
+ return
106
34
  }
107
35
 
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
-
36
+ const enums = [...new Map(enumSchemaNodes.map((n) => [n.name, n])).values()].map((node) => {
149
37
  return {
150
- nameNode,
151
- typeNode,
152
- name,
153
- typeName,
38
+ node,
39
+ ...getEnumNames({ node, enumType, enumTypeSuffix, resolver }),
154
40
  }
155
41
  })
156
42
 
@@ -161,28 +47,12 @@ export function Type({
161
47
  return (
162
48
  <>
163
49
  {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
- </>
50
+ enums.map(({ node }) => (
51
+ <Enum key={node.name} node={node} enumType={enumType} enumTypeSuffix={enumTypeSuffix} enumKeyCasing={enumKeyCasing} resolver={resolver} />
182
52
  ))}
183
53
  {shouldExportType && (
184
- <File.Source name={typedName} isTypeOnly isExportable isIndexable>
185
- {safePrint(...typeNodes)}
54
+ <File.Source name={name} isTypeOnly isExportable isIndexable>
55
+ {output}
186
56
  </File.Source>
187
57
  )}
188
58
  </>
package/src/constants.ts CHANGED
@@ -6,24 +6,34 @@ type EnumType = PluginTs['resolvedOptions']['enumType']
6
6
  /**
7
7
  * `optionalType` values that cause a property's type to include `| undefined`.
8
8
  */
9
- export const OPTIONAL_ADDS_UNDEFINED = new Set<OptionalType>(['undefined', 'questionTokenAndUndefined'])
9
+ export const OPTIONAL_ADDS_UNDEFINED = new Set<OptionalType>(['undefined', 'questionTokenAndUndefined'] as const)
10
10
 
11
11
  /**
12
12
  * `optionalType` values that render the property key with a `?` token.
13
13
  */
14
- export const OPTIONAL_ADDS_QUESTION_TOKEN = new Set<OptionalType>(['questionToken', 'questionTokenAndUndefined'])
14
+ export const OPTIONAL_ADDS_QUESTION_TOKEN = new Set<OptionalType>(['questionToken', 'questionTokenAndUndefined'] as const)
15
15
 
16
16
  /**
17
17
  * `enumType` values that append a `Key` suffix to the generated enum type alias.
18
18
  */
19
- export const ENUM_TYPES_WITH_KEY_SUFFIX = new Set<EnumType>(['asConst', 'asPascalConst'])
19
+ export const ENUM_TYPES_WITH_KEY_SUFFIX = new Set<EnumType>(['asConst', 'asPascalConst'] as const)
20
20
 
21
21
  /**
22
22
  * `enumType` values that require a runtime value declaration (object, enum, or literal).
23
23
  */
24
- export const ENUM_TYPES_WITH_RUNTIME_VALUE = new Set<EnumType | undefined>(['enum', 'asConst', 'asPascalConst', 'constEnum', 'literal', undefined])
24
+ export const ENUM_TYPES_WITH_RUNTIME_VALUE = new Set<EnumType | undefined>(['enum', 'asConst', 'asPascalConst', 'constEnum', 'literal', undefined] as const)
25
25
 
26
26
  /**
27
27
  * `enumType` values whose type declaration is type-only (no runtime value emitted for the type alias).
28
28
  */
29
- export const ENUM_TYPES_WITH_TYPE_ONLY = new Set<EnumType | undefined>(['asConst', 'asPascalConst', 'literal', undefined])
29
+ export const ENUM_TYPES_WITH_TYPE_ONLY = new Set<EnumType | undefined>(['asConst', 'asPascalConst', 'literal', undefined] as const)
30
+
31
+ /**
32
+ * Ordering priority for function parameters: lower = sorted earlier.
33
+ */
34
+ export const PARAM_RANK = {
35
+ required: 0,
36
+ optional: 1,
37
+ withDefault: 2,
38
+ rest: 3,
39
+ } as const