@kubb/core 5.0.0-alpha.3 → 5.0.0-alpha.31
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-D0dY_hpJ.d.ts +1986 -0
- package/dist/{chunk-ByKO4r7w.cjs → chunk-MlS0t1Af.cjs} +15 -0
- package/dist/chunk-O_arW02_.js +17 -0
- package/dist/hooks.cjs +13 -28
- package/dist/hooks.cjs.map +1 -1
- package/dist/hooks.d.ts +11 -37
- package/dist/hooks.js +14 -28
- package/dist/hooks.js.map +1 -1
- package/dist/index.cjs +1469 -831
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +572 -191
- package/dist/index.js +1443 -826
- package/dist/index.js.map +1 -1
- package/package.json +7 -7
- package/src/Kubb.ts +38 -56
- package/src/KubbFile.ts +143 -0
- package/src/{PluginManager.ts → PluginDriver.ts} +159 -170
- package/src/build.ts +213 -65
- package/src/constants.ts +39 -6
- package/src/createAdapter.ts +25 -0
- package/src/createPlugin.ts +30 -0
- package/src/createStorage.ts +58 -0
- package/src/{config.ts → defineConfig.ts} +11 -16
- package/src/defineGenerator.ts +126 -0
- package/src/defineLogger.ts +13 -3
- package/src/defineParser.ts +57 -0
- package/src/definePresets.ts +16 -0
- package/src/defineResolver.ts +454 -0
- package/src/hooks/index.ts +1 -6
- package/src/hooks/useDriver.ts +11 -0
- package/src/hooks/useMode.ts +4 -4
- package/src/hooks/usePlugin.ts +3 -3
- package/src/index.ts +22 -10
- package/src/renderNode.tsx +25 -0
- package/src/storages/fsStorage.ts +2 -2
- package/src/storages/memoryStorage.ts +2 -2
- package/src/types.ts +639 -52
- package/src/utils/FunctionParams.ts +2 -2
- package/src/utils/TreeNode.ts +40 -2
- package/src/utils/diagnostics.ts +4 -1
- package/src/utils/executeStrategies.ts +29 -10
- package/src/utils/formatters.ts +10 -21
- package/src/utils/getBarrelFiles.ts +80 -10
- package/src/utils/getConfigs.ts +9 -23
- package/src/utils/getPreset.ts +78 -0
- package/src/utils/isInputPath.ts +8 -0
- package/src/utils/linters.ts +23 -3
- package/src/utils/packageJSON.ts +76 -0
- package/dist/chunk--u3MIqq1.js +0 -8
- package/dist/types-CiPWLv-5.d.ts +0 -1001
- 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/useKubb.ts +0 -22
- package/src/hooks/usePluginManager.ts +0 -11
- package/src/utils/getPlugins.ts +0 -23
|
@@ -0,0 +1,454 @@
|
|
|
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 * as KubbFile from './KubbFile.ts'
|
|
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
|
+
case 'contentType':
|
|
48
|
+
return !!node.requestBody?.contentType?.match(pattern)
|
|
49
|
+
default:
|
|
50
|
+
return false
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Checks if a schema matches a pattern for a given filter type (`schemaName`).
|
|
56
|
+
*
|
|
57
|
+
* Returns `null` when the filter type doesn't apply to schemas.
|
|
58
|
+
*/
|
|
59
|
+
function matchesSchemaPattern(node: SchemaNode, type: string, pattern: string | RegExp): boolean | null {
|
|
60
|
+
switch (type) {
|
|
61
|
+
case 'schemaName':
|
|
62
|
+
return node.name ? !!node.name.match(pattern) : false
|
|
63
|
+
default:
|
|
64
|
+
return null
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Default name resolver used by `defineResolver`.
|
|
70
|
+
*
|
|
71
|
+
* - `camelCase` for `function` and `file` types.
|
|
72
|
+
* - `PascalCase` for `type`.
|
|
73
|
+
* - `camelCase` for everything else.
|
|
74
|
+
*/
|
|
75
|
+
function defaultResolver(name: ResolveNameParams['name'], type: ResolveNameParams['type']): string {
|
|
76
|
+
let resolvedName = camelCase(name)
|
|
77
|
+
|
|
78
|
+
if (type === 'file' || type === 'function') {
|
|
79
|
+
resolvedName = camelCase(name, {
|
|
80
|
+
isFile: type === 'file',
|
|
81
|
+
})
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
if (type === 'type') {
|
|
85
|
+
resolvedName = pascalCase(name)
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
return resolvedName
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Default option resolver — applies include/exclude filters and merges matching override options.
|
|
93
|
+
*
|
|
94
|
+
* Returns `null` when the node is filtered out by an `exclude` rule or not matched by any `include` rule.
|
|
95
|
+
*
|
|
96
|
+
* @example Include/exclude filtering
|
|
97
|
+
* ```ts
|
|
98
|
+
* const options = defaultResolveOptions(operationNode, {
|
|
99
|
+
* options: { output: 'types' },
|
|
100
|
+
* exclude: [{ type: 'tag', pattern: 'internal' }],
|
|
101
|
+
* })
|
|
102
|
+
* // → null when node has tag 'internal'
|
|
103
|
+
* ```
|
|
104
|
+
*
|
|
105
|
+
* @example Override merging
|
|
106
|
+
* ```ts
|
|
107
|
+
* const options = defaultResolveOptions(operationNode, {
|
|
108
|
+
* options: { enumType: 'asConst' },
|
|
109
|
+
* override: [{ type: 'operationId', pattern: 'listPets', options: { enumType: 'enum' } }],
|
|
110
|
+
* })
|
|
111
|
+
* // → { enumType: 'enum' } when operationId matches
|
|
112
|
+
* ```
|
|
113
|
+
*/
|
|
114
|
+
export function defaultResolveOptions<TOptions>(
|
|
115
|
+
node: Node,
|
|
116
|
+
{ options, exclude = [], include, override = [] }: ResolveOptionsContext<TOptions>,
|
|
117
|
+
): TOptions | null {
|
|
118
|
+
if (isOperationNode(node)) {
|
|
119
|
+
const isExcluded = exclude.some(({ type, pattern }) => matchesOperationPattern(node, type, pattern))
|
|
120
|
+
if (isExcluded) {
|
|
121
|
+
return null
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
if (include && !include.some(({ type, pattern }) => matchesOperationPattern(node, type, pattern))) {
|
|
125
|
+
return null
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
const overrideOptions = override.find(({ type, pattern }) => matchesOperationPattern(node, type, pattern))?.options
|
|
129
|
+
|
|
130
|
+
return { ...options, ...overrideOptions }
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
if (isSchemaNode(node)) {
|
|
134
|
+
if (exclude.some(({ type, pattern }) => matchesSchemaPattern(node, type, pattern) === true)) {
|
|
135
|
+
return null
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
if (include) {
|
|
139
|
+
const results = include.map(({ type, pattern }) => matchesSchemaPattern(node, type, pattern))
|
|
140
|
+
const applicable = results.filter((r) => r !== null)
|
|
141
|
+
if (applicable.length > 0 && !applicable.includes(true)) {
|
|
142
|
+
return null
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
const overrideOptions = override.find(({ type, pattern }) => matchesSchemaPattern(node, type, pattern) === true)?.options
|
|
147
|
+
|
|
148
|
+
return { ...options, ...overrideOptions }
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
return options
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* Default path resolver used by `defineResolver`.
|
|
156
|
+
*
|
|
157
|
+
* - Returns the output directory in `single` mode.
|
|
158
|
+
* - Resolves into a tag- or path-based subdirectory when `group` and a `tag`/`path` value are provided.
|
|
159
|
+
* - Falls back to a flat `output/baseName` path otherwise.
|
|
160
|
+
*
|
|
161
|
+
* A custom `group.name` function overrides the default subdirectory naming.
|
|
162
|
+
* For `tag` groups the default is `${camelCase(tag)}Controller`.
|
|
163
|
+
* For `path` groups the default is the first path segment after `/`.
|
|
164
|
+
*
|
|
165
|
+
* @example Flat output
|
|
166
|
+
* ```ts
|
|
167
|
+
* defaultResolvePath({ baseName: 'petTypes.ts' }, { root: '/src', output: { path: 'types' } })
|
|
168
|
+
* // → '/src/types/petTypes.ts'
|
|
169
|
+
* ```
|
|
170
|
+
*
|
|
171
|
+
* @example Tag-based grouping
|
|
172
|
+
* ```ts
|
|
173
|
+
* defaultResolvePath(
|
|
174
|
+
* { baseName: 'petTypes.ts', tag: 'pets' },
|
|
175
|
+
* { root: '/src', output: { path: 'types' }, group: { type: 'tag' } },
|
|
176
|
+
* )
|
|
177
|
+
* // → '/src/types/petsController/petTypes.ts'
|
|
178
|
+
* ```
|
|
179
|
+
*
|
|
180
|
+
* @example Path-based grouping
|
|
181
|
+
* ```ts
|
|
182
|
+
* defaultResolvePath(
|
|
183
|
+
* { baseName: 'petTypes.ts', path: '/pets/list' },
|
|
184
|
+
* { root: '/src', output: { path: 'types' }, group: { type: 'path' } },
|
|
185
|
+
* )
|
|
186
|
+
* // → '/src/types/pets/petTypes.ts'
|
|
187
|
+
* ```
|
|
188
|
+
*
|
|
189
|
+
* @example Single-file mode
|
|
190
|
+
* ```ts
|
|
191
|
+
* defaultResolvePath(
|
|
192
|
+
* { baseName: 'petTypes.ts', pathMode: 'single' },
|
|
193
|
+
* { root: '/src', output: { path: 'types' } },
|
|
194
|
+
* )
|
|
195
|
+
* // → '/src/types'
|
|
196
|
+
* ```
|
|
197
|
+
*/
|
|
198
|
+
export function defaultResolvePath({ baseName, pathMode, tag, path: groupPath }: ResolverPathParams, { root, output, group }: ResolverContext): KubbFile.Path {
|
|
199
|
+
const mode = pathMode ?? getMode(path.resolve(root, output.path))
|
|
200
|
+
|
|
201
|
+
if (mode === 'single') {
|
|
202
|
+
return path.resolve(root, output.path) as KubbFile.Path
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
if (group && (groupPath || tag)) {
|
|
206
|
+
return path.resolve(root, output.path, group.name({ group: group.type === 'path' ? groupPath! : tag! }), baseName) as KubbFile.Path
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
return path.resolve(root, output.path, baseName) as KubbFile.Path
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
/**
|
|
213
|
+
* Default file resolver used by `defineResolver`.
|
|
214
|
+
*
|
|
215
|
+
* Resolves a `KubbFile.File` by combining name resolution (`resolver.default`) with
|
|
216
|
+
* path resolution (`resolver.resolvePath`). The resolved file always has empty
|
|
217
|
+
* `sources`, `imports`, and `exports` arrays — consumers populate those separately.
|
|
218
|
+
*
|
|
219
|
+
* In `single` mode the name is omitted and the file sits directly in the output directory.
|
|
220
|
+
*
|
|
221
|
+
* @example Resolve a schema file
|
|
222
|
+
* ```ts
|
|
223
|
+
* const file = defaultResolveFile.call(resolver,
|
|
224
|
+
* { name: 'pet', extname: '.ts' },
|
|
225
|
+
* { root: '/src', output: { path: 'types' } },
|
|
226
|
+
* )
|
|
227
|
+
* // → { baseName: 'pet.ts', path: '/src/types/pet.ts', sources: [], ... }
|
|
228
|
+
* ```
|
|
229
|
+
*
|
|
230
|
+
* @example Resolve an operation file with tag grouping
|
|
231
|
+
* ```ts
|
|
232
|
+
* const file = defaultResolveFile.call(resolver,
|
|
233
|
+
* { name: 'listPets', extname: '.ts', tag: 'pets' },
|
|
234
|
+
* { root: '/src', output: { path: 'types' }, group: { type: 'tag' } },
|
|
235
|
+
* )
|
|
236
|
+
* // → { baseName: 'listPets.ts', path: '/src/types/petsController/listPets.ts', ... }
|
|
237
|
+
* ```
|
|
238
|
+
*/
|
|
239
|
+
export function defaultResolveFile(this: Resolver, { name, extname, tag, path: groupPath }: ResolverFileParams, context: ResolverContext): KubbFile.File {
|
|
240
|
+
const pathMode = getMode(path.resolve(context.root, context.output.path))
|
|
241
|
+
const resolvedName = pathMode === 'single' ? '' : this.default(name, 'file')
|
|
242
|
+
const baseName = `${resolvedName}${extname}` as KubbFile.BaseName
|
|
243
|
+
const filePath = this.resolvePath({ baseName, pathMode, tag, path: groupPath }, context)
|
|
244
|
+
|
|
245
|
+
return {
|
|
246
|
+
path: filePath,
|
|
247
|
+
baseName: path.basename(filePath) as KubbFile.BaseName,
|
|
248
|
+
meta: {
|
|
249
|
+
pluginName: this.pluginName,
|
|
250
|
+
},
|
|
251
|
+
sources: [],
|
|
252
|
+
imports: [],
|
|
253
|
+
exports: [],
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
/**
|
|
258
|
+
* Generates the default "Generated by Kubb" banner from config and optional node metadata.
|
|
259
|
+
*/
|
|
260
|
+
export function buildDefaultBanner({
|
|
261
|
+
title,
|
|
262
|
+
description,
|
|
263
|
+
version,
|
|
264
|
+
config,
|
|
265
|
+
}: {
|
|
266
|
+
title?: string
|
|
267
|
+
description?: string
|
|
268
|
+
version?: string
|
|
269
|
+
config: Config
|
|
270
|
+
}): string {
|
|
271
|
+
try {
|
|
272
|
+
let source = ''
|
|
273
|
+
if (Array.isArray(config.input)) {
|
|
274
|
+
const first = config.input[0]
|
|
275
|
+
if (first && 'path' in first) {
|
|
276
|
+
source = path.basename(first.path)
|
|
277
|
+
}
|
|
278
|
+
} else if ('path' in config.input) {
|
|
279
|
+
source = path.basename(config.input.path)
|
|
280
|
+
} else if ('data' in config.input) {
|
|
281
|
+
source = 'text content'
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
let banner = '/**\n* Generated by Kubb (https://kubb.dev/).\n* Do not edit manually.\n'
|
|
285
|
+
|
|
286
|
+
if (config.output.defaultBanner === 'simple') {
|
|
287
|
+
banner += '*/\n'
|
|
288
|
+
return banner
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
if (source) {
|
|
292
|
+
banner += `* Source: ${source}\n`
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
if (title) {
|
|
296
|
+
banner += `* Title: ${title}\n`
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
if (description) {
|
|
300
|
+
const formattedDescription = description.replace(/\n/gm, '\n* ')
|
|
301
|
+
banner += `* Description: ${formattedDescription}\n`
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
if (version) {
|
|
305
|
+
banner += `* OpenAPI spec version: ${version}\n`
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
banner += '*/\n'
|
|
309
|
+
return banner
|
|
310
|
+
} catch (_error) {
|
|
311
|
+
return '/**\n* Generated by Kubb (https://kubb.dev/).\n* Do not edit manually.\n*/'
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
/**
|
|
316
|
+
* Default banner resolver — returns the banner string for a generated file.
|
|
317
|
+
*
|
|
318
|
+
* A user-supplied `output.banner` overrides the default Kubb "Generated by Kubb" notice.
|
|
319
|
+
* When no `output.banner` is set, the Kubb notice is used (including `title` and `version`
|
|
320
|
+
* from the OAS spec when a `node` is provided).
|
|
321
|
+
*
|
|
322
|
+
* - When `output.banner` is a function and `node` is provided, returns `output.banner(node)`.
|
|
323
|
+
* - When `output.banner` is a function and `node` is absent, falls back to the Kubb notice.
|
|
324
|
+
* - When `output.banner` is a string, returns it directly.
|
|
325
|
+
* - When `config.output.defaultBanner` is `false`, returns `undefined`.
|
|
326
|
+
* - Otherwise returns the Kubb "Generated by Kubb" notice.
|
|
327
|
+
*
|
|
328
|
+
* @example String banner overrides default
|
|
329
|
+
* ```ts
|
|
330
|
+
* defaultResolveBanner(undefined, { output: { banner: '// my banner' }, config })
|
|
331
|
+
* // → '// my banner'
|
|
332
|
+
* ```
|
|
333
|
+
*
|
|
334
|
+
* @example Function banner with node
|
|
335
|
+
* ```ts
|
|
336
|
+
* defaultResolveBanner(rootNode, { output: { banner: (node) => `// v${node.version}` }, config })
|
|
337
|
+
* // → '// v3.0.0'
|
|
338
|
+
* ```
|
|
339
|
+
*
|
|
340
|
+
* @example No user banner — Kubb notice with OAS metadata
|
|
341
|
+
* ```ts
|
|
342
|
+
* defaultResolveBanner(rootNode, { config })
|
|
343
|
+
* // → '/** Generated by Kubb ... Title: Pet Store ... *\/'
|
|
344
|
+
* ```
|
|
345
|
+
*
|
|
346
|
+
* @example Disabled default banner
|
|
347
|
+
* ```ts
|
|
348
|
+
* defaultResolveBanner(undefined, { config: { output: { defaultBanner: false }, ...config } })
|
|
349
|
+
* // → undefined
|
|
350
|
+
* ```
|
|
351
|
+
*/
|
|
352
|
+
export function defaultResolveBanner(node: RootNode | undefined, { output, config }: ResolveBannerContext): string | undefined {
|
|
353
|
+
if (typeof output?.banner === 'function') {
|
|
354
|
+
return output.banner(node)
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
if (typeof output?.banner === 'string') {
|
|
358
|
+
return output.banner
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
if (config.output.defaultBanner === false) {
|
|
362
|
+
return undefined
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
return buildDefaultBanner({ title: node?.meta?.title, version: node?.meta?.version, config })
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
/**
|
|
369
|
+
* Default footer resolver — returns the footer string for a generated file.
|
|
370
|
+
*
|
|
371
|
+
* - When `output.footer` is a function and `node` is provided, calls it with the node.
|
|
372
|
+
* - When `output.footer` is a function and `node` is absent, returns `undefined`.
|
|
373
|
+
* - When `output.footer` is a string, returns it directly.
|
|
374
|
+
* - Otherwise returns `undefined`.
|
|
375
|
+
*
|
|
376
|
+
* @example String footer
|
|
377
|
+
* ```ts
|
|
378
|
+
* defaultResolveFooter(undefined, { output: { footer: '// end of file' }, config })
|
|
379
|
+
* // → '// end of file'
|
|
380
|
+
* ```
|
|
381
|
+
*
|
|
382
|
+
* @example Function footer with node
|
|
383
|
+
* ```ts
|
|
384
|
+
* defaultResolveFooter(rootNode, { output: { footer: (node) => `// ${node.title}` }, config })
|
|
385
|
+
* // → '// Pet Store'
|
|
386
|
+
* ```
|
|
387
|
+
*/
|
|
388
|
+
export function defaultResolveFooter(node: RootNode | undefined, { output }: ResolveBannerContext): string | undefined {
|
|
389
|
+
if (typeof output?.footer === 'function') {
|
|
390
|
+
return node ? output.footer(node) : undefined
|
|
391
|
+
}
|
|
392
|
+
if (typeof output?.footer === 'string') {
|
|
393
|
+
return output.footer
|
|
394
|
+
}
|
|
395
|
+
return undefined
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
/**
|
|
399
|
+
* Defines a resolver for a plugin, injecting built-in defaults for name casing,
|
|
400
|
+
* include/exclude/override filtering, path resolution, and file construction.
|
|
401
|
+
*
|
|
402
|
+
* All four defaults can be overridden by providing them in the builder function:
|
|
403
|
+
* - `default` — name casing strategy (camelCase / PascalCase)
|
|
404
|
+
* - `resolveOptions` — include/exclude/override filtering
|
|
405
|
+
* - `resolvePath` — output path computation
|
|
406
|
+
* - `resolveFile` — full `KubbFile.File` construction
|
|
407
|
+
*
|
|
408
|
+
* Methods in the builder have access to `this` (the full resolver object), so they
|
|
409
|
+
* can call other resolver methods without circular imports.
|
|
410
|
+
*
|
|
411
|
+
* @example Basic resolver with naming helpers
|
|
412
|
+
* ```ts
|
|
413
|
+
* export const resolver = defineResolver<PluginTs>(() => ({
|
|
414
|
+
* name: 'default',
|
|
415
|
+
* resolveName(node) {
|
|
416
|
+
* return this.default(node.name, 'function')
|
|
417
|
+
* },
|
|
418
|
+
* resolveTypedName(node) {
|
|
419
|
+
* return this.default(node.name, 'type')
|
|
420
|
+
* },
|
|
421
|
+
* }))
|
|
422
|
+
* ```
|
|
423
|
+
*
|
|
424
|
+
* @example Override resolvePath for a custom output structure
|
|
425
|
+
* ```ts
|
|
426
|
+
* export const resolver = defineResolver<PluginTs>(() => ({
|
|
427
|
+
* name: 'custom',
|
|
428
|
+
* resolvePath({ baseName }, { root, output }) {
|
|
429
|
+
* return path.resolve(root, output.path, 'generated', baseName)
|
|
430
|
+
* },
|
|
431
|
+
* }))
|
|
432
|
+
* ```
|
|
433
|
+
*
|
|
434
|
+
* @example Use this.default inside a helper
|
|
435
|
+
* ```ts
|
|
436
|
+
* export const resolver = defineResolver<PluginTs>(() => ({
|
|
437
|
+
* name: 'default',
|
|
438
|
+
* resolveParamName(node, param) {
|
|
439
|
+
* return this.default(`${node.operationId} ${param.in} ${param.name}`, 'type')
|
|
440
|
+
* },
|
|
441
|
+
* }))
|
|
442
|
+
* ```
|
|
443
|
+
*/
|
|
444
|
+
export function defineResolver<T extends PluginFactoryOptions>(build: ResolverBuilder<T>): T['resolver'] {
|
|
445
|
+
return {
|
|
446
|
+
default: defaultResolver,
|
|
447
|
+
resolveOptions: defaultResolveOptions,
|
|
448
|
+
resolvePath: defaultResolvePath,
|
|
449
|
+
resolveFile: defaultResolveFile,
|
|
450
|
+
resolveBanner: defaultResolveBanner,
|
|
451
|
+
resolveFooter: defaultResolveFooter,
|
|
452
|
+
...build(),
|
|
453
|
+
} as T['resolver']
|
|
454
|
+
}
|
package/src/hooks/index.ts
CHANGED
|
@@ -1,8 +1,3 @@
|
|
|
1
|
-
export {
|
|
2
|
-
|
|
3
|
-
/** @deprecated Use `useKubb` from `@kubb/core/hooks` instead */
|
|
1
|
+
export { useDriver } from './useDriver.ts'
|
|
4
2
|
export { useMode } from './useMode.ts'
|
|
5
|
-
/** @deprecated Use `useKubb` from `@kubb/core/hooks` instead */
|
|
6
3
|
export { usePlugin } from './usePlugin.ts'
|
|
7
|
-
/** @deprecated Use `useKubb` from `@kubb/core/hooks` instead */
|
|
8
|
-
export { usePluginManager } from './usePluginManager.ts'
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { useFabric } from '@kubb/react-fabric'
|
|
2
|
+
import type { PluginDriver } from '../PluginDriver.ts'
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* @deprecated use `driver` from the generator component props instead
|
|
6
|
+
*/
|
|
7
|
+
export function useDriver(): PluginDriver {
|
|
8
|
+
const { meta } = useFabric<{ driver: PluginDriver }>()
|
|
9
|
+
|
|
10
|
+
return meta.driver
|
|
11
|
+
}
|
package/src/hooks/useMode.ts
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
1
|
+
import { useFabric } from '@kubb/react-fabric'
|
|
2
|
+
import type * as KubbFile from '../KubbFile.ts'
|
|
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
|
-
const { meta } =
|
|
8
|
+
const { meta } = useFabric<{ mode: KubbFile.Mode }>()
|
|
9
9
|
|
|
10
10
|
return meta.mode
|
|
11
11
|
}
|
package/src/hooks/usePlugin.ts
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import {
|
|
1
|
+
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
|
-
const { meta } =
|
|
8
|
+
const { meta } = useFabric<{ plugin: Plugin<TOptions> }>()
|
|
9
9
|
|
|
10
10
|
return meta.plugin
|
|
11
11
|
}
|
package/src/index.ts
CHANGED
|
@@ -1,22 +1,34 @@
|
|
|
1
1
|
export { AsyncEventEmitter, URLPath } from '@internals/utils'
|
|
2
|
-
export { definePrinter } from '@kubb/ast'
|
|
2
|
+
export { composeTransformers, definePrinter } from '@kubb/ast'
|
|
3
3
|
export { build, build as default, safeBuild, setup } from './build.ts'
|
|
4
|
-
export { type CLIOptions, type ConfigInput, defineConfig, isInputPath } from './config.ts'
|
|
5
4
|
export { formatters, linters, logLevel } from './constants.ts'
|
|
6
|
-
export {
|
|
5
|
+
export { createAdapter } from './createAdapter.ts'
|
|
6
|
+
export { createPlugin } from './createPlugin.ts'
|
|
7
|
+
export { createStorage } from './createStorage.ts'
|
|
8
|
+
export { defineConfig } from './defineConfig.ts'
|
|
9
|
+
export { defineGenerator, mergeGenerators } from './defineGenerator.ts'
|
|
7
10
|
export { defineLogger } from './defineLogger.ts'
|
|
8
|
-
export {
|
|
9
|
-
export {
|
|
10
|
-
export {
|
|
11
|
-
|
|
12
|
-
|
|
11
|
+
export { defineParser } from './defineParser.ts'
|
|
12
|
+
export { definePresets } from './definePresets.ts'
|
|
13
|
+
export {
|
|
14
|
+
buildDefaultBanner,
|
|
15
|
+
defaultResolveBanner,
|
|
16
|
+
defaultResolveFile,
|
|
17
|
+
defaultResolveFooter,
|
|
18
|
+
defaultResolveOptions,
|
|
19
|
+
defaultResolvePath,
|
|
20
|
+
defineResolver,
|
|
21
|
+
} from './defineResolver.ts'
|
|
22
|
+
export * as KubbFile from './KubbFile.ts'
|
|
23
|
+
export { getMode, PluginDriver } from './PluginDriver.ts'
|
|
13
24
|
export { fsStorage } from './storages/fsStorage.ts'
|
|
14
25
|
export { memoryStorage } from './storages/memoryStorage.ts'
|
|
15
26
|
export * from './types.ts'
|
|
16
|
-
export type { FunctionParamsAST } from './utils/FunctionParams.ts'
|
|
17
27
|
export { FunctionParams } from './utils/FunctionParams.ts'
|
|
18
28
|
export { detectFormatter } from './utils/formatters.ts'
|
|
19
|
-
export type { FileMetaBase } from './utils/getBarrelFiles.ts'
|
|
20
29
|
export { getBarrelFiles } from './utils/getBarrelFiles.ts'
|
|
21
30
|
export { getConfigs } from './utils/getConfigs.ts'
|
|
31
|
+
export { getPreset } from './utils/getPreset.ts'
|
|
32
|
+
export { isInputPath } from './utils/isInputPath.ts'
|
|
22
33
|
export { detectLinter } from './utils/linters.ts'
|
|
34
|
+
export { satisfiesDependency } from './utils/packageJSON.ts'
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { createReactFabric, Fabric } from '@kubb/react-fabric'
|
|
2
|
+
import type { FabricReactNode, Fabric as FabricType } from '@kubb/react-fabric/types'
|
|
3
|
+
import type * as KubbFile from './KubbFile.ts'
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Handles the return value of a plugin AST hook or generator method.
|
|
7
|
+
*
|
|
8
|
+
* - React element → rendered via an isolated react-fabric context, files merged into `fabric`
|
|
9
|
+
* - `Array<KubbFile.File>` → upserted directly into `fabric`
|
|
10
|
+
* - `void` / `null` / `undefined` → no-op (plugin handled it via `this.upsertFile`)
|
|
11
|
+
*/
|
|
12
|
+
export async function applyHookResult(result: FabricReactNode | Array<KubbFile.File> | void, fabric: FabricType): Promise<void> {
|
|
13
|
+
if (!result) return
|
|
14
|
+
|
|
15
|
+
if (Array.isArray(result)) {
|
|
16
|
+
await fabric.upsertFile(...(result as Array<KubbFile.File>))
|
|
17
|
+
return
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
// Non-array truthy result is treated as a React element (FabricReactNode)
|
|
21
|
+
const fabricChild = createReactFabric()
|
|
22
|
+
await fabricChild.render(<Fabric>{result as FabricReactNode}</Fabric>)
|
|
23
|
+
fabric.context.fileManager.upsert(...fabricChild.files)
|
|
24
|
+
fabricChild.unmount()
|
|
25
|
+
}
|
|
@@ -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 {
|