@kubb/core 5.0.0-alpha.20 → 5.0.0-alpha.21
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/{PluginDriver-BkSenc-R.d.ts → PluginDriver-CEQPafXV.d.ts} +271 -104
- package/dist/hooks.cjs +7 -94
- package/dist/hooks.cjs.map +1 -1
- package/dist/hooks.d.ts +6 -79
- package/dist/hooks.js +6 -91
- package/dist/hooks.js.map +1 -1
- package/dist/index.cjs +313 -32
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +260 -24
- package/dist/index.js +308 -33
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
- package/src/defineBuilder.ts +26 -0
- package/src/defineGenerator.ts +3 -0
- package/src/definePreset.ts +9 -5
- package/src/defineResolver.ts +333 -16
- package/src/hooks/index.ts +1 -2
- package/src/hooks/{usePluginDriver.ts → useDriver.ts} +1 -4
- package/src/hooks/useMode.ts +1 -1
- package/src/hooks/usePlugin.ts +1 -1
- package/src/index.ts +10 -1
- package/src/renderNode.tsx +10 -13
- package/src/types.ts +204 -16
- package/src/utils/getBarrelFiles.ts +1 -1
- package/src/utils/getPreset.ts +17 -6
- package/src/hooks/useKubb.ts +0 -160
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kubb/core",
|
|
3
|
-
"version": "5.0.0-alpha.
|
|
3
|
+
"version": "5.0.0-alpha.21",
|
|
4
4
|
"description": "Core functionality for Kubb's plugin-based code generation system, providing the foundation for transforming OpenAPI specifications.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"typescript",
|
|
@@ -71,7 +71,7 @@
|
|
|
71
71
|
"remeda": "^2.33.6",
|
|
72
72
|
"semver": "^7.7.4",
|
|
73
73
|
"tinyexec": "^1.0.4",
|
|
74
|
-
"@kubb/ast": "5.0.0-alpha.
|
|
74
|
+
"@kubb/ast": "5.0.0-alpha.21"
|
|
75
75
|
},
|
|
76
76
|
"devDependencies": {
|
|
77
77
|
"@types/semver": "^7.7.1",
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type { PluginFactoryOptions } from './types.ts'
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Builder type for the plugin-specific builder fields.
|
|
5
|
+
* `name` is required; all other methods are defined by the concrete plugin builder type.
|
|
6
|
+
*/
|
|
7
|
+
type BuilderBuilder<T extends PluginFactoryOptions> = () => T['builder'] & ThisType<T['builder']>
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Defines a builder for a plugin — a named collection of schema-building helpers that
|
|
11
|
+
* can be exported alongside the plugin and imported by other plugins or generators.
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* export const builder = defineBuilder<PluginTs>(() => ({
|
|
15
|
+
* name: 'default',
|
|
16
|
+
* buildParamsSchema({ params, node, resolver }) {
|
|
17
|
+
* return createSchema({ type: 'object', properties: [] })
|
|
18
|
+
* },
|
|
19
|
+
* buildDataSchemaNode({ node, resolver }) {
|
|
20
|
+
* return createSchema({ type: 'object', properties: [] })
|
|
21
|
+
* },
|
|
22
|
+
* }))
|
|
23
|
+
*/
|
|
24
|
+
export function defineBuilder<T extends PluginFactoryOptions>(build: BuilderBuilder<T>): T['builder'] {
|
|
25
|
+
return build() as T['builder']
|
|
26
|
+
}
|
package/src/defineGenerator.ts
CHANGED
|
@@ -10,6 +10,7 @@ export type Version = '1' | '2'
|
|
|
10
10
|
*/
|
|
11
11
|
export type OperationsV2Props<TPlugin extends PluginFactoryOptions = PluginFactoryOptions> = {
|
|
12
12
|
config: Config
|
|
13
|
+
plugin: Plugin<TPlugin>
|
|
13
14
|
adapter: Adapter
|
|
14
15
|
options: Plugin<TPlugin>['options']
|
|
15
16
|
nodes: Array<OperationNode>
|
|
@@ -21,6 +22,7 @@ export type OperationsV2Props<TPlugin extends PluginFactoryOptions = PluginFacto
|
|
|
21
22
|
export type OperationV2Props<TPlugin extends PluginFactoryOptions = PluginFactoryOptions> = {
|
|
22
23
|
config: Config
|
|
23
24
|
adapter: Adapter
|
|
25
|
+
plugin: Plugin<TPlugin>
|
|
24
26
|
options: Plugin<TPlugin>['options']
|
|
25
27
|
node: OperationNode
|
|
26
28
|
}
|
|
@@ -31,6 +33,7 @@ export type OperationV2Props<TPlugin extends PluginFactoryOptions = PluginFactor
|
|
|
31
33
|
export type SchemaV2Props<TPlugin extends PluginFactoryOptions = PluginFactoryOptions> = {
|
|
32
34
|
config: Config
|
|
33
35
|
adapter: Adapter
|
|
36
|
+
plugin: Plugin<TPlugin>
|
|
34
37
|
options: Plugin<TPlugin>['options']
|
|
35
38
|
node: SchemaNode
|
|
36
39
|
}
|
package/src/definePreset.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import type { Visitor } from '@kubb/ast/types'
|
|
2
|
-
import type { Preset, Resolver } from './types.ts'
|
|
2
|
+
import type { Generator, Preset, Resolver } from './types.ts'
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
|
-
* Creates a typed preset object that bundles a name, resolvers,
|
|
6
|
-
* transformers — the building block for composable plugin presets.
|
|
5
|
+
* Creates a typed preset object that bundles a name, resolvers, optional
|
|
6
|
+
* transformers, and optional generators — the building block for composable plugin presets.
|
|
7
7
|
*
|
|
8
8
|
* @example
|
|
9
9
|
* import { definePreset } from '@kubb/core'
|
|
@@ -14,10 +14,14 @@ import type { Preset, Resolver } from './types.ts'
|
|
|
14
14
|
* @example
|
|
15
15
|
* // With custom transformers
|
|
16
16
|
* export const myPreset = definePreset('myPreset', { resolvers: [resolverTsLegacy], transformers: [myTransformer] })
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* // With generators
|
|
20
|
+
* export const myPreset = definePreset('myPreset', { resolvers: [resolverTsLegacy], generators: [typeGeneratorLegacy] })
|
|
17
21
|
*/
|
|
18
22
|
export function definePreset<TResolver extends Resolver = Resolver, TName extends string = string>(
|
|
19
23
|
name: TName,
|
|
20
|
-
{ resolvers, transformers }: { resolvers: Array<TResolver>; transformers?: Array<Visitor
|
|
24
|
+
{ resolvers, transformers, generators }: { resolvers: Array<TResolver>; transformers?: Array<Visitor>; generators?: Array<Generator<any>> },
|
|
21
25
|
): Preset<TResolver> & { name: TName } {
|
|
22
|
-
return { name, resolvers, transformers }
|
|
26
|
+
return { name, resolvers, transformers, generators }
|
|
23
27
|
}
|
package/src/defineResolver.ts
CHANGED
|
@@ -1,14 +1,35 @@
|
|
|
1
|
+
import path from 'node:path'
|
|
1
2
|
import { camelCase, pascalCase } from '@internals/utils'
|
|
2
3
|
import { isOperationNode, isSchemaNode } from '@kubb/ast'
|
|
3
|
-
import type { Node, OperationNode, SchemaNode } from '@kubb/ast/types'
|
|
4
|
-
import type {
|
|
4
|
+
import type { Node, OperationNode, RootNode, SchemaNode } from '@kubb/ast/types'
|
|
5
|
+
import type { KubbFile } from '@kubb/fabric-core/types'
|
|
6
|
+
import { getMode } from './PluginDriver.ts'
|
|
7
|
+
import type {
|
|
8
|
+
Config,
|
|
9
|
+
PluginFactoryOptions,
|
|
10
|
+
ResolveBannerContext,
|
|
11
|
+
ResolveNameParams,
|
|
12
|
+
ResolveOptionsContext,
|
|
13
|
+
Resolver,
|
|
14
|
+
ResolverContext,
|
|
15
|
+
ResolverFileParams,
|
|
16
|
+
ResolverPathParams,
|
|
17
|
+
} from './types.ts'
|
|
5
18
|
|
|
6
19
|
/**
|
|
7
20
|
* Builder type for the plugin-specific resolver fields.
|
|
8
|
-
*
|
|
21
|
+
*
|
|
22
|
+
* `default`, `resolveOptions`, `resolvePath`, `resolveFile`, `resolveBanner`, and `resolveFooter`
|
|
23
|
+
* are optional — built-in fallbacks are injected when omitted.
|
|
9
24
|
*/
|
|
10
|
-
type ResolverBuilder<T extends PluginFactoryOptions> = () => Omit<
|
|
11
|
-
|
|
25
|
+
type ResolverBuilder<T extends PluginFactoryOptions> = () => Omit<
|
|
26
|
+
T['resolver'],
|
|
27
|
+
'default' | 'resolveOptions' | 'resolvePath' | 'resolveFile' | 'resolveBanner' | 'resolveFooter' | 'name' | 'pluginName'
|
|
28
|
+
> &
|
|
29
|
+
Partial<Pick<T['resolver'], 'default' | 'resolveOptions' | 'resolvePath' | 'resolveFile' | 'resolveBanner' | 'resolveFooter'>> & {
|
|
30
|
+
name: string
|
|
31
|
+
pluginName: T['name']
|
|
32
|
+
} & ThisType<T['resolver']>
|
|
12
33
|
|
|
13
34
|
/**
|
|
14
35
|
* Checks if an operation matches a pattern for a given filter type (`tag`, `operationId`, `path`, `method`).
|
|
@@ -30,6 +51,7 @@ function matchesOperationPattern(node: OperationNode, type: string, pattern: str
|
|
|
30
51
|
|
|
31
52
|
/**
|
|
32
53
|
* Checks if a schema matches a pattern for a given filter type (`schemaName`).
|
|
54
|
+
*
|
|
33
55
|
* Returns `null` when the filter type doesn't apply to schemas.
|
|
34
56
|
*/
|
|
35
57
|
function matchesSchemaPattern(node: SchemaNode, type: string, pattern: string | RegExp): boolean | null {
|
|
@@ -42,7 +64,11 @@ function matchesSchemaPattern(node: SchemaNode, type: string, pattern: string |
|
|
|
42
64
|
}
|
|
43
65
|
|
|
44
66
|
/**
|
|
45
|
-
* Default name resolver
|
|
67
|
+
* Default name resolver used by `defineResolver`.
|
|
68
|
+
*
|
|
69
|
+
* - `camelCase` for `function` and `file` types.
|
|
70
|
+
* - `PascalCase` for `type`.
|
|
71
|
+
* - `camelCase` for everything else.
|
|
46
72
|
*/
|
|
47
73
|
function defaultResolver(name: ResolveNameParams['name'], type: ResolveNameParams['type']): string {
|
|
48
74
|
let resolvedName = camelCase(name)
|
|
@@ -61,8 +87,27 @@ function defaultResolver(name: ResolveNameParams['name'], type: ResolveNameParam
|
|
|
61
87
|
}
|
|
62
88
|
|
|
63
89
|
/**
|
|
64
|
-
* Default option resolver — applies include/exclude filters and merges
|
|
65
|
-
*
|
|
90
|
+
* Default option resolver — applies include/exclude filters and merges matching override options.
|
|
91
|
+
*
|
|
92
|
+
* Returns `null` when the node is filtered out by an `exclude` rule or not matched by any `include` rule.
|
|
93
|
+
*
|
|
94
|
+
* @example Include/exclude filtering
|
|
95
|
+
* ```ts
|
|
96
|
+
* const options = defaultResolveOptions(operationNode, {
|
|
97
|
+
* options: { output: 'types' },
|
|
98
|
+
* exclude: [{ type: 'tag', pattern: 'internal' }],
|
|
99
|
+
* })
|
|
100
|
+
* // → null when node has tag 'internal'
|
|
101
|
+
* ```
|
|
102
|
+
*
|
|
103
|
+
* @example Override merging
|
|
104
|
+
* ```ts
|
|
105
|
+
* const options = defaultResolveOptions(operationNode, {
|
|
106
|
+
* options: { enumType: 'asConst' },
|
|
107
|
+
* override: [{ type: 'operationId', pattern: 'listPets', options: { enumType: 'enum' } }],
|
|
108
|
+
* })
|
|
109
|
+
* // → { enumType: 'enum' } when operationId matches
|
|
110
|
+
* ```
|
|
66
111
|
*/
|
|
67
112
|
export function defaultResolveOptions<TOptions>(
|
|
68
113
|
node: Node,
|
|
@@ -105,27 +150,299 @@ export function defaultResolveOptions<TOptions>(
|
|
|
105
150
|
}
|
|
106
151
|
|
|
107
152
|
/**
|
|
108
|
-
*
|
|
109
|
-
*
|
|
153
|
+
* Default path resolver used by `defineResolver`.
|
|
154
|
+
*
|
|
155
|
+
* - Returns the output directory in `single` mode.
|
|
156
|
+
* - Resolves into a tag- or path-based subdirectory when `group` and a `tag`/`path` value are provided.
|
|
157
|
+
* - Falls back to a flat `output/baseName` path otherwise.
|
|
158
|
+
*
|
|
159
|
+
* A custom `group.name` function overrides the default subdirectory naming.
|
|
160
|
+
* For `tag` groups the default is `${camelCase(tag)}Controller`.
|
|
161
|
+
* For `path` groups the default is the first path segment after `/`.
|
|
162
|
+
*
|
|
163
|
+
* @example Flat output
|
|
164
|
+
* ```ts
|
|
165
|
+
* defaultResolvePath({ baseName: 'petTypes.ts' }, { root: '/src', output: { path: 'types' } })
|
|
166
|
+
* // → '/src/types/petTypes.ts'
|
|
167
|
+
* ```
|
|
168
|
+
*
|
|
169
|
+
* @example Tag-based grouping
|
|
170
|
+
* ```ts
|
|
171
|
+
* defaultResolvePath(
|
|
172
|
+
* { baseName: 'petTypes.ts', tag: 'pets' },
|
|
173
|
+
* { root: '/src', output: { path: 'types' }, group: { type: 'tag' } },
|
|
174
|
+
* )
|
|
175
|
+
* // → '/src/types/petsController/petTypes.ts'
|
|
176
|
+
* ```
|
|
177
|
+
*
|
|
178
|
+
* @example Path-based grouping
|
|
179
|
+
* ```ts
|
|
180
|
+
* defaultResolvePath(
|
|
181
|
+
* { baseName: 'petTypes.ts', path: '/pets/list' },
|
|
182
|
+
* { root: '/src', output: { path: 'types' }, group: { type: 'path' } },
|
|
183
|
+
* )
|
|
184
|
+
* // → '/src/types/pets/petTypes.ts'
|
|
185
|
+
* ```
|
|
186
|
+
*
|
|
187
|
+
* @example Single-file mode
|
|
188
|
+
* ```ts
|
|
189
|
+
* defaultResolvePath(
|
|
190
|
+
* { baseName: 'petTypes.ts', pathMode: 'single' },
|
|
191
|
+
* { root: '/src', output: { path: 'types' } },
|
|
192
|
+
* )
|
|
193
|
+
* // → '/src/types'
|
|
194
|
+
* ```
|
|
195
|
+
*/
|
|
196
|
+
export function defaultResolvePath({ baseName, pathMode, tag, path: groupPath }: ResolverPathParams, { root, output, group }: ResolverContext): KubbFile.Path {
|
|
197
|
+
const mode = pathMode ?? getMode(path.resolve(root, output.path))
|
|
198
|
+
|
|
199
|
+
if (mode === 'single') {
|
|
200
|
+
return path.resolve(root, output.path) as KubbFile.Path
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
if (group && (groupPath || tag)) {
|
|
204
|
+
const groupName = group.name
|
|
205
|
+
? group.name
|
|
206
|
+
: (ctx: { group: string }) => {
|
|
207
|
+
if (group.type === 'path') {
|
|
208
|
+
return `${ctx.group.split('/')[1]}`
|
|
209
|
+
}
|
|
210
|
+
return `${camelCase(ctx.group)}Controller`
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
return path.resolve(root, output.path, groupName({ group: group.type === 'path' ? groupPath! : tag! }), baseName) as KubbFile.Path
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
return path.resolve(root, output.path, baseName) as KubbFile.Path
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
/**
|
|
220
|
+
* Default file resolver used by `defineResolver`.
|
|
221
|
+
*
|
|
222
|
+
* Resolves a `KubbFile.File` by combining name resolution (`resolver.default`) with
|
|
223
|
+
* path resolution (`resolver.resolvePath`). The resolved file always has empty
|
|
224
|
+
* `sources`, `imports`, and `exports` arrays — consumers populate those separately.
|
|
225
|
+
*
|
|
226
|
+
* In `single` mode the name is omitted and the file sits directly in the output directory.
|
|
227
|
+
*
|
|
228
|
+
* @example Resolve a schema file
|
|
229
|
+
* ```ts
|
|
230
|
+
* const file = defaultResolveFile.call(resolver,
|
|
231
|
+
* { name: 'pet', extname: '.ts' },
|
|
232
|
+
* { root: '/src', output: { path: 'types' } },
|
|
233
|
+
* )
|
|
234
|
+
* // → { baseName: 'pet.ts', path: '/src/types/pet.ts', sources: [], ... }
|
|
235
|
+
* ```
|
|
236
|
+
*
|
|
237
|
+
* @example Resolve an operation file with tag grouping
|
|
238
|
+
* ```ts
|
|
239
|
+
* const file = defaultResolveFile.call(resolver,
|
|
240
|
+
* { name: 'listPets', extname: '.ts', tag: 'pets' },
|
|
241
|
+
* { root: '/src', output: { path: 'types' }, group: { type: 'tag' } },
|
|
242
|
+
* )
|
|
243
|
+
* // → { baseName: 'listPets.ts', path: '/src/types/petsController/listPets.ts', ... }
|
|
244
|
+
* ```
|
|
245
|
+
*/
|
|
246
|
+
export function defaultResolveFile(this: Resolver, { name, extname, tag, path: groupPath }: ResolverFileParams, context: ResolverContext): KubbFile.File {
|
|
247
|
+
const pathMode = getMode(path.resolve(context.root, context.output.path))
|
|
248
|
+
const resolvedName = pathMode === 'single' ? '' : this.default(name, 'file')
|
|
249
|
+
const baseName = `${resolvedName}${extname}` as KubbFile.BaseName
|
|
250
|
+
const filePath = this.resolvePath({ baseName, pathMode, tag, path: groupPath }, context)
|
|
251
|
+
|
|
252
|
+
return {
|
|
253
|
+
path: filePath,
|
|
254
|
+
baseName: path.basename(filePath) as KubbFile.BaseName,
|
|
255
|
+
meta: {
|
|
256
|
+
pluginName: this.pluginName,
|
|
257
|
+
},
|
|
258
|
+
sources: [],
|
|
259
|
+
imports: [],
|
|
260
|
+
exports: [],
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
/**
|
|
265
|
+
* Generates the default "Generated by Kubb" banner from config and optional node metadata.
|
|
266
|
+
*/
|
|
267
|
+
export function buildDefaultBanner({
|
|
268
|
+
title,
|
|
269
|
+
description,
|
|
270
|
+
version,
|
|
271
|
+
config,
|
|
272
|
+
}: {
|
|
273
|
+
title?: string
|
|
274
|
+
description?: string
|
|
275
|
+
version?: string
|
|
276
|
+
config: Config
|
|
277
|
+
}): string {
|
|
278
|
+
try {
|
|
279
|
+
let source = ''
|
|
280
|
+
if (Array.isArray(config.input)) {
|
|
281
|
+
const first = config.input[0]
|
|
282
|
+
if (first && 'path' in first) {
|
|
283
|
+
source = path.basename(first.path)
|
|
284
|
+
}
|
|
285
|
+
} else if ('path' in config.input) {
|
|
286
|
+
source = path.basename(config.input.path)
|
|
287
|
+
} else if ('data' in config.input) {
|
|
288
|
+
source = 'text content'
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
let banner = '/**\n* Generated by Kubb (https://kubb.dev/).\n* Do not edit manually.\n'
|
|
292
|
+
|
|
293
|
+
if (config.output.defaultBanner === 'simple') {
|
|
294
|
+
banner += '*/\n'
|
|
295
|
+
return banner
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
if (source) {
|
|
299
|
+
banner += `* Source: ${source}\n`
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
if (title) {
|
|
303
|
+
banner += `* Title: ${title}\n`
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
if (description) {
|
|
307
|
+
const formattedDescription = description.replace(/\n/gm, '\n* ')
|
|
308
|
+
banner += `* Description: ${formattedDescription}\n`
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
if (version) {
|
|
312
|
+
banner += `* OpenAPI spec version: ${version}\n`
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
banner += '*/\n'
|
|
316
|
+
return banner
|
|
317
|
+
} catch (_error) {
|
|
318
|
+
return '/**\n* Generated by Kubb (https://kubb.dev/).\n* Do not edit manually.\n*/'
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
/**
|
|
323
|
+
* Default banner resolver — returns the banner string for a generated file.
|
|
110
324
|
*
|
|
111
|
-
*
|
|
325
|
+
* - When `output.banner` is a function and `node` is provided, calls it with the node.
|
|
326
|
+
* - When `output.banner` is a function and `node` is absent, falls back to the default Kubb banner.
|
|
327
|
+
* - When `output.banner` is a string, returns it directly.
|
|
328
|
+
* - When `config.output.defaultBanner` is `false`, returns `undefined`.
|
|
329
|
+
* - Otherwise returns the default "Generated by Kubb" banner.
|
|
330
|
+
*
|
|
331
|
+
* @example String banner
|
|
332
|
+
* ```ts
|
|
333
|
+
* defaultResolveBanner(undefined, { output: { banner: '// my banner' }, config })
|
|
334
|
+
* // → '// my banner'
|
|
335
|
+
* ```
|
|
336
|
+
*
|
|
337
|
+
* @example Function banner with node
|
|
338
|
+
* ```ts
|
|
339
|
+
* defaultResolveBanner(rootNode, { output: { banner: (node) => `// v${node.version}` }, config })
|
|
340
|
+
* // → '// v3.0.0'
|
|
341
|
+
* ```
|
|
342
|
+
*
|
|
343
|
+
* @example Disabled banner
|
|
344
|
+
* ```ts
|
|
345
|
+
* defaultResolveBanner(undefined, { config: { output: { defaultBanner: false }, ...config } })
|
|
346
|
+
* // → undefined
|
|
347
|
+
* ```
|
|
348
|
+
*/
|
|
349
|
+
export function defaultResolveBanner(node: RootNode | undefined, { output, config }: ResolveBannerContext): string | undefined {
|
|
350
|
+
if (typeof output?.banner === 'function') {
|
|
351
|
+
return node ? output.banner(node) : buildDefaultBanner({ config })
|
|
352
|
+
}
|
|
353
|
+
if (typeof output?.banner === 'string') {
|
|
354
|
+
return output.banner
|
|
355
|
+
}
|
|
356
|
+
if (config.output.defaultBanner === false) {
|
|
357
|
+
return undefined
|
|
358
|
+
}
|
|
359
|
+
return buildDefaultBanner({ config })
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
/**
|
|
363
|
+
* Default footer resolver — returns the footer string for a generated file.
|
|
364
|
+
*
|
|
365
|
+
* - When `output.footer` is a function and `node` is provided, calls it with the node.
|
|
366
|
+
* - When `output.footer` is a function and `node` is absent, returns `undefined`.
|
|
367
|
+
* - When `output.footer` is a string, returns it directly.
|
|
368
|
+
* - Otherwise returns `undefined`.
|
|
369
|
+
*
|
|
370
|
+
* @example String footer
|
|
371
|
+
* ```ts
|
|
372
|
+
* defaultResolveFooter(undefined, { output: { footer: '// end of file' }, config })
|
|
373
|
+
* // → '// end of file'
|
|
374
|
+
* ```
|
|
375
|
+
*
|
|
376
|
+
* @example Function footer with node
|
|
377
|
+
* ```ts
|
|
378
|
+
* defaultResolveFooter(rootNode, { output: { footer: (node) => `// ${node.title}` }, config })
|
|
379
|
+
* // → '// Pet Store'
|
|
380
|
+
* ```
|
|
381
|
+
*/
|
|
382
|
+
export function defaultResolveFooter(node: RootNode | undefined, { output }: ResolveBannerContext): string | undefined {
|
|
383
|
+
if (typeof output?.footer === 'function') {
|
|
384
|
+
return node ? output.footer(node) : undefined
|
|
385
|
+
}
|
|
386
|
+
if (typeof output?.footer === 'string') {
|
|
387
|
+
return output.footer
|
|
388
|
+
}
|
|
389
|
+
return undefined
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
/**
|
|
393
|
+
* Defines a resolver for a plugin, injecting built-in defaults for name casing,
|
|
394
|
+
* include/exclude/override filtering, path resolution, and file construction.
|
|
395
|
+
*
|
|
396
|
+
* All four defaults can be overridden by providing them in the builder function:
|
|
397
|
+
* - `default` — name casing strategy (camelCase / PascalCase)
|
|
398
|
+
* - `resolveOptions` — include/exclude/override filtering
|
|
399
|
+
* - `resolvePath` — output path computation
|
|
400
|
+
* - `resolveFile` — full `KubbFile.File` construction
|
|
401
|
+
*
|
|
402
|
+
* Methods in the builder have access to `this` (the full resolver object), so they
|
|
403
|
+
* can call other resolver methods without circular imports.
|
|
404
|
+
*
|
|
405
|
+
* @example Basic resolver with naming helpers
|
|
406
|
+
* ```ts
|
|
112
407
|
* export const resolver = defineResolver<PluginTs>(() => ({
|
|
113
408
|
* name: 'default',
|
|
114
|
-
* resolveName(
|
|
115
|
-
* return this.default(name, 'function')
|
|
409
|
+
* resolveName(node) {
|
|
410
|
+
* return this.default(node.name, 'function')
|
|
411
|
+
* },
|
|
412
|
+
* resolveTypedName(node) {
|
|
413
|
+
* return this.default(node.name, 'type')
|
|
116
414
|
* },
|
|
117
|
-
*
|
|
118
|
-
*
|
|
415
|
+
* }))
|
|
416
|
+
* ```
|
|
417
|
+
*
|
|
418
|
+
* @example Override resolvePath for a custom output structure
|
|
419
|
+
* ```ts
|
|
420
|
+
* export const resolver = defineResolver<PluginTs>(() => ({
|
|
421
|
+
* name: 'custom',
|
|
422
|
+
* resolvePath({ baseName }, { root, output }) {
|
|
423
|
+
* return path.resolve(root, output.path, 'generated', baseName)
|
|
119
424
|
* },
|
|
425
|
+
* }))
|
|
426
|
+
* ```
|
|
427
|
+
*
|
|
428
|
+
* @example Use this.default inside a helper
|
|
429
|
+
* ```ts
|
|
430
|
+
* export const resolver = defineResolver<PluginTs>(() => ({
|
|
431
|
+
* name: 'default',
|
|
120
432
|
* resolveParamName(node, param) {
|
|
121
|
-
* return this.
|
|
433
|
+
* return this.default(`${node.operationId} ${param.in} ${param.name}`, 'type')
|
|
122
434
|
* },
|
|
123
435
|
* }))
|
|
436
|
+
* ```
|
|
124
437
|
*/
|
|
125
438
|
export function defineResolver<T extends PluginFactoryOptions>(build: ResolverBuilder<T>): T['resolver'] {
|
|
126
439
|
return {
|
|
127
440
|
default: defaultResolver,
|
|
128
441
|
resolveOptions: defaultResolveOptions,
|
|
442
|
+
resolvePath: defaultResolvePath,
|
|
443
|
+
resolveFile: defaultResolveFile,
|
|
444
|
+
resolveBanner: defaultResolveBanner,
|
|
445
|
+
resolveFooter: defaultResolveFooter,
|
|
129
446
|
...build(),
|
|
130
447
|
} as T['resolver']
|
|
131
448
|
}
|
package/src/hooks/index.ts
CHANGED
|
@@ -1,10 +1,7 @@
|
|
|
1
1
|
import { useFabric } from '@kubb/react-fabric'
|
|
2
2
|
import type { PluginDriver } from '../PluginDriver.ts'
|
|
3
3
|
|
|
4
|
-
|
|
5
|
-
* @deprecated use `useKubb` instead
|
|
6
|
-
*/
|
|
7
|
-
export function usePluginDriver(): PluginDriver {
|
|
4
|
+
export function useDriver(): PluginDriver {
|
|
8
5
|
const { meta } = useFabric<{ driver: PluginDriver }>()
|
|
9
6
|
|
|
10
7
|
return meta.driver
|
package/src/hooks/useMode.ts
CHANGED
|
@@ -2,7 +2,7 @@ import type { KubbFile } from '@kubb/fabric-core/types'
|
|
|
2
2
|
import { useFabric } from '@kubb/react-fabric'
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
|
-
* @deprecated use `
|
|
5
|
+
* @deprecated use `mode` from the generator component props instead
|
|
6
6
|
*/
|
|
7
7
|
export function useMode(): KubbFile.Mode {
|
|
8
8
|
const { meta } = useFabric<{ mode: KubbFile.Mode }>()
|
package/src/hooks/usePlugin.ts
CHANGED
|
@@ -2,7 +2,7 @@ import { useFabric } from '@kubb/react-fabric'
|
|
|
2
2
|
import type { Plugin, PluginFactoryOptions } from '../types.ts'
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
|
-
* @deprecated use
|
|
5
|
+
* @deprecated use `plugin` from the generator component props instead
|
|
6
6
|
*/
|
|
7
7
|
export function usePlugin<TOptions extends PluginFactoryOptions = PluginFactoryOptions>(): Plugin<TOptions> {
|
|
8
8
|
const { meta } = useFabric<{ plugin: Plugin<TOptions> }>()
|
package/src/index.ts
CHANGED
|
@@ -6,11 +6,20 @@ export { formatters, linters, logLevel } from './constants.ts'
|
|
|
6
6
|
export { createAdapter } from './createAdapter.ts'
|
|
7
7
|
export { createPlugin } from './createPlugin.ts'
|
|
8
8
|
export { createStorage } from './createStorage.ts'
|
|
9
|
+
export { defineBuilder } from './defineBuilder.ts'
|
|
9
10
|
export { defineGenerator } from './defineGenerator.ts'
|
|
10
11
|
export { defineLogger } from './defineLogger.ts'
|
|
11
12
|
export { definePreset } from './definePreset.ts'
|
|
12
13
|
export { definePresets } from './definePresets.ts'
|
|
13
|
-
export {
|
|
14
|
+
export {
|
|
15
|
+
buildDefaultBanner,
|
|
16
|
+
defaultResolveBanner,
|
|
17
|
+
defaultResolveFile,
|
|
18
|
+
defaultResolveFooter,
|
|
19
|
+
defaultResolveOptions,
|
|
20
|
+
defaultResolvePath,
|
|
21
|
+
defineResolver,
|
|
22
|
+
} from './defineResolver.ts'
|
|
14
23
|
export { getMode, PluginDriver } from './PluginDriver.ts'
|
|
15
24
|
export { renderOperation, renderOperations, renderSchema } from './renderNode.tsx'
|
|
16
25
|
export { fsStorage } from './storages/fsStorage.ts'
|
package/src/renderNode.tsx
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import type { OperationNode, SchemaNode } from '@kubb/ast/types'
|
|
2
|
-
import type { KubbFile } from '@kubb/fabric-core/types'
|
|
3
2
|
import { createReactFabric, Fabric } from '@kubb/react-fabric'
|
|
4
3
|
import type { Fabric as FabricType } from '@kubb/react-fabric/types'
|
|
5
4
|
import type { PluginDriver } from './PluginDriver.ts'
|
|
@@ -12,7 +11,6 @@ type BuildOperationsV2Options<TOptions extends PluginFactoryOptions> = {
|
|
|
12
11
|
Component: ReactGeneratorV2<TOptions>['Operations'] | undefined
|
|
13
12
|
adapter: Adapter
|
|
14
13
|
driver: PluginDriver
|
|
15
|
-
mode: KubbFile.Mode
|
|
16
14
|
options: TOptions['resolvedOptions']
|
|
17
15
|
}
|
|
18
16
|
|
|
@@ -23,7 +21,7 @@ export async function renderOperations<TOptions extends PluginFactoryOptions>(
|
|
|
23
21
|
nodes: Array<OperationNode>,
|
|
24
22
|
options: BuildOperationsV2Options<TOptions>,
|
|
25
23
|
): Promise<void> {
|
|
26
|
-
const { config, fabric, plugin, Component, adapter } = options
|
|
24
|
+
const { config, fabric, plugin, Component, driver, adapter } = options
|
|
27
25
|
|
|
28
26
|
if (!Component) {
|
|
29
27
|
return undefined
|
|
@@ -32,8 +30,8 @@ export async function renderOperations<TOptions extends PluginFactoryOptions>(
|
|
|
32
30
|
const fabricChild = createReactFabric()
|
|
33
31
|
|
|
34
32
|
await fabricChild.render(
|
|
35
|
-
<Fabric meta={{ plugin }}>
|
|
36
|
-
<Component config={config} adapter={adapter} nodes={nodes} options={options.options} />
|
|
33
|
+
<Fabric meta={{ plugin, driver }}>
|
|
34
|
+
<Component config={config} plugin={plugin} adapter={adapter} nodes={nodes} options={options.options} />
|
|
37
35
|
</Fabric>,
|
|
38
36
|
)
|
|
39
37
|
|
|
@@ -48,7 +46,6 @@ type BuildOperationV2Options<TOptions extends PluginFactoryOptions> = {
|
|
|
48
46
|
Component: ReactGeneratorV2<TOptions>['Operation'] | undefined
|
|
49
47
|
adapter: Adapter
|
|
50
48
|
driver: PluginDriver
|
|
51
|
-
mode: KubbFile.Mode
|
|
52
49
|
options: TOptions['resolvedOptions']
|
|
53
50
|
}
|
|
54
51
|
|
|
@@ -56,7 +53,7 @@ type BuildOperationV2Options<TOptions extends PluginFactoryOptions> = {
|
|
|
56
53
|
* Renders a React component for a single operation node (V2 generators).
|
|
57
54
|
*/
|
|
58
55
|
export async function renderOperation<TOptions extends PluginFactoryOptions>(node: OperationNode, options: BuildOperationV2Options<TOptions>): Promise<void> {
|
|
59
|
-
const { config, fabric, plugin, Component, adapter, driver
|
|
56
|
+
const { config, fabric, plugin, Component, adapter, driver } = options
|
|
60
57
|
|
|
61
58
|
if (!Component) {
|
|
62
59
|
return undefined
|
|
@@ -65,8 +62,8 @@ export async function renderOperation<TOptions extends PluginFactoryOptions>(nod
|
|
|
65
62
|
const fabricChild = createReactFabric()
|
|
66
63
|
|
|
67
64
|
await fabricChild.render(
|
|
68
|
-
<Fabric meta={{ plugin, driver
|
|
69
|
-
<Component config={config} adapter={adapter} node={node} options={options.options} />
|
|
65
|
+
<Fabric meta={{ plugin, driver }}>
|
|
66
|
+
<Component config={config} plugin={plugin} adapter={adapter} node={node} options={options.options} />
|
|
70
67
|
</Fabric>,
|
|
71
68
|
)
|
|
72
69
|
|
|
@@ -81,7 +78,6 @@ type BuildSchemaV2Options<TOptions extends PluginFactoryOptions> = {
|
|
|
81
78
|
Component: ReactGeneratorV2<TOptions>['Schema'] | undefined
|
|
82
79
|
adapter: Adapter
|
|
83
80
|
driver: PluginDriver
|
|
84
|
-
mode: KubbFile.Mode
|
|
85
81
|
options: TOptions['resolvedOptions']
|
|
86
82
|
}
|
|
87
83
|
|
|
@@ -89,7 +85,7 @@ type BuildSchemaV2Options<TOptions extends PluginFactoryOptions> = {
|
|
|
89
85
|
* Renders a React component for a single schema node (V2 generators).
|
|
90
86
|
*/
|
|
91
87
|
export async function renderSchema<TOptions extends PluginFactoryOptions>(node: SchemaNode, options: BuildSchemaV2Options<TOptions>): Promise<void> {
|
|
92
|
-
const { config, fabric, plugin, Component, adapter, driver
|
|
88
|
+
const { config, fabric, plugin, Component, adapter, driver } = options
|
|
93
89
|
|
|
94
90
|
if (!Component) {
|
|
95
91
|
return undefined
|
|
@@ -98,11 +94,12 @@ export async function renderSchema<TOptions extends PluginFactoryOptions>(node:
|
|
|
98
94
|
const fabricChild = createReactFabric()
|
|
99
95
|
|
|
100
96
|
await fabricChild.render(
|
|
101
|
-
<Fabric meta={{ plugin, driver
|
|
102
|
-
<Component config={config} adapter={adapter} node={node} options={options.options} />
|
|
97
|
+
<Fabric meta={{ plugin, driver }}>
|
|
98
|
+
<Component config={config} plugin={plugin} adapter={adapter} node={node} options={options.options} />
|
|
103
99
|
</Fabric>,
|
|
104
100
|
)
|
|
105
101
|
|
|
106
102
|
fabric.context.fileManager.upsert(...fabricChild.files)
|
|
103
|
+
|
|
107
104
|
fabricChild.unmount()
|
|
108
105
|
}
|