@kubb/core 5.0.0-alpha.2 → 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/{types-B7eZvqwD.d.ts → PluginDriver-CEQPafXV.d.ts} +687 -298
- package/dist/hooks.cjs +15 -9
- package/dist/hooks.cjs.map +1 -1
- package/dist/hooks.d.ts +11 -5
- package/dist/hooks.js +16 -10
- package/dist/hooks.js.map +1 -1
- package/dist/index.cjs +1131 -536
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +674 -89
- package/dist/index.js +1114 -532
- package/dist/index.js.map +1 -1
- package/package.json +6 -6
- package/src/Kubb.ts +37 -55
- package/src/{PluginManager.ts → PluginDriver.ts} +51 -40
- package/src/build.ts +74 -29
- package/src/config.ts +9 -8
- package/src/constants.ts +44 -1
- package/src/createAdapter.ts +25 -0
- package/src/createPlugin.ts +28 -0
- package/src/createStorage.ts +58 -0
- package/src/defineBuilder.ts +26 -0
- package/src/defineGenerator.ts +137 -0
- package/src/defineLogger.ts +13 -3
- package/src/definePreset.ts +27 -0
- package/src/definePresets.ts +16 -0
- package/src/defineResolver.ts +448 -0
- package/src/hooks/index.ts +1 -1
- package/src/hooks/useDriver.ts +8 -0
- package/src/hooks/useMode.ts +5 -2
- package/src/hooks/usePlugin.ts +5 -2
- package/src/index.ts +21 -6
- package/src/renderNode.tsx +105 -0
- package/src/storages/fsStorage.ts +2 -2
- package/src/storages/memoryStorage.ts +2 -2
- package/src/types.ts +342 -42
- package/src/utils/FunctionParams.ts +2 -2
- package/src/utils/TreeNode.ts +24 -1
- package/src/utils/diagnostics.ts +4 -1
- package/src/utils/executeStrategies.ts +23 -10
- package/src/utils/formatters.ts +10 -21
- package/src/utils/getBarrelFiles.ts +79 -9
- package/src/utils/getConfigs.ts +8 -22
- package/src/utils/getPreset.ts +52 -0
- package/src/utils/linters.ts +23 -3
- package/src/utils/mergeResolvers.ts +8 -0
- package/src/utils/packageJSON.ts +76 -0
- package/src/BarrelManager.ts +0 -74
- package/src/PackageManager.ts +0 -180
- package/src/PromiseManager.ts +0 -40
- package/src/defineAdapter.ts +0 -22
- package/src/definePlugin.ts +0 -12
- package/src/defineStorage.ts +0 -56
- package/src/errors.ts +0 -1
- package/src/hooks/usePluginManager.ts +0 -8
- package/src/utils/getPlugins.ts +0 -23
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { Preset, Presets, Resolver } from './types.ts'
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Creates a typed presets registry object — a named collection of {@link Preset} entries.
|
|
5
|
+
*
|
|
6
|
+
* @example
|
|
7
|
+
* import { definePreset, definePresets } from '@kubb/core'
|
|
8
|
+
* import { resolverTsLegacy } from '@kubb/plugin-ts'
|
|
9
|
+
*
|
|
10
|
+
* export const myPresets = definePresets({
|
|
11
|
+
* kubbV4: definePreset('kubbV4', { resolvers: [resolverTsLegacy] }),
|
|
12
|
+
* })
|
|
13
|
+
*/
|
|
14
|
+
export function definePresets<TResolver extends Resolver = Resolver>(presets: Presets<TResolver>): Presets<TResolver> {
|
|
15
|
+
return presets
|
|
16
|
+
}
|
|
@@ -0,0 +1,448 @@
|
|
|
1
|
+
import path from 'node:path'
|
|
2
|
+
import { camelCase, pascalCase } from '@internals/utils'
|
|
3
|
+
import { isOperationNode, isSchemaNode } from '@kubb/ast'
|
|
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'
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Builder type for the plugin-specific resolver fields.
|
|
21
|
+
*
|
|
22
|
+
* `default`, `resolveOptions`, `resolvePath`, `resolveFile`, `resolveBanner`, and `resolveFooter`
|
|
23
|
+
* are optional — built-in fallbacks are injected when omitted.
|
|
24
|
+
*/
|
|
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']>
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Checks if an operation matches a pattern for a given filter type (`tag`, `operationId`, `path`, `method`).
|
|
36
|
+
*/
|
|
37
|
+
function matchesOperationPattern(node: OperationNode, type: string, pattern: string | RegExp): boolean {
|
|
38
|
+
switch (type) {
|
|
39
|
+
case 'tag':
|
|
40
|
+
return node.tags.some((tag) => !!tag.match(pattern))
|
|
41
|
+
case 'operationId':
|
|
42
|
+
return !!node.operationId.match(pattern)
|
|
43
|
+
case 'path':
|
|
44
|
+
return !!node.path.match(pattern)
|
|
45
|
+
case 'method':
|
|
46
|
+
return !!(node.method.toLowerCase() as string).match(pattern)
|
|
47
|
+
default:
|
|
48
|
+
return false
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Checks if a schema matches a pattern for a given filter type (`schemaName`).
|
|
54
|
+
*
|
|
55
|
+
* Returns `null` when the filter type doesn't apply to schemas.
|
|
56
|
+
*/
|
|
57
|
+
function matchesSchemaPattern(node: SchemaNode, type: string, pattern: string | RegExp): boolean | null {
|
|
58
|
+
switch (type) {
|
|
59
|
+
case 'schemaName':
|
|
60
|
+
return node.name ? !!node.name.match(pattern) : false
|
|
61
|
+
default:
|
|
62
|
+
return null
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Default name resolver used by `defineResolver`.
|
|
68
|
+
*
|
|
69
|
+
* - `camelCase` for `function` and `file` types.
|
|
70
|
+
* - `PascalCase` for `type`.
|
|
71
|
+
* - `camelCase` for everything else.
|
|
72
|
+
*/
|
|
73
|
+
function defaultResolver(name: ResolveNameParams['name'], type: ResolveNameParams['type']): string {
|
|
74
|
+
let resolvedName = camelCase(name)
|
|
75
|
+
|
|
76
|
+
if (type === 'file' || type === 'function') {
|
|
77
|
+
resolvedName = camelCase(name, {
|
|
78
|
+
isFile: type === 'file',
|
|
79
|
+
})
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
if (type === 'type') {
|
|
83
|
+
resolvedName = pascalCase(name)
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
return resolvedName
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
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
|
+
* ```
|
|
111
|
+
*/
|
|
112
|
+
export function defaultResolveOptions<TOptions>(
|
|
113
|
+
node: Node,
|
|
114
|
+
{ options, exclude = [], include, override = [] }: ResolveOptionsContext<TOptions>,
|
|
115
|
+
): TOptions | null {
|
|
116
|
+
if (isOperationNode(node)) {
|
|
117
|
+
const isExcluded = exclude.some(({ type, pattern }) => matchesOperationPattern(node, type, pattern))
|
|
118
|
+
if (isExcluded) {
|
|
119
|
+
return null
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
if (include && !include.some(({ type, pattern }) => matchesOperationPattern(node, type, pattern))) {
|
|
123
|
+
return null
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
const overrideOptions = override.find(({ type, pattern }) => matchesOperationPattern(node, type, pattern))?.options
|
|
127
|
+
|
|
128
|
+
return { ...options, ...overrideOptions }
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
if (isSchemaNode(node)) {
|
|
132
|
+
if (exclude.some(({ type, pattern }) => matchesSchemaPattern(node, type, pattern) === true)) {
|
|
133
|
+
return null
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
if (include) {
|
|
137
|
+
const results = include.map(({ type, pattern }) => matchesSchemaPattern(node, type, pattern))
|
|
138
|
+
const applicable = results.filter((r) => r !== null)
|
|
139
|
+
if (applicable.length > 0 && !applicable.includes(true)) {
|
|
140
|
+
return null
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
const overrideOptions = override.find(({ type, pattern }) => matchesSchemaPattern(node, type, pattern) === true)?.options
|
|
145
|
+
|
|
146
|
+
return { ...options, ...overrideOptions }
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
return options
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/**
|
|
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.
|
|
324
|
+
*
|
|
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
|
|
407
|
+
* export const resolver = defineResolver<PluginTs>(() => ({
|
|
408
|
+
* name: 'default',
|
|
409
|
+
* resolveName(node) {
|
|
410
|
+
* return this.default(node.name, 'function')
|
|
411
|
+
* },
|
|
412
|
+
* resolveTypedName(node) {
|
|
413
|
+
* return this.default(node.name, 'type')
|
|
414
|
+
* },
|
|
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)
|
|
424
|
+
* },
|
|
425
|
+
* }))
|
|
426
|
+
* ```
|
|
427
|
+
*
|
|
428
|
+
* @example Use this.default inside a helper
|
|
429
|
+
* ```ts
|
|
430
|
+
* export const resolver = defineResolver<PluginTs>(() => ({
|
|
431
|
+
* name: 'default',
|
|
432
|
+
* resolveParamName(node, param) {
|
|
433
|
+
* return this.default(`${node.operationId} ${param.in} ${param.name}`, 'type')
|
|
434
|
+
* },
|
|
435
|
+
* }))
|
|
436
|
+
* ```
|
|
437
|
+
*/
|
|
438
|
+
export function defineResolver<T extends PluginFactoryOptions>(build: ResolverBuilder<T>): T['resolver'] {
|
|
439
|
+
return {
|
|
440
|
+
default: defaultResolver,
|
|
441
|
+
resolveOptions: defaultResolveOptions,
|
|
442
|
+
resolvePath: defaultResolvePath,
|
|
443
|
+
resolveFile: defaultResolveFile,
|
|
444
|
+
resolveBanner: defaultResolveBanner,
|
|
445
|
+
resolveFooter: defaultResolveFooter,
|
|
446
|
+
...build(),
|
|
447
|
+
} as T['resolver']
|
|
448
|
+
}
|
package/src/hooks/index.ts
CHANGED
package/src/hooks/useMode.ts
CHANGED
|
@@ -1,8 +1,11 @@
|
|
|
1
1
|
import type { KubbFile } from '@kubb/fabric-core/types'
|
|
2
|
-
import {
|
|
2
|
+
import { useFabric } from '@kubb/react-fabric'
|
|
3
3
|
|
|
4
|
+
/**
|
|
5
|
+
* @deprecated use `mode` from the generator component props instead
|
|
6
|
+
*/
|
|
4
7
|
export function useMode(): KubbFile.Mode {
|
|
5
|
-
const { meta } =
|
|
8
|
+
const { meta } = useFabric<{ mode: KubbFile.Mode }>()
|
|
6
9
|
|
|
7
10
|
return meta.mode
|
|
8
11
|
}
|
package/src/hooks/usePlugin.ts
CHANGED
|
@@ -1,8 +1,11 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { useFabric } from '@kubb/react-fabric'
|
|
2
2
|
import type { Plugin, PluginFactoryOptions } from '../types.ts'
|
|
3
3
|
|
|
4
|
+
/**
|
|
5
|
+
* @deprecated use `plugin` from the generator component props instead
|
|
6
|
+
*/
|
|
4
7
|
export function usePlugin<TOptions extends PluginFactoryOptions = PluginFactoryOptions>(): Plugin<TOptions> {
|
|
5
|
-
const { meta } =
|
|
8
|
+
const { meta } = useFabric<{ plugin: Plugin<TOptions> }>()
|
|
6
9
|
|
|
7
10
|
return meta.plugin
|
|
8
11
|
}
|
package/src/index.ts
CHANGED
|
@@ -3,13 +3,25 @@ export { definePrinter } from '@kubb/ast'
|
|
|
3
3
|
export { build, build as default, safeBuild, setup } from './build.ts'
|
|
4
4
|
export { type CLIOptions, type ConfigInput, defineConfig, isInputPath } from './config.ts'
|
|
5
5
|
export { formatters, linters, logLevel } from './constants.ts'
|
|
6
|
-
export {
|
|
6
|
+
export { createAdapter } from './createAdapter.ts'
|
|
7
|
+
export { createPlugin } from './createPlugin.ts'
|
|
8
|
+
export { createStorage } from './createStorage.ts'
|
|
9
|
+
export { defineBuilder } from './defineBuilder.ts'
|
|
10
|
+
export { defineGenerator } from './defineGenerator.ts'
|
|
7
11
|
export { defineLogger } from './defineLogger.ts'
|
|
8
|
-
export {
|
|
9
|
-
export {
|
|
10
|
-
export {
|
|
11
|
-
|
|
12
|
-
|
|
12
|
+
export { definePreset } from './definePreset.ts'
|
|
13
|
+
export { definePresets } from './definePresets.ts'
|
|
14
|
+
export {
|
|
15
|
+
buildDefaultBanner,
|
|
16
|
+
defaultResolveBanner,
|
|
17
|
+
defaultResolveFile,
|
|
18
|
+
defaultResolveFooter,
|
|
19
|
+
defaultResolveOptions,
|
|
20
|
+
defaultResolvePath,
|
|
21
|
+
defineResolver,
|
|
22
|
+
} from './defineResolver.ts'
|
|
23
|
+
export { getMode, PluginDriver } from './PluginDriver.ts'
|
|
24
|
+
export { renderOperation, renderOperations, renderSchema } from './renderNode.tsx'
|
|
13
25
|
export { fsStorage } from './storages/fsStorage.ts'
|
|
14
26
|
export { memoryStorage } from './storages/memoryStorage.ts'
|
|
15
27
|
export * from './types.ts'
|
|
@@ -19,4 +31,7 @@ export { detectFormatter } from './utils/formatters.ts'
|
|
|
19
31
|
export type { FileMetaBase } from './utils/getBarrelFiles.ts'
|
|
20
32
|
export { getBarrelFiles } from './utils/getBarrelFiles.ts'
|
|
21
33
|
export { getConfigs } from './utils/getConfigs.ts'
|
|
34
|
+
export { getPreset } from './utils/getPreset.ts'
|
|
22
35
|
export { detectLinter } from './utils/linters.ts'
|
|
36
|
+
export { mergeResolvers } from './utils/mergeResolvers.ts'
|
|
37
|
+
export { satisfiesDependency } from './utils/packageJSON.ts'
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import type { OperationNode, SchemaNode } from '@kubb/ast/types'
|
|
2
|
+
import { createReactFabric, Fabric } from '@kubb/react-fabric'
|
|
3
|
+
import type { Fabric as FabricType } from '@kubb/react-fabric/types'
|
|
4
|
+
import type { PluginDriver } from './PluginDriver.ts'
|
|
5
|
+
import type { Adapter, Config, Plugin, PluginFactoryOptions, ReactGeneratorV2 } from './types.ts'
|
|
6
|
+
|
|
7
|
+
type BuildOperationsV2Options<TOptions extends PluginFactoryOptions> = {
|
|
8
|
+
config: Config
|
|
9
|
+
fabric: FabricType
|
|
10
|
+
plugin: Plugin<TOptions>
|
|
11
|
+
Component: ReactGeneratorV2<TOptions>['Operations'] | undefined
|
|
12
|
+
adapter: Adapter
|
|
13
|
+
driver: PluginDriver
|
|
14
|
+
options: TOptions['resolvedOptions']
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Renders a React component for a list of operation nodes (V2 generators).
|
|
19
|
+
*/
|
|
20
|
+
export async function renderOperations<TOptions extends PluginFactoryOptions>(
|
|
21
|
+
nodes: Array<OperationNode>,
|
|
22
|
+
options: BuildOperationsV2Options<TOptions>,
|
|
23
|
+
): Promise<void> {
|
|
24
|
+
const { config, fabric, plugin, Component, driver, adapter } = options
|
|
25
|
+
|
|
26
|
+
if (!Component) {
|
|
27
|
+
return undefined
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const fabricChild = createReactFabric()
|
|
31
|
+
|
|
32
|
+
await fabricChild.render(
|
|
33
|
+
<Fabric meta={{ plugin, driver }}>
|
|
34
|
+
<Component config={config} plugin={plugin} adapter={adapter} nodes={nodes} options={options.options} />
|
|
35
|
+
</Fabric>,
|
|
36
|
+
)
|
|
37
|
+
|
|
38
|
+
fabric.context.fileManager.upsert(...fabricChild.files)
|
|
39
|
+
fabricChild.unmount()
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
type BuildOperationV2Options<TOptions extends PluginFactoryOptions> = {
|
|
43
|
+
config: Config
|
|
44
|
+
fabric: FabricType
|
|
45
|
+
plugin: Plugin<TOptions>
|
|
46
|
+
Component: ReactGeneratorV2<TOptions>['Operation'] | undefined
|
|
47
|
+
adapter: Adapter
|
|
48
|
+
driver: PluginDriver
|
|
49
|
+
options: TOptions['resolvedOptions']
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Renders a React component for a single operation node (V2 generators).
|
|
54
|
+
*/
|
|
55
|
+
export async function renderOperation<TOptions extends PluginFactoryOptions>(node: OperationNode, options: BuildOperationV2Options<TOptions>): Promise<void> {
|
|
56
|
+
const { config, fabric, plugin, Component, adapter, driver } = options
|
|
57
|
+
|
|
58
|
+
if (!Component) {
|
|
59
|
+
return undefined
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
const fabricChild = createReactFabric()
|
|
63
|
+
|
|
64
|
+
await fabricChild.render(
|
|
65
|
+
<Fabric meta={{ plugin, driver }}>
|
|
66
|
+
<Component config={config} plugin={plugin} adapter={adapter} node={node} options={options.options} />
|
|
67
|
+
</Fabric>,
|
|
68
|
+
)
|
|
69
|
+
|
|
70
|
+
fabric.context.fileManager.upsert(...fabricChild.files)
|
|
71
|
+
fabricChild.unmount()
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
type BuildSchemaV2Options<TOptions extends PluginFactoryOptions> = {
|
|
75
|
+
config: Config
|
|
76
|
+
fabric: FabricType
|
|
77
|
+
plugin: Plugin<TOptions>
|
|
78
|
+
Component: ReactGeneratorV2<TOptions>['Schema'] | undefined
|
|
79
|
+
adapter: Adapter
|
|
80
|
+
driver: PluginDriver
|
|
81
|
+
options: TOptions['resolvedOptions']
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Renders a React component for a single schema node (V2 generators).
|
|
86
|
+
*/
|
|
87
|
+
export async function renderSchema<TOptions extends PluginFactoryOptions>(node: SchemaNode, options: BuildSchemaV2Options<TOptions>): Promise<void> {
|
|
88
|
+
const { config, fabric, plugin, Component, adapter, driver } = options
|
|
89
|
+
|
|
90
|
+
if (!Component) {
|
|
91
|
+
return undefined
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
const fabricChild = createReactFabric()
|
|
95
|
+
|
|
96
|
+
await fabricChild.render(
|
|
97
|
+
<Fabric meta={{ plugin, driver }}>
|
|
98
|
+
<Component config={config} plugin={plugin} adapter={adapter} node={node} options={options.options} />
|
|
99
|
+
</Fabric>,
|
|
100
|
+
)
|
|
101
|
+
|
|
102
|
+
fabric.context.fileManager.upsert(...fabricChild.files)
|
|
103
|
+
|
|
104
|
+
fabricChild.unmount()
|
|
105
|
+
}
|
|
@@ -2,7 +2,7 @@ import type { Dirent } from 'node:fs'
|
|
|
2
2
|
import { access, readdir, readFile, rm } from 'node:fs/promises'
|
|
3
3
|
import { join, resolve } from 'node:path'
|
|
4
4
|
import { clean, write } from '@internals/utils'
|
|
5
|
-
import {
|
|
5
|
+
import { createStorage } from '../createStorage.ts'
|
|
6
6
|
|
|
7
7
|
/**
|
|
8
8
|
* Built-in filesystem storage driver.
|
|
@@ -27,7 +27,7 @@ import { defineStorage } from '../defineStorage.ts'
|
|
|
27
27
|
* })
|
|
28
28
|
* ```
|
|
29
29
|
*/
|
|
30
|
-
export const fsStorage =
|
|
30
|
+
export const fsStorage = createStorage(() => ({
|
|
31
31
|
name: 'fs',
|
|
32
32
|
async hasItem(key: string) {
|
|
33
33
|
try {
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { createStorage } from '../createStorage.ts'
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* In-memory storage driver. Useful for testing and dry-run scenarios where
|
|
@@ -17,7 +17,7 @@ import { defineStorage } from '../defineStorage.ts'
|
|
|
17
17
|
* })
|
|
18
18
|
* ```
|
|
19
19
|
*/
|
|
20
|
-
export const memoryStorage =
|
|
20
|
+
export const memoryStorage = createStorage(() => {
|
|
21
21
|
const store = new Map<string, string>()
|
|
22
22
|
|
|
23
23
|
return {
|