@kubb/core 5.0.0-beta.3 → 5.0.0-beta.31

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 (46) hide show
  1. package/README.md +8 -38
  2. package/dist/KubbDriver-CFx2DdhF.js +2131 -0
  3. package/dist/KubbDriver-CFx2DdhF.js.map +1 -0
  4. package/dist/KubbDriver-vyD7F0Ip.cjs +2252 -0
  5. package/dist/KubbDriver-vyD7F0Ip.cjs.map +1 -0
  6. package/dist/{types-CC09VtBt.d.ts → createKubb-6zii1jo-.d.ts} +1610 -1257
  7. package/dist/index.cjs +351 -1125
  8. package/dist/index.cjs.map +1 -1
  9. package/dist/index.d.ts +3 -186
  10. package/dist/index.js +341 -1119
  11. package/dist/index.js.map +1 -1
  12. package/dist/mocks.cjs +30 -21
  13. package/dist/mocks.cjs.map +1 -1
  14. package/dist/mocks.d.ts +5 -5
  15. package/dist/mocks.js +29 -20
  16. package/dist/mocks.js.map +1 -1
  17. package/package.json +6 -18
  18. package/src/FileManager.ts +78 -61
  19. package/src/FileProcessor.ts +48 -38
  20. package/src/KubbDriver.ts +930 -0
  21. package/src/constants.ts +11 -6
  22. package/src/createAdapter.ts +113 -17
  23. package/src/createKubb.ts +1039 -478
  24. package/src/createRenderer.ts +58 -27
  25. package/src/createStorage.ts +36 -23
  26. package/src/defineGenerator.ts +127 -15
  27. package/src/defineLogger.ts +66 -7
  28. package/src/defineMiddleware.ts +19 -17
  29. package/src/defineParser.ts +30 -13
  30. package/src/definePlugin.ts +329 -14
  31. package/src/defineResolver.ts +365 -167
  32. package/src/devtools.ts +8 -1
  33. package/src/index.ts +2 -2
  34. package/src/mocks.ts +11 -14
  35. package/src/storages/fsStorage.ts +13 -37
  36. package/src/types.ts +48 -1292
  37. package/dist/PluginDriver-BXibeQk-.cjs +0 -1036
  38. package/dist/PluginDriver-BXibeQk-.cjs.map +0 -1
  39. package/dist/PluginDriver-DV3p2Hky.js +0 -945
  40. package/dist/PluginDriver-DV3p2Hky.js.map +0 -1
  41. package/src/Kubb.ts +0 -300
  42. package/src/PluginDriver.ts +0 -424
  43. package/src/renderNode.ts +0 -35
  44. package/src/utils/diagnostics.ts +0 -18
  45. package/src/utils/isInputPath.ts +0 -10
  46. package/src/utils/packageJSON.ts +0 -99
@@ -3,53 +3,84 @@ import type { FileNode } from '@kubb/ast'
3
3
  /**
4
4
  * Minimal interface any Kubb renderer must satisfy.
5
5
  *
6
- * The generic `TElement` is the type of the element the renderer accepts
7
- * e.g. `KubbReactElement` for `@kubb/renderer-jsx`, or a custom type for
8
- * your own renderer. Defaults to `unknown` so that generators which do not
9
- * care about the element type continue to work without specifying it.
10
- *
11
- * This allows core to drive rendering without a hard dependency on
12
- * `@kubb/renderer-jsx` or any specific renderer implementation.
6
+ * `TElement` is the type the renderer accepts, for example `KubbReactElement`
7
+ * for `@kubb/renderer-jsx` or a custom type for your own renderer. Defaults to
8
+ * `unknown` so generators that don't care about the element type work without
9
+ * specifying it.
13
10
  */
