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