@kubb/plugin-ts 3.0.0-alpha.2 → 3.0.0-alpha.21

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 (94) hide show
  1. package/README.md +13 -4
  2. package/dist/chunk-3IKQZT26.cjs +2541 -0
  3. package/dist/chunk-3IKQZT26.cjs.map +1 -0
  4. package/dist/chunk-CAATRNVS.js +2514 -0
  5. package/dist/chunk-CAATRNVS.js.map +1 -0
  6. package/dist/chunk-CPJWNK44.js +307 -0
  7. package/dist/chunk-CPJWNK44.js.map +1 -0
  8. package/dist/chunk-YLKAQOZ5.cjs +336 -0
  9. package/dist/chunk-YLKAQOZ5.cjs.map +1 -0
  10. package/dist/components.cjs +11 -6
  11. package/dist/components.cjs.map +1 -1
  12. package/dist/components.d.cts +14 -44
  13. package/dist/components.d.ts +14 -44
  14. package/dist/components.js +2 -10
  15. package/dist/components.js.map +1 -1
  16. package/dist/generators.cjs +17 -0
  17. package/dist/generators.cjs.map +1 -0
  18. package/dist/generators.d.cts +10 -0
  19. package/dist/generators.d.ts +10 -0
  20. package/dist/generators.js +4 -0
  21. package/dist/generators.js.map +1 -0
  22. package/dist/index.cjs +12 -4
  23. package/dist/index.cjs.map +1 -1
  24. package/dist/index.d.cts +4 -122
  25. package/dist/index.d.ts +4 -122
  26. package/dist/index.js +3 -8
  27. package/dist/index.js.map +1 -1
  28. package/dist/types-W01J9acJ.d.cts +106 -0
  29. package/dist/types-W01J9acJ.d.ts +106 -0
  30. package/package.json +21 -15
  31. package/src/components/OasType.tsx +11 -58
  32. package/src/components/Type.tsx +133 -0
  33. package/src/components/index.ts +1 -2
  34. package/src/generators/__snapshots__/bodyUploadFileApiAssetsPost.ts +6 -0
  35. package/src/generators/__snapshots__/catTypeAsConst.ts +11 -0
  36. package/src/generators/__snapshots__/createPet.ts +34 -0
  37. package/src/generators/__snapshots__/createPetWithUnknownTypeUnknown.ts +34 -0
  38. package/src/generators/__snapshots__/deletePet.ts +5 -0
  39. package/src/generators/__snapshots__/dogTypeAsConst.ts +11 -0
  40. package/src/generators/__snapshots__/enumAllOf.ts +10 -0
  41. package/src/generators/__snapshots__/enumArray.ts +19 -0
  42. package/src/generators/__snapshots__/enumInObject.ts +13 -0
  43. package/src/generators/__snapshots__/enumItems.ts +8 -0
  44. package/src/generators/__snapshots__/enumNames.ts +6 -0
  45. package/src/generators/__snapshots__/enumNamesConst.ts +6 -0
  46. package/src/generators/__snapshots__/enumNamesLiteral.ts +3 -0
  47. package/src/generators/__snapshots__/enumNamesPascalConst.ts +8 -0
  48. package/src/generators/__snapshots__/enumNamesType.ts +8 -0
  49. package/src/generators/__snapshots__/enumNullableMember.ts +8 -0
  50. package/src/generators/__snapshots__/enumNullableType.ts +8 -0
  51. package/src/generators/__snapshots__/enumString.ts +10 -0
  52. package/src/generators/__snapshots__/enumVarNamesType.ts +8 -0
  53. package/src/generators/__snapshots__/getPets.ts +27 -0
  54. package/src/generators/__snapshots__/mixedValueTypeConst.ts +9 -0
  55. package/src/generators/__snapshots__/nullConstNull.ts +6 -0
  56. package/src/generators/__snapshots__/numberValueConst.ts +9 -0
  57. package/src/generators/__snapshots__/petMapper.ts +15 -0
  58. package/src/generators/__snapshots__/petQuestionToken.ts +15 -0
  59. package/src/generators/__snapshots__/petQuestionTokenAndUndefined.ts +15 -0
  60. package/src/generators/__snapshots__/petUndefined.ts +15 -0
  61. package/src/generators/__snapshots__/pets.ts +14 -0
  62. package/src/generators/__snapshots__/petsStoreDiscriminator.ts +13 -0
  63. package/src/generators/__snapshots__/petsStoreRef.ts +1 -0
  64. package/src/generators/__snapshots__/plainDateDate.ts +1 -0
  65. package/src/generators/__snapshots__/plainDateString.ts +1 -0
  66. package/src/generators/__snapshots__/plainEmail.ts +1 -0
  67. package/src/generators/__snapshots__/plainFile.ts +1 -0
  68. package/src/generators/__snapshots__/plainTimeDate.ts +1 -0
  69. package/src/generators/__snapshots__/plainUuid.ts +1 -0
  70. package/src/generators/__snapshots__/showPetById.ts +32 -0
  71. package/src/generators/__snapshots__/stringValueConst.ts +9 -0
  72. package/src/generators/index.ts +2 -0
  73. package/src/generators/oasGenerator.tsx +29 -0
  74. package/src/generators/typeGenerator.tsx +200 -0
  75. package/src/parser/index.ts +2 -1
  76. package/src/plugin.ts +21 -20
  77. package/src/types.ts +9 -35
  78. package/dist/chunk-JF3PKBEA.cjs +0 -608
  79. package/dist/chunk-JF3PKBEA.cjs.map +0 -1
  80. package/dist/chunk-PU22CLWV.js +0 -608
  81. package/dist/chunk-PU22CLWV.js.map +0 -1
  82. package/dist/oas.cjs +0 -1
  83. package/dist/oas.cjs.map +0 -1
  84. package/dist/oas.d.cts +0 -1
  85. package/dist/oas.d.ts +0 -1
  86. package/dist/oas.js +0 -1
  87. package/dist/oas.js.map +0 -1
  88. package/src/OperationGenerator.tsx +0 -49
  89. package/src/SchemaGenerator.tsx +0 -31
  90. package/src/components/OperationSchema.tsx +0 -161
  91. package/src/components/Schema.tsx +0 -135
  92. package/src/components/__snapshots__/Schema/pets.ts +0 -23
  93. package/src/components/__snapshots__/Schema/showPetById.ts +0 -28
  94. package/src/oas/index.ts +0 -1