14
11
  export type Renderer<TElement = unknown> = {
12
+ /**
13
+ * Renders `element` and populates {@link files} with the resulting {@link FileNode} objects.
14
+ * Called once per render cycle; must resolve before {@link files} is read.
15
+ */
15
16
  render(element: TElement): Promise<void>
17
+ /**
18
+ * Tears down the renderer and releases any held resources.
19
+ * Pass an `Error` to signal a failure, a number for an exit code, or omit for a clean shutdown.
20
+ */
16
21
  unmount(error?: Error | number | null): void
22
+ /**
23
+ * Releases any held resources. `[Symbol.dispose]` delegates here.
24
+ */
25
+ dispose(): void
26
+ /**
27
+ * Accumulated {@link FileNode} results produced by the last {@link render} call.
28
+ * Not populated when {@link stream} is implemented.
29
+ */
17
30
  readonly files: Array<FileNode>
31
+ /**
32
+ * When present, core calls this instead of {@link render} and {@link files},
33
+ * forwarding each file to `FileManager` as soon as it is ready.
34
+ */
35
+ stream?(element: TElement): Iterable<FileNode>
36
+ /**
37
+ * Disposer hook so renderers participate in `using` blocks: `using r = rendererFactory()`
38
+ * guarantees {@link dispose} runs on every exit path, including thrown errors.
39
+ */
40
+ [Symbol.dispose](): void
18
41
  }
19
42
 
20
43
  /**
21
- * A factory function that produces a fresh {@link Renderer} per render.
44
+ * A factory function that produces a fresh {@link Renderer} per render cycle.
22
45
  *
23
46
  * Generators use this to declare which renderer handles their output.
24
47
  */
25
48
  export type RendererFactory<TElement = unknown> = () => Renderer<TElement>
26
49
 
27
50
  /**
28
- * Creates a renderer factory for use in generator definitions.
51
+ * Defines a renderer factory. Renderers turn the generator's return value
52
+ * (JSX, a template string, a tree of any shape) into `FileNode`s that get
53
+ * written to disk.
29
54
  *
30
- * Wrap your renderer factory function with this helper to register it as the
31
- * renderer for a generator. Core will call this factory once per render cycle
32
- * to obtain a fresh renderer instance.
55
+ * Use this to support output formats beyond JSX for instance, a Handlebars
56
+ * renderer, a string-template renderer, or a renderer that writes binary
57
+ * files. Plugins and generators pick the renderer to use via the `renderer`
58
+ * field on `defineGenerator`.
33
59
  *
34
- * @example
60
+ * @example A minimal renderer that wraps a custom runtime
35
61
  * ```ts
36
- * // packages/renderer-jsx/src/index.ts
37
- * export const jsxRenderer = createRenderer(() => {
38
- * const runtime = new Runtime()
62
+ * import { createRenderer } from '@kubb/core'
63
+ *
64
+ * export const myRenderer = createRenderer(() => {
65
+ * const runtime = new MyRuntime()
39
66
  * return {
40
- * async render(element) { await runtime.render(element) },
41
- * get files() { return runtime.nodes },
42
- * unmount(error) { runtime.unmount(error) },
67
+ * async render(element) {
68
+ * await runtime.render(element)
69
+ * },
70
+ * get files() {
71
+ * return runtime.files
72
+ * },
73
+ * dispose() {
74
+ * runtime.dispose()
75
+ * },
76
+ * unmount(error) {
77
+ * runtime.dispose(error)
78
+ * },
79
+ * [Symbol.dispose]() {
80
+ * this.dispose()
81
+ * },
43
82
  * }
44
83
  * })
45
- *
46
- * // packages/plugin-zod/src/generators/zodGenerator.tsx
47
- * import { jsxRenderer } from '@kubb/renderer-jsx'
48
- * export const zodGenerator = defineGenerator<PluginZod>({
49
- * name: 'zod',
50
- * renderer: jsxRenderer,
51
- * schema(node, options) { return <File ...>...</File> },
52
- * })
53
84
  * ```
54
85
  */
