@kubb/core 5.0.0-alpha.7 → 5.0.0-alpha.70

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (69) hide show
  1. package/README.md +3 -2
  2. package/dist/PluginDriver-BXibeQk-.cjs +1036 -0
  3. package/dist/PluginDriver-BXibeQk-.cjs.map +1 -0
  4. package/dist/PluginDriver-DV3p2Hky.js +945 -0
  5. package/dist/PluginDriver-DV3p2Hky.js.map +1 -0
  6. package/dist/index.cjs +730 -1773
  7. package/dist/index.cjs.map +1 -1
  8. package/dist/index.d.ts +289 -240
  9. package/dist/index.js +717 -1741
  10. package/dist/index.js.map +1 -1
  11. package/dist/mocks.cjs +139 -0
  12. package/dist/mocks.cjs.map +1 -0
  13. package/dist/mocks.d.ts +74 -0
  14. package/dist/mocks.js +134 -0
  15. package/dist/mocks.js.map +1 -0
  16. package/dist/types-DWtkW2RX.d.ts +1915 -0
  17. package/package.json +51 -57
  18. package/src/FileManager.ts +115 -0
  19. package/src/FileProcessor.ts +86 -0
  20. package/src/Kubb.ts +205 -130
  21. package/src/PluginDriver.ts +326 -565
  22. package/src/constants.ts +20 -47
  23. package/src/createAdapter.ts +25 -0
  24. package/src/createKubb.ts +546 -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 +64 -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 -16
  35. package/src/mocks.ts +172 -0
  36. package/src/renderNode.ts +35 -0
  37. package/src/storages/fsStorage.ts +43 -13
  38. package/src/storages/memoryStorage.ts +6 -4
  39. package/src/types.ts +752 -226
  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/PluginDriver-Dma9KhLK.d.ts +0 -1056
  44. package/dist/chunk-ByKO4r7w.cjs +0 -38
  45. package/dist/hooks.cjs +0 -102
  46. package/dist/hooks.cjs.map +0 -1
  47. package/dist/hooks.d.ts +0 -75
  48. package/dist/hooks.js +0 -97
  49. package/dist/hooks.js.map +0 -1
  50. package/src/PackageManager.ts +0 -180
  51. package/src/build.ts +0 -419
  52. package/src/config.ts +0 -56
  53. package/src/defineAdapter.ts +0 -22
  54. package/src/defineStorage.ts +0 -56
  55. package/src/errors.ts +0 -1
  56. package/src/hooks/index.ts +0 -4
  57. package/src/hooks/useKubb.ts +0 -138
  58. package/src/hooks/useMode.ts +0 -11
  59. package/src/hooks/usePlugin.ts +0 -11
  60. package/src/hooks/usePluginDriver.ts +0 -11
  61. package/src/utils/FunctionParams.ts +0 -155
  62. package/src/utils/TreeNode.ts +0 -215
  63. package/src/utils/executeStrategies.ts +0 -81
  64. package/src/utils/formatters.ts +0 -56
  65. package/src/utils/getBarrelFiles.ts +0 -141
  66. package/src/utils/getConfigs.ts +0 -30
  67. package/src/utils/getPlugins.ts +0 -23
  68. package/src/utils/linters.ts +0 -25
  69. package/src/utils/resolveOptions.ts +0 -93
