@kubb/plugin-ts 5.0.0-alpha.25 → 5.0.0-alpha.27
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/dist/index.cjs +139 -115
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +145 -118
- package/dist/index.js +140 -116
- package/dist/index.js.map +1 -1
- package/package.json +3 -3
- package/src/components/Type.tsx +8 -39
- package/src/generators/typeGenerator.tsx +40 -19
- package/src/generators/typeGeneratorLegacy.tsx +36 -18
- package/src/index.ts +1 -0
- package/src/plugin.ts +9 -5
- package/src/presets.ts +5 -2
- package/src/printers/printerTs.ts +33 -4
- package/src/resolvers/resolverTs.ts +11 -15
- package/src/resolvers/resolverTsLegacy.ts +8 -8
- package/src/types.ts +31 -18
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kubb/plugin-ts",
|
|
3
|
-
"version": "5.0.0-alpha.
|
|
3
|
+
"version": "5.0.0-alpha.27",
|
|
4
4
|
"description": "TypeScript code generation plugin for Kubb, transforming OpenAPI schemas into TypeScript interfaces, types, and utility functions.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"typescript",
|
|
@@ -53,8 +53,8 @@
|
|
|
53
53
|
"@kubb/react-fabric": "0.15.1",
|
|
54
54
|
"remeda": "^2.33.7",
|
|
55
55
|
"typescript": "5.9.3",
|
|
56
|
-
"@kubb/ast": "5.0.0-alpha.
|
|
57
|
-
"@kubb/core": "5.0.0-alpha.
|
|
56
|
+
"@kubb/ast": "5.0.0-alpha.27",
|
|
57
|
+
"@kubb/core": "5.0.0-alpha.27"
|
|
58
58
|
},
|
|
59
59
|
"peerDependencies": {
|
|
60
60
|
"@kubb/react-fabric": "0.15.1"
|
package/src/components/Type.tsx
CHANGED
|
@@ -1,46 +1,27 @@
|
|
|
1
|
+
import type { Printer } from '@kubb/ast'
|
|
1
2
|
import { collect, narrowSchema, schemaTypes } from '@kubb/ast'
|
|
2
3
|
import type { EnumSchemaNode, SchemaNode } from '@kubb/ast/types'
|
|
3
4
|
import { File } from '@kubb/react-fabric'
|
|
4
5
|
import type { FabricReactNode } from '@kubb/react-fabric/types'
|
|
5
|
-
import {
|
|
6
|
+
import type { PrinterTsFactory } from '../printers/printerTs.ts'
|
|
6
7
|
import type { PluginTs } from '../types.ts'
|
|
7
8
|
import { Enum, getEnumNames } from './Enum.tsx'
|
|
8
9
|
|
|
9
10
|
type Props = {
|
|
10
11
|
name: string
|
|
11
12
|
node: SchemaNode
|
|
12
|
-
|
|
13
|
-
|
|
13
|
+
/**
|
|
14
|
+
* Pre-configured printer instance created by the generator.
|
|
15
|
+
* Created with `printerTs({ ..., nodes: options.printer?.nodes })`.
|
|
16
|
+
*/
|
|
17
|
+
printer: Printer<PrinterTsFactory>
|
|
14
18
|
enumType: PluginTs['resolvedOptions']['enumType']
|
|
15
19
|
enumTypeSuffix: PluginTs['resolvedOptions']['enumTypeSuffix']
|
|
16
20
|
enumKeyCasing: PluginTs['resolvedOptions']['enumKeyCasing']
|
|
17
|
-
syntaxType: PluginTs['resolvedOptions']['syntaxType']
|
|
18
21
|
resolver: PluginTs['resolver']
|
|
19
|
-
description?: string
|
|
20
|
-
keysToOmit?: string[]
|
|
21
|
-
/**
|
|
22
|
-
* Names of top-level schemas that are enums.
|
|
23
|
-
* Used so the printer's `ref` handler can use the suffixed type name (e.g. `StatusKey`)
|
|
24
|
-
* instead of the plain PascalCase name (e.g. `Status`) when resolving `$ref` enum targets.
|
|
25
|
-
*/
|
|
26
|
-
enumSchemaNames?: Set<string>
|
|
27
22
|
}
|
|
28
23
|
|
|
29
|
-
export function Type({
|
|
30
|
-
name,
|
|
31
|
-
node,
|
|
32
|
-
keysToOmit,
|
|
33
|
-
optionalType,
|
|
34
|
-
arrayType,
|
|
35
|
-
syntaxType,
|
|
36
|
-
enumType,
|
|
37
|
-
enumTypeSuffix,
|
|
38
|
-
enumKeyCasing,
|
|
39
|
-
description,
|
|
40
|
-
resolver,
|
|
41
|
-
enumSchemaNames,
|
|
42
|
-
}: Props): FabricReactNode {
|
|
43
|
-
const resolvedDescription = description || node?.description
|
|
24
|
+
export function Type({ name, node, printer, enumType, enumTypeSuffix, enumKeyCasing, resolver }: Props): FabricReactNode {
|
|
44
25
|
const enumSchemaNodes = collect<EnumSchemaNode>(node, {
|
|
45
26
|
schema(n): EnumSchemaNode | undefined {
|
|
46
27
|
const enumNode = narrowSchema(n, schemaTypes.enum)
|
|
@@ -48,18 +29,6 @@ export function Type({
|
|
|
48
29
|
},
|
|
49
30
|
})
|
|
50
31
|
|
|
51
|
-
const printer = printerTs({
|
|
52
|
-
optionalType,
|
|
53
|
-
arrayType,
|
|
54
|
-
enumType,
|
|
55
|
-
enumTypeSuffix,
|
|
56
|
-
name,
|
|
57
|
-
syntaxType,
|
|
58
|
-
description: resolvedDescription,
|
|
59
|
-
keysToOmit,
|
|
60
|
-
resolver,
|
|
61
|
-
enumSchemaNames,
|
|
62
|
-
})
|
|
63
32
|
const output = printer.print(node)
|
|
64
33
|
|
|
65
34
|
if (!output) {
|
|
@@ -1,20 +1,21 @@
|
|
|
1
1
|
import path from 'node:path'
|
|
2
|
-
import { caseParams,
|
|
2
|
+
import { caseParams, narrowSchema, schemaTypes, transform } from '@kubb/ast'
|
|
3
3
|
import type { SchemaNode } from '@kubb/ast/types'
|
|
4
4
|
import { defineGenerator, getMode } from '@kubb/core'
|
|
5
5
|
import { File } from '@kubb/react-fabric'
|
|
6
6
|
import { Type } from '../components/Type.tsx'
|
|
7
7
|
import { ENUM_TYPES_WITH_KEY_SUFFIX } from '../constants.ts'
|
|
8
|
+
import { printerTs } from '../printers/printerTs.ts'
|
|
8
9
|
import type { PluginTs } from '../types'
|
|
9
10
|
import { buildData, buildResponses, buildResponseUnion } from '../utils.ts'
|
|
10
11
|
|
|
11
12
|
export const typeGenerator = defineGenerator<PluginTs>({
|
|
12
13
|
name: 'typescript',
|
|
13
14
|
type: 'react',
|
|
14
|
-
Schema({ node, adapter, options, config, resolver }) {
|
|
15
|
-
const { enumType, enumTypeSuffix, enumKeyCasing, syntaxType, optionalType, arrayType, output, group,
|
|
15
|
+
Schema({ node, adapter, options, config, resolver, plugin }) {
|
|
16
|
+
const { enumType, enumTypeSuffix, enumKeyCasing, syntaxType, optionalType, arrayType, output, group, printer } = options
|
|
16
17
|
|
|
17
|
-
const transformedNode = transform(node,
|
|
18
|
+
const transformedNode = plugin.transformer ? transform(node, plugin.transformer) : node
|
|
18
19
|
|
|
19
20
|
if (!transformedNode.name) {
|
|
20
21
|
return
|
|
@@ -30,7 +31,7 @@ export const typeGenerator = defineGenerator<PluginTs>({
|
|
|
30
31
|
if (ENUM_TYPES_WITH_KEY_SUFFIX.has(enumType) && enumTypeSuffix && enumSchemaNames.has(schemaName)) {
|
|
31
32
|
return resolver.resolveEnumKeyName({ name: schemaName }, enumTypeSuffix)
|
|
32
33
|
}
|
|
33
|
-
return resolver.
|
|
34
|
+
return resolver.resolveTypeName(schemaName)
|
|
34
35
|
}
|
|
35
36
|
|
|
36
37
|
const imports = adapter.getImports(transformedNode, (schemaName) => ({
|
|
@@ -44,10 +45,23 @@ export const typeGenerator = defineGenerator<PluginTs>({
|
|
|
44
45
|
name:
|
|
45
46
|
ENUM_TYPES_WITH_KEY_SUFFIX.has(enumType) && isEnumSchema
|
|
46
47
|
? resolver.resolveEnumKeyName(transformedNode, enumTypeSuffix)
|
|
47
|
-
: resolver.
|
|
48
|
+
: resolver.resolveTypeName(transformedNode.name),
|
|
48
49
|
file: resolver.resolveFile({ name: transformedNode.name, extname: '.ts' }, { root, output, group }),
|
|
49
50
|
} as const
|
|
50
51
|
|
|
52
|
+
const schemaPrinter = printerTs({
|
|
53
|
+
optionalType,
|
|
54
|
+
arrayType,
|
|
55
|
+
enumType,
|
|
56
|
+
enumTypeSuffix,
|
|
57
|
+
name: meta.name,
|
|
58
|
+
syntaxType,
|
|
59
|
+
description: transformedNode.description,
|
|
60
|
+
resolver,
|
|
61
|
+
enumSchemaNames,
|
|
62
|
+
nodes: printer?.nodes,
|
|
63
|
+
})
|
|
64
|
+
|
|
51
65
|
return (
|
|
52
66
|
<File
|
|
53
67
|
baseName={meta.file.baseName}
|
|
@@ -66,19 +80,16 @@ export const typeGenerator = defineGenerator<PluginTs>({
|
|
|
66
80
|
enumType={enumType}
|
|
67
81
|
enumTypeSuffix={enumTypeSuffix}
|
|
68
82
|
enumKeyCasing={enumKeyCasing}
|
|
69
|
-
optionalType={optionalType}
|
|
70
|
-
arrayType={arrayType}
|
|
71
|
-
syntaxType={syntaxType}
|
|
72
83
|
resolver={resolver}
|
|
73
|
-
|
|
84
|
+
printer={schemaPrinter}
|
|
74
85
|
/>
|
|
75
86
|
</File>
|
|
76
87
|
)
|
|
77
88
|
},
|
|
78
|
-
Operation({ node, adapter, options, config, resolver }) {
|
|
79
|
-
const { enumType, enumTypeSuffix, enumKeyCasing, optionalType, arrayType, syntaxType, paramsCasing, group, output,
|
|
89
|
+
Operation({ node, adapter, options, config, resolver, plugin }) {
|
|
90
|
+
const { enumType, enumTypeSuffix, enumKeyCasing, optionalType, arrayType, syntaxType, paramsCasing, group, output, printer } = options
|
|
80
91
|
|
|
81
|
-
const transformedNode = transform(node,
|
|
92
|
+
const transformedNode = plugin.transformer ? transform(node, plugin.transformer) : node
|
|
82
93
|
|
|
83
94
|
const root = path.resolve(config.root, config.output.path)
|
|
84
95
|
const mode = getMode(path.resolve(root, output.path))
|
|
@@ -100,7 +111,7 @@ export const typeGenerator = defineGenerator<PluginTs>({
|
|
|
100
111
|
if (ENUM_TYPES_WITH_KEY_SUFFIX.has(enumType) && enumTypeSuffix && enumSchemaNames.has(schemaName)) {
|
|
101
112
|
return resolver.resolveEnumKeyName({ name: schemaName }, enumTypeSuffix)
|
|
102
113
|
}
|
|
103
|
-
return resolver.
|
|
114
|
+
return resolver.resolveTypeName(schemaName)
|
|
104
115
|
}
|
|
105
116
|
|
|
106
117
|
function renderSchemaType({ schema, name, keysToOmit }: { schema: SchemaNode | null; name: string; keysToOmit?: Array<string> }) {
|
|
@@ -111,6 +122,20 @@ export const typeGenerator = defineGenerator<PluginTs>({
|
|
|
111
122
|
path: resolver.resolveFile({ name: schemaName, extname: '.ts' }, { root, output, group }).path,
|
|
112
123
|
}))
|
|
113
124
|
|
|
125
|
+
const schemaPrinter = printerTs({
|
|
126
|
+
optionalType,
|
|
127
|
+
arrayType,
|
|
128
|
+
enumType,
|
|
129
|
+
enumTypeSuffix,
|
|
130
|
+
name,
|
|
131
|
+
syntaxType,
|
|
132
|
+
description: schema.description,
|
|
133
|
+
keysToOmit,
|
|
134
|
+
resolver,
|
|
135
|
+
enumSchemaNames,
|
|
136
|
+
nodes: printer?.nodes,
|
|
137
|
+
})
|
|
138
|
+
|
|
114
139
|
return (
|
|
115
140
|
<>
|
|
116
141
|
{mode === 'split' &&
|
|
@@ -123,12 +148,8 @@ export const typeGenerator = defineGenerator<PluginTs>({
|
|
|
123
148
|
enumType={enumType}
|
|
124
149
|
enumTypeSuffix={enumTypeSuffix}
|
|
125
150
|
enumKeyCasing={enumKeyCasing}
|
|
126
|
-
optionalType={optionalType}
|
|
127
|
-
arrayType={arrayType}
|
|
128
|
-
syntaxType={syntaxType}
|
|
129
151
|
resolver={resolver}
|
|
130
|
-
|
|
131
|
-
enumSchemaNames={enumSchemaNames}
|
|
152
|
+
printer={schemaPrinter}
|
|
132
153
|
/>
|
|
133
154
|
</>
|
|
134
155
|
)
|
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
import path from 'node:path'
|
|
2
2
|
import { pascalCase } from '@internals/utils'
|
|
3
|
-
import { caseParams,
|
|
3
|
+
import { caseParams, createProperty, createSchema, narrowSchema, schemaTypes, transform } from '@kubb/ast'
|
|
4
4
|
import type { OperationNode, ParameterNode, SchemaNode } from '@kubb/ast/types'
|
|
5
5
|
import { defineGenerator, getMode } from '@kubb/core'
|
|
6
6
|
import { File } from '@kubb/react-fabric'
|
|
7
7
|
import { Type } from '../components/Type.tsx'
|
|
8
8
|
import { ENUM_TYPES_WITH_KEY_SUFFIX } from '../constants.ts'
|
|
9
|
+
import { printerTs } from '../printers/printerTs.ts'
|
|
9
10
|
import { resolverTsLegacy } from '../resolvers/resolverTsLegacy.ts'
|
|
10
11
|
import type { PluginTs, ResolverTs } from '../types'
|
|
11
12
|
|
|
@@ -158,10 +159,10 @@ function nameUnnamedEnums(node: SchemaNode, parentName: string): SchemaNode {
|
|
|
158
159
|
export const typeGeneratorLegacy = defineGenerator<PluginTs>({
|
|
159
160
|
name: 'typescript-legacy',
|
|
160
161
|
type: 'react',
|
|
161
|
-
Schema({ node, adapter, options, config, resolver }) {
|
|
162
|
-
const { enumType, enumTypeSuffix, enumKeyCasing, syntaxType, optionalType, arrayType, output, group
|
|
162
|
+
Schema({ node, adapter, options, config, resolver, plugin }) {
|
|
163
|
+
const { enumType, enumTypeSuffix, enumKeyCasing, syntaxType, optionalType, arrayType, output, group } = options
|
|
163
164
|
|
|
164
|
-
const transformedNode = transform(node,
|
|
165
|
+
const transformedNode = plugin.transformer ? transform(node, plugin.transformer) : node
|
|
165
166
|
|
|
166
167
|
if (!transformedNode.name) {
|
|
167
168
|
return
|
|
@@ -171,7 +172,7 @@ export const typeGeneratorLegacy = defineGenerator<PluginTs>({
|
|
|
171
172
|
const mode = getMode(path.resolve(root, output.path))
|
|
172
173
|
|
|
173
174
|
const imports = adapter.getImports(transformedNode, (schemaName) => ({
|
|
174
|
-
name: resolver.
|
|
175
|
+
name: resolver.resolveTypeName(schemaName),
|
|
175
176
|
path: resolver.resolveFile({ name: schemaName, extname: '.ts' }, { root, output, group }).path,
|
|
176
177
|
}))
|
|
177
178
|
|
|
@@ -181,10 +182,21 @@ export const typeGeneratorLegacy = defineGenerator<PluginTs>({
|
|
|
181
182
|
name:
|
|
182
183
|
ENUM_TYPES_WITH_KEY_SUFFIX.has(enumType) && isEnumSchema
|
|
183
184
|
? resolver.resolveEnumKeyName(transformedNode, enumTypeSuffix)
|
|
184
|
-
: resolver.
|
|
185
|
+
: resolver.resolveTypeName(transformedNode.name),
|
|
185
186
|
file: resolver.resolveFile({ name: transformedNode.name, extname: '.ts' }, { root, output, group }),
|
|
186
187
|
} as const
|
|
187
188
|
|
|
189
|
+
const schemaPrinter = printerTs({
|
|
190
|
+
optionalType,
|
|
191
|
+
arrayType,
|
|
192
|
+
enumType,
|
|
193
|
+
enumTypeSuffix,
|
|
194
|
+
name: meta.name,
|
|
195
|
+
syntaxType,
|
|
196
|
+
description: transformedNode.description,
|
|
197
|
+
resolver,
|
|
198
|
+
})
|
|
199
|
+
|
|
188
200
|
return (
|
|
189
201
|
<File
|
|
190
202
|
baseName={meta.file.baseName}
|
|
@@ -203,18 +215,16 @@ export const typeGeneratorLegacy = defineGenerator<PluginTs>({
|
|
|
203
215
|
enumType={enumType}
|
|
204
216
|
enumTypeSuffix={enumTypeSuffix}
|
|
205
217
|
enumKeyCasing={enumKeyCasing}
|
|
206
|
-
optionalType={optionalType}
|
|
207
|
-
arrayType={arrayType}
|
|
208
|
-
syntaxType={syntaxType}
|
|
209
218
|
resolver={resolver}
|
|
219
|
+
printer={schemaPrinter}
|
|
210
220
|
/>
|
|
211
221
|
</File>
|
|
212
222
|
)
|
|
213
223
|
},
|
|
214
|
-
Operation({ node, adapter, options, config, resolver }) {
|
|
215
|
-
const { enumType, enumTypeSuffix, enumKeyCasing, optionalType, arrayType, syntaxType, paramsCasing, group, output
|
|
224
|
+
Operation({ node, adapter, options, config, resolver, plugin }) {
|
|
225
|
+
const { enumType, enumTypeSuffix, enumKeyCasing, optionalType, arrayType, syntaxType, paramsCasing, group, output } = options
|
|
216
226
|
|
|
217
|
-
const transformedNode = transform(node,
|
|
227
|
+
const transformedNode = plugin.transformer ? transform(node, plugin.transformer) : node
|
|
218
228
|
|
|
219
229
|
const root = path.resolve(config.root, config.output.path)
|
|
220
230
|
const mode = getMode(path.resolve(root, output.path))
|
|
@@ -241,10 +251,22 @@ export const typeGeneratorLegacy = defineGenerator<PluginTs>({
|
|
|
241
251
|
if (!schema) return null
|
|
242
252
|
|
|
243
253
|
const imports = adapter.getImports(schema, (schemaName) => ({
|
|
244
|
-
name: resolver.
|
|
254
|
+
name: resolver.resolveTypeName(schemaName),
|
|
245
255
|
path: resolver.resolveFile({ name: schemaName, extname: '.ts' }, { root, output, group }).path,
|
|
246
256
|
}))
|
|
247
257
|
|
|
258
|
+
const opPrinter = printerTs({
|
|
259
|
+
optionalType,
|
|
260
|
+
arrayType,
|
|
261
|
+
enumType,
|
|
262
|
+
enumTypeSuffix,
|
|
263
|
+
name,
|
|
264
|
+
syntaxType,
|
|
265
|
+
description,
|
|
266
|
+
keysToOmit,
|
|
267
|
+
resolver,
|
|
268
|
+
})
|
|
269
|
+
|
|
248
270
|
return (
|
|
249
271
|
<>
|
|
250
272
|
{mode === 'split' &&
|
|
@@ -254,15 +276,11 @@ export const typeGeneratorLegacy = defineGenerator<PluginTs>({
|
|
|
254
276
|
<Type
|
|
255
277
|
name={name}
|
|
256
278
|
node={schema}
|
|
257
|
-
description={description}
|
|
258
279
|
enumType={enumType}
|
|
259
280
|
enumTypeSuffix={enumTypeSuffix}
|
|
260
281
|
enumKeyCasing={enumKeyCasing}
|
|
261
|
-
optionalType={optionalType}
|
|
262
|
-
arrayType={arrayType}
|
|
263
|
-
syntaxType={syntaxType}
|
|
264
282
|
resolver={resolver}
|
|
265
|
-
|
|
283
|
+
printer={opPrinter}
|
|
266
284
|
/>
|
|
267
285
|
</>
|
|
268
286
|
)
|
package/src/index.ts
CHANGED
|
@@ -3,6 +3,7 @@ export { Type } from './components/Type.tsx'
|
|
|
3
3
|
export { typeGenerator } from './generators/typeGenerator.tsx'
|
|
4
4
|
export { pluginTs, pluginTsName } from './plugin.ts'
|
|
5
5
|
export { functionPrinter } from './printers/functionPrinter.ts'
|
|
6
|
+
export type { PrinterTsFactory, PrinterTsNodes, PrinterTsOptions } from './printers/printerTs.ts'
|
|
6
7
|
export { printerTs } from './printers/printerTs.ts'
|
|
7
8
|
export { resolverTs } from './resolvers/resolverTs.ts'
|
|
8
9
|
export { resolverTsLegacy } from './resolvers/resolverTsLegacy.ts'
|
package/src/plugin.ts
CHANGED
|
@@ -41,17 +41,18 @@ export const pluginTs = createPlugin<PluginTs>((options) => {
|
|
|
41
41
|
arrayType = 'array',
|
|
42
42
|
syntaxType = 'type',
|
|
43
43
|
paramsCasing,
|
|
44
|
+
printer,
|
|
44
45
|
compatibilityPreset = 'default',
|
|
45
|
-
|
|
46
|
-
|
|
46
|
+
resolver: userResolver,
|
|
47
|
+
transformer: userTransformer,
|
|
47
48
|
generators: userGenerators = [],
|
|
48
49
|
} = options
|
|
49
50
|
|
|
50
51
|
const preset = getPreset({
|
|
51
52
|
preset: compatibilityPreset,
|
|
52
53
|
presets: presets,
|
|
53
|
-
|
|
54
|
-
|
|
54
|
+
resolver: userResolver,
|
|
55
|
+
transformer: userTransformer,
|
|
55
56
|
generators: userGenerators,
|
|
56
57
|
})
|
|
57
58
|
|
|
@@ -63,6 +64,9 @@ export const pluginTs = createPlugin<PluginTs>((options) => {
|
|
|
63
64
|
get resolver() {
|
|
64
65
|
return preset.resolver
|
|
65
66
|
},
|
|
67
|
+
get transformer() {
|
|
68
|
+
return preset.transformer
|
|
69
|
+
},
|
|
66
70
|
get options() {
|
|
67
71
|
return {
|
|
68
72
|
output,
|
|
@@ -84,7 +88,7 @@ export const pluginTs = createPlugin<PluginTs>((options) => {
|
|
|
84
88
|
enumKeyCasing,
|
|
85
89
|
syntaxType,
|
|
86
90
|
paramsCasing,
|
|
87
|
-
|
|
91
|
+
printer,
|
|
88
92
|
}
|
|
89
93
|
},
|
|
90
94
|
resolvePath(baseName, pathMode, options) {
|
package/src/presets.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { definePresets } from '@kubb/core'
|
|
2
2
|
import { typeGenerator } from './generators/typeGenerator.tsx'
|
|
3
3
|
import { typeGeneratorLegacy } from './generators/typeGeneratorLegacy.tsx'
|
|
4
|
+
import { printerTs } from './printers/printerTs.ts'
|
|
4
5
|
import { resolverTs } from './resolvers/resolverTs.ts'
|
|
5
6
|
import { resolverTsLegacy } from './resolvers/resolverTsLegacy.ts'
|
|
6
7
|
import type { ResolverTs } from './types.ts'
|
|
@@ -14,12 +15,14 @@ import type { ResolverTs } from './types.ts'
|
|
|
14
15
|
export const presets = definePresets<ResolverTs>({
|
|
15
16
|
default: {
|
|
16
17
|
name: 'default',
|
|
17
|
-
|
|
18
|
+
resolver: resolverTs,
|
|
18
19
|
generators: [typeGenerator],
|
|
20
|
+
printer: printerTs,
|
|
19
21
|
},
|
|
20
22
|
kubbV4: {
|
|
21
23
|
name: 'kubbV4',
|
|
22
|
-
|
|
24
|
+
resolver: resolverTsLegacy,
|
|
23
25
|
generators: [typeGeneratorLegacy],
|
|
26
|
+
printer: printerTs,
|
|
24
27
|
},
|
|
25
28
|
})
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { extractRefName, isStringType, narrowSchema, schemaTypes, syncSchemaRef } from '@kubb/ast'
|
|
2
|
-
import type { PrinterFactoryOptions } from '@kubb/core'
|
|
2
|
+
import type { PrinterFactoryOptions, PrinterPartial } from '@kubb/core'
|
|
3
3
|
import { definePrinter } from '@kubb/core'
|
|
4
4
|
import { safePrint } from '@kubb/fabric-core/parsers/typescript'
|
|
5
5
|
import type ts from 'typescript'
|
|
@@ -8,7 +8,29 @@ import * as factory from '../factory.ts'
|
|
|
8
8
|
import type { PluginTs, ResolverTs } from '../types.ts'
|
|
9
9
|
import { buildPropertyJSDocComments } from '../utils.ts'
|
|
10
10
|
|
|
11
|
-
|
|
11
|
+
/**
|
|
12
|
+
* Partial map of node-type overrides for the TypeScript printer.
|
|
13
|
+
*
|
|
14
|
+
* Each key is a `SchemaType` string (e.g. `'date'`, `'string'`). The function
|
|
15
|
+
* replaces the built-in handler for that node type. Use `this.transform` to
|
|
16
|
+
* recurse into nested schema nodes, and `this.options` to read printer options.
|
|
17
|
+
*
|
|
18
|
+
* @example Override the `date` handler
|
|
19
|
+
* ```ts
|
|
20
|
+
* pluginTs({
|
|
21
|
+
* printer: {
|
|
22
|
+
* nodes: {
|
|
23
|
+
* date(node) {
|
|
24
|
+
* return ts.factory.createTypeReferenceNode('Date', [])
|
|
25
|
+
* },
|
|
26
|
+
* },
|
|
27
|
+
* },
|
|
28
|
+
* })
|
|
29
|
+
* ```
|
|
30
|
+
*/
|
|
31
|
+
export type PrinterTsNodes = PrinterPartial<ts.TypeNode, PrinterTsOptions>
|
|
32
|
+
|
|
33
|
+
export type PrinterTsOptions = {
|
|
12
34
|
/**
|
|
13
35
|
* @default `'questionToken'`
|
|
14
36
|
*/
|
|
@@ -57,12 +79,18 @@ type TsOptions = {
|
|
|
57
79
|
* instead of the plain PascalCase name, so imports align with what the enum file actually exports.
|
|
58
80
|
*/
|
|
59
81
|
enumSchemaNames?: Set<string>
|
|
82
|
+
/**
|
|
83
|
+
* Partial map of node-type overrides. Each entry replaces the built-in handler for that node type.
|
|
84
|
+
*/
|
|
85
|
+
nodes?: PrinterTsNodes
|
|
60
86
|
}
|
|
61
87
|
|
|
62
88
|
/**
|
|
63
89
|
* TypeScript printer factory options: maps `SchemaNode` → `ts.TypeNode` (raw) or `ts.Node` (full declaration).
|
|
64
90
|
*/
|
|
65
|
-
type
|
|
91
|
+
export type PrinterTsFactory = PrinterFactoryOptions<'typescript', PrinterTsOptions, ts.TypeNode, string>
|
|
92
|
+
|
|
93
|
+
type PrinterTs = PrinterTsFactory
|
|
66
94
|
|
|
67
95
|
/**
|
|
68
96
|
* TypeScript type printer built with `definePrinter`.
|
|
@@ -87,7 +115,7 @@ type TsPrinter = PrinterFactoryOptions<'typescript', TsOptions, ts.TypeNode, str
|
|
|
87
115
|
* const declaration = printer.print(schemaNode) // ts.TypeAliasDeclaration | ts.InterfaceDeclaration
|
|
88
116
|
* ```
|
|
89
117
|
*/
|
|
90
|
-
export const printerTs = definePrinter<
|
|
118
|
+
export const printerTs = definePrinter<PrinterTs>((options) => {
|
|
91
119
|
const addsUndefined = OPTIONAL_ADDS_UNDEFINED.has(options.optionalType)
|
|
92
120
|
|
|
93
121
|
return {
|
|
@@ -227,6 +255,7 @@ export const printerTs = definePrinter<TsPrinter>((options) => {
|
|
|
227
255
|
|
|
228
256
|
return factory.createTypeLiteralNode(allElements)
|
|
229
257
|
},
|
|
258
|
+
...options.nodes,
|
|
230
259
|
},
|
|
231
260
|
print(node) {
|
|
232
261
|
const { name, syntaxType = 'type', description, keysToOmit } = this.options
|
|
@@ -2,10 +2,6 @@ import { pascalCase } from '@internals/utils'
|
|
|
2
2
|
import { defineResolver } from '@kubb/core'
|
|
3
3
|
import type { PluginTs } from '../types.ts'
|
|
4
4
|
|
|
5
|
-
function toTypeName(name: string, type?: 'file' | 'function' | 'type' | 'const'): string {
|
|
6
|
-
return pascalCase(name, { isFile: type === 'file' })
|
|
7
|
-
}
|
|
8
|
-
|
|
9
5
|
/**
|
|
10
6
|
* Resolver for `@kubb/plugin-ts` that provides the default naming and path-resolution
|
|
11
7
|
* helpers used by the plugin. Import this in other plugins to resolve the exact names and
|
|
@@ -28,34 +24,34 @@ export const resolverTs = defineResolver<PluginTs>(() => {
|
|
|
28
24
|
name: 'default',
|
|
29
25
|
pluginName: 'plugin-ts',
|
|
30
26
|
default(name, type) {
|
|
31
|
-
return
|
|
27
|
+
return pascalCase(name, { isFile: type === 'file' })
|
|
32
28
|
},
|
|
33
|
-
|
|
34
|
-
return
|
|
29
|
+
resolveTypeName(name) {
|
|
30
|
+
return pascalCase(name)
|
|
35
31
|
},
|
|
36
32
|
resolvePathName(name, type) {
|
|
37
|
-
return
|
|
33
|
+
return pascalCase(name, { isFile: type === 'file' })
|
|
38
34
|
},
|
|
39
35
|
resolveParamName(node, param) {
|
|
40
|
-
return this.
|
|
36
|
+
return this.resolveTypeName(`${node.operationId} ${param.in} ${param.name}`)
|
|
41
37
|
},
|
|
42
38
|
resolveResponseStatusName(node, statusCode) {
|
|
43
|
-
return this.
|
|
39
|
+
return this.resolveTypeName(`${node.operationId} Status ${statusCode}`)
|
|
44
40
|
},
|
|
45
41
|
resolveDataName(node) {
|
|
46
|
-
return this.
|
|
42
|
+
return this.resolveTypeName(`${node.operationId} Data`)
|
|
47
43
|
},
|
|
48
44
|
resolveRequestConfigName(node) {
|
|
49
|
-
return this.
|
|
45
|
+
return this.resolveTypeName(`${node.operationId} RequestConfig`)
|
|
50
46
|
},
|
|
51
47
|
resolveResponsesName(node) {
|
|
52
|
-
return this.
|
|
48
|
+
return this.resolveTypeName(`${node.operationId} Responses`)
|
|
53
49
|
},
|
|
54
50
|
resolveResponseName(node) {
|
|
55
|
-
return this.
|
|
51
|
+
return this.resolveTypeName(`${node.operationId} Response`)
|
|
56
52
|
},
|
|
57
53
|
resolveEnumKeyName(node, enumTypeSuffix = 'key') {
|
|
58
|
-
return `${this.
|
|
54
|
+
return `${this.resolveTypeName(node.name ?? '')}${enumTypeSuffix}`
|
|
59
55
|
},
|
|
60
56
|
resolvePathParamsName(node, param) {
|
|
61
57
|
return this.resolveParamName(node, param)
|
|
@@ -31,30 +31,30 @@ export const resolverTsLegacy = defineResolver<PluginTs>(() => {
|
|
|
31
31
|
pluginName: 'plugin-ts',
|
|
32
32
|
resolveResponseStatusName(node, statusCode) {
|
|
33
33
|
if (statusCode === 'default') {
|
|
34
|
-
return this.
|
|
34
|
+
return this.resolveTypeName(`${node.operationId} Error`)
|
|
35
35
|
}
|
|
36
|
-
return this.
|
|
36
|
+
return this.resolveTypeName(`${node.operationId} ${statusCode}`)
|
|
37
37
|
},
|
|
38
38
|
resolveDataName(node) {
|
|
39
39
|
const suffix = node.method === 'GET' ? 'QueryRequest' : 'MutationRequest'
|
|
40
|
-
return this.
|
|
40
|
+
return this.resolveTypeName(`${node.operationId} ${suffix}`)
|
|
41
41
|
},
|
|
42
42
|
resolveResponsesName(node) {
|
|
43
43
|
const suffix = node.method === 'GET' ? 'Query' : 'Mutation'
|
|
44
|
-
return this.
|
|
44
|
+
return this.resolveTypeName(`${node.operationId} ${suffix}`)
|
|
45
45
|
},
|
|
46
46
|
resolveResponseName(node) {
|
|
47
47
|
const suffix = node.method === 'GET' ? 'QueryResponse' : 'MutationResponse'
|
|
48
|
-
return this.
|
|
48
|
+
return this.resolveTypeName(`${node.operationId} ${suffix}`)
|
|
49
49
|
},
|
|
50
50
|
resolvePathParamsName(node, _param) {
|
|
51
|
-
return this.
|
|
51
|
+
return this.resolveTypeName(`${node.operationId} PathParams`)
|
|
52
52
|
},
|
|
53
53
|
resolveQueryParamsName(node, _param) {
|
|
54
|
-
return this.
|
|
54
|
+
return this.resolveTypeName(`${node.operationId} QueryParams`)
|
|
55
55
|
},
|
|
56
56
|
resolveHeaderParamsName(node, _param) {
|
|
57
|
-
return this.
|
|
57
|
+
return this.resolveTypeName(`${node.operationId} HeaderParams`)
|
|
58
58
|
},
|
|
59
59
|
}
|
|
60
60
|
})
|