@kubb/swagger-ts 1.15.0-canary.20231112T135108 → 2.0.0-alpha.10
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/README.md +1 -1
- package/dist/index.cjs +432 -316
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +29 -91
- package/dist/index.d.ts +29 -91
- package/dist/index.js +413 -308
- package/dist/index.js.map +1 -1
- package/package.json +13 -12
- package/src/builders/TypeBuilder.ts +93 -0
- package/src/builders/index.ts +1 -0
- package/src/generators/OperationGenerator.ts +211 -0
- package/src/generators/TypeGenerator.ts +378 -0
- package/src/generators/index.ts +2 -0
- package/src/index.ts +6 -0
- package/src/plugin.ts +229 -0
- package/src/types.ts +82 -0
@@ -0,0 +1,378 @@
|
|
1
|
+
import { SchemaGenerator } from '@kubb/core'
|
2
|
+
import { getUniqueName } from '@kubb/core/utils'
|
3
|
+
import * as factory from '@kubb/parser/factory'
|
4
|
+
import { isReference } from '@kubb/swagger/utils'
|
5
|
+
|
6
|
+
import { camelCase } from 'change-case'
|
7
|
+
|
8
|
+
import type { PluginContext } from '@kubb/core'
|
9
|
+
import type { ts } from '@kubb/parser'
|
10
|
+
import type { OpenAPIV3, Refs } from '@kubb/swagger'
|
11
|
+
import type { Options as CaseOptions } from 'change-case'
|
12
|
+
|
13
|
+
// based on https://github.com/cellular/oazapfts/blob/7ba226ebb15374e8483cc53e7532f1663179a22c/src/codegen/generate.ts#L398
|
14
|
+
|
15
|
+
type Options = {
|
16
|
+
usedEnumNames: Record<string, number>
|
17
|
+
|
18
|
+
withJSDocs?: boolean
|
19
|
+
resolveName: PluginContext['resolveName']
|
20
|
+
enumType: 'enum' | 'asConst' | 'asPascalConst'
|
21
|
+
dateType: 'string' | 'date'
|
22
|
+
optionalType: 'questionToken' | 'undefined' | 'questionTokenAndUndefined'
|
23
|
+
}
|
24
|
+
export class TypeGenerator extends SchemaGenerator<Options, OpenAPIV3.SchemaObject, ts.Node[]> {
|
25
|
+
refs: Refs = {}
|
26
|
+
|
27
|
+
extraNodes: ts.Node[] = []
|
28
|
+
|
29
|
+
aliases: ts.TypeAliasDeclaration[] = []
|
30
|
+
|
31
|
+
// Keep track of already used type aliases
|
32
|
+
#usedAliasNames: Record<string, number> = {}
|
33
|
+
|
34
|
+
#caseOptions: CaseOptions = {
|
35
|
+
delimiter: '',
|
36
|
+
stripRegexp: /[^A-Z0-9$]/gi,
|
37
|
+
}
|
38
|
+
|
39
|
+
constructor(
|
40
|
+
options: Options = {
|
41
|
+
usedEnumNames: {},
|
42
|
+
withJSDocs: true,
|
43
|
+
resolveName: ({ name }) => name,
|
44
|
+
enumType: 'asConst',
|
45
|
+
dateType: 'string',
|
46
|
+
optionalType: 'questionToken',
|
47
|
+
},
|
48
|
+
) {
|
49
|
+
super(options)
|
50
|
+
|
51
|
+
return this
|
52
|
+
}
|
53
|
+
|
54
|
+
build({
|
55
|
+
schema,
|
56
|
+
baseName,
|
57
|
+
description,
|
58
|
+
keysToOmit,
|
59
|
+
}: {
|
60
|
+
schema: OpenAPIV3.SchemaObject
|
61
|
+
baseName: string
|
62
|
+
description?: string
|
63
|
+
keysToOmit?: string[]
|
64
|
+
}): ts.Node[] {
|
65
|
+
const nodes: ts.Node[] = []
|
66
|
+
const type = this.#getTypeFromSchema(schema, baseName)
|
67
|
+
|
68
|
+
if (!type) {
|
69
|
+
return this.extraNodes
|
70
|
+
}
|
71
|
+
|
72
|
+
const node = factory.createTypeAliasDeclaration({
|
73
|
+
modifiers: [factory.modifiers.export],
|
74
|
+
name: this.options.resolveName({ name: baseName }) || baseName,
|
75
|
+
type: keysToOmit?.length ? factory.createOmitDeclaration({ keys: keysToOmit, type, nonNullable: true }) : type,
|
76
|
+
})
|
77
|
+
|
78
|
+
if (description) {
|
79
|
+
nodes.push(
|
80
|
+
factory.appendJSDocToNode({
|
81
|
+
node,
|
82
|
+
comments: [`@description ${description}`],
|
83
|
+
}),
|
84
|
+
)
|
85
|
+
} else {
|
86
|
+
nodes.push(node)
|
87
|
+
}
|
88
|
+
|
89
|
+
// filter out if the export name is the same as one that we already defined in extraNodes(see enum)
|
90
|
+
const filterdNodes = nodes.filter(
|
91
|
+
(node: ts.Node) =>
|
92
|
+
!this.extraNodes.some(
|
93
|
+
(extraNode: ts.Node) => (extraNode as ts.TypeAliasDeclaration)?.name?.escapedText === (node as ts.TypeAliasDeclaration)?.name?.escapedText,
|
94
|
+
),
|
95
|
+
)
|
96
|
+
|
97
|
+
return [...this.extraNodes, ...filterdNodes]
|
98
|
+
}
|
99
|
+
|
100
|
+
/**
|
101
|
+
* Creates a type node from a given schema.
|
102
|
+
* Delegates to getBaseTypeFromSchema internally and
|
103
|
+
* optionally adds a union with null.
|
104
|
+
*/
|
105
|
+
#getTypeFromSchema(schema?: OpenAPIV3.SchemaObject, name?: string): ts.TypeNode | null {
|
106
|
+
const type = this.#getBaseTypeFromSchema(schema, name)
|
107
|
+
|
108
|
+
if (!type) {
|
109
|
+
return null
|
110
|
+
}
|
111
|
+
|
112
|
+
if (schema && !schema.nullable) {
|
113
|
+
return type
|
114
|
+
}
|
115
|
+
|
116
|
+
return factory.createUnionDeclaration({ nodes: [type, factory.keywordTypeNodes.null] })
|
117
|
+
}
|
118
|
+
|
119
|
+
/**
|
120
|
+
* Recursively creates a type literal with the given props.
|
121
|
+
*/
|
122
|
+
#getTypeFromProperties(baseSchema?: OpenAPIV3.SchemaObject, baseName?: string) {
|
123
|
+
const { optionalType } = this.options
|
124
|
+
const properties = baseSchema?.properties || {}
|
125
|
+
const required = baseSchema?.required
|
126
|
+
const additionalProperties = baseSchema?.additionalProperties
|
127
|
+
|
128
|
+
const members: Array<ts.TypeElement | null> = Object.keys(properties).map((name) => {
|
129
|
+
const schema = properties[name] as OpenAPIV3.SchemaObject
|
130
|
+
|
131
|
+
const isRequired = required && required.includes(name)
|
132
|
+
let type = this.#getTypeFromSchema(schema, this.options.resolveName({ name: `${baseName || ''} ${name}` }))
|
133
|
+
|
134
|
+
if (!type) {
|
135
|
+
return null
|
136
|
+
}
|
137
|
+
|
138
|
+
if (!isRequired && ['undefined', 'questionTokenAndUndefined'].includes(optionalType)) {
|
139
|
+
type = factory.createUnionDeclaration({ nodes: [type, factory.keywordTypeNodes.undefined] })
|
140
|
+
}
|
141
|
+
const propertySignature = factory.createPropertySignature({
|
142
|
+
questionToken: ['questionToken', 'questionTokenAndUndefined'].includes(optionalType) && !isRequired,
|
143
|
+
name,
|
144
|
+
type: type as ts.TypeNode,
|
145
|
+
readOnly: schema.readOnly,
|
146
|
+
})
|
147
|
+
if (this.options.withJSDocs) {
|
148
|
+
return factory.appendJSDocToNode({
|
149
|
+
node: propertySignature,
|
150
|
+
comments: [
|
151
|
+
schema.description ? `@description ${schema.description}` : undefined,
|
152
|
+
schema.type ? `@type ${schema.type}${isRequired ? '' : ' | undefined'} ${schema.format || ''}` : undefined,
|
153
|
+
schema.example ? `@example ${schema.example as string}` : undefined,
|
154
|
+
schema.deprecated ? `@deprecated` : undefined,
|
155
|
+
schema.default !== undefined && typeof schema.default === 'string' ? `@default '${schema.default}'` : undefined,
|
156
|
+
schema.default !== undefined && typeof schema.default !== 'string' ? `@default ${schema.default as string}` : undefined,
|
157
|
+
].filter(Boolean),
|
158
|
+
})
|
159
|
+
}
|
160
|
+
|
161
|
+
return propertySignature
|
162
|
+
})
|
163
|
+
if (additionalProperties) {
|
164
|
+
const type = additionalProperties === true ? factory.keywordTypeNodes.any : this.#getTypeFromSchema(additionalProperties as OpenAPIV3.SchemaObject)
|
165
|
+
|
166
|
+
if (type) {
|
167
|
+
members.push(factory.createIndexSignature(type))
|
168
|
+
}
|
169
|
+
}
|
170
|
+
return factory.createTypeLiteralNode(members.filter(Boolean))
|
171
|
+
}
|
172
|
+
|
173
|
+
/**
|
174
|
+
* Create a type alias for the schema referenced by the given ReferenceObject
|
175
|
+
*/
|
176
|
+
#getRefAlias(obj: OpenAPIV3.ReferenceObject, _baseName?: string) {
|
177
|
+
const { $ref } = obj
|
178
|
+
let ref = this.refs[$ref]
|
179
|
+
|
180
|
+
if (ref) {
|
181
|
+
return factory.createTypeReferenceNode(ref.propertyName, undefined)
|
182
|
+
}
|
183
|
+
|
184
|
+
const originalName = getUniqueName($ref.replace(/.+\//, ''), this.#usedAliasNames)
|
185
|
+
const propertyName = this.options.resolveName({ name: originalName }) || originalName
|
186
|
+
|
187
|
+
ref = this.refs[$ref] = {
|
188
|
+
propertyName,
|
189
|
+
originalName,
|
190
|
+
}
|
191
|
+
|
192
|
+
return factory.createTypeReferenceNode(ref.propertyName, undefined)
|
193
|
+
}
|
194
|
+
|
195
|
+
/**
|
196
|
+
* This is the very core of the OpenAPI to TS conversion - it takes a
|
197
|
+
* schema and returns the appropriate type.
|
198
|
+
*/
|
199
|
+
#getBaseTypeFromSchema(schema: OpenAPIV3.SchemaObject | OpenAPIV3.ReferenceObject | undefined, baseName?: string): ts.TypeNode | null {
|
200
|
+
if (!schema) {
|
201
|
+
return factory.keywordTypeNodes.any
|
202
|
+
}
|
203
|
+
|
204
|
+
if (isReference(schema)) {
|
205
|
+
return this.#getRefAlias(schema, baseName)
|
206
|
+
}
|
207
|
+
|
208
|
+
if (schema.oneOf) {
|
209
|
+
// union
|
210
|
+
const schemaWithoutOneOf = { ...schema, oneOf: undefined }
|
211
|
+
|
212
|
+
const union = factory.createUnionDeclaration({
|
213
|
+
withParentheses: true,
|
214
|
+
nodes: schema.oneOf
|
215
|
+
.map((item: OpenAPIV3.SchemaObject | OpenAPIV3.ReferenceObject) => {
|
216
|
+
return this.#getBaseTypeFromSchema(item)
|
217
|
+
})
|
218
|
+
.filter((item) => {
|
219
|
+
return item && item !== factory.keywordTypeNodes.any
|
220
|
+
}) as Array<ts.TypeNode>,
|
221
|
+
})
|
222
|
+
|
223
|
+
if (schemaWithoutOneOf.properties) {
|
224
|
+
return factory.createIntersectionDeclaration({
|
225
|
+
nodes: [this.#getBaseTypeFromSchema(schemaWithoutOneOf, baseName), union].filter(Boolean),
|
226
|
+
})
|
227
|
+
}
|
228
|
+
|
229
|
+
return union
|
230
|
+
}
|
231
|
+
|
232
|
+
if (schema.anyOf) {
|
233
|
+
const schemaWithoutAnyOf = { ...schema, anyOf: undefined }
|
234
|
+
|
235
|
+
const union = factory.createUnionDeclaration({
|
236
|
+
withParentheses: true,
|
237
|
+
nodes: schema.anyOf
|
238
|
+
.map((item: OpenAPIV3.SchemaObject | OpenAPIV3.ReferenceObject) => {
|
239
|
+
return this.#getBaseTypeFromSchema(item)
|
240
|
+
})
|
241
|
+
.filter((item) => {
|
242
|
+
return item && item !== factory.keywordTypeNodes.any
|
243
|
+
}) as Array<ts.TypeNode>,
|
244
|
+
})
|
245
|
+
|
246
|
+
if (schemaWithoutAnyOf.properties) {
|
247
|
+
return factory.createIntersectionDeclaration({
|
248
|
+
nodes: [this.#getBaseTypeFromSchema(schemaWithoutAnyOf, baseName), union].filter(Boolean),
|
249
|
+
})
|
250
|
+
}
|
251
|
+
|
252
|
+
return union
|
253
|
+
}
|
254
|
+
if (schema.allOf) {
|
255
|
+
// intersection/add
|
256
|
+
const schemaWithoutAllOf = { ...schema, allOf: undefined }
|
257
|
+
|
258
|
+
const and = factory.createIntersectionDeclaration({
|
259
|
+
withParentheses: true,
|
260
|
+
nodes: schema.allOf
|
261
|
+
.map((item: OpenAPIV3.SchemaObject | OpenAPIV3.ReferenceObject) => {
|
262
|
+
return this.#getBaseTypeFromSchema(item)
|
263
|
+
})
|
264
|
+
.filter((item) => {
|
265
|
+
return item && item !== factory.keywordTypeNodes.any
|
266
|
+
}) as Array<ts.TypeNode>,
|
267
|
+
})
|
268
|
+
|
269
|
+
if (schemaWithoutAllOf.properties) {
|
270
|
+
return factory.createIntersectionDeclaration({
|
271
|
+
nodes: [this.#getBaseTypeFromSchema(schemaWithoutAllOf, baseName), and].filter(Boolean),
|
272
|
+
})
|
273
|
+
}
|
274
|
+
|
275
|
+
return and
|
276
|
+
}
|
277
|
+
|
278
|
+
/**
|
279
|
+
* Enum will be defined outside the baseType(hints the baseName check)
|
280
|
+
*/
|
281
|
+
if (schema.enum && baseName) {
|
282
|
+
const enumName = getUniqueName(baseName, this.options.usedEnumNames)
|
283
|
+
|
284
|
+
let enums: [key: string, value: string | number][] = [...new Set(schema.enum)].map((key) => [key, key])
|
285
|
+
|
286
|
+
if ('x-enumNames' in schema) {
|
287
|
+
enums = [...new Set(schema['x-enumNames'] as string[])].map((key: string, index) => {
|
288
|
+
return [key, schema.enum?.[index]]
|
289
|
+
})
|
290
|
+
}
|
291
|
+
|
292
|
+
this.extraNodes.push(
|
293
|
+
...factory.createEnumDeclaration({
|
294
|
+
name: camelCase(enumName, this.#caseOptions),
|
295
|
+
typeName: this.options.resolveName({ name: enumName }),
|
296
|
+
enums,
|
297
|
+
type: this.options.enumType,
|
298
|
+
}),
|
299
|
+
)
|
300
|
+
return factory.createTypeReferenceNode(this.options.resolveName({ name: enumName }), undefined)
|
301
|
+
}
|
302
|
+
|
303
|
+
if (schema.enum) {
|
304
|
+
return factory.createUnionDeclaration({
|
305
|
+
nodes: schema.enum.map((name: string) => {
|
306
|
+
return factory.createLiteralTypeNode(typeof name === 'number' ? factory.createNumericLiteral(name) : factory.createStringLiteral(`${name}`))
|
307
|
+
}) as unknown as Array<ts.TypeNode>,
|
308
|
+
})
|
309
|
+
}
|
310
|
+
|
311
|
+
if ('items' in schema) {
|
312
|
+
// items -> array
|
313
|
+
const node = this.#getTypeFromSchema(schema.items as OpenAPIV3.SchemaObject, baseName)
|
314
|
+
if (node) {
|
315
|
+
return factory.createArrayTypeNode(node)
|
316
|
+
}
|
317
|
+
}
|
318
|
+
/**
|
319
|
+
* OpenAPI 3.1
|
320
|
+
* @link https://json-schema.org/understanding-json-schema/reference/array.html#tuple-validation
|
321
|
+
*/
|
322
|
+
if ('prefixItems' in schema) {
|
323
|
+
const prefixItems = schema.prefixItems as OpenAPIV3.SchemaObject[]
|
324
|
+
|
325
|
+
return factory.createTupleDeclaration({
|
326
|
+
nodes: prefixItems.map((item) => {
|
327
|
+
// no baseType so we can fall back on an union when using enum
|
328
|
+
return this.#getBaseTypeFromSchema(item, undefined)
|
329
|
+
}) as Array<ts.TypeNode>,
|
330
|
+
})
|
331
|
+
}
|
332
|
+
|
333
|
+
if (schema.properties || schema.additionalProperties) {
|
334
|
+
// properties -> literal type
|
335
|
+
return this.#getTypeFromProperties(schema, baseName)
|
336
|
+
}
|
337
|
+
|
338
|
+
if (schema.type) {
|
339
|
+
if (Array.isArray(schema.type)) {
|
340
|
+
// OPENAPI v3.1.0: https://www.openapis.org/blog/2021/02/16/migrating-from-openapi-3-0-to-3-1-0
|
341
|
+
const [type, nullable] = schema.type as Array<OpenAPIV3.NonArraySchemaObjectType>
|
342
|
+
|
343
|
+
return factory.createUnionDeclaration({
|
344
|
+
nodes: [
|
345
|
+
this.#getBaseTypeFromSchema(
|
346
|
+
{
|
347
|
+
...schema,
|
348
|
+
type,
|
349
|
+
},
|
350
|
+
baseName,
|
351
|
+
),
|
352
|
+
nullable ? factory.createLiteralTypeNode(factory.createNull()) : undefined,
|
353
|
+
].filter(Boolean),
|
354
|
+
})
|
355
|
+
}
|
356
|
+
|
357
|
+
if (this.options.dateType === 'date' && ['date', 'date-time'].some((item) => item === schema.format)) {
|
358
|
+
return factory.createTypeReferenceNode(factory.createIdentifier('Date'))
|
359
|
+
}
|
360
|
+
|
361
|
+
// string, boolean, null, number
|
362
|
+
if (schema.type in factory.keywordTypeNodes) {
|
363
|
+
return factory.keywordTypeNodes[schema.type as keyof typeof factory.keywordTypeNodes]
|
364
|
+
}
|
365
|
+
}
|
366
|
+
|
367
|
+
if (schema.format === 'binary') {
|
368
|
+
return factory.createTypeReferenceNode('Blob', [])
|
369
|
+
}
|
370
|
+
|
371
|
+
// detect assertion "const" and define the type property as a Literal
|
372
|
+
if ('const' in schema && schema['const'] !== undefined && typeof schema['const'] === 'string') {
|
373
|
+
return factory.createLiteralTypeNode(factory.createStringLiteral(schema['const']))
|
374
|
+
}
|
375
|
+
|
376
|
+
return factory.keywordTypeNodes.any
|
377
|
+
}
|
378
|
+
}
|
package/src/index.ts
ADDED
package/src/plugin.ts
ADDED
@@ -0,0 +1,229 @@
|
|
1
|
+
import path from 'node:path'
|
2
|
+
|
3
|
+
import { createPlugin, FileManager, PluginManager } from '@kubb/core'
|
4
|
+
import { getRelativePath, renderTemplate } from '@kubb/core/utils'
|
5
|
+
import { pluginName as swaggerPluginName } from '@kubb/swagger'
|
6
|
+
|
7
|
+
import { camelCase, camelCaseTransformMerge, pascalCase, pascalCaseTransformMerge } from 'change-case'
|
8
|
+
|
9
|
+
import { TypeBuilder } from './builders/index.ts'
|
10
|
+
import { OperationGenerator } from './generators/index.ts'
|
11
|
+
|
12
|
+
import type { KubbFile, KubbPlugin } from '@kubb/core'
|
13
|
+
import type { OpenAPIV3, PluginOptions as SwaggerPluginOptions } from '@kubb/swagger'
|
14
|
+
import type { PluginOptions } from './types.ts'
|
15
|
+
|
16
|
+
export const pluginName = 'swagger-ts' satisfies PluginOptions['name']
|
17
|
+
export const pluginKey: PluginOptions['key'] = ['schema', pluginName] satisfies PluginOptions['key']
|
18
|
+
|
19
|
+
export const definePlugin = createPlugin<PluginOptions>((options) => {
|
20
|
+
const {
|
21
|
+
output = 'types',
|
22
|
+
group,
|
23
|
+
exclude = [],
|
24
|
+
include,
|
25
|
+
override = [],
|
26
|
+
enumType = 'asConst',
|
27
|
+
dateType = 'string',
|
28
|
+
optionalType = 'questionToken',
|
29
|
+
transformers = {},
|
30
|
+
exportAs,
|
31
|
+
} = options
|
32
|
+
const template = group?.output ? group.output : `${output}/{{tag}}Controller`
|
33
|
+
let pluginsOptions: [KubbPlugin<SwaggerPluginOptions>]
|
34
|
+
|
35
|
+
return {
|
36
|
+
name: pluginName,
|
37
|
+
options,
|
38
|
+
kind: 'schema',
|
39
|
+
validate(plugins) {
|
40
|
+
pluginsOptions = PluginManager.getDependedPlugins<SwaggerPluginOptions>(plugins, [swaggerPluginName])
|
41
|
+
|
42
|
+
return true
|
43
|
+
},
|
44
|
+
resolvePath(baseName, directory, options) {
|
45
|
+
const root = path.resolve(this.config.root, this.config.output.path)
|
46
|
+
const mode = FileManager.getMode(path.resolve(root, output))
|
47
|
+
|
48
|
+
if (mode === 'file') {
|
49
|
+
/**
|
50
|
+
* when output is a file then we will always append to the same file(output file), see fileManager.addOrAppend
|
51
|
+
* Other plugins then need to call addOrAppend instead of just add from the fileManager class
|
52
|
+
*/
|
53
|
+
return path.resolve(root, output)
|
54
|
+
}
|
55
|
+
|
56
|
+
if (options?.tag && group?.type === 'tag') {
|
57
|
+
const tag = camelCase(options.tag, { delimiter: '', transform: camelCaseTransformMerge })
|
58
|
+
|
59
|
+
return path.resolve(root, renderTemplate(template, { tag }), baseName)
|
60
|
+
}
|
61
|
+
|
62
|
+
return path.resolve(root, output, baseName)
|
63
|
+
},
|
64
|
+
resolveName(name, type) {
|
65
|
+
const resolvedName = pascalCase(name, { delimiter: '', stripRegexp: /[^A-Z0-9$]/gi, transform: pascalCaseTransformMerge })
|
66
|
+
|
67
|
+
if (type) {
|
68
|
+
return transformers?.name?.(resolvedName, type) || resolvedName
|
69
|
+
}
|
70
|
+
|
71
|
+
return resolvedName
|
72
|
+
},
|
73
|
+
async writeFile(source, writePath) {
|
74
|
+
if (!writePath.endsWith('.ts') || !source) {
|
75
|
+
return
|
76
|
+
}
|
77
|
+
|
78
|
+
return this.fileManager.write(source, writePath)
|
79
|
+
},
|
80
|
+
async buildStart() {
|
81
|
+
const [swaggerPlugin] = pluginsOptions
|
82
|
+
|
83
|
+
const oas = await swaggerPlugin.api.getOas()
|
84
|
+
|
85
|
+
const schemas = await swaggerPlugin.api.getSchemas()
|
86
|
+
const root = path.resolve(this.config.root, this.config.output.path)
|
87
|
+
const mode = FileManager.getMode(path.resolve(root, output))
|
88
|
+
// keep the used enumnames between TypeBuilder and OperationGenerator per plugin(pluginKey)
|
89
|
+
const usedEnumNames = {}
|
90
|
+
|
91
|
+
if (mode === 'directory') {
|
92
|
+
const builder = await new TypeBuilder({
|
93
|
+
usedEnumNames,
|
94
|
+
resolveName: (params) => this.resolveName({ pluginKey: this.plugin.key, ...params }),
|
95
|
+
fileResolver: (name) => {
|
96
|
+
const resolvedTypeId = this.resolvePath({
|
97
|
+
baseName: `${name}.ts`,
|
98
|
+
pluginKey: this.plugin.key,
|
99
|
+
})
|
100
|
+
|
101
|
+
const root = this.resolvePath({ baseName: ``, pluginKey: this.plugin.key })
|
102
|
+
|
103
|
+
return getRelativePath(root, resolvedTypeId)
|
104
|
+
},
|
105
|
+
withJSDocs: true,
|
106
|
+
enumType,
|
107
|
+
dateType,
|
108
|
+
optionalType,
|
109
|
+
}).configure()
|
110
|
+
Object.entries(schemas).forEach(([name, schema]: [string, OpenAPIV3.SchemaObject]) => {
|
111
|
+
// generate and pass through new code back to the core so it can be write to that file
|
112
|
+
return builder.add({
|
113
|
+
schema,
|
114
|
+
name,
|
115
|
+
})
|
116
|
+
})
|
117
|
+
|
118
|
+
const mapFolderSchema = async ([name]: [string, OpenAPIV3.SchemaObject]) => {
|
119
|
+
const resolvedPath = this.resolvePath({ baseName: `${this.resolveName({ name, pluginKey: this.plugin.key })}.ts`, pluginKey: this.plugin.key })
|
120
|
+
|
121
|
+
if (!resolvedPath) {
|
122
|
+
return null
|
123
|
+
}
|
124
|
+
|
125
|
+
return this.addFile({
|
126
|
+
path: resolvedPath,
|
127
|
+
baseName: `${this.resolveName({ name, pluginKey: this.plugin.key })}.ts`,
|
128
|
+
source: builder.print(name),
|
129
|
+
meta: {
|
130
|
+
pluginKey: this.plugin.key,
|
131
|
+
},
|
132
|
+
})
|
133
|
+
}
|
134
|
+
|
135
|
+
const promises = Object.entries(schemas).map(mapFolderSchema)
|
136
|
+
|
137
|
+
await Promise.all(promises)
|
138
|
+
}
|
139
|
+
|
140
|
+
if (mode === 'file') {
|
141
|
+
// outside the loop because we need to add files to just one instance to have the correct sorting, see refsSorter
|
142
|
+
const builder = new TypeBuilder({
|
143
|
+
usedEnumNames,
|
144
|
+
resolveName: (params) => this.resolveName({ pluginKey: this.plugin.key, ...params }),
|
145
|
+
withJSDocs: true,
|
146
|
+
enumType,
|
147
|
+
dateType,
|
148
|
+
optionalType,
|
149
|
+
}).configure()
|
150
|
+
Object.entries(schemas).forEach(([name, schema]: [string, OpenAPIV3.SchemaObject]) => {
|
151
|
+
// generate and pass through new code back to the core so it can be write to that file
|
152
|
+
return builder.add({
|
153
|
+
schema,
|
154
|
+
name,
|
155
|
+
})
|
156
|
+
})
|
157
|
+
|
158
|
+
const resolvedPath = this.resolvePath({ baseName: '', pluginKey: this.plugin.key })
|
159
|
+
if (!resolvedPath) {
|
160
|
+
return
|
161
|
+
}
|
162
|
+
|
163
|
+
await this.addFile({
|
164
|
+
path: resolvedPath,
|
165
|
+
baseName: output as KubbFile.BaseName,
|
166
|
+
source: builder.print(),
|
167
|
+
meta: {
|
168
|
+
pluginKey: this.plugin.key,
|
169
|
+
},
|
170
|
+
validate: false,
|
171
|
+
})
|
172
|
+
}
|
173
|
+
|
174
|
+
const operationGenerator = new OperationGenerator(
|
175
|
+
{
|
176
|
+
mode,
|
177
|
+
enumType,
|
178
|
+
dateType,
|
179
|
+
optionalType,
|
180
|
+
usedEnumNames,
|
181
|
+
},
|
182
|
+
{
|
183
|
+
oas,
|
184
|
+
pluginManager: this.pluginManager,
|
185
|
+
plugin: this.plugin,
|
186
|
+
contentType: swaggerPlugin.api.contentType,
|
187
|
+
exclude,
|
188
|
+
include,
|
189
|
+
override,
|
190
|
+
},
|
191
|
+
)
|
192
|
+
|
193
|
+
const files = await operationGenerator.build()
|
194
|
+
await this.addFile(...files)
|
195
|
+
},
|
196
|
+
async buildEnd() {
|
197
|
+
if (this.config.output.write === false) {
|
198
|
+
return
|
199
|
+
}
|
200
|
+
|
201
|
+
const root = path.resolve(this.config.root, this.config.output.path)
|
202
|
+
|
203
|
+
await this.fileManager.addIndexes({
|
204
|
+
root,
|
205
|
+
extName: '.ts',
|
206
|
+
meta: { pluginKey: this.plugin.key },
|
207
|
+
options: {
|
208
|
+
map: (file) => {
|
209
|
+
return {
|
210
|
+
...file,
|
211
|
+
exports: file.exports?.map((item) => {
|
212
|
+
if (exportAs) {
|
213
|
+
return {
|
214
|
+
...item,
|
215
|
+
name: exportAs,
|
216
|
+
asAlias: !!exportAs,
|
217
|
+
}
|
218
|
+
}
|
219
|
+
return item
|
220
|
+
}),
|
221
|
+
}
|
222
|
+
},
|
223
|
+
output,
|
224
|
+
isTypeOnly: true,
|
225
|
+
},
|
226
|
+
})
|
227
|
+
},
|
228
|
+
}
|
229
|
+
})
|
package/src/types.ts
ADDED
@@ -0,0 +1,82 @@
|
|
1
|
+
import type { KubbPlugin, PluginFactoryOptions, ResolveNameParams } from '@kubb/core'
|
2
|
+
import type { Exclude, Include, Override, ResolvePathOptions } from '@kubb/swagger'
|
3
|
+
|
4
|
+
export type Options = {
|
5
|
+
/**
|
6
|
+
* Relative path to save the TypeScript types.
|
7
|
+
* When output is a file it will save all models inside that file else it will create a file per schema item.
|
8
|
+
* @default 'types'
|
9
|
+
*/
|
10
|
+
output?: string
|
11
|
+
/**
|
12
|
+
* Group the TypeScript types based on the provided name.
|
13
|
+
*/
|
14
|
+
group?: {
|
15
|
+
/**
|
16
|
+
* Tag will group based on the operation tag inside the Swagger file.
|
17
|
+
*/
|
18
|
+
type: 'tag'
|
19
|
+
/**
|
20
|
+
* Relative path to save the grouped TypeScript Types.
|
21
|
+
*
|
22
|
+
* `{{tag}}` will be replaced by the current tagName.
|
23
|
+
* @example `${output}/{{tag}}Controller` => `models/PetController`
|
24
|
+
* @default `${output}/{{tag}}Controller`
|
25
|
+
*/
|
26
|
+
output?: string
|
27
|
+
}
|
28
|
+
/**
|
29
|
+
* Name to be used for the `export * as {{exportAs}} from './`
|
30
|
+
*/
|
31
|
+
exportAs?: string
|
32
|
+
/**
|
33
|
+
* Array containing exclude paramaters to exclude/skip tags/operations/methods/paths.
|
34
|
+
*/
|
35
|
+
exclude?: Array<Exclude>
|
36
|
+
/**
|
37
|
+
* Array containing include paramaters to include tags/operations/methods/paths.
|
38
|
+
*/
|
39
|
+
include?: Array<Include>
|
40
|
+
/**
|
41
|
+
* Array containing override paramaters to override `options` based on tags/operations/methods/paths.
|
42
|
+
*/
|
43
|
+
override?: Array<Override<Options>>
|
44
|
+
/**
|
45
|
+
* Choose to use `enum` or `as const` for enums
|
46
|
+
* @default 'asConst'
|
47
|
+
*/
|
48
|
+
enumType?: 'enum' | 'asConst' | 'asPascalConst'
|
49
|
+
/**
|
50
|
+
* Choose to use `date` or `datetime` as JavaScript `Date` instead of `string`.
|
51
|
+
* @default 'string'
|
52
|
+
*/
|
53
|
+
dateType?: 'string' | 'date'
|
54
|
+
/**
|
55
|
+
* Choose what to use as mode for an optional value.
|
56
|
+
* @examples 'questionToken': type?: string
|
57
|
+
* @examples 'undefined': type: string | undefined
|
58
|
+
* @examples 'questionTokenAndUndefined': type?: string | undefined
|
59
|
+
* @default 'questionToken'
|
60
|
+
*/
|
61
|
+
optionalType?: 'questionToken' | 'undefined' | 'questionTokenAndUndefined'
|
62
|
+
transformers?: {
|
63
|
+
/**
|
64
|
+
* Customize the names based on the type that is provided by the plugin.
|
65
|
+
*/
|
66
|
+
name?: (name: ResolveNameParams['name'], type?: ResolveNameParams['type']) => string
|
67
|
+
}
|
68
|
+
}
|
69
|
+
|
70
|
+
export type FileMeta = {
|
71
|
+
pluginKey?: KubbPlugin['key']
|
72
|
+
name?: string
|
73
|
+
tag?: string
|
74
|
+
}
|
75
|
+
|
76
|
+
export type PluginOptions = PluginFactoryOptions<'swagger-ts', 'schema', Options, Options, never, ResolvePathOptions>
|
77
|
+
|
78
|
+
declare module '@kubb/core' {
|
79
|
+
export interface _Register {
|
80
|
+
['@kubb/swagger-ts']: PluginOptions
|
81
|
+
}
|
82
|
+
}
|