@kubb/core 5.0.0-beta.2 → 5.0.0-beta.21

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (45) hide show
  1. package/README.md +8 -38
  2. package/dist/KubbDriver-BBRa5CH2.cjs +2231 -0
  3. package/dist/KubbDriver-BBRa5CH2.cjs.map +1 -0
  4. package/dist/KubbDriver-Cq1isv2P.js +2110 -0
  5. package/dist/KubbDriver-Cq1isv2P.js.map +1 -0
  6. package/dist/{types-CC09VtBt.d.ts → createKubb-CYrw_xaR.d.ts} +1414 -1255
  7. package/dist/index.cjs +221 -1074
  8. package/dist/index.cjs.map +1 -1
  9. package/dist/index.d.ts +2 -185
  10. package/dist/index.js +211 -1068
  11. package/dist/index.js.map +1 -1
  12. package/dist/mocks.cjs +30 -21
  13. package/dist/mocks.cjs.map +1 -1
  14. package/dist/mocks.d.ts +5 -5
  15. package/dist/mocks.js +29 -20
  16. package/dist/mocks.js.map +1 -1
  17. package/package.json +6 -18
  18. package/src/FileManager.ts +75 -58
  19. package/src/FileProcessor.ts +48 -38
  20. package/src/KubbDriver.ts +915 -0
  21. package/src/constants.ts +11 -6
  22. package/src/createAdapter.ts +84 -1
  23. package/src/createKubb.ts +1022 -485
  24. package/src/createRenderer.ts +33 -22
  25. package/src/defineGenerator.ts +96 -7
  26. package/src/defineLogger.ts +42 -3
  27. package/src/defineMiddleware.ts +1 -1
  28. package/src/defineParser.ts +1 -1
  29. package/src/definePlugin.ts +304 -8
  30. package/src/defineResolver.ts +271 -150
  31. package/src/devtools.ts +8 -1
  32. package/src/index.ts +2 -2
  33. package/src/mocks.ts +11 -14
  34. package/src/storages/fsStorage.ts +13 -37
  35. package/src/types.ts +39 -1292
  36. package/dist/PluginDriver-BXibeQk-.cjs +0 -1036
  37. package/dist/PluginDriver-BXibeQk-.cjs.map +0 -1
  38. package/dist/PluginDriver-DV3p2Hky.js +0 -945
  39. package/dist/PluginDriver-DV3p2Hky.js.map +0 -1
  40. package/src/Kubb.ts +0 -300
  41. package/src/PluginDriver.ts +0 -424
  42. package/src/renderNode.ts +0 -35
  43. package/src/utils/diagnostics.ts +0 -18
  44. package/src/utils/isInputPath.ts +0 -10
  45. package/src/utils/packageJSON.ts +0 -99
@@ -1,5 +1,5 @@
1
1
  import { t as __name } from "./chunk--u3MIqq1.js";
2
- import { FileNode, HttpMethod, ImportNode, InputNode, Node, OperationNode, SchemaNode, UserFileNode, Visitor } from "@kubb/ast";
2
+ import { FileNode, HttpMethod, ImportNode, InputMeta, InputNode, InputStreamNode, Node, OperationNode, SchemaNode, UserFileNode, Visitor } from "@kubb/ast";
3
3
 
4
4
  //#region ../../internals/utils/src/asyncEventEmitter.d.ts
5
5
  /**
@@ -33,7 +33,7 @@ declare class AsyncEventEmitter<TEvents extends { [K in keyof TEvents]: unknown[
33
33
  * await emitter.emit('build', 'petstore')
34
34
  * ```
35
35
  */
36
- emit<TEventName extends keyof TEvents & string>(eventName: TEventName, ...eventArgs: TEvents[TEventName]): Promise<void>;
36
+ emit<TEventName extends keyof TEvents & string>(eventName: TEventName, ...eventArgs: TEvents[TEventName]): Promise<void> | void;
37
37
  /**
38
38
  * Registers a persistent listener for `eventName`.
39
39
  *
@@ -98,7 +98,7 @@ type PossiblePromise<T> = Promise<T> | T;
98
98
  /**
99
99
  * Base URL for the Kubb Studio web app.
100
100
  */
101
- declare const DEFAULT_STUDIO_URL: "https://studio.kubb.dev";
101
+ declare const DEFAULT_STUDIO_URL: "https://kubb.studio";
102
102
  /**
103
103
  * Numeric log-level thresholds used internally to compare verbosity.
104
104
  *
@@ -113,55 +113,183 @@ declare const logLevel: {
113
113
  readonly debug: 5;
114
114
  };
115
115
  //#endregion
116
+ //#region src/createAdapter.d.ts
117
+ /**
118
+ * Source data passed to an adapter's `parse` function.
119
+ * Mirrors the config input shape with paths resolved to absolute.
120
+ */
121
+ type AdapterSource = {
122
+ type: 'path';
123
+ path: string;
124
+ } | {
125
+ type: 'data';
126
+ data: string | unknown;
127
+ } | {
128
+ type: 'paths';
129
+ paths: Array<string>;
130
+ };
131
+ /**
132
+ * Generic type parameters for an adapter definition.
133
+ *
134
+ * - `TName` — unique identifier (e.g. `'oas'`, `'asyncapi'`)
135
+ * - `TOptions` — user-facing options passed to the adapter factory
136
+ * - `TResolvedOptions` — options after defaults applied
137
+ * - `TDocument` — type of the parsed source document
138
+ */
139
+ type AdapterFactoryOptions<TName extends string = string, TOptions extends object = object, TResolvedOptions extends object = TOptions, TDocument = unknown> = {
140
+ name: TName;
141
+ options: TOptions;
142
+ resolvedOptions: TResolvedOptions;
143
+ document: TDocument;
144
+ };
145
+ /**
146
+ * Adapter that converts input files or data into an `InputNode`.
147
+ *
148
+ * Adapters parse different schema formats (OpenAPI, AsyncAPI, Drizzle, etc.) into Kubb's
149
+ * universal intermediate representation that all plugins consume.
150
+ *
151
+ * @example
152
+ * ```ts
153
+ * import { adapterOas } from '@kubb/adapter-oas'
154
+ *
155
+ * export default defineConfig({
156
+ * adapter: adapterOas(),
157
+ * input: { path: './openapi.yaml' },
158
+ * plugins: [pluginTs(), pluginZod()],
159
+ * })
160
+ * ```
161
+ */
162
+ type Adapter<TOptions extends AdapterFactoryOptions = AdapterFactoryOptions> = {
163
+ /**
164
+ * Human-readable adapter identifier (e.g. `'oas'`, `'asyncapi'`).
165
+ */
166
+ name: TOptions['name'];
167
+ /**
168
+ * Resolved adapter options after defaults have been applied.
169
+ */
170
+ options: TOptions['resolvedOptions'];
171
+ /**
172
+ * Parsed source document after the first `parse()` call. `null` before parsing.
173
+ */
174
+ document: TOptions['document'] | null;
175
+ /**
176
+ * Parse the source into a universal `InputNode`.
177
+ */
178
+ parse: (source: AdapterSource) => PossiblePromise<InputNode>;
179
+ /**
180
+ * Extract `ImportNode` entries for a schema tree.
181
+ * Returns an empty array before the first `parse()` call.
182
+ *
183
+ * The `resolve` callback receives the collision-corrected schema name and must
184
+ * return `{ name, path }` for the import, or `undefined` to skip it.
185
+ */
186
+ getImports: (node: SchemaNode, resolve: (schemaName: string) => {
187
+ name: string;
188
+ path: string;
189
+ }) => Array<ImportNode>;
190
+ /**
191
+ * Validate the document at the given path or URL.
192
+ */
193
+ validate: (input: string, options?: {
194
+ throwOnError?: boolean;
195
+ }) => Promise<void>;
196
+ /**
197
+ * Memory-efficient streaming variant of `parse()`.
198
+ *
199
+ * Returns an `InputStreamNode` whose `schemas` and `operations` are `AsyncIterable`.
200
+ * Each `for await` loop creates a fresh parse pass over the cached in-memory document —
201
+ * no pre-built arrays are held in memory.
202
+ */
203
+ stream?: (source: AdapterSource) => Promise<InputStreamNode>;
204
+ };
205
+ type AdapterBuilder<T extends AdapterFactoryOptions> = (options: T['options']) => Adapter<T>;
206
+ /**
207
+ * Factory for implementing custom adapters that translate non-OpenAPI specs into Kubb's AST.
208
+ *
209
+ * Use this to support GraphQL schemas, gRPC definitions, AsyncAPI, or custom domain-specific languages.
210
+ * Built-in adapters include `@kubb/adapter-oas` for OpenAPI and Swagger documents.
211
+ *
212
+ * @note Adapters must parse their input format to Kubb's `InputNode` structure.
213
+ *
214
+ * @example
215
+ * ```ts
216
+ * export const myAdapter = createAdapter<MyAdapter>((options) => {
217
+ * return {
218
+ * name: 'my-adapter',
219
+ * options,
220
+ * async parse(source) {
221
+ * // Transform source format to InputNode
222
+ * return { ... }
223
+ * },
224
+ * }
225
+ * })
226
+ *
227
+ * // Instantiate:
228
+ * const adapter = myAdapter({ validate: true })
229
+ * ```
230
+ */
231
+ declare function createAdapter<T extends AdapterFactoryOptions = AdapterFactoryOptions>(build: AdapterBuilder<T>): (options?: T['options']) => Adapter<T>;
232
+ //#endregion
116
233
  //#region src/createRenderer.d.ts
117
234
  /**
118
235
  * Minimal interface any Kubb renderer must satisfy.
119
236
  *
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.
237
+ * `TElement` is the type the renderer accepts, for example `KubbReactElement`
238
+ * for `@kubb/renderer-jsx` or a custom type for your own renderer. Defaults to
239
+ * `unknown` so generators that don't care about the element type work without
240
+ * specifying it.
127
241
  */
128
242
  type Renderer<TElement = unknown> = {
243
+ /**
244
+ * Renders `element` and populates {@link files} with the resulting {@link FileNode} objects.
245
+ * Called once per render cycle; must resolve before {@link files} is read.
246
+ */
129
247
  render(element: TElement): Promise<void>;
248
+ /**
249
+ * Tears down the renderer and releases any held resources.
250
+ * Pass an `Error` to signal a failure, a number for an exit code, or omit for a clean shutdown.
251
+ */
130
252
  unmount(error?: Error | number | null): void;
253
+ /**
254
+ * Releases any held resources. `[Symbol.dispose]` delegates here.
255
+ */
256
+ dispose(): void;
257
+ /**
258
+ * Accumulated {@link FileNode} results produced by the last {@link render} call.
259
+ * Not populated when {@link stream} is implemented.
260
+ */
131
261
  readonly files: Array<FileNode>;
262
+ /**
263
+ * When present, core calls this instead of {@link render} and {@link files},
264
+ * forwarding each file to `FileManager` as soon as it is ready.
265
+ */
266
+ stream?(element: TElement): Iterable<FileNode>;
267
+ /**
268
+ * Disposer hook so renderers participate in `using` blocks: `using r = rendererFactory()`
269
+ * guarantees {@link dispose} runs on every exit path, including thrown errors.
270
+ */
271
+ [Symbol.dispose](): void;
132
272
  };
133
273
  /**
134
- * A factory function that produces a fresh {@link Renderer} per render.
274
+ * A factory function that produces a fresh {@link Renderer} per render cycle.
135
275
  *
136
276
  * Generators use this to declare which renderer handles their output.
137
277
  */
138
278
  type RendererFactory<TElement = unknown> = () => Renderer<TElement>;
139
279
  /**
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.
280
+ * Wraps a renderer factory for use in generator definitions.
145
281
  *
146
282
  * @example
147
283
  * ```ts
148
- * // packages/renderer-jsx/src/index.ts
149
284
  * export const jsxRenderer = createRenderer(() => {
150
285
  * const runtime = new Runtime()
151
286
  * return {
152
287
  * async render(element) { await runtime.render(element) },
153
288
  * get files() { return runtime.nodes },
289
+ * dispose() { runtime.unmount() },
154
290
  * unmount(error) { runtime.unmount(error) },
155
291
  * }
156
292
  * })
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
293
  * ```
166
294
  */
167
295
  declare function createRenderer<TElement = unknown>(factory: RendererFactory<TElement>): RendererFactory<TElement>;