@@ -0,0 +1,200 @@
1
+ import type { PluginManager } from '@kubb/core'
2
+ import transformers from '@kubb/core/transformers'
3
+ import { print } from '@kubb/parser-ts'
4
+ import * as factory from '@kubb/parser-ts/factory'
5
+ import { type OperationSchema as OperationSchemaType, type OperationSchemas, SchemaGenerator, createReactGenerator, schemaKeywords } from '@kubb/plugin-oas'
6
+ import { Oas } from '@kubb/plugin-oas/components'
7
+ import { useOas, useOperationManager, useSchemaManager } from '@kubb/plugin-oas/hooks'
8
+ import { pluginTsName } from '@kubb/plugin-ts'
9
+ import { File, useApp } from '@kubb/react'
10
+ import type ts from 'typescript'
11
+ import { Type } from '../components'
12
+ import type { PluginTs } from '../types'
13
+
14
+ function printCombinedSchema({ name, schemas, pluginManager }: { name: string; schemas: OperationSchemas; pluginManager: PluginManager }): string {
15
+ const properties: Record<string, ts.TypeNode> = {}
16
+
17
+ if (schemas.response) {
18
+ const identifier = pluginManager.resolveName({
19
+ name: schemas.response.name,
20
+ pluginKey: [pluginTsName],
21
+ type: 'function',
22
+ })
23
+ properties['response'] = factory.createTypeReferenceNode(factory.createIdentifier(identifier), undefined)
24
+ }
25
+
26
+ if (schemas.request) {
27
+ const identifier = pluginManager.resolveName({
28
+ name: schemas.request.name,
29
+ pluginKey: [pluginTsName],
30
+ type: 'function',
31
+ })
32
+ properties['request'] = factory.createTypeReferenceNode(factory.createIdentifier(identifier), undefined)
33
+ }
34
+
35
+ if (schemas.pathParams) {
36
+ const identifier = pluginManager.resolveName({
37
+ name: schemas.pathParams.name,
38
+ pluginKey: [pluginTsName],
39
+ type: 'function',
40
+ })
41
+ properties['pathParams'] = factory.createTypeReferenceNode(factory.createIdentifier(identifier), undefined)
42
+ }
43
+
44
+ if (schemas.queryParams) {
45
+ const identifier = pluginManager.resolveName({
46
+ name: schemas.queryParams.name,
47
+ pluginKey: [pluginTsName],
48
+ type: 'function',
49
+ })
50
+ properties['queryParams'] = factory.createTypeReferenceNode(factory.createIdentifier(identifier), undefined)
51
+ }
52
+
53
+ if (schemas.headerParams) {
54
+ const identifier = pluginManager.resolveName({
55
+ name: schemas.headerParams.name,
56
+ pluginKey: [pluginTsName],
57
+ type: 'function',
58
+ })
59
+ properties['headerParams'] = factory.createTypeReferenceNode(factory.createIdentifier(identifier), undefined)
60
+ }
61
+
62
+ if (schemas.errors) {
63
+ properties['errors'] = factory.createUnionDeclaration({
64
+ nodes: schemas.errors.map((error) => {
65
+ const identifier = pluginManager.resolveName({
66
+ name: error.name,
67
+ pluginKey: [pluginTsName],
68
+ type: 'function',
69
+ })
70
+
71
+ return factory.createTypeReferenceNode(factory.createIdentifier(identifier), undefined)
72
+ }),
73
+ })!
74
+ }
75
+
76
+ const namespaceNode = factory.createTypeAliasDeclaration({
77
+ name,
78
+ type: factory.createTypeLiteralNode(
79
+ Object.keys(properties)
80
+ .map((key) => {
81
+ const type = properties[key]
82
+ if (!type) {
83
+ return undefined
84
+ }
85
+
86
+ return factory.createPropertySignature({
87
+ name: transformers.pascalCase(key),
88
+ type,
89
+ })
90
+ })
91
+ .filter(Boolean),
92
+ ),
93
+ modifiers: [factory.modifiers.export],
94
+ })
95
+
96
+ return print([namespaceNode])
97
+ }
98
+
99
+ export const typeGenerator = createReactGenerator<PluginTs>({
100
+ name: 'typescript',
101
+ Operation({ operation, options }) {
102
+ const { mapper, enumType, optionalType } = options
103
+
104
+ const { plugin, pluginManager, mode } = useApp<PluginTs>()
105
+ const oas = useOas()
106
+ const { getSchemas, getFile, getName } = useOperationManager()
107
+ const schemaManager = useSchemaManager()
108
+
109
+ const file = getFile(operation)
110
+ const schemas = getSchemas(operation)
111
+ const type = getName(operation, { type: 'function', pluginKey: [pluginTsName] })
112
+ const combinedSchemaName = operation.method === 'get' ? `${type}Query` : `${type}Mutation`
113
+ const schemaGenerator = new SchemaGenerator(options, {
114
+ oas,
115
+ plugin,
116
+ pluginManager,
117
+ mode,
118
+ override: options.override,
119
+ })
120
+
121
+ const operationSchemas = [schemas.pathParams, schemas.queryParams, schemas.headerParams, schemas.statusCodes, schemas.request, schemas.response]
122
+ .flat()
123
+ .filter(Boolean)
124
+
125
+ const mapOperationSchema = ({ name, schema, description, keysToOmit, ...options }: OperationSchemaType, i: number) => {
126
+ // hack so Params can be optional when needed
127
+ const tree = schemaGenerator.parse({ schema, name })
128
+ const imports = schemaManager.getImports(tree)
129
+
130
+ const type = {
131
+ name: schemaManager.getName(name, { type: 'type' }),
132
+ typedName: schemaManager.getName(name, { type: 'type' }),
133
+ file: schemaManager.getFile(options.operationName || name, { tag: options.operation?.getTags()[0]?.name }),
134
+ }
135
+
136
+ return (
137
+ <Oas.Schema key={i} name={name} value={schema} tree={tree}>
138
+ {mode === 'split' && imports.map((imp, index) => <File.Import key={index} root={file.path} path={imp.path} name={imp.name} isTypeOnly />)}
139
+ <Type
140
+ name={type.name}
141
+ typedName={type.typedName}
142
+ description={description}
143
+ tree={tree}
144
+ mapper={mapper}
145
+ enumType={enumType}
146
+ optionalType={optionalType}
147
+ keysToOmit={keysToOmit}
148
+ />
149
+ </Oas.Schema>
150
+ )
151
+ }
152
+
153
+ return (
154
+ <File baseName={file.baseName} path={file.path} meta={file.meta} banner={plugin.options.output?.banner} footer={plugin.options.output?.footer}>
155
+ {operationSchemas.map(mapOperationSchema)}
156
+
157
+ <File.Source name={combinedSchemaName} isExportable isIndexable isTypeOnly>
158
+ {printCombinedSchema({ name: combinedSchemaName, schemas, pluginManager })}
159
+ </File.Source>
160
+ </File>
161
+ )
162
+ },
163
+ Schema({ schema, options }) {
164
+ const { mapper, enumType, optionalType } = options
165
+ const {
166
+ mode,
167
+ plugin: {
168
+ options: { output },
169
+ },
170
+ } = useApp<PluginTs>()
171
+
172
+ const { getName, getImports, getFile } = useSchemaManager()
173
+ const imports = getImports(schema.tree)
174
+
175
+ if (enumType === 'asPascalConst') {
176
+ console.warn(`enumType '${enumType}' is deprecated`)
177
+ }
178
+
179
+ const type = {
180
+ name: getName(schema.name, { type: 'function' }),
181
+ typedName: getName(schema.name, { type: 'type' }),
182
+ file: getFile(schema.name),
183
+ }
184
+
185
+ return (
186
+ <File baseName={type.file.baseName} path={type.file.path} meta={type.file.meta} banner={output?.banner} footer={output?.footer}>
187
+ {mode === 'split' && imports.map((imp, index) => <File.Import key={index} root={type.file.path} path={imp.path} name={imp.name} isTypeOnly />)}
188
+ <Type
189
+ name={type.name}
190
+ typedName={type.typedName}
191
+ description={schema.value.description}
192
+ tree={schema.tree}
193
+ mapper={mapper}
194
+ enumType={enumType}
195
+ optionalType={optionalType}
196
+ />
197
+ </File>
198
+ )
199
+ },
200
+ })
@@ -2,8 +2,8 @@ import transformers from '@kubb/core/transformers'
2
2
  import * as factory from '@kubb/parser-ts/factory'
3
3
  import { isKeyword, schemaKeywords } from '@kubb/plugin-oas'
4
4
 
5
- import type { ts } from '@kubb/parser-ts'
6
5
  import type { Schema, SchemaKeywordMapper, SchemaMapper } from '@kubb/plugin-oas'
6
+ import type ts from 'typescript'
7
7
 
8
8
  export const typeKeywordMapper = {
9
9
  any: () => factory.keywordTypeNodes.any,
@@ -120,6 +120,7 @@ type ParserOptions = {
120
120
  optionalType: 'questionToken' | 'undefined' | 'questionTokenAndUndefined'
121
121
  /**
122
122
  * @default `'asConst'`
123
+ * asPascalConst is deprecated
123
124
  */
124
125
  enumType: 'enum' | 'asConst' | 'asPascalConst' | 'constEnum' | 'literal'
125
126
  keysToOmit?: string[]
package/src/plugin.ts CHANGED
@@ -3,13 +3,11 @@ import path from 'node:path'
3
3
  import { FileManager, PluginManager, createPlugin } from '@kubb/core'
4
4
  import { camelCase, pascalCase } from '@kubb/core/transformers'
5
5
  import { renderTemplate } from '@kubb/core/utils'
6
- import { pluginOasName } from '@kubb/plugin-oas'
7
-
8
- import { OperationGenerator } from './OperationGenerator.tsx'
9
- import { SchemaGenerator } from './SchemaGenerator.tsx'
6
+ import { OperationGenerator, SchemaGenerator, pluginOasName } from '@kubb/plugin-oas'
10
7
 
11
8
  import type { Plugin } from '@kubb/core'
12
9
  import type { PluginOas as SwaggerPluginOptions } from '@kubb/plugin-oas'
10
+ import { oasGenerator, typeGenerator } from './generators'
13
11
  import type { PluginTs } from './types.ts'
14
12
 
15
13
  export const pluginTsName = 'plugin-ts' satisfies PluginTs['name']
@@ -35,7 +33,10 @@ export const pluginTs = createPlugin<PluginTs>((options) => {
35
33
  return {
36
34
  name: pluginTsName,
37
35
  options: {
38
- extName: output.extName,
36
+ output: {
37
+ exportType: 'barrelNamed',
38
+ ...output,
39
+ },
39
40
  transformers,
40
41
  dateType,
41
42
  optionalType,
@@ -96,7 +97,7 @@ export const pluginTs = createPlugin<PluginTs>((options) => {
96
97
  output: output.path,
97
98
  })
98
99
 
99
- const schemaFiles = await schemaGenerator.build()
100
+ const schemaFiles = await schemaGenerator.build(...[typeGenerator, oasType === 'infer' ? oasGenerator : undefined].filter(Boolean))
100
101
  await this.addFile(...schemaFiles)
101
102
 
102
103
  const operationGenerator = new OperationGenerator(this.plugin.options, {
@@ -110,22 +111,22 @@ export const pluginTs = createPlugin<PluginTs>((options) => {
110
111
  mode,
111
112
  })
112
113
 
113
- const operationFiles = await operationGenerator.build()
114
+ const operationFiles = await operationGenerator.build(...[typeGenerator, oasType === 'infer' ? oasGenerator : undefined].filter(Boolean))
114
115
  await this.addFile(...operationFiles)
115
- },
116
- async buildEnd() {
117
- if (this.config.output.write === false) {
118
- return
119
- }
120
116
 
121
- const root = path.resolve(this.config.root, this.config.output.path)
122
-
123
- await this.fileManager.addIndexes({
124
- root,
125
- output,
126
- meta: { pluginKey: this.plugin.key },
127
- logger: this.logger,
128
- })
117
+ if (this.config.output.exportType) {
118
+ const barrelFiles = await this.fileManager.getBarrelFiles({
119
+ root,
120
+ output,
121
+ files: this.fileManager.files,
122
+ meta: {
123
+ pluginKey: this.plugin.key,
124
+ },
125
+ logger: this.logger,
126
+ })
127
+
128
+ await this.addFile(...barrelFiles)
129
+ }
129
130
  },
130
131
  }
131
132
  })
package/src/types.ts CHANGED
@@ -1,30 +1,12 @@
1
- import type { Plugin, PluginFactoryOptions, ResolveNameParams } from '@kubb/core'
2
- import type * as KubbFile from '@kubb/fs/types'
1
+ import type { Output, PluginFactoryOptions, ResolveNameParams } from '@kubb/core'
3
2
  import type { Exclude, Include, Override, ResolvePathOptions } from '@kubb/plugin-oas'
4
- import type { ts } from '@kubb/parser-ts'
3
+ import type ts from 'typescript'
5
4
 
6
5
  export type Options = {
7
- output?: {
8
- /**
9
- * Relative path to save the TypeScript types.
10
- * When output is a file it will save all models inside that file else it will create a file per schema item.
11
- * @default 'types'
12
- */
13
- path: string
14
- /**
15
- * Name to be used for the `export * as {{exportAs}} from './'`
16
- */
17
- exportAs?: string
18
- /**
19
- * Add an extension to the generated imports and exports, default it will not use an extension
20
- */
21
- extName?: KubbFile.Extname
22
- /**
23
- * Define what needs to exported, here you can also disable the export of barrel files
24
- * @default `'barrel'`
25
- */
26
- exportType?: 'barrel' | 'barrelNamed' | false
27
- }
6
+ /**
7
+ * @default 'types'
8
+ */
9
+ output?: Output
28
10
  /**
29
11
  * Group the TypeScript types based on the provided name.
30
12
  */
@@ -57,6 +39,7 @@ export type Options = {
57
39
  /**
58
40
  * Choose to use `enum` or `as const` for enums
59
41
  * @default 'asConst'
42
+ * asPascalConst is deprecated
60
43
  */
61
44
  enumType?: 'enum' | 'asConst' | 'asPascalConst' | 'constEnum' | 'literal'
62
45
  /**
@@ -108,26 +91,17 @@ export type Options = {
108
91
  }
109
92
 
110
93
  type ResolvedOptions = {
111
- extName: KubbFile.Extname | undefined
94
+ output: Output
95
+ override: NonNullable<Options['override']>
112
96
  enumType: NonNullable<Options['enumType']>
113
97
  enumSuffix: NonNullable<Options['enumSuffix']>
114
98
  dateType: NonNullable<Options['dateType']>
115
99
  unknownType: NonNullable<Options['unknownType']>
116
100
  optionalType: NonNullable<Options['optionalType']>
117
- override: NonNullable<Options['override']>
118
101
  transformers: NonNullable<Options['transformers']>
119
102
  oasType: NonNullable<Options['oasType']>
120
103
  usedEnumNames: Record<string, number>
121
104
  mapper: Record<string, any>
122
105
  }
123
106
 
124
- export type FileMeta = {
125
- pluginKey?: Plugin['key']
126
- name?: string
127
- tag?: string
128
- }
129
-
130
107
  export type PluginTs = PluginFactoryOptions<'plugin-ts', Options, ResolvedOptions, never, ResolvePathOptions>
131
-
132
- // external packages
133
- export * as Oas from './oas/index.ts'