@kubb/plugin-ts 0.0.0-canary-20241104172400
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/LICENSE +21 -0
- package/README.md +54 -0
- package/dist/chunk-HJ6SFXKJ.js +306 -0
- package/dist/chunk-HJ6SFXKJ.js.map +1 -0
- package/dist/chunk-HZ6BGJL7.js +2539 -0
- package/dist/chunk-HZ6BGJL7.js.map +1 -0
- package/dist/chunk-PTC7IOBI.cjs +335 -0
- package/dist/chunk-PTC7IOBI.cjs.map +1 -0
- package/dist/chunk-RORQTUX5.cjs +2566 -0
- package/dist/chunk-RORQTUX5.cjs.map +1 -0
- package/dist/components.cjs +16 -0
- package/dist/components.cjs.map +1 -0
- package/dist/components.d.cts +30 -0
- package/dist/components.d.ts +30 -0
- package/dist/components.js +3 -0
- package/dist/components.js.map +1 -0
- package/dist/generators.cjs +17 -0
- package/dist/generators.cjs.map +1 -0
- package/dist/generators.d.cts +10 -0
- package/dist/generators.d.ts +10 -0
- package/dist/generators.js +4 -0
- package/dist/generators.js.map +1 -0
- package/dist/index.cjs +17 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +9 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.js +4 -0
- package/dist/index.js.map +1 -0
- package/dist/types-zQCmVvxv.d.cts +97 -0
- package/dist/types-zQCmVvxv.d.ts +97 -0
- package/package.json +97 -0
- package/src/components/OasType.tsx +29 -0
- package/src/components/Type.tsx +141 -0
- package/src/components/index.ts +2 -0
- package/src/generators/__snapshots__/bodyUploadFileApiAssetsPost.ts +6 -0
- package/src/generators/__snapshots__/catTypeAsConst.ts +11 -0
- package/src/generators/__snapshots__/createPet.ts +34 -0
- package/src/generators/__snapshots__/createPetWithUnknownTypeUnknown.ts +34 -0
- package/src/generators/__snapshots__/deletePet.ts +7 -0
- package/src/generators/__snapshots__/dogTypeAsConst.ts +11 -0
- package/src/generators/__snapshots__/enumAllOf.ts +10 -0
- package/src/generators/__snapshots__/enumArray.ts +19 -0
- package/src/generators/__snapshots__/enumInObject.ts +13 -0
- package/src/generators/__snapshots__/enumItems.ts +8 -0
- package/src/generators/__snapshots__/enumNames.ts +6 -0
- package/src/generators/__snapshots__/enumNamesConst.ts +6 -0
- package/src/generators/__snapshots__/enumNamesLiteral.ts +3 -0
- package/src/generators/__snapshots__/enumNamesPascalConst.ts +8 -0
- package/src/generators/__snapshots__/enumNamesType.ts +8 -0
- package/src/generators/__snapshots__/enumNullableMember.ts +8 -0
- package/src/generators/__snapshots__/enumNullableType.ts +8 -0
- package/src/generators/__snapshots__/enumString.ts +10 -0
- package/src/generators/__snapshots__/enumVarNamesType.ts +8 -0
- package/src/generators/__snapshots__/getPets.ts +26 -0
- package/src/generators/__snapshots__/mixedValueTypeConst.ts +9 -0
- package/src/generators/__snapshots__/nullConstNull.ts +7 -0
- package/src/generators/__snapshots__/numberValueConst.ts +9 -0
- package/src/generators/__snapshots__/petMapper.ts +15 -0
- package/src/generators/__snapshots__/petQuestionToken.ts +19 -0
- package/src/generators/__snapshots__/petQuestionTokenAndUndefined.ts +19 -0
- package/src/generators/__snapshots__/petUndefined.ts +19 -0
- package/src/generators/__snapshots__/pets.ts +14 -0
- package/src/generators/__snapshots__/petsStoreAdvancedDiscriminator.ts +31 -0
- package/src/generators/__snapshots__/petsStoreDiscriminator.ts +13 -0
- package/src/generators/__snapshots__/petsStoreRef.ts +1 -0
- package/src/generators/__snapshots__/plainDateDate.ts +1 -0
- package/src/generators/__snapshots__/plainDateString.ts +1 -0
- package/src/generators/__snapshots__/plainEmail.ts +1 -0
- package/src/generators/__snapshots__/plainFile.ts +1 -0
- package/src/generators/__snapshots__/plainTimeDate.ts +1 -0
- package/src/generators/__snapshots__/plainUuid.ts +1 -0
- package/src/generators/__snapshots__/readOnly.ts +20 -0
- package/src/generators/__snapshots__/showPetById.ts +31 -0
- package/src/generators/__snapshots__/stringValueConst.ts +9 -0
- package/src/generators/index.ts +2 -0
- package/src/generators/oasGenerator.tsx +29 -0
- package/src/generators/typeGenerator.tsx +205 -0
- package/src/index.ts +2 -0
- package/src/parser/index.ts +279 -0
- package/src/plugin.ts +127 -0
- package/src/types.ts +97 -0
|
@@ -0,0 +1,279 @@
|
|
|
1
|
+
import transformers from '@kubb/core/transformers'
|
|
2
|
+
import * as factory from '@kubb/parser-ts/factory'
|
|
3
|
+
import { type SchemaTree, isKeyword, schemaKeywords } from '@kubb/plugin-oas'
|
|
4
|
+
|
|
5
|
+
import type { Schema, SchemaKeywordMapper, SchemaMapper } from '@kubb/plugin-oas'
|
|
6
|
+
import type ts from 'typescript'
|
|
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 | boolean, format?: 'string' | 'number' | 'boolean') => {
|
|
58
|
+
if (!name) {
|
|
59
|
+
return undefined
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
if (format === 'boolean') {
|
|
63
|
+
if (name === true) {
|
|
64
|
+
return factory.createLiteralTypeNode(factory.createTrue())
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
return factory.createLiteralTypeNode(factory.createFalse())
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
if (format === 'number' && typeof name === 'number') {
|
|
71
|
+
return factory.createLiteralTypeNode(factory.createNumericLiteral(name))
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
return factory.createLiteralTypeNode(factory.createStringLiteral(name.toString()))
|
|
75
|
+
},
|
|
76
|
+
datetime: () => factory.keywordTypeNodes.string,
|
|
77
|
+
date: (type: 'date' | 'string' = 'string') =>
|
|
78
|
+
type === 'string' ? factory.keywordTypeNodes.string : factory.createTypeReferenceNode(factory.createIdentifier('Date')),
|
|
79
|
+
time: (type: 'date' | 'string' = 'string') =>
|
|
80
|
+
type === 'string' ? factory.keywordTypeNodes.string : factory.createTypeReferenceNode(factory.createIdentifier('Date')),
|
|
81
|
+
uuid: undefined,
|
|
82
|
+
url: undefined,
|
|
83
|
+
strict: undefined,
|
|
84
|
+
default: undefined,
|
|
85
|
+
and: (nodes?: ts.TypeNode[]) => {
|
|
86
|
+
if (!nodes) {
|
|
87
|
+
return undefined
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
return factory.createIntersectionDeclaration({
|
|
91
|
+
withParentheses: true,
|
|
92
|
+
nodes,
|
|
93
|
+
})
|
|
94
|
+
},
|
|
95
|
+
describe: undefined,
|
|
96
|
+
min: undefined,
|
|
97
|
+
max: undefined,
|
|
98
|
+
optional: undefined,
|
|
99
|
+
matches: undefined,
|
|
100
|
+
email: undefined,
|
|
101
|
+
firstName: undefined,
|
|
102
|
+
lastName: undefined,
|
|
103
|
+
password: undefined,
|
|
104
|
+
phone: undefined,
|
|
105
|
+
readOnly: undefined,
|
|
106
|
+
writeOnly: undefined,
|
|
107
|
+
ref: (propertyName?: string) => {
|
|
108
|
+
if (!propertyName) {
|
|
109
|
+
return undefined
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
return factory.createTypeReferenceNode(propertyName, undefined)
|
|
113
|
+
},
|
|
114
|
+
blob: () => factory.createTypeReferenceNode('Blob', []),
|
|
115
|
+
deprecated: undefined,
|
|
116
|
+
example: undefined,
|
|
117
|
+
schema: undefined,
|
|
118
|
+
catchall: undefined,
|
|
119
|
+
name: undefined,
|
|
120
|
+
} satisfies SchemaMapper<ts.Node | null | undefined>
|
|
121
|
+
|
|
122
|
+
type ParserOptions = {
|
|
123
|
+
name: string
|
|
124
|
+
typeName?: string
|
|
125
|
+
description?: string
|
|
126
|
+
/**
|
|
127
|
+
* @default `'questionToken'`
|
|
128
|
+
*/
|
|
129
|
+
optionalType: 'questionToken' | 'undefined' | 'questionTokenAndUndefined'
|
|
130
|
+
/**
|
|
131
|
+
* @default `'asConst'`
|
|
132
|
+
* asPascalConst is deprecated
|
|
133
|
+
*/
|
|
134
|
+
enumType: 'enum' | 'asConst' | 'asPascalConst' | 'constEnum' | 'literal'
|
|
135
|
+
keysToOmit?: string[]
|
|
136
|
+
mapper?: Record<string, ts.PropertySignature>
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
export function parse({ parent, current, siblings }: SchemaTree, options: ParserOptions): ts.Node | null | undefined {
|
|
140
|
+
const value = typeKeywordMapper[current.keyword as keyof typeof typeKeywordMapper]
|
|
141
|
+
|
|
142
|
+
if (!value) {
|
|
143
|
+
return undefined
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
if (isKeyword(current, schemaKeywords.union)) {
|
|
147
|
+
return typeKeywordMapper.union(
|
|
148
|
+
current.args.map((schema) => parse({ parent: current, current: schema, siblings }, options)).filter(Boolean) as ts.TypeNode[],
|
|
149
|
+
)
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
if (isKeyword(current, schemaKeywords.and)) {
|
|
153
|
+
return typeKeywordMapper.and(current.args.map((schema) => parse({ parent: current, current: schema, siblings }, options)).filter(Boolean) as ts.TypeNode[])
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
if (isKeyword(current, schemaKeywords.array)) {
|
|
157
|
+
return typeKeywordMapper.array(
|
|
158
|
+
current.args.items.map((schema) => parse({ parent: current, current: schema, siblings }, options)).filter(Boolean) as ts.TypeNode[],
|
|
159
|
+
)
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
if (isKeyword(current, schemaKeywords.enum)) {
|
|
163
|
+
return typeKeywordMapper.enum(current.args.typeName)
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
if (isKeyword(current, schemaKeywords.ref)) {
|
|
167
|
+
return typeKeywordMapper.ref(current.args.name)
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
if (isKeyword(current, schemaKeywords.blob)) {
|
|
171
|
+
return value()
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
if (isKeyword(current, schemaKeywords.tuple)) {
|
|
175
|
+
return typeKeywordMapper.tuple(
|
|
176
|
+
current.args.items.map((schema) => parse({ parent: current, current: schema, siblings }, options)).filter(Boolean) as ts.TypeNode[],
|
|
177
|
+
)
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
if (isKeyword(current, schemaKeywords.const)) {
|
|
181
|
+
return typeKeywordMapper.const(current.args.name, current.args.format)
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
if (isKeyword(current, schemaKeywords.object)) {
|
|
185
|
+
const properties = Object.entries(current.args?.properties || {})
|
|
186
|
+
.filter((item) => {
|
|
187
|
+
const schemas = item[1]
|
|
188
|
+
return schemas && typeof schemas.map === 'function'
|
|
189
|
+
})
|
|
190
|
+
.map(([name, schemas]) => {
|
|
191
|
+
const nameSchema = schemas.find((schema) => schema.keyword === schemaKeywords.name) as SchemaKeywordMapper['name']
|
|
192
|
+
const mappedName = nameSchema?.args || name
|
|
193
|
+
|
|
194
|
+
// custom mapper(pluginOptions)
|
|
195
|
+
if (options.mapper?.[mappedName]) {
|
|
196
|
+
return options.mapper?.[mappedName]
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
const isNullish = schemas.some((schema) => schema.keyword === schemaKeywords.nullish)
|
|
200
|
+
const isNullable = schemas.some((schema) => schema.keyword === schemaKeywords.nullable)
|
|
201
|
+
const isOptional = schemas.some((schema) => schema.keyword === schemaKeywords.optional)
|
|
202
|
+
const isReadonly = schemas.some((schema) => schema.keyword === schemaKeywords.readOnly)
|
|
203
|
+
const describeSchema = schemas.find((schema) => schema.keyword === schemaKeywords.describe) as SchemaKeywordMapper['describe'] | undefined
|
|
204
|
+
const deprecatedSchema = schemas.find((schema) => schema.keyword === schemaKeywords.deprecated) as SchemaKeywordMapper['deprecated'] | undefined
|
|
205
|
+
const defaultSchema = schemas.find((schema) => schema.keyword === schemaKeywords.default) as SchemaKeywordMapper['default'] | undefined
|
|
206
|
+
const exampleSchema = schemas.find((schema) => schema.keyword === schemaKeywords.example) as SchemaKeywordMapper['example'] | undefined
|
|
207
|
+
const schemaSchema = schemas.find((schema) => schema.keyword === schemaKeywords.schema) as SchemaKeywordMapper['schema'] | undefined
|
|
208
|
+
|
|
209
|
+
let type = schemas.map((schema) => parse({ parent: current, current: schema, siblings }, options)).filter(Boolean)[0] as ts.TypeNode
|
|
210
|
+
|
|
211
|
+
if (isNullable) {
|
|
212
|
+
type = factory.createUnionDeclaration({
|
|
213
|
+
nodes: [type, factory.keywordTypeNodes.null],
|
|
214
|
+
}) as ts.TypeNode
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
if (isNullish && ['undefined', 'questionTokenAndUndefined'].includes(options.optionalType as string)) {
|
|
218
|
+
type = factory.createUnionDeclaration({
|
|
219
|
+
nodes: [type, factory.keywordTypeNodes.undefined],
|
|
220
|
+
}) as ts.TypeNode
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
if (isOptional && ['undefined', 'questionTokenAndUndefined'].includes(options.optionalType as string)) {
|
|
224
|
+
type = factory.createUnionDeclaration({
|
|
225
|
+
nodes: [type, factory.keywordTypeNodes.undefined],
|
|
226
|
+
}) as ts.TypeNode
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
const propertySignature = factory.createPropertySignature({
|
|
230
|
+
questionToken: isOptional || isNullish ? ['questionToken', 'questionTokenAndUndefined'].includes(options.optionalType as string) : false,
|
|
231
|
+
name: mappedName,
|
|
232
|
+
type,
|
|
233
|
+
readOnly: isReadonly,
|
|
234
|
+
})
|
|
235
|
+
|
|
236
|
+
return factory.appendJSDocToNode({
|
|
237
|
+
node: propertySignature,
|
|
238
|
+
comments: [
|
|
239
|
+
describeSchema ? `@description ${transformers.jsStringEscape(describeSchema.args)}` : undefined,
|
|
240
|
+
deprecatedSchema ? '@deprecated' : undefined,
|
|
241
|
+
defaultSchema ? `@default ${defaultSchema.args}` : undefined,
|
|
242
|
+
exampleSchema ? `@example ${exampleSchema.args}` : undefined,
|
|
243
|
+
schemaSchema?.args?.type || schemaSchema?.args?.format
|
|
244
|
+
? [`@type ${schemaSchema?.args?.type || 'unknown'}${!isOptional ? '' : ' | undefined'}`, schemaSchema?.args?.format].filter(Boolean).join(', ')
|
|
245
|
+
: undefined,
|
|
246
|
+
].filter(Boolean),
|
|
247
|
+
})
|
|
248
|
+
})
|
|
249
|
+
|
|
250
|
+
const additionalProperties = current.args?.additionalProperties?.length
|
|
251
|
+
? factory.createIndexSignature(
|
|
252
|
+
current.args.additionalProperties
|
|
253
|
+
.map((schema) => parse({ parent: current, current: schema, siblings }, options))
|
|
254
|
+
.filter(Boolean)
|
|
255
|
+
.at(0) as ts.TypeNode,
|
|
256
|
+
)
|
|
257
|
+
: undefined
|
|
258
|
+
|
|
259
|
+
return typeKeywordMapper.object([...properties, additionalProperties].filter(Boolean))
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
if (isKeyword(current, schemaKeywords.datetime)) {
|
|
263
|
+
return typeKeywordMapper.datetime()
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
if (isKeyword(current, schemaKeywords.date)) {
|
|
267
|
+
return typeKeywordMapper.date(current.args.type)
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
if (isKeyword(current, schemaKeywords.time)) {
|
|
271
|
+
return typeKeywordMapper.time(current.args.type)
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
if (current.keyword in typeKeywordMapper) {
|
|
275
|
+
return value()
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
return undefined
|
|
279
|
+
}
|
package/src/plugin.ts
ADDED
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
import path from 'node:path'
|
|
2
|
+
|
|
3
|
+
import { FileManager, type Group, PluginManager, createPlugin } from '@kubb/core'
|
|
4
|
+
import { camelCase, pascalCase } from '@kubb/core/transformers'
|
|
5
|
+
import { OperationGenerator, SchemaGenerator, pluginOasName } from '@kubb/plugin-oas'
|
|
6
|
+
|
|
7
|
+
import type { Plugin } from '@kubb/core'
|
|
8
|
+
import type { PluginOas as SwaggerPluginOptions } from '@kubb/plugin-oas'
|
|
9
|
+
import { oasGenerator, typeGenerator } from './generators'
|
|
10
|
+
import type { PluginTs } from './types.ts'
|
|
11
|
+
|
|
12
|
+
export const pluginTsName = 'plugin-ts' satisfies PluginTs['name']
|
|
13
|
+
|
|
14
|
+
export const pluginTs = createPlugin<PluginTs>((options) => {
|
|
15
|
+
const {
|
|
16
|
+
output = { path: 'types', barrelType: 'named' },
|
|
17
|
+
group,
|
|
18
|
+
exclude = [],
|
|
19
|
+
include,
|
|
20
|
+
override = [],
|
|
21
|
+
enumType = 'asConst',
|
|
22
|
+
enumSuffix = 'enum',
|
|
23
|
+
dateType = 'string',
|
|
24
|
+
unknownType = 'any',
|
|
25
|
+
optionalType = 'questionToken',
|
|
26
|
+
transformers = {},
|
|
27
|
+
oasType = false,
|
|
28
|
+
mapper = {},
|
|
29
|
+
generators = [typeGenerator, oasType === 'infer' ? oasGenerator : undefined].filter(Boolean),
|
|
30
|
+
} = options
|
|
31
|
+
|
|
32
|
+
return {
|
|
33
|
+
name: pluginTsName,
|
|
34
|
+
options: {
|
|
35
|
+
output,
|
|
36
|
+
transformers,
|
|
37
|
+
dateType,
|
|
38
|
+
optionalType,
|
|
39
|
+
oasType,
|
|
40
|
+
enumType,
|
|
41
|
+
enumSuffix,
|
|
42
|
+
// keep the used enumnames between SchemaGenerator and OperationGenerator per plugin(pluginKey)
|
|
43
|
+
usedEnumNames: {},
|
|
44
|
+
unknownType,
|
|
45
|
+
override,
|
|
46
|
+
mapper,
|
|
47
|
+
},
|
|
48
|
+
pre: [pluginOasName],
|
|
49
|
+
resolvePath(baseName, pathMode, options) {
|
|
50
|
+
const root = path.resolve(this.config.root, this.config.output.path)
|
|
51
|
+
const mode = pathMode ?? FileManager.getMode(path.resolve(root, output.path))
|
|
52
|
+
|
|
53
|
+
if (options?.tag && group?.type === 'tag') {
|
|
54
|
+
const groupName: Group['name'] = group?.name ? group.name : (ctx) => `${ctx.group}Controller`
|
|
55
|
+
|
|
56
|
+
return path.resolve(root, output.path, groupName({ group: camelCase(options.tag) }), baseName)
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
if (mode === 'single') {
|
|
60
|
+
/**
|
|
61
|
+
* when output is a file then we will always append to the same file(output file), see fileManager.addOrAppend
|
|
62
|
+
* Other plugins then need to call addOrAppend instead of just add from the fileManager class
|
|
63
|
+
*/
|
|
64
|
+
return path.resolve(root, output.path)
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
return path.resolve(root, output.path, baseName)
|
|
68
|
+
},
|
|
69
|
+
resolveName(name, type) {
|
|
70
|
+
const resolvedName = pascalCase(name, { isFile: type === 'file' })
|
|
71
|
+
|
|
72
|
+
if (type) {
|
|
73
|
+
return transformers?.name?.(resolvedName, type) || resolvedName
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
return resolvedName
|
|
77
|
+
},
|
|
78
|
+
async buildStart() {
|
|
79
|
+
const [swaggerPlugin]: [Plugin<SwaggerPluginOptions>] = PluginManager.getDependedPlugins<SwaggerPluginOptions>(this.plugins, [pluginOasName])
|
|
80
|
+
|
|
81
|
+
const oas = await swaggerPlugin.context.getOas()
|
|
82
|
+
const root = path.resolve(this.config.root, this.config.output.path)
|
|
83
|
+
const mode = FileManager.getMode(path.resolve(root, output.path))
|
|
84
|
+
|
|
85
|
+
const schemaGenerator = new SchemaGenerator(this.plugin.options, {
|
|
86
|
+
oas,
|
|
87
|
+
pluginManager: this.pluginManager,
|
|
88
|
+
plugin: this.plugin,
|
|
89
|
+
contentType: swaggerPlugin.context.contentType,
|
|
90
|
+
include: undefined,
|
|
91
|
+
override,
|
|
92
|
+
mode,
|
|
93
|
+
output: output.path,
|
|
94
|
+
})
|
|
95
|
+
|
|
96
|
+
const schemaFiles = await schemaGenerator.build(...generators)
|
|
97
|
+
await this.addFile(...schemaFiles)
|
|
98
|
+
|
|
99
|
+
const operationGenerator = new OperationGenerator(this.plugin.options, {
|
|
100
|
+
oas,
|
|
101
|
+
pluginManager: this.pluginManager,
|
|
102
|
+
plugin: this.plugin,
|
|
103
|
+
contentType: swaggerPlugin.context.contentType,
|
|
104
|
+
exclude,
|
|
105
|
+
include,
|
|
106
|
+
override,
|
|
107
|
+
mode,
|
|
108
|
+
})
|
|
109
|
+
|
|
110
|
+
const operationFiles = await operationGenerator.build(...generators)
|
|
111
|
+
await this.addFile(...operationFiles)
|
|
112
|
+
|
|
113
|
+
const barrelFiles = await this.fileManager.getBarrelFiles({
|
|
114
|
+
type: output.barrelType ?? 'named',
|
|
115
|
+
root,
|
|
116
|
+
output,
|
|
117
|
+
files: this.fileManager.files,
|
|
118
|
+
meta: {
|
|
119
|
+
pluginKey: this.plugin.key,
|
|
120
|
+
},
|
|
121
|
+
logger: this.logger,
|
|
122
|
+
})
|
|
123
|
+
|
|
124
|
+
await this.addFile(...barrelFiles)
|
|
125
|
+
},
|
|
126
|
+
}
|
|
127
|
+
})
|
package/src/types.ts
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import type { Group, Output, PluginFactoryOptions, ResolveNameParams } from '@kubb/core'
|
|
2
|
+
import type { Exclude, Generator, Include, Override, ResolvePathOptions } from '@kubb/plugin-oas'
|
|
3
|
+
import type ts from 'typescript'
|
|
4
|
+
|
|
5
|
+
export type Options = {
|
|
6
|
+
/**
|
|
7
|
+
* Specify the export location for the files and define the behavior of the output
|
|
8
|
+
* @default { path: 'types', barrelType: 'named' }
|
|
9
|
+
*/
|
|
10
|
+
output?: Output
|
|
11
|
+
/**
|
|
12
|
+
* Group the clients based on the provided name.
|
|
13
|
+
*/
|
|
14
|
+
group?: Group
|
|
15
|
+
/**
|
|
16
|
+
* Array containing exclude parameters to exclude/skip tags/operations/methods/paths.
|
|
17
|
+
*/
|
|
18
|
+
exclude?: Array<Exclude>
|
|
19
|
+
/**
|
|
20
|
+
* Array containing include parameters to include tags/operations/methods/paths.
|
|
21
|
+
*/
|
|
22
|
+
include?: Array<Include>
|
|
23
|
+
/**
|
|
24
|
+
* Array containing override parameters to override `options` based on tags/operations/methods/paths.
|
|
25
|
+
*/
|
|
26
|
+
override?: Array<Override<ResolvedOptions>>
|
|
27
|
+
/**
|
|
28
|
+
* Choose to use `enum` or `as const` for enums
|
|
29
|
+
* @default 'asConst'
|
|
30
|
+
* asPascalConst is deprecated
|
|
31
|
+
*/
|
|
32
|
+
enumType?: 'enum' | 'asConst' | 'asPascalConst' | 'constEnum' | 'literal'
|
|
33
|
+
/**
|
|
34
|
+
* Set a suffix for the generated enums.
|
|
35
|
+
* @default 'enum'
|
|
36
|
+
*/
|
|
37
|
+
enumSuffix?: string
|
|
38
|
+
/**
|
|
39
|
+
* Choose to use `date` or `datetime` as JavaScript `Date` instead of `string`.
|
|
40
|
+
* @default 'string'
|
|
41
|
+
*/
|
|
42
|
+
dateType?: 'string' | 'date'
|
|
43
|
+
/**
|
|
44
|
+
* Which type to use when the Swagger/OpenAPI file is not providing more information.
|
|
45
|
+
* @default 'any'
|
|
46
|
+
*/
|
|
47
|
+
unknownType?: 'any' | 'unknown'
|
|
48
|
+
/**
|
|
49
|
+
* Choose what to use as mode for an optional value.
|
|
50
|
+
* @examples 'questionToken': type?: string
|
|
51
|
+
* @examples 'undefined': type: string | undefined
|
|
52
|
+
* @examples 'questionTokenAndUndefined': type?: string | undefined
|
|
53
|
+
* @default 'questionToken'
|
|
54
|
+
*/
|
|
55
|
+
optionalType?: 'questionToken' | 'undefined' | 'questionTokenAndUndefined'
|
|
56
|
+
transformers?: {
|
|
57
|
+
/**
|
|
58
|
+
* Customize the names based on the type that is provided by the plugin.
|
|
59
|
+
*/
|
|
60
|
+
name?: (name: ResolveNameParams['name'], type?: ResolveNameParams['type']) => string
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Export an Oas object as Oas type with `import type { Infer } from '@kubb/oas'`
|
|
64
|
+
*/
|
|
65
|
+
oasType?: 'infer' | false
|
|
66
|
+
/**
|
|
67
|
+
* @example
|
|
68
|
+
* Use https://ts-ast-viewer.com to generate factory code(see createPropertySignature)
|
|
69
|
+
* category: factory.createPropertySignature(
|
|
70
|
+
* undefined,
|
|
71
|
+
* factory.createIdentifier("category"),
|
|
72
|
+
* factory.createToken(ts.SyntaxKind.QuestionToken),
|
|
73
|
+
* factory.createKeywordTypeNode(ts.SyntaxKind.StringKeyword)
|
|
74
|
+
* )
|
|
75
|
+
*/
|
|
76
|
+
mapper?: Record<string, ts.PropertySignature>
|
|
77
|
+
/**
|
|
78
|
+
* Define some generators next to the ts generators
|
|
79
|
+
*/
|
|
80
|
+
generators?: Array<Generator<PluginTs>>
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
type ResolvedOptions = {
|
|
84
|
+
output: Output
|
|
85
|
+
override: NonNullable<Options['override']>
|
|
86
|
+
enumType: NonNullable<Options['enumType']>
|
|
87
|
+
enumSuffix: NonNullable<Options['enumSuffix']>
|
|
88
|
+
dateType: NonNullable<Options['dateType']>
|
|
89
|
+
unknownType: NonNullable<Options['unknownType']>
|
|
90
|
+
optionalType: NonNullable<Options['optionalType']>
|
|
91
|
+
transformers: NonNullable<Options['transformers']>
|
|
92
|
+
oasType: NonNullable<Options['oasType']>
|
|
93
|
+
usedEnumNames: Record<string, number>
|
|
94
|
+
mapper: Record<string, any>
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
export type PluginTs = PluginFactoryOptions<'plugin-ts', Options, ResolvedOptions, never, ResolvePathOptions>
|