@@ -235,698 +363,1059 @@ type Storage = {
235
363
  */
236
364
  declare function createStorage<TOptions = Record<string, never>>(build: (options: TOptions) => Storage): (options?: TOptions) => Storage;
237
365
  //#endregion
238
- //#region src/defineGenerator.d.ts
366
+ //#region src/devtools.d.ts
367
+ type DevtoolsOptions = {
368
+ /**
369
+ * Open the AST inspector in Kubb Studio (`/ast`). Defaults to the main Studio page.
370
+ * @default false
371
+ */
372
+ ast?: boolean;
373
+ };
374
+ //#endregion
375
+ //#region src/defineParser.d.ts
376
+ type PrintOptions = {
377
+ extname?: FileNode['extname'];
378
+ };
379
+ type Parser<TMeta extends object = any> = {
380
+ name: string;
381
+ /**
382
+ * File extensions this parser handles.
383
+ * Use `undefined` to create a catch-all fallback parser.
384
+ *
385
+ * @example Handled extensions
386
+ * `['.ts', '.js']`
387
+ */
388
+ extNames: Array<FileNode['extname']> | undefined;
389
+ /**
390
+ * Convert a resolved file to a string.
391
+ */
392
+ parse(file: FileNode<TMeta>, options?: PrintOptions): string;
393
+ };
239
394
  /**
240
- * Declares a named generator unit that walks the AST and emits files.
241
- *
242
- * Each method (`schema`, `operation`, `operations`) is called for the matching node type.
243
- * Each method returns `TElement | Array<FileNode> | void`. JSX-based generators require a `renderer` factory.
244
- * Return `Array<FileNode>` directly or call `ctx.upsertFile()` manually and return `void` to bypass rendering.
395
+ * Defines a parser with type safety. Creates parsers that transform generated files to strings based on their extension.
245
396
  *
246
- * @note Generators are consumed by plugins and registered via `ctx.addGenerator()` in `kubb:plugin:setup`.
397
+ * @note Call the returned factory with optional options to instantiate the parser.
247
398
  *
248
399
  * @example
249
400
  * ```ts
250
- * import { defineGenerator } from '@kubb/core'
251
- * import { jsxRenderer } from '@kubb/renderer-jsx'
401
+ * import { defineParser } from '@kubb/core'
252
402
  *
253
- * export const typeGenerator = defineGenerator({
254
- * name: 'typescript',
255
- * renderer: jsxRenderer,
256
- * schema(node, ctx) {
257
- * const { adapter, resolver, root, options } = ctx
258
- * return <File ...><Type node={node} resolver={resolver} /></File>
403
+ * export const jsonParser = defineParser({
404
+ * name: 'json',
405
+ * extNames: ['.json'],
406
+ * parse(file) {
407
+ * const { extractStringsFromNodes } = await import('@kubb/ast')
408
+ * return file.sources.map((s) => extractStringsFromNodes(s.nodes ?? [])).join('\n')
259
409
  * },
260
410
  * })
261
411
  * ```
262
412
  */
263
- type Generator<TOptions extends PluginFactoryOptions = PluginFactoryOptions, TElement = unknown> = {
264
- /**
265
- * Used in diagnostic messages and debug output.
266
- */
267
- name: string;
268
- /**
269
- * Optional renderer factory that produces a {@link Renderer} for each render cycle.
270
- *
271
- * Generators that return renderer elements (e.g. JSX via `@kubb/renderer-jsx`) must set this
272
- * to the matching renderer factory (e.g. `jsxRenderer` from `@kubb/renderer-jsx`).
273
- *
274
- * Generators that only return `Array<FileNode>` or `void` do not need to set this.
275
- *
276
- * Set `renderer: null` to explicitly opt out of rendering even when the parent plugin
277
- * declares a `renderer` (overrides the plugin-level fallback).
278
- *
279
- * @example
280
- * ```ts
281
- * import { jsxRenderer } from '@kubb/renderer-jsx'
282
- * export const myGenerator = defineGenerator<PluginTs>({
283
- * renderer: jsxRenderer,
284
- * schema(node, ctx) { return <File ...>...</File> },
285
- * })
286
- * ```
287
- */
288
- renderer?: RendererFactory<TElement> | null;
289
- /**
290
- * Called for each schema node in the AST walk.
291
- * `ctx` carries the plugin context with `adapter` and `inputNode` guaranteed present,
292
- * plus `ctx.options` with the per-node resolved options (after exclude/include/override).
293
- */
294
- schema?: (node: SchemaNode, ctx: GeneratorContext<TOptions>) => PossiblePromise<TElement | Array<FileNode> | void>;
413
+ declare function defineParser<TMeta extends object = any>(parser: Parser<TMeta>): Parser<TMeta>;
414
+ //#endregion
415
+ //#region src/FileProcessor.d.ts
416
+ type ParseOptions = {
417
+ parsers?: Map<FileNode['extname'], Parser>;
418
+ extension?: Record<FileNode['extname'], FileNode['extname'] | ''>;
419
+ };
420
+ type FileProcessorEvents = {
421
+ start: [files: Array<FileNode>];
422
+ update: [params: {
423
+ file: FileNode;
424
+ source?: string;
425
+ processed: number;
426
+ total: number;
427
+ percentage: number;
428
+ }];
429
+ end: [files: Array<FileNode>];
430
+ };
431
+ type ParsedFile = {
432
+ file: FileNode;
433
+ source: string;
434
+ processed: number;
435
+ total: number;
436
+ percentage: number;
437
+ };
438
+ /**
439
+ * Converts a single file to a string using the registered parsers.
440
+ * Falls back to joining source values when no matching parser is found.
441
+ *
442
+ * @internal
443
+ */
444
+ declare class FileProcessor {
445
+ readonly events: AsyncEventEmitter<FileProcessorEvents>;
446
+ parse(file: FileNode, {
447
+ parsers,
448
+ extension
449
+ }?: ParseOptions): string;
450
+ stream(files: ReadonlyArray<FileNode>, options?: ParseOptions): Generator<ParsedFile>;
451
+ run(files: Array<FileNode>, options?: ParseOptions): Promise<Array<FileNode>>;
295
452
  /**
296
- * Called for each operation node in the AST walk.
297
- * `ctx` carries the plugin context with `adapter` and `inputNode` guaranteed present,
298
- * plus `ctx.options` with the per-node resolved options (after exclude/include/override).
453
+ * Clears all registered event listeners.
299
454
  */
300
- operation?: (node: OperationNode, ctx: GeneratorContext<TOptions>) => PossiblePromise<TElement | Array<FileNode> | void>;
455
+ dispose(): void;
456
+ [Symbol.dispose](): void;
457
+ }
458
+ //#endregion
459
+ //#region src/defineLogger.d.ts
460
+ type LoggerOptions = {
301
461
  /**
302
- * Called once after all operations have been walked.
303
- * `ctx` carries the plugin context with `adapter` and `inputNode` guaranteed present,
304
- * plus `ctx.options` with the plugin-level options for the batch call.
462
+ * Log level for output verbosity.
463
+ * @default 3
305
464
  */
306
- operations?: (nodes: Array<OperationNode>, ctx: GeneratorContext<TOptions>) => PossiblePromise<TElement | Array<FileNode> | void>;
465
+ logLevel: (typeof logLevel)[keyof typeof logLevel];
307
466
  };
308
467
  /**
309
- * Defines a generator. Returns the object as-is with correct `this` typings.
310
- * `applyHookResult` handles renderer elements and `File[]` uniformly using
311
- * the generator's declared `renderer` factory.
468
+ * Shared context passed to plugins, parsers, and other internals.
312
469
  */
313
- declare function defineGenerator<TOptions extends PluginFactoryOptions = PluginFactoryOptions, TElement = unknown>(generator: Generator<TOptions, TElement>): Generator<TOptions, TElement>;
314
- //#endregion
315
- //#region src/definePlugin.d.ts
470
+ type LoggerContext = AsyncEventEmitter<KubbHooks>;
471
+ type Logger<TOptions extends LoggerOptions = LoggerOptions, TInstallReturn = void> = {
472
+ name: string;
473
+ install: (context: LoggerContext, options?: TOptions) => TInstallReturn | Promise<TInstallReturn>;
474
+ };
475
+ type UserLogger<TOptions extends LoggerOptions = LoggerOptions, TInstallReturn = void> = Logger<TOptions, TInstallReturn>;
316
476
  /**
317
- * A plugin object produced by `definePlugin`.
318
- * Instead of flat lifecycle methods, it groups all handlers under a `hooks:` property
319
- * (matching Astro's integration naming convention).
477
+ * Wraps a logger definition into a typed {@link Logger}.
320
478
  *
321
- * @template TFactory - The plugin's `PluginFactoryOptions` type.
479
+ * The optional second type parameter `TInstallReturn` allows loggers to return
480
+ * a value from `install` — for example, a sink factory that the caller can
481
+ * forward to hook execution.
482
+ *
483
+ * @example Basic logger
484
+ * ```ts
485
+ * export const myLogger = defineLogger({
486
+ * name: 'my-logger',
487
+ * install(context, options) {
488
+ * context.on('kubb:info', (message) => console.log('ℹ', message))
489
+ * context.on('kubb:error', (error) => console.error('✗', error.message))
490
+ * },
491
+ * })
492
+ * ```
493
+ *
494
+ * @example Logger that returns a hook sink factory
495
+ * ```ts
496
+ * export const myLogger = defineLogger<LoggerOptions, HookSinkFactory>({
497
+ * name: 'my-logger',
498
+ * install(context, options) {
499
+ * // … register event handlers …
500
+ * return (commandWithArgs) => ({ onStdout: console.log })
501
+ * },
502
+ * })
503
+ * ```
322
504
  */
323
- type Plugin<TFactory extends PluginFactoryOptions = PluginFactoryOptions> = {
505
+ declare function defineLogger<Options extends LoggerOptions = LoggerOptions, TInstallReturn = void>(logger: UserLogger<Options, TInstallReturn>): Logger<Options, TInstallReturn>;
506
+ //#endregion
507
+ //#region src/defineMiddleware.d.ts
508
+ /**
509
+ * A middleware instance produced by calling a factory created with `defineMiddleware`.
510
+ * It declares event handlers under a `hooks` object which are registered on the
511
+ * shared emitter after all plugin hooks, so middleware handlers for any event
512
+ * always fire last.
513
+ */
514
+ type Middleware = {
324
515
  /**
325
- * Unique name for the plugin, following the same naming convention as `createPlugin`.
516
+ * Unique identifier for this middleware.
326
517
  */
327
518
  name: string;
328
519
  /**
329
- * Plugins that must be registered before this plugin executes.
330
- * An error is thrown at startup when any listed dependency is missing.
331
- */
332
- dependencies?: Array<string>;
333
- /**
334
- * Controls the execution order of this plugin relative to others.
335
- *
336
- * - `'pre'` — runs before all normal plugins.
337
- * - `'post'` — runs after all normal plugins.
338
- * - `undefined` (default) — runs in declaration order among normal plugins.
339
- *
340
- * Dependency constraints always take precedence over `enforce`.
341
- */
342
- enforce?: 'pre' | 'post';
343
- /**
344
- * The options passed by the user when calling the plugin factory.
345
- */
346
- options?: TFactory['options'];
347
- /**
348
- * Lifecycle event handlers for this plugin.
520
+ * Lifecycle event handlers for this middleware.
349
521
  * Any event from the global `KubbHooks` map can be subscribed to here.
522
+ * Handlers are registered after all plugin handlers, so they always fire last.
350
523
  */
351
- hooks: { [K in Exclude<keyof KubbHooks, 'kubb:plugin:setup'>]?: (...args: KubbHooks[K]) => void | Promise<void> } & {
352
- 'kubb:plugin:setup'?(ctx: KubbPluginSetupContext<TFactory>): void | Promise<void>;
353
- };
524
+ hooks: { [K in keyof KubbHooks]?: (...args: KubbHooks[K]) => void | Promise<void> };
354
525
  };
355
526
  /**
356
- * Wraps a factory function and returns a typed `Plugin` with lifecycle handlers grouped under `hooks`.
527
+ * Creates a middleware factory using the hook-style `hooks` API.
357
528
  *
358
- * Handlers live in a single `hooks` object (inspired by Astro integrations).
359
- * All lifecycle events from `KubbHooks` are available for subscription.
529
+ * Middleware handlers fire after all plugin handlers for any given event, making them ideal for post-processing, logging, and auditing.
530
+ * Per-build state (such as accumulators) belongs inside the factory closure so each `createKubb` invocation gets its own isolated instance.
360
531
  *
361
- * @note For real plugins, use a `PluginFactoryOptions` type parameter to get type-safe context in `kubb:plugin:setup`.
362
- * Plugin names should follow the convention `plugin-<feature>` (e.g., `plugin-react-query`, `plugin-zod`).
532
+ * @note The factory can accept typed options. See examples for using options and per-build state patterns.
363
533
  *
364
534
  * @example
365
535
  * ```ts
366
- * import { definePlugin } from '@kubb/core'
536
+ * import { defineMiddleware } from '@kubb/core'
367
537
  *
368
- * export const pluginTs = definePlugin((options: { prefix?: string } = {}) => ({
369
- * name: 'plugin-ts',
538
+ * // Stateless middleware
539
+ * export const logMiddleware = defineMiddleware(() => ({
540
+ * name: 'log-middleware',
370
541
  * hooks: {
371
- * 'kubb:plugin:setup'(ctx) {
372
- * ctx.setResolver(resolverTs)
542
+ * 'kubb:build:end'({ files }) {
543
+ * console.log(`Build complete with ${files.length} files`)
373
544
  * },
374
545
  * },
375
546
  * }))
547
+ *
548
+ * // Middleware with options and per-build state
549
+ * export const prefixMiddleware = defineMiddleware((options: { prefix: string } = { prefix: '' }) => {
550
+ * const seen = new Set<string>()
551
+ * return {
552
+ * name: 'prefix-middleware',
553
+ * hooks: {
554
+ * 'kubb:plugin:end'({ plugin }) {
555
+ * seen.add(`${options.prefix}${plugin.name}`)
556
+ * },
557
+ * },
558
+ * }
559
+ * })
376
560
  * ```
377
561
  */
378
- declare function definePlugin<TFactory extends PluginFactoryOptions = PluginFactoryOptions>(factory: (options: TFactory['options']) => Plugin<TFactory>): (options?: TFactory['options']) => Plugin<TFactory>;
562
+ declare function defineMiddleware<TOptions extends object = object>(factory: (options: TOptions) => Middleware): (options?: TOptions) => Middleware;
379
563
  //#endregion
380
- //#region src/FileManager.d.ts
564
+ //#region src/defineResolver.d.ts
565
+ /**
566
+ * Type/string pattern filter for include/exclude/override matching.
567
+ */
568
+ type PatternFilter = {
569
+ type: string;
570
+ pattern: string | RegExp;
571
+ };
381
572
  /**
382
- * In-memory file store for generated files.
573
+ * Pattern filter with partial option overrides applied when the pattern matches.
574
+ */
575
+ type PatternOverride<TOptions> = PatternFilter & {
576
+ options: Omit<Partial<TOptions>, 'override'>;
577
+ };
578
+ /**
579
+ * Context for resolving filtered options for a given operation or schema node.
580
+ *
581
+ * @internal
582
+ */
583
+ type ResolveOptionsContext<TOptions> = {
584
+ options: TOptions;
585
+ exclude?: Array<PatternFilter>;
586
+ include?: Array<PatternFilter>;
587
+ override?: Array<PatternOverride<TOptions>>;
588
+ };
589
+ /**
590
+ * Base constraint for all plugin resolver objects.
383
591
  *
384
- * Files with the same `path` are merged — sources, imports, and exports are concatenated.
385
- * The `files` getter returns all stored files sorted by path length (shortest first).
592
+ * `default`, `resolveOptions`, `resolvePath`, `resolveFile`, `resolveBanner`, and `resolveFooter`
593
+ * are injected automatically by `defineResolver` extend this type to add custom resolution methods.
386
594
  *
387
595
  * @example
388
596
  * ```ts
389
- * import { FileManager } from '@kubb/core'
597
+ * type MyResolver = Resolver & {
598
+ * resolveName(node: SchemaNode): string
599
+ * resolveTypedName(node: SchemaNode): string
600
+ * }
601
+ * ```
602
+ */
603
+ type Resolver = {
604
+ name: string;
605
+ pluginName: string;
606
+ default(name: string, type?: 'file' | 'function' | 'type' | 'const'): string;
607
+ resolveOptions<TOptions>(node: Node, context: ResolveOptionsContext<TOptions>): TOptions | null;
608
+ resolvePath(params: ResolverPathParams, context: ResolverContext): string;
609
+ resolveFile(params: ResolverFileParams, context: ResolverContext): FileNode;
610
+ resolveBanner(meta: InputMeta | undefined, context: ResolveBannerContext): string | null;
611
+ resolveFooter(meta: InputMeta | undefined, context: ResolveBannerContext): string | null;
612
+ };
613
+ /**
614
+ * File-specific parameters for `Resolver.resolvePath`.
390
615
  *
391
- * const manager = new FileManager()
392
- * manager.upsert(myFile)
393
- * console.log(manager.files) // all stored files
616
+ * Pass alongside a `ResolverContext` to identify which file to resolve.
617
+ * Provide `tag` for tag-based grouping or `path` for path-based grouping.
618
+ *
619
+ * @example
620
+ * ```ts
621
+ * resolver.resolvePath(
622
+ * { baseName: 'petTypes.ts', tag: 'pets' },
623
+ * { root: '/src', output: { path: 'types' }, group: { type: 'tag' } },
624
+ * )
625
+ * // → '/src/types/petsController/petTypes.ts'
394
626
  * ```
395
627
  */
396
- declare class FileManager {
397
- #private;
628
+ type ResolverPathParams = {
629
+ baseName: FileNode['baseName'];
630
+ pathMode?: 'single' | 'split';
398
631
  /**
399
- * Adds one or more files. Incoming files with the same path are merged
400
- * (sources/imports/exports concatenated), but existing cache entries are
401
- * replaced — use {@link upsert} when you want to merge into the cache too.
632
+ * Tag value used when `group.type === 'tag'`.
402
633
  */
403
- add(...files: Array<FileNode>): Array<FileNode>;
634
+ tag?: string;
404
635
  /**
405
- * Adds or merges one or more files.
406
- * If a file with the same path already exists in the cache, its
407
- * sources/imports/exports are merged into the incoming file.
636
+ * Path value used when `group.type === 'path'`.
408
637
  */
409
- upsert(...files: Array<FileNode>): Array<FileNode>;
410
- getByPath(path: string): FileNode | null;
411
- deleteByPath(path: string): void;
412
- clear(): void;
638
+ path?: string;
639
+ };
640
+ /**
641
+ * Shared context passed as the second argument to `Resolver.resolvePath` and `Resolver.resolveFile`.
642
+ *
643
+ * Describes where on disk output is rooted, which output config is active, and the optional
644
+ * grouping strategy that controls subdirectory layout.
645
+ *
646
+ * @example
647
+ * ```ts
648
+ * const context: ResolverContext = {
649
+ * root: config.root,
650
+ * output,
651
+ * group,
652
+ * }
653
+ * ```
654
+ */
655
+ type ResolverContext = {
656
+ root: string;
657
+ output: Output;
658
+ group?: Group;
413
659
  /**
414
- * All stored files, sorted by path length (shorter paths first).
660
+ * Plugin name used to populate `meta.pluginName` on the resolved file.
415
661
  */
416
- get files(): Array<FileNode>;
417
- }
418
- //#endregion
419
- //#region src/PluginDriver.d.ts
420
- type Options = {
421
- hooks: AsyncEventEmitter<KubbHooks>;
662
+ pluginName?: string;
422
663
  };
423
- declare class PluginDriver {
424
- #private;
425
- readonly config: Config;
426
- readonly options: Options;
664
+ /**
665
+ * File-specific parameters for `Resolver.resolveFile`.
666
+ *
667
+ * Pass alongside a `ResolverContext` to fully describe the file to resolve.
668
+ * `tag` and `path` are used only when a matching `group` is present in the context.
669
+ *
670
+ * @example
671
+ * ```ts
672
+ * resolver.resolveFile(
673
+ * { name: 'listPets', extname: '.ts', tag: 'pets' },
674
+ * { root: '/src', output: { path: 'types' }, group: { type: 'tag' } },
675
+ * )
676
+ * // → { baseName: 'listPets.ts', path: '/src/types/petsController/listPets.ts', ... }
677
+ * ```
678
+ */
679
+ type ResolverFileParams = {
680
+ name: string;
681
+ extname: FileNode['extname'];
427
682
  /**
428
- * Returns `'single'` when `fileOrFolder` has a file extension, `'split'` otherwise.
429
- *
430
- * @example
431
- * ```ts
432
- * PluginDriver.getMode('src/gen/types.ts') // 'single'
433
- * PluginDriver.getMode('src/gen/types') // 'split'
434
- * ```
683
+ * Tag value used when `group.type === 'tag'`.
435
684
  */
436
- static getMode(fileOrFolder: string | undefined | null): 'single' | 'split';
685
+ tag?: string;
437
686
  /**
438
- * The universal `@kubb/ast` `InputNode` produced by the adapter, set by
439
- * the build pipeline after the adapter's `parse()` resolves.
687
+ * Path value used when `group.type === 'path'`.
440
688
  */
441
- inputNode: InputNode | undefined;
442
- adapter: Adapter | undefined;
689
+ path?: string;
690
+ };
691
+ /**
692
+ * Context passed to `Resolver.resolveBanner` and `Resolver.resolveFooter`.
693
+ *
694
+ * `output` is optional — not every plugin configures a banner/footer.
695
+ * `config` carries the global Kubb config, used to derive the default Kubb banner.
696
+ *
697
+ * @example
698
+ * ```ts
699
+ * resolver.resolveBanner(meta, { output: { banner: '// generated' }, config })
700
+ * // → '// generated'
701
+ * ```
702
+ */
703
+ type ResolveBannerContext = {
704
+ output?: Pick<Output, 'banner' | 'footer'>;
705
+ config: Config;
706
+ };
707
+ /**
708
+ * Builder type for the plugin-specific resolver fields.
709
+ *
710
+ * `default`, `resolveOptions`, `resolvePath`, `resolveFile`, `resolveBanner`, and `resolveFooter`
711
+ * are optional — built-in fallbacks are injected when omitted.
712
+ *
713
+ * Methods in the returned object can call sibling resolver methods via `this`.
714
+ */
715
+ type ResolverBuilder<T extends PluginFactoryOptions> = () => Omit<T['resolver'], 'default' | 'resolveOptions' | 'resolvePath' | 'resolveFile' | 'resolveBanner' | 'resolveFooter' | 'name' | 'pluginName'> & Partial<Pick<T['resolver'], 'default' | 'resolveOptions' | 'resolvePath' | 'resolveFile' | 'resolveBanner' | 'resolveFooter'>> & {
716
+ name: string;
717
+ pluginName: T['name'];
718
+ } & ThisType<T['resolver']>;
719
+ /**
720
+ * Defines a resolver for a plugin, injecting built-in defaults for name casing,
721
+ * include/exclude/override filtering, path resolution, and file construction.
722
+ *
723
+ * All four defaults can be overridden by providing them in the builder function:
724
+ * - `default` — name casing strategy (camelCase / PascalCase)
725
+ * - `resolveOptions` — include/exclude/override filtering
726
+ * - `resolvePath` — output path computation
727
+ * - `resolveFile` — full `FileNode` construction
728
+ *
729
+ * Methods in the returned object can call sibling resolver methods via `this`.
730
+ *
731
+ * @example Basic resolver with naming helpers
732
+ * ```ts
733
+ * export const resolver = defineResolver<PluginTs>(() => ({
734
+ * name: 'default',
735
+ * resolveName(node) {
736
+ * return this.default(node.name, 'function')
737
+ * },
738
+ * resolveTypedName(node) {
739
+ * return this.default(node.name, 'type')
740
+ * },
741
+ * }))
742
+ * ```
743
+ *
744
+ * @example Override resolvePath for a custom output structure
745
+ * ```ts
746
+ * export const resolver = defineResolver<PluginTs>(() => ({
747
+ * name: 'custom',
748
+ * resolvePath({ baseName }, { root, output }) {
749
+ * return path.resolve(root, output.path, 'generated', baseName)
750
+ * },
751
+ * }))
752
+ * ```
753
+ *
754
+ * @example Use this.default inside a helper
755
+ * ```ts
756
+ * export const resolver = defineResolver<PluginTs>(() => ({
757
+ * name: 'default',
758
+ * resolveParamName(node, param) {
759
+ * return this.default(`${node.operationId} ${param.in} ${param.name}`, 'type')
760
+ * },
761
+ * }))
762
+ * ```
763
+ */
764
+ declare function defineResolver<T extends PluginFactoryOptions>(build: ResolverBuilder<T>): T['resolver'];
765
+ //#endregion
766
+ //#region src/definePlugin.d.ts
767
+ /**
768
+ * Safely extracts a type from a registry, returning `{}` if the key doesn't exist.
769
+ * Enables optional interface augmentation for `Kubb.ConfigOptionsRegistry` and `Kubb.PluginOptionsRegistry`
770
+ * without requiring changes to core.
771
+ *
772
+ * @internal
773
+ */
774
+ type ExtractRegistryKey$1<T, K extends PropertyKey> = K extends keyof T ? T[K] : {};
775
+ /**
776
+ * Output configuration for generated files.
777
+ */
778
+ type Output<_TOptions = unknown> = {
443
779
  /**
444
- * Central file store for all generated files.
445
- * Plugins should use `this.addFile()` / `this.upsertFile()` (via their context) to
446
- * add files; this property gives direct read/write access when needed.
780
+ * Output folder or file path for generated code.
447
781
  */
448
- readonly fileManager: FileManager;
449
- readonly plugins: Map<string, NormalizedPlugin>;
450
- constructor(config: Config, options: Options);
451
- get hooks(): AsyncEventEmitter<KubbHooks>;
782
+ path: string;
452
783
  /**
453
- * Registers a hook-style plugin's lifecycle handlers on the shared `AsyncEventEmitter`.
454
- *
455
- * For `kubb:plugin:setup`, the registered listener wraps the globally emitted context with a
456
- * plugin-specific one so that `addGenerator`, `setResolver`, `setTransformer`, and
457
- * `setRenderer` all target the correct `normalizedPlugin` entry in the plugins map.
458
- *
459
- * All other hooks are iterated and registered directly as pass-through listeners.
460
- * Any event key present in the global `KubbHooks` interface can be subscribed to.
461
- *
462
- * External tooling can subscribe to any of these events via `hooks.on(...)` to observe
463
- * the plugin lifecycle without modifying plugin behavior.
464
- *
465
- * @internal
784
+ * Text or function prepended to every generated file.
785
+ * When a function, receives the document metadata and returns a string.
466
786
  */
467
- registerPluginHooks(hookPlugin: Plugin, normalizedPlugin: NormalizedPlugin): void;
787
+ banner?: string | ((meta?: InputMeta) => string);
468
788
  /**
469
- * Emits the `kubb:plugin:setup` event so that all registered hook-style plugin listeners
470
- * can configure generators, resolvers, transformers and renderers before `buildStart` runs.
471
- *
472
- * Call this once from `safeBuild` before the plugin execution loop begins.
789
+ * Text or function appended to every generated file.
790
+ * When a function, receives the document metadata and returns a string.
473
791
  */
474
- emitSetupHooks(): Promise<void>;
792
+ footer?: string | ((meta?: InputMeta) => string);
475
793
  /**
476
- * Registers a generator for the given plugin on the shared event emitter.
477
- *
478
- * The generator's `schema`, `operation`, and `operations` methods are registered as
479
- * listeners on `kubb:generate:schema`, `kubb:generate:operation`, and `kubb:generate:operations`
480
- * respectively. Each listener is scoped to the owning plugin via a `ctx.plugin.name` check
481
- * so that generators from different plugins do not cross-fire.
482
- *
483
- * The renderer resolution chain is: `generator.renderer → plugin.renderer → config.renderer`.
484
- * Set `generator.renderer = null` to explicitly opt out of rendering even when the plugin
485
- * declares a renderer.
486
- *
487
- * Call this method inside `addGenerator()` (in `kubb:plugin:setup`) to wire up a generator.
794
+ * Whether to override existing external files if they already exist.
795
+ * @default false
488
796
  */
489
- registerGenerator(pluginName: string, gen: Generator): void;
797
+ override?: boolean;
798
+ } & ExtractRegistryKey$1<Kubb.PluginOptionsRegistry, 'output'>;
799
+ type Group = {
490
800
  /**
491
- * Returns `true` when at least one generator was registered for the given plugin
492
- * via `addGenerator()` in `kubb:plugin:setup` (event-based path).
493
- *
494
- * Used by the build loop to decide whether to walk the AST and emit generator events
495
- * for a plugin that has no static `plugin.generators`.
801
+ * How to group files into subdirectories.
802
+ * - `'tag'` group by OpenAPI tags
803
+ * - `'path'` — group by OpenAPI paths
496
804
  */
497
- hasRegisteredGenerators(pluginName: string): boolean;
805
+ type: 'tag' | 'path';
498
806
  /**
499
- * Unregisters all plugin lifecycle listeners from the shared event emitter.
500
- * Called at the end of a build to prevent listener leaks across repeated builds.
501
- *
502
- * @internal
807
+ * Function that returns the subdirectory name for a group value.
808
+ * Defaults to `${camelCase(group)}Controller` for tags, first path segment for paths.
503
809
  */
504
- dispose(): void;
810
+ name?: (context: {
811
+ group: string;
812
+ }) => string;
813
+ };
814
+ type ByTag = {
505
815
  /**
506
- * Merges `partial` with the plugin's default resolver and stores the result.
507
- * Also mirrors it onto `plugin.resolver` so callers using `getPlugin(name).resolver`
508
- * get the up-to-date resolver without going through `getResolver()`.
816
+ * Filter by OpenAPI `tags` field. Matches one or more tags assigned to operations.
509
817
  */
510
- setPluginResolver(pluginName: string, partial: Partial<Resolver>): void;
818
+ type: 'tag';
511
819
  /**
512
- * Returns the resolver for the given plugin.
513
- *
514
- * Resolution order: dynamic resolver set via `setPluginResolver` → static resolver on the
515
- * plugin → lazily created default resolver (identity name, no path transforms).
820
+ * Tag name to match (case-sensitive). Can be a literal string or regex pattern.
516
821
  */
517
- getResolver<TName extends keyof Kubb.PluginRegistry>(pluginName: TName): Kubb.PluginRegistry[TName]['resolver'];
518
- getResolver<TResolver extends Resolver = Resolver>(pluginName: string): TResolver;
519
- getContext<TOptions extends PluginFactoryOptions>(plugin: NormalizedPlugin<TOptions>): GeneratorContext<TOptions> & Record<string, unknown>;
520
- getPlugin<TName extends keyof Kubb.PluginRegistry>(pluginName: TName): Plugin<Kubb.PluginRegistry[TName]> | undefined;
521
- getPlugin<TOptions extends PluginFactoryOptions = PluginFactoryOptions>(pluginName: string): Plugin<TOptions> | undefined;
822
+ pattern: string | RegExp;
823
+ };
824
+ type ByOperationId = {
522
825
  /**
523
- * Like `getPlugin` but throws a descriptive error when the plugin is not found.
826
+ * Filter by OpenAPI `operationId` field. Each operation (GET, POST, etc.) has a unique identifier.
524
827
  */
525
- requirePlugin<TName extends keyof Kubb.PluginRegistry>(pluginName: TName): Plugin<Kubb.PluginRegistry[TName]>;
526
- requirePlugin<TOptions extends PluginFactoryOptions = PluginFactoryOptions>(pluginName: string): Plugin<TOptions>;
527
- }
528
- //#endregion
529
- //#region src/createKubb.d.ts
530
- /**
531
- * Full output produced by a successful or failed build.
532
- */
533
- type BuildOutput = {
828
+ type: 'operationId';
534
829
  /**
535
- * Plugins that threw during installation, paired with the caught error.
830
+ * Operation ID to match (case-sensitive). Can be a literal string or regex pattern.
536
831
  */
537
- failedPlugins: Set<{
538
- plugin: Plugin;
539
- error: Error;
540
- }>;
541
- files: Array<FileNode>;
542
- driver: PluginDriver;
832
+ pattern: string | RegExp;
833
+ };
834
+ type ByPath = {
543
835
  /**
544
- * Elapsed time in milliseconds for each plugin, keyed by plugin name.
836
+ * Filter by OpenAPI `path` (URL endpoint). Useful to group or filter by service segments like `/pets`, `/users`, etc.
545
837
  */
546
- pluginTimings: Map<string, number>;
547
- error?: Error;
838
+ type: 'path';
548
839
  /**
549
- * Raw generated source, keyed by absolute file path.
840
+ * URL path to match (case-sensitive). Can be a literal string or regex pattern. Matches against the full path.
550
841
  */
551
- sources: Map<string, string>;
552
- };
553
- type CreateKubbOptions = {
554
- hooks?: AsyncEventEmitter<KubbHooks>;
842
+ pattern: string | RegExp;
555
843
  };
556
- /**
557
- * Creates a Kubb instance bound to a single config entry.
558
- *
559
- * Accepts a user-facing config shape and resolves it to a full {@link Config} during
560
- * `setup()`. The instance then holds shared state (`hooks`, `sources`, `driver`, `config`)
561
- * across the `setup → build` lifecycle. Attach event listeners to `kubb.hooks` before
562
- * calling `setup()` or `build()`.
563
- *
564
- * @example
565
- * ```ts
566
- * const kubb = createKubb(userConfig)
567
- *
568
- * kubb.hooks.on('kubb:plugin:end', ({ plugin, duration }) => {
569
- * console.log(`${plugin.name} completed in ${duration}ms`)
570
- * })
571
- *
572
- * const { files, failedPlugins } = await kubb.safeBuild()
573
- * ```
574
- */
575
- declare function createKubb(userConfig: UserConfig, options?: CreateKubbOptions): Kubb$1;
576
- //#endregion
577
- //#region src/Kubb.d.ts
578
- /**
579
- * Kubb code generation instance returned by {@link createKubb}.
580
- *
581
- * Use this when orchestrating multiple builds, inspecting plugin timings, or integrating Kubb into a larger toolchain.
582
- * For a single one-off build, chain directly: `await createKubb(config).build()`.
583
- */
584
- type Kubb$1 = {
585
- /**
586
- * Shared event emitter for lifecycle and status events. Attach listeners before calling `setup()` or `build()`.
587
- */
588
- readonly hooks: AsyncEventEmitter<KubbHooks>;
844
+ type ByMethod = {
589
845
  /**
590
- * Generated source code keyed by absolute file path. Available after `build()` or `safeBuild()` completes.
846
+ * Filter by HTTP method: `'get'`, `'post'`, `'put'`, `'delete'`, `'patch'`, `'head'`, `'options'`.
591
847
  */
592
- readonly sources: Map<string, string>;
848
+ type: 'method';
593
849
  /**
594
- * Plugin driver managing all plugins. Available after `setup()` completes.
850
+ * HTTP method to match (case-insensitive when using string, or regex for dynamic matching).
595
851
  */
596
- readonly driver: PluginDriver | undefined;
852
+ pattern: HttpMethod | RegExp;
853
+ };
854
+ type BySchemaName = {
597
855
  /**
598
- * Resolved configuration with defaults applied. Available after `setup()` completes.
856
+ * Filter by schema component name (TypeScript or JSON schema). Matches schemas in `#/components/schemas`.
599
857
  */
600
- readonly config: Config | undefined;
858
+ type: 'schemaName';
601
859
  /**
602
- * Resolves config and initializes the driver. `build()` calls this automatically.
860
+ * Schema name to match (case-sensitive). Can be a literal string or regex pattern.
603
861
  */
604
- setup(): Promise<void>;
862
+ pattern: string | RegExp;
863
+ };
864
+ type ByContentType = {
605
865
  /**
606
- * Runs the full pipeline and throws on any plugin error. Automatically calls `setup()` if needed.
866
+ * Filter by response or request content type: `'application/json'`, `'application/xml'`, etc.
607
867
  */
608
- build(): Promise<BuildOutput>;
868
+ type: 'contentType';
609
869
  /**
610
- * Runs the full pipeline and captures errors in `BuildOutput` instead of throwing. Automatically calls `setup()` if needed.
870
+ * Content type to match (case-sensitive). Can be a literal string or regex pattern.
611
871
  */
612
- safeBuild(): Promise<BuildOutput>;
872
+ pattern: string | RegExp;
613
873
  };
614
874
  /**
615
- * Lifecycle events emitted during Kubb code generation.
616
- * Use these for logging, progress tracking, and custom integrations.
875
+ * A pattern filter that prevents matching nodes from being generated.
876
+ *
877
+ * Use to skip code generation for specific operations or schemas. For example, exclude deprecated endpoints
878
+ * or internal-only schemas. Can filter by tag, operationId, path, HTTP method, content type, or schema name.
617
879
  *
618
880
  * @example
619
- * ```typescript
620
- * import type { AsyncEventEmitter } from '@internals/utils'
621
- * import type { KubbHooks } from '@kubb/core'
881
+ * ```ts
882
+ * exclude: [
883
+ * { type: 'tag', pattern: 'internal' }, // skip "internal" tag
884
+ * { type: 'path', pattern: /^\/admin/ }, // skip all /admin endpoints
885
+ * { type: 'operationId', pattern: 'deprecated_*' } // skip operationIds matching pattern
886
+ * ]
887
+ * ```
888
+ */
889
+ type Exclude = ByTag | ByOperationId | ByPath | ByMethod | ByContentType | BySchemaName;
890
+ /**
891
+ * A pattern filter that restricts generation to only matching nodes.
622
892
  *
623
- * const hooks: AsyncEventEmitter<KubbHooks> = new AsyncEventEmitter()
893
+ * Use to generate code for a subset of operations or schemas. For example, only generate for a specific service
894
+ * tag or only for "production" endpoints. Can filter by tag, operationId, path, HTTP method, content type, or schema name.
624
895
  *
625
- * hooks.on('kubb:lifecycle:start', () => {
626
- * console.log('Starting Kubb generation')
627
- * })
896
+ * @example
897
+ * ```ts
898
+ * include: [
899
+ * { type: 'tag', pattern: 'public' }, // generate only "public" tag
900
+ * { type: 'path', pattern: /^\/api\/v1/ }, // generate only v1 endpoints
901
+ * ]
902
+ * ```
903
+ */
904
+ type Include = ByTag | ByOperationId | ByPath | ByMethod | ByContentType | BySchemaName;
905
+ /**
906
+ * A pattern filter paired with partial option overrides applied when the pattern matches.
628
907
  *
629
- * hooks.on('kubb:plugin:end', ({ plugin, duration }) => {
630
- * console.log(`Plugin ${plugin.name} completed in ${duration}ms`)
631
- * })
908
+ * Use to customize generation for specific operations or schemas. For example, apply different output paths
909
+ * for different tags, or use custom resolver functions per operation. Can filter by tag, operationId, path,
910
+ * HTTP method, schema name, or content type.
911
+ *
912
+ * @example
913
+ * ```ts
914
+ * override: [
915
+ * {
916
+ * type: 'tag',
917
+ * pattern: 'admin',
918
+ * options: { output: { path: './src/gen/admin' } } // admin APIs go to separate folder
919
+ * },
920
+ * {
921
+ * type: 'operationId',
922
+ * pattern: 'listPets',
923
+ * options: { exclude: true } // skip this specific operation
924
+ * }
925
+ * ]
632
926
  * ```
633
927
  */
634
- interface KubbHooks {
928
+ type Override<TOptions> = (ByTag | ByOperationId | ByPath | ByMethod | BySchemaName | ByContentType) & {
929
+ options: Partial<TOptions>;
930
+ };
931
+ type PluginFactoryOptions<
932
+ /**
933
+ * Unique plugin name.
934
+ */
935
+ TName extends string = string,
936
+ /**
937
+ * User-facing plugin options.
938
+ */
939
+ TOptions extends object = object,
940
+ /**
941
+ * Plugin options after defaults are applied.
942
+ */
943
+ TResolvedOptions extends object = TOptions,
944
+ /**
945
+ * Resolver that encapsulates naming and path-resolution helpers.
946
+ * Define with `defineResolver` and export alongside the plugin.
947
+ */
948
+ TResolver extends Resolver = Resolver> = {
949
+ name: TName;
950
+ options: TOptions;
951
+ resolvedOptions: TResolvedOptions;
952
+ resolver: TResolver;
953
+ };
954
+ /**
955
+ * Context for hook-style plugin `kubb:plugin:setup` handler.
956
+ * Provides methods to register generators, configure resolvers, transformers, and renderers.
957
+ */
958
+ type KubbPluginSetupContext<TFactory extends PluginFactoryOptions = PluginFactoryOptions> = {
635
959
  /**
636
- * Fires at the start of the Kubb lifecycle, before code generation begins.
960
+ * Register a generator dynamically. Generators fire during the AST walk (schema/operation/operations)
961
+ * just like generators declared statically on `createPlugin`.
637
962
  */
638
- 'kubb:lifecycle:start': [ctx: KubbLifecycleStartContext];
963
+ addGenerator<TElement = unknown>(generator: Generator$1<TFactory, TElement>): void;
639
964
  /**
640
- * Fires at the end of the Kubb lifecycle, after all code generation completes.
965
+ * Set or override the resolver for this plugin.
966
+ * The resolver controls file naming and path resolution.
641
967
  */
642
- 'kubb:lifecycle:end': [];
968
+ setResolver(resolver: Partial<TFactory['resolver']>): void;
643
969
  /**
644
- * Fires when configuration loading starts.
970
+ * Set the AST transformer to pre-process nodes before they reach generators.
645
971
  */
646
- 'kubb:config:start': [];
972
+ setTransformer(visitor: Visitor): void;
647
973
  /**
648
- * Fires when configuration loading completes.
974
+ * Set the renderer factory to process JSX elements from generators.
649
975
  */
650
- 'kubb:config:end': [ctx: KubbConfigEndContext];
976
+ setRenderer(renderer: RendererFactory): void;
651
977
  /**
652
- * Fires when code generation starts.
978
+ * Set resolved options merged into the normalized plugin's `options`.
979
+ * Call this in `kubb:plugin:setup` to provide options generators need.
653
980
  */
654
- 'kubb:generation:start': [ctx: KubbGenerationStartContext];
981
+ setOptions(options: TFactory['resolvedOptions']): void;
655
982
  /**
656
- * Fires when code generation completes.
983
+ * Inject a raw file into the build output, bypassing the generation pipeline.
657
984
  */
658
- 'kubb:generation:end': [ctx: KubbGenerationEndContext];
985
+ injectFile(userFileNode: UserFileNode): void;
659
986
  /**
660
- * Fires with a generation summary including summary lines, title, and success status.
987
+ * Merge a partial config update into the current build configuration.
661
988
  */
662
- 'kubb:generation:summary': [ctx: KubbGenerationSummaryContext];
989
+ updateConfig(config: Partial<Config>): void;
663
990
  /**
664
- * Fires when code formatting starts (e.g., Biome or Prettier).
991
+ * The resolved build configuration at setup time.
665
992
  */
666
- 'kubb:format:start': [];
993
+ config: Config;
667
994
  /**
668
- * Fires when code formatting completes.
995
+ * The plugin's user-provided options.
669
996
  */
670
- 'kubb:format:end': [];
997
+ options: TFactory['options'];
998
+ };
999
+ /**
1000
+ * A plugin object produced by `definePlugin`.
1001
+ * Instead of flat lifecycle methods, it groups all handlers under a `hooks:` property
1002
+ * (matching Astro's integration naming convention).
1003
+ *
1004
+ * @template TFactory - The plugin's `PluginFactoryOptions` type.
1005
+ */
1006
+ type Plugin<TFactory extends PluginFactoryOptions = PluginFactoryOptions> = {
671
1007
  /**
672
- * Fires when linting starts.
1008
+ * Unique name for the plugin, following the same naming convention as `createPlugin`.
673
1009
  */
674
- 'kubb:lint:start': [];
1010
+ name: string;
675
1011
  /**
676
- * Fires when linting completes.
1012
+ * Plugins that must be registered before this plugin executes.
1013
+ * An error is thrown at startup when any listed dependency is missing.
677
1014
  */
678
- 'kubb:lint:end': [];
1015
+ dependencies?: Array<string>;
679
1016
  /**
680
- * Fires when plugin hooks execution starts.
1017
+ * Controls the execution order of this plugin relative to others.
1018
+ *
1019
+ * - `'pre'` — runs before all normal plugins.
1020
+ * - `'post'` — runs after all normal plugins.
1021
+ * - `undefined` (default) — runs in declaration order among normal plugins.
1022
+ *
1023
+ * Dependency constraints always take precedence over `enforce`.
681
1024
  */
682
- 'kubb:hooks:start': [];
1025
+ enforce?: 'pre' | 'post';
683
1026
  /**
684
- * Fires when plugin hooks execution completes.
1027
+ * The options passed by the user when calling the plugin factory.
685
1028
  */
686
- 'kubb:hooks:end': [];
1029
+ options?: TFactory['options'];
687
1030
  /**
688
- * Fires when a single hook executes (e.g., format or lint). The callback is invoked when the command finishes.
1031
+ * Lifecycle event handlers for this plugin.
1032
+ * Any event from the global `KubbHooks` map can be subscribed to here.
689
1033
  */
690
- 'kubb:hook:start': [ctx: KubbHookStartContext];
1034
+ hooks: { [K in keyof KubbHooks as K extends 'kubb:plugin:setup' ? never : K]?: (...args: KubbHooks[K]) => void | Promise<void> } & {
1035
+ 'kubb:plugin:setup'?(ctx: KubbPluginSetupContext<TFactory>): void | Promise<void>;
1036
+ };
1037
+ };
1038
+ /**
1039
+ * Normalized plugin after setup, with runtime fields populated.
1040
+ * For internal use only — plugins use the public `Plugin` type externally.
1041
+ *
1042
+ * @internal
1043
+ */
1044
+ type NormalizedPlugin<TOptions extends PluginFactoryOptions = PluginFactoryOptions> = Plugin<TOptions> & {
1045
+ options: TOptions['resolvedOptions'] & {
1046
+ output: Output;
1047
+ include?: Array<Include>;
1048
+ exclude: Array<Exclude>;
1049
+ override: Array<Override<TOptions['resolvedOptions']>>;
1050
+ };
1051
+ resolver: TOptions['resolver'];
1052
+ transformer?: Visitor;
1053
+ renderer?: RendererFactory;
1054
+ generators?: Array<Generator$1>;
1055
+ apply?: (config: Config) => boolean;
1056
+ version?: string;
1057
+ };
1058
+ type KubbPluginStartContext = {
1059
+ plugin: NormalizedPlugin;
1060
+ };
1061
+ type KubbPluginEndContext = {
1062
+ plugin: NormalizedPlugin;
1063
+ duration: number;
1064
+ success: boolean;
1065
+ error?: Error;
1066
+ config: Config;
691
1067
  /**
692
- * Fires when a single hook execution completes.
1068
+ * Returns all files currently in the file manager (lazy snapshot).
1069
+ * Includes files added by plugins that have already run.
693
1070
  */
694
- 'kubb:hook:end': [ctx: KubbHookEndContext];
1071
+ readonly files: ReadonlyArray<FileNode>;
695
1072
  /**
696
- * Fires when a new Kubb version is available.
1073
+ * Upsert one or more files into the file manager.
697
1074
  */
698
- 'kubb:version:new': [ctx: KubbVersionNewContext];
1075
+ upsertFile: (...files: Array<FileNode>) => void;
1076
+ };
1077
+ /**
1078
+ * Wraps a factory function and returns a typed `Plugin` with lifecycle handlers grouped under `hooks`.
1079
+ *
1080
+ * Handlers live in a single `hooks` object (inspired by Astro integrations).
1081
+ * All lifecycle events from `KubbHooks` are available for subscription.
1082
+ *
1083
+ * @note For real plugins, use a `PluginFactoryOptions` type parameter to get type-safe context in `kubb:plugin:setup`.
1084
+ * Plugin names should follow the convention `plugin-<feature>` (e.g., `plugin-react-query`, `plugin-zod`).
1085
+ *
1086
+ * @example
1087
+ * ```ts
1088
+ * import { definePlugin } from '@kubb/core'
1089
+ *
1090
+ * export const pluginTs = definePlugin((options: { prefix?: string } = {}) => ({
1091
+ * name: 'plugin-ts',
1092
+ * hooks: {
1093
+ * 'kubb:plugin:setup'(ctx) {
1094
+ * ctx.setResolver(resolverTs)
1095
+ * },
1096
+ * },
1097
+ * }))
1098
+ * ```
1099
+ */
1100
+ declare function definePlugin<TFactory extends PluginFactoryOptions = PluginFactoryOptions>(factory: (options: TFactory['options']) => Plugin<TFactory>): (options?: TFactory['options']) => Plugin<TFactory>;
1101
+ //#endregion
1102
+ //#region src/FileManager.d.ts
1103
+ /**
1104
+ * In-memory file store for generated files. Files sharing a `path` are merged
1105
+ * (sources/imports/exports concatenated). The `files` getter is sorted by
1106
+ * path length (barrel `index.ts` last within a bucket).
1107
+ *
1108
+ * @example
1109
+ * ```ts
1110
+ * const manager = new FileManager()
1111
+ * manager.upsert(myFile)
1112
+ * manager.files // sorted view
1113
+ * ```
1114
+ */
1115
+ declare class FileManager {
1116
+ #private;
699
1117
  /**
700
- * Informational message event.
1118
+ * Registers a callback invoked with the resolved {@link FileNode} on every
1119
+ * `add` / `upsert`. Used by the build loop to track newly written files
1120
+ * without keeping its own scan-based diff. Single subscriber by design —
1121
+ * setting again replaces the previous callback. Pass `null` to detach.
701
1122
  */
702
- 'kubb:info': [ctx: KubbInfoContext];
1123
+ setOnUpsert(callback: ((file: FileNode) => void) | null): void;
1124
+ add(...files: Array<FileNode>): Array<FileNode>;
1125
+ upsert(...files: Array<FileNode>): Array<FileNode>;
1126
+ getByPath(path: string): FileNode | null;
1127
+ deleteByPath(path: string): void;
1128
+ clear(): void;
703
1129
  /**
704
- * Error event, fired when an error occurs during generation.
1130
+ * Releases all stored files. Called by the core after `kubb:build:end`.
705
1131
  */
706
- 'kubb:error': [ctx: KubbErrorContext];
1132
+ dispose(): void;
1133
+ [Symbol.dispose](): void;
707
1134
  /**
708
- * Success message event.
1135
+ * All stored files in stable sort order (shortest path first, barrel files
1136
+ * last within a length bucket). Returns a cached view — do not mutate.
709
1137
  */
710
- 'kubb:success': [ctx: KubbSuccessContext];
1138
+ get files(): Array<FileNode>;
1139
+ }
1140
+ //#endregion
1141
+ //#region src/KubbDriver.d.ts
1142
+ type Options = {
1143
+ hooks: AsyncEventEmitter<KubbHooks>;
1144
+ };
1145
+ declare class KubbDriver {
1146
+ #private;
1147
+ readonly config: Config;
1148
+ readonly options: Options;
711
1149
  /**
712
- * Warning message event.
1150
+ * Returns `'single'` when `fileOrFolder` has a file extension, `'split'` otherwise.
1151
+ *
1152
+ * @example
1153
+ * ```ts
1154
+ * KubbDriver.getMode('src/gen/types.ts') // 'single'
1155
+ * KubbDriver.getMode('src/gen/types') // 'split'
1156
+ * ```
713
1157
  */
714
- 'kubb:warn': [ctx: KubbWarnContext];
1158
+ static getMode(fileOrFolder: string | undefined | null): 'single' | 'split';
715
1159
  /**
716
- * Debug event for detailed logging with timestamp and optional filename.
1160
+ * The streaming `InputStreamNode` produced by the adapter.
1161
+ * Always set after adapter setup — parse-only adapters are wrapped automatically.
717
1162
  */
718
- 'kubb:debug': [ctx: KubbDebugContext];
1163
+ inputNode: InputStreamNode | null;
1164
+ adapter: Adapter | null;
719
1165
  /**
720
- * Fires when file processing starts with the list of files to process.
1166
+ * Central file store for all generated files.
1167
+ * Plugins should use `this.addFile()` / `this.upsertFile()` (via their context) to
1168
+ * add files; this property gives direct read/write access when needed.
721
1169
  */
722
- 'kubb:files:processing:start': [ctx: KubbFilesProcessingStartContext];
1170
+ readonly fileManager: FileManager;
1171
+ readonly plugins: Map<string, NormalizedPlugin>;
1172
+ constructor(config: Config, options: Options);
1173
+ setup(): Promise<void>;
1174
+ get hooks(): AsyncEventEmitter<KubbHooks>;
723
1175
  /**
724
- * Fires for each file with progress updates: processed count, total, percentage, and file details.
1176
+ * Emits the `kubb:plugin:setup` event so that all registered hook-style plugin listeners
1177
+ * can configure generators, resolvers, transformers and renderers before `buildStart` runs.
1178
+ *
1179
+ * Call this once from `safeBuild` before the plugin execution loop begins.
725
1180
  */
726
- 'kubb:file:processing:update': [ctx: KubbFileProcessingUpdateContext];
1181
+ emitSetupHooks(): Promise<void>;
727
1182
  /**
728
- * Fires when file processing completes with the list of processed files.
1183
+ * Registers a generator for the given plugin on the shared event emitter.
1184
+ *
1185
+ * The generator's `schema`, `operation`, and `operations` methods are registered as
1186
+ * listeners on `kubb:generate:schema`, `kubb:generate:operation`, and `kubb:generate:operations`
1187
+ * respectively. Each listener is scoped to the owning plugin via a `ctx.plugin.name` check
1188
+ * so that generators from different plugins do not cross-fire.
1189
+ *
1190
+ * The renderer resolution chain is: `generator.renderer → plugin.renderer → config.renderer`.
1191
+ * Set `generator.renderer = null` to explicitly opt out of rendering even when the plugin
1192
+ * declares a renderer.
1193
+ *
1194
+ * Call this method inside `addGenerator()` (in `kubb:plugin:setup`) to wire up a generator.
729
1195
  */
730
- 'kubb:files:processing:end': [ctx: KubbFilesProcessingEndContext];
1196
+ registerGenerator(pluginName: string, gen: Generator$1): void;
731
1197
  /**
732
- * Fires when a plugin starts execution.
1198
+ * Returns `true` when at least one generator was registered for the given plugin
1199
+ * via `addGenerator()` in `kubb:plugin:setup` (event-based path).
1200
+ *
1201
+ * Used by the build loop to decide whether to walk the AST and emit generator events
1202
+ * for a plugin that has no static `plugin.generators`.
733
1203
  */
734
- 'kubb:plugin:start': [ctx: KubbPluginStartContext];
1204
+ hasEventGenerators(pluginName: string): boolean;
1205
+ /**
1206
+ * Runs the full plugin pipeline. Returns timings/failures collected so far even
1207
+ * when an outer hook throws — the orchestrator preserves partial state by capturing
1208
+ * the error into `error` instead of propagating.
1209
+ */
1210
+ run({
1211
+ storage
1212
+ }: {
1213
+ storage: Storage;
1214
+ }): Promise<{
1215
+ failedPlugins: Set<{
1216
+ plugin: Plugin;
1217
+ error: Error;
1218
+ }>;
1219
+ pluginTimings: Map<string, number>;
1220
+ error?: Error;
1221
+ }>;
735
1222
  /**
736
- * Fires when a plugin completes execution. Duration measured in milliseconds.
1223
+ * Unregisters all plugin lifecycle listeners from the shared event emitter.
1224
+ * Called at the end of a build to prevent listener leaks across repeated builds.
1225
+ *
1226
+ * @internal
737
1227
  */
738
- 'kubb:plugin:end': [ctx: KubbPluginEndContext];
1228
+ dispose(): void;
1229
+ [Symbol.dispose](): void;
739
1230
  /**
740
- * Fires once before plugins execute allowing plugins to register generators, configure resolvers/transformers/renderers, or inject files.
1231
+ * Merges `partial` with the plugin's default resolver and stores the result.
1232
+ * Also mirrors it onto `plugin.resolver` so callers using `getPlugin(name).resolver`
1233
+ * get the up-to-date resolver without going through `getResolver()`.
741
1234
  */
742
- 'kubb:plugin:setup': [ctx: KubbPluginSetupContext];
1235
+ setPluginResolver(pluginName: string, partial: Partial<Resolver>): void;
743
1236
  /**
744
- * Fires before the plugin execution loop begins. The adapter has already parsed the source and `inputNode` is available.
1237
+ * Returns the resolver for the given plugin.
1238
+ *
1239
+ * Resolution order: dynamic resolver set via `setPluginResolver` → static resolver on the
1240
+ * plugin → lazily created default resolver (identity name, no path transforms).
745
1241
  */
746
- 'kubb:build:start': [ctx: KubbBuildStartContext];
1242
+ getResolver<TName extends keyof Kubb.PluginRegistry>(pluginName: TName): Kubb.PluginRegistry[TName]['resolver'];
1243
+ getResolver<TResolver extends Resolver = Resolver>(pluginName: string): TResolver;
1244
+ getContext<TOptions extends PluginFactoryOptions>(plugin: NormalizedPlugin<TOptions>): GeneratorContext<TOptions> & Record<string, unknown>;
1245
+ getPlugin<TName extends keyof Kubb.PluginRegistry>(pluginName: TName): Plugin<Kubb.PluginRegistry[TName]> | undefined;
1246
+ getPlugin<TOptions extends PluginFactoryOptions = PluginFactoryOptions>(pluginName: string): Plugin<TOptions> | undefined;
747
1247
  /**
748
- * Fires after all plugins run and per-plugin barrels generate, but before files write to disk.
749
- * Use this to inject final files that must persist in the same write pass as plugin output.
1248
+ * Like `getPlugin` but throws a descriptive error when the plugin is not found.
750
1249
  */
751
- 'kubb:plugins:end': [ctx: KubbPluginsEndContext];
1250
+ requirePlugin<TName extends keyof Kubb.PluginRegistry>(pluginName: TName): Plugin<Kubb.PluginRegistry[TName]>;
1251
+ requirePlugin<TOptions extends PluginFactoryOptions = PluginFactoryOptions>(pluginName: string): Plugin<TOptions>;
1252
+ }
1253
+ //#endregion
1254
+ //#region src/defineGenerator.d.ts
1255
+ /**
1256
+ * Context object passed to generator `schema`, `operation`, and `operations` methods.
1257
+ *
1258
+ * The adapter is always defined (guaranteed by `runPluginAstHooks`) so no runtime checks
1259
+ * are needed. `ctx.options` carries resolved per-node options after exclude/include/override
1260
+ * filtering for individual schema/operation calls, or plugin-level options for operations.
1261
+ */
1262
+ type GeneratorContext<TOptions extends PluginFactoryOptions = PluginFactoryOptions> = {
1263
+ config: Config;
752
1264
  /**
753
- * Fires after all files write to disk.
1265
+ * Absolute path to the current plugin's output directory.
754
1266
  */
755
- 'kubb:build:end': [ctx: KubbBuildEndContext];
1267
+ root: string;
756
1268
  /**
757
- * Fires for each schema node during AST traversal. Generator listeners respond to this.
1269
+ * Determine output mode based on the output config.
1270
+ * Returns `'single'` when `output.path` is a file, `'split'` for a directory.
758
1271
  */
759
- 'kubb:generate:schema': [node: SchemaNode, ctx: GeneratorContext];
1272
+ getMode: (output: {
1273
+ path: string;
1274
+ }) => 'single' | 'split';
1275
+ driver: KubbDriver;
760
1276
  /**
761
- * Fires for each operation node during AST traversal. Generator listeners respond to this.
1277
+ * Get a plugin by name, typed via `Kubb.PluginRegistry` when registered.
762
1278
  */
763
- 'kubb:generate:operation': [node: OperationNode, ctx: GeneratorContext];
1279
+ getPlugin<TName extends keyof Kubb.PluginRegistry>(name: TName): Plugin<Kubb.PluginRegistry[TName]> | undefined;
1280
+ getPlugin(name: string): Plugin | undefined;
764
1281
  /**
765
- * Fires once after all operations traverse with the full collected array. Batch generator listeners respond to this.
1282
+ * Get a plugin by name, throws an error if not found.
766
1283
  */
767
- 'kubb:generate:operations': [nodes: Array<OperationNode>, ctx: GeneratorContext];
768
- }
769
- declare global {
770
- namespace Kubb {
771
- /**
772
- * Registry that maps plugin names to their `PluginFactoryOptions`.
773
- * Augment this interface in each plugin's `types.ts` to enable automatic
774
- * typing for `getPlugin` and `requirePlugin`.
775
- *
776
- * @example
777
- * ```ts
778
- * // packages/plugin-ts/src/types.ts
779
- * declare global {
780
- * namespace Kubb {
781
- * interface PluginRegistry {
782
- * 'plugin-ts': PluginTs
783
- * }
784
- * }
785
- * }
786
- * ```
787
- */
788
- interface PluginRegistry {}
789
- /**
790
- * Extension point for root `Config['output']` options.
791
- * Augment the `output` key in middleware or plugin packages to add extra fields
792
- * to the global output configuration without touching core types.
793
- *
794
- * @example
795
- * ```ts
796
- * // packages/middleware-barrel/src/types.ts
797
- * declare global {
798
- * namespace Kubb {
799
- * interface ConfigOptionsRegistry {
800
- * output: {
801
- * barrel?: import('./types.ts').BarrelConfig | false
802
- * }
803
- * }
804
- * }
805
- * }
806
- * ```
807
- */
808
- interface ConfigOptionsRegistry {}
809
- /**
810
- * Extension point for per-plugin `Output` options.
811
- * Augment the `output` key in middleware or plugin packages to add extra fields
812
- * to the per-plugin output configuration without touching core types.
813
- *
814
- * @example
815
- * ```ts
816
- * // packages/middleware-barrel/src/types.ts
817
- * declare global {
818
- * namespace Kubb {
819
- * interface PluginOptionsRegistry {
820
- * output: {
821
- * barrel?: import('./types.ts').PluginBarrelConfig | false
822
- * }
823
- * }
824
- * }
825
- * }
826
- * ```
827
- */
828
- interface PluginOptionsRegistry {}
829
- }
830
- }
831
- //#endregion
832
- //#region src/defineMiddleware.d.ts
833
- /**
834
- * A middleware instance produced by calling a factory created with `defineMiddleware`.
835
- * It declares event handlers under a `hooks` object which are registered on the
836
- * shared emitter after all plugin hooks, so middleware handlers for any event
837
- * always fire last.
838
- */
839
- type Middleware = {
1284
+ requirePlugin<TName extends keyof Kubb.PluginRegistry>(name: TName): Plugin<Kubb.PluginRegistry[TName]>;
1285
+ requirePlugin(name: string): Plugin;
840
1286
  /**
841
- * Unique identifier for this middleware.
1287
+ * Get a resolver by plugin name, typed via `Kubb.PluginRegistry` when registered.
842
1288
  */
843
- name: string;
1289
+ getResolver<TName extends keyof Kubb.PluginRegistry>(name: TName): Kubb.PluginRegistry[TName]['resolver'];
1290
+ getResolver(name: string): Resolver;
844
1291
  /**
845
- * Lifecycle event handlers for this middleware.
846
- * Any event from the global `KubbHooks` map can be subscribed to here.
847
- * Handlers are registered after all plugin handlers, so they always fire last.
1292
+ * Add files only if they don't exist.
848
1293
  */
849
- hooks: { [K in keyof KubbHooks]?: (...args: KubbHooks[K]) => void | Promise<void> };
1294
+ addFile: (...file: Array<FileNode>) => Promise<void>;
1295
+ /**
1296
+ * Merge sources into the same output file.
1297
+ */
1298
+ upsertFile: (...file: Array<FileNode>) => Promise<void>;
1299
+ hooks: AsyncEventEmitter<KubbHooks>;
1300
+ /**
1301
+ * The current plugin instance.
1302
+ */
1303
+ plugin: Plugin<TOptions>;
1304
+ /**
1305
+ * The current plugin's resolver.
1306
+ */
1307
+ resolver: TOptions['resolver'];
1308
+ /**
1309
+ * The current plugin's transformer.
1310
+ */
1311
+ transformer: Visitor | undefined;
1312
+ /**
1313
+ * Emit a warning.
1314
+ */
1315
+ warn: (message: string) => void;
1316
+ /**
1317
+ * Emit an error.
1318
+ */
1319
+ error: (error: string | Error) => void;
1320
+ /**
1321
+ * Emit an info message.
1322
+ */
1323
+ info: (message: string) => void;
1324
+ /**
1325
+ * Open the current input node in Kubb Studio.
1326
+ */
1327
+ openInStudio: (options?: DevtoolsOptions) => Promise<void>;
1328
+ /**
1329
+ * The configured adapter instance.
1330
+ */
1331
+ adapter: Adapter;
1332
+ /**
1333
+ * Document metadata from the adapter — title, version, base URL, and pre-computed
1334
+ * schema index fields (`circularNames`, `enumNames`).
1335
+ */
1336
+ meta: InputMeta;
1337
+ /**
1338
+ * Resolved options after exclude/include/override filtering.
1339
+ */
1340
+ options: TOptions['resolvedOptions'];
850
1341
  };
851
1342
  /**
852
- * Creates a middleware factory using the hook-style `hooks` API.
1343
+ * Declares a named generator unit that walks the AST and emits files.
853
1344
  *
854
- * Middleware handlers fire after all plugin handlers for any given event, making them ideal for post-processing, logging, and auditing.
855
- * Per-build state (such as accumulators) belongs inside the factory closure so each `createKubb` invocation gets its own isolated instance.
1345
+ * Each method (`schema`, `operation`, `operations`) is called for the matching node type.
1346
+ * Each method returns `TElement | Array<FileNode> | void`. JSX-based generators require a `renderer` factory.
1347
+ * Return `Array<FileNode>` directly or call `ctx.upsertFile()` manually and return `void` to bypass rendering.
856
1348
  *
857
- * @note The factory can accept typed options. See examples for using options and per-build state patterns.
1349
+ * @note Generators are consumed by plugins and registered via `ctx.addGenerator()` in `kubb:plugin:setup`.
858
1350
  *
859
1351
  * @example
860
1352
  * ```ts
861
- * import { defineMiddleware } from '@kubb/core'
1353
+ * import { defineGenerator } from '@kubb/core'
1354
+ * import { jsxRenderer } from '@kubb/renderer-jsx'
862
1355
  *
863
- * // Stateless middleware
864
- * export const logMiddleware = defineMiddleware(() => ({
865
- * name: 'log-middleware',
866
- * hooks: {
867
- * 'kubb:build:end'({ files }) {
868
- * console.log(`Build complete with ${files.length} files`)
869
- * },
1356
+ * export const typeGenerator = defineGenerator({
1357
+ * name: 'typescript',
1358
+ * renderer: jsxRenderer,
1359
+ * schema(node, ctx) {
1360
+ * const { adapter, resolver, root, options } = ctx
1361
+ * return <File ...><Type node={node} resolver={resolver} /></File>
870
1362
  * },
871
- * }))
872
- *
873
- * // Middleware with options and per-build state
874
- * export const prefixMiddleware = defineMiddleware((options: { prefix: string } = { prefix: '' }) => {
875
- * const seen = new Set<string>()
876
- * return {
877
- * name: 'prefix-middleware',
878
- * hooks: {
879
- * 'kubb:plugin:end'({ plugin }) {
880
- * seen.add(`${options.prefix}${plugin.name}`)
881
- * },
882
- * },
883
- * }
884
1363
  * })
885
1364
  * ```
886
1365
  */
887
- declare function defineMiddleware<TOptions extends object = object>(factory: (options: TOptions) => Middleware): (options?: TOptions) => Middleware;
888
- //#endregion
889
- //#region src/defineParser.d.ts
890
- type PrintOptions = {
891
- extname?: FileNode['extname'];
892
- };
893
- type Parser<TMeta extends object = any> = {
1366
+ type Generator$1<TOptions extends PluginFactoryOptions = PluginFactoryOptions, TElement = unknown> = {
1367
+ /**
1368
+ * Used in diagnostic messages and debug output.
1369
+ */
894
1370
  name: string;
895
1371
  /**
896
- * File extensions this parser handles.
897
- * Use `undefined` to create a catch-all fallback parser.
1372
+ * Optional renderer factory that produces a {@link Renderer} for each render cycle.
898
1373
  *
899
- * @example Handled extensions
900
- * `['.ts', '.js']`
1374
+ * Generators that return renderer elements (e.g. JSX via `@kubb/renderer-jsx`) must set this
1375
+ * to the matching renderer factory (e.g. `jsxRenderer` from `@kubb/renderer-jsx`).
1376
+ *
1377
+ * Generators that only return `Array<FileNode>` or `void` do not need to set this.
1378
+ *
1379
+ * Set `renderer: null` to explicitly opt out of rendering even when the parent plugin
1380
+ * declares a `renderer` (overrides the plugin-level fallback).
1381
+ *
1382
+ * @example
1383
+ * ```ts
1384
+ * import { jsxRenderer } from '@kubb/renderer-jsx'
1385
+ * export const myGenerator = defineGenerator<PluginTs>({
1386
+ * renderer: jsxRenderer,
1387
+ * schema(node, ctx) { return <File ...>...</File> },
1388
+ * })
1389
+ * ```
901
1390
  */
902
- extNames: Array<FileNode['extname']> | undefined;
1391
+ renderer?: RendererFactory<TElement> | null;
903
1392
  /**
904
- * Convert a resolved file to a string.
1393
+ * Called for each schema node in the AST walk.
1394
+ * `ctx` carries the plugin context with `adapter` and `meta` (document metadata),
1395
+ * plus `ctx.options` with the per-node resolved options (after exclude/include/override).
1396
+ */
1397
+ schema?: (node: SchemaNode, ctx: GeneratorContext<TOptions>) => PossiblePromise<TElement | Array<FileNode> | void>;
1398
+ /**
1399
+ * Called for each operation node in the AST walk.
1400
+ * `ctx` carries the plugin context with `adapter` and `meta` (document metadata),
1401
+ * plus `ctx.options` with the per-node resolved options (after exclude/include/override).
1402
+ */
1403
+ operation?: (node: OperationNode, ctx: GeneratorContext<TOptions>) => PossiblePromise<TElement | Array<FileNode> | void>;
1404
+ /**
1405
+ * Called once after all operations have been walked.
1406
+ * `ctx` carries the plugin context with `adapter` and `meta` (document metadata),
1407
+ * plus `ctx.options` with the plugin-level options for the batch call.
905
1408
  */
906
- parse(file: FileNode<TMeta>, options?: PrintOptions): Promise<string> | string;
1409
+ operations?: (nodes: Array<OperationNode>, ctx: GeneratorContext<TOptions>) => PossiblePromise<TElement | Array<FileNode> | void>;
907
1410
  };
908
1411
  /**
909
- * Defines a parser with type safety. Creates parsers that transform generated files to strings based on their extension.
910
- *
911
- * @note Call the returned factory with optional options to instantiate the parser.
912
- *
913
- * @example
914
- * ```ts
915
- * import { defineParser } from '@kubb/core'
916
- *
917
- * export const jsonParser = defineParser({
918
- * name: 'json',
919
- * extNames: ['.json'],
920
- * parse(file) {
921
- * const { extractStringsFromNodes } = await import('@kubb/ast')
922
- * return file.sources.map((s) => extractStringsFromNodes(s.nodes ?? [])).join('\n')
923
- * },
924
- * })
925
- * ```
1412
+ * Defines a generator. Returns the object as-is with correct `this` typings.
1413
+ * `applyHookResult` handles renderer elements and `File[]` uniformly using
1414
+ * the generator's declared `renderer` factory.
926
1415
  */
927
- declare function defineParser<TMeta extends object = any>(parser: Parser<TMeta>): Parser<TMeta>;
1416
+ declare function defineGenerator<TOptions extends PluginFactoryOptions = PluginFactoryOptions, TElement = unknown>(generator: Generator$1<TOptions, TElement>): Generator$1<TOptions, TElement>;
928
1417
  //#endregion
929
- //#region src/types.d.ts
1418
+ //#region src/createKubb.d.ts
930
1419
  /**
931
1420
  * Safely extracts a type from a registry, returning `{}` if the key doesn't exist.
932
1421
  * Enables optional interface augmentation for `Kubb.ConfigOptionsRegistry` and `Kubb.PluginOptionsRegistry`
@@ -973,116 +1462,34 @@ type InputData = {
973
1462
  };
974
1463
  type Input = InputPath | InputData;
975
1464
  /**
976
- * Source data passed to an adapter's `parse` function.
977
- * Mirrors the config input shape with paths resolved to absolute.
978
- */
979
- type AdapterSource = {
980
- type: 'path';
981
- path: string;
982
- } | {
983
- type: 'data';
984
- data: string | unknown;
985
- } | {
986
- type: 'paths';
987
- paths: Array<string>;
988
- };
989
- /**
990
- * Generic type parameters for an adapter definition.
991
- *
992
- * - `TName` — unique identifier (e.g. `'oas'`, `'asyncapi'`)
993
- * - `TOptions` — user-facing options passed to the adapter factory
994
- * - `TResolvedOptions` — options after defaults applied
995
- * - `TDocument` — type of the parsed source document
996
- */
997
- type AdapterFactoryOptions<TName extends string = string, TOptions extends object = object, TResolvedOptions extends object = TOptions, TDocument = unknown> = {
998
- name: TName;
999
- options: TOptions;
1000
- resolvedOptions: TResolvedOptions;
1001
- document: TDocument;
1002
- };
1003
- /**
1004
- * Adapter that converts input files or data into an `InputNode`.
1465
+ * Build configuration for Kubb code generation.
1005
1466
  *
1006
- * Adapters parse different schema formats (OpenAPI, AsyncAPI, Drizzle, etc.) into Kubb's
1007
- * universal intermediate representation that all plugins consume.
1467
+ * The Config is the main entry point for customizing how Kubb generates code. It specifies:
1468
+ * - What to generate from (adapter + input)
1469
+ * - Where to output generated code (output)
1470
+ * - How to generate (plugins + middleware)
1471
+ * - Runtime details (parsers, storage, renderer)
1008
1472
  *
1009
- * @example
1010
- * ```ts
1011
- * import { adapterOas } from '@kubb/adapter-oas'
1473
+ * See `UserConfig` for a relaxed version with sensible defaults.
1012
1474
  *
1013
- * export default defineConfig({
1014
- * adapter: adapterOas(),
1015
- * input: { path: './openapi.yaml' },
1016
- * plugins: [pluginTs(), pluginZod()],
1017
- * })
1018
- * ```
1475
+ * @private
1019
1476
  */
1020
- type Adapter<TOptions extends AdapterFactoryOptions = AdapterFactoryOptions> = {
1021
- /**
1022
- * Human-readable adapter identifier (e.g. `'oas'`, `'asyncapi'`).
1023
- */
1024
- name: TOptions['name'];
1025
- /**
1026
- * Resolved adapter options after defaults have been applied.
1027
- */
1028
- options: TOptions['resolvedOptions'];
1477
+ type Config<TInput = Input> = {
1029
1478
  /**
1030
- * Parsed source document after the first `parse()` call. `null` before parsing.
1479
+ * Display name for this configuration in CLI output and logs.
1480
+ * Useful when running multiple builds with `defineConfig` arrays.
1481
+ *
1482
+ * @example
1483
+ * ```ts
1484
+ * name: 'api-client'
1485
+ * ```
1031
1486
  */
1032
- document: TOptions['document'] | null;
1033
- inputNode: InputNode | null;
1487
+ name?: string;
1034
1488
  /**
1035
- * Parse the source into a universal `InputNode`.
1489
+ * Project root directory, absolute or relative to the config file.
1490
+ * @default process.cwd()
1036
1491
  */
1037
- parse: (source: AdapterSource) => PossiblePromise<InputNode>;
1038
- /**
1039
- * Extract `ImportNode` entries for a schema tree.
1040
- * Returns an empty array before the first `parse()` call.
1041
- *
1042
- * The `resolve` callback receives the collision-corrected schema name and must
1043
- * return `{ name, path }` for the import, or `undefined` to skip it.
1044
- */
1045
- getImports: (node: SchemaNode, resolve: (schemaName: string) => {
1046
- name: string;
1047
- path: string;
1048
- }) => Array<ImportNode>;
1049
- };
1050
- type DevtoolsOptions = {
1051
- /**
1052
- * Open the AST inspector in Kubb Studio (`/ast`). Defaults to the main Studio page.
1053
- * @default false
1054
- */
1055
- ast?: boolean;
1056
- };
1057
- /**
1058
- * Build configuration for Kubb code generation.
1059
- *
1060
- * The Config is the main entry point for customizing how Kubb generates code. It specifies:
1061
- * - What to generate from (adapter + input)
1062
- * - Where to output generated code (output)
1063
- * - How to generate (plugins + middleware)
1064
- * - Runtime details (parsers, storage, renderer)
1065
- *
1066
- * See `UserConfig` for a relaxed version with sensible defaults.
1067
- *
1068
- * @private
1069
- */
1070
- type Config<TInput = Input> = {
1071
- /**
1072
- * Display name for this configuration in CLI output and logs.
1073
- * Useful when running multiple builds with `defineConfig` arrays.
1074
- *
1075
- * @example
1076
- * ```ts
1077
- * name: 'api-client'
1078
- * ```
1079
- */
1080
- name?: string;
1081
- /**
1082
- * Project root directory, absolute or relative to the config file.
1083
- * @default process.cwd()
1084
- */
1085
- root: string;
1492
+ root: string;
1086
1493
  /**
1087
1494
  * Parsers that convert generated files to strings.
1088
1495
  * Each parser handles specific extensions (e.g. `.ts`, `.tsx`).
@@ -1100,9 +1507,13 @@ type Config<TInput = Input> = {
1100
1507
  */
1101
1508
  parsers: Array<Parser>;
1102
1509
  /**
1103
- * Adapter that parses input files into the universal `InputNode` representation.
1510
+ * Adapter that parses input files into the universal AST representation.
1104
1511
  * Use `@kubb/adapter-oas` for OpenAPI/Swagger or `@kubb/adapter-asyncapi` for other formats.
1105
1512
  *
1513
+ * When omitted, Kubb runs in plugin-only mode: `kubb:plugin:setup` fires and files
1514
+ * injected via `injectFile` are written, but no AST walk occurs and generator hooks
1515
+ * (`kubb:generate:schema`, `kubb:generate:operation`) are never emitted.
1516
+ *
1106
1517
  * @example
1107
1518
  * ```ts
1108
1519
  * import { adapterOas } from '@kubb/adapter-oas'
@@ -1112,12 +1523,13 @@ type Config<TInput = Input> = {
1112
1523
  * })
1113
1524
  * ```
1114
1525
  */
1115
- adapter: Adapter;
1526
+ adapter?: Adapter;
1116
1527
  /**
1117
1528
  * Source file or data to generate code from.
1118
1529
  * Use `input.path` for a file path or `input.data` for inline data.
1530
+ * Required when an adapter is configured; omit when running in plugin-only mode.
1119
1531
  */
1120
- input: TInput;
1532
+ input?: TInput;
1121
1533
  output: {
1122
1534
  /**
1123
1535
  * Output directory for generated files, absolute or relative to `root`.
@@ -1146,13 +1558,6 @@ type Config<TInput = Input> = {
1146
1558
  * ```
1147
1559
  */
1148
1560
  clean?: boolean;
1149
- /**
1150
- * Persists generated files to the file system.
1151
- *
1152
- * @default true
1153
- * @deprecated Use `storage` option to control where files are written instead.
1154
- */
1155
- write?: boolean;
1156
1561
  /**
1157
1562
  * Auto-format generated files after code generation completes.
1158
1563
  *
@@ -1226,7 +1631,7 @@ type Config<TInput = Input> = {
1226
1631
  * ```
1227
1632
  */
1228
1633
  override?: boolean;
1229
- } & ExtractRegistryKey<Kubb.ConfigOptionsRegistry, 'output'>;
1634
+ } & ExtractRegistryKey<Kubb$1.ConfigOptionsRegistry, 'output'>;
1230
1635
  /**
1231
1636
  * Storage backend that controls where and how generated files are persisted.
1232
1637
  *
@@ -1247,7 +1652,7 @@ type Config<TInput = Input> = {
1247
1652
  *
1248
1653
  * @see {@link Storage} interface for implementing custom backends.
1249
1654
  */
1250
- storage?: Storage;
1655
+ storage: Storage;
1251
1656
  /**
1252
1657
  * Plugins that execute during the build to generate code and transform the AST.
1253
1658
  *
@@ -1288,19 +1693,6 @@ type Config<TInput = Input> = {
1288
1693
  * @see {@link defineMiddleware} to create custom middleware.
1289
1694
  */
1290
1695
  middleware?: Array<Middleware>;
1291
- /**
1292
- * Default renderer factory used by all plugins and generators.
1293
- * Resolution chain: `generator.renderer` → `plugin.renderer` → `config.renderer` → `undefined` (raw FileNode[] mode).
1294
- *
1295
- * @example
1296
- * ```ts
1297
- * import { jsxRenderer } from '@kubb/renderer-jsx'
1298
- * export default defineConfig({
1299
- * renderer: jsxRenderer,
1300
- * plugins: [pluginTs(), pluginZod()],
1301
- * })
1302
- * ```
1303
- */
1304
1696
  /**
1305
1697
  * Renderer that converts generated AST nodes to code strings.
1306
1698
  *
@@ -1320,7 +1712,7 @@ type Config<TInput = Input> = {
1320
1712
  /**
1321
1713
  * Kubb Studio cloud integration settings.
1322
1714
  *
1323
- * Kubb Studio (https://studio.kubb.dev) is a web-based IDE for managing API specs and generated code.
1715
+ * Kubb Studio (https://kubb.studio) is a web-based IDE for managing API specs and generated code.
1324
1716
  * Set to `true` to enable with default settings, or pass an object to customize the Studio URL.
1325
1717
  *
1326
1718
  * @default false // disabled by default
@@ -1333,7 +1725,7 @@ type Config<TInput = Input> = {
1333
1725
  devtools?: true | {
1334
1726
  /**
1335
1727
  * Override the Kubb Studio base URL.
1336
- * @default 'https://studio.kubb.dev'
1728
+ * @default 'https://kubb.studio'
1337
1729
  */
1338
1730
  studioUrl?: typeof DEFAULT_STUDIO_URL | (string & {});
1339
1731
  };
@@ -1369,97 +1761,6 @@ type Config<TInput = Input> = {
1369
1761
  done?: string | Array<string>;
1370
1762
  };
1371
1763
  };
1372
- /**
1373
- * Type/string pattern filter for include/exclude/override matching.
1374
- */
1375
- type PatternFilter = {
1376
- type: string;
1377
- pattern: string | RegExp;
1378
- };
1379
- /**
1380
- * Pattern filter with partial option overrides applied when the pattern matches.
1381
- */
1382
- type PatternOverride<TOptions> = PatternFilter & {
1383
- options: Omit<Partial<TOptions>, 'override'>;
1384
- };
1385
- /**
1386
- * Context for resolving filtered options for a given operation or schema node.
1387
- *
1388
- * @internal
1389
- */
1390
- type ResolveOptionsContext<TOptions> = {
1391
- options: TOptions;
1392
- exclude?: Array<PatternFilter>;
1393
- include?: Array<PatternFilter>;
1394
- override?: Array<PatternOverride<TOptions>>;
1395
- };
1396
- /**
1397
- * Base constraint for all plugin resolver objects.
1398
- *
1399
- * `default`, `resolveOptions`, `resolvePath`, `resolveFile`, `resolveBanner`, and `resolveFooter`
1400
- * are injected automatically by `defineResolver` — extend this type to add custom resolution methods.
1401
- *
1402
- * @example
1403
- * ```ts
1404
- * type MyResolver = Resolver & {
1405
- * resolveName(node: SchemaNode): string
1406
- * resolveTypedName(node: SchemaNode): string
1407
- * }
1408
- * ```
1409
- */
1410
- type Resolver = {
1411
- name: string;
1412
- pluginName: Plugin['name'];
1413
- default(name: string, type?: 'file' | 'function' | 'type' | 'const'): string;
1414
- resolveOptions<TOptions>(node: Node, context: ResolveOptionsContext<TOptions>): TOptions | null;
1415
- resolvePath(params: ResolverPathParams, context: ResolverContext): string;
1416
- resolveFile(params: ResolverFileParams, context: ResolverContext): FileNode;
1417
- resolveBanner(node: InputNode | null, context: ResolveBannerContext): string | undefined;
1418
- resolveFooter(node: InputNode | null, context: ResolveBannerContext): string | undefined;
1419
- };
1420
- type PluginFactoryOptions<
1421
- /**
1422
- * Unique plugin name.
1423
- */
1424
- TName extends string = string,
1425
- /**
1426
- * User-facing plugin options.
1427
- */
1428
- TOptions extends object = object,
1429
- /**
1430
- * Plugin options after defaults are applied.
1431
- */
1432
- TResolvedOptions extends object = TOptions,
1433
- /**
1434
- * Resolver that encapsulates naming and path-resolution helpers.
1435
- * Define with `defineResolver` and export alongside the plugin.
1436
- */
1437
- TResolver extends Resolver = Resolver> = {
1438
- name: TName;
1439
- options: TOptions;
1440
- resolvedOptions: TResolvedOptions;
1441
- resolver: TResolver;
1442
- };
1443
- /**
1444
- * Normalized plugin after setup, with runtime fields populated.
1445
- * For internal use only — plugins use the public `Plugin` type externally.
1446
- *
1447
- * @internal
1448
- */
1449
- type NormalizedPlugin<TOptions extends PluginFactoryOptions = PluginFactoryOptions> = Plugin<TOptions> & {
1450
- options: TOptions['resolvedOptions'] & {
1451
- output: Output;
1452
- include?: Array<Include>;
1453
- exclude: Array<Exclude$1>;
1454
- override: Array<Override<TOptions['resolvedOptions']>>;
1455
- };
1456
- resolver: TOptions['resolver'];
1457
- transformer?: Visitor;
1458
- renderer?: RendererFactory;
1459
- generators?: Array<Generator>;
1460
- apply?: (config: Config) => boolean;
1461
- version?: string;
1462
- };
1463
1764
  /**
1464
1765
  * Partial `Config` for user-facing entry points with sensible defaults.
1465
1766
  *
@@ -1471,678 +1772,536 @@ type NormalizedPlugin<TOptions extends PluginFactoryOptions = PluginFactoryOptio
1471
1772
  * ```ts
1472
1773
  * export default defineConfig({
1473
1774
  * input: { path: './petstore.yaml' },
1474
- * output: { path: './src/gen' },
1475
- * plugins: [pluginTs(), pluginZod()],
1476
- * })
1477
- * ```
1478
- */
1479
- type UserConfig<TInput = Input> = Omit<Config<TInput>, 'root' | 'plugins' | 'parsers' | 'adapter'> & {
1480
- /**
1481
- * Project root directory, absolute or relative to the config file location.
1482
- *
1483
- * Used as the base path for `root`-relative paths (e.g., `output.path`, file paths in hooks).
1484
- *
1485
- * @default process.cwd()
1486
- * @example
1487
- * ```ts
1488
- * root: '/home/user/my-project'
1489
- * root: './my-project' // relative to config file
1490
- * ```
1491
- */
1492
- root?: string;
1493
- /**
1494
- * Custom parsers that convert generated AST nodes to strings (TypeScript, JSON, markdown, etc.).
1495
- *
1496
- * Each parser handles a specific file type. By default, Kubb uses `parserTs` from `@kubb/parser-ts` for TypeScript files.
1497
- * Pass custom parsers to support additional languages or custom formats.
1498
- *
1499
- * @default [parserTs] // from @kubb/parser-ts
1500
- * @example
1501
- * ```ts
1502
- * import { parserTs } from '@kubb/parser-ts'
1503
- * import { parserJsonSchema } from '@kubb/parser-json-schema'
1504
- *
1505
- * parsers: [parserTs(), parserJsonSchema()]
1506
- * ```
1507
- *
1508
- * @see {@link Parser} to implement a custom parser.
1509
- */
1510
- parsers?: Array<Parser>;
1511
- /**
1512
- * Adapter that parses your API specification (OpenAPI, GraphQL, AsyncAPI, etc.) into Kubb's universal AST.
1513
- *
1514
- * The adapter bridge between your input format and Kubb's internal representation. By default, uses the OAS adapter.
1515
- * Pass an alternative adapter (or multiple configs with different adapters) to support different spec formats.
1516
- *
1517
- * @default new OasAdapter() // from @kubb/adapter-oas
1518
- * @example
1519
- * ```ts
1520
- * import { Oas } from '@kubb/adapter-oas'
1521
- *
1522
- * adapter: new Oas({ apiVersion: '3.0.0' })
1523
- * ```
1524
- *
1525
- * @see {@link Adapter} to implement a custom adapter for GraphQL or other formats.
1526
- */
1527
- adapter?: Adapter;
1528
- /**
1529
- * Plugins that execute during the build to generate code and transform the AST.
1530
- *
1531
- * Each plugin processes the AST produced by the adapter and can emit files for different
1532
- * programming languages or formats (TypeScript, Zod schemas, Faker data, etc.).
1533
- *
1534
- * @default [] // no plugins (useful for setup/testing)
1535
- * @example
1536
- * ```ts
1537
- * plugins: [
1538
- * pluginTs({ output: { path: './src/gen' } }),
1539
- * pluginZod({ output: { path: './src/gen' } }),
1540
- * ]
1541
- * ```
1542
- *
1543
- * @see {@link definePlugin} to create a custom plugin.
1544
- */
1545
- plugins?: Array<Plugin>;
1546
- };
1547
- type ResolveNameParams = {
1548
- name: string;
1549
- pluginName?: string;
1550
- /**
1551
- * Entity type being named.
1552
- * - `'file'` — file name (camelCase)
1553
- * - `'function'` — exported function name (camelCase)
1554
- * - `'type'` — TypeScript type name (PascalCase)
1555
- * - `'const'` — variable name (camelCase)
1556
- */
1557
- type?: 'file' | 'function' | 'type' | 'const';
1558
- };
1559
- /**
1560
- * Context object passed to generator `schema`, `operation`, and `operations` methods.
1561
- *
1562
- * The adapter is always defined (guaranteed by `runPluginAstHooks`) so no runtime checks
1563
- * are needed. `ctx.options` carries resolved per-node options after exclude/include/override
1564
- * filtering for individual schema/operation calls, or plugin-level options for operations.
1565
- */
1566
- type GeneratorContext<TOptions extends PluginFactoryOptions = PluginFactoryOptions> = {
1567
- config: Config;
1568
- /**
1569
- * Absolute path to the current plugin's output directory.
1570
- */
1571
- root: string;
1572
- /**
1573
- * Determine output mode based on the output config.
1574
- * Returns `'single'` when `output.path` is a file, `'split'` for a directory.
1575
- */
1576
- getMode: (output: {
1577
- path: string;
1578
- }) => 'single' | 'split';
1579
- driver: PluginDriver;
1580
- /**
1581
- * Get a plugin by name, typed via `Kubb.PluginRegistry` when registered.
1582
- */
1583
- getPlugin<TName extends keyof Kubb.PluginRegistry>(name: TName): Plugin<Kubb.PluginRegistry[TName]> | undefined;
1584
- getPlugin(name: string): Plugin | undefined;
1585
- /**
1586
- * Get a plugin by name, throws an error if not found.
1587
- */
1588
- requirePlugin<TName extends keyof Kubb.PluginRegistry>(name: TName): Plugin<Kubb.PluginRegistry[TName]>;
1589
- requirePlugin(name: string): Plugin;
1590
- /**
1591
- * Get a resolver by plugin name, typed via `Kubb.PluginRegistry` when registered.
1592
- */
1593
- getResolver<TName extends keyof Kubb.PluginRegistry>(name: TName): Kubb.PluginRegistry[TName]['resolver'];
1594
- getResolver(name: string): Resolver;
1595
- /**
1596
- * Add files only if they don't exist.
1597
- */
1598
- addFile: (...file: Array<FileNode>) => Promise<void>;
1599
- /**
1600
- * Merge sources into the same output file.
1601
- */
1602
- upsertFile: (...file: Array<FileNode>) => Promise<void>;
1603
- hooks: AsyncEventEmitter<KubbHooks>;
1604
- /**
1605
- * The current plugin instance.
1606
- */
1607
- plugin: Plugin<TOptions>;
1608
- /**
1609
- * The current plugin's resolver.
1610
- */
1611
- resolver: TOptions['resolver'];
1612
- /**
1613
- * The current plugin's transformer.
1614
- */
1615
- transformer: Visitor | undefined;
1616
- /**
1617
- * Emit a warning.
1618
- */
1619
- warn: (message: string) => void;
1620
- /**
1621
- * Emit an error.
1622
- */
1623
- error: (error: string | Error) => void;
1624
- /**
1625
- * Emit an info message.
1626
- */
1627
- info: (message: string) => void;
1628
- /**
1629
- * Open the current input node in Kubb Studio.
1630
- */
1631
- openInStudio: (options?: DevtoolsOptions) => Promise<void>;
1632
- /**
1633
- * The configured adapter instance.
1634
- */
1635
- adapter: Adapter;
1636
- /**
1637
- * The universal `InputNode` produced by the adapter.
1638
- */
1639
- inputNode: InputNode;
1640
- /**
1641
- * Resolved options after exclude/include/override filtering.
1642
- */
1643
- options: TOptions['resolvedOptions'];
1644
- };
1645
- /**
1646
- * Output configuration for generated files.
1647
- */
1648
- type Output<_TOptions = unknown> = {
1649
- /**
1650
- * Output folder or file path for generated code.
1651
- */
1652
- path: string;
1653
- /**
1654
- * Text or function prepended to every generated file.
1655
- * When a function, receives the current `InputNode` and returns a string.
1656
- */
1657
- banner?: string | ((node?: InputNode) => string);
1658
- /**
1659
- * Text or function appended to every generated file.
1660
- * When a function, receives the current `InputNode` and returns a string.
1661
- */
1662
- footer?: string | ((node?: InputNode) => string);
1663
- /**
1664
- * Whether to override existing external files if they already exist.
1665
- * @default false
1666
- */
1667
- override?: boolean;
1668
- } & ExtractRegistryKey<Kubb.PluginOptionsRegistry, 'output'>;
1669
- type Group = {
1670
- /**
1671
- * How to group files into subdirectories.
1672
- * - `'tag'` — group by OpenAPI tags
1673
- * - `'path'` — group by OpenAPI paths
1674
- */
1675
- type: 'tag' | 'path';
1676
- /**
1677
- * Function that returns the subdirectory name for a group value.
1678
- * Defaults to `${camelCase(group)}Controller` for tags, first path segment for paths.
1679
- */
1680
- name?: (context: {
1681
- group: string;
1682
- }) => string;
1683
- };
1684
- type LoggerOptions = {
1685
- /**
1686
- * Log level for output verbosity.
1687
- * @default 3
1688
- */
1689
- logLevel: (typeof logLevel)[keyof typeof logLevel];
1690
- };
1691
- /**
1692
- * Shared context passed to plugins, parsers, and other internals.
1693
- */
1694
- type LoggerContext = AsyncEventEmitter<KubbHooks>;
1695
- type Logger<TOptions extends LoggerOptions = LoggerOptions> = {
1696
- name: string;
1697
- install: (context: LoggerContext, options?: TOptions) => void | Promise<void>;
1698
- };
1699
- type UserLogger<TOptions extends LoggerOptions = LoggerOptions> = Logger<TOptions>;
1700
- /**
1701
- * Context for hook-style plugin `kubb:plugin:setup` handler.
1702
- * Provides methods to register generators, configure resolvers, transformers, and renderers.
1775
+ * output: { path: './src/gen' },
1776
+ * plugins: [pluginTs(), pluginZod()],
1777
+ * })
1778
+ * ```
1703
1779
  */
1704
- type KubbPluginSetupContext<TFactory extends PluginFactoryOptions = PluginFactoryOptions> = {
1705
- /**
1706
- * Register a generator dynamically. Generators fire during the AST walk (schema/operation/operations)
1707
- * just like generators declared statically on `createPlugin`.
1708
- */
1709
- addGenerator<TElement = unknown>(generator: Generator<TFactory, TElement>): void;
1710
- /**
1711
- * Set or override the resolver for this plugin.
1712
- * The resolver controls file naming and path resolution.
1713
- */
1714
- setResolver(resolver: Partial<TFactory['resolver']>): void;
1715
- /**
1716
- * Set the AST transformer to pre-process nodes before they reach generators.
1717
- */
1718
- setTransformer(visitor: Visitor): void;
1719
- /**
1720
- * Set the renderer factory to process JSX elements from generators.
1721
- */
1722
- setRenderer(renderer: RendererFactory): void;
1780
+ type UserConfig<TInput = Input> = Omit<Config<TInput>, 'root' | 'plugins' | 'parsers' | 'adapter' | 'storage'> & {
1723
1781
  /**
1724
- * Set resolved options merged into the normalized plugin's `options`.
1725
- * Call this in `kubb:plugin:setup` to provide options generators need.
1782
+ * Project root directory, absolute or relative to the config file location.
1783
+ * @default process.cwd()
1726
1784
  */
1727
- setOptions(options: TFactory['resolvedOptions']): void;
1785
+ root?: string;
1728
1786
  /**
1729
- * Inject a raw file into the build output, bypassing the generation pipeline.
1787
+ * Custom parsers that convert generated AST nodes to strings (TypeScript, JSON, markdown, etc.).
1788
+ * @default [parserTs] // from `@kubb/parser-ts`
1730
1789
  */
1731
- injectFile(userFileNode: UserFileNode): void;
1790
+ parsers?: Array<Parser>;
1732
1791
  /**
1733
- * Merge a partial config update into the current build configuration.
1792
+ * Adapter that parses your API specification into Kubb's universal AST.
1793
+ * When omitted, Kubb runs in plugin-only mode.
1734
1794
  */
1735
- updateConfig(config: Partial<Config>): void;
1795
+ adapter?: Adapter;
1736
1796
  /**
1737
- * The resolved build configuration at setup time.
1797
+ * Plugins that execute during the build to generate code and transform the AST.
1798
+ * @default []
1738
1799
  */
1739
- config: Config;
1800
+ plugins?: Array<Plugin>;
1740
1801
  /**
1741
- * The plugin's user-provided options.
1802
+ * Storage backend that controls where and how generated files are persisted.
1803
+ * @default fsStorage()
1742
1804
  */
1743
- options: TFactory['options'];
1805
+ storage?: Storage;
1744
1806
  };
1807
+ declare global {
1808
+ namespace Kubb {
1809
+ /**
1810
+ * Registry that maps plugin names to their `PluginFactoryOptions`.
1811
+ * Augment this interface in each plugin's `types.ts` to enable automatic
1812
+ * typing for `getPlugin` and `requirePlugin`.
1813
+ *
1814
+ * @example
1815
+ * ```ts
1816
+ * // packages/plugin-ts/src/types.ts
1817
+ * declare global {
1818
+ * namespace Kubb {
1819
+ * interface PluginRegistry {
1820
+ * 'plugin-ts': PluginTs
1821
+ * }
1822
+ * }
1823
+ * }
1824
+ * ```
1825
+ */
1826
+ interface PluginRegistry {}
1827
+ /**
1828
+ * Extension point for root `Config['output']` options.
1829
+ * Augment the `output` key in middleware or plugin packages to add extra fields
1830
+ * to the global output configuration without touching core types.
1831
+ *
1832
+ * @example
1833
+ * ```ts
1834
+ * // packages/middleware-barrel/src/types.ts
1835
+ * declare global {
1836
+ * namespace Kubb {
1837
+ * interface ConfigOptionsRegistry {
1838
+ * output: {
1839
+ * barrel?: import('./types.ts').BarrelConfig | false
1840
+ * }
1841
+ * }
1842
+ * }
1843
+ * }
1844
+ * ```
1845
+ */
1846
+ interface ConfigOptionsRegistry {}
1847
+ /**
1848
+ * Extension point for per-plugin `Output` options.
1849
+ * Augment the `output` key in middleware or plugin packages to add extra fields
1850
+ * to the per-plugin output configuration without touching core types.
1851
+ *
1852
+ * @example
1853
+ * ```ts
1854
+ * // packages/middleware-barrel/src/types.ts
1855
+ * declare global {
1856
+ * namespace Kubb {
1857
+ * interface PluginOptionsRegistry {
1858
+ * output: {
1859
+ * barrel?: import('./types.ts').PluginBarrelConfig | false
1860
+ * }
1861
+ * }
1862
+ * }
1863
+ * }
1864
+ * ```
1865
+ */
1866
+ interface PluginOptionsRegistry {}
1867
+ }
1868
+ }
1745
1869
  /**
1746
- * Context for hook-style plugin `kubb:build:start` handler.
1747
- * Fires immediately before the plugin execution loop begins.
1870
+ * Lifecycle events emitted during Kubb code generation.
1871
+ * Attach listeners before calling `setup()` or `build()` to observe and react to build progress.
1872
+ *
1873
+ * @example
1874
+ * ```ts
1875
+ * kubb.hooks.on('kubb:lifecycle:start', () => {
1876
+ * console.log('Starting Kubb generation')
1877
+ * })
1878
+ *
1879
+ * kubb.hooks.on('kubb:plugin:end', ({ plugin, duration }) => {
1880
+ * console.log(`${plugin.name} completed in ${duration}ms`)
1881
+ * })
1882
+ * ```
1748
1883
  */
1884
+ interface KubbHooks {
1885
+ 'kubb:lifecycle:start': [ctx: KubbLifecycleStartContext];
1886
+ 'kubb:lifecycle:end': [];
1887
+ 'kubb:config:start': [];
1888
+ 'kubb:config:end': [ctx: KubbConfigEndContext];
1889
+ 'kubb:generation:start': [ctx: KubbGenerationStartContext];
1890
+ 'kubb:generation:end': [ctx: KubbGenerationEndContext];
1891
+ 'kubb:generation:summary': [ctx: KubbGenerationSummaryContext];
1892
+ 'kubb:format:start': [];
1893
+ 'kubb:format:end': [];
1894
+ 'kubb:lint:start': [];
1895
+ 'kubb:lint:end': [];
1896
+ 'kubb:hooks:start': [];
1897
+ 'kubb:hooks:end': [];
1898
+ 'kubb:hook:start': [ctx: KubbHookStartContext];
1899
+ 'kubb:hook:end': [ctx: KubbHookEndContext];
1900
+ 'kubb:version:new': [ctx: KubbVersionNewContext];
1901
+ 'kubb:info': [ctx: KubbInfoContext];
1902
+ 'kubb:error': [ctx: KubbErrorContext];
1903
+ 'kubb:success': [ctx: KubbSuccessContext];
1904
+ 'kubb:warn': [ctx: KubbWarnContext];
1905
+ 'kubb:debug': [ctx: KubbDebugContext];
1906
+ 'kubb:files:processing:start': [ctx: KubbFilesProcessingStartContext];
1907
+ 'kubb:files:processing:update': [ctx: KubbFilesProcessingUpdateContext];
1908
+ 'kubb:files:processing:end': [ctx: KubbFilesProcessingEndContext];
1909
+ 'kubb:plugin:start': [ctx: KubbPluginStartContext];
1910
+ 'kubb:plugin:end': [ctx: KubbPluginEndContext];
1911
+ 'kubb:plugin:setup': [ctx: KubbPluginSetupContext];
1912
+ 'kubb:build:start': [ctx: KubbBuildStartContext];
1913
+ 'kubb:plugins:end': [ctx: KubbPluginsEndContext];
1914
+ 'kubb:build:end': [ctx: KubbBuildEndContext];
1915
+ 'kubb:generate:schema': [node: SchemaNode, ctx: GeneratorContext];
1916
+ 'kubb:generate:operation': [node: OperationNode, ctx: GeneratorContext];
1917
+ 'kubb:generate:operations': [nodes: Array<OperationNode>, ctx: GeneratorContext];
1918
+ }
1749
1919
  type KubbBuildStartContext = {
1920
+ /**
1921
+ * Resolved configuration for this build.
1922
+ */
1750
1923
  config: Config;
1924
+ /**
1925
+ * Adapter that parsed the input into the universal AST.
1926
+ */
1751
1927
  adapter: Adapter;
1752
- inputNode: InputNode;
1753
1928
  /**
1754
- * Get a plugin by name, typed via `Kubb.PluginRegistry` when registered.
1929
+ * Metadata about the parsed document (title, version, base URL, circular schema names, enum names).
1930
+ * To observe individual schemas and operations use the `kubb:generate:schema` / `kubb:generate:operation` hooks.
1755
1931
  */
1756
- getPlugin<TName extends keyof Kubb.PluginRegistry>(name: TName): Plugin<Kubb.PluginRegistry[TName]> | undefined;
1932
+ meta: InputMeta | undefined;
1933
+ /**
1934
+ * Looks up a registered plugin by name, typed by the plugin registry.
1935
+ */
1936
+ getPlugin<TName extends keyof Kubb$1.PluginRegistry>(name: TName): Plugin<Kubb$1.PluginRegistry[TName]> | undefined;
1757
1937
  getPlugin(name: string): Plugin | undefined;
1758
1938
  /**
1759
- * Get all files currently in the file manager.
1760
- * Call this lazily (e.g. in `kubb:plugin:end`) to see files added by prior plugins.
1939
+ * Snapshot of all files accumulated so far.
1761
1940
  */
1762
1941
  readonly files: ReadonlyArray<FileNode>;
1763
1942
  /**
1764
- * Upsert one or more files into the file manager.
1765
- * Files with the same path are merged; new files are appended.
1766
- * Safe to call at any point during the plugin lifecycle, including inside `kubb:plugin:end`.
1943
+ * Adds or merges one or more files into the file manager.
1767
1944
  */
1768
1945
  upsertFile: (...files: Array<FileNode>) => void;
1769
1946
  };
1770
- /**
1771
- * Context for `kubb:plugins:end` handlers.
1772
- * Fires after plugins run and per-plugin barrels are written, before final write to disk.
1773
- * Middleware that needs final files (e.g. root barrel) use this event.
1774
- */
1775
1947
  type KubbPluginsEndContext = {
1948
+ /**
1949
+ * Resolved configuration for this build.
1950
+ */
1776
1951
  config: Config;
1777
1952
  /**
1778
- * All files currently in the file manager (lazy snapshot).
1953
+ * Snapshot of all files accumulated across all plugins.
1779
1954
  */
1780
1955
  readonly files: ReadonlyArray<FileNode>;
1781
1956
  /**
1782
- * Upsert files into the file manager.
1783
- * Files added here are included in the write pass.
1957
+ * Adds or merges one or more files into the file manager.
1784
1958
  */
1785
1959
  upsertFile: (...files: Array<FileNode>) => void;
1786
1960
  };
1787
- /**
1788
- * Context for hook-style plugin `kubb:build:end` handler.
1789
- * Fires after all files have been written to disk.
1790
- */
1791
1961
  type KubbBuildEndContext = {
1962
+ /**
1963
+ * All files generated during this build.
1964
+ */
1792
1965
  files: Array<FileNode>;
1966
+ /**
1967
+ * Resolved configuration for this build.
1968
+ */
1793
1969
  config: Config;
1970
+ /**
1971
+ * Absolute path to the output directory.
1972
+ */
1794
1973
  outputDir: string;
1795
1974
  };
1796
1975
  type KubbLifecycleStartContext = {
1976
+ /**
1977
+ * Current Kubb version string.
1978
+ */
1797
1979
  version: string;
1798
1980
  };
1799
1981
  type KubbConfigEndContext = {
1982
+ /**
1983
+ * All resolved configs after defaults are applied.
1984
+ */
1800
1985
  configs: Array<Config>;
1801
1986
  };
1802
1987
  type KubbGenerationStartContext = {
1988
+ /**
1989
+ * Resolved configuration for this generation run.
1990
+ */
1803
1991
  config: Config;
1804
1992
  };
1805
1993
  type KubbGenerationEndContext = {
1994
+ /**
1995
+ * Resolved configuration for this generation run.
1996
+ */
1806
1997
  config: Config;
1807
- files: Array<FileNode>;
1808
- sources: Map<string, string>;
1998
+ /**
1999
+ * Read-only view of the files written during this build.
2000
+ * Reads go directly to `config.storage` — nothing extra is held in memory.
2001
+ *
2002
+ * @example Read a generated file
2003
+ * `const code = await storage.getItem('/src/gen/pet.ts')`
2004
+ *
2005
+ * @example Walk every generated file
2006
+ * ```ts
2007
+ * for (const path of await storage.getKeys()) {
2008
+ * const code = await storage.getItem(path)
2009
+ * }
2010
+ * ```
2011
+ */
2012
+ storage: Storage;
1809
2013
  };
1810
2014
  type KubbGenerationSummaryContext = {
2015
+ /**
2016
+ * Resolved configuration for this generation run.
2017
+ */
1811
2018
  config: Config;
2019
+ /**
2020
+ * Plugins that threw during generation, paired with their errors.
2021
+ */
1812
2022
  failedPlugins: Set<{
1813
2023
  plugin: Plugin;
1814
2024
  error: Error;
1815
2025
  }>;
2026
+ /**
2027
+ * `'success'` when all plugins completed without errors, `'failed'` otherwise.
2028
+ */
1816
2029
  status: 'success' | 'failed';
2030
+ /**
2031
+ * High-resolution start time from `process.hrtime()`.
2032
+ */
1817
2033
  hrStart: [number, number];
2034
+ /**
2035
+ * Total number of files created during this run.
2036
+ */
1818
2037
  filesCreated: number;
2038
+ /**
2039
+ * Elapsed milliseconds per plugin, keyed by plugin name.
2040
+ */
1819
2041
  pluginTimings?: Map<Plugin['name'], number>;
1820
2042
  };
1821
2043
  type KubbVersionNewContext = {
2044
+ /**
2045
+ * The installed Kubb version.
2046
+ */
1822
2047
  currentVersion: string;
2048
+ /**
2049
+ * The newest available version on npm.
2050
+ */
1823
2051
  latestVersion: string;
1824
2052
  };
1825
2053
  type KubbInfoContext = {
2054
+ /**
2055
+ * Human-readable info message.
2056
+ */
1826
2057
  message: string;
2058
+ /**
2059
+ * Optional supplementary detail.
2060
+ */
1827
2061
  info?: string;
1828
2062
  };
1829
2063
  type KubbErrorContext = {
2064
+ /**
2065
+ * The caught error.
2066
+ */
1830
2067
  error: Error;
2068
+ /**
2069
+ * Optional structured metadata for additional context.
2070
+ */
1831
2071
  meta?: Record<string, unknown>;
1832
2072
  };
1833
2073
  type KubbSuccessContext = {
2074
+ /**
2075
+ * Human-readable success message.
2076
+ */
1834
2077
  message: string;
2078
+ /**
2079
+ * Optional supplementary detail.
2080
+ */
1835
2081
  info?: string;
1836
2082
  };
1837
2083
  type KubbWarnContext = {
1838
- message: string;
1839
- info?: string;
1840
- };
1841
- type KubbDebugContext = {
1842
- date: Date;
1843
- logs: Array<string>;
1844
- fileName?: string;
1845
- };
1846
- type KubbFilesProcessingStartContext = {
1847
- files: Array<FileNode>;
1848
- };
1849
- type KubbFileProcessingUpdateContext = {
1850
2084
  /**
1851
- * Number of files processed.
2085
+ * Human-readable warning message.
1852
2086
  */
1853
- processed: number;
2087
+ message: string;
1854
2088
  /**
1855
- * Total files to process.
2089
+ * Optional supplementary detail.
1856
2090
  */
1857
- total: number;
2091
+ info?: string;
2092
+ };
2093
+ type KubbDebugContext = {
1858
2094
  /**
1859
- * Processing percentage (0–100).
2095
+ * Timestamp when the debug entry was created.
1860
2096
  */
1861
- percentage: number;
2097
+ date: Date;
1862
2098
  /**
1863
- * Optional source identifier.
2099
+ * One or more log lines to emit.
1864
2100
  */
1865
- source?: string;
2101
+ logs: Array<string>;
1866
2102
  /**
1867
- * The file being processed.
2103
+ * Optional source file name associated with this entry.
1868
2104
  */
1869
- file: FileNode;
2105
+ fileName?: string;
2106
+ };
2107
+ type KubbFilesProcessingStartContext = {
1870
2108
  /**
1871
- * The current build configuration.
2109
+ * Files about to be serialised and written.
1872
2110
  */
1873
- config: Config;
1874
- };
1875
- type KubbFilesProcessingEndContext = {
1876
2111
  files: Array<FileNode>;
1877
2112
  };
1878
- type KubbPluginStartContext = {
1879
- plugin: NormalizedPlugin;
1880
- };
1881
- type KubbPluginEndContext = {
1882
- plugin: NormalizedPlugin;
1883
- duration: number;
1884
- success: boolean;
1885
- error?: Error;
1886
- config: Config;
1887
- /**
1888
- * Returns all files currently in the file manager (lazy snapshot).
1889
- * Includes files added by plugins that have already run.
2113
+ type KubbFileProcessingUpdate = {
2114
+ /**
2115
+ * Number of files processed so far in this batch.
1890
2116
  */
1891
- readonly files: ReadonlyArray<FileNode>;
2117
+ processed: number;
1892
2118
  /**
1893
- * Upsert one or more files into the file manager.
2119
+ * Total number of files in this batch.
1894
2120
  */
1895
- upsertFile: (...files: Array<FileNode>) => void;
1896
- };
1897
- type KubbHookStartContext = {
1898
- id?: string;
1899
- command: string;
1900
- args?: readonly string[];
1901
- };
1902
- type KubbHookEndContext = {
1903
- id?: string;
1904
- command: string;
1905
- args?: readonly string[];
1906
- success: boolean;
1907
- error: Error | null;
1908
- };
1909
- type ByTag = {
2121
+ total: number;
1910
2122
  /**
1911
- * Filter by OpenAPI `tags` field. Matches one or more tags assigned to operations.
2123
+ * Completion percentage (`0`–`100`).
1912
2124
  */
1913
- type: 'tag';
2125
+ percentage: number;
1914
2126
  /**
1915
- * Tag name to match (case-sensitive). Can be a literal string or regex pattern.
2127
+ * Serialised file content, or `undefined` when the file produced no output.
1916
2128
  */
1917
- pattern: string | RegExp;
1918
- };
1919
- type ByOperationId = {
2129
+ source?: string;
1920
2130
  /**
1921
- * Filter by OpenAPI `operationId` field. Each operation (GET, POST, etc.) has a unique identifier.
2131
+ * The file that was just processed.
1922
2132
  */
1923
- type: 'operationId';
2133
+ file: FileNode;
1924
2134
  /**
1925
- * Operation ID to match (case-sensitive). Can be a literal string or regex pattern.
2135
+ * Resolved configuration for this build.
1926
2136
  */
1927
- pattern: string | RegExp;
2137
+ config: Config;
1928
2138
  };
1929
- type ByPath = {
2139
+ type KubbFilesProcessingUpdateContext = {
1930
2140
  /**
1931
- * Filter by OpenAPI `path` (URL endpoint). Useful to group or filter by service segments like `/pets`, `/users`, etc.
2141
+ * All files processed in this flush chunk.
1932
2142
  */
1933
- type: 'path';
2143
+ files: Array<KubbFileProcessingUpdate>;
2144
+ };
2145
+ type KubbFilesProcessingEndContext = {
1934
2146
  /**
1935
- * URL path to match (case-sensitive). Can be a literal string or regex pattern. Matches against the full path.
2147
+ * All files that were serialised in this batch.
1936
2148
  */
1937
- pattern: string | RegExp;
2149
+ files: Array<FileNode>;
1938
2150
  };
1939
- type ByMethod = {
2151
+ type KubbHookStartContext = {
1940
2152
  /**
1941
- * Filter by HTTP method: `'get'`, `'post'`, `'put'`, `'delete'`, `'patch'`, `'head'`, `'options'`.
2153
+ * Optional identifier for correlating start/end events.
1942
2154
  */
1943
- type: 'method';
2155
+ id?: string;
1944
2156
  /**
1945
- * HTTP method to match (case-insensitive when using string, or regex for dynamic matching).
2157
+ * The shell command that is about to run.
1946
2158
  */
1947
- pattern: HttpMethod | RegExp;
2159
+ command: string;
2160
+ /**
2161
+ * Parsed argument list, when available.
2162
+ */
2163
+ args?: readonly string[];
1948
2164
  };
1949
- type BySchemaName = {
2165
+ type KubbHookEndContext = {
1950
2166
  /**
1951
- * Filter by schema component name (TypeScript or JSON schema). Matches schemas in `#/components/schemas`.
2167
+ * Optional identifier matching the corresponding `kubb:hook:start` event.
1952
2168
  */
1953
- type: 'schemaName';
2169
+ id?: string;
1954
2170
  /**
1955
- * Schema name to match (case-sensitive). Can be a literal string or regex pattern.
2171
+ * The shell command that ran.
1956
2172
  */
1957
- pattern: string | RegExp;
1958
- };
1959
- type ByContentType = {
2173
+ command: string;
1960
2174
  /**
1961
- * Filter by response or request content type: `'application/json'`, `'application/xml'`, etc.
2175
+ * Parsed argument list, when available.
1962
2176
  */
1963
- type: 'contentType';
2177
+ args?: readonly string[];
1964
2178
  /**
1965
- * Content type to match (case-sensitive). Can be a literal string or regex pattern.
2179
+ * `true` when the command exited with code `0`.
1966
2180
  */
1967
- pattern: string | RegExp;
2181
+ success: boolean;
2182
+ /**
2183
+ * Error thrown by the command, or `null` on success.
2184
+ */
2185
+ error: Error | null;
1968
2186
  };
1969
2187
  /**
1970
- * A pattern filter that prevents matching nodes from being generated.
1971
- *
1972
- * Use to skip code generation for specific operations or schemas. For example, exclude deprecated endpoints
1973
- * or internal-only schemas. Can filter by tag, operationId, path, HTTP method, content type, or schema name.
1974
- *
1975
- * @example
1976
- * ```ts
1977
- * exclude: [
1978
- * { type: 'tag', pattern: 'internal' }, // skip "internal" tag
1979
- * { type: 'path', pattern: /^\/admin/ }, // skip all /admin endpoints
1980
- * { type: 'operationId', pattern: 'deprecated_*' } // skip operationIds matching pattern
1981
- * ]
1982
- * ```
1983
- */
1984
- type Exclude$1 = ByTag | ByOperationId | ByPath | ByMethod | ByContentType | BySchemaName;
1985
- /**
1986
- * A pattern filter that restricts generation to only matching nodes.
1987
- *
1988
- * Use to generate code for a subset of operations or schemas. For example, only generate for a specific service
1989
- * tag or only for "production" endpoints. Can filter by tag, operationId, path, HTTP method, content type, or schema name.
1990
- *
1991
- * @example
1992
- * ```ts
1993
- * include: [
1994
- * { type: 'tag', pattern: 'public' }, // generate only "public" tag
1995
- * { type: 'path', pattern: /^\/api\/v1/ }, // generate only v1 endpoints
1996
- * ]
1997
- * ```
2188
+ * CLI options derived from command-line flags.
1998
2189
  */
1999
- type Include = ByTag | ByOperationId | ByPath | ByMethod | ByContentType | BySchemaName;
2190
+ type CLIOptions = {
2191
+ /**
2192
+ * Path to the Kubb config file.
2193
+ */
2194
+ config?: string;
2195
+ /**
2196
+ * Re-run generation whenever input files change.
2197
+ */
2198
+ watch?: boolean;
2199
+ /**
2200
+ * Controls how much output the CLI prints.
2201
+ *
2202
+ * @default 'silent'
2203
+ */
2204
+ logLevel?: 'silent' | 'info' | 'debug';
2205
+ };
2000
2206
  /**
2001
- * A pattern filter paired with partial option overrides applied when the pattern matches.
2002
- *
2003
- * Use to customize generation for specific operations or schemas. For example, apply different output paths
2004
- * for different tags, or use custom resolver functions per operation. Can filter by tag, operationId, path,
2005
- * HTTP method, schema name, or content type.
2006
- *
2007
- * @example
2008
- * ```ts
2009
- * override: [
2010
- * {
2011
- * type: 'tag',
2012
- * pattern: 'admin',
2013
- * options: { output: { path: './src/gen/admin' } } // admin APIs go to separate folder
2014
- * },
2015
- * {
2016
- * type: 'operationId',
2017
- * pattern: 'listPets',
2018
- * options: { exclude: true } // skip this specific operation
2019
- * }
2020
- * ]
2021
- * ```
2207
+ * All accepted forms of a Kubb configuration.
2208
+ * Accepts `Config`/`Config[]`/promise or a factory (optionally receiving `TCliOptions`.
2022
2209
  */
2023
- type Override<TOptions> = (ByTag | ByOperationId | ByPath | ByMethod | BySchemaName | ByContentType) & {
2024
- options: Partial<TOptions>;
2025
- };
2210
+ type PossibleConfig<TCliOptions = undefined> = PossiblePromise<Config | Config[]> | ((...args: [TCliOptions] extends [undefined] ? [] : [TCliOptions]) => PossiblePromise<Config | Config[]>);
2026
2211
  /**
2027
- * File-specific parameters for `Resolver.resolvePath`.
2028
- *
2029
- * Pass alongside a `ResolverContext` to identify which file to resolve.
2030
- * Provide `tag` for tag-based grouping or `path` for path-based grouping.
2031
- *
2032
- * @example
2033
- * ```ts
2034
- * resolver.resolvePath(
2035
- * { baseName: 'petTypes.ts', tag: 'pets' },
2036
- * { root: '/src', output: { path: 'types' }, group: { type: 'tag' } },
2037
- * )
2038
- * // → '/src/types/petsController/petTypes.ts'
2039
- * ```
2212
+ * Full output produced by a successful or failed build.
2040
2213
  */
2041
- type ResolverPathParams = {
2042
- baseName: FileNode['baseName'];
2043
- pathMode?: 'single' | 'split';
2214
+ type BuildOutput = {
2044
2215
  /**
2045
- * Tag value used when `group.type === 'tag'`.
2216
+ * Plugins that threw during generation, paired with their errors.
2046
2217
  */
2047
- tag?: string;
2218
+ failedPlugins: Set<{
2219
+ plugin: Plugin;
2220
+ error: Error;
2221
+ }>;
2048
2222
  /**
2049
- * Path value used when `group.type === 'path'`.
2223
+ * All files generated during this build.
2050
2224
  */
2051
- path?: string;
2052
- };
2053
- /**
2054
- * Shared context passed as the second argument to `Resolver.resolvePath` and `Resolver.resolveFile`.
2055
- *
2056
- * Describes where on disk output is rooted, which output config is active, and the optional
2057
- * grouping strategy that controls subdirectory layout.
2058
- *
2059
- * @example
2060
- * ```ts
2061
- * const context: ResolverContext = {
2062
- * root: config.root,
2063
- * output,
2064
- * group,
2065
- * }
2066
- * ```
2067
- */
2068
- type ResolverContext = {
2069
- root: string;
2070
- output: Output;
2071
- group?: Group;
2225
+ files: Array<FileNode>;
2072
2226
  /**
2073
- * Plugin name used to populate `meta.pluginName` on the resolved file.
2227
+ * The plugin driver that orchestrated this build.
2074
2228
  */
2075
- pluginName?: string;
2076
- };
2077
- /**
2078
- * File-specific parameters for `Resolver.resolveFile`.
2079
- *
2080
- * Pass alongside a `ResolverContext` to fully describe the file to resolve.
2081
- * `tag` and `path` are used only when a matching `group` is present in the context.
2082
- *
2083
- * @example
2084
- * ```ts
2085
- * resolver.resolveFile(
2086
- * { name: 'listPets', extname: '.ts', tag: 'pets' },
2087
- * { root: '/src', output: { path: 'types' }, group: { type: 'tag' } },
2088
- * )
2089
- * // → { baseName: 'listPets.ts', path: '/src/types/petsController/listPets.ts', ... }
2090
- * ```
2091
- */
2092
- type ResolverFileParams = {
2093
- name: string;
2094
- extname: FileNode['extname'];
2229
+ driver: KubbDriver;
2095
2230
  /**
2096
- * Tag value used when `group.type === 'tag'`.
2231
+ * Elapsed milliseconds per plugin, keyed by plugin name.
2097
2232
  */
2098
- tag?: string;
2233
+ pluginTimings: Map<string, number>;
2099
2234
  /**
2100
- * Path value used when `group.type === 'path'`.
2235
+ * Top-level error when the build threw before completing, otherwise `undefined`.
2101
2236
  */
2102
- path?: string;
2237
+ error?: Error;
2238
+ /**
2239
+ * Read-only view of every file written during this build.
2240
+ * Reads go straight to `config.storage` — nothing extra is held in memory.
2241
+ *
2242
+ * @example Read a generated file
2243
+ * `const code = await buildOutput.storage.getItem('/src/gen/pet.ts')`
2244
+ *
2245
+ * @example List all generated file paths
2246
+ * `const paths = await buildOutput.storage.getKeys()`
2247
+ */
2248
+ storage: Storage;
2103
2249
  };
2104
2250
  /**
2105
- * Context passed to `Resolver.resolveBanner` and `Resolver.resolveFooter`.
2251
+ * Type guard to check if a given config has an `input.path`.
2252
+ */
2253
+ declare function isInputPath(config: UserConfig | undefined): config is UserConfig<InputPath> & {
2254
+ input: InputPath;
2255
+ };
2256
+ declare function isInputPath(config: Config | undefined): config is Config<InputPath> & {
2257
+ input: InputPath;
2258
+ };
2259
+ type CreateKubbOptions = {
2260
+ hooks?: AsyncEventEmitter<KubbHooks>;
2261
+ };
2262
+ /**
2263
+ * Kubb code-generation instance bound to a single config entry. Resolves the user
2264
+ * config during `setup()` and shares `hooks`, `storage`, `driver`, and `config` across
2265
+ * the `setup → build` lifecycle.
2106
2266
  *
2107
- * `output` is optional not every plugin configures a banner/footer.
2108
- * `config` carries the global Kubb config, used to derive the default Kubb banner.
2267
+ * Attach event listeners to `.hooks` before calling `setup()` or `build()`.
2109
2268
  *
2110
2269
  * @example
2111
2270
  * ```ts
2112
- * resolver.resolveBanner(inputNode, { output: { banner: '// generated' }, config })
2113
- * // '// generated'
2271
+ * const kubb = createKubb(userConfig)
2272
+ * kubb.hooks.on('kubb:plugin:end', ({ plugin, duration }) => console.log(plugin.name, duration))
2273
+ * const { files, failedPlugins } = await kubb.safeBuild()
2114
2274
  * ```
2115
2275
  */
2116
- type ResolveBannerContext = {
2117
- output?: Pick<Output, 'banner' | 'footer'>;
2118
- config: Config;
2119
- };
2120
- /**
2121
- * CLI options derived from command-line flags.
2122
- */
2123
- type CLIOptions = {
2276
+ declare class Kubb$1 {
2277
+ #private;
2278
+ readonly hooks: AsyncEventEmitter<KubbHooks>;
2279
+ constructor(userConfig: UserConfig, options?: CreateKubbOptions);
2280
+ get storage(): Storage;
2281
+ get driver(): KubbDriver;
2282
+ get config(): Config;
2124
2283
  /**
2125
- * Path to `kubb.config.js`.
2284
+ * Resolves config and initializes the driver. `build()` calls this automatically.
2126
2285
  */
2127
- config?: string;
2286
+ setup(): Promise<void>;
2128
2287
  /**
2129
- * Enable watch mode for input files.
2288
+ * Runs the full pipeline and throws on any plugin error.
2289
+ * Automatically calls `setup()` if needed.
2130
2290
  */
2131
- watch?: boolean;
2291
+ build(): Promise<BuildOutput>;
2132
2292
  /**
2133
- * Logging verbosity for CLI usage.
2134
- * @default 'silent'
2293
+ * Runs the full pipeline and captures errors in `BuildOutput` instead of throwing.
2294
+ * Automatically calls `setup()` if needed.
2135
2295
  */
2136
- logLevel?: 'silent' | 'info' | 'debug';
2137
- };
2296
+ safeBuild(): Promise<BuildOutput>;
2297
+ dispose(): void;
2298
+ [Symbol.dispose](): void;
2299
+ }
2138
2300
  /**
2139
- * All accepted forms of a Kubb configuration.
2140
- *
2141
- * Config is always `@kubb/core` {@link Config}.
2142
- * - `PossibleConfig` accepts `Config`/`Config[]`/promise or a no-arg config factory.
2143
- * - `PossibleConfig<TCliOptions>` accepts the same config forms or a config factory receiving `TCliOptions`.
2301
+ * Factory for {@link Kubb}. Equivalent to `new Kubb(userConfig, options)` and kept
2302
+ * as the canonical public entry point.
2144
2303
  */
2145
- type PossibleConfig<TCliOptions = undefined> = PossiblePromise<Config | Config[]> | ((...args: [TCliOptions] extends [undefined] ? [] : [TCliOptions]) => PossiblePromise<Config | Config[]>);
2304
+ declare function createKubb(userConfig: UserConfig, options?: CreateKubbOptions): Kubb$1;
2146
2305
  //#endregion
2147
- 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 };
2148
- //# sourceMappingURL=types-CC09VtBt.d.ts.map
2306
+ export { ResolverFileParams as $, createKubb as A, KubbPluginEndContext as B, KubbLifecycleStartContext as C, createAdapter as Ct, KubbWarnContext as D, KubbVersionNewContext as E, KubbDriver as F, Override as G, KubbPluginStartContext as H, FileManager as I, definePlugin as J, Plugin as K, Exclude as L, Generator$1 as M, GeneratorContext as N, PossibleConfig as O, defineGenerator as P, ResolverContext as Q, Group as R, KubbInfoContext as S, AdapterSource as St, KubbSuccessContext as T, AsyncEventEmitter as Tt, NormalizedPlugin as U, KubbPluginSetupContext as V, Output as W, ResolveOptionsContext as X, ResolveBannerContext as Y, Resolver as Z, KubbGenerationStartContext as _, Renderer as _t, InputPath as a, LoggerContext as at, KubbHookStartContext as b, Adapter as bt, KubbBuildStartContext as c, defineLogger as ct, KubbErrorContext as d, ParsedFile as dt, ResolverPathParams as et, KubbFileProcessingUpdate as f, Parser as ft, KubbGenerationEndContext as g, createStorage as gt, KubbFilesProcessingUpdateContext as h, Storage as ht, InputData as i, Logger as it, isInputPath as j, UserConfig as k, KubbConfigEndContext as l, FileProcessor as lt, KubbFilesProcessingStartContext as m, DevtoolsOptions as mt, CLIOptions as n, Middleware as nt, Kubb$1 as o, LoggerOptions as ot, KubbFilesProcessingEndContext as p, defineParser as pt, PluginFactoryOptions as q, Config as r, defineMiddleware as rt, KubbBuildEndContext as s, UserLogger as st, BuildOutput as t, defineResolver as tt, KubbDebugContext as u, FileProcessorEvents as ut, KubbGenerationSummaryContext as v, RendererFactory as vt, KubbPluginsEndContext as w, logLevel as wt, KubbHooks as x, AdapterFactoryOptions as xt, KubbHookEndContext as y, createRenderer as yt, Include as z };
2307
+ //# sourceMappingURL=createKubb-CYrw_xaR.d.ts.map