@kubb/core 5.0.0-alpha.7 → 5.0.0-alpha.70
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/README.md +3 -2
- package/dist/PluginDriver-BXibeQk-.cjs +1036 -0
- package/dist/PluginDriver-BXibeQk-.cjs.map +1 -0
- package/dist/PluginDriver-DV3p2Hky.js +945 -0
- package/dist/PluginDriver-DV3p2Hky.js.map +1 -0
- package/dist/index.cjs +730 -1773
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +289 -240
- package/dist/index.js +717 -1741
- package/dist/index.js.map +1 -1
- package/dist/mocks.cjs +139 -0
- package/dist/mocks.cjs.map +1 -0
- package/dist/mocks.d.ts +74 -0
- package/dist/mocks.js +134 -0
- package/dist/mocks.js.map +1 -0
- package/dist/types-DWtkW2RX.d.ts +1915 -0
- package/package.json +51 -57
- package/src/FileManager.ts +115 -0
- package/src/FileProcessor.ts +86 -0
- package/src/Kubb.ts +205 -130
- package/src/PluginDriver.ts +326 -565
- package/src/constants.ts +20 -47
- package/src/createAdapter.ts +25 -0
- package/src/createKubb.ts +546 -0
- package/src/createRenderer.ts +57 -0
- package/src/createStorage.ts +58 -0
- package/src/defineGenerator.ts +88 -100
- package/src/defineLogger.ts +13 -3
- package/src/defineMiddleware.ts +64 -0
- package/src/defineParser.ts +45 -0
- package/src/definePlugin.ts +78 -7
- package/src/defineResolver.ts +521 -0
- package/src/devtools.ts +14 -14
- package/src/index.ts +13 -16
- package/src/mocks.ts +172 -0
- package/src/renderNode.ts +35 -0
- package/src/storages/fsStorage.ts +43 -13
- package/src/storages/memoryStorage.ts +6 -4
- package/src/types.ts +752 -226
- package/src/utils/diagnostics.ts +4 -1
- package/src/utils/isInputPath.ts +10 -0
- package/src/utils/packageJSON.ts +99 -0
- package/dist/PluginDriver-Dma9KhLK.d.ts +0 -1056
- package/dist/chunk-ByKO4r7w.cjs +0 -38
- package/dist/hooks.cjs +0 -102
- package/dist/hooks.cjs.map +0 -1
- package/dist/hooks.d.ts +0 -75
- package/dist/hooks.js +0 -97
- package/dist/hooks.js.map +0 -1
- package/src/PackageManager.ts +0 -180
- package/src/build.ts +0 -419
- package/src/config.ts +0 -56
- package/src/defineAdapter.ts +0 -22
- package/src/defineStorage.ts +0 -56
- package/src/errors.ts +0 -1
- package/src/hooks/index.ts +0 -4
- package/src/hooks/useKubb.ts +0 -138
- package/src/hooks/useMode.ts +0 -11
- package/src/hooks/usePlugin.ts +0 -11
- package/src/hooks/usePluginDriver.ts +0 -11
- package/src/utils/FunctionParams.ts +0 -155
- package/src/utils/TreeNode.ts +0 -215
- package/src/utils/executeStrategies.ts +0 -81
- package/src/utils/formatters.ts +0 -56
- package/src/utils/getBarrelFiles.ts +0 -141
- package/src/utils/getConfigs.ts +0 -30
- package/src/utils/getPlugins.ts +0 -23
- package/src/utils/linters.ts +0 -25
- package/src/utils/resolveOptions.ts +0 -93
|
@@ -0,0 +1,1915 @@
|
|
|
1
|
+
import { t as __name } from "./chunk--u3MIqq1.js";
|
|
2
|
+
import { FileNode, HttpMethod, ImportNode, InputNode, Node, OperationNode, SchemaNode, UserFileNode, Visitor } from "@kubb/ast";
|
|
3
|
+
|
|
4
|
+
//#region ../../internals/utils/src/asyncEventEmitter.d.ts
|
|
5
|
+
/**
|
|
6
|
+
* A function that can be registered as an event listener, synchronous or async.
|
|
7
|
+
*/
|
|
8
|
+
type AsyncListener<TArgs extends unknown[]> = (...args: TArgs) => void | Promise<void>;
|
|
9
|
+
/**
|
|
10
|
+
* Typed `EventEmitter` that awaits all async listeners before resolving.
|
|
11
|
+
* Wraps Node's `EventEmitter` with full TypeScript event-map inference.
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* ```ts
|
|
15
|
+
* const emitter = new AsyncEventEmitter<{ build: [name: string] }>()
|
|
16
|
+
* emitter.on('build', async (name) => { console.log(name) })
|
|
17
|
+
* await emitter.emit('build', 'petstore') // all listeners awaited
|
|
18
|
+
* ```
|
|
19
|
+
*/
|
|
20
|
+
declare class AsyncEventEmitter<TEvents extends { [K in keyof TEvents]: unknown[] }> {
|
|
21
|
+
#private;
|
|
22
|
+
/**
|
|
23
|
+
* Maximum number of listeners per event before Node emits a memory-leak warning.
|
|
24
|
+
* @default 10
|
|
25
|
+
*/
|
|
26
|
+
constructor(maxListener?: number);
|
|
27
|
+
/**
|
|
28
|
+
* Emits `eventName` and awaits all registered listeners sequentially.
|
|
29
|
+
* Throws if any listener rejects, wrapping the cause with the event name and serialized arguments.
|
|
30
|
+
*
|
|
31
|
+
* @example
|
|
32
|
+
* ```ts
|
|
33
|
+
* await emitter.emit('build', 'petstore')
|
|
34
|
+
* ```
|
|
35
|
+
*/
|
|
36
|
+
emit<TEventName extends keyof TEvents & string>(eventName: TEventName, ...eventArgs: TEvents[TEventName]): Promise<void>;
|
|
37
|
+
/**
|
|
38
|
+
* Registers a persistent listener for `eventName`.
|
|
39
|
+
*
|
|
40
|
+
* @example
|
|
41
|
+
* ```ts
|
|
42
|
+
* emitter.on('build', async (name) => { console.log(name) })
|
|
43
|
+
* ```
|
|
44
|
+
*/
|
|
45
|
+
on<TEventName extends keyof TEvents & string>(eventName: TEventName, handler: AsyncListener<TEvents[TEventName]>): void;
|
|
46
|
+
/**
|
|
47
|
+
* Registers a one-shot listener that removes itself after the first invocation.
|
|
48
|
+
*
|
|
49
|
+
* @example
|
|
50
|
+
* ```ts
|
|
51
|
+
* emitter.onOnce('build', async (name) => { console.log(name) })
|
|
52
|
+
* ```
|
|
53
|
+
*/
|
|
54
|
+
onOnce<TEventName extends keyof TEvents & string>(eventName: TEventName, handler: AsyncListener<TEvents[TEventName]>): void;
|
|
55
|
+
/**
|
|
56
|
+
* Removes a previously registered listener.
|
|
57
|
+
*
|
|
58
|
+
* @example
|
|
59
|
+
* ```ts
|
|
60
|
+
* emitter.off('build', handler)
|
|
61
|
+
* ```
|
|
62
|
+
*/
|
|
63
|
+
off<TEventName extends keyof TEvents & string>(eventName: TEventName, handler: AsyncListener<TEvents[TEventName]>): void;
|
|
64
|
+
/**
|
|
65
|
+
* Returns the number of listeners registered for `eventName`.
|
|
66
|
+
*
|
|
67
|
+
* @example
|
|
68
|
+
* ```ts
|
|
69
|
+
* emitter.on('build', handler)
|
|
70
|
+
* emitter.listenerCount('build') // 1
|
|
71
|
+
* ```
|
|
72
|
+
*/
|
|
73
|
+
listenerCount<TEventName extends keyof TEvents & string>(eventName: TEventName): number;
|
|
74
|
+
/**
|
|
75
|
+
* Removes all listeners from every event channel.
|
|
76
|
+
*
|
|
77
|
+
* @example
|
|
78
|
+
* ```ts
|
|
79
|
+
* emitter.removeAll()
|
|
80
|
+
* ```
|
|
81
|
+
*/
|
|
82
|
+
removeAll(): void;
|
|
83
|
+
}
|
|
84
|
+
//#endregion
|
|
85
|
+
//#region ../../internals/utils/src/promise.d.ts
|
|
86
|
+
/** A value that may already be resolved or still pending.
|
|
87
|
+
*
|
|
88
|
+
* @example
|
|
89
|
+
* ```ts
|
|
90
|
+
* function load(id: string): PossiblePromise<string> {
|
|
91
|
+
* return cache.get(id) ?? fetchRemote(id)
|
|
92
|
+
* }
|
|
93
|
+
* ```
|
|
94
|
+
*/
|
|
95
|
+
type PossiblePromise<T> = Promise<T> | T;
|
|
96
|
+
//#endregion
|
|
97
|
+
//#region src/constants.d.ts
|
|
98
|
+
/**
|
|
99
|
+
* Base URL for the Kubb Studio web app.
|
|
100
|
+
*/
|
|
101
|
+
declare const DEFAULT_STUDIO_URL: "https://studio.kubb.dev";
|
|
102
|
+
/**
|
|
103
|
+
* Numeric log-level thresholds used internally to compare verbosity.
|
|
104
|
+
*
|
|
105
|
+
* Higher numbers are more verbose.
|
|
106
|
+
*/
|
|
107
|
+
declare const logLevel: {
|
|
108
|
+
readonly silent: number;
|
|
109
|
+
readonly error: 0;
|
|
110
|
+
readonly warn: 1;
|
|
111
|
+
readonly info: 3;
|
|
112
|
+
readonly verbose: 4;
|
|
113
|
+
readonly debug: 5;
|
|
114
|
+
};
|
|
115
|
+
//#endregion
|
|
116
|
+
//#region src/createRenderer.d.ts
|
|
117
|
+
/**
|
|
118
|
+
* Minimal interface any Kubb renderer must satisfy.
|
|
119
|
+
*
|
|
120
|
+
* The generic `TElement` is the type of the element the renderer accepts —
|
|
121
|
+
* e.g. `KubbReactElement` for `@kubb/renderer-jsx`, or a custom type for
|
|
122
|
+
* your own renderer. Defaults to `unknown` so that generators which do not
|
|
123
|
+
* care about the element type continue to work without specifying it.
|
|
124
|
+
*
|
|
125
|
+
* This allows core to drive rendering without a hard dependency on
|
|
126
|
+
* `@kubb/renderer-jsx` or any specific renderer implementation.
|
|
127
|
+
*/
|
|
128
|
+
type Renderer<TElement = unknown> = {
|
|
129
|
+
render(element: TElement): Promise<void>;
|
|
130
|
+
unmount(error?: Error | number | null): void;
|
|
131
|
+
readonly files: Array<FileNode>;
|
|
132
|
+
};
|
|
133
|
+
/**
|
|
134
|
+
* A factory function that produces a fresh {@link Renderer} per render.
|
|
135
|
+
*
|
|
136
|
+
* Generators use this to declare which renderer handles their output.
|
|
137
|
+
*/
|
|
138
|
+
type RendererFactory<TElement = unknown> = () => Renderer<TElement>;
|
|
139
|
+
/**
|
|
140
|
+
* Creates a renderer factory for use in generator definitions.
|
|
141
|
+
*
|
|
142
|
+
* Wrap your renderer factory function with this helper to register it as the
|
|
143
|
+
* renderer for a generator. Core will call this factory once per render cycle
|
|
144
|
+
* to obtain a fresh renderer instance.
|
|
145
|
+
*
|
|
146
|
+
* @example
|
|
147
|
+
* ```ts
|
|
148
|
+
* // packages/renderer-jsx/src/index.ts
|
|
149
|
+
* export const jsxRenderer = createRenderer(() => {
|
|
150
|
+
* const runtime = new Runtime()
|
|
151
|
+
* return {
|
|
152
|
+
* async render(element) { await runtime.render(element) },
|
|
153
|
+
* get files() { return runtime.nodes },
|
|
154
|
+
* unmount(error) { runtime.unmount(error) },
|
|
155
|
+
* }
|
|
156
|
+
* })
|
|
157
|
+
*
|
|
158
|
+
* // packages/plugin-zod/src/generators/zodGenerator.tsx
|
|
159
|
+
* import { jsxRenderer } from '@kubb/renderer-jsx'
|
|
160
|
+
* export const zodGenerator = defineGenerator<PluginZod>({
|
|
161
|
+
* name: 'zod',
|
|
162
|
+
* renderer: jsxRenderer,
|
|
163
|
+
* schema(node, options) { return <File ...>...</File> },
|
|
164
|
+
* })
|
|
165
|
+
* ```
|
|
166
|
+
*/
|
|
167
|
+
declare function createRenderer<TElement = unknown>(factory: RendererFactory<TElement>): RendererFactory<TElement>;
|
|
168
|
+
//#endregion
|
|
169
|
+
//#region src/createStorage.d.ts
|
|
170
|
+
type Storage = {
|
|
171
|
+
/**
|
|
172
|
+
* Identifier used for logging and debugging (e.g. `'fs'`, `'s3'`).
|
|
173
|
+
*/
|
|
174
|
+
readonly name: string;
|
|
175
|
+
/**
|
|
176
|
+
* Returns `true` when an entry for `key` exists in storage.
|
|
177
|
+
*/
|
|
178
|
+
hasItem(key: string): Promise<boolean>;
|
|
179
|
+
/**
|
|
180
|
+
* Returns the stored string value, or `null` when `key` does not exist.
|
|
181
|
+
*/
|
|
182
|
+
getItem(key: string): Promise<string | null>;
|
|
183
|
+
/**
|
|
184
|
+
* Persists `value` under `key`, creating any required structure.
|
|
185
|
+
*/
|
|
186
|
+
setItem(key: string, value: string): Promise<void>;
|
|
187
|
+
/**
|
|
188
|
+
* Removes the entry for `key`. No-ops when the key does not exist.
|
|
189
|
+
*/
|
|
190
|
+
removeItem(key: string): Promise<void>;
|
|
191
|
+
/**
|
|
192
|
+
* Returns all keys, optionally filtered to those starting with `base`.
|
|
193
|
+
*/
|
|
194
|
+
getKeys(base?: string): Promise<Array<string>>;
|
|
195
|
+
/**
|
|
196
|
+
* Removes all entries, optionally scoped to those starting with `base`.
|
|
197
|
+
*/
|
|
198
|
+
clear(base?: string): Promise<void>;
|
|
199
|
+
/**
|
|
200
|
+
* Optional teardown hook called after the build completes.
|
|
201
|
+
*/
|
|
202
|
+
dispose?(): Promise<void>;
|
|
203
|
+
};
|
|
204
|
+
/**
|
|
205
|
+
* Creates a storage factory. Call the returned function with optional options to get the storage instance.
|
|
206
|
+
*
|
|
207
|
+
* @example
|
|
208
|
+
* export const memoryStorage = createStorage(() => {
|
|
209
|
+
* const store = new Map<string, string>()
|
|
210
|
+
* return {
|
|
211
|
+
* name: 'memory',
|
|
212
|
+
* async hasItem(key) { return store.has(key) },
|
|
213
|
+
* async getItem(key) { return store.get(key) ?? null },
|
|
214
|
+
* async setItem(key, value) { store.set(key, value) },
|
|
215
|
+
* async removeItem(key) { store.delete(key) },
|
|
216
|
+
* async getKeys(base) {
|
|
217
|
+
* const keys = [...store.keys()]
|
|
218
|
+
* return base ? keys.filter((k) => k.startsWith(base)) : keys
|
|
219
|
+
* },
|
|
220
|
+
* async clear(base) { if (!base) store.clear() },
|
|
221
|
+
* }
|
|
222
|
+
* })
|
|
223
|
+
*/
|
|
224
|
+
declare function createStorage<TOptions = Record<string, never>>(build: (options: TOptions) => Storage): (options?: TOptions) => Storage;
|
|
225
|
+
//#endregion
|
|
226
|
+
//#region src/defineGenerator.d.ts
|
|
227
|
+
/**
|
|
228
|
+
* A generator is a named object with optional `schema`, `operation`, and `operations`
|
|
229
|
+
* methods. Each method receives the AST node as the first argument and a typed
|
|
230
|
+
* `ctx` object as the second, giving access to `ctx.config`, `ctx.resolver`,
|
|
231
|
+
* `ctx.adapter`, `ctx.options`, `ctx.upsertFile`, etc.
|
|
232
|
+
*
|
|
233
|
+
* Generators that return renderer elements (e.g. JSX) must declare a `renderer`
|
|
234
|
+
* factory so that core knows how to process the output without coupling core
|
|
235
|
+
* to any specific renderer package.
|
|
236
|
+
*
|
|
237
|
+
* Return a renderer element, an array of `FileNode`, or `void` to handle file
|
|
238
|
+
* writing manually via `ctx.upsertFile`.
|
|
239
|
+
*
|
|
240
|
+
* @example
|
|
241
|
+
* ```ts
|
|
242
|
+
* import { jsxRenderer } from '@kubb/renderer-jsx'
|
|
243
|
+
*
|
|
244
|
+
* export const typeGenerator = defineGenerator<PluginTs>({
|
|
245
|
+
* name: 'typescript',
|
|
246
|
+
* renderer: jsxRenderer,
|
|
247
|
+
* schema(node, ctx) {
|
|
248
|
+
* const { adapter, resolver, root, options } = ctx
|
|
249
|
+
* return <File ...><Type node={node} resolver={resolver} /></File>
|
|
250
|
+
* },
|
|
251
|
+
* operation(node, ctx) {
|
|
252
|
+
* const { options } = ctx
|
|
253
|
+
* return <File ...><OperationType node={node} /></File>
|
|
254
|
+
* },
|
|
255
|
+
* })
|
|
256
|
+
* ```
|
|
257
|
+
*/
|
|
258
|
+
type Generator<TOptions extends PluginFactoryOptions = PluginFactoryOptions, TElement = unknown> = {
|
|
259
|
+
/**
|
|
260
|
+
* Used in diagnostic messages and debug output.
|
|
261
|
+
*/
|
|
262
|
+
name: string;
|
|
263
|
+
/**
|
|
264
|
+
* Optional renderer factory that produces a {@link Renderer} for each render cycle.
|
|
265
|
+
*
|
|
266
|
+
* Generators that return renderer elements (e.g. JSX via `@kubb/renderer-jsx`) must set this
|
|
267
|
+
* to the matching renderer factory (e.g. `jsxRenderer` from `@kubb/renderer-jsx`).
|
|
268
|
+
*
|
|
269
|
+
* Generators that only return `Array<FileNode>` or `void` do not need to set this.
|
|
270
|
+
*
|
|
271
|
+
* Set `renderer: null` to explicitly opt out of rendering even when the parent plugin
|
|
272
|
+
* declares a `renderer` (overrides the plugin-level fallback).
|
|
273
|
+
*
|
|
274
|
+
* @example
|
|
275
|
+
* ```ts
|
|
276
|
+
* import { jsxRenderer } from '@kubb/renderer-jsx'
|
|
277
|
+
* export const myGenerator = defineGenerator<PluginTs>({
|
|
278
|
+
* renderer: jsxRenderer,
|
|
279
|
+
* schema(node, ctx) { return <File ...>...</File> },
|
|
280
|
+
* })
|
|
281
|
+
* ```
|
|
282
|
+
*/
|
|
283
|
+
renderer?: RendererFactory<TElement> | null;
|
|
284
|
+
/**
|
|
285
|
+
* Called for each schema node in the AST walk.
|
|
286
|
+
* `ctx` carries the plugin context with `adapter` and `inputNode` guaranteed present,
|
|
287
|
+
* plus `ctx.options` with the per-node resolved options (after exclude/include/override).
|
|
288
|
+
*/
|
|
289
|
+
schema?: (node: SchemaNode, ctx: GeneratorContext<TOptions>) => PossiblePromise<TElement | Array<FileNode> | void>;
|
|
290
|
+
/**
|
|
291
|
+
* Called for each operation node in the AST walk.
|
|
292
|
+
* `ctx` carries the plugin context with `adapter` and `inputNode` guaranteed present,
|
|
293
|
+
* plus `ctx.options` with the per-node resolved options (after exclude/include/override).
|
|
294
|
+
*/
|
|
295
|
+
operation?: (node: OperationNode, ctx: GeneratorContext<TOptions>) => PossiblePromise<TElement | Array<FileNode> | void>;
|
|
296
|
+
/**
|
|
297
|
+
* Called once after all operations have been walked.
|
|
298
|
+
* `ctx` carries the plugin context with `adapter` and `inputNode` guaranteed present,
|
|
299
|
+
* plus `ctx.options` with the plugin-level options for the batch call.
|
|
300
|
+
*/
|
|
301
|
+
operations?: (nodes: Array<OperationNode>, ctx: GeneratorContext<TOptions>) => PossiblePromise<TElement | Array<FileNode> | void>;
|
|
302
|
+
};
|
|
303
|
+
/**
|
|
304
|
+
* Defines a generator. Returns the object as-is with correct `this` typings.
|
|
305
|
+
* `applyHookResult` handles renderer elements and `File[]` uniformly using
|
|
306
|
+
* the generator's declared `renderer` factory.
|
|
307
|
+
*/
|
|
308
|
+
declare function defineGenerator<TOptions extends PluginFactoryOptions = PluginFactoryOptions, TElement = unknown>(generator: Generator<TOptions, TElement>): Generator<TOptions, TElement>;
|
|
309
|
+
//#endregion
|
|
310
|
+
//#region src/definePlugin.d.ts
|
|
311
|
+
/**
|
|
312
|
+
* A plugin object produced by `definePlugin`.
|
|
313
|
+
* Instead of flat lifecycle methods, it groups all handlers under a `hooks:` property
|
|
314
|
+
* (matching Astro's integration naming convention).
|
|
315
|
+
*
|
|
316
|
+
* @template TFactory - The plugin's `PluginFactoryOptions` type.
|
|
317
|
+
*/
|
|
318
|
+
type Plugin<TFactory extends PluginFactoryOptions = PluginFactoryOptions> = {
|
|
319
|
+
/**
|
|
320
|
+
* Unique name for the plugin, following the same naming convention as `createPlugin`.
|
|
321
|
+
*/
|
|
322
|
+
name: string;
|
|
323
|
+
/**
|
|
324
|
+
* Plugins that must be registered before this plugin executes.
|
|
325
|
+
* An error is thrown at startup when any listed dependency is missing.
|
|
326
|
+
*/
|
|
327
|
+
dependencies?: Array<string>;
|
|
328
|
+
/**
|
|
329
|
+
* Controls the execution order of this plugin relative to others.
|
|
330
|
+
*
|
|
331
|
+
* - `'pre'` — runs before all normal plugins.
|
|
332
|
+
* - `'post'` — runs after all normal plugins.
|
|
333
|
+
* - `undefined` (default) — runs in declaration order among normal plugins.
|
|
334
|
+
*
|
|
335
|
+
* Dependency constraints always take precedence over `enforce`.
|
|
336
|
+
*/
|
|
337
|
+
enforce?: 'pre' | 'post';
|
|
338
|
+
/**
|
|
339
|
+
* The options passed by the user when calling the plugin factory.
|
|
340
|
+
*/
|
|
341
|
+
options?: TFactory['options'];
|
|
342
|
+
/**
|
|
343
|
+
* Lifecycle event handlers for this plugin.
|
|
344
|
+
* Any event from the global `KubbHooks` map can be subscribed to here.
|
|
345
|
+
*/
|
|
346
|
+
hooks: { [K in Exclude<keyof KubbHooks, 'kubb:plugin:setup'>]?: (...args: KubbHooks[K]) => void | Promise<void> } & {
|
|
347
|
+
'kubb:plugin:setup'?(ctx: KubbPluginSetupContext<TFactory>): void | Promise<void>;
|
|
348
|
+
};
|
|
349
|
+
};
|
|
350
|
+
/**
|
|
351
|
+
* Creates a plugin factory using the hook-style (`hooks:`) API.
|
|
352
|
+
*
|
|
353
|
+
* The returned factory is called with optional options and produces a `Plugin`
|
|
354
|
+
* that coexists with plugins created via the legacy `createPlugin` API in the same
|
|
355
|
+
* `kubb.config.ts`.
|
|
356
|
+
*
|
|
357
|
+
* Lifecycle handlers are registered on the `PluginDriver`'s `AsyncEventEmitter`, enabling
|
|
358
|
+
* both the plugin's own handlers and external tooling (CLI, devtools) to observe every event.
|
|
359
|
+
*
|
|
360
|
+
* @example
|
|
361
|
+
* ```ts
|
|
362
|
+
* // With PluginFactoryOptions (recommended for real plugins)
|
|
363
|
+
* export const pluginTs = definePlugin<PluginTs>((options) => ({
|
|
364
|
+
* name: 'plugin-ts',
|
|
365
|
+
* hooks: {
|
|
366
|
+
* 'kubb:plugin:setup'(ctx) {
|
|
367
|
+
* ctx.setResolver(resolverTs) // typed as Partial<ResolverTs>
|
|
368
|
+
* },
|
|
369
|
+
* },
|
|
370
|
+
* }))
|
|
371
|
+
* ```
|
|
372
|
+
*/
|
|
373
|
+
declare function definePlugin<TFactory extends PluginFactoryOptions = PluginFactoryOptions>(factory: (options: TFactory['options']) => Plugin<TFactory>): (options?: TFactory['options']) => Plugin<TFactory>;
|
|
374
|
+
//#endregion
|
|
375
|
+
//#region src/FileManager.d.ts
|
|
376
|
+
/**
|
|
377
|
+
* In-memory file store for generated files.
|
|
378
|
+
*
|
|
379
|
+
* Files with the same `path` are merged — sources, imports, and exports are concatenated.
|
|
380
|
+
* The `files` getter returns all stored files sorted by path length (shortest first).
|
|
381
|
+
*
|
|
382
|
+
* @example
|
|
383
|
+
* ```ts
|
|
384
|
+
* import { FileManager } from '@kubb/core'
|
|
385
|
+
*
|
|
386
|
+
* const manager = new FileManager()
|
|
387
|
+
* manager.upsert(myFile)
|
|
388
|
+
* console.log(manager.files) // all stored files
|
|
389
|
+
* ```
|
|
390
|
+
*/
|
|
391
|
+
declare class FileManager {
|
|
392
|
+
#private;
|
|
393
|
+
/**
|
|
394
|
+
* Adds one or more files. Incoming files with the same path are merged
|
|
395
|
+
* (sources/imports/exports concatenated), but existing cache entries are
|
|
396
|
+
* replaced — use {@link upsert} when you want to merge into the cache too.
|
|
397
|
+
*/
|
|
398
|
+
add(...files: Array<FileNode>): Array<FileNode>;
|
|
399
|
+
/**
|
|
400
|
+
* Adds or merges one or more files.
|
|
401
|
+
* If a file with the same path already exists in the cache, its
|
|
402
|
+
* sources/imports/exports are merged into the incoming file.
|
|
403
|
+
*/
|
|
404
|
+
upsert(...files: Array<FileNode>): Array<FileNode>;
|
|
405
|
+
getByPath(path: string): FileNode | null;
|
|
406
|
+
deleteByPath(path: string): void;
|
|
407
|
+
clear(): void;
|
|
408
|
+
/**
|
|
409
|
+
* All stored files, sorted by path length (shorter paths first).
|
|
410
|
+
*/
|
|
411
|
+
get files(): Array<FileNode>;
|
|
412
|
+
}
|
|
413
|
+
//#endregion
|
|
414
|
+
//#region src/PluginDriver.d.ts
|
|
415
|
+
type Options = {
|
|
416
|
+
hooks: AsyncEventEmitter<KubbHooks>;
|
|
417
|
+
};
|
|
418
|
+
declare class PluginDriver {
|
|
419
|
+
#private;
|
|
420
|
+
readonly config: Config;
|
|
421
|
+
readonly options: Options;
|
|
422
|
+
/**
|
|
423
|
+
* Returns `'single'` when `fileOrFolder` has a file extension, `'split'` otherwise.
|
|
424
|
+
*
|
|
425
|
+
* @example
|
|
426
|
+
* ```ts
|
|
427
|
+
* PluginDriver.getMode('src/gen/types.ts') // 'single'
|
|
428
|
+
* PluginDriver.getMode('src/gen/types') // 'split'
|
|
429
|
+
* ```
|
|
430
|
+
*/
|
|
431
|
+
static getMode(fileOrFolder: string | undefined | null): 'single' | 'split';
|
|
432
|
+
/**
|
|
433
|
+
* The universal `@kubb/ast` `InputNode` produced by the adapter, set by
|
|
434
|
+
* the build pipeline after the adapter's `parse()` resolves.
|
|
435
|
+
*/
|
|
436
|
+
inputNode: InputNode | undefined;
|
|
437
|
+
adapter: Adapter | undefined;
|
|
438
|
+
/**
|
|
439
|
+
* Central file store for all generated files.
|
|
440
|
+
* Plugins should use `this.addFile()` / `this.upsertFile()` (via their context) to
|
|
441
|
+
* add files; this property gives direct read/write access when needed.
|
|
442
|
+
*/
|
|
443
|
+
readonly fileManager: FileManager;
|
|
444
|
+
readonly plugins: Map<string, NormalizedPlugin>;
|
|
445
|
+
constructor(config: Config, options: Options);
|
|
446
|
+
get hooks(): AsyncEventEmitter<KubbHooks>;
|
|
447
|
+
/**
|
|
448
|
+
* Registers a hook-style plugin's lifecycle handlers on the shared `AsyncEventEmitter`.
|
|
449
|
+
*
|
|
450
|
+
* For `kubb:plugin:setup`, the registered listener wraps the globally emitted context with a
|
|
451
|
+
* plugin-specific one so that `addGenerator`, `setResolver`, `setTransformer`, and
|
|
452
|
+
* `setRenderer` all target the correct `normalizedPlugin` entry in the plugins map.
|
|
453
|
+
*
|
|
454
|
+
* All other hooks are iterated and registered directly as pass-through listeners.
|
|
455
|
+
* Any event key present in the global `KubbHooks` interface can be subscribed to.
|
|
456
|
+
*
|
|
457
|
+
* External tooling can subscribe to any of these events via `hooks.on(...)` to observe
|
|
458
|
+
* the plugin lifecycle without modifying plugin behavior.
|
|
459
|
+
*
|
|
460
|
+
* @internal
|
|
461
|
+
*/
|
|
462
|
+
registerPluginHooks(hookPlugin: Plugin, normalizedPlugin: NormalizedPlugin): void;
|
|
463
|
+
/**
|
|
464
|
+
* Emits the `kubb:plugin:setup` event so that all registered hook-style plugin listeners
|
|
465
|
+
* can configure generators, resolvers, transformers and renderers before `buildStart` runs.
|
|
466
|
+
*
|
|
467
|
+
* Call this once from `safeBuild` before the plugin execution loop begins.
|
|
468
|
+
*/
|
|
469
|
+
emitSetupHooks(): Promise<void>;
|
|
470
|
+
/**
|
|
471
|
+
* Registers a generator for the given plugin on the shared event emitter.
|
|
472
|
+
*
|
|
473
|
+
* The generator's `schema`, `operation`, and `operations` methods are registered as
|
|
474
|
+
* listeners on `kubb:generate:schema`, `kubb:generate:operation`, and `kubb:generate:operations`
|
|
475
|
+
* respectively. Each listener is scoped to the owning plugin via a `ctx.plugin.name` check
|
|
476
|
+
* so that generators from different plugins do not cross-fire.
|
|
477
|
+
*
|
|
478
|
+
* The renderer resolution chain is: `generator.renderer → plugin.renderer → config.renderer`.
|
|
479
|
+
* Set `generator.renderer = null` to explicitly opt out of rendering even when the plugin
|
|
480
|
+
* declares a renderer.
|
|
481
|
+
*
|
|
482
|
+
* Call this method inside `addGenerator()` (in `kubb:plugin:setup`) to wire up a generator.
|
|
483
|
+
*/
|
|
484
|
+
registerGenerator(pluginName: string, gen: Generator): void;
|
|
485
|
+
/**
|
|
486
|
+
* Returns `true` when at least one generator was registered for the given plugin
|
|
487
|
+
* via `addGenerator()` in `kubb:plugin:setup` (event-based path).
|
|
488
|
+
*
|
|
489
|
+
* Used by the build loop to decide whether to walk the AST and emit generator events
|
|
490
|
+
* for a plugin that has no static `plugin.generators`.
|
|
491
|
+
*/
|
|
492
|
+
hasRegisteredGenerators(pluginName: string): boolean;
|
|
493
|
+
/**
|
|
494
|
+
* Unregisters all plugin lifecycle listeners from the shared event emitter.
|
|
495
|
+
* Called at the end of a build to prevent listener leaks across repeated builds.
|
|
496
|
+
*
|
|
497
|
+
* @internal
|
|
498
|
+
*/
|
|
499
|
+
dispose(): void;
|
|
500
|
+
/**
|
|
501
|
+
* Merges `partial` with the plugin's default resolver and stores the result.
|
|
502
|
+
* Also mirrors it onto `plugin.resolver` so callers using `getPlugin(name).resolver`
|
|
503
|
+
* get the up-to-date resolver without going through `getResolver()`.
|
|
504
|
+
*/
|
|
505
|
+
setPluginResolver(pluginName: string, partial: Partial<Resolver>): void;
|
|
506
|
+
/**
|
|
507
|
+
* Returns the resolver for the given plugin.
|
|
508
|
+
*
|
|
509
|
+
* Resolution order: dynamic resolver set via `setPluginResolver` → static resolver on the
|
|
510
|
+
* plugin → lazily created default resolver (identity name, no path transforms).
|
|
511
|
+
*/
|
|
512
|
+
getResolver<TName extends keyof Kubb.PluginRegistry>(pluginName: TName): Kubb.PluginRegistry[TName]['resolver'];
|
|
513
|
+
getResolver<TResolver extends Resolver = Resolver>(pluginName: string): TResolver;
|
|
514
|
+
getContext<TOptions extends PluginFactoryOptions>(plugin: NormalizedPlugin<TOptions>): GeneratorContext<TOptions> & Record<string, unknown>;
|
|
515
|
+
getPlugin<TName extends keyof Kubb.PluginRegistry>(pluginName: TName): Plugin<Kubb.PluginRegistry[TName]> | undefined;
|
|
516
|
+
getPlugin<TOptions extends PluginFactoryOptions = PluginFactoryOptions>(pluginName: string): Plugin<TOptions> | undefined;
|
|
517
|
+
/**
|
|
518
|
+
* Like `getPlugin` but throws a descriptive error when the plugin is not found.
|
|
519
|
+
*/
|
|
520
|
+
requirePlugin<TName extends keyof Kubb.PluginRegistry>(pluginName: TName): Plugin<Kubb.PluginRegistry[TName]>;
|
|
521
|
+
requirePlugin<TOptions extends PluginFactoryOptions = PluginFactoryOptions>(pluginName: string): Plugin<TOptions>;
|
|
522
|
+
}
|
|
523
|
+
//#endregion
|
|
524
|
+
//#region src/createKubb.d.ts
|
|
525
|
+
/**
|
|
526
|
+
* Full output produced by a successful or failed build.
|
|
527
|
+
*/
|
|
528
|
+
type BuildOutput = {
|
|
529
|
+
/**
|
|
530
|
+
* Plugins that threw during installation, paired with the caught error.
|
|
531
|
+
*/
|
|
532
|
+
failedPlugins: Set<{
|
|
533
|
+
plugin: Plugin;
|
|
534
|
+
error: Error;
|
|
535
|
+
}>;
|
|
536
|
+
files: Array<FileNode>;
|
|
537
|
+
driver: PluginDriver;
|
|
538
|
+
/**
|
|
539
|
+
* Elapsed time in milliseconds for each plugin, keyed by plugin name.
|
|
540
|
+
*/
|
|
541
|
+
pluginTimings: Map<string, number>;
|
|
542
|
+
error?: Error;
|
|
543
|
+
/**
|
|
544
|
+
* Raw generated source, keyed by absolute file path.
|
|
545
|
+
*/
|
|
546
|
+
sources: Map<string, string>;
|
|
547
|
+
};
|
|
548
|
+
type CreateKubbOptions = {
|
|
549
|
+
hooks?: AsyncEventEmitter<KubbHooks>;
|
|
550
|
+
};
|
|
551
|
+
/**
|
|
552
|
+
* Creates a Kubb instance bound to a single config entry.
|
|
553
|
+
*
|
|
554
|
+
* Accepts a user-facing config shape and resolves it to a full {@link Config} during
|
|
555
|
+
* `setup()`. The instance then holds shared state (`hooks`, `sources`, `driver`, `config`)
|
|
556
|
+
* across the `setup → build` lifecycle. Attach event listeners to `kubb.hooks` before
|
|
557
|
+
* calling `setup()` or `build()`.
|
|
558
|
+
*
|
|
559
|
+
* @example
|
|
560
|
+
* ```ts
|
|
561
|
+
* const kubb = createKubb(userConfig)
|
|
562
|
+
*
|
|
563
|
+
* kubb.hooks.on('kubb:plugin:end', ({ plugin, duration }) => {
|
|
564
|
+
* console.log(`${plugin.name} completed in ${duration}ms`)
|
|
565
|
+
* })
|
|
566
|
+
*
|
|
567
|
+
* const { files, failedPlugins } = await kubb.safeBuild()
|
|
568
|
+
* ```
|
|
569
|
+
*/
|
|
570
|
+
declare function createKubb(userConfig: UserConfig, options?: CreateKubbOptions): Kubb$1;
|
|
571
|
+
//#endregion
|
|
572
|
+
//#region src/Kubb.d.ts
|
|
573
|
+
/**
|
|
574
|
+
* The instance returned by {@link createKubb}.
|
|
575
|
+
*/
|
|
576
|
+
type Kubb$1 = {
|
|
577
|
+
/**
|
|
578
|
+
* The shared event emitter. Attach listeners here before calling `setup()` or `build()`.
|
|
579
|
+
*/
|
|
580
|
+
readonly hooks: AsyncEventEmitter<KubbHooks>;
|
|
581
|
+
/**
|
|
582
|
+
* Raw generated source, keyed by absolute file path.
|
|
583
|
+
* Populated after a successful `build()` or `safeBuild()` call.
|
|
584
|
+
*/
|
|
585
|
+
readonly sources: Map<string, string>;
|
|
586
|
+
/**
|
|
587
|
+
* The plugin driver. Available after `setup()` has been called.
|
|
588
|
+
*/
|
|
589
|
+
readonly driver: PluginDriver | undefined;
|
|
590
|
+
/**
|
|
591
|
+
* The resolved config with applied defaults. Available after `setup()` has been called.
|
|
592
|
+
*/
|
|
593
|
+
readonly config: Config | undefined;
|
|
594
|
+
/**
|
|
595
|
+
* Initializes all Kubb infrastructure: validates input, applies config defaults,
|
|
596
|
+
* runs the adapter, and creates the PluginDriver.
|
|
597
|
+
*
|
|
598
|
+
* Calling `build()` or `safeBuild()` without calling `setup()` first will
|
|
599
|
+
* automatically invoke `setup()` before proceeding.
|
|
600
|
+
*/
|
|
601
|
+
setup(): Promise<void>;
|
|
602
|
+
/**
|
|
603
|
+
* Runs a full Kubb build and throws on any error or plugin failure.
|
|
604
|
+
* Automatically calls `setup()` if it has not been called yet.
|
|
605
|
+
*/
|
|
606
|
+
build(): Promise<BuildOutput>;
|
|
607
|
+
/**
|
|
608
|
+
* Runs a full Kubb build and captures errors instead of throwing.
|
|
609
|
+
* Automatically calls `setup()` if it has not been called yet.
|
|
610
|
+
*/
|
|
611
|
+
safeBuild(): Promise<BuildOutput>;
|
|
612
|
+
};
|
|
613
|
+
/**
|
|
614
|
+
* Events emitted during the Kubb code generation lifecycle.
|
|
615
|
+
* These events can be listened to for logging, progress tracking, and custom integrations.
|
|
616
|
+
*
|
|
617
|
+
* @example
|
|
618
|
+
* ```typescript
|
|
619
|
+
* import type { AsyncEventEmitter } from '@internals/utils'
|
|
620
|
+
* import type { KubbHooks } from '@kubb/core'
|
|
621
|
+
*
|
|
622
|
+
* const hooks: AsyncEventEmitter<KubbHooks> = new AsyncEventEmitter()
|
|
623
|
+
*
|
|
624
|
+
* hooks.on('kubb:lifecycle:start', () => {
|
|
625
|
+
* console.log('Starting Kubb generation')
|
|
626
|
+
* })
|
|
627
|
+
*
|
|
628
|
+
* hooks.on('kubb:plugin:end', ({ plugin, duration }) => {
|
|
629
|
+
* console.log(`Plugin ${plugin.name} completed in ${duration}ms`)
|
|
630
|
+
* })
|
|
631
|
+
* ```
|
|
632
|
+
*/
|
|
633
|
+
interface KubbHooks {
|
|
634
|
+
/**
|
|
635
|
+
* Emitted at the beginning of the Kubb lifecycle, before any code generation starts.
|
|
636
|
+
*/
|
|
637
|
+
'kubb:lifecycle:start': [ctx: KubbLifecycleStartContext];
|
|
638
|
+
/**
|
|
639
|
+
* Emitted at the end of the Kubb lifecycle, after all code generation is complete.
|
|
640
|
+
*/
|
|
641
|
+
'kubb:lifecycle:end': [];
|
|
642
|
+
/**
|
|
643
|
+
* Emitted when configuration loading starts.
|
|
644
|
+
*/
|
|
645
|
+
'kubb:config:start': [];
|
|
646
|
+
/**
|
|
647
|
+
* Emitted when configuration loading is complete.
|
|
648
|
+
*/
|
|
649
|
+
'kubb:config:end': [ctx: KubbConfigEndContext];
|
|
650
|
+
/**
|
|
651
|
+
* Emitted when code generation phase starts.
|
|
652
|
+
*/
|
|
653
|
+
'kubb:generation:start': [ctx: KubbGenerationStartContext];
|
|
654
|
+
/**
|
|
655
|
+
* Emitted when code generation phase completes.
|
|
656
|
+
*/
|
|
657
|
+
'kubb:generation:end': [ctx: KubbGenerationEndContext];
|
|
658
|
+
/**
|
|
659
|
+
* Emitted with a summary of the generation results.
|
|
660
|
+
* Contains summary lines, title, and success status.
|
|
661
|
+
*/
|
|
662
|
+
'kubb:generation:summary': [ctx: KubbGenerationSummaryContext];
|
|
663
|
+
/**
|
|
664
|
+
* Emitted when code formatting starts (e.g., running Biome or Prettier).
|
|
665
|
+
*/
|
|
666
|
+
'kubb:format:start': [];
|
|
667
|
+
/**
|
|
668
|
+
* Emitted when code formatting completes.
|
|
669
|
+
*/
|
|
670
|
+
'kubb:format:end': [];
|
|
671
|
+
/**
|
|
672
|
+
* Emitted when linting starts.
|
|
673
|
+
*/
|
|
674
|
+
'kubb:lint:start': [];
|
|
675
|
+
/**
|
|
676
|
+
* Emitted when linting completes.
|
|
677
|
+
*/
|
|
678
|
+
'kubb:lint:end': [];
|
|
679
|
+
/**
|
|
680
|
+
* Emitted when plugin hooks execution starts.
|
|
681
|
+
*/
|
|
682
|
+
'kubb:hooks:start': [];
|
|
683
|
+
/**
|
|
684
|
+
* Emitted when plugin hooks execution completes.
|
|
685
|
+
*/
|
|
686
|
+
'kubb:hooks:end': [];
|
|
687
|
+
/**
|
|
688
|
+
* Emitted when a single hook execution starts (e.g., format or lint).
|
|
689
|
+
* The callback should be invoked when the command completes.
|
|
690
|
+
*/
|
|
691
|
+
'kubb:hook:start': [ctx: KubbHookStartContext];
|
|
692
|
+
/**
|
|
693
|
+
* Emitted when a single hook execution completes.
|
|
694
|
+
*/
|
|
695
|
+
'kubb:hook:end': [ctx: KubbHookEndContext];
|
|
696
|
+
/**
|
|
697
|
+
* Emitted when a new version of Kubb is available.
|
|
698
|
+
*/
|
|
699
|
+
'kubb:version:new': [ctx: KubbVersionNewContext];
|
|
700
|
+
/**
|
|
701
|
+
* Informational message event.
|
|
702
|
+
*/
|
|
703
|
+
'kubb:info': [ctx: KubbInfoContext];
|
|
704
|
+
/**
|
|
705
|
+
* Error event. Emitted when an error occurs during code generation.
|
|
706
|
+
*/
|
|
707
|
+
'kubb:error': [ctx: KubbErrorContext];
|
|
708
|
+
/**
|
|
709
|
+
* Success message event.
|
|
710
|
+
*/
|
|
711
|
+
'kubb:success': [ctx: KubbSuccessContext];
|
|
712
|
+
/**
|
|
713
|
+
* Warning message event.
|
|
714
|
+
*/
|
|
715
|
+
'kubb:warn': [ctx: KubbWarnContext];
|
|
716
|
+
/**
|
|
717
|
+
* Debug event for detailed logging.
|
|
718
|
+
* Contains timestamp, log messages, and optional filename.
|
|
719
|
+
*/
|
|
720
|
+
'kubb:debug': [ctx: KubbDebugContext];
|
|
721
|
+
/**
|
|
722
|
+
* Emitted when file processing starts.
|
|
723
|
+
* Contains the list of files to be processed.
|
|
724
|
+
*/
|
|
725
|
+
'kubb:files:processing:start': [ctx: KubbFilesProcessingStartContext];
|
|
726
|
+
/**
|
|
727
|
+
* Emitted for each file being processed, providing progress updates.
|
|
728
|
+
* Contains processed count, total count, percentage, and file details.
|
|
729
|
+
*/
|
|
730
|
+
'kubb:file:processing:update': [ctx: KubbFileProcessingUpdateContext];
|
|
731
|
+
/**
|
|
732
|
+
* Emitted when file processing completes.
|
|
733
|
+
* Contains the list of processed files.
|
|
734
|
+
*/
|
|
735
|
+
'kubb:files:processing:end': [ctx: KubbFilesProcessingEndContext];
|
|
736
|
+
/**
|
|
737
|
+
* Emitted when a plugin starts executing.
|
|
738
|
+
*/
|
|
739
|
+
'kubb:plugin:start': [ctx: KubbPluginStartContext];
|
|
740
|
+
/**
|
|
741
|
+
* Emitted when a plugin completes execution.
|
|
742
|
+
* Duration in ms.
|
|
743
|
+
*/
|
|
744
|
+
'kubb:plugin:end': [ctx: KubbPluginEndContext];
|
|
745
|
+
/**
|
|
746
|
+
* Fired once — before any plugin's `buildStart` runs — so that hook-style plugins
|
|
747
|
+
* can register generators, configure resolvers/transformers/renderers, or inject
|
|
748
|
+
* extra files. All `kubb:plugin:setup` handlers registered via `definePlugin` receive
|
|
749
|
+
* a plugin-specific context (with the correct `addGenerator` closure).
|
|
750
|
+
* External tooling can observe this event via `hooks.on('kubb:plugin:setup', …)`.
|
|
751
|
+
*/
|
|
752
|
+
'kubb:plugin:setup': [ctx: KubbPluginSetupContext];
|
|
753
|
+
/**
|
|
754
|
+
* Fired immediately before the plugin execution loop begins.
|
|
755
|
+
* The adapter has already parsed the source and `inputNode` is available.
|
|
756
|
+
*/
|
|
757
|
+
'kubb:build:start': [ctx: KubbBuildStartContext];
|
|
758
|
+
/**
|
|
759
|
+
* Fired after all plugins have run and all per-plugin barrels have been generated,
|
|
760
|
+
* but BEFORE files are written to disk.
|
|
761
|
+
* Use this event to inject final files (e.g. a root barrel) that must be persisted
|
|
762
|
+
* in the same write pass as the plugin output.
|
|
763
|
+
*/
|
|
764
|
+
'kubb:plugins:end': [ctx: KubbPluginsEndContext];
|
|
765
|
+
/**
|
|
766
|
+
* Fired after all files have been written to disk.
|
|
767
|
+
*/
|
|
768
|
+
'kubb:build:end': [ctx: KubbBuildEndContext];
|
|
769
|
+
/**
|
|
770
|
+
* Emitted for each schema node during the AST walk.
|
|
771
|
+
* Generator listeners registered via `addGenerator()` in `kubb:plugin:setup` respond to this event.
|
|
772
|
+
* The `ctx.plugin.name` identifies which plugin is driving the current walk.
|
|
773
|
+
* `ctx.options` carries the per-node resolved options (after exclude/include/override).
|
|
774
|
+
*/
|
|
775
|
+
'kubb:generate:schema': [node: SchemaNode, ctx: GeneratorContext];
|
|
776
|
+
/**
|
|
777
|
+
* Emitted for each operation node during the AST walk.
|
|
778
|
+
* Generator listeners registered via `addGenerator()` in `kubb:plugin:setup` respond to this event.
|
|
779
|
+
* The `ctx.plugin.name` identifies which plugin is driving the current walk.
|
|
780
|
+
* `ctx.options` carries the per-node resolved options (after exclude/include/override).
|
|
781
|
+
*/
|
|
782
|
+
'kubb:generate:operation': [node: OperationNode, ctx: GeneratorContext];
|
|
783
|
+
/**
|
|
784
|
+
* Emitted once after all operations have been walked, with the full collected array.
|
|
785
|
+
* Generator listeners with an `operations()` method respond to this event.
|
|
786
|
+
* The `ctx.plugin.name` identifies which plugin is driving the current walk.
|
|
787
|
+
* `ctx.options` carries the plugin-level resolved options for the batch call.
|
|
788
|
+
*/
|
|
789
|
+
'kubb:generate:operations': [nodes: Array<OperationNode>, ctx: GeneratorContext];
|
|
790
|
+
}
|
|
791
|
+
declare global {
|
|
792
|
+
namespace Kubb {
|
|
793
|
+
/**
|
|
794
|
+
* Registry that maps plugin names to their `PluginFactoryOptions`.
|
|
795
|
+
* Augment this interface in each plugin's `types.ts` to enable automatic
|
|
796
|
+
* typing for `getPlugin` and `requirePlugin`.
|
|
797
|
+
*
|
|
798
|
+
* @example
|
|
799
|
+
* ```ts
|
|
800
|
+
* // packages/plugin-ts/src/types.ts
|
|
801
|
+
* declare global {
|
|
802
|
+
* namespace Kubb {
|
|
803
|
+
* interface PluginRegistry {
|
|
804
|
+
* 'plugin-ts': PluginTs
|
|
805
|
+
* }
|
|
806
|
+
* }
|
|
807
|
+
* }
|
|
808
|
+
* ```
|
|
809
|
+
*/
|
|
810
|
+
interface PluginRegistry {}
|
|
811
|
+
/**
|
|
812
|
+
* Extension point for root `Config['output']` options.
|
|
813
|
+
* Augment the `output` key in middleware or plugin packages to add extra fields
|
|
814
|
+
* to the global output configuration without touching core types.
|
|
815
|
+
*
|
|
816
|
+
* @example
|
|
817
|
+
* ```ts
|
|
818
|
+
* // packages/middleware-barrel/src/types.ts
|
|
819
|
+
* declare global {
|
|
820
|
+
* namespace Kubb {
|
|
821
|
+
* interface ConfigOptionsRegistry {
|
|
822
|
+
* output: {
|
|
823
|
+
* barrelType?: import('./types.ts').BarrelType | false
|
|
824
|
+
* }
|
|
825
|
+
* }
|
|
826
|
+
* }
|
|
827
|
+
* }
|
|
828
|
+
* ```
|
|
829
|
+
*/
|
|
830
|
+
interface ConfigOptionsRegistry {}
|
|
831
|
+
/**
|
|
832
|
+
* Extension point for per-plugin `Output` options.
|
|
833
|
+
* Augment the `output` key in middleware or plugin packages to add extra fields
|
|
834
|
+
* to the per-plugin output configuration without touching core types.
|
|
835
|
+
*
|
|
836
|
+
* @example
|
|
837
|
+
* ```ts
|
|
838
|
+
* // packages/middleware-barrel/src/types.ts
|
|
839
|
+
* declare global {
|
|
840
|
+
* namespace Kubb {
|
|
841
|
+
* interface PluginOptionsRegistry {
|
|
842
|
+
* output: {
|
|
843
|
+
* barrelType?: import('./types.ts').BarrelType | false
|
|
844
|
+
* }
|
|
845
|
+
* }
|
|
846
|
+
* }
|
|
847
|
+
* }
|
|
848
|
+
* ```
|
|
849
|
+
*/
|
|
850
|
+
interface PluginOptionsRegistry {}
|
|
851
|
+
}
|
|
852
|
+
}
|
|
853
|
+
//#endregion
|
|
854
|
+
//#region src/defineMiddleware.d.ts
|
|
855
|
+
/**
|
|
856
|
+
* A middleware instance produced by calling a factory created with `defineMiddleware`.
|
|
857
|
+
* It declares event handlers under a `hooks` object which are registered on the
|
|
858
|
+
* shared emitter after all plugin hooks, so middleware handlers for any event
|
|
859
|
+
* always fire last.
|
|
860
|
+
*/
|
|
861
|
+
type Middleware = {
|
|
862
|
+
/**
|
|
863
|
+
* Unique identifier for this middleware.
|
|
864
|
+
*/
|
|
865
|
+
name: string;
|
|
866
|
+
/**
|
|
867
|
+
* Lifecycle event handlers for this middleware.
|
|
868
|
+
* Any event from the global `KubbHooks` map can be subscribed to here.
|
|
869
|
+
* Handlers are registered after all plugin handlers, so they always fire last.
|
|
870
|
+
*/
|
|
871
|
+
hooks: { [K in keyof KubbHooks]?: (...args: KubbHooks[K]) => void | Promise<void> };
|
|
872
|
+
};
|
|
873
|
+
/**
|
|
874
|
+
* Creates a middleware factory using the hook-style (`hooks:`) API.
|
|
875
|
+
*
|
|
876
|
+
* Mirrors `definePlugin`: the factory is called with optional options and returns a
|
|
877
|
+
* fresh `Middleware` instance. Placing per-build state (e.g. accumulators) inside the
|
|
878
|
+
* factory closure ensures each `createKubb` invocation gets its own isolated instance.
|
|
879
|
+
*
|
|
880
|
+
* @example
|
|
881
|
+
* ```ts
|
|
882
|
+
* // Stateless middleware
|
|
883
|
+
* export const logMiddleware = defineMiddleware(() => ({
|
|
884
|
+
* name: 'log-middleware',
|
|
885
|
+
* hooks: {
|
|
886
|
+
* 'kubb:build:end'({ files }) {
|
|
887
|
+
* console.log(`Build complete with ${files.length} files`)
|
|
888
|
+
* },
|
|
889
|
+
* },
|
|
890
|
+
* }))
|
|
891
|
+
*
|
|
892
|
+
* // Middleware with options and per-build state
|
|
893
|
+
* export const myMiddleware = defineMiddleware((options: { prefix: string } = { prefix: '' }) => {
|
|
894
|
+
* const seen = new Set<string>()
|
|
895
|
+
* return {
|
|
896
|
+
* name: 'my-middleware',
|
|
897
|
+
* hooks: {
|
|
898
|
+
* 'kubb:plugin:end'({ plugin }) {
|
|
899
|
+
* seen.add(`${options.prefix}${plugin.name}`)
|
|
900
|
+
* },
|
|
901
|
+
* },
|
|
902
|
+
* }
|
|
903
|
+
* })
|
|
904
|
+
*
|
|
905
|
+
* // Usage in kubb.config.ts:
|
|
906
|
+
* export default defineConfig({
|
|
907
|
+
* middleware: [logMiddleware(), myMiddleware({ prefix: 'pfx:' })],
|
|
908
|
+
* })
|
|
909
|
+
* ```
|
|
910
|
+
*/
|
|
911
|
+
declare function defineMiddleware<TOptions extends object = object>(factory: (options: TOptions) => Middleware): (options?: TOptions) => Middleware;
|
|
912
|
+
//#endregion
|
|
913
|
+
//#region src/defineParser.d.ts
|
|
914
|
+
type PrintOptions = {
|
|
915
|
+
extname?: FileNode['extname'];
|
|
916
|
+
};
|
|
917
|
+
type Parser<TMeta extends object = any> = {
|
|
918
|
+
name: string;
|
|
919
|
+
/**
|
|
920
|
+
* File extensions this parser handles.
|
|
921
|
+
* Use `undefined` to create a catch-all fallback parser.
|
|
922
|
+
*
|
|
923
|
+
* @example Handled extensions
|
|
924
|
+
* `['.ts', '.js']`
|
|
925
|
+
*/
|
|
926
|
+
extNames: Array<FileNode['extname']> | undefined;
|
|
927
|
+
/**
|
|
928
|
+
* Convert a resolved file to a string.
|
|
929
|
+
*/
|
|
930
|
+
parse(file: FileNode<TMeta>, options?: PrintOptions): Promise<string> | string;
|
|
931
|
+
};
|
|
932
|
+
/**
|
|
933
|
+
* Defines a parser with type safety.
|
|
934
|
+
*
|
|
935
|
+
* Use this function to create parsers that transform generated files to strings
|
|
936
|
+
* based on their extension.
|
|
937
|
+
*
|
|
938
|
+
* @example
|
|
939
|
+
* ```ts
|
|
940
|
+
* import { defineParser } from '@kubb/core'
|
|
941
|
+
*
|
|
942
|
+
* export const jsonParser = defineParser({
|
|
943
|
+
* name: 'json',
|
|
944
|
+
* extNames: ['.json'],
|
|
945
|
+
* parse(file) {
|
|
946
|
+
* const { extractStringsFromNodes } = await import('@kubb/ast')
|
|
947
|
+
* return file.sources.map((s) => extractStringsFromNodes(s.nodes ?? [])).join('\n')
|
|
948
|
+
* },
|
|
949
|
+
* })
|
|
950
|
+
* ```
|
|
951
|
+
*/
|
|
952
|
+
declare function defineParser<TMeta extends object = any>(parser: Parser<TMeta>): Parser<TMeta>;
|
|
953
|
+
//#endregion
|
|
954
|
+
//#region src/types.d.ts
|
|
955
|
+
/**
|
|
956
|
+
* Safely extracts the type of key `K` from `T`, returning `{}` when `K` is not a key of `T`.
|
|
957
|
+
* Used to implement optional interface augmentation for `Kubb.ConfigOptionsRegistry` and
|
|
958
|
+
* `Kubb.PluginOptionsRegistry` so that middleware and plugin packages can extend core types
|
|
959
|
+
* without requiring modifications to core.
|
|
960
|
+
*
|
|
961
|
+
* @internal
|
|
962
|
+
*/
|
|
963
|
+
type ExtractRegistryKey<T, K extends PropertyKey> = K extends keyof T ? T[K] : {};
|
|
964
|
+
type InputPath = {
|
|
965
|
+
/**
|
|
966
|
+
* Specify your Swagger/OpenAPI file, either as an absolute path or a path relative to the root.
|
|
967
|
+
*/
|
|
968
|
+
path: string;
|
|
969
|
+
};
|
|
970
|
+
type InputData = {
|
|
971
|
+
/**
|
|
972
|
+
* A `string` or `object` that contains your Swagger/OpenAPI data.
|
|
973
|
+
*/
|
|
974
|
+
data: string | unknown;
|
|
975
|
+
};
|
|
976
|
+
type Input = InputPath | InputData;
|
|
977
|
+
/**
|
|
978
|
+
* The raw source passed to an adapter's `parse` function.
|
|
979
|
+
* Mirrors the shape of `Config['input']` with paths already resolved to absolute.
|
|
980
|
+
*/
|
|
981
|
+
type AdapterSource = {
|
|
982
|
+
type: 'path';
|
|
983
|
+
path: string;
|
|
984
|
+
} | {
|
|
985
|
+
type: 'data';
|
|
986
|
+
data: string | unknown;
|
|
987
|
+
} | {
|
|
988
|
+
type: 'paths';
|
|
989
|
+
paths: Array<string>;
|
|
990
|
+
};
|
|
991
|
+
/**
|
|
992
|
+
* Type parameters for an adapter definition.
|
|
993
|
+
*
|
|
994
|
+
* Mirrors `PluginFactoryOptions` but scoped to the adapter lifecycle:
|
|
995
|
+
* - `TName` — unique string identifier (e.g. `'oas'`, `'asyncapi'`)
|
|
996
|
+
* - `TOptions` — raw user-facing options passed to the adapter factory
|
|
997
|
+
* - `TResolvedOptions` — defaults applied; what the adapter stores as `options`
|
|
998
|
+
* - `TDocument` — type of the raw source document exposed by the adapter after `parse()`
|
|
999
|
+
*/
|
|
1000
|
+
type AdapterFactoryOptions<TName extends string = string, TOptions extends object = object, TResolvedOptions extends object = TOptions, TDocument = unknown> = {
|
|
1001
|
+
name: TName;
|
|
1002
|
+
options: TOptions;
|
|
1003
|
+
resolvedOptions: TResolvedOptions;
|
|
1004
|
+
document: TDocument;
|
|
1005
|
+
};
|
|
1006
|
+
/**
|
|
1007
|
+
* An adapter converts a source file or data into a `@kubb/ast` `InputNode`.
|
|
1008
|
+
*
|
|
1009
|
+
* Adapters are the single entry-point for different schema formats
|
|
1010
|
+
* (OpenAPI, AsyncAPI, Drizzle, …) and produce the universal `InputNode`
|
|
1011
|
+
* that all Kubb plugins consume.
|
|
1012
|
+
*
|
|
1013
|
+
* @example
|
|
1014
|
+
* ```ts
|
|
1015
|
+
* import { oasAdapter } from '@kubb/adapter-oas'
|
|
1016
|
+
*
|
|
1017
|
+
* export default defineConfig({
|
|
1018
|
+
* adapter: adapterOas(), // default — OpenAPI / Swagger
|
|
1019
|
+
* input: { path: './openapi.yaml' },
|
|
1020
|
+
* plugins: [pluginTs(), pluginZod()],
|
|
1021
|
+
* })
|
|
1022
|
+
* ```
|
|
1023
|
+
*/
|
|
1024
|
+
type Adapter<TOptions extends AdapterFactoryOptions = AdapterFactoryOptions> = {
|
|
1025
|
+
/**
|
|
1026
|
+
* Human-readable identifier, e.g. `'oas'`, `'drizzle'`, `'asyncapi'`.
|
|
1027
|
+
*/
|
|
1028
|
+
name: TOptions['name'];
|
|
1029
|
+
/**
|
|
1030
|
+
* Resolved options (after defaults have been applied).
|
|
1031
|
+
*/
|
|
1032
|
+
options: TOptions['resolvedOptions'];
|
|
1033
|
+
/**
|
|
1034
|
+
* The raw source document produced after the first `parse()` call.
|
|
1035
|
+
* `undefined` before parsing; typed by the adapter's `TDocument` generic.
|
|
1036
|
+
*/
|
|
1037
|
+
document: TOptions['document'] | null;
|
|
1038
|
+
inputNode: InputNode | null;
|
|
1039
|
+
/**
|
|
1040
|
+
* Convert the raw source into a universal `InputNode`.
|
|
1041
|
+
*/
|
|
1042
|
+
parse: (source: AdapterSource) => PossiblePromise<InputNode>;
|
|
1043
|
+
/**
|
|
1044
|
+
* Extracts `ImportNode` entries needed by a `SchemaNode` tree.
|
|
1045
|
+
* Populated after the first `parse()` call. Returns an empty array before that.
|
|
1046
|
+
*
|
|
1047
|
+
* The `resolve` callback receives the collision-corrected schema name and must
|
|
1048
|
+
* return the `{ name, path }` pair for the import, or `undefined` to skip it.
|
|
1049
|
+
*/
|
|
1050
|
+
getImports: (node: SchemaNode, resolve: (schemaName: string) => {
|
|
1051
|
+
name: string;
|
|
1052
|
+
path: string;
|
|
1053
|
+
}) => Array<ImportNode>;
|
|
1054
|
+
};
|
|
1055
|
+
type DevtoolsOptions = {
|
|
1056
|
+
/**
|
|
1057
|
+
* Open the AST inspector view (`/ast`) in Kubb Studio.
|
|
1058
|
+
* When `false`, opens the main Studio page instead.
|
|
1059
|
+
* @default false
|
|
1060
|
+
*/
|
|
1061
|
+
ast?: boolean;
|
|
1062
|
+
};
|
|
1063
|
+
/**
|
|
1064
|
+
* @private
|
|
1065
|
+
*/
|
|
1066
|
+
type Config<TInput = Input> = {
|
|
1067
|
+
/**
|
|
1068
|
+
* The name to display in the CLI output.
|
|
1069
|
+
*/
|
|
1070
|
+
name?: string;
|
|
1071
|
+
/**
|
|
1072
|
+
* The project root directory, which can be either an absolute path or a path relative to the location of your `kubb.config.ts` file.
|
|
1073
|
+
* @default process.cwd()
|
|
1074
|
+
*/
|
|
1075
|
+
root: string;
|
|
1076
|
+
/**
|
|
1077
|
+
* An array of parsers used to convert generated files to strings.
|
|
1078
|
+
* Each parser handles specific file extensions (e.g. `.ts`, `.tsx`).
|
|
1079
|
+
*
|
|
1080
|
+
* A catch-all fallback parser is always appended last for any unhandled extension.
|
|
1081
|
+
*
|
|
1082
|
+
* When omitted, `parserTs` from `@kubb/parser-ts` is used automatically as the
|
|
1083
|
+
* default (requires `@kubb/parser-ts` to be installed as an optional dependency).
|
|
1084
|
+
* @default [parserTs] — from `@kubb/parser-ts`
|
|
1085
|
+
* @example
|
|
1086
|
+
* ```ts
|
|
1087
|
+
* import { parserTs, tsxParser } from '@kubb/parser-ts'
|
|
1088
|
+
* export default defineConfig({
|
|
1089
|
+
* parsers: [parserTs, tsxParser],
|
|
1090
|
+
* })
|
|
1091
|
+
* ```
|
|
1092
|
+
*/
|
|
1093
|
+
parsers: Array<Parser>;
|
|
1094
|
+
/**
|
|
1095
|
+
* Adapter that converts the input file into a `@kubb/ast` `InputNode` — the universal
|
|
1096
|
+
* intermediate representation consumed by all Kubb plugins.
|
|
1097
|
+
*
|
|
1098
|
+
* - Use `@kubb/adapter-oas` for OpenAPI / Swagger.
|
|
1099
|
+
* - Use `@kubb/adapter-drizzle` or `@kubb/adapter-asyncapi` for other formats.
|
|
1100
|
+
*
|
|
1101
|
+
* @example
|
|
1102
|
+
* ```ts
|
|
1103
|
+
* import { adapterOas } from '@kubb/adapter-oas'
|
|
1104
|
+
* export default defineConfig({
|
|
1105
|
+
* adapter: adapterOas(),
|
|
1106
|
+
* input: { path: './petStore.yaml' },
|
|
1107
|
+
* })
|
|
1108
|
+
* ```
|
|
1109
|
+
*/
|
|
1110
|
+
adapter: Adapter;
|
|
1111
|
+
/**
|
|
1112
|
+
* Source file or data to generate code from.
|
|
1113
|
+
* Use `input.path` for a file on disk or `input.data` for an inline string or object.
|
|
1114
|
+
*/
|
|
1115
|
+
input: TInput;
|
|
1116
|
+
output: {
|
|
1117
|
+
/**
|
|
1118
|
+
* Output directory for generated files.
|
|
1119
|
+
* Accepts an absolute path or a path relative to `root`.
|
|
1120
|
+
*/
|
|
1121
|
+
path: string;
|
|
1122
|
+
/**
|
|
1123
|
+
* Clean the output directory before each build.
|
|
1124
|
+
*/
|
|
1125
|
+
clean?: boolean;
|
|
1126
|
+
/**
|
|
1127
|
+
* Save files to the file system.
|
|
1128
|
+
* @default true
|
|
1129
|
+
* @deprecated Use `storage` to control where files are written.
|
|
1130
|
+
*/
|
|
1131
|
+
write?: boolean;
|
|
1132
|
+
/**
|
|
1133
|
+
* Specifies the formatting tool to be used.
|
|
1134
|
+
* - 'auto' automatically detects and uses oxfmt, biome, or prettier (in that order of preference).
|
|
1135
|
+
* - 'oxfmt' uses Oxfmt for code formatting.
|
|
1136
|
+
* - 'prettier' uses Prettier for code formatting.
|
|
1137
|
+
* - 'biome' uses Biome for code formatting.
|
|
1138
|
+
* - false disables code formatting.
|
|
1139
|
+
* @default 'prettier'
|
|
1140
|
+
*/
|
|
1141
|
+
format?: 'auto' | 'prettier' | 'biome' | 'oxfmt' | false;
|
|
1142
|
+
/**
|
|
1143
|
+
* Specifies the linter that should be used to analyze the code.
|
|
1144
|
+
* - 'auto' automatically detects and uses oxlint, biome, or eslint (in that order of preference).
|
|
1145
|
+
* - 'oxlint' uses Oxlint for linting.
|
|
1146
|
+
* - 'eslint' uses ESLint for linting.
|
|
1147
|
+
* - 'biome' uses Biome for linting.
|
|
1148
|
+
* - false disables linting.
|
|
1149
|
+
* @default 'auto'
|
|
1150
|
+
*/
|
|
1151
|
+
lint?: 'auto' | 'eslint' | 'biome' | 'oxlint' | false;
|
|
1152
|
+
/**
|
|
1153
|
+
* Overrides the extension for generated imports and exports. By default, each plugin adds an extension.
|
|
1154
|
+
* @default { '.ts': '.ts'}
|
|
1155
|
+
*/
|
|
1156
|
+
extension?: Record<FileNode['extname'], FileNode['extname'] | ''>;
|
|
1157
|
+
/**
|
|
1158
|
+
* Adds a default banner to the start of every generated file indicating it was generated by Kubb.
|
|
1159
|
+
* - 'simple' adds banner with link to Kubb.
|
|
1160
|
+
* - 'full' adds source, title, description, and OpenAPI version.
|
|
1161
|
+
* - false disables banner generation.
|
|
1162
|
+
* @default 'simple'
|
|
1163
|
+
*/
|
|
1164
|
+
defaultBanner?: 'simple' | 'full' | false;
|
|
1165
|
+
/**
|
|
1166
|
+
* Whether to override existing external files if they already exist.
|
|
1167
|
+
* When setting the option in the global configuration, all plugins inherit the same behavior by default.
|
|
1168
|
+
* However, all plugins also have an `output.override` option, which can be used to override the behavior for a specific plugin.
|
|
1169
|
+
* @default false
|
|
1170
|
+
*/
|
|
1171
|
+
override?: boolean;
|
|
1172
|
+
} & ExtractRegistryKey<Kubb.ConfigOptionsRegistry, 'output'>;
|
|
1173
|
+
/**
|
|
1174
|
+
* Storage backend for generated files.
|
|
1175
|
+
* Defaults to `fsStorage()` — the built-in filesystem driver.
|
|
1176
|
+
* Accepts any object implementing the {@link Storage} interface.
|
|
1177
|
+
* Keys are root-relative paths (e.g. `src/gen/api/getPets.ts`).
|
|
1178
|
+
* @default fsStorage()
|
|
1179
|
+
* @example
|
|
1180
|
+
* ```ts
|
|
1181
|
+
* import { memoryStorage } from '@kubb/core'
|
|
1182
|
+
* storage: memoryStorage()
|
|
1183
|
+
* ```
|
|
1184
|
+
*/
|
|
1185
|
+
storage?: Storage;
|
|
1186
|
+
/**
|
|
1187
|
+
* An array of Kubb plugins used for code generation.
|
|
1188
|
+
* Each plugin may declare additional configurable options.
|
|
1189
|
+
* If a plugin depends on another, an error is thrown when the dependency is missing.
|
|
1190
|
+
* Use `dependencies` on the plugin to declare execution order.
|
|
1191
|
+
*/
|
|
1192
|
+
plugins: Array<Plugin>;
|
|
1193
|
+
/**
|
|
1194
|
+
* Middleware instances that observe and post-process the build output.
|
|
1195
|
+
* Each middleware receives the `hooks` emitter and attaches its own listeners.
|
|
1196
|
+
* Middleware listeners are always registered after all plugin listeners,
|
|
1197
|
+
* so middleware hooks fire last for any given event.
|
|
1198
|
+
*
|
|
1199
|
+
* @example
|
|
1200
|
+
* ```ts
|
|
1201
|
+
* import { middlewareBarrel } from '@kubb/middleware-barrel'
|
|
1202
|
+
* export default defineConfig({
|
|
1203
|
+
* middleware: [middlewareBarrel],
|
|
1204
|
+
* plugins: [pluginTs(), pluginZod()],
|
|
1205
|
+
* })
|
|
1206
|
+
* ```
|
|
1207
|
+
*/
|
|
1208
|
+
middleware?: Array<Middleware>;
|
|
1209
|
+
/**
|
|
1210
|
+
* Project-wide renderer factory. All plugins and generators that do not declare their own
|
|
1211
|
+
* `renderer` ultimately fall back to this value.
|
|
1212
|
+
*
|
|
1213
|
+
* The resolution chain is: `generator.renderer` → `plugin.renderer` → `config.renderer` → `undefined` (raw `FileNode[]` mode).
|
|
1214
|
+
*
|
|
1215
|
+
* @example
|
|
1216
|
+
* ```ts
|
|
1217
|
+
* import { jsxRenderer } from '@kubb/renderer-jsx'
|
|
1218
|
+
* export default defineConfig({
|
|
1219
|
+
* renderer: jsxRenderer,
|
|
1220
|
+
* plugins: [pluginTs(), pluginZod()],
|
|
1221
|
+
* })
|
|
1222
|
+
* ```
|
|
1223
|
+
*/
|
|
1224
|
+
renderer?: RendererFactory;
|
|
1225
|
+
/**
|
|
1226
|
+
* Devtools configuration for Kubb Studio integration.
|
|
1227
|
+
*/
|
|
1228
|
+
devtools?: true | {
|
|
1229
|
+
/**
|
|
1230
|
+
* Override the Kubb Studio base URL.
|
|
1231
|
+
* @default 'https://studio.kubb.dev'
|
|
1232
|
+
*/
|
|
1233
|
+
studioUrl?: typeof DEFAULT_STUDIO_URL | (string & {});
|
|
1234
|
+
};
|
|
1235
|
+
/**
|
|
1236
|
+
* Hooks triggered when a specific action occurs in Kubb.
|
|
1237
|
+
*/
|
|
1238
|
+
hooks?: {
|
|
1239
|
+
/**
|
|
1240
|
+
* Hook that triggers at the end of all executions.
|
|
1241
|
+
* Useful for running Prettier or ESLint to format/lint your code.
|
|
1242
|
+
*/
|
|
1243
|
+
done?: string | Array<string>;
|
|
1244
|
+
};
|
|
1245
|
+
};
|
|
1246
|
+
/**
|
|
1247
|
+
* A type/string-pattern filter used for `include`, `exclude`, and `override` matching.
|
|
1248
|
+
*/
|
|
1249
|
+
type PatternFilter = {
|
|
1250
|
+
type: string;
|
|
1251
|
+
pattern: string | RegExp;
|
|
1252
|
+
};
|
|
1253
|
+
/**
|
|
1254
|
+
* A pattern filter paired with partial option overrides to apply when the pattern matches.
|
|
1255
|
+
*/
|
|
1256
|
+
type PatternOverride<TOptions> = PatternFilter & {
|
|
1257
|
+
options: Omit<Partial<TOptions>, 'override'>;
|
|
1258
|
+
};
|
|
1259
|
+
/**
|
|
1260
|
+
* Context passed to `resolver.resolveOptions` to apply include/exclude/override filtering
|
|
1261
|
+
* for a given operation or schema node.
|
|
1262
|
+
*/
|
|
1263
|
+
/**
|
|
1264
|
+
* Resolves filtered options for a given operation or schema node.
|
|
1265
|
+
*
|
|
1266
|
+
* @internal
|
|
1267
|
+
*/
|
|
1268
|
+
type ResolveOptionsContext<TOptions> = {
|
|
1269
|
+
options: TOptions;
|
|
1270
|
+
exclude?: Array<PatternFilter>;
|
|
1271
|
+
include?: Array<PatternFilter>;
|
|
1272
|
+
override?: Array<PatternOverride<TOptions>>;
|
|
1273
|
+
};
|
|
1274
|
+
/**
|
|
1275
|
+
* Base constraint for all plugin resolver objects.
|
|
1276
|
+
*
|
|
1277
|
+
* `default`, `resolveOptions`, `resolvePath`, and `resolveFile` are injected automatically
|
|
1278
|
+
* by `defineResolver` — plugin authors may override them but never need to implement them
|
|
1279
|
+
* from scratch.
|
|
1280
|
+
*
|
|
1281
|
+
* @example
|
|
1282
|
+
* ```ts
|
|
1283
|
+
* type MyResolver = Resolver & {
|
|
1284
|
+
* resolveName(node: SchemaNode): string
|
|
1285
|
+
* resolveTypedName(node: SchemaNode): string
|
|
1286
|
+
* }
|
|
1287
|
+
* ```
|
|
1288
|
+
*/
|
|
1289
|
+
type Resolver = {
|
|
1290
|
+
name: string;
|
|
1291
|
+
pluginName: Plugin['name'];
|
|
1292
|
+
default(name: string, type?: 'file' | 'function' | 'type' | 'const'): string;
|
|
1293
|
+
resolveOptions<TOptions>(node: Node, context: ResolveOptionsContext<TOptions>): TOptions | null;
|
|
1294
|
+
resolvePath(params: ResolverPathParams, context: ResolverContext): string;
|
|
1295
|
+
resolveFile(params: ResolverFileParams, context: ResolverContext): FileNode;
|
|
1296
|
+
resolveBanner(node: InputNode | null, context: ResolveBannerContext): string | undefined;
|
|
1297
|
+
resolveFooter(node: InputNode | null, context: ResolveBannerContext): string | undefined;
|
|
1298
|
+
};
|
|
1299
|
+
type PluginFactoryOptions<
|
|
1300
|
+
/**
|
|
1301
|
+
* Name to be used for the plugin.
|
|
1302
|
+
*/
|
|
1303
|
+
TName extends string = string,
|
|
1304
|
+
/**
|
|
1305
|
+
* Options of the plugin.
|
|
1306
|
+
*/
|
|
1307
|
+
TOptions extends object = object,
|
|
1308
|
+
/**
|
|
1309
|
+
* Options of the plugin that can be used later on, see `options` inside your plugin config.
|
|
1310
|
+
*/
|
|
1311
|
+
TResolvedOptions extends object = TOptions,
|
|
1312
|
+
/**
|
|
1313
|
+
* Resolver object that encapsulates the naming and path-resolution helpers used by this plugin.
|
|
1314
|
+
* Use `defineResolver` to define the resolver object and export it alongside the plugin.
|
|
1315
|
+
*/
|
|
1316
|
+
TResolver extends Resolver = Resolver> = {
|
|
1317
|
+
name: TName;
|
|
1318
|
+
options: TOptions;
|
|
1319
|
+
resolvedOptions: TResolvedOptions;
|
|
1320
|
+
resolver: TResolver;
|
|
1321
|
+
};
|
|
1322
|
+
/**
|
|
1323
|
+
* Internal representation of a plugin after normalization.
|
|
1324
|
+
* Extends the user-facing `Plugin` with runtime fields populated during `kubb:plugin:setup`.
|
|
1325
|
+
* Not part of the public API — use `Plugin` for external-facing interactions.
|
|
1326
|
+
* @internal
|
|
1327
|
+
*/
|
|
1328
|
+
type NormalizedPlugin<TOptions extends PluginFactoryOptions = PluginFactoryOptions> = Plugin<TOptions> & {
|
|
1329
|
+
options: TOptions['resolvedOptions'] & {
|
|
1330
|
+
output: Output;
|
|
1331
|
+
include?: Array<Include>;
|
|
1332
|
+
exclude: Array<Exclude$1>;
|
|
1333
|
+
override: Array<Override<TOptions['resolvedOptions']>>;
|
|
1334
|
+
};
|
|
1335
|
+
resolver: TOptions['resolver'];
|
|
1336
|
+
transformer?: Visitor;
|
|
1337
|
+
renderer?: RendererFactory;
|
|
1338
|
+
generators?: Array<Generator>;
|
|
1339
|
+
apply?: (config: Config) => boolean;
|
|
1340
|
+
version?: string;
|
|
1341
|
+
};
|
|
1342
|
+
/**
|
|
1343
|
+
* Partial version of {@link Config} intended for user-facing config entry points.
|
|
1344
|
+
*
|
|
1345
|
+
* Fields that have sensible defaults (`root`, `plugins`, `parsers`, `adapter`) are optional.
|
|
1346
|
+
*/
|
|
1347
|
+
type UserConfig<TInput = Input> = Omit<Config<TInput>, 'root' | 'plugins' | 'parsers' | 'adapter'> & {
|
|
1348
|
+
/**
|
|
1349
|
+
* The project root directory, which can be either an absolute path or a path relative to the location of your `kubb.config.ts` file.
|
|
1350
|
+
* @default process.cwd()
|
|
1351
|
+
*/
|
|
1352
|
+
root?: string;
|
|
1353
|
+
/**
|
|
1354
|
+
* An array of parsers used to convert generated files to strings.
|
|
1355
|
+
* Each parser handles specific file extensions (e.g. `.ts`, `.tsx`).
|
|
1356
|
+
*
|
|
1357
|
+
* A catch-all fallback parser is always appended last for any unhandled extension.
|
|
1358
|
+
*/
|
|
1359
|
+
parsers?: Array<Parser>;
|
|
1360
|
+
/**
|
|
1361
|
+
* Adapter that converts the input file into a `@kubb/ast` `InputNode`.
|
|
1362
|
+
*/
|
|
1363
|
+
adapter?: Adapter;
|
|
1364
|
+
/**
|
|
1365
|
+
* An array of Kubb plugins used for code generation.
|
|
1366
|
+
* Each entry is a hook-style plugin created with `definePlugin`.
|
|
1367
|
+
*/
|
|
1368
|
+
plugins?: Array<Plugin>;
|
|
1369
|
+
};
|
|
1370
|
+
type ResolveNameParams = {
|
|
1371
|
+
name: string;
|
|
1372
|
+
pluginName?: string;
|
|
1373
|
+
/**
|
|
1374
|
+
* Specifies the type of entity being named.
|
|
1375
|
+
* - `'file'` — customizes the name of the created file (camelCase).
|
|
1376
|
+
* - `'function'` — customizes the exported function names (camelCase).
|
|
1377
|
+
* - `'type'` — customizes TypeScript type names (PascalCase).
|
|
1378
|
+
* - `'const'` — customizes variable names (camelCase).
|
|
1379
|
+
*/
|
|
1380
|
+
type?: 'file' | 'function' | 'type' | 'const';
|
|
1381
|
+
};
|
|
1382
|
+
/**
|
|
1383
|
+
* Context object passed as the second argument to generator `schema`, `operation`, and
|
|
1384
|
+
* `operations` methods.
|
|
1385
|
+
*
|
|
1386
|
+
* Generators are only invoked from `runPluginAstHooks`, which already guards against a
|
|
1387
|
+
* missing adapter. This type reflects that guarantee — `ctx.adapter` and `ctx.inputNode`
|
|
1388
|
+
* are always defined, so no runtime checks or casts are needed inside generator bodies.
|
|
1389
|
+
*
|
|
1390
|
+
* `ctx.options` carries the per-node resolved options for `schema`/`operation` calls
|
|
1391
|
+
* (after exclude/include/override filtering) and the plugin-level options for `operations`.
|
|
1392
|
+
*/
|
|
1393
|
+
type GeneratorContext<TOptions extends PluginFactoryOptions = PluginFactoryOptions> = {
|
|
1394
|
+
config: Config;
|
|
1395
|
+
/**
|
|
1396
|
+
* Absolute path to the output directory for the current plugin.
|
|
1397
|
+
* Shorthand for `path.resolve(config.root, config.output.path)`.
|
|
1398
|
+
*/
|
|
1399
|
+
root: string;
|
|
1400
|
+
/**
|
|
1401
|
+
* Returns the output mode for the given output config.
|
|
1402
|
+
* Returns `'single'` when `output.path` has a file extension, `'split'` otherwise.
|
|
1403
|
+
*/
|
|
1404
|
+
getMode: (output: {
|
|
1405
|
+
path: string;
|
|
1406
|
+
}) => 'single' | 'split';
|
|
1407
|
+
driver: PluginDriver;
|
|
1408
|
+
/**
|
|
1409
|
+
* Get a plugin by name. Returns the plugin typed via `Kubb.PluginRegistry` when
|
|
1410
|
+
* the name is a registered key, otherwise returns the generic `Plugin`.
|
|
1411
|
+
*/
|
|
1412
|
+
getPlugin<TName extends keyof Kubb.PluginRegistry>(name: TName): Plugin<Kubb.PluginRegistry[TName]> | undefined;
|
|
1413
|
+
getPlugin(name: string): Plugin | undefined;
|
|
1414
|
+
/**
|
|
1415
|
+
* Like `getPlugin` but throws a descriptive error when the plugin is not found.
|
|
1416
|
+
*/
|
|
1417
|
+
requirePlugin<TName extends keyof Kubb.PluginRegistry>(name: TName): Plugin<Kubb.PluginRegistry[TName]>;
|
|
1418
|
+
requirePlugin(name: string): Plugin;
|
|
1419
|
+
/**
|
|
1420
|
+
* Get a resolver by plugin name. Returns the resolver typed via `Kubb.PluginRegistry` when
|
|
1421
|
+
* the name is a registered key, otherwise returns the generic `Resolver`.
|
|
1422
|
+
*/
|
|
1423
|
+
getResolver<TName extends keyof Kubb.PluginRegistry>(name: TName): Kubb.PluginRegistry[TName]['resolver'];
|
|
1424
|
+
getResolver(name: string): Resolver;
|
|
1425
|
+
/**
|
|
1426
|
+
* Add files only when they do not exist yet.
|
|
1427
|
+
*/
|
|
1428
|
+
addFile: (...file: Array<FileNode>) => Promise<void>;
|
|
1429
|
+
/**
|
|
1430
|
+
* Merge multiple sources into the same output file.
|
|
1431
|
+
*/
|
|
1432
|
+
upsertFile: (...file: Array<FileNode>) => Promise<void>;
|
|
1433
|
+
hooks: AsyncEventEmitter<KubbHooks>;
|
|
1434
|
+
/**
|
|
1435
|
+
* The current plugin.
|
|
1436
|
+
*/
|
|
1437
|
+
plugin: Plugin<TOptions>;
|
|
1438
|
+
/**
|
|
1439
|
+
* Resolver for the current plugin.
|
|
1440
|
+
*/
|
|
1441
|
+
resolver: TOptions['resolver'];
|
|
1442
|
+
/**
|
|
1443
|
+
* Composed transformer for the current plugin.
|
|
1444
|
+
*/
|
|
1445
|
+
transformer: Visitor | undefined;
|
|
1446
|
+
/**
|
|
1447
|
+
* Emit a warning via the build event system.
|
|
1448
|
+
*/
|
|
1449
|
+
warn: (message: string) => void;
|
|
1450
|
+
/**
|
|
1451
|
+
* Emit an error via the build event system.
|
|
1452
|
+
*/
|
|
1453
|
+
error: (error: string | Error) => void;
|
|
1454
|
+
/**
|
|
1455
|
+
* Emit an info message via the build event system.
|
|
1456
|
+
*/
|
|
1457
|
+
info: (message: string) => void;
|
|
1458
|
+
/**
|
|
1459
|
+
* Opens the Kubb Studio URL for the current `inputNode` in the default browser.
|
|
1460
|
+
*/
|
|
1461
|
+
openInStudio: (options?: DevtoolsOptions) => Promise<void>;
|
|
1462
|
+
/**
|
|
1463
|
+
* The adapter from `@kubb/ast`.
|
|
1464
|
+
*/
|
|
1465
|
+
adapter: Adapter;
|
|
1466
|
+
/**
|
|
1467
|
+
* The universal `@kubb/ast` `InputNode` produced by the configured adapter.
|
|
1468
|
+
*/
|
|
1469
|
+
inputNode: InputNode;
|
|
1470
|
+
/**
|
|
1471
|
+
* Per-node resolved options (after exclude/include/override filtering).
|
|
1472
|
+
*/
|
|
1473
|
+
options: TOptions['resolvedOptions'];
|
|
1474
|
+
};
|
|
1475
|
+
/**
|
|
1476
|
+
* Configure generated file output location and behavior.
|
|
1477
|
+
*/
|
|
1478
|
+
type Output<_TOptions = unknown> = {
|
|
1479
|
+
/**
|
|
1480
|
+
* Path to the output folder or file that will contain generated code.
|
|
1481
|
+
*/
|
|
1482
|
+
path: string;
|
|
1483
|
+
/**
|
|
1484
|
+
* Text or function appended at the start of every generated file.
|
|
1485
|
+
* When a function, receives the current `InputNode` and must return a string.
|
|
1486
|
+
*/
|
|
1487
|
+
banner?: string | ((node?: InputNode) => string);
|
|
1488
|
+
/**
|
|
1489
|
+
* Text or function appended at the end of every generated file.
|
|
1490
|
+
* When a function, receives the current `InputNode` and must return a string.
|
|
1491
|
+
*/
|
|
1492
|
+
footer?: string | ((node?: InputNode) => string);
|
|
1493
|
+
/**
|
|
1494
|
+
* Whether to override existing external files if they already exist.
|
|
1495
|
+
* @default false
|
|
1496
|
+
*/
|
|
1497
|
+
override?: boolean;
|
|
1498
|
+
} & ExtractRegistryKey<Kubb.PluginOptionsRegistry, 'output'>;
|
|
1499
|
+
type Group = {
|
|
1500
|
+
/**
|
|
1501
|
+
* Determines how files are grouped into subdirectories.
|
|
1502
|
+
* - `'tag'` groups files by OpenAPI tags.
|
|
1503
|
+
* - `'path'` groups files by OpenAPI paths.
|
|
1504
|
+
*/
|
|
1505
|
+
type: 'tag' | 'path';
|
|
1506
|
+
/**
|
|
1507
|
+
* Returns the subdirectory name for a given group value.
|
|
1508
|
+
* Defaults to `${camelCase(group)}Controller` for tags and the first path segment for paths.
|
|
1509
|
+
*/
|
|
1510
|
+
name?: (context: {
|
|
1511
|
+
group: string;
|
|
1512
|
+
}) => string;
|
|
1513
|
+
};
|
|
1514
|
+
type LoggerOptions = {
|
|
1515
|
+
/**
|
|
1516
|
+
* @default 3
|
|
1517
|
+
*/
|
|
1518
|
+
logLevel: (typeof logLevel)[keyof typeof logLevel];
|
|
1519
|
+
};
|
|
1520
|
+
/**
|
|
1521
|
+
* Shared context passed to all plugins, parsers, and other internals.
|
|
1522
|
+
*/
|
|
1523
|
+
type LoggerContext = AsyncEventEmitter<KubbHooks>;
|
|
1524
|
+
type Logger<TOptions extends LoggerOptions = LoggerOptions> = {
|
|
1525
|
+
name: string;
|
|
1526
|
+
install: (context: LoggerContext, options?: TOptions) => void | Promise<void>;
|
|
1527
|
+
};
|
|
1528
|
+
type UserLogger<TOptions extends LoggerOptions = LoggerOptions> = Logger<TOptions>;
|
|
1529
|
+
/**
|
|
1530
|
+
* Context passed to a hook-style plugin's `kubb:plugin:setup` handler.
|
|
1531
|
+
* Provides methods to register generators, configure the resolver, transformer,
|
|
1532
|
+
* and renderer, as well as access to the current build configuration.
|
|
1533
|
+
*/
|
|
1534
|
+
type KubbPluginSetupContext<TFactory extends PluginFactoryOptions = PluginFactoryOptions> = {
|
|
1535
|
+
/**
|
|
1536
|
+
* Register a generator on this plugin. Generators are invoked during the AST walk
|
|
1537
|
+
* (schema/operation/operations) exactly like generators declared statically on `createPlugin`.
|
|
1538
|
+
*/
|
|
1539
|
+
addGenerator<TElement = unknown>(generator: Generator<TFactory, TElement>): void;
|
|
1540
|
+
/**
|
|
1541
|
+
* Set or partially override the resolver for this plugin.
|
|
1542
|
+
* The resolver controls file naming and path resolution for generated files.
|
|
1543
|
+
*
|
|
1544
|
+
* When `TFactory` is a concrete `PluginFactoryOptions` (e.g. `PluginClient`),
|
|
1545
|
+
* the resolver parameter is typed to the plugin's own resolver type (e.g. `ResolverClient`).
|
|
1546
|
+
*/
|
|
1547
|
+
setResolver(resolver: Partial<TFactory['resolver']>): void;
|
|
1548
|
+
/**
|
|
1549
|
+
* Set the AST transformer (visitor) for this plugin.
|
|
1550
|
+
* The transformer pre-processes nodes before they reach the generators.
|
|
1551
|
+
*/
|
|
1552
|
+
setTransformer(visitor: Visitor): void;
|
|
1553
|
+
/**
|
|
1554
|
+
* Set the renderer factory for this plugin.
|
|
1555
|
+
* Used to process JSX elements returned by generators.
|
|
1556
|
+
*/
|
|
1557
|
+
setRenderer(renderer: RendererFactory): void;
|
|
1558
|
+
/**
|
|
1559
|
+
* Set the resolved options for the build loop. These options are merged into the
|
|
1560
|
+
* normalized plugin's `options` object (which includes `output`, `exclude`, `override`).
|
|
1561
|
+
*
|
|
1562
|
+
* Call this in `kubb:plugin:setup` to provide the resolved options that generators
|
|
1563
|
+
* and the build loop need (e.g., `enumType`, `optionalType`, `group`).
|
|
1564
|
+
*/
|
|
1565
|
+
setOptions(options: TFactory['resolvedOptions']): void;
|
|
1566
|
+
/**
|
|
1567
|
+
* Inject a raw file into the build output, bypassing the normal generation pipeline.
|
|
1568
|
+
*/
|
|
1569
|
+
injectFile(userFileNode: UserFileNode): void;
|
|
1570
|
+
/**
|
|
1571
|
+
* Merge a partial config update into the current build configuration.
|
|
1572
|
+
*/
|
|
1573
|
+
updateConfig(config: Partial<Config>): void;
|
|
1574
|
+
/**
|
|
1575
|
+
* The resolved build configuration at the time of setup.
|
|
1576
|
+
*/
|
|
1577
|
+
config: Config;
|
|
1578
|
+
/**
|
|
1579
|
+
* The plugin's own options as passed by the user.
|
|
1580
|
+
*/
|
|
1581
|
+
options: TFactory['options'];
|
|
1582
|
+
};
|
|
1583
|
+
/**
|
|
1584
|
+
* Context passed to a hook-style plugin's `kubb:build:start` handler.
|
|
1585
|
+
* Fires immediately before the plugin execution loop begins.
|
|
1586
|
+
*/
|
|
1587
|
+
type KubbBuildStartContext = {
|
|
1588
|
+
config: Config;
|
|
1589
|
+
adapter: Adapter;
|
|
1590
|
+
inputNode: InputNode;
|
|
1591
|
+
/**
|
|
1592
|
+
* Get a plugin by name. Returns the plugin typed via `Kubb.PluginRegistry` when
|
|
1593
|
+
* the name is a registered key, otherwise returns the generic `Plugin`.
|
|
1594
|
+
*/
|
|
1595
|
+
getPlugin<TName extends keyof Kubb.PluginRegistry>(name: TName): Plugin<Kubb.PluginRegistry[TName]> | undefined;
|
|
1596
|
+
getPlugin(name: string): Plugin | undefined;
|
|
1597
|
+
/**
|
|
1598
|
+
* Returns all files currently in the file manager.
|
|
1599
|
+
* Call this lazily (e.g. inside a `kubb:plugin:end` listener) to see files added by plugins
|
|
1600
|
+
* that have already run.
|
|
1601
|
+
*/
|
|
1602
|
+
readonly files: ReadonlyArray<FileNode>;
|
|
1603
|
+
/**
|
|
1604
|
+
* Upsert one or more files into the file manager.
|
|
1605
|
+
* Files with the same path are merged; new files are appended.
|
|
1606
|
+
* Safe to call at any point during the plugin lifecycle, including inside `kubb:plugin:end`.
|
|
1607
|
+
*/
|
|
1608
|
+
upsertFile: (...files: Array<FileNode>) => void;
|
|
1609
|
+
};
|
|
1610
|
+
/**
|
|
1611
|
+
* Context passed to `kubb:plugins:end` handlers.
|
|
1612
|
+
* Fires after all plugins have run and per-plugin barrels have been written,
|
|
1613
|
+
* but BEFORE files are written to disk.
|
|
1614
|
+
* Middleware that needs to inject final files (e.g. a root barrel) should use this event.
|
|
1615
|
+
*/
|
|
1616
|
+
type KubbPluginsEndContext = {
|
|
1617
|
+
config: Config;
|
|
1618
|
+
/**
|
|
1619
|
+
* Returns all files currently in the file manager (lazy snapshot).
|
|
1620
|
+
* Includes files added by plugins and per-plugin barrel middleware.
|
|
1621
|
+
*/
|
|
1622
|
+
readonly files: ReadonlyArray<FileNode>;
|
|
1623
|
+
/**
|
|
1624
|
+
* Upsert one or more files into the file manager.
|
|
1625
|
+
* Files added here will be included in the write pass that follows.
|
|
1626
|
+
*/
|
|
1627
|
+
upsertFile: (...files: Array<FileNode>) => void;
|
|
1628
|
+
};
|
|
1629
|
+
/**
|
|
1630
|
+
* Context passed to a hook-style plugin's `kubb:build:end` handler.
|
|
1631
|
+
* Fires after all files have been written to disk.
|
|
1632
|
+
*/
|
|
1633
|
+
type KubbBuildEndContext = {
|
|
1634
|
+
files: Array<FileNode>;
|
|
1635
|
+
config: Config;
|
|
1636
|
+
outputDir: string;
|
|
1637
|
+
};
|
|
1638
|
+
type KubbLifecycleStartContext = {
|
|
1639
|
+
version: string;
|
|
1640
|
+
};
|
|
1641
|
+
type KubbConfigEndContext = {
|
|
1642
|
+
configs: Array<Config>;
|
|
1643
|
+
};
|
|
1644
|
+
type KubbGenerationStartContext = {
|
|
1645
|
+
config: Config;
|
|
1646
|
+
};
|
|
1647
|
+
type KubbGenerationEndContext = {
|
|
1648
|
+
config: Config;
|
|
1649
|
+
files: Array<FileNode>;
|
|
1650
|
+
sources: Map<string, string>;
|
|
1651
|
+
};
|
|
1652
|
+
type KubbGenerationSummaryContext = {
|
|
1653
|
+
config: Config;
|
|
1654
|
+
failedPlugins: Set<{
|
|
1655
|
+
plugin: Plugin;
|
|
1656
|
+
error: Error;
|
|
1657
|
+
}>;
|
|
1658
|
+
status: 'success' | 'failed';
|
|
1659
|
+
hrStart: [number, number];
|
|
1660
|
+
filesCreated: number;
|
|
1661
|
+
pluginTimings?: Map<Plugin['name'], number>;
|
|
1662
|
+
};
|
|
1663
|
+
type KubbVersionNewContext = {
|
|
1664
|
+
currentVersion: string;
|
|
1665
|
+
latestVersion: string;
|
|
1666
|
+
};
|
|
1667
|
+
type KubbInfoContext = {
|
|
1668
|
+
message: string;
|
|
1669
|
+
info?: string;
|
|
1670
|
+
};
|
|
1671
|
+
type KubbErrorContext = {
|
|
1672
|
+
error: Error;
|
|
1673
|
+
meta?: Record<string, unknown>;
|
|
1674
|
+
};
|
|
1675
|
+
type KubbSuccessContext = {
|
|
1676
|
+
message: string;
|
|
1677
|
+
info?: string;
|
|
1678
|
+
};
|
|
1679
|
+
type KubbWarnContext = {
|
|
1680
|
+
message: string;
|
|
1681
|
+
info?: string;
|
|
1682
|
+
};
|
|
1683
|
+
type KubbDebugContext = {
|
|
1684
|
+
date: Date;
|
|
1685
|
+
logs: Array<string>;
|
|
1686
|
+
fileName?: string;
|
|
1687
|
+
};
|
|
1688
|
+
type KubbFilesProcessingStartContext = {
|
|
1689
|
+
files: Array<FileNode>;
|
|
1690
|
+
};
|
|
1691
|
+
type KubbFileProcessingUpdateContext = {
|
|
1692
|
+
/**
|
|
1693
|
+
* Number of files processed so far.
|
|
1694
|
+
*/
|
|
1695
|
+
processed: number;
|
|
1696
|
+
/**
|
|
1697
|
+
* Total number of files to process.
|
|
1698
|
+
*/
|
|
1699
|
+
total: number;
|
|
1700
|
+
/**
|
|
1701
|
+
* Processing percentage (0–100).
|
|
1702
|
+
*/
|
|
1703
|
+
percentage: number;
|
|
1704
|
+
/**
|
|
1705
|
+
* Optional source identifier.
|
|
1706
|
+
*/
|
|
1707
|
+
source?: string;
|
|
1708
|
+
/**
|
|
1709
|
+
* The file being processed.
|
|
1710
|
+
*/
|
|
1711
|
+
file: FileNode;
|
|
1712
|
+
/**
|
|
1713
|
+
* Kubb configuration.
|
|
1714
|
+
* Provides access to the current config during file processing.
|
|
1715
|
+
*/
|
|
1716
|
+
config: Config;
|
|
1717
|
+
};
|
|
1718
|
+
type KubbFilesProcessingEndContext = {
|
|
1719
|
+
files: Array<FileNode>;
|
|
1720
|
+
};
|
|
1721
|
+
type KubbPluginStartContext = {
|
|
1722
|
+
plugin: NormalizedPlugin;
|
|
1723
|
+
};
|
|
1724
|
+
type KubbPluginEndContext = {
|
|
1725
|
+
plugin: NormalizedPlugin;
|
|
1726
|
+
duration: number;
|
|
1727
|
+
success: boolean;
|
|
1728
|
+
error?: Error;
|
|
1729
|
+
config: Config;
|
|
1730
|
+
/**
|
|
1731
|
+
* Returns all files currently in the file manager (lazy snapshot).
|
|
1732
|
+
* Includes files added by plugins that have already run.
|
|
1733
|
+
*/
|
|
1734
|
+
readonly files: ReadonlyArray<FileNode>;
|
|
1735
|
+
/**
|
|
1736
|
+
* Upsert one or more files into the file manager.
|
|
1737
|
+
*/
|
|
1738
|
+
upsertFile: (...files: Array<FileNode>) => void;
|
|
1739
|
+
};
|
|
1740
|
+
type KubbHookStartContext = {
|
|
1741
|
+
id?: string;
|
|
1742
|
+
command: string;
|
|
1743
|
+
args?: readonly string[];
|
|
1744
|
+
};
|
|
1745
|
+
type KubbHookEndContext = {
|
|
1746
|
+
id?: string;
|
|
1747
|
+
command: string;
|
|
1748
|
+
args?: readonly string[];
|
|
1749
|
+
success: boolean;
|
|
1750
|
+
error: Error | null;
|
|
1751
|
+
};
|
|
1752
|
+
type ByTag = {
|
|
1753
|
+
type: 'tag';
|
|
1754
|
+
pattern: string | RegExp;
|
|
1755
|
+
};
|
|
1756
|
+
type ByOperationId = {
|
|
1757
|
+
type: 'operationId';
|
|
1758
|
+
pattern: string | RegExp;
|
|
1759
|
+
};
|
|
1760
|
+
type ByPath = {
|
|
1761
|
+
type: 'path';
|
|
1762
|
+
pattern: string | RegExp;
|
|
1763
|
+
};
|
|
1764
|
+
type ByMethod = {
|
|
1765
|
+
type: 'method';
|
|
1766
|
+
pattern: HttpMethod | RegExp;
|
|
1767
|
+
};
|
|
1768
|
+
type BySchemaName = {
|
|
1769
|
+
type: 'schemaName';
|
|
1770
|
+
pattern: string | RegExp;
|
|
1771
|
+
};
|
|
1772
|
+
type ByContentType = {
|
|
1773
|
+
type: 'contentType';
|
|
1774
|
+
pattern: string | RegExp;
|
|
1775
|
+
};
|
|
1776
|
+
/**
|
|
1777
|
+
* A pattern filter that prevents matching nodes from being generated.
|
|
1778
|
+
* Match by `tag`, `operationId`, `path`, `method`, `contentType`, or `schemaName`.
|
|
1779
|
+
*/
|
|
1780
|
+
type Exclude$1 = ByTag | ByOperationId | ByPath | ByMethod | ByContentType | BySchemaName;
|
|
1781
|
+
/**
|
|
1782
|
+
* A pattern filter that restricts generation to only matching nodes.
|
|
1783
|
+
* Match by `tag`, `operationId`, `path`, `method`, `contentType`, or `schemaName`.
|
|
1784
|
+
*/
|
|
1785
|
+
type Include = ByTag | ByOperationId | ByPath | ByMethod | ByContentType | BySchemaName;
|
|
1786
|
+
/**
|
|
1787
|
+
* A pattern filter paired with partial option overrides applied when the pattern matches.
|
|
1788
|
+
* Match by `tag`, `operationId`, `path`, `method`, `schemaName`, or `contentType`.
|
|
1789
|
+
*/
|
|
1790
|
+
type Override<TOptions> = (ByTag | ByOperationId | ByPath | ByMethod | BySchemaName | ByContentType) & {
|
|
1791
|
+
options: Partial<TOptions>;
|
|
1792
|
+
};
|
|
1793
|
+
/**
|
|
1794
|
+
* File-specific parameters for `Resolver.resolvePath`.
|
|
1795
|
+
*
|
|
1796
|
+
* Pass alongside a `ResolverContext` to identify which file to resolve.
|
|
1797
|
+
* Provide `tag` for tag-based grouping or `path` for path-based grouping.
|
|
1798
|
+
*
|
|
1799
|
+
* @example
|
|
1800
|
+
* ```ts
|
|
1801
|
+
* resolver.resolvePath(
|
|
1802
|
+
* { baseName: 'petTypes.ts', tag: 'pets' },
|
|
1803
|
+
* { root: '/src', output: { path: 'types' }, group: { type: 'tag' } },
|
|
1804
|
+
* )
|
|
1805
|
+
* // → '/src/types/petsController/petTypes.ts'
|
|
1806
|
+
* ```
|
|
1807
|
+
*/
|
|
1808
|
+
type ResolverPathParams = {
|
|
1809
|
+
baseName: FileNode['baseName'];
|
|
1810
|
+
pathMode?: 'single' | 'split';
|
|
1811
|
+
/**
|
|
1812
|
+
* Tag value used when `group.type === 'tag'`.
|
|
1813
|
+
*/
|
|
1814
|
+
tag?: string;
|
|
1815
|
+
/**
|
|
1816
|
+
* Path value used when `group.type === 'path'`.
|
|
1817
|
+
*/
|
|
1818
|
+
path?: string;
|
|
1819
|
+
};
|
|
1820
|
+
/**
|
|
1821
|
+
* Shared context passed as the second argument to `Resolver.resolvePath` and `Resolver.resolveFile`.
|
|
1822
|
+
*
|
|
1823
|
+
* Describes where on disk output is rooted, which output config is active, and the optional
|
|
1824
|
+
* grouping strategy that controls subdirectory layout.
|
|
1825
|
+
*
|
|
1826
|
+
* @example
|
|
1827
|
+
* ```ts
|
|
1828
|
+
* const context: ResolverContext = {
|
|
1829
|
+
* root: config.root,
|
|
1830
|
+
* output,
|
|
1831
|
+
* group,
|
|
1832
|
+
* }
|
|
1833
|
+
* ```
|
|
1834
|
+
*/
|
|
1835
|
+
type ResolverContext = {
|
|
1836
|
+
root: string;
|
|
1837
|
+
output: Output;
|
|
1838
|
+
group?: Group;
|
|
1839
|
+
/**
|
|
1840
|
+
* Plugin name used to populate `meta.pluginName` on the resolved file.
|
|
1841
|
+
*/
|
|
1842
|
+
pluginName?: string;
|
|
1843
|
+
};
|
|
1844
|
+
/**
|
|
1845
|
+
* File-specific parameters for `Resolver.resolveFile`.
|
|
1846
|
+
*
|
|
1847
|
+
* Pass alongside a `ResolverContext` to fully describe the file to resolve.
|
|
1848
|
+
* `tag` and `path` are used only when a matching `group` is present in the context.
|
|
1849
|
+
*
|
|
1850
|
+
* @example
|
|
1851
|
+
* ```ts
|
|
1852
|
+
* resolver.resolveFile(
|
|
1853
|
+
* { name: 'listPets', extname: '.ts', tag: 'pets' },
|
|
1854
|
+
* { root: '/src', output: { path: 'types' }, group: { type: 'tag' } },
|
|
1855
|
+
* )
|
|
1856
|
+
* // → { baseName: 'listPets.ts', path: '/src/types/petsController/listPets.ts', ... }
|
|
1857
|
+
* ```
|
|
1858
|
+
*/
|
|
1859
|
+
type ResolverFileParams = {
|
|
1860
|
+
name: string;
|
|
1861
|
+
extname: FileNode['extname'];
|
|
1862
|
+
/**
|
|
1863
|
+
* Tag value used when `group.type === 'tag'`.
|
|
1864
|
+
*/
|
|
1865
|
+
tag?: string;
|
|
1866
|
+
/**
|
|
1867
|
+
* Path value used when `group.type === 'path'`.
|
|
1868
|
+
*/
|
|
1869
|
+
path?: string;
|
|
1870
|
+
};
|
|
1871
|
+
/**
|
|
1872
|
+
* Context passed to `Resolver.resolveBanner` and `Resolver.resolveFooter`.
|
|
1873
|
+
*
|
|
1874
|
+
* `output` is optional — not every plugin configures a banner/footer.
|
|
1875
|
+
* `config` carries the global Kubb config, used to derive the default Kubb banner.
|
|
1876
|
+
*
|
|
1877
|
+
* @example
|
|
1878
|
+
* ```ts
|
|
1879
|
+
* resolver.resolveBanner(inputNode, { output: { banner: '// generated' }, config })
|
|
1880
|
+
* // → '// generated'
|
|
1881
|
+
* ```
|
|
1882
|
+
*/
|
|
1883
|
+
type ResolveBannerContext = {
|
|
1884
|
+
output?: Pick<Output, 'banner' | 'footer'>;
|
|
1885
|
+
config: Config;
|
|
1886
|
+
};
|
|
1887
|
+
/**
|
|
1888
|
+
* CLI options derived from command-line flags.
|
|
1889
|
+
*/
|
|
1890
|
+
type CLIOptions = {
|
|
1891
|
+
/**
|
|
1892
|
+
* Path to `kubb.config.js`.
|
|
1893
|
+
*/
|
|
1894
|
+
config?: string;
|
|
1895
|
+
/**
|
|
1896
|
+
* Enable watch mode for input files.
|
|
1897
|
+
*/
|
|
1898
|
+
watch?: boolean;
|
|
1899
|
+
/**
|
|
1900
|
+
* Logging verbosity for CLI usage.
|
|
1901
|
+
* @default 'silent'
|
|
1902
|
+
*/
|
|
1903
|
+
logLevel?: 'silent' | 'info' | 'debug';
|
|
1904
|
+
};
|
|
1905
|
+
/**
|
|
1906
|
+
* All accepted forms of a Kubb configuration.
|
|
1907
|
+
*
|
|
1908
|
+
* Config is always `@kubb/core` {@link Config}.
|
|
1909
|
+
* - `PossibleConfig` accepts `Config`/`Config[]`/promise or a no-arg config factory.
|
|
1910
|
+
* - `PossibleConfig<TCliOptions>` accepts the same config forms or a config factory receiving `TCliOptions`.
|
|
1911
|
+
*/
|
|
1912
|
+
type PossibleConfig<TCliOptions = undefined> = PossiblePromise<Config | Config[]> | ((...args: [TCliOptions] extends [undefined] ? [] : [TCliOptions]) => PossiblePromise<Config | Config[]>);
|
|
1913
|
+
//#endregion
|
|
1914
|
+
export { defineParser as $, KubbPluginStartContext as A, Override as B, KubbGenerationSummaryContext as C, KubbLifecycleStartContext as D, KubbInfoContext as E, Logger as F, ResolveOptionsContext as G, PossibleConfig as H, LoggerContext as I, ResolverFileParams as J, Resolver as K, LoggerOptions as L, KubbSuccessContext as M, KubbVersionNewContext as N, KubbPluginEndContext as O, KubbWarnContext as P, Parser as Q, NormalizedPlugin as R, KubbGenerationStartContext as S, KubbHookStartContext as T, ResolveBannerContext as U, PluginFactoryOptions as V, ResolveNameParams as W, UserConfig as X, ResolverPathParams as Y, UserLogger as Z, KubbErrorContext as _, logLevel as _t, Config as a, createKubb as at, KubbFilesProcessingStartContext as b, GeneratorContext as c, Plugin as ct, InputData as d, defineGenerator as dt, Middleware as et, InputPath as f, Storage as ft, KubbDebugContext as g, createRenderer as gt, KubbConfigEndContext as h, RendererFactory as ht, CLIOptions as i, BuildOutput as it, KubbPluginsEndContext as j, KubbPluginSetupContext as k, Group as l, definePlugin as lt, KubbBuildStartContext as m, Renderer as mt, AdapterFactoryOptions as n, Kubb$1 as nt, DevtoolsOptions as o, PluginDriver as ot, KubbBuildEndContext as p, createStorage as pt, ResolverContext as q, AdapterSource as r, KubbHooks as rt, Exclude$1 as s, FileManager as st, Adapter as t, defineMiddleware as tt, Include as u, Generator as ut, KubbFileProcessingUpdateContext as v, AsyncEventEmitter as vt, KubbHookEndContext as w, KubbGenerationEndContext as x, KubbFilesProcessingEndContext as y, Output as z };
|
|
1915
|
+
//# sourceMappingURL=types-DWtkW2RX.d.ts.map
|