@kubb/core 5.0.0-alpha.6 → 5.0.0-alpha.60

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (72) hide show
  1. package/README.md +3 -2
  2. package/dist/PluginDriver-Bc0HQM8V.js +948 -0
  3. package/dist/PluginDriver-Bc0HQM8V.js.map +1 -0
  4. package/dist/PluginDriver-Dyl2fwfQ.cjs +1039 -0
  5. package/dist/PluginDriver-Dyl2fwfQ.cjs.map +1 -0
  6. package/dist/index.cjs +691 -1798
  7. package/dist/index.cjs.map +1 -1
  8. package/dist/index.d.ts +279 -265
  9. package/dist/index.js +678 -1765
  10. package/dist/index.js.map +1 -1
  11. package/dist/mocks.cjs +138 -0
  12. package/dist/mocks.cjs.map +1 -0
  13. package/dist/mocks.d.ts +74 -0
  14. package/dist/mocks.js +133 -0
  15. package/dist/mocks.js.map +1 -0
  16. package/dist/types-i0b4_23K.d.ts +1903 -0
  17. package/package.json +51 -57
  18. package/src/FileManager.ts +110 -0
  19. package/src/FileProcessor.ts +86 -0
  20. package/src/Kubb.ts +205 -130
  21. package/src/PluginDriver.ts +424 -0
  22. package/src/constants.ts +20 -47
  23. package/src/createAdapter.ts +25 -0
  24. package/src/createKubb.ts +527 -0
  25. package/src/createRenderer.ts +57 -0
  26. package/src/createStorage.ts +58 -0
  27. package/src/defineGenerator.ts +88 -100
  28. package/src/defineLogger.ts +13 -3
  29. package/src/defineMiddleware.ts +59 -0
  30. package/src/defineParser.ts +45 -0
  31. package/src/definePlugin.ts +78 -7
  32. package/src/defineResolver.ts +521 -0
  33. package/src/devtools.ts +14 -14
  34. package/src/index.ts +13 -17
  35. package/src/mocks.ts +171 -0
  36. package/src/renderNode.ts +35 -0
  37. package/src/storages/fsStorage.ts +40 -11
  38. package/src/storages/memoryStorage.ts +4 -3
  39. package/src/types.ts +738 -218
  40. package/src/utils/diagnostics.ts +4 -1
  41. package/src/utils/isInputPath.ts +10 -0
  42. package/src/utils/packageJSON.ts +99 -0
  43. package/dist/PluginManager-vZodFEMe.d.ts +0 -1056
  44. package/dist/chunk-ByKO4r7w.cjs +0 -38
  45. package/dist/hooks.cjs +0 -60
  46. package/dist/hooks.cjs.map +0 -1
  47. package/dist/hooks.d.ts +0 -64
  48. package/dist/hooks.js +0 -56
  49. package/dist/hooks.js.map +0 -1
  50. package/src/BarrelManager.ts +0 -74
  51. package/src/PackageManager.ts +0 -180
  52. package/src/PluginManager.ts +0 -667
  53. package/src/PromiseManager.ts +0 -40
  54. package/src/build.ts +0 -419
  55. package/src/config.ts +0 -56
  56. package/src/defineAdapter.ts +0 -22
  57. package/src/defineStorage.ts +0 -56
  58. package/src/errors.ts +0 -1
  59. package/src/hooks/index.ts +0 -4
  60. package/src/hooks/useKubb.ts +0 -55
  61. package/src/hooks/useMode.ts +0 -11
  62. package/src/hooks/usePlugin.ts +0 -11
  63. package/src/hooks/usePluginManager.ts +0 -11
  64. package/src/utils/FunctionParams.ts +0 -155
  65. package/src/utils/TreeNode.ts +0 -215
  66. package/src/utils/executeStrategies.ts +0 -81
  67. package/src/utils/formatters.ts +0 -56
  68. package/src/utils/getBarrelFiles.ts +0 -79
  69. package/src/utils/getConfigs.ts +0 -30
  70. package/src/utils/getPlugins.ts +0 -23
  71. package/src/utils/linters.ts +0 -25
  72. package/src/utils/resolveOptions.ts +0 -93
@@ -1,106 +1,94 @@
1
- import type { OperationNode, SchemaNode } from '@kubb/ast/types'
2
- import type { KubbFile } from '@kubb/fabric-core/types'
3
- import type { FabricReactNode } from '@kubb/react-fabric/types'
4
- import type { Adapter, Config, Plugin, PluginFactoryOptions } from './types.ts'
1
+ import type { PossiblePromise } from '@internals/utils'
2
+ import type { FileNode, OperationNode, SchemaNode } from '@kubb/ast'
3
+ import type { RendererFactory } from './createRenderer.ts'
4
+ import type { GeneratorContext, PluginFactoryOptions } from './types.ts'
5
5
 