55
86
  export function createRenderer<TElement = unknown>(factory: RendererFactory<TElement>): RendererFactory<TElement> {
@@ -1,68 +1,81 @@
1
+ /**
2
+ * Backend that persists generated files. Kubb ships with `fsStorage` (writes
3
+ * to disk) and `memoryStorage` (keeps everything in RAM). Implement this
4
+ * interface to write to S3, a database, or any other target.
5
+ */
1
6
  export type Storage = {
2
7
  /**
3
- * Identifier used for logging and debugging (e.g. `'fs'`, `'s3'`).
8
+ * Identifier used in logs and diagnostics (`'fs'`, `'memory'`, `'s3'`).
4
9
  */
5
10
  readonly name: string
6
11
  /**
7
- * Returns `true` when an entry for `key` exists in storage.
12
+ * Returns `true` when an entry for `key` exists.
8
13
  */
9
14
  hasItem(key: string): Promise<boolean>
10
15
  /**
11
- * Returns the stored string value, or `null` when `key` does not exist.
16
+ * Reads the stored string. Returns `null` when the key is missing.
12
17
  */
13
18
  getItem(key: string): Promise<string | null>
14
19
  /**
15
- * Persists `value` under `key`, creating any required structure.
20
+ * Stores `value` under `key`, creating any required structure (directories,
21
+ * buckets, ...).
16
22
  */
17
23
  setItem(key: string, value: string): Promise<void>
18
24
  /**
19
- * Removes the entry for `key`. No-ops when the key does not exist.
25
+ * Deletes the entry for `key`. No-op when the key does not exist.
20
26
  */
21
27
  removeItem(key: string): Promise<void>
22
28
  /**
23
- * Returns all keys, optionally filtered to those starting with `base`.
29
+ * Returns every key. Pass `base` to filter to keys starting with that prefix.
24
30
  */
25
31
  getKeys(base?: string): Promise<Array<string>>
26
32
  /**
27
- * Removes all entries, optionally scoped to those starting with `base`.
33
+ * Removes every entry. Pass `base` to scope the wipe to a key prefix.
28
34
  */
29
35
  clear(base?: string): Promise<void>
30
36
  /**
31
- * Optional teardown hook called after the build completes.
37
+ * Optional teardown hook called after the build completes. Use to flush
38
+ * buffers, close connections, or release file locks.
32
39
  */
33
40
  dispose?(): Promise<void>
34
41
  }
35
42
 
36
43
  /**
37
- * Factory for implementing custom storage backends that control where generated files are written.
38
- *
39
- * Takes a builder function `(options: TOptions) => Storage` and returns a factory `(options?: TOptions) => Storage`.
40
- * Kubb provides filesystem and in-memory implementations out of the box.
44
+ * Defines a custom storage backend. The builder receives user options and
45
+ * returns a `Storage` implementation. Kubb ships with filesystem and
46
+ * in-memory storages reach for this when you need to write generated files
47
+ * elsewhere (cloud storage, a database, a remote API).
41
48
  *
42
- * @note Call the returned factory with optional options to instantiate the storage adapter.
43
- *
44
- * @example
49
+ * @example In-memory storage (the built-in implementation)
45
50
  * ```ts
46
51
  * import { createStorage } from '@kubb/core'
47
52
  *
48
53
  * export const memoryStorage = createStorage(() => {
49
54
  * const store = new Map<string, string>()
55
+ *
50
56
  * return {
51
57
  * name: 'memory',
52
- * async hasItem(key) { return store.has(key) },
53
- * async getItem(key) { return store.get(key) ?? null },
54
- * async setItem(key, value) { store.set(key, value) },
55
- * async removeItem(key) { store.delete(key) },
58
+ * async hasItem(key) {
59
+ * return store.has(key)
60
+ * },
61
+ * async getItem(key) {
62
+ * return store.get(key) ?? null
63
+ * },
64
+ * async setItem(key, value) {
65
+ * store.set(key, value)
66
+ * },
67
+ * async removeItem(key) {
68
+ * store.delete(key)
69
+ * },
56
70
  * async getKeys(base) {
57
71
  * const keys = [...store.keys()]
58
72
  * return base ? keys.filter((k) => k.startsWith(base)) : keys
59
73
  * },
60
- * async clear(base) { if (!base) store.clear() },
74
+ * async clear(base) {
75
+ * if (!base) store.clear()
76
+ * },
61
77
  * }
62
78
  * })
63
- *
64
- * // Instantiate:
65
- * const storage = memoryStorage()
66
79
  * ```
67
80
  */
68
81
  export function createStorage<TOptions = Record<string, never>>(build: (options: TOptions) => Storage): (options?: TOptions) => Storage {
@@ -1,16 +1,105 @@
1
- import type { PossiblePromise } from '@internals/utils'
2
- import type { FileNode, OperationNode, SchemaNode } from '@kubb/ast'
1
+ import type { AsyncEventEmitter, PossiblePromise } from '@internals/utils'
2
+ import type { FileNode, InputMeta, OperationNode, SchemaNode, Visitor } from '@kubb/ast'
3
+ import type { Adapter } from './createAdapter.ts'
3
4
  import type { RendererFactory } from './createRenderer.ts'
4
- import type { GeneratorContext, PluginFactoryOptions } from './types.ts'
5
+ import type { KubbHooks } from './types.ts'
6
+ import type { KubbDriver } from './KubbDriver.ts'
7
+ import type { Plugin, PluginFactoryOptions } from './definePlugin.ts'
8
+ import type { Resolver } from './defineResolver.ts'
9
+ import type { Config, DevtoolsOptions } from './types.ts'
5
10
 
6
- export type { GeneratorContext } from './types.ts'
11
+ /**
12
+ * Context object passed to generator `schema`, `operation`, and `operations` methods.
13
+ *
14
+ * The adapter is always defined (guaranteed by `runPluginAstHooks`) so no runtime checks
15
+ * are needed. `ctx.options` carries resolved per-node options after exclude/include/override
16
+ * filtering for individual schema/operation calls, or plugin-level options for operations.
17
+ */
18
+ export type GeneratorContext<TOptions extends PluginFactoryOptions = PluginFactoryOptions> = {
19
+ config: Config
20
+ /**
21
+ * Absolute path to the current plugin's output directory.
22
+ */
23
+ root: string
24
+ /**
25
+ * Determine output mode based on the output config.
26
+ * Returns `'single'` when `output.path` is a file, `'split'` for a directory.
27
+ */
28
+ getMode: (output: { path: string }) => 'single' | 'split'
29
+ driver: KubbDriver
30
+ /**
31
+ * Get a plugin by name, typed via `Kubb.PluginRegistry` when registered.
32
+ */
33
+ getPlugin<TName extends keyof Kubb.PluginRegistry>(name: TName): Plugin<Kubb.PluginRegistry[TName]> | undefined
34
+ getPlugin(name: string): Plugin | undefined
35
+ /**
36
+ * Get a plugin by name, throws an error if not found.
37
+ */
38
+ requirePlugin<TName extends keyof Kubb.PluginRegistry>(name: TName): Plugin<Kubb.PluginRegistry[TName]>
39
+ requirePlugin(name: string): Plugin
40
+ /**
41
+ * Get a resolver by plugin name, typed via `Kubb.PluginRegistry` when registered.
42
+ */
43
+ getResolver<TName extends keyof Kubb.PluginRegistry>(name: TName): Kubb.PluginRegistry[TName]['resolver']
44
+ getResolver(name: string): Resolver
45
+ /**
46
+ * Add files only if they don't exist.
47
+ */
48
+ addFile: (...file: Array<FileNode>) => Promise<void>
49
+ /**
50
+ * Merge sources into the same output file.
51
+ */
52
+ upsertFile: (...file: Array<FileNode>) => Promise<void>
53
+ hooks: AsyncEventEmitter<KubbHooks>
54
+ /**
55
+ * The current plugin instance.
56
+ */
57
+ plugin: Plugin<TOptions>
58
+ /**
59
+ * The current plugin's resolver.
60
+ */
61
+ resolver: TOptions['resolver']
62
+ /**
63
+ * The current plugin's transformer.
64
+ */
65
+ transformer: Visitor | undefined
66
+ /**
67
+ * Emit a warning.
68
+ */
69
+ warn: (message: string) => void
70
+ /**
71
+ * Emit an error.
72
+ */
73
+ error: (error: string | Error) => void
74
+ /**
75
+ * Emit an info message.
76
+ */
77
+ info: (message: string) => void
78
+ /**
79
+ * Open the current input node in Kubb Studio.
80
+ */
81
+ openInStudio: (options?: DevtoolsOptions) => Promise<void>
82
+ /**
83
+ * The configured adapter instance.
84
+ */
85
+ adapter: Adapter
86
+ /**
87
+ * Document metadata from the adapter — title, version, base URL, and pre-computed
88
+ * schema index fields (`circularNames`, `enumNames`).
89
+ */
90
+ meta: InputMeta
91
+ /**
92
+ * Resolved options after exclude/include/override filtering.
93
+ */
94
+ options: TOptions['resolvedOptions']
95
+ }
7
96
 
8
97
  /**
9
98
  * Declares a named generator unit that walks the AST and emits files.
10
99
  *
11
100
  * Each method (`schema`, `operation`, `operations`) is called for the matching node type.
12
- * Each method returns `TElement | Array<FileNode> | void`. JSX-based generators require a `renderer` factory.
13
- * Return `Array<FileNode>` directly or call `ctx.upsertFile()` manually and return `void` to bypass rendering.
101
+ * Each method returns `TElement | Array<FileNode> | undefined | null`. JSX-based generators require a `renderer` factory.
102
+ * Return `Array<FileNode>` directly or call `ctx.upsertFile()` manually and return `undefined` or `null` to bypass rendering.
14
103
  *
15
104
  * @note Generators are consumed by plugins and registered via `ctx.addGenerator()` in `kubb:plugin:setup`.
16
105
  *
@@ -57,28 +146,51 @@ export type Generator<TOptions extends PluginFactoryOptions = PluginFactoryOptio
57
146
  renderer?: RendererFactory<TElement> | null
58
147
  /**
59
148
  * Called for each schema node in the AST walk.
60
- * `ctx` carries the plugin context with `adapter` and `inputNode` guaranteed present,
149
+ * `ctx` carries the plugin context with `adapter` and `meta` (document metadata),
61
150
  * plus `ctx.options` with the per-node resolved options (after exclude/include/override).
62
151
  */
63
- schema?: (node: SchemaNode, ctx: GeneratorContext<TOptions>) => PossiblePromise<TElement | Array<FileNode> | void>
152
+ schema?: (node: SchemaNode, ctx: GeneratorContext<TOptions>) => PossiblePromise<TElement | Array<FileNode> | undefined | null>
64
153
  /**
65
154
  * Called for each operation node in the AST walk.
66
- * `ctx` carries the plugin context with `adapter` and `inputNode` guaranteed present,
155
+ * `ctx` carries the plugin context with `adapter` and `meta` (document metadata),
67
156
  * plus `ctx.options` with the per-node resolved options (after exclude/include/override).
68
157
  */
69
- operation?: (node: OperationNode, ctx: GeneratorContext<TOptions>) => PossiblePromise<TElement | Array<FileNode> | void>
158
+ operation?: (node: OperationNode, ctx: GeneratorContext<TOptions>) => PossiblePromise<TElement | Array<FileNode> | undefined | null>
70
159
  /**
71
160
  * Called once after all operations have been walked.
72
- * `ctx` carries the plugin context with `adapter` and `inputNode` guaranteed present,
161
+ * `ctx` carries the plugin context with `adapter` and `meta` (document metadata),
73
162
  * plus `ctx.options` with the plugin-level options for the batch call.
74
163
  */
75
- operations?: (nodes: Array<OperationNode>, ctx: GeneratorContext<TOptions>) => PossiblePromise<TElement | Array<FileNode> | void>
164
+ operations?: (nodes: Array<OperationNode>, ctx: GeneratorContext<TOptions>) => PossiblePromise<TElement | Array<FileNode> | undefined | null>
76
165
  }
77
166
 
78
167
  /**
79
- * Defines a generator. Returns the object as-is with correct `this` typings.
80
- * `applyHookResult` handles renderer elements and `File[]` uniformly using
81
- * the generator's declared `renderer` factory.
168
+ * Defines a generator: a unit of work that runs during the plugin's AST walk
169
+ * and produces files. Plugins register generators via `ctx.addGenerator()`
170
+ * inside `kubb:plugin:setup`.
171
+ *
172
+ * The returned object is the input as-is, but with `this` types preserved so
173
+ * `schema`/`operation`/`operations` methods are correctly typed against the
174
+ * plugin's `PluginFactoryOptions`. Renderer elements and `FileNode[]` returns
175
+ * are both handled by the runtime — pick whichever style fits.
176
+ *
177
+ * @example JSX-based schema generator
178
+ * ```tsx
179
+ * import { defineGenerator } from '@kubb/core'
180
+ * import { jsxRenderer } from '@kubb/renderer-jsx'
181
+ *
182
+ * export const typeGenerator = defineGenerator({
183
+ * name: 'typescript',
184
+ * renderer: jsxRenderer,
185
+ * schema(node, ctx) {
186
+ * return (
187
+ * <File path={`${ctx.root}/${node.name}.ts`}>
188
+ * <Type node={node} resolver={ctx.resolver} />
189
+ * </File>
190
+ * )
191
+ * },
192
+ * })
193
+ * ```
82
194
  */
83
195
  export function defineGenerator<TOptions extends PluginFactoryOptions = PluginFactoryOptions, TElement = unknown>(
84
196
  generator: Generator<TOptions, TElement>,
@@ -1,19 +1,78 @@
1
- import type { Logger, LoggerOptions, UserLogger } from './types.ts'
1
+ import type { AsyncEventEmitter } from '@internals/utils'
2
+ import type { logLevel } from './constants.ts'
3
+ import type { KubbHooks } from './types.ts'
2
4
 
3
5
  /**
4
- * Wraps a logger definition into a typed {@link Logger}.
6
+ * Options accepted by a logger's `install` callback.
7
+ */
8
+ export type LoggerOptions = {
9
+ /**
10
+ * Output verbosity. Use the `logLevel` constants exported from `@kubb/core`
11
+ * (`silent`, `error`, `warn`, `info`, `verbose`, `debug`).
12
+ */
13
+ logLevel: (typeof logLevel)[keyof typeof logLevel]
14
+ }
15
+
16
+ /**
17
+ * Event emitter handed to `Logger.install`. Use `.on('kubb:info', ...)` and
18
+ * friends to subscribe to build events.
19
+ */
20
+ export type LoggerContext = AsyncEventEmitter<KubbHooks>
21
+
22
+ /**
23
+ * Logger contract. A logger receives the build's event emitter and subscribes
24
+ * to whichever lifecycle events it wants to forward to its destination
25
+ * (console, file, remote sink).
26
+ */
27
+ export type Logger<TOptions extends LoggerOptions = LoggerOptions, TInstallReturn = void> = {
28
+ /**
29
+ * Display name used in diagnostics.
30
+ */
31
+ name: string
32
+ /**
33
+ * Called once per build with the shared event emitter. Subscribe to events
34
+ * here. The return value (if any) is forwarded to whoever installed the
35
+ * logger, which is handy for sink factories.
36
+ */
37
+ install: (context: LoggerContext, options?: TOptions) => TInstallReturn | Promise<TInstallReturn>
38
+ }
39
+
40
+ export type UserLogger<TOptions extends LoggerOptions = LoggerOptions, TInstallReturn = void> = Logger<TOptions, TInstallReturn>
41
+
42
+ /**
43
+ * Defines a typed logger. Use the second type parameter to declare a return
44
+ * value from `install`, which is handy when the logger exposes a sink factory
45
+ * or cleanup callback to the caller.
5
46
  *
6
- * @example
47
+ * @example Basic logger
7
48
  * ```ts
49
+ * import { defineLogger } from '@kubb/core'
50
+ *
8
51
  * export const myLogger = defineLogger({
9
52
  * name: 'my-logger',
10
- * install(context, options) {
11
- * context.on('kubb:info', (message) => console.log('ℹ', message))
12
- * context.on('kubb:error', (error) => console.error('✗', error.message))
53
+ * install(context) {
54
+ * context.on('kubb:info', ({ message }) => console.log('ℹ', message))
55
+ * context.on('kubb:error', ({ error }) => console.error('✗', error.message))
56
+ * },
57
+ * })
58
+ * ```
59
+ *
60
+ * @example Logger that returns a hook sink factory
61
+ * ```ts
62
+ * import { defineLogger, type LoggerOptions } from '@kubb/core'
63
+ * import type { HookSinkFactory } from './sinks'
64
+ *
65
+ * export const myLogger = defineLogger<LoggerOptions, HookSinkFactory>({
66
+ * name: 'my-logger',
67
+ * install(context) {
68
+ * // … register event handlers …
69
+ * return () => ({ onStdout: console.log })
13
70
  * },
14
71
  * })
15
72
  * ```
16
73
  */
17
- export function defineLogger<Options extends LoggerOptions = LoggerOptions>(logger: UserLogger<Options>): Logger<Options> {
74
+ export function defineLogger<Options extends LoggerOptions = LoggerOptions, TInstallReturn = void>(
75
+ logger: UserLogger<Options, TInstallReturn>,
76
+ ): Logger<Options, TInstallReturn> {
18
77
  return logger
19
78
  }
@@ -1,20 +1,19 @@
1
- import type { KubbHooks } from './Kubb.ts'
1
+ import type { KubbHooks } from './types.ts'
2
2
 
3
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.
4
+ * A middleware instance. Subscribes to lifecycle events via `hooks`. Middleware
5
+ * handlers always fire after every plugin handler for the same event, so they
6
+ * see the full set of generated files.
8
7
  */
9
8
  export type Middleware = {
10
9
  /**
11
- * Unique identifier for this middleware.
10
+ * Unique name. Use a `middleware-<feature>` convention (e.g.
11
+ * `middleware-barrel`).
12
12
  */
13
13
  name: string
14
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.
15
+ * Lifecycle event handlers. Any event from the global `KubbHooks` map can be
16
+ * subscribed to here. Handlers run after all plugin handlers for that event.
18
17
  */
19
18
  hooks: {
20
19
  [K in keyof KubbHooks]?: (...args: KubbHooks[K]) => void | Promise<void>
@@ -22,18 +21,17 @@ export type Middleware = {
22
21
  }
23
22
 
24
23
  /**
25
- * Creates a middleware factory using the hook-style `hooks` API.
24
+ * Creates a middleware factory. Middleware fires after every plugin handler
25
+ * for the same event, which makes it the natural place for post-processing
26
+ * (barrel files, lint runs, audit logs).
26
27
  *
27
- * Middleware handlers fire after all plugin handlers for any given event, making them ideal for post-processing, logging, and auditing.
28
- * Per-build state (such as accumulators) belongs inside the factory closure so each `createKubb` invocation gets its own isolated instance.
28
+ * Per-build state belongs inside the factory closure so each `createKubb`
29
+ * invocation gets its own isolated instance.
29
30
  *
30
- * @note The factory can accept typed options. See examples for using options and per-build state patterns.
31
- *
32
- * @example
31
+ * @example Stateless middleware
33
32
  * ```ts
34
33
  * import { defineMiddleware } from '@kubb/core'
35
34
  *
36
- * // Stateless middleware
37
35
  * export const logMiddleware = defineMiddleware(() => ({
38
36
  * name: 'log-middleware',
39
37
  * hooks: {
@@ -42,8 +40,12 @@ export type Middleware = {
42
40
  * },
43
41
  * },
44
42
  * }))
43
+ * ```
44
+ *
45
+ * @example Middleware with options and per-build state
46
+ * ```ts
47
+ * import { defineMiddleware } from '@kubb/core'
45
48
  *
46
- * // Middleware with options and per-build state
47
49
  * export const prefixMiddleware = defineMiddleware((options: { prefix: string } = { prefix: '' }) => {
48
50
  * const seen = new Set<string>()
49
51
  * return {
@@ -4,41 +4,58 @@ type PrintOptions = {
4
4
  extname?: FileNode['extname']
5
5
  }
6
6
 
7
- export type Parser<TMeta extends object = any> = {
7
+ /**
8
+ * Converts a resolved {@link FileNode} into the final source string that gets
9
+ * written to disk. Kubb ships with TypeScript and TSX parsers; add your own
10
+ * for new file types (JSON, Markdown, ...).
11
+ */
12
+ export type Parser<TMeta extends object = any, TNode = unknown> = {
13
+ /**
14
+ * Display name used in diagnostics and the parser registry.
15
+ */
8
16
  name: string
9
17
  /**
10
- * File extensions this parser handles.
11
- * Use `undefined` to create a catch-all fallback parser.
18
+ * File extensions this parser handles. Set to `undefined` to define a
19
+ * catch-all fallback used when no other parser claims the extension.
12
20
  *
13
- * @example Handled extensions
21
+ * @example
14
22
  * `['.ts', '.js']`
15
23
  */
16
24
  extNames: Array<FileNode['extname']> | undefined
17
25
  /**
18
- * Convert a resolved file to a string.
26
+ * Serialise the file's AST into source code.
19
27
  */
20
- parse(file: FileNode<TMeta>, options?: PrintOptions): Promise<string> | string
28
+ parse(file: FileNode<TMeta>, options?: PrintOptions): string
29
+ /**
30
+ * Render compiler AST nodes for this parser's language into source text.
31
+ * Plugins call this to format the nodes they assemble before handing them
32
+ * back to the parser as `FileNode.sources`.
33
+ */
34
+ print(...nodes: Array<TNode>): string
21
35
  }
22
36
 
23
37
  /**
24
- * Defines a parser with type safety. Creates parsers that transform generated files to strings based on their extension.
25
- *
26
- * @note Call the returned factory with optional options to instantiate the parser.
38
+ * Defines a parser with type-safe `this`. Used to register handlers for new
39
+ * file extensions or to plug a non-TypeScript output into the build.
27
40
  *
28
41
  * @example
29
42
  * ```ts
30
- * import { defineParser } from '@kubb/core'
43
+ * import { defineParser, ast } from '@kubb/core'
31
44
  *
32
45
  * export const jsonParser = defineParser({
33
46
  * name: 'json',
34
47
  * extNames: ['.json'],
35
48
  * parse(file) {
36
- * const { extractStringsFromNodes } = await import('@kubb/ast')
37
- * return file.sources.map((s) => extractStringsFromNodes(s.nodes ?? [])).join('\n')
49
+ * return file.sources
50
+ * .map((source) => ast.extractStringsFromNodes(source.nodes ?? []))
51
+ * .join('\n')
52
+ * },
53
+ * print(...nodes) {
54
+ * return nodes.map(String).join('\n')
38
55
  * },
39
56
  * })
40
57
  * ```
41
58
  */
42
- export function defineParser<TMeta extends object = any>(parser: Parser<TMeta>): Parser<TMeta> {
59
+ export function defineParser<T extends Parser>(parser: T): T {
43
60
  return parser
44
61
  }