@@ -0,0 +1,58 @@
1
+ export type Storage = {
2
+ /**
3
+ * Identifier used for logging and debugging (e.g. `'fs'`, `'s3'`).
4
+ */
5
+ readonly name: string
6
+ /**
7
+ * Returns `true` when an entry for `key` exists in storage.
8
+ */
9
+ hasItem(key: string): Promise<boolean>
10
+ /**
11
+ * Returns the stored string value, or `null` when `key` does not exist.
12
+ */
13
+ getItem(key: string): Promise<string | null>
14
+ /**
15
+ * Persists `value` under `key`, creating any required structure.
16
+ */
17
+ setItem(key: string, value: string): Promise<void>
18
+ /**
19
+ * Removes the entry for `key`. No-ops when the key does not exist.
20
+ */
21
+ removeItem(key: string): Promise<void>
22
+ /**
23
+ * Returns all keys, optionally filtered to those starting with `base`.
24
+ */
25
+ getKeys(base?: string): Promise<Array<string>>
26
+ /**
27
+ * Removes all entries, optionally scoped to those starting with `base`.
28
+ */
29
+ clear(base?: string): Promise<void>
30
+ /**
31
+ * Optional teardown hook called after the build completes.
32
+ */
33
+ dispose?(): Promise<void>
34
+ }
35
+
36
+ /**
37
+ * Creates a storage factory. Call the returned function with optional options to get the storage instance.
38
+ *
39
+ * @example
40
+ * export const memoryStorage = createStorage(() => {
41
+ * const store = new Map<string, string>()
42
+ * return {
43
+ * name: 'memory',
44
+ * async hasItem(key) { return store.has(key) },
45
+ * async getItem(key) { return store.get(key) ?? null },
46
+ * async setItem(key, value) { store.set(key, value) },
47
+ * async removeItem(key) { store.delete(key) },
48
+ * async getKeys(base) {
49
+ * const keys = [...store.keys()]
50
+ * return base ? keys.filter((k) => k.startsWith(base)) : keys
51
+ * },
52
+ * async clear(base) { if (!base) store.clear() },
53
+ * }
54
+ * })
55
+ */
56
+ export function createStorage<TOptions = Record<string, never>>(build: (options: TOptions) => Storage): (options?: TOptions) => Storage {
57
+ return (options) => build(options ?? ({} as TOptions))
58
+ }
@@ -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,64 @@
1
+ import type { KubbHooks } from './Kubb.ts'
2
+
3
+ /**
4
+ * A middleware instance produced by calling a factory created with `defineMiddleware`.
5
+ * It declares event handlers under a `hooks` object which are registered on the
6
+ * shared emitter after all plugin hooks, so middleware handlers for any event
7
+ * always fire last.
8
+ */
9
+ export type Middleware = {
10
+ /**
11
+ * Unique identifier for this middleware.
12
+ */
13
+ name: string
14
+ /**
15
+ * Lifecycle event handlers for this middleware.
16
+ * Any event from the global `KubbHooks` map can be subscribed to here.
17
+ * Handlers are registered after all plugin handlers, so they always fire last.
18
+ */
19
+ hooks: {
20
+ [K in keyof KubbHooks]?: (...args: KubbHooks[K]) => void | Promise<void>
21
+ }
22
+ }
23
+
24
+ /**
25
+ * Creates a middleware factory using the hook-style (`hooks:`) API.
26
+ *
27
+ * Mirrors `definePlugin`: the factory is called with optional options and returns a
28
+ * fresh `Middleware` instance. Placing per-build state (e.g. accumulators) inside the
29
+ * factory closure ensures each `createKubb` invocation gets its own isolated instance.
30
+ *
31
+ * @example
32
+ * ```ts
33
+ * // Stateless middleware
34
+ * export const logMiddleware = defineMiddleware(() => ({
35
+ * name: 'log-middleware',
36
+ * hooks: {
37
+ * 'kubb:build:end'({ files }) {
38
+ * console.log(`Build complete with ${files.length} files`)
39
+ * },
40
+ * },
41
+ * }))
42
+ *
43
+ * // Middleware with options and per-build state
44
+ * export const myMiddleware = defineMiddleware((options: { prefix: string } = { prefix: '' }) => {
45
+ * const seen = new Set<string>()
46
+ * return {
47
+ * name: 'my-middleware',
48
+ * hooks: {
49
+ * 'kubb:plugin:end'({ plugin }) {
50
+ * seen.add(`${options.prefix}${plugin.name}`)
51
+ * },
52
+ * },
53
+ * }
54
+ * })
55
+ *
56
+ * // Usage in kubb.config.ts:
57
+ * export default defineConfig({
58
+ * middleware: [logMiddleware(), myMiddleware({ prefix: 'pfx:' })],
59
+ * })
60
+ * ```
61
+ */
62
+ export function defineMiddleware<TOptions extends object = object>(factory: (options: TOptions) => Middleware): (options?: TOptions) => Middleware {
63
+ return (options) => factory(options ?? ({} as TOptions))
64
+ }
@@ -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
  }