@kubb/core 5.0.0-alpha.20 → 5.0.0-alpha.22
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-DZdEyCoa.d.ts} +344 -123
- package/dist/hooks.cjs +9 -91
- package/dist/hooks.cjs.map +1 -1
- package/dist/hooks.d.ts +10 -78
- package/dist/hooks.js +8 -88
- package/dist/hooks.js.map +1 -1
- package/dist/index.cjs +347 -34
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +275 -31
- package/dist/index.js +342 -35
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
- package/src/Kubb.ts +5 -5
- package/src/PluginDriver.ts +29 -9
- package/src/build.ts +9 -9
- package/src/constants.ts +2 -2
- package/src/defineBuilder.ts +26 -0
- package/src/defineGenerator.ts +25 -7
- package/src/definePreset.ts +9 -5
- package/src/defineResolver.ts +336 -16
- package/src/hooks/index.ts +1 -2
- package/src/hooks/useDriver.ts +13 -0
- package/src/hooks/useMode.ts +4 -4
- package/src/hooks/usePlugin.ts +1 -1
- package/src/index.ts +10 -1
- package/src/renderNode.tsx +10 -13
- package/src/types.ts +235 -26
- package/src/utils/TreeNode.ts +22 -7
- package/src/utils/getBarrelFiles.ts +10 -7
- package/src/utils/getPreset.ts +17 -6
- package/src/utils/mergeResolvers.ts +9 -1
- package/src/hooks/useKubb.ts +0 -160
- package/src/hooks/usePluginDriver.ts +0 -11
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 { FabricFile } 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,302 @@ 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(
|
|
197
|
+
{ baseName, pathMode, tag, path: groupPath }: ResolverPathParams,
|
|
198
|
+
{ root, output, group }: ResolverContext,
|
|
199
|
+
): FabricFile.Path {
|
|
200
|
+
const mode = pathMode ?? getMode(path.resolve(root, output.path))
|
|
201
|
+
|
|
202
|
+
if (mode === 'single') {
|
|
203
|
+
return path.resolve(root, output.path) as FabricFile.Path
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
if (group && (groupPath || tag)) {
|
|
207
|
+
const groupName = group.name
|
|
208
|
+
? group.name
|
|
209
|
+
: (ctx: { group: string }) => {
|
|
210
|
+
if (group.type === 'path') {
|
|
211
|
+
return `${ctx.group.split('/')[1]}`
|
|
212
|
+
}
|
|
213
|
+
return `${camelCase(ctx.group)}Controller`
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
return path.resolve(root, output.path, groupName({ group: group.type === 'path' ? groupPath! : tag! }), baseName) as FabricFile.Path
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
return path.resolve(root, output.path, baseName) as FabricFile.Path
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
/**
|
|
223
|
+
* Default file resolver used by `defineResolver`.
|
|
224
|
+
*
|
|
225
|
+
* Resolves a `FabricFile.File` by combining name resolution (`resolver.default`) with
|
|
226
|
+
* path resolution (`resolver.resolvePath`). The resolved file always has empty
|
|
227
|
+
* `sources`, `imports`, and `exports` arrays — consumers populate those separately.
|
|
228
|
+
*
|
|
229
|
+
* In `single` mode the name is omitted and the file sits directly in the output directory.
|
|
230
|
+
*
|
|
231
|
+
* @example Resolve a schema file
|
|
232
|
+
* ```ts
|
|
233
|
+
* const file = defaultResolveFile.call(resolver,
|
|
234
|
+
* { name: 'pet', extname: '.ts' },
|
|
235
|
+
* { root: '/src', output: { path: 'types' } },
|
|
236
|
+
* )
|
|
237
|
+
* // → { baseName: 'pet.ts', path: '/src/types/pet.ts', sources: [], ... }
|
|
238
|
+
* ```
|
|
239
|
+
*
|
|
240
|
+
* @example Resolve an operation file with tag grouping
|
|
241
|
+
* ```ts
|
|
242
|
+
* const file = defaultResolveFile.call(resolver,
|
|
243
|
+
* { name: 'listPets', extname: '.ts', tag: 'pets' },
|
|
244
|
+
* { root: '/src', output: { path: 'types' }, group: { type: 'tag' } },
|
|
245
|
+
* )
|
|
246
|
+
* // → { baseName: 'listPets.ts', path: '/src/types/petsController/listPets.ts', ... }
|
|
247
|
+
* ```
|
|
248
|
+
*/
|
|
249
|
+
export function defaultResolveFile(this: Resolver, { name, extname, tag, path: groupPath }: ResolverFileParams, context: ResolverContext): FabricFile.File {
|
|
250
|
+
const pathMode = getMode(path.resolve(context.root, context.output.path))
|
|
251
|
+
const resolvedName = pathMode === 'single' ? '' : this.default(name, 'file')
|
|
252
|
+
const baseName = `${resolvedName}${extname}` as FabricFile.BaseName
|
|
253
|
+
const filePath = this.resolvePath({ baseName, pathMode, tag, path: groupPath }, context)
|
|
254
|
+
|
|
255
|
+
return {
|
|
256
|
+
path: filePath,
|
|
257
|
+
baseName: path.basename(filePath) as FabricFile.BaseName,
|
|
258
|
+
meta: {
|
|
259
|
+
pluginName: this.pluginName,
|
|
260
|
+
},
|
|
261
|
+
sources: [],
|
|
262
|
+
imports: [],
|
|
263
|
+
exports: [],
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
/**
|
|
268
|
+
* Generates the default "Generated by Kubb" banner from config and optional node metadata.
|
|
269
|
+
*/
|
|
270
|
+
export function buildDefaultBanner({
|
|
271
|
+
title,
|
|
272
|
+
description,
|
|
273
|
+
version,
|
|
274
|
+
config,
|
|
275
|
+
}: {
|
|
276
|
+
title?: string
|
|
277
|
+
description?: string
|
|
278
|
+
version?: string
|
|
279
|
+
config: Config
|
|
280
|
+
}): string {
|
|
281
|
+
try {
|
|
282
|
+
let source = ''
|
|
283
|
+
if (Array.isArray(config.input)) {
|
|
284
|
+
const first = config.input[0]
|
|
285
|
+
if (first && 'path' in first) {
|
|
286
|
+
source = path.basename(first.path)
|
|
287
|
+
}
|
|
288
|
+
} else if ('path' in config.input) {
|
|
289
|
+
source = path.basename(config.input.path)
|
|
290
|
+
} else if ('data' in config.input) {
|
|
291
|
+
source = 'text content'
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
let banner = '/**\n* Generated by Kubb (https://kubb.dev/).\n* Do not edit manually.\n'
|
|
295
|
+
|
|
296
|
+
if (config.output.defaultBanner === 'simple') {
|
|
297
|
+
banner += '*/\n'
|
|
298
|
+
return banner
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
if (source) {
|
|
302
|
+
banner += `* Source: ${source}\n`
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
if (title) {
|
|
306
|
+
banner += `* Title: ${title}\n`
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
if (description) {
|
|
310
|
+
const formattedDescription = description.replace(/\n/gm, '\n* ')
|
|
311
|
+
banner += `* Description: ${formattedDescription}\n`
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
if (version) {
|
|
315
|
+
banner += `* OpenAPI spec version: ${version}\n`
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
banner += '*/\n'
|
|
319
|
+
return banner
|
|
320
|
+
} catch (_error) {
|
|
321
|
+
return '/**\n* Generated by Kubb (https://kubb.dev/).\n* Do not edit manually.\n*/'
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
/**
|
|
326
|
+
* Default banner resolver — returns the banner string for a generated file.
|
|
110
327
|
*
|
|
111
|
-
*
|
|
328
|
+
* - When `output.banner` is a function and `node` is provided, calls it with the node.
|
|
329
|
+
* - When `output.banner` is a function and `node` is absent, falls back to the default Kubb banner.
|
|
330
|
+
* - When `output.banner` is a string, returns it directly.
|
|
331
|
+
* - When `config.output.defaultBanner` is `false`, returns `undefined`.
|
|
332
|
+
* - Otherwise returns the default "Generated by Kubb" banner.
|
|
333
|
+
*
|
|
334
|
+
* @example String banner
|
|
335
|
+
* ```ts
|
|
336
|
+
* defaultResolveBanner(undefined, { output: { banner: '// my banner' }, config })
|
|
337
|
+
* // → '// my banner'
|
|
338
|
+
* ```
|
|
339
|
+
*
|
|
340
|
+
* @example Function banner with node
|
|
341
|
+
* ```ts
|
|
342
|
+
* defaultResolveBanner(rootNode, { output: { banner: (node) => `// v${node.version}` }, config })
|
|
343
|
+
* // → '// v3.0.0'
|
|
344
|
+
* ```
|
|
345
|
+
*
|
|
346
|
+
* @example Disabled 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 node ? output.banner(node) : buildDefaultBanner({ config })
|
|
355
|
+
}
|
|
356
|
+
if (typeof output?.banner === 'string') {
|
|
357
|
+
return output.banner
|
|
358
|
+
}
|
|
359
|
+
if (config.output.defaultBanner === false) {
|
|
360
|
+
return undefined
|
|
361
|
+
}
|
|
362
|
+
return buildDefaultBanner({ config })
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
/**
|
|
366
|
+
* Default footer resolver — returns the footer string for a generated file.
|
|
367
|
+
*
|
|
368
|
+
* - When `output.footer` is a function and `node` is provided, calls it with the node.
|
|
369
|
+
* - When `output.footer` is a function and `node` is absent, returns `undefined`.
|
|
370
|
+
* - When `output.footer` is a string, returns it directly.
|
|
371
|
+
* - Otherwise returns `undefined`.
|
|
372
|
+
*
|
|
373
|
+
* @example String footer
|
|
374
|
+
* ```ts
|
|
375
|
+
* defaultResolveFooter(undefined, { output: { footer: '// end of file' }, config })
|
|
376
|
+
* // → '// end of file'
|
|
377
|
+
* ```
|
|
378
|
+
*
|
|
379
|
+
* @example Function footer with node
|
|
380
|
+
* ```ts
|
|
381
|
+
* defaultResolveFooter(rootNode, { output: { footer: (node) => `// ${node.title}` }, config })
|
|
382
|
+
* // → '// Pet Store'
|
|
383
|
+
* ```
|
|
384
|
+
*/
|
|
385
|
+
export function defaultResolveFooter(node: RootNode | undefined, { output }: ResolveBannerContext): string | undefined {
|
|
386
|
+
if (typeof output?.footer === 'function') {
|
|
387
|
+
return node ? output.footer(node) : undefined
|
|
388
|
+
}
|
|
389
|
+
if (typeof output?.footer === 'string') {
|
|
390
|
+
return output.footer
|
|
391
|
+
}
|
|
392
|
+
return undefined
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
/**
|
|
396
|
+
* Defines a resolver for a plugin, injecting built-in defaults for name casing,
|
|
397
|
+
* include/exclude/override filtering, path resolution, and file construction.
|
|
398
|
+
*
|
|
399
|
+
* All four defaults can be overridden by providing them in the builder function:
|
|
400
|
+
* - `default` — name casing strategy (camelCase / PascalCase)
|
|
401
|
+
* - `resolveOptions` — include/exclude/override filtering
|
|
402
|
+
* - `resolvePath` — output path computation
|
|
403
|
+
* - `resolveFile` — full `FabricFile.File` construction
|
|
404
|
+
*
|
|
405
|
+
* Methods in the builder have access to `this` (the full resolver object), so they
|
|
406
|
+
* can call other resolver methods without circular imports.
|
|
407
|
+
*
|
|
408
|
+
* @example Basic resolver with naming helpers
|
|
409
|
+
* ```ts
|
|
112
410
|
* export const resolver = defineResolver<PluginTs>(() => ({
|
|
113
411
|
* name: 'default',
|
|
114
|
-
* resolveName(
|
|
115
|
-
* return this.default(name, 'function')
|
|
412
|
+
* resolveName(node) {
|
|
413
|
+
* return this.default(node.name, 'function')
|
|
414
|
+
* },
|
|
415
|
+
* resolveTypedName(node) {
|
|
416
|
+
* return this.default(node.name, 'type')
|
|
116
417
|
* },
|
|
117
|
-
*
|
|
118
|
-
*
|
|
418
|
+
* }))
|
|
419
|
+
* ```
|
|
420
|
+
*
|
|
421
|
+
* @example Override resolvePath for a custom output structure
|
|
422
|
+
* ```ts
|
|
423
|
+
* export const resolver = defineResolver<PluginTs>(() => ({
|
|
424
|
+
* name: 'custom',
|
|
425
|
+
* resolvePath({ baseName }, { root, output }) {
|
|
426
|
+
* return path.resolve(root, output.path, 'generated', baseName)
|
|
119
427
|
* },
|
|
428
|
+
* }))
|
|
429
|
+
* ```
|
|
430
|
+
*
|
|
431
|
+
* @example Use this.default inside a helper
|
|
432
|
+
* ```ts
|
|
433
|
+
* export const resolver = defineResolver<PluginTs>(() => ({
|
|
434
|
+
* name: 'default',
|
|
120
435
|
* resolveParamName(node, param) {
|
|
121
|
-
* return this.
|
|
436
|
+
* return this.default(`${node.operationId} ${param.in} ${param.name}`, 'type')
|
|
122
437
|
* },
|
|
123
438
|
* }))
|
|
439
|
+
* ```
|
|
124
440
|
*/
|
|
125
441
|
export function defineResolver<T extends PluginFactoryOptions>(build: ResolverBuilder<T>): T['resolver'] {
|
|
126
442
|
return {
|
|
127
443
|
default: defaultResolver,
|
|
128
444
|
resolveOptions: defaultResolveOptions,
|
|
445
|
+
resolvePath: defaultResolvePath,
|
|
446
|
+
resolveFile: defaultResolveFile,
|
|
447
|
+
resolveBanner: defaultResolveBanner,
|
|
448
|
+
resolveFooter: defaultResolveFooter,
|
|
129
449
|
...build(),
|
|
130
450
|
} as T['resolver']
|
|
131
451
|
}
|
package/src/hooks/index.ts
CHANGED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { useFabric } from '@kubb/react-fabric'
|
|
2
|
+
import type { PluginDriver } from '../PluginDriver.ts'
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Returns the `PluginDriver` instance from Fabric context.
|
|
6
|
+
*
|
|
7
|
+
* Use this inside React generator components to access the driver, config, and adapter.
|
|
8
|
+
*/
|
|
9
|
+
export function useDriver(): PluginDriver {
|
|
10
|
+
const { meta } = useFabric<{ driver: PluginDriver }>()
|
|
11
|
+
|
|
12
|
+
return meta.driver
|
|
13
|
+
}
|
package/src/hooks/useMode.ts
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { FabricFile } 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
|
-
export function useMode():
|
|
8
|
-
const { meta } = useFabric<{ mode:
|
|
7
|
+
export function useMode(): FabricFile.Mode {
|
|
8
|
+
const { meta } = useFabric<{ mode: FabricFile.Mode }>()
|
|
9
9
|
|
|
10
10
|
return meta.mode
|
|
11
11
|
}
|
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
|
}
|