6
- export type Version = '1' | '2'
6
+ export type { GeneratorContext } from './types.ts'
7
7
 
8
- // V2 props — fully typed with @kubb/ast (already a @kubb/core dependency)
9
- export type OperationsV2Props<TPlugin extends PluginFactoryOptions = PluginFactoryOptions> = {
10
- config: Config
11
- adapter: Adapter
12
- options: Plugin<TPlugin>['options']
13
- nodes: Array<OperationNode>
14
- }
15
-
16
- export type OperationV2Props<TPlugin extends PluginFactoryOptions = PluginFactoryOptions> = {
17
- config: Config
18
- adapter: Adapter
19
- options: Plugin<TPlugin>['options']
20
- node: OperationNode
21
- }
22
-
23
- export type SchemaV2Props<TPlugin extends PluginFactoryOptions = PluginFactoryOptions> = {
24
- config: Config
25
- adapter: Adapter
26
- options: Plugin<TPlugin>['options']
27
- node: SchemaNode
28
- }
29
-
30
- type UserCoreGeneratorV2<TPlugin extends PluginFactoryOptions> = {
8
+ /**
9
+ * A generator is a named object with optional `schema`, `operation`, and `operations`
10
+ * methods. Each method receives the AST node as the first argument and a typed
11
+ * `ctx` object as the second, giving access to `ctx.config`, `ctx.resolver`,
12
+ * `ctx.adapter`, `ctx.options`, `ctx.upsertFile`, etc.
13
+ *
14
+ * Generators that return renderer elements (e.g. JSX) must declare a `renderer`
15
+ * factory so that core knows how to process the output without coupling core
16
+ * to any specific renderer package.
17
+ *
18
+ * Return a renderer element, an array of `FileNode`, or `void` to handle file
19
+ * writing manually via `ctx.upsertFile`.
20
+ *
21
+ * @example
22
+ * ```ts
23
+ * import { jsxRenderer } from '@kubb/renderer-jsx'
24
+ *
25
+ * export const typeGenerator = defineGenerator<PluginTs>({
26
+ * name: 'typescript',
27
+ * renderer: jsxRenderer,
28
+ * schema(node, ctx) {
29
+ * const { adapter, resolver, root, options } = ctx
30
+ * return <File ...><Type node={node} resolver={resolver} /></File>
31
+ * },
32
+ * operation(node, ctx) {
33
+ * const { options } = ctx
34
+ * return <File ...><OperationType node={node} /></File>
35
+ * },
36
+ * })
37
+ * ```
38
+ */
39
+ export type Generator<TOptions extends PluginFactoryOptions = PluginFactoryOptions, TElement = unknown> = {
40
+ /**
41
+ * Used in diagnostic messages and debug output.
42
+ */
31
43
  name: string
32
- type: 'core'
33
- version?: '2'
34
- operations?(props: OperationsV2Props<TPlugin>): Promise<Array<KubbFile.File>>
35
- operation?(props: OperationV2Props<TPlugin>): Promise<Array<KubbFile.File>>
36
- schema?(props: SchemaV2Props<TPlugin>): Promise<Array<KubbFile.File>>
44
+ /**
45
+ * Optional renderer factory that produces a {@link Renderer} for each render cycle.
46
+ *
47
+ * Generators that return renderer elements (e.g. JSX via `@kubb/renderer-jsx`) must set this
48
+ * to the matching renderer factory (e.g. `jsxRenderer` from `@kubb/renderer-jsx`).
49
+ *
50
+ * Generators that only return `Array<FileNode>` or `void` do not need to set this.
51
+ *
52
+ * Set `renderer: null` to explicitly opt out of rendering even when the parent plugin
53
+ * declares a `renderer` (overrides the plugin-level fallback).
54
+ *
55
+ * @example
56
+ * ```ts
57
+ * import { jsxRenderer } from '@kubb/renderer-jsx'
58
+ * export const myGenerator = defineGenerator<PluginTs>({
59
+ * renderer: jsxRenderer,
60
+ * schema(node, ctx) { return <File ...>...</File> },
61
+ * })
62
+ * ```
63
+ */
64
+ renderer?: RendererFactory<TElement> | null
65
+ /**
66
+ * Called for each schema node in the AST walk.
67
+ * `ctx` carries the plugin context with `adapter` and `inputNode` guaranteed present,
68
+ * plus `ctx.options` with the per-node resolved options (after exclude/include/override).
69
+ */
70
+ schema?: (node: SchemaNode, ctx: GeneratorContext<TOptions>) => PossiblePromise<TElement | Array<FileNode> | void>
71
+ /**
72
+ * Called for each operation node in the AST walk.
73
+ * `ctx` carries the plugin context with `adapter` and `inputNode` guaranteed present,
74
+ * plus `ctx.options` with the per-node resolved options (after exclude/include/override).
75
+ */
76
+ operation?: (node: OperationNode, ctx: GeneratorContext<TOptions>) => PossiblePromise<TElement | Array<FileNode> | void>
77
+ /**
78
+ * Called once after all operations have been walked.
79
+ * `ctx` carries the plugin context with `adapter` and `inputNode` guaranteed present,
80
+ * plus `ctx.options` with the plugin-level options for the batch call.
81
+ */
82
+ operations?: (nodes: Array<OperationNode>, ctx: GeneratorContext<TOptions>) => PossiblePromise<TElement | Array<FileNode> | void>
37
83
  }
38
84
 
39
- type UserReactGeneratorV2<TPlugin extends PluginFactoryOptions> = {
40
- name: string
41
- type: 'react'
42
- version?: '2'
43
- Operations?(props: OperationsV2Props<TPlugin>): FabricReactNode
44
- Operation?(props: OperationV2Props<TPlugin>): FabricReactNode
45
- Schema?(props: SchemaV2Props<TPlugin>): FabricReactNode
46
- }
47
-
48
- export type CoreGeneratorV2<TPlugin extends PluginFactoryOptions = PluginFactoryOptions> = {
49
- name: string
50
- type: 'core'
51
- version: '2'
52
- operations(props: OperationsV2Props<TPlugin>): Promise<Array<KubbFile.File>>
53
- operation(props: OperationV2Props<TPlugin>): Promise<Array<KubbFile.File>>
54
- schema(props: SchemaV2Props<TPlugin>): Promise<Array<KubbFile.File>>
55
- }
56
-
57
- export type ReactGeneratorV2<TPlugin extends PluginFactoryOptions = PluginFactoryOptions> = {
58
- name: string
59
- type: 'react'
60
- version: '2'
61
- Operations(props: OperationsV2Props<TPlugin>): FabricReactNode
62
- Operation(props: OperationV2Props<TPlugin>): FabricReactNode
63
- Schema(props: SchemaV2Props<TPlugin>): FabricReactNode
64
- }
65
-
66
- export type Generator<TPlugin extends PluginFactoryOptions = PluginFactoryOptions> = UserCoreGeneratorV2<TPlugin> | UserReactGeneratorV2<TPlugin>
67
-
68
- export function defineGenerator<TPlugin extends PluginFactoryOptions = PluginFactoryOptions>(
69
- generator: UserReactGeneratorV2<TPlugin>,
70
- ): ReactGeneratorV2<TPlugin>
71
-
72
- export function defineGenerator<TPlugin extends PluginFactoryOptions = PluginFactoryOptions>(generator: UserCoreGeneratorV2<TPlugin>): CoreGeneratorV2<TPlugin>
73
-
74
- export function defineGenerator<TPlugin extends PluginFactoryOptions = PluginFactoryOptions>(
75
- generator: UserCoreGeneratorV2<TPlugin> | UserReactGeneratorV2<TPlugin>,
76
- ): unknown {
77
- if (generator.type === 'react') {
78
- return {
79
- version: '2',
80
- Operations() {
81
- return null
82
- },
83
- Operation() {
84
- return null
85
- },
86
- Schema() {
87
- return null
88
- },
89
- ...generator,
90
- }
91
- }
92
-
93
- return {
94
- version: '2',
95
- async operations() {
96
- return []
97
- },
98
- async operation() {
99
- return []
100
- },
101
- async schema() {
102
- return []
103
- },
104
- ...generator,
105
- }
85
+ /**
86
+ * Defines a generator. Returns the object as-is with correct `this` typings.
87
+ * `applyHookResult` handles renderer elements and `File[]` uniformly using
88
+ * the generator's declared `renderer` factory.
89
+ */
90
+ export function defineGenerator<TOptions extends PluginFactoryOptions = PluginFactoryOptions, TElement = unknown>(
91
+ generator: Generator<TOptions, TElement>,
92
+ ): Generator<TOptions, TElement> {
93
+ return generator
106
94
  }
@@ -1,7 +1,17 @@
1
1
  import type { Logger, LoggerOptions, UserLogger } from './types.ts'
2
2
 
3
+ /**
4
+ * Wraps a logger definition into a typed {@link Logger}.
5
+ *
6
+ * @example
7
+ * export const myLogger = defineLogger({
8
+ * name: 'my-logger',
9
+ * install(context, options) {
10
+ * context.on('kubb:info', (message) => console.log('ℹ', message))
11
+ * context.on('kubb:error', (error) => console.error('✗', error.message))
12
+ * },
13
+ * })
14
+ */
3
15
  export function defineLogger<Options extends LoggerOptions = LoggerOptions>(logger: UserLogger<Options>): Logger<Options> {
4
- return {
5
- ...logger,
6
- }
16
+ return logger
7
17
  }
@@ -0,0 +1,59 @@
1
+ import type { AsyncEventEmitter } from '@internals/utils'
2
+ import type { KubbHooks } from './Kubb.ts'
3
+
4
+ /**
5
+ * A middleware observes and post-processes the build output produced by plugins.
6
+ * It attaches listeners to the shared `hooks` emitter before the plugin execution loop
7
+ * begins and reacts to lifecycle events (e.g. `kubb:plugin:end`, `kubb:build:end`) to
8
+ * inject barrel files or perform other cross-cutting concerns.
9
+ *
10
+ * Middleware listeners are always registered **after** all plugin listeners, because
11
+ * `createKubb` installs middleware only after the `PluginDriver` has registered every
12
+ * plugin's hooks. This means middleware hooks for any event always fire last.
13
+ *
14
+ * @example
15
+ * ```ts
16
+ * import { defineMiddleware } from '@kubb/core'
17
+ *
18
+ * export const myMiddleware = defineMiddleware({
19
+ * name: 'my-middleware',
20
+ * install(hooks) {
21
+ * hooks.on('kubb:build:end', ({ files }) => {
22
+ * console.log(`Build complete with ${files.length} files`)
23
+ * })
24
+ * },
25
+ * })
26
+ * ```
27
+ */
28
+ export type Middleware = {
29
+ /**
30
+ * Unique identifier for this middleware.
31
+ */
32
+ name: string
33
+ /**
34
+ * Called during `createKubb` after `setup()` but before the plugin
35
+ * execution loop starts. Attach listeners to `hooks` here.
36
+ */
37
+ install(hooks: AsyncEventEmitter<KubbHooks>): void
38
+ }
39
+
40
+ /**
41
+ * Identity factory for middleware.
42
+ * Returns the middleware object unchanged but provides a typed entry-point
43
+ * to define middleware with proper inference.
44
+ *
45
+ * @example
46
+ * ```ts
47
+ * export const myMiddleware = defineMiddleware({
48
+ * name: 'my-middleware',
49
+ * install(hooks) {
50
+ * hooks.on('kubb:build:end', ({ files }) => {
51
+ * console.log(`Build complete with ${files.length} files`)
52
+ * })
53
+ * },
54
+ * })
55
+ * ```
56
+ */
57
+ export function defineMiddleware(middleware: Middleware): Middleware {
58
+ return middleware
59
+ }
@@ -0,0 +1,45 @@
1
+ import type { FileNode } from '@kubb/ast'
2
+
3
+ type PrintOptions = {
4
+ extname?: FileNode['extname']
5
+ }
6
+
7
+ export type Parser<TMeta extends object = any> = {
8
+ name: string
9
+ /**
10
+ * File extensions this parser handles.
11
+ * Use `undefined` to create a catch-all fallback parser.
12
+ *
13
+ * @example Handled extensions
14
+ * `['.ts', '.js']`
15
+ */
16
+ extNames: Array<FileNode['extname']> | undefined
17
+ /**
18
+ * Convert a resolved file to a string.
19
+ */
20
+ parse(file: FileNode<TMeta>, options?: PrintOptions): Promise<string> | string
21
+ }
22
+
23
+ /**
24
+ * Defines a parser with type safety.
25
+ *
26
+ * Use this function to create parsers that transform generated files to strings
27
+ * based on their extension.
28
+ *
29
+ * @example
30
+ * ```ts
31
+ * import { defineParser } from '@kubb/core'
32
+ *
33
+ * export const jsonParser = defineParser({
34
+ * name: 'json',
35
+ * extNames: ['.json'],
36
+ * parse(file) {
37
+ * const { extractStringsFromNodes } = await import('@kubb/ast')
38
+ * return file.sources.map((s) => extractStringsFromNodes(s.nodes ?? [])).join('\n')
39
+ * },
40
+ * })
41
+ * ```
42
+ */
43
+ export function defineParser<TMeta extends object = any>(parser: Parser<TMeta>): Parser<TMeta> {
44
+ return parser
45
+ }
@@ -1,12 +1,83 @@
1
- import type { PluginFactoryOptions, UserPluginWithLifeCycle } from './types.ts'
1
+ import type { KubbHooks } from './Kubb.ts'
2
+ import type { KubbPluginSetupContext, PluginFactoryOptions } from './types.ts'
2
3
 
3
- type PluginBuilder<T extends PluginFactoryOptions = PluginFactoryOptions> = (options: T['options']) => UserPluginWithLifeCycle<T>
4
+ /**
5
+ * A plugin object produced by `definePlugin`.
6
+ * Instead of flat lifecycle methods, it groups all handlers under a `hooks:` property
7
+ * (matching Astro's integration naming convention).
8
+ *
9
+ * @template TFactory - The plugin's `PluginFactoryOptions` type.
10
+ */
11
+ export type Plugin<TFactory extends PluginFactoryOptions = PluginFactoryOptions> = {
12
+ /**
13
+ * Unique name for the plugin, following the same naming convention as `createPlugin`.
14
+ */
15
+ name: string
16
+ /**
17
+ * Plugins that must be registered before this plugin executes.
18
+ * An error is thrown at startup when any listed dependency is missing.
19
+ */
20
+ dependencies?: Array<string>
21
+ /**
22
+ * Controls the execution order of this plugin relative to others.
23
+ *
24
+ * - `'pre'` — runs before all normal plugins.
25
+ * - `'post'` — runs after all normal plugins.
26
+ * - `undefined` (default) — runs in declaration order among normal plugins.
27
+ *
28
+ * Dependency constraints always take precedence over `enforce`.
29
+ */
30
+ enforce?: 'pre' | 'post'
31
+ /**
32
+ * The options passed by the user when calling the plugin factory.
33
+ */
34
+ options?: TFactory['options']
35
+ /**
36
+ * Lifecycle event handlers for this plugin.
37
+ * Any event from the global `KubbHooks` map can be subscribed to here.
38
+ */
39
+ hooks: {
40
+ [K in Exclude<keyof KubbHooks, 'kubb:plugin:setup'>]?: (...args: KubbHooks[K]) => void | Promise<void>
41
+ } & {
42
+ 'kubb:plugin:setup'?(ctx: KubbPluginSetupContext<TFactory>): void | Promise<void>
43
+ }
44
+ }
45
+
46
+ /**
47
+ * Returns `true` when `plugin` is a hook-style plugin created with `definePlugin`.
48
+ *
49
+ * Used by `PluginDriver` to distinguish hook-style plugins from legacy `createPlugin` plugins
50
+ * so it can normalize them and register their handlers on the `AsyncEventEmitter`.
51
+ */
52
+ export function isPlugin(plugin: unknown): plugin is Plugin {
53
+ return typeof plugin === 'object' && plugin !== null && 'hooks' in plugin
54
+ }
4
55
 
5
56
  /**
6
- * Wraps a plugin builder to make the options parameter optional.
57
+ * Creates a plugin factory using the hook-style (`hooks:`) API.
58
+ *
59
+ * The returned factory is called with optional options and produces a `Plugin`
60
+ * that coexists with plugins created via the legacy `createPlugin` API in the same
61
+ * `kubb.config.ts`.
62
+ *
63
+ * Lifecycle handlers are registered on the `PluginDriver`'s `AsyncEventEmitter`, enabling
64
+ * both the plugin's own handlers and external tooling (CLI, devtools) to observe every event.
65
+ *
66
+ * @example
67
+ * ```ts
68
+ * // With PluginFactoryOptions (recommended for real plugins)
69
+ * export const pluginTs = definePlugin<PluginTs>((options) => ({
70
+ * name: 'plugin-ts',
71
+ * hooks: {
72
+ * 'kubb:plugin:setup'(ctx) {
73
+ * ctx.setResolver(resolverTs) // typed as Partial<ResolverTs>
74
+ * },
75
+ * },
76
+ * }))
77
+ * ```
7
78
  */
8
- export function definePlugin<T extends PluginFactoryOptions = PluginFactoryOptions>(
9
- build: PluginBuilder<T>,
10
- ): (options?: T['options']) => UserPluginWithLifeCycle<T> {
11
- return (options) => build(options ?? ({} as T['options']))
79
+ export function definePlugin<TFactory extends PluginFactoryOptions = PluginFactoryOptions>(
80
+ factory: (options: TFactory['options']) => Plugin<TFactory>,
81
+ ): (options?: TFactory['options']) => Plugin<TFactory> {
82
+ return (options) => factory(options ?? ({} as TFactory['options']))
12
83
  }