@kubb/plugin-ts 3.0.0-alpha.0

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.
@@ -0,0 +1,263 @@
1
+ import transformers from '@kubb/core/transformers'
2
+ import * as factory from '@kubb/parser-ts/factory'
3
+ import { isKeyword, schemaKeywords } from '@kubb/plugin-oas'
4
+
5
+ import type { ts } from '@kubb/parser-ts'
6
+ import type { Schema, SchemaKeywordMapper, SchemaMapper } from '@kubb/plugin-oas'
7
+
8
+ export const typeKeywordMapper = {
9
+ any: () => factory.keywordTypeNodes.any,
10
+ unknown: () => factory.keywordTypeNodes.unknown,
11
+ number: () => factory.keywordTypeNodes.number,
12
+ integer: () => factory.keywordTypeNodes.number,
13
+ object: (nodes?: ts.TypeElement[]) => {
14
+ if (!nodes || !nodes.length) {
15
+ return factory.keywordTypeNodes.object
16
+ }
17
+
18
+ return factory.createTypeLiteralNode(nodes)
19
+ },
20
+ string: () => factory.keywordTypeNodes.string,
21
+ boolean: () => factory.keywordTypeNodes.boolean,
22
+ undefined: () => factory.keywordTypeNodes.undefined,
23
+ nullable: undefined,
24
+ null: () => factory.keywordTypeNodes.null,
25
+ nullish: undefined,
26
+ array: (nodes?: ts.TypeNode[]) => {
27
+ if (!nodes) {
28
+ return undefined
29
+ }
30
+
31
+ return factory.createArrayDeclaration({ nodes })
32
+ },
33
+ tuple: (nodes?: ts.TypeNode[]) => {
34
+ if (!nodes) {
35
+ return undefined
36
+ }
37
+
38
+ return factory.createTupleTypeNode(nodes)
39
+ },
40
+ enum: (name?: string) => {
41
+ if (!name) {
42
+ return undefined
43
+ }
44
+
45
+ return factory.createTypeReferenceNode(name, undefined)
46
+ },
47
+ union: (nodes?: ts.TypeNode[]) => {
48
+ if (!nodes) {
49
+ return undefined
50
+ }
51
+
52
+ return factory.createUnionDeclaration({
53
+ withParentheses: true,
54
+ nodes,
55
+ })
56
+ },
57
+ const: (name?: string | number, format?: 'string' | 'number') => {
58
+ if (!name) {
59
+ return undefined
60
+ }
61
+
62
+ if (format === 'number') {
63
+ return factory.createLiteralTypeNode(factory.createNumericLiteral(name))
64
+ }
65
+
66
+ return factory.createLiteralTypeNode(factory.createStringLiteral(name.toString()))
67
+ },
68
+ datetime: () => factory.keywordTypeNodes.string,
69
+ date: (type: 'date' | 'string' = 'string') =>
70
+ type === 'string' ? factory.keywordTypeNodes.string : factory.createTypeReferenceNode(factory.createIdentifier('Date')),
71
+ time: (type: 'date' | 'string' = 'string') =>
72
+ type === 'string' ? factory.keywordTypeNodes.string : factory.createTypeReferenceNode(factory.createIdentifier('Date')),
73
+ uuid: undefined,
74
+ url: undefined,
75
+ strict: undefined,
76
+ default: undefined,
77
+ and: (nodes?: ts.TypeNode[]) => {
78
+ if (!nodes) {
79
+ return undefined
80
+ }
81
+
82
+ return factory.createIntersectionDeclaration({
83
+ withParentheses: true,
84
+ nodes,
85
+ })
86
+ },
87
+ describe: undefined,
88
+ min: undefined,
89
+ max: undefined,
90
+ optional: undefined,
91
+ matches: undefined,
92
+ email: undefined,
93
+ firstName: undefined,
94
+ lastName: undefined,
95
+ password: undefined,
96
+ phone: undefined,
97
+ readOnly: undefined,
98
+ ref: (propertyName?: string) => {
99
+ if (!propertyName) {
100
+ return undefined
101
+ }
102
+
103
+ return factory.createTypeReferenceNode(propertyName, undefined)
104
+ },
105
+ blob: () => factory.createTypeReferenceNode('Blob', []),
106
+ deprecated: undefined,
107
+ example: undefined,
108
+ schema: undefined,
109
+ catchall: undefined,
110
+ name: undefined,
111
+ } satisfies SchemaMapper<ts.Node | null | undefined>
112
+
113
+ type ParserOptions = {
114
+ name: string
115
+ typeName?: string
116
+ description?: string
117
+ /**
118
+ * @default `'questionToken'`
119
+ */
120
+ optionalType: 'questionToken' | 'undefined' | 'questionTokenAndUndefined'
121
+ /**
122
+ * @default `'asConst'`
123
+ */
124
+ enumType: 'enum' | 'asConst' | 'asPascalConst' | 'constEnum' | 'literal'
125
+ keysToOmit?: string[]
126
+ mapper?: Record<string, ts.PropertySignature>
127
+ }
128
+
129
+ export function parse(parent: Schema | undefined, current: Schema, options: ParserOptions): ts.Node | null | undefined {
130
+ const value = typeKeywordMapper[current.keyword as keyof typeof typeKeywordMapper]
131
+
132
+ if (!value) {
133
+ return undefined
134
+ }
135
+
136
+ if (isKeyword(current, schemaKeywords.union)) {
137
+ return typeKeywordMapper.union(current.args.map((schema) => parse(current, schema, options)).filter(Boolean) as ts.TypeNode[])
138
+ }
139
+
140
+ if (isKeyword(current, schemaKeywords.and)) {
141
+ return typeKeywordMapper.and(current.args.map((schema) => parse(current, schema, options)).filter(Boolean) as ts.TypeNode[])
142
+ }
143
+
144
+ if (isKeyword(current, schemaKeywords.array)) {
145
+ return typeKeywordMapper.array(current.args.items.map((schema) => parse(current, schema, options)).filter(Boolean) as ts.TypeNode[])
146
+ }
147
+
148
+ if (isKeyword(current, schemaKeywords.enum)) {
149
+ return typeKeywordMapper.enum(current.args.typeName)
150
+ }
151
+
152
+ if (isKeyword(current, schemaKeywords.ref)) {
153
+ return typeKeywordMapper.ref(current.args.name)
154
+ }
155
+
156
+ if (isKeyword(current, schemaKeywords.blob)) {
157
+ return value()
158
+ }
159
+
160
+ if (isKeyword(current, schemaKeywords.tuple)) {
161
+ return typeKeywordMapper.tuple(current.args.items.map((schema) => parse(current, schema, options)).filter(Boolean) as ts.TypeNode[])
162
+ }
163
+
164
+ if (isKeyword(current, schemaKeywords.const)) {
165
+ return typeKeywordMapper.const(current.args.name, current.args.format)
166
+ }
167
+
168
+ if (isKeyword(current, schemaKeywords.object)) {
169
+ const properties = Object.entries(current.args?.properties || {})
170
+ .filter((item) => {
171
+ const schemas = item[1]
172
+ return schemas && typeof schemas.map === 'function'
173
+ })
174
+ .map(([name, schemas]) => {
175
+ const nameSchema = schemas.find((schema) => schema.keyword === schemaKeywords.name) as SchemaKeywordMapper['name']
176
+ const mappedName = nameSchema?.args || name
177
+
178
+ // custom mapper(pluginOptions)
179
+ if (options.mapper?.[mappedName]) {
180
+ return options.mapper?.[mappedName]
181
+ }
182
+
183
+ const isNullish = schemas.some((schema) => schema.keyword === schemaKeywords.nullish)
184
+ const isNullable = schemas.some((schema) => schema.keyword === schemaKeywords.nullable)
185
+ const isOptional = schemas.some((schema) => schema.keyword === schemaKeywords.optional)
186
+ const isReadonly = schemas.some((schema) => schema.keyword === schemaKeywords.readOnly)
187
+ const describeSchema = schemas.find((schema) => schema.keyword === schemaKeywords.describe) as SchemaKeywordMapper['describe'] | undefined
188
+ const deprecatedSchema = schemas.find((schema) => schema.keyword === schemaKeywords.deprecated) as SchemaKeywordMapper['deprecated'] | undefined
189
+ const defaultSchema = schemas.find((schema) => schema.keyword === schemaKeywords.default) as SchemaKeywordMapper['default'] | undefined
190
+ const exampleSchema = schemas.find((schema) => schema.keyword === schemaKeywords.example) as SchemaKeywordMapper['example'] | undefined
191
+ const schemaSchema = schemas.find((schema) => schema.keyword === schemaKeywords.schema) as SchemaKeywordMapper['schema'] | undefined
192
+
193
+ let type = schemas.map((schema) => parse(current, schema, options)).filter(Boolean)[0] as ts.TypeNode
194
+
195
+ if (isNullable) {
196
+ type = factory.createUnionDeclaration({
197
+ nodes: [type, factory.keywordTypeNodes.null],
198
+ }) as ts.TypeNode
199
+ }
200
+
201
+ if (isNullish && ['undefined', 'questionTokenAndUndefined'].includes(options.optionalType as string)) {
202
+ type = factory.createUnionDeclaration({
203
+ nodes: [type, factory.keywordTypeNodes.undefined],
204
+ }) as ts.TypeNode
205
+ }
206
+
207
+ if (isOptional && ['undefined', 'questionTokenAndUndefined'].includes(options.optionalType as string)) {
208
+ type = factory.createUnionDeclaration({
209
+ nodes: [type, factory.keywordTypeNodes.undefined],
210
+ }) as ts.TypeNode
211
+ }
212
+
213
+ const propertySignature = factory.createPropertySignature({
214
+ questionToken: isOptional || isNullish ? ['questionToken', 'questionTokenAndUndefined'].includes(options.optionalType as string) : false,
215
+ name: mappedName,
216
+ type,
217
+ readOnly: isReadonly,
218
+ })
219
+
220
+ return factory.appendJSDocToNode({
221
+ node: propertySignature,
222
+ comments: [
223
+ describeSchema ? `@description ${transformers.jsStringEscape(describeSchema.args)}` : undefined,
224
+ deprecatedSchema ? '@deprecated' : undefined,
225
+ defaultSchema ? `@default ${defaultSchema.args}` : undefined,
226
+ exampleSchema ? `@example ${exampleSchema.args}` : undefined,
227
+ schemaSchema?.args?.type || schemaSchema?.args?.format
228
+ ? [`@type ${schemaSchema?.args?.type || 'unknown'}${!isOptional ? '' : ' | undefined'}`, schemaSchema?.args?.format].filter(Boolean).join(', ')
229
+ : undefined,
230
+ ].filter(Boolean),
231
+ })
232
+ })
233
+
234
+ const additionalProperties = current.args?.additionalProperties?.length
235
+ ? factory.createIndexSignature(
236
+ current.args.additionalProperties
237
+ .map((schema) => parse(current, schema, options))
238
+ .filter(Boolean)
239
+ .at(0) as ts.TypeNode,
240
+ )
241
+ : undefined
242
+
243
+ return typeKeywordMapper.object([...properties, additionalProperties].filter(Boolean))
244
+ }
245
+
246
+ if (isKeyword(current, schemaKeywords.datetime)) {
247
+ return typeKeywordMapper.datetime()
248
+ }
249
+
250
+ if (isKeyword(current, schemaKeywords.date)) {
251
+ return typeKeywordMapper.date(current.args.type)
252
+ }
253
+
254
+ if (isKeyword(current, schemaKeywords.time)) {
255
+ return typeKeywordMapper.time(current.args.type)
256
+ }
257
+
258
+ if (current.keyword in typeKeywordMapper) {
259
+ return value()
260
+ }
261
+
262
+ return undefined
263
+ }
package/src/plugin.ts ADDED
@@ -0,0 +1,138 @@
1
+ import path from 'node:path'
2
+
3
+ import { FileManager, PluginManager, createPlugin } from '@kubb/core'
4
+ import { camelCase, pascalCase } from '@kubb/core/transformers'
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'
10
+
11
+ import type { Plugin } from '@kubb/core'
12
+ import type { PluginOas as SwaggerPluginOptions } from '@kubb/plugin-oas'
13
+ import type { PluginTs } from './types.ts'
14
+
15
+ export const pluginTsName = 'plugin-ts' satisfies PluginTs['name']
16
+
17
+ export const pluginTs = createPlugin<PluginTs>((options) => {
18
+ const {
19
+ output = { path: 'types' },
20
+ group,
21
+ exclude = [],
22
+ include,
23
+ override = [],
24
+ enumType = 'asConst',
25
+ enumSuffix = '',
26
+ dateType = 'string',
27
+ unknownType = 'any',
28
+ optionalType = 'questionToken',
29
+ transformers = {},
30
+ oasType = false,
31
+ mapper = {},
32
+ } = options
33
+ const template = group?.output ? group.output : `${output.path}/{{tag}}Controller`
34
+
35
+ return {
36
+ name: pluginTsName,
37
+ options: {
38
+ extName: output.extName,
39
+ transformers,
40
+ dateType,
41
+ optionalType,
42
+ oasType,
43
+ enumType,
44
+ enumSuffix,
45
+ // keep the used enumnames between SchemaGenerator and OperationGenerator per plugin(pluginKey)
46
+ usedEnumNames: {},
47
+ unknownType,
48
+ override,
49
+ mapper,
50
+ },
51
+ pre: [pluginOasName],
52
+ resolvePath(baseName, pathMode, options) {
53
+ const root = path.resolve(this.config.root, this.config.output.path)
54
+ const mode = pathMode ?? FileManager.getMode(path.resolve(root, output.path))
55
+
56
+ if (mode === 'single') {
57
+ /**
58
+ * when output is a file then we will always append to the same file(output file), see fileManager.addOrAppend
59
+ * Other plugins then need to call addOrAppend instead of just add from the fileManager class
60
+ */
61
+ return path.resolve(root, output.path)
62
+ }
63
+
64
+ if (options?.tag && group?.type === 'tag') {
65
+ const tag = camelCase(options.tag)
66
+
67
+ return path.resolve(root, renderTemplate(template, { tag }), baseName)
68
+ }
69
+
70
+ return path.resolve(root, output.path, baseName)
71
+ },
72
+ resolveName(name, type) {
73
+ const resolvedName = pascalCase(name, { isFile: type === 'file' })
74
+
75
+ if (type) {
76
+ return transformers?.name?.(resolvedName, type) || resolvedName
77
+ }
78
+
79
+ return resolvedName
80
+ },
81
+ async writeFile(path, source) {
82
+ if (!path.endsWith('.ts') || !source) {
83
+ return
84
+ }
85
+
86
+ return this.fileManager.write(path, source, { sanity: false })
87
+ },
88
+ async buildStart() {
89
+ const [swaggerPlugin]: [Plugin<SwaggerPluginOptions>] = PluginManager.getDependedPlugins<SwaggerPluginOptions>(this.plugins, [pluginOasName])
90
+
91
+ const oas = await swaggerPlugin.api.getOas()
92
+ const root = path.resolve(this.config.root, this.config.output.path)
93
+ const mode = FileManager.getMode(path.resolve(root, output.path))
94
+
95
+ const schemaGenerator = new SchemaGenerator(this.plugin.options, {
96
+ oas,
97
+ pluginManager: this.pluginManager,
98
+ plugin: this.plugin,
99
+ contentType: swaggerPlugin.api.contentType,
100
+ include: undefined,
101
+ override,
102
+ mode,
103
+ output: output.path,
104
+ })
105
+
106
+ const schemaFiles = await schemaGenerator.build()
107
+ await this.addFile(...schemaFiles)
108
+
109
+ const operationGenerator = new OperationGenerator(this.plugin.options, {
110
+ oas,
111
+ pluginManager: this.pluginManager,
112
+ plugin: this.plugin,
113
+ contentType: swaggerPlugin.api.contentType,
114
+ exclude,
115
+ include,
116
+ override,
117
+ mode,
118
+ })
119
+
120
+ const operationFiles = await operationGenerator.build()
121
+ await this.addFile(...operationFiles)
122
+ },
123
+ async buildEnd() {
124
+ if (this.config.output.write === false) {
125
+ return
126
+ }
127
+
128
+ const root = path.resolve(this.config.root, this.config.output.path)
129
+
130
+ await this.fileManager.addIndexes({
131
+ root,
132
+ output,
133
+ meta: { pluginKey: this.plugin.key },
134
+ logger: this.logger,
135
+ })
136
+ },
137
+ }
138
+ })
package/src/types.ts ADDED
@@ -0,0 +1,138 @@
1
+ import type { Plugin, PluginFactoryOptions, ResolveNameParams } from '@kubb/core'
2
+ import type * as KubbFile from '@kubb/fs/types'
3
+ import type { Exclude, Include, Override, ResolvePathOptions } from '@kubb/plugin-oas'
4
+ import type { ts } from '@kubb/parser-ts'
5
+
6
+ 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
+ }
28
+ /**
29
+ * Group the TypeScript types based on the provided name.
30
+ */
31
+ group?: {
32
+ /**
33
+ * Tag will group based on the operation tag inside the Swagger file.
34
+ */
35
+ type: 'tag'
36
+ /**
37
+ * Relative path to save the grouped TypeScript Types.
38
+ *
39
+ * `{{tag}}` will be replaced by the current tagName.
40
+ * @example `${output}/{{tag}}Controller` => `models/PetController`
41
+ * @default `${output}/{{tag}}Controller`
42
+ */
43
+ output?: string
44
+ }
45
+ /**
46
+ * Array containing exclude parameters to exclude/skip tags/operations/methods/paths.
47
+ */
48
+ exclude?: Array<Exclude>
49
+ /**
50
+ * Array containing include parameters to include tags/operations/methods/paths.
51
+ */
52
+ include?: Array<Include>
53
+ /**
54
+ * Array containing override parameters to override `options` based on tags/operations/methods/paths.
55
+ */
56
+ override?: Array<Override<ResolvedOptions>>
57
+ /**
58
+ * Choose to use `enum` or `as const` for enums
59
+ * @default 'asConst'
60
+ */
61
+ enumType?: 'enum' | 'asConst' | 'asPascalConst' | 'constEnum' | 'literal'
62
+ /**
63
+ * Set a suffix for the generated enums.
64
+ * @default ''
65
+ * Default will be `'enum'` in version 3 of Kubb
66
+ */
67
+ enumSuffix?: string
68
+
69
+ /**
70
+ * Choose to use `date` or `datetime` as JavaScript `Date` instead of `string`.
71
+ * @default 'string'
72
+ */
73
+ dateType?: 'string' | 'date'
74
+ /**
75
+ * Which type to use when the Swagger/OpenAPI file is not providing more information.
76
+ * @default 'any'
77
+ */
78
+ unknownType?: 'any' | 'unknown'
79
+ /**
80
+ * Choose what to use as mode for an optional value.
81
+ * @examples 'questionToken': type?: string
82
+ * @examples 'undefined': type: string | undefined
83
+ * @examples 'questionTokenAndUndefined': type?: string | undefined
84
+ * @default 'questionToken'
85
+ */
86
+ optionalType?: 'questionToken' | 'undefined' | 'questionTokenAndUndefined'
87
+ transformers?: {
88
+ /**
89
+ * Customize the names based on the type that is provided by the plugin.
90
+ */
91
+ name?: (name: ResolveNameParams['name'], type?: ResolveNameParams['type']) => string
92
+ }
93
+ /**
94
+ * Export an Oas object as Oas type with `import type { Infer } from '@kubb/oas'`
95
+ */
96
+ oasType?: 'infer' | false
97
+ /**
98
+ * @example
99
+ * Use https://ts-ast-viewer.com to generate factory code(see createPropertySignature)
100
+ * category: factory.createPropertySignature(
101
+ * undefined,
102
+ * factory.createIdentifier("category"),
103
+ * factory.createToken(ts.SyntaxKind.QuestionToken),
104
+ * factory.createKeywordTypeNode(ts.SyntaxKind.StringKeyword)
105
+ * )
106
+ */
107
+ mapper?: Record<string, ts.PropertySignature>
108
+ }
109
+
110
+ type ResolvedOptions = {
111
+ extName: KubbFile.Extname | undefined
112
+ enumType: NonNullable<Options['enumType']>
113
+ enumSuffix: NonNullable<Options['enumSuffix']>
114
+ dateType: NonNullable<Options['dateType']>
115
+ unknownType: NonNullable<Options['unknownType']>
116
+ optionalType: NonNullable<Options['optionalType']>
117
+ override: NonNullable<Options['override']>
118
+ transformers: NonNullable<Options['transformers']>
119
+ oasType: NonNullable<Options['oasType']>
120
+ usedEnumNames: Record<string, number>
121
+ mapper: Record<string, any>
122
+ }
123
+
124
+ export type FileMeta = {
125
+ pluginKey?: Plugin['key']
126
+ name?: string
127
+ tag?: string
128
+ }
129
+
130
+ export type PluginTs = PluginFactoryOptions<'plugin-ts', Options, ResolvedOptions, never, ResolvePathOptions>
131
+
132
+ declare module '@kubb/core' {
133
+ export interface _Register {
134
+ ['@kubb/plugin-ts']: PluginTs
135
+ }
136
+ }
137
+ // external packages
138
+ export * as Oas from './oas/index.ts'