@kubb/core 5.0.0-alpha.29 → 5.0.0-alpha.30
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-C6VX0skO.d.ts → PluginDriver-D110FoJ-.d.ts} +265 -108
- package/dist/hooks.d.ts +1 -1
- package/dist/index.cjs +495 -529
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +6 -80
- package/dist/index.js +496 -525
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
- package/src/PluginDriver.ts +44 -10
- package/src/build.ts +87 -8
- package/src/createPlugin.ts +3 -1
- package/src/defineGenerator.ts +101 -134
- package/src/index.ts +1 -2
- package/src/renderNode.tsx +14 -186
- package/src/types.ts +213 -11
- package/src/utils/executeStrategies.ts +7 -1
package/src/renderNode.tsx
CHANGED
|
@@ -1,197 +1,25 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { FabricFile } from '@kubb/fabric-core/types'
|
|
2
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, Exclude, Generator, Include, Override, 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
|
-
resolver: TOptions['resolver']
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
/**
|
|
19
|
-
* Renders a React component for a list of operation nodes (V2 generators).
|
|
20
|
-
*/
|
|
21
|
-
export async function renderOperations<TOptions extends PluginFactoryOptions>(
|
|
22
|
-
nodes: Array<OperationNode>,
|
|
23
|
-
options: BuildOperationsV2Options<TOptions>,
|
|
24
|
-
): Promise<void> {
|
|
25
|
-
const { config, fabric, plugin, Component, driver, adapter } = options
|
|
26
|
-
|
|
27
|
-
if (!Component) {
|
|
28
|
-
return undefined
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
const fabricChild = createReactFabric()
|
|
32
|
-
|
|
33
|
-
await fabricChild.render(
|
|
34
|
-
<Fabric>
|
|
35
|
-
<Component config={config} plugin={plugin} driver={driver} adapter={adapter} nodes={nodes} options={options.options} resolver={options.resolver} />
|
|
36
|
-
</Fabric>,
|
|
37
|
-
)
|
|
38
|
-
|
|
39
|
-
fabric.context.fileManager.upsert(...fabricChild.files)
|
|
40
|
-
fabricChild.unmount()
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
type BuildOperationV2Options<TOptions extends PluginFactoryOptions> = {
|
|
44
|
-
config: Config
|
|
45
|
-
fabric: FabricType
|
|
46
|
-
plugin: Plugin<TOptions>
|
|
47
|
-
Component: ReactGeneratorV2<TOptions>['Operation'] | undefined
|
|
48
|
-
adapter: Adapter
|
|
49
|
-
driver: PluginDriver
|
|
50
|
-
options: TOptions['resolvedOptions']
|
|
51
|
-
resolver: TOptions['resolver']
|
|
52
|
-
}
|
|
3
|
+
import type { FabricReactNode, Fabric as FabricType } from '@kubb/react-fabric/types'
|
|
53
4
|
|
|
54
5
|
/**
|
|
55
|
-
*
|
|
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<FabricFile.File>` → upserted directly into `fabric`
|
|
10
|
+
* - `void` / `null` / `undefined` → no-op (plugin handled it via `this.upsertFile`)
|
|
56
11
|
*/
|
|
57
|
-
export async function
|
|
58
|
-
|
|
12
|
+
export async function applyHookResult(result: FabricReactNode | Array<FabricFile.File> | void, fabric: FabricType): Promise<void> {
|
|
13
|
+
if (!result) return
|
|
59
14
|
|
|
60
|
-
if (
|
|
61
|
-
|
|
15
|
+
if (Array.isArray(result)) {
|
|
16
|
+
await fabric.upsertFile(...(result as Array<FabricFile.File>))
|
|
17
|
+
return
|
|
62
18
|
}
|
|
63
19
|
|
|
20
|
+
// Non-array truthy result is treated as a React element (FabricReactNode)
|
|
64
21
|
const fabricChild = createReactFabric()
|
|
65
|
-
|
|
66
|
-
await fabricChild.render(
|
|
67
|
-
<Fabric>
|
|
68
|
-
<Component config={config} plugin={plugin} driver={driver} adapter={adapter} node={node} options={options.options} resolver={options.resolver} />
|
|
69
|
-
</Fabric>,
|
|
70
|
-
)
|
|
71
|
-
|
|
72
|
-
fabric.context.fileManager.upsert(...fabricChild.files)
|
|
73
|
-
fabricChild.unmount()
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
type BuildSchemaV2Options<TOptions extends PluginFactoryOptions> = {
|
|
77
|
-
config: Config
|
|
78
|
-
fabric: FabricType
|
|
79
|
-
plugin: Plugin<TOptions>
|
|
80
|
-
Component: ReactGeneratorV2<TOptions>['Schema'] | undefined
|
|
81
|
-
adapter: Adapter
|
|
82
|
-
driver: PluginDriver
|
|
83
|
-
options: TOptions['resolvedOptions']
|
|
84
|
-
resolver: TOptions['resolver']
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
/**
|
|
88
|
-
* Renders a React component for a single schema node (V2 generators).
|
|
89
|
-
*/
|
|
90
|
-
export async function renderSchema<TOptions extends PluginFactoryOptions>(node: SchemaNode, options: BuildSchemaV2Options<TOptions>): Promise<void> {
|
|
91
|
-
const { config, fabric, plugin, Component, adapter, driver } = options
|
|
92
|
-
|
|
93
|
-
if (!Component) {
|
|
94
|
-
return undefined
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
const fabricChild = createReactFabric()
|
|
98
|
-
|
|
99
|
-
await fabricChild.render(
|
|
100
|
-
<Fabric>
|
|
101
|
-
<Component config={config} plugin={plugin} driver={driver} adapter={adapter} node={node} options={options.options} resolver={options.resolver} />
|
|
102
|
-
</Fabric>,
|
|
103
|
-
)
|
|
104
|
-
|
|
22
|
+
await fabricChild.render(<Fabric>{result as FabricReactNode}</Fabric>)
|
|
105
23
|
fabric.context.fileManager.upsert(...fabricChild.files)
|
|
106
|
-
|
|
107
24
|
fabricChild.unmount()
|
|
108
25
|
}
|
|
109
|
-
|
|
110
|
-
/**
|
|
111
|
-
* Shared context passed to every `runGenerator*` helper.
|
|
112
|
-
* Contains everything a generator needs to produce and write files.
|
|
113
|
-
*/
|
|
114
|
-
type RunGeneratorContext<TOptions extends PluginFactoryOptions> = {
|
|
115
|
-
generators: Array<Generator<TOptions>>
|
|
116
|
-
plugin: Plugin<TOptions>
|
|
117
|
-
resolver: TOptions['resolver']
|
|
118
|
-
exclude: Array<Exclude>
|
|
119
|
-
include: Array<Include> | undefined
|
|
120
|
-
override: Array<Override<TOptions['resolvedOptions']>>
|
|
121
|
-
fabric: FabricType
|
|
122
|
-
adapter: Adapter
|
|
123
|
-
config: Config
|
|
124
|
-
driver: PluginDriver
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
/**
|
|
128
|
-
* Dispatches a single schema node to all generators (react + core).
|
|
129
|
-
* Resolves options per generator and skips excluded nodes.
|
|
130
|
-
*/
|
|
131
|
-
export async function runGeneratorSchema<TOptions extends PluginFactoryOptions>(node: SchemaNode, ctx: RunGeneratorContext<TOptions>): Promise<void> {
|
|
132
|
-
const { generators, plugin, resolver, exclude, include, override, fabric, adapter, config, driver } = ctx
|
|
133
|
-
|
|
134
|
-
for (const generator of generators) {
|
|
135
|
-
const options = resolver.resolveOptions(node, { options: plugin.options, exclude, include, override })
|
|
136
|
-
|
|
137
|
-
if (options === null) {
|
|
138
|
-
continue
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
if (generator.type === 'react' && generator.version === '2') {
|
|
142
|
-
await renderSchema(node, { options, resolver, adapter, config, fabric, Component: generator.Schema, plugin, driver })
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
if (generator.type === 'core' && generator.version === '2') {
|
|
146
|
-
const files = (await generator.schema?.({ node, options, resolver, adapter, config, plugin, driver })) ?? []
|
|
147
|
-
await fabric.upsertFile(...files)
|
|
148
|
-
}
|
|
149
|
-
}
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
/**
|
|
153
|
-
* Dispatches a single operation node to all generators (react + core).
|
|
154
|
-
* Resolves options per generator and skips excluded nodes.
|
|
155
|
-
*/
|
|
156
|
-
export async function runGeneratorOperation<TOptions extends PluginFactoryOptions>(node: OperationNode, ctx: RunGeneratorContext<TOptions>): Promise<void> {
|
|
157
|
-
const { generators, plugin, resolver, exclude, include, override, fabric, adapter, config, driver } = ctx
|
|
158
|
-
|
|
159
|
-
for (const generator of generators) {
|
|
160
|
-
const options = resolver.resolveOptions(node, { options: plugin.options, exclude, include, override })
|
|
161
|
-
|
|
162
|
-
if (options === null) {
|
|
163
|
-
continue
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
if (generator.type === 'react' && generator.version === '2') {
|
|
167
|
-
await renderOperation(node, { options, resolver, adapter, config, fabric, Component: generator.Operation, plugin, driver })
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
if (generator.type === 'core' && generator.version === '2') {
|
|
171
|
-
const files = (await generator.operation?.({ node, options, resolver, adapter, config, plugin, driver })) ?? []
|
|
172
|
-
await fabric.upsertFile(...files)
|
|
173
|
-
}
|
|
174
|
-
}
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
/**
|
|
178
|
-
* Batch-dispatches all collected operation nodes to every generator (react + core).
|
|
179
|
-
* Uses `plugin.options` directly — no per-node option resolution.
|
|
180
|
-
*/
|
|
181
|
-
export async function runGeneratorOperations<TOptions extends PluginFactoryOptions>(
|
|
182
|
-
nodes: Array<OperationNode>,
|
|
183
|
-
ctx: Omit<RunGeneratorContext<TOptions>, 'exclude' | 'include' | 'override'>,
|
|
184
|
-
): Promise<void> {
|
|
185
|
-
const { generators, plugin, resolver, fabric, adapter, config, driver } = ctx
|
|
186
|
-
|
|
187
|
-
for (const generator of generators) {
|
|
188
|
-
if (generator.type === 'react' && generator.version === '2') {
|
|
189
|
-
await renderOperations(nodes, { options: plugin.options, resolver, adapter, config, fabric, Component: generator.Operations, plugin, driver })
|
|
190
|
-
}
|
|
191
|
-
|
|
192
|
-
if (generator.type === 'core' && generator.version === '2') {
|
|
193
|
-
const files = (await generator.operations?.({ nodes, options: plugin.options, resolver, adapter, config, plugin, driver })) ?? []
|
|
194
|
-
await fabric.upsertFile(...files)
|
|
195
|
-
}
|
|
196
|
-
}
|
|
197
|
-
}
|
package/src/types.ts
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import type { AsyncEventEmitter, PossiblePromise } from '@internals/utils'
|
|
2
|
-
import type { Node, Printer, RootNode, SchemaNode, Visitor } from '@kubb/ast/types'
|
|
2
|
+
import type { Node, OperationNode, Printer, RootNode, SchemaNode, Visitor } from '@kubb/ast/types'
|
|
3
3
|
import type { FabricFile, Fabric as FabricType } from '@kubb/fabric-core/types'
|
|
4
4
|
import type { HttpMethod } from '@kubb/oas'
|
|
5
|
+
import type { FabricReactNode } from '@kubb/react-fabric/types'
|
|
5
6
|
import type { DEFAULT_STUDIO_URL, logLevel } from './constants.ts'
|
|
6
7
|
import type { Storage } from './createStorage.ts'
|
|
7
8
|
import type { Generator } from './defineGenerator.ts'
|
|
@@ -13,6 +14,24 @@ export type { Printer, PrinterFactoryOptions, PrinterPartial } from '@kubb/ast/t
|
|
|
13
14
|
declare global {
|
|
14
15
|
namespace Kubb {
|
|
15
16
|
interface PluginContext {}
|
|
17
|
+
/**
|
|
18
|
+
* Registry that maps plugin names to their `PluginFactoryOptions`.
|
|
19
|
+
* Augment this interface in each plugin's `types.ts` to enable automatic
|
|
20
|
+
* typing for `getPlugin` and `requirePlugin`.
|
|
21
|
+
*
|
|
22
|
+
* @example
|
|
23
|
+
* ```ts
|
|
24
|
+
* // packages/plugin-ts/src/types.ts
|
|
25
|
+
* declare global {
|
|
26
|
+
* namespace Kubb {
|
|
27
|
+
* interface PluginRegistry {
|
|
28
|
+
* 'plugin-ts': PluginTs
|
|
29
|
+
* }
|
|
30
|
+
* }
|
|
31
|
+
* }
|
|
32
|
+
* ```
|
|
33
|
+
*/
|
|
34
|
+
interface PluginRegistry {}
|
|
16
35
|
}
|
|
17
36
|
}
|
|
18
37
|
|
|
@@ -394,7 +413,12 @@ export type UserPlugin<TOptions extends PluginFactoryOptions = PluginFactoryOpti
|
|
|
394
413
|
/**
|
|
395
414
|
* Options set for a specific plugin(see kubb.config.js), passthrough of options.
|
|
396
415
|
*/
|
|
397
|
-
options: TOptions['resolvedOptions']
|
|
416
|
+
options: TOptions['resolvedOptions'] & {
|
|
417
|
+
output: Output
|
|
418
|
+
include?: Array<Include>
|
|
419
|
+
exclude: Array<Exclude>
|
|
420
|
+
override: Array<Override<TOptions['resolvedOptions']>>
|
|
421
|
+
}
|
|
398
422
|
/**
|
|
399
423
|
* The resolver for this plugin.
|
|
400
424
|
* Composed by `getPreset` from the preset resolver and the user's `resolver` partial override.
|
|
@@ -415,13 +439,64 @@ export type UserPlugin<TOptions extends PluginFactoryOptions = PluginFactoryOpti
|
|
|
415
439
|
* Specifies the succeeding plugins for the current plugin. You can pass an array of succeeding plugin names, and the current plugin is executed before these plugins.
|
|
416
440
|
*/
|
|
417
441
|
post?: Array<string>
|
|
418
|
-
|
|
442
|
+
/**
|
|
443
|
+
* When `apply` is defined, the plugin is only activated when `apply(config)` returns `true`.
|
|
444
|
+
* Inspired by Vite's `apply` option.
|
|
445
|
+
*
|
|
446
|
+
* @example
|
|
447
|
+
* ```ts
|
|
448
|
+
* apply: (config) => config.output.path !== 'disabled'
|
|
449
|
+
* ```
|
|
450
|
+
*/
|
|
451
|
+
apply?: (config: Config) => boolean
|
|
452
|
+
/**
|
|
453
|
+
* Expose shared helpers or data to all other plugins via `PluginContext`.
|
|
454
|
+
* The object returned is merged into the context that every plugin receives.
|
|
455
|
+
* Use the `declare global { namespace Kubb { interface PluginContext { … } } }` pattern
|
|
456
|
+
* to make the injected properties type-safe.
|
|
457
|
+
*
|
|
458
|
+
* @example
|
|
459
|
+
* ```ts
|
|
460
|
+
* inject() {
|
|
461
|
+
* return { getOas: () => parseSpec(this.config) }
|
|
462
|
+
* }
|
|
463
|
+
* // Other plugins can then call `this.getOas()` inside buildStart()
|
|
464
|
+
* ```
|
|
465
|
+
*/
|
|
466
|
+
inject?: (this: PluginContext<TOptions>) => TOptions['context']
|
|
419
467
|
}
|
|
420
468
|
|
|
421
469
|
export type UserPluginWithLifeCycle<TOptions extends PluginFactoryOptions = PluginFactoryOptions> = UserPlugin<TOptions> & PluginLifecycle<TOptions>
|
|
422
470
|
|
|
423
471
|
type UnknownUserPlugin = UserPlugin<PluginFactoryOptions<string, object, object, unknown, object>>
|
|
424
472
|
|
|
473
|
+
/**
|
|
474
|
+
* Handler for a single schema node. Used by the `schema` hook on a plugin.
|
|
475
|
+
*/
|
|
476
|
+
export type SchemaHook<TOptions extends PluginFactoryOptions = PluginFactoryOptions> = (
|
|
477
|
+
this: GeneratorContext<TOptions>,
|
|
478
|
+
node: SchemaNode,
|
|
479
|
+
options: TOptions['resolvedOptions'],
|
|
480
|
+
) => PossiblePromise<FabricReactNode | Array<FabricFile.File> | void>
|
|
481
|
+
|
|
482
|
+
/**
|
|
483
|
+
* Handler for a single operation node. Used by the `operation` hook on a plugin.
|
|
484
|
+
*/
|
|
485
|
+
export type OperationHook<TOptions extends PluginFactoryOptions = PluginFactoryOptions> = (
|
|
486
|
+
this: GeneratorContext<TOptions>,
|
|
487
|
+
node: OperationNode,
|
|
488
|
+
options: TOptions['resolvedOptions'],
|
|
489
|
+
) => PossiblePromise<FabricReactNode | Array<FabricFile.File> | void>
|
|
490
|
+
|
|
491
|
+
/**
|
|
492
|
+
* Handler for all collected operation nodes. Used by the `operations` hook on a plugin.
|
|
493
|
+
*/
|
|
494
|
+
export type OperationsHook<TOptions extends PluginFactoryOptions = PluginFactoryOptions> = (
|
|
495
|
+
this: GeneratorContext<TOptions>,
|
|
496
|
+
nodes: Array<OperationNode>,
|
|
497
|
+
options: TOptions['resolvedOptions'],
|
|
498
|
+
) => PossiblePromise<FabricReactNode | Array<FabricFile.File> | void>
|
|
499
|
+
|
|
425
500
|
export type Plugin<TOptions extends PluginFactoryOptions = PluginFactoryOptions> = {
|
|
426
501
|
/**
|
|
427
502
|
* Unique name used for the plugin
|
|
@@ -440,7 +515,12 @@ export type Plugin<TOptions extends PluginFactoryOptions = PluginFactoryOptions>
|
|
|
440
515
|
/**
|
|
441
516
|
* Options set for a specific plugin(see kubb.config.js), passthrough of options.
|
|
442
517
|
*/
|
|
443
|
-
options: TOptions['resolvedOptions']
|
|
518
|
+
options: TOptions['resolvedOptions'] & {
|
|
519
|
+
output: Output
|
|
520
|
+
include?: Array<Include>
|
|
521
|
+
exclude: Array<Exclude>
|
|
522
|
+
override: Array<Override<TOptions['resolvedOptions']>>
|
|
523
|
+
}
|
|
444
524
|
/**
|
|
445
525
|
* The resolver for this plugin.
|
|
446
526
|
* Composed by `getPreset` from the preset resolver and the user's `resolver` partial override.
|
|
@@ -453,21 +533,90 @@ export type Plugin<TOptions extends PluginFactoryOptions = PluginFactoryOptions>
|
|
|
453
533
|
*/
|
|
454
534
|
transformer?: Visitor
|
|
455
535
|
|
|
456
|
-
install: (this: PluginContext<TOptions>, context: PluginContext<TOptions>) => PossiblePromise<void>
|
|
457
536
|
/**
|
|
458
|
-
*
|
|
537
|
+
* When `apply` is defined, the plugin is only activated when `apply(config)` returns `true`.
|
|
538
|
+
* Inspired by Vite's `apply` option.
|
|
539
|
+
*/
|
|
540
|
+
apply?: (config: Config) => boolean
|
|
541
|
+
/**
|
|
542
|
+
* Optional semver version string for this plugin, e.g. `"1.2.3"`.
|
|
543
|
+
* Used in diagnostic messages and version-conflict detection.
|
|
544
|
+
*/
|
|
545
|
+
version?: string
|
|
546
|
+
|
|
547
|
+
buildStart: (this: PluginContext<TOptions>) => PossiblePromise<void>
|
|
548
|
+
/**
|
|
549
|
+
* Called once per plugin after all files have been written to disk.
|
|
550
|
+
* Use this for post-processing, copying assets, or generating summary reports.
|
|
551
|
+
*/
|
|
552
|
+
buildEnd: (this: PluginContext<TOptions>) => PossiblePromise<void>
|
|
553
|
+
/**
|
|
554
|
+
* Called for each schema node during the AST walk.
|
|
555
|
+
* Return a React element, an array of `FabricFile.File`, or `void` for manual handling.
|
|
556
|
+
* Nodes matching `exclude`/`include` filters are skipped automatically.
|
|
557
|
+
*
|
|
558
|
+
* For multiple generators, use `composeGenerators` inside the plugin factory.
|
|
559
|
+
*/
|
|
560
|
+
schema?: SchemaHook<TOptions>
|
|
561
|
+
/**
|
|
562
|
+
* Called for each operation node during the AST walk.
|
|
563
|
+
* Return a React element, an array of `FabricFile.File`, or `void` for manual handling.
|
|
564
|
+
*
|
|
565
|
+
* For multiple generators, use `composeGenerators` inside the plugin factory.
|
|
566
|
+
*/
|
|
567
|
+
operation?: OperationHook<TOptions>
|
|
568
|
+
/**
|
|
569
|
+
* Called once after all operations have been walked, with the full collected set.
|
|
570
|
+
*
|
|
571
|
+
* For multiple generators, use `composeGenerators` inside the plugin factory.
|
|
572
|
+
*/
|
|
573
|
+
operations?: OperationsHook<TOptions>
|
|
574
|
+
/**
|
|
575
|
+
* Expose shared helpers or data to all other plugins via `PluginContext`.
|
|
576
|
+
* The returned object is merged into the context received by every plugin.
|
|
459
577
|
*/
|
|
460
|
-
inject: (this: PluginContext<TOptions
|
|
578
|
+
inject: (this: PluginContext<TOptions>) => TOptions['context']
|
|
461
579
|
}
|
|
462
580
|
|
|
463
581
|
export type PluginWithLifeCycle<TOptions extends PluginFactoryOptions = PluginFactoryOptions> = Plugin<TOptions> & PluginLifecycle<TOptions>
|
|
464
582
|
|
|
465
583
|
export type PluginLifecycle<TOptions extends PluginFactoryOptions = PluginFactoryOptions> = {
|
|
466
584
|
/**
|
|
467
|
-
*
|
|
585
|
+
* Called once per plugin at the start of its processing phase, before schema/operation/operations hooks run.
|
|
586
|
+
* Use this to set up shared state, fetch remote data, or perform any async initialization.
|
|
468
587
|
* @type hookParallel
|
|
469
588
|
*/
|
|
470
|
-
|
|
589
|
+
buildStart?: (this: PluginContext<TOptions>) => PossiblePromise<void>
|
|
590
|
+
/**
|
|
591
|
+
* Called once per plugin after all files have been written to disk.
|
|
592
|
+
* Use this for post-processing, copying assets, or generating summary reports.
|
|
593
|
+
* @type hookParallel
|
|
594
|
+
*/
|
|
595
|
+
buildEnd?: (this: PluginContext<TOptions>) => PossiblePromise<void>
|
|
596
|
+
/**
|
|
597
|
+
* Called for each schema node during the AST walk.
|
|
598
|
+
* Return a React element (`<File>...</File>`), an array of `FabricFile.File` objects,
|
|
599
|
+
* or `void` to handle file writing manually via `this.upsertFile`.
|
|
600
|
+
* Nodes matching `exclude` / `include` filters are skipped automatically.
|
|
601
|
+
*
|
|
602
|
+
* For multiple generators, use `composeGenerators` inside the plugin factory.
|
|
603
|
+
*/
|
|
604
|
+
schema?: SchemaHook<TOptions>
|
|
605
|
+
/**
|
|
606
|
+
* Called for each operation node during the AST walk.
|
|
607
|
+
* Return a React element (`<File>...</File>`), an array of `FabricFile.File` objects,
|
|
608
|
+
* or `void` to handle file writing manually via `this.upsertFile`.
|
|
609
|
+
*
|
|
610
|
+
* For multiple generators, use `composeGenerators` inside the plugin factory.
|
|
611
|
+
*/
|
|
612
|
+
operation?: OperationHook<TOptions>
|
|
613
|
+
/**
|
|
614
|
+
* Called once after all operation nodes have been walked, with the full collection.
|
|
615
|
+
* Useful for generating index/barrel files per group or aggregate operation handlers.
|
|
616
|
+
*
|
|
617
|
+
* For multiple generators, use `composeGenerators` inside the plugin factory.
|
|
618
|
+
*/
|
|
619
|
+
operations?: OperationsHook<TOptions>
|
|
471
620
|
/**
|
|
472
621
|
* Resolve to a Path based on a baseName(example: `./Pet.ts`) and directory(example: `./models`).
|
|
473
622
|
* Options can als be included.
|
|
@@ -522,8 +671,30 @@ export type ResolveNameParams = {
|
|
|
522
671
|
export type PluginContext<TOptions extends PluginFactoryOptions = PluginFactoryOptions> = {
|
|
523
672
|
fabric: FabricType
|
|
524
673
|
config: Config
|
|
674
|
+
/**
|
|
675
|
+
* Absolute path to the output directory for the current plugin.
|
|
676
|
+
* Shorthand for `path.resolve(config.root, config.output.path)`.
|
|
677
|
+
*/
|
|
678
|
+
root: string
|
|
679
|
+
/**
|
|
680
|
+
* Returns the output mode for the given output config.
|
|
681
|
+
* Returns `'single'` when `output.path` has a file extension, `'split'` otherwise.
|
|
682
|
+
* Shorthand for `getMode(path.resolve(this.root, output.path))`.
|
|
683
|
+
*/
|
|
684
|
+
getMode: (output: { path: string }) => FabricFile.Mode
|
|
525
685
|
driver: PluginDriver
|
|
526
|
-
|
|
686
|
+
/**
|
|
687
|
+
* Get a plugin by name. Returns the plugin typed via `Kubb.PluginRegistry` when
|
|
688
|
+
* the name is a registered key, otherwise returns the generic `Plugin`.
|
|
689
|
+
*/
|
|
690
|
+
getPlugin<TName extends keyof Kubb.PluginRegistry>(name: TName): Plugin<Kubb.PluginRegistry[TName]> | undefined
|
|
691
|
+
getPlugin(name: string): Plugin | undefined
|
|
692
|
+
/**
|
|
693
|
+
* Like `getPlugin` but throws a descriptive error when the plugin is not found.
|
|
694
|
+
* Useful for enforcing dependencies inside `buildStart()`.
|
|
695
|
+
*/
|
|
696
|
+
requirePlugin<TName extends keyof Kubb.PluginRegistry>(name: TName): Plugin<Kubb.PluginRegistry[TName]>
|
|
697
|
+
requirePlugin(name: string): Plugin
|
|
527
698
|
/**
|
|
528
699
|
* Only add when the file does not exist yet
|
|
529
700
|
*/
|
|
@@ -532,6 +703,9 @@ export type PluginContext<TOptions extends PluginFactoryOptions = PluginFactoryO
|
|
|
532
703
|
* merging multiple sources into the same output file
|
|
533
704
|
*/
|
|
534
705
|
upsertFile: (...file: Array<FabricFile.File>) => Promise<void>
|
|
706
|
+
/**
|
|
707
|
+
* @deprecated use this.warn, this.error, this.info instead
|
|
708
|
+
*/
|
|
535
709
|
events: AsyncEventEmitter<KubbEvents>
|
|
536
710
|
/**
|
|
537
711
|
* Current plugin
|
|
@@ -547,6 +721,21 @@ export type PluginContext<TOptions extends PluginFactoryOptions = PluginFactoryO
|
|
|
547
721
|
*/
|
|
548
722
|
transformer: Visitor | undefined
|
|
549
723
|
|
|
724
|
+
/**
|
|
725
|
+
* Emit a warning via the build event system.
|
|
726
|
+
* Shorthand for `this.events.emit('warn', message)`.
|
|
727
|
+
*/
|
|
728
|
+
warn: (message: string) => void
|
|
729
|
+
/**
|
|
730
|
+
* Emit an error via the build event system.
|
|
731
|
+
* Shorthand for `this.events.emit('error', error)`.
|
|
732
|
+
*/
|
|
733
|
+
error: (error: string | Error) => void
|
|
734
|
+
/**
|
|
735
|
+
* Emit an info message via the build event system.
|
|
736
|
+
* Shorthand for `this.events.emit('info', message)`.
|
|
737
|
+
*/
|
|
738
|
+
info: (message: string) => void
|
|
550
739
|
/**
|
|
551
740
|
* Opens the Kubb Studio URL for the current `rootNode` in the default browser.
|
|
552
741
|
* Falls back to printing the URL if the browser cannot be launched.
|
|
@@ -571,6 +760,19 @@ export type PluginContext<TOptions extends PluginFactoryOptions = PluginFactoryO
|
|
|
571
760
|
}
|
|
572
761
|
) &
|
|
573
762
|
Kubb.PluginContext
|
|
763
|
+
|
|
764
|
+
/**
|
|
765
|
+
* Narrowed `PluginContext` used as the `this` type inside generator and plugin AST hook methods.
|
|
766
|
+
*
|
|
767
|
+
* Generators and the `schema`/`operation`/`operations` plugin hooks are only invoked from
|
|
768
|
+
* `runPluginAstHooks`, which already guards against a missing adapter. This type reflects
|
|
769
|
+
* that guarantee — `this.adapter` and `this.rootNode` are always defined, so no runtime
|
|
770
|
+
* checks or casts are needed inside the method bodies.
|
|
771
|
+
*/
|
|
772
|
+
export type GeneratorContext<TOptions extends PluginFactoryOptions = PluginFactoryOptions> = Omit<PluginContext<TOptions>, 'adapter' | 'rootNode'> & {
|
|
773
|
+
adapter: Adapter
|
|
774
|
+
rootNode: RootNode
|
|
775
|
+
}
|
|
574
776
|
/**
|
|
575
777
|
* Specify the export location for the files and define the behavior of the output
|
|
576
778
|
*/
|
|
@@ -654,7 +856,7 @@ export type UserLogger<TOptions extends LoggerOptions = LoggerOptions> = Logger<
|
|
|
654
856
|
export type CompatibilityPreset = 'default' | 'kubbV4'
|
|
655
857
|
|
|
656
858
|
export type { Storage } from './createStorage.ts'
|
|
657
|
-
export type {
|
|
859
|
+
export type { Generator } from './defineGenerator.ts'
|
|
658
860
|
export type { KubbEvents } from './Kubb.ts'
|
|
659
861
|
|
|
660
862
|
/**
|
|
@@ -12,6 +12,7 @@ type SeqOutput<TInput extends Array<PromiseFunc<TValue, null>>, TValue> = Promis
|
|
|
12
12
|
* - Each function receives the accumulated state from the previous call.
|
|
13
13
|
* - Skips functions that return a falsy value (acts as a no-op for that step).
|
|
14
14
|
* - Returns an array of all individual results.
|
|
15
|
+
* @deprecated
|
|
15
16
|
*/
|
|
16
17
|
export function hookSeq<TInput extends Array<PromiseFunc<TValue, null>>, TValue, TOutput = SeqOutput<TInput, TValue>>(promises: TInput): TOutput {
|
|
17
18
|
return promises.filter(Boolean).reduce(
|
|
@@ -41,6 +42,7 @@ type HookFirstOutput<TInput extends Array<PromiseFunc<TValue, null>>, TValue = u
|
|
|
41
42
|
*
|
|
42
43
|
* - Stops as soon as `nullCheck` passes for a result (default: `!== null`).
|
|
43
44
|
* - Subsequent functions are skipped once a match is found.
|
|
45
|
+
* @deprecated
|
|
44
46
|
*/
|
|
45
47
|
export function hookFirst<TInput extends Array<PromiseFunc<TValue, null>>, TValue = unknown, TOutput = HookFirstOutput<TInput, TValue>>(
|
|
46
48
|
promises: TInput,
|
|
@@ -68,6 +70,7 @@ type HookParallelOutput<TInput extends Array<PromiseFunc<TValue, null>>, TValue>
|
|
|
68
70
|
*
|
|
69
71
|
* - Limits simultaneous executions to `concurrency` (default: unlimited).
|
|
70
72
|
* - Uses `Promise.allSettled` so individual failures do not cancel other tasks.
|
|
73
|
+
* @deprecated
|
|
71
74
|
*/
|
|
72
75
|
export function hookParallel<TInput extends Array<PromiseFunc<TValue, null>>, TValue = unknown, TOutput = HookParallelOutput<TInput, TValue>>(
|
|
73
76
|
promises: TInput,
|
|
@@ -82,6 +85,7 @@ export function hookParallel<TInput extends Array<PromiseFunc<TValue, null>>, TV
|
|
|
82
85
|
|
|
83
86
|
/**
|
|
84
87
|
* Execution strategy used when dispatching plugin hook calls.
|
|
88
|
+
* @deprecated
|
|
85
89
|
*/
|
|
86
90
|
export type Strategy = 'seq' | 'first' | 'parallel'
|
|
87
91
|
|
|
@@ -90,5 +94,7 @@ type StrategyOutputMap<TInput extends Array<PromiseFunc<TValue, null>>, TValue>
|
|
|
90
94
|
seq: SeqOutput<TInput, TValue>
|
|
91
95
|
parallel: HookParallelOutput<TInput, TValue>
|
|
92
96
|
}
|
|
93
|
-
|
|
97
|
+
/**
|
|
98
|
+
* @deprecated
|
|
99
|
+
*/
|
|
94
100
|
export type StrategySwitch<TStrategy extends Strategy, TInput extends Array<PromiseFunc<TValue, null>>, TValue> = StrategyOutputMap<TInput, TValue>[TStrategy]
|