@kubb/core 5.0.0-alpha.43 → 5.0.0-alpha.44

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/src/types.ts CHANGED
@@ -1,11 +1,11 @@
1
1
  import type { AsyncEventEmitter, PossiblePromise } from '@internals/utils'
2
- import type { FileNode, HttpMethod, ImportNode, InputNode, Node, OperationNode, SchemaNode, Visitor } from '@kubb/ast'
2
+ import type { FileNode, HttpMethod, ImportNode, InputNode, Node, SchemaNode, Visitor } from '@kubb/ast'
3
3
  import type { DEFAULT_STUDIO_URL, logLevel } from './constants.ts'
4
4
  import type { RendererFactory } from './createRenderer.ts'
5
5
  import type { Storage } from './createStorage.ts'
6
6
  import type { Generator } from './defineGenerator.ts'
7
7
  import type { Parser } from './defineParser.ts'
8
- import type { HookStylePlugin } from './definePlugin.ts'
8
+ import type { Plugin } from './definePlugin.ts'
9
9
  import type { KubbHooks } from './Kubb.ts'
10
10
  import type { PluginDriver } from './PluginDriver.ts'
11
11
 
@@ -314,6 +314,11 @@ type PatternOverride<TOptions> = PatternFilter & {
314
314
  * Context passed to `resolver.resolveOptions` to apply include/exclude/override filtering
315
315
  * for a given operation or schema node.
316
316
  */
317
+ /**
318
+ * Resolves filtered options for a given operation or schema node.
319
+ *
320
+ * @internal
321
+ */
317
322
  export type ResolveOptionsContext<TOptions> = {
318
323
  options: TOptions
319
324
  exclude?: Array<PatternFilter>
@@ -339,7 +344,7 @@ export type ResolveOptionsContext<TOptions> = {
339
344
  export type Resolver = {
340
345
  name: string
341
346
  pluginName: Plugin['name']
342
- default(name: ResolveNameParams['name'], type?: ResolveNameParams['type']): string
347
+ default(name: string, type?: 'file' | 'function' | 'type' | 'const'): string
343
348
  resolveOptions<TOptions>(node: Node, context: ResolveOptionsContext<TOptions>): TOptions | null
344
349
  resolvePath(params: ResolverPathParams, context: ResolverContext): string
345
350
  resolveFile(params: ResolverFileParams, context: ResolverContext): FileNode
@@ -383,97 +388,26 @@ export type PluginFactoryOptions<
383
388
  }
384
389
 
385
390
  /**
386
- * @deprecated
391
+ * Internal representation of a plugin after normalization.
392
+ * Extends the user-facing `Plugin` with runtime fields populated during `kubb:plugin:setup`.
393
+ * Not part of the public API — use `Plugin` for external-facing interactions.
394
+ * @internal
387
395
  */
388
- export type UserPlugin<TOptions extends PluginFactoryOptions = PluginFactoryOptions> = {
389
- /**
390
- * Unique name used for the plugin.
391
- * The name follows the format `scope:foo-bar` or `foo-bar` — adding a scope avoids conflicts with other plugins.
392
- *
393
- * @example Plugin name
394
- * `'@kubb/typescript'`
395
- */
396
- name: TOptions['name']
397
- /**
398
- * Resolved options merged with output/include/exclude/override defaults for the current plugin.
399
- */
396
+ export type NormalizedPlugin<TOptions extends PluginFactoryOptions = PluginFactoryOptions> = Plugin<TOptions> & {
400
397
  options: TOptions['resolvedOptions'] & {
401
398
  output: Output
402
399
  include?: Array<Include>
403
400
  exclude: Array<Exclude>
404
401
  override: Array<Override<TOptions['resolvedOptions']>>
405
402
  }
406
- /**
407
- * The resolver for this plugin.
408
- * Set via `setResolver()` in `kubb:plugin:setup` or passed as a user option.
409
- */
410
- resolver?: TOptions['resolver']
411
- /**
412
- * The composed transformer for this plugin.
413
- * Set via `setTransformer()` in `kubb:plugin:setup` or passed as a user option.
414
- */
403
+ resolver: TOptions['resolver']
415
404
  transformer?: Visitor
416
- /**
417
- * Plugin-level renderer factory. All generators that do not declare their own `renderer`
418
- * inherit this value. A generator can explicitly opt out by setting `renderer: null`.
419
- *
420
- * @example
421
- * ```ts
422
- * import { jsxRenderer } from '@kubb/renderer-jsx'
423
- * createPlugin((options) => ({
424
- * name: 'my-plugin',
425
- * renderer: jsxRenderer,
426
- * generators: [
427
- * { name: 'types', schema(node) { return <File>...</File> } }, // inherits jsxRenderer
428
- * { name: 'raw', renderer: null, schema(node) { return [...] } }, // explicit opt-out
429
- * ],
430
- * }))
431
- * ```
432
- */
433
405
  renderer?: RendererFactory
434
- /**
435
- * Generators declared directly on the plugin. Each generator's `renderer` takes precedence
436
- * over `plugin.renderer`; set `renderer: null` on a generator to opt out of rendering even
437
- * when the plugin declares a renderer.
438
- */
439
406
  generators?: Array<Generator>
440
- /**
441
- * Specifies the plugins that the current plugin depends on. The current plugin is executed after all listed plugins.
442
- * An error is returned if any required dependency plugin is missing.
443
- */
444
- dependencies?: Array<string>
445
- /**
446
- * When `apply` is defined, the plugin is only activated when `apply(config)` returns `true`.
447
- * Inspired by Vite's `apply` option.
448
- *
449
- * @example
450
- * ```ts
451
- * apply: (config) => config.output.path !== 'disabled'
452
- * ```
453
- */
454
407
  apply?: (config: Config) => boolean
455
- /**
456
- * Expose shared helpers or data to all other plugins via `PluginContext`.
457
- * The object returned is merged into the context that every plugin receives.
458
- * Use the `declare global { namespace Kubb { interface PluginContext { … } } }` pattern
459
- * to make the injected properties type-safe.
460
- *
461
- * @example
462
- * ```ts
463
- * inject() {
464
- * return { getOas: () => parseSpec(this.config) }
465
- * }
466
- * // Other plugins can then call `this.getOas()` inside buildStart()
467
- * ```
468
- */
469
- inject?: (this: PluginContext<TOptions>) => TOptions['context']
408
+ version?: string
470
409
  }
471
410
 
472
- /**
473
- * @deprecated
474
- */
475
- export type UserPluginWithLifeCycle<TOptions extends PluginFactoryOptions = PluginFactoryOptions> = UserPlugin<TOptions> & PluginLifecycle<TOptions>
476
-
477
411
  /**
478
412
  * Partial version of {@link Config} intended for user-facing config entry points.
479
413
  *
@@ -497,207 +431,10 @@ export type UserConfig<TInput = Input> = Omit<Config<TInput>, 'root' | 'plugins'
497
431
  */
498
432
  adapter?: Adapter
499
433
  /**
500
- * User-facing plugins can be either legacy createPlugin instances or hook-style plugins.
501
- */
502
- plugins?: Array<Omit<UserPlugin<any>, 'inject'> | HookStylePlugin<any>>
503
- }
504
-
505
- /**
506
- * Handler for a single schema node. Used by the `schema` hook on a plugin.
507
- */
508
- export type SchemaHook<TOptions extends PluginFactoryOptions = PluginFactoryOptions> = (
509
- this: GeneratorContext<TOptions>,
510
- node: SchemaNode,
511
- options: TOptions['resolvedOptions'],
512
- ) => PossiblePromise<unknown | Array<FileNode> | void>
513
-
514
- /**
515
- * Handler for a single operation node. Used by the `operation` hook on a plugin.
516
- */
517
- export type OperationHook<TOptions extends PluginFactoryOptions = PluginFactoryOptions> = (
518
- this: GeneratorContext<TOptions>,
519
- node: OperationNode,
520
- options: TOptions['resolvedOptions'],
521
- ) => PossiblePromise<unknown | Array<FileNode> | void>
522
-
523
- /**
524
- * Handler for all collected operation nodes. Used by the `operations` hook on a plugin.
525
- */
526
- export type OperationsHook<TOptions extends PluginFactoryOptions = PluginFactoryOptions> = (
527
- this: GeneratorContext<TOptions>,
528
- nodes: Array<OperationNode>,
529
- options: TOptions['resolvedOptions'],
530
- ) => PossiblePromise<unknown | Array<FileNode> | void>
531
- /**
532
- * @deprecated will be replaced with HookStylePlugin
533
- */
534
- export type Plugin<TOptions extends PluginFactoryOptions = PluginFactoryOptions> = {
535
- /**
536
- * Unique name used for the plugin.
537
- *
538
- * @example Plugin name
539
- * `'@kubb/typescript'`
540
- */
541
- name: TOptions['name']
542
- /**
543
- * Specifies the plugins that the current plugin depends on. The current plugin is executed after all listed plugins.
544
- * An error is returned if any required dependency plugin is missing.
545
- */
546
- dependencies?: Array<string>
547
- /**
548
- * Options set for a specific plugin(see kubb.config.js), passthrough of options.
549
- */
550
- options: TOptions['resolvedOptions'] & {
551
- output: Output
552
- include?: Array<Include>
553
- exclude: Array<Exclude>
554
- override: Array<Override<TOptions['resolvedOptions']>>
555
- }
556
- /**
557
- * The resolver for this plugin.
558
- * Set via `setResolver()` in `kubb:plugin:setup` or passed as a user option.
559
- */
560
- resolver: TOptions['resolver']
561
- /**
562
- * The composed transformer for this plugin.
563
- * Set via `setTransformer()` in `kubb:plugin:setup` or passed as a user option.
564
- */
565
- transformer?: Visitor
566
-
567
- /**
568
- * When `apply` is defined, the plugin is only activated when `apply(config)` returns `true`.
569
- * Inspired by Vite's `apply` option.
570
- */
571
- apply?: (config: Config) => boolean
572
- /**
573
- * Optional semver version string for this plugin, e.g. `"1.2.3"`.
574
- * Used in diagnostic messages and version-conflict detection.
575
- */
576
- version?: string
577
- /**
578
- * Plugin-level renderer factory. All generators that do not declare their own `renderer`
579
- * inherit this value. A generator can explicitly opt out by setting `renderer: null`.
580
- */
581
- renderer?: RendererFactory
582
- /**
583
- * Generators declared directly on the plugin. Each generator's `renderer` takes precedence
584
- * over `plugin.renderer`; set `renderer: null` on a generator to opt out of rendering even
585
- * when the plugin declares a renderer.
586
- */
587
- generators?: Array<Generator>
588
-
589
- buildStart: (this: PluginContext<TOptions>) => PossiblePromise<void>
590
- /**
591
- * Called once per plugin after all files have been written to disk.
592
- * Use this for post-processing, copying assets, or generating summary reports.
593
- */
594
- buildEnd: (this: PluginContext<TOptions>) => PossiblePromise<void>
595
- /**
596
- * Called for each schema node during the AST walk.
597
- * Return a React element, an array of `FileNode`, or `void` for manual handling.
598
- * Nodes matching `exclude`/`include` filters are skipped automatically.
599
- *
600
- * For multiple generators, use `composeGenerators` inside the plugin factory.
601
- */
602
- schema?: SchemaHook<TOptions>
603
- /**
604
- * Called for each operation node during the AST walk.
605
- * Return a React element, an array of `FileNode`, or `void` for manual handling.
606
- *
607
- * For multiple generators, use `composeGenerators` inside the plugin factory.
608
- */
609
- operation?: OperationHook<TOptions>
610
- /**
611
- * Called once after all operations have been walked, with the full collected set.
612
- *
613
- * For multiple generators, use `composeGenerators` inside the plugin factory.
614
- */
615
- operations?: OperationsHook<TOptions>
616
- /**
617
- * Expose shared helpers or data to all other plugins via `PluginContext`.
618
- * The returned object is merged into the context received by every plugin.
619
- */
620
- inject: (this: PluginContext<TOptions>) => TOptions['context']
621
- }
622
- /**
623
- * @deprecated
624
- */
625
- export type PluginWithLifeCycle<TOptions extends PluginFactoryOptions = PluginFactoryOptions> = Plugin<TOptions> & PluginLifecycle<TOptions>
626
- /**
627
- * @deprecated
628
- */
629
- export type PluginLifecycle<TOptions extends PluginFactoryOptions = PluginFactoryOptions> = {
630
- /**
631
- * Called once per plugin at the start of its processing phase, before schema/operation/operations hooks run.
632
- * Use this to set up shared state, fetch remote data, or perform any async initialization.
633
- */
634
- buildStart?: (this: PluginContext<TOptions>) => PossiblePromise<void>
635
- /**
636
- * Called once per plugin after all files have been written to disk.
637
- * Use this for post-processing, copying assets, or generating summary reports.
638
- */
639
- buildEnd?: (this: PluginContext<TOptions>) => PossiblePromise<void>
640
- /**
641
- * Called for each schema node during the AST walk.
642
- * Return a React element (`<File>...</File>`), an array of `FileNode` objects,
643
- * or `void` to handle file writing manually via `this.upsertFile`.
644
- * Nodes matching `exclude` / `include` filters are skipped automatically.
645
- *
646
- * For multiple generators, use `composeGenerators` inside the plugin factory.
647
- */
648
- schema?: SchemaHook<TOptions>
649
- /**
650
- * Called for each operation node during the AST walk.
651
- * Return a React element (`<File>...</File>`), an array of `FileNode` objects,
652
- * or `void` to handle file writing manually via `this.upsertFile`.
653
- *
654
- * For multiple generators, use `composeGenerators` inside the plugin factory.
655
- */
656
- operation?: OperationHook<TOptions>
657
- /**
658
- * Called once after all operation nodes have been walked, with the full collection.
659
- * Useful for generating index/barrel files per group or aggregate operation handlers.
660
- *
661
- * For multiple generators, use `composeGenerators` inside the plugin factory.
662
- */
663
- operations?: OperationsHook<TOptions>
664
- /**
665
- * Resolves a path from a baseName and directory.
666
- * Options can also be included.
667
- *
668
- * @example
669
- * `('./Pet.ts', './src/gen/') => '/src/gen/Pet.ts'`
670
- *
671
- * @deprecated Use resolvers instead.
672
- */
673
- resolvePath?: (this: PluginContext<TOptions>, baseName: FileNode['baseName'], mode?: 'single' | 'split', options?: TOptions['resolvePathOptions']) => string
674
- /**
675
- * Resolves a display name from a raw string.
676
- * Useful when converting to PascalCase or camelCase.
677
- *
678
- * @example
679
- * `('pet') => 'Pet'`
680
- *
681
- * @deprecated Use resolvers instead.
682
- */
683
- resolveName?: (this: PluginContext<TOptions>, name: ResolveNameParams['name'], type?: ResolveNameParams['type']) => string
684
- }
685
-
686
- /**
687
- * @deprecated
688
- */
689
- export type PluginLifecycleHooks = keyof PluginLifecycle
690
-
691
- export type PluginParameter<H extends PluginLifecycleHooks> = Parameters<Required<PluginLifecycle>[H]>
692
-
693
- export type ResolvePathParams<TOptions = object> = {
694
- pluginName?: string
695
- baseName: FileNode['baseName']
696
- mode?: 'single' | 'split'
697
- /**
698
- * Options passed as the third argument to `resolvePath`.
434
+ * An array of Kubb plugins used for code generation.
435
+ * Each entry is a hook-style plugin created with `definePlugin`.
699
436
  */
700
- options?: TOptions
437
+ plugins?: Array<Plugin>
701
438
  }
702
439
 
703
440
  export type ResolveNameParams = {
@@ -713,9 +450,17 @@ export type ResolveNameParams = {
713
450
  type?: 'file' | 'function' | 'type' | 'const'
714
451
  }
715
452
  /**
716
- * @deprecated
453
+ * Context object passed as the second argument to generator `schema`, `operation`, and
454
+ * `operations` methods.
455
+ *
456
+ * Generators are only invoked from `runPluginAstHooks`, which already guards against a
457
+ * missing adapter. This type reflects that guarantee — `ctx.adapter` and `ctx.inputNode`
458
+ * are always defined, so no runtime checks or casts are needed inside generator bodies.
459
+ *
460
+ * `ctx.options` carries the per-node resolved options for `schema`/`operation` calls
461
+ * (after exclude/include/override filtering) and the plugin-level options for `operations`.
717
462
  */
718
- export type PluginContext<TOptions extends PluginFactoryOptions = PluginFactoryOptions> = {
463
+ export type GeneratorContext<TOptions extends PluginFactoryOptions = PluginFactoryOptions> = {
719
464
  config: Config
720
465
  /**
721
466
  * Absolute path to the output directory for the current plugin.
@@ -725,7 +470,6 @@ export type PluginContext<TOptions extends PluginFactoryOptions = PluginFactoryO
725
470
  /**
726
471
  * Returns the output mode for the given output config.
727
472
  * Returns `'single'` when `output.path` has a file extension, `'split'` otherwise.
728
- * Shorthand for `PluginDriver.getMode(path.resolve(this.root, output.path))`.
729
473
  */
730
474
  getMode: (output: { path: string }) => 'single' | 'split'
731
475
  driver: PluginDriver
@@ -737,7 +481,6 @@ export type PluginContext<TOptions extends PluginFactoryOptions = PluginFactoryO
737
481
  getPlugin(name: string): Plugin | undefined
738
482
  /**
739
483
  * Like `getPlugin` but throws a descriptive error when the plugin is not found.
740
- * Useful for enforcing dependencies inside `buildStart()`.
741
484
  */
742
485
  requirePlugin<TName extends keyof Kubb.PluginRegistry>(name: TName): Plugin<Kubb.PluginRegistry[TName]>
743
486
  requirePlugin(name: string): Plugin
@@ -755,69 +498,40 @@ export type PluginContext<TOptions extends PluginFactoryOptions = PluginFactoryO
755
498
  */
756
499
  plugin: Plugin<TOptions>
757
500
  /**
758
- * Resolver for the current plugin. Shorthand for `plugin.resolver`.
501
+ * Resolver for the current plugin.
759
502
  */
760
503
  resolver: TOptions['resolver']
761
504
  /**
762
- * Composed transformer for the current plugin. Shorthand for `plugin.transformer`.
763
- * Apply with `transform(node, context.transformer)` to pre-process AST nodes before printing.
505
+ * Composed transformer for the current plugin.
764
506
  */
765
507
  transformer: Visitor | undefined
766
-
767
508
  /**
768
509
  * Emit a warning via the build event system.
769
- * Shorthand for `this.hooks.emit('kubb:warn', message)`.
770
510
  */
771
511
  warn: (message: string) => void
772
512
  /**
773
513
  * Emit an error via the build event system.
774
- * Shorthand for `this.hooks.emit('kubb:error', error)`.
775
514
  */
776
515
  error: (error: string | Error) => void
777
516
  /**
778
517
  * Emit an info message via the build event system.
779
- * Shorthand for `this.hooks.emit('kubb:info', message)`.
780
518
  */
781
519
  info: (message: string) => void
782
520
  /**
783
521
  * Opens the Kubb Studio URL for the current `inputNode` in the default browser.
784
- * Falls back to printing the URL if the browser cannot be launched.
785
- * No-ops silently when no adapter has set an `inputNode`.
786
522
  */
787
523
  openInStudio: (options?: DevtoolsOptions) => Promise<void>
788
- } & (
789
- | {
790
- /**
791
- * Returns the universal `@kubb/ast` `InputNode` produced by the configured adapter.
792
- * Returns `undefined` when no adapter was set (legacy OAS-only usage).
793
- */
794
- inputNode: InputNode
795
- /**
796
- * The adapter from `@kubb/ast`.
797
- */
798
- adapter: Adapter
799
- }
800
- | {
801
- inputNode?: never
802
- adapter?: never
803
- }
804
- ) &
805
- Kubb.PluginContext
806
-
807
- /**
808
- * Context object passed as the second argument to generator `schema`, `operation`, and
809
- * `operations` methods.
810
- *
811
- * Generators are only invoked from `runPluginAstHooks`, which already guards against a
812
- * missing adapter. This type reflects that guarantee — `ctx.adapter` and `ctx.inputNode`
813
- * are always defined, so no runtime checks or casts are needed inside generator bodies.
814
- *
815
- * `ctx.options` carries the per-node resolved options for `schema`/`operation` calls
816
- * (after exclude/include/override filtering) and the plugin-level options for `operations`.
817
- */
818
- export type GeneratorContext<TOptions extends PluginFactoryOptions = PluginFactoryOptions> = Omit<PluginContext<TOptions>, 'adapter' | 'inputNode'> & {
524
+ /**
525
+ * The adapter from `@kubb/ast`.
526
+ */
819
527
  adapter: Adapter
528
+ /**
529
+ * The universal `@kubb/ast` `InputNode` produced by the configured adapter.
530
+ */
820
531
  inputNode: InputNode
532
+ /**
533
+ * Per-node resolved options (after exclude/include/override filtering).
534
+ */
821
535
  options: TOptions['resolvedOptions']
822
536
  }
823
537
  /**
@@ -896,16 +610,9 @@ export type Logger<TOptions extends LoggerOptions = LoggerOptions> = {
896
610
 
897
611
  export type UserLogger<TOptions extends LoggerOptions = LoggerOptions> = Logger<TOptions>
898
612
 
899
- /**
900
- * Compatibility preset for code generation tools.
901
- * - `'default'` – no compatibility adjustments (default behavior).
902
- * - `'kubbV4'` – align generated names and structures with Kubb v4 output.
903
- */
904
- export type CompatibilityPreset = 'default' | 'kubbV4'
905
-
906
613
  export type { Storage } from './createStorage.ts'
907
614
  export type { Generator } from './defineGenerator.ts'
908
- export type { HookStylePlugin, PluginHooks } from './definePlugin.ts'
615
+ export type { Plugin } from './definePlugin.ts'
909
616
  export type { Kubb, KubbHooks } from './Kubb.ts'
910
617
 
911
618
  /**
@@ -1040,15 +747,6 @@ export type Override<TOptions> = (ByTag | ByOperationId | ByPath | ByMethod | By
1040
747
  options: Partial<TOptions>
1041
748
  }
1042
749
 
1043
- export type ResolvePathOptions = {
1044
- pluginName?: string
1045
- group?: {
1046
- tag?: string
1047
- path?: string
1048
- }
1049
- type?: ResolveNameParams['type']
1050
- }
1051
-
1052
750
  /**
1053
751
  * File-specific parameters for `Resolver.resolvePath`.
1054
752
  *
@@ -3,9 +3,15 @@ import { join } from 'node:path'
3
3
  import { getRelativePath } from '@internals/utils'
4
4
  import type { FileNode } from '@kubb/ast'
5
5
  import { createExport, createFile, createSource } from '@kubb/ast'
6
+ import { BARREL_BASENAME, BARREL_FILENAME } from '../constants.ts'
6
7
  import type { BarrelType } from '../types.ts'
7
8
  import { TreeNode } from './TreeNode.ts'
8
9
 
10
+ /**
11
+ * Minimal file metadata attached to every generated file for barrel-file bookkeeping.
12
+ *
13
+ * @internal
14
+ */
9
15
  export type FileMetaBase = {
10
16
  pluginName?: string
11
17
  }
@@ -38,10 +44,10 @@ function getBarrelFilesByRoot(root: string | undefined, files: Array<FileNode>):
38
44
  return
39
45
  }
40
46
 
41
- const barrelFilePath = join(treeNode.parent?.data.path, 'index.ts')
47
+ const barrelFilePath = join(treeNode.parent?.data.path, BARREL_FILENAME)
42
48
  const barrelFile = createFile({
43
49
  path: barrelFilePath,
44
- baseName: 'index.ts',
50
+ baseName: BARREL_FILENAME,
45
51
  exports: [],
46
52
  imports: [],
47
53
  sources: [],
@@ -123,7 +129,7 @@ export async function getBarrelFiles(files: Array<FileNode>, { type, meta = {},
123
129
 
124
130
  const pathToBuildFrom = join(root, output.path)
125
131
 
126
- if (trimExtName(pathToBuildFrom).endsWith('index')) {
132
+ if (trimExtName(pathToBuildFrom).endsWith(BARREL_BASENAME)) {
127
133
  return []
128
134
  }
129
135