@kubb/core 5.0.0-alpha.8 → 5.0.0-beta.1

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 (70) hide show
  1. package/README.md +23 -20
  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 +756 -1693
  7. package/dist/index.cjs.map +1 -1
  8. package/dist/index.d.ts +297 -239
  9. package/dist/index.js +743 -1661
  10. package/dist/index.js.map +1 -1
  11. package/dist/mocks.cjs +145 -0
  12. package/dist/mocks.cjs.map +1 -0
  13. package/dist/mocks.d.ts +80 -0
  14. package/dist/mocks.js +140 -0
  15. package/dist/mocks.js.map +1 -0
  16. package/dist/types-CuNocrbJ.d.ts +2148 -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 +208 -160
  21. package/src/PluginDriver.ts +326 -565
  22. package/src/constants.ts +20 -47
  23. package/src/createAdapter.ts +16 -6
  24. package/src/createKubb.ts +548 -0
  25. package/src/createRenderer.ts +57 -0
  26. package/src/createStorage.ts +40 -26
  27. package/src/defineGenerator.ts +87 -0
  28. package/src/defineLogger.ts +19 -0
  29. package/src/defineMiddleware.ts +62 -0
  30. package/src/defineParser.ts +44 -0
  31. package/src/definePlugin.ts +83 -0
  32. package/src/defineResolver.ts +521 -0
  33. package/src/devtools.ts +14 -14
  34. package/src/index.ts +14 -17
  35. package/src/mocks.ts +178 -0
  36. package/src/renderNode.ts +35 -0
  37. package/src/storages/fsStorage.ts +41 -11
  38. package/src/storages/memoryStorage.ts +4 -2
  39. package/src/types.ts +1054 -270
  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-DRfJIbG1.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/createGenerator.ts +0 -106
  54. package/src/createLogger.ts +0 -7
  55. package/src/createPlugin.ts +0 -12
  56. package/src/errors.ts +0 -1
  57. package/src/hooks/index.ts +0 -4
  58. package/src/hooks/useKubb.ts +0 -138
  59. package/src/hooks/useMode.ts +0 -11
  60. package/src/hooks/usePlugin.ts +0 -11
  61. package/src/hooks/usePluginDriver.ts +0 -11
  62. package/src/utils/FunctionParams.ts +0 -155
  63. package/src/utils/TreeNode.ts +0 -215
  64. package/src/utils/executeStrategies.ts +0 -81
  65. package/src/utils/formatters.ts +0 -56
  66. package/src/utils/getBarrelFiles.ts +0 -141
  67. package/src/utils/getConfigs.ts +0 -30
  68. package/src/utils/getPlugins.ts +0 -23
  69. package/src/utils/linters.ts +0 -25
  70. package/src/utils/resolveOptions.ts +0 -93
@@ -0,0 +1,57 @@
1
+ import type { FileNode } from '@kubb/ast'
2
+
3
+ /**
4
+ * Minimal interface any Kubb renderer must satisfy.
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.
13
+ */
14
+ export type Renderer<TElement = unknown> = {
15
+ render(element: TElement): Promise<void>
16
+ unmount(error?: Error | number | null): void
17
+ readonly files: Array<FileNode>
18
+ }
19
+
20
+ /**
21
+ * A factory function that produces a fresh {@link Renderer} per render.
22
+ *
23
+ * Generators use this to declare which renderer handles their output.
24
+ */
25
+ export type RendererFactory<TElement = unknown> = () => Renderer<TElement>
26
+
27
+ /**
28
+ * Creates a renderer factory for use in generator definitions.
29
+ *
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.
33
+ *
34
+ * @example
35
+ * ```ts
36
+ * // packages/renderer-jsx/src/index.ts
37
+ * export const jsxRenderer = createRenderer(() => {
38
+ * const runtime = new Runtime()
39
+ * return {
40
+ * async render(element) { await runtime.render(element) },
41
+ * get files() { return runtime.nodes },
42
+ * unmount(error) { runtime.unmount(error) },
43
+ * }
44
+ * })
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
+ * ```
54
+ */
55
+ export function createRenderer<TElement = unknown>(factory: RendererFactory<TElement>): RendererFactory<TElement> {
56
+ return factory
57
+ }
@@ -1,43 +1,51 @@
1
- /**
2
- * Storage interface for persisting Kubb output.
3
- *
4
- * Keys are root-relative forward-slash paths (e.g. `src/gen/api/getPets.ts`).
5
- * Implement this interface to route generated files to any backend — filesystem,
6
- * S3, Redis, in-memory, etc.
7
- *
8
- * Use `createStorage` to create a typed storage driver.
9
- */
10
- export interface DefineStorage {
11
- /** Identifier used for logging and debugging (e.g. `'fs'`, `'s3'`). */
1
+ export type Storage = {
2
+ /**
3
+ * Identifier used for logging and debugging (e.g. `'fs'`, `'s3'`).
4
+ */
12
5
  readonly name: string
13
- /** Returns `true` when an entry for `key` exists in storage. */
6
+ /**
7
+ * Returns `true` when an entry for `key` exists in storage.
8
+ */
14
9
  hasItem(key: string): Promise<boolean>
15
- /** Returns the stored string value, or `null` when `key` does not exist. */
10
+ /**
11
+ * Returns the stored string value, or `null` when `key` does not exist.
12
+ */
16
13
  getItem(key: string): Promise<string | null>
17
- /** Persists `value` under `key`, creating any required structure. */
14
+ /**
15
+ * Persists `value` under `key`, creating any required structure.
16
+ */
18
17
  setItem(key: string, value: string): Promise<void>
19
- /** Removes the entry for `key`. No-ops when the key does not exist. */
18
+ /**
19
+ * Removes the entry for `key`. No-ops when the key does not exist.
20
+ */
20
21
  removeItem(key: string): Promise<void>
21
- /** Returns all keys, optionally filtered to those starting with `base`. */
22
+ /**
23
+ * Returns all keys, optionally filtered to those starting with `base`.
24
+ */
22
25
  getKeys(base?: string): Promise<Array<string>>
23
- /** Removes all entries, optionally scoped to those starting with `base`. */
26
+ /**
27
+ * Removes all entries, optionally scoped to those starting with `base`.
28
+ */
24
29
  clear(base?: string): Promise<void>
25
- /** Optional teardown hook called after the build completes. */
30
+ /**
31
+ * Optional teardown hook called after the build completes.
32
+ */
26
33
  dispose?(): Promise<void>
27
34
  }
28
35
 
29
36
  /**
30
- * Wraps a storage builder so the `options` argument is optional, following the
31
- * same factory pattern as `createPlugin`, `createLogger`, and `createAdapter`.
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.
32
41
  *
33
- * The builder receives the resolved options object and must return a
34
- * `DefineStorage`-compatible object that includes a `name` string.
42
+ * @note Call the returned factory with optional options to instantiate the storage adapter.
35
43
  *
36
44
  * @example
37
45
  * ```ts
38
46
  * import { createStorage } from '@kubb/core'
39
47
  *
40
- * export const memoryStorage = createStorage((_options) => {
48
+ * export const memoryStorage = createStorage(() => {
41
49
  * const store = new Map<string, string>()
42
50
  * return {
43
51
  * name: 'memory',
@@ -45,12 +53,18 @@ export interface DefineStorage {
45
53
  * async getItem(key) { return store.get(key) ?? null },
46
54
  * async setItem(key, value) { store.set(key, value) },
47
55
  * async removeItem(key) { store.delete(key) },
48
- * async getKeys() { return [...store.keys()] },
49
- * async clear() { store.clear() },
56
+ * async getKeys(base) {
57
+ * const keys = [...store.keys()]
58
+ * return base ? keys.filter((k) => k.startsWith(base)) : keys
59
+ * },
60
+ * async clear(base) { if (!base) store.clear() },
50
61
  * }
51
62
  * })
63
+ *
64
+ * // Instantiate:
65
+ * const storage = memoryStorage()
52
66
  * ```
53
67
  */
54
- export function createStorage<TOptions = Record<string, never>>(build: (options: TOptions) => DefineStorage): (options?: TOptions) => DefineStorage {
68
+ export function createStorage<TOptions = Record<string, never>>(build: (options: TOptions) => Storage): (options?: TOptions) => Storage {
55
69
  return (options) => build(options ?? ({} as TOptions))
56
70
  }
@@ -0,0 +1,87 @@
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
+
6
+ export type { GeneratorContext } from './types.ts'
7
+
8
+ /**
9
+ * Declares a named generator unit that walks the AST and emits files.
10
+ *
11
+ * 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.
14
+ *
15
+ * @note Generators are consumed by plugins and registered via `ctx.addGenerator()` in `kubb:plugin:setup`.
16
+ *
17
+ * @example
18
+ * ```ts
19
+ * import { defineGenerator } from '@kubb/core'
20
+ * import { jsxRenderer } from '@kubb/renderer-jsx'
21
+ *
22
+ * export const typeGenerator = defineGenerator({
23
+ * name: 'typescript',
24
+ * renderer: jsxRenderer,
25
+ * schema(node, ctx) {
26
+ * const { adapter, resolver, root, options } = ctx
27
+ * return <File ...><Type node={node} resolver={resolver} /></File>
28
+ * },
29
+ * })
30
+ * ```
31
+ */
32
+ export type Generator<TOptions extends PluginFactoryOptions = PluginFactoryOptions, TElement = unknown> = {
33
+ /**
34
+ * Used in diagnostic messages and debug output.
35
+ */
36
+ name: string
37
+ /**
38
+ * Optional renderer factory that produces a {@link Renderer} for each render cycle.
39
+ *
40
+ * Generators that return renderer elements (e.g. JSX via `@kubb/renderer-jsx`) must set this
41
+ * to the matching renderer factory (e.g. `jsxRenderer` from `@kubb/renderer-jsx`).
42
+ *
43
+ * Generators that only return `Array<FileNode>` or `void` do not need to set this.
44
+ *
45
+ * Set `renderer: null` to explicitly opt out of rendering even when the parent plugin
46
+ * declares a `renderer` (overrides the plugin-level fallback).
47
+ *
48
+ * @example
49
+ * ```ts
50
+ * import { jsxRenderer } from '@kubb/renderer-jsx'
51
+ * export const myGenerator = defineGenerator<PluginTs>({
52
+ * renderer: jsxRenderer,
53
+ * schema(node, ctx) { return <File ...>...</File> },
54
+ * })
55
+ * ```
56
+ */
57
+ renderer?: RendererFactory<TElement> | null
58
+ /**
59
+ * Called for each schema node in the AST walk.
60
+ * `ctx` carries the plugin context with `adapter` and `inputNode` guaranteed present,
61
+ * plus `ctx.options` with the per-node resolved options (after exclude/include/override).
62
+ */
63
+ schema?: (node: SchemaNode, ctx: GeneratorContext<TOptions>) => PossiblePromise<TElement | Array<FileNode> | void>
64
+ /**
65
+ * Called for each operation node in the AST walk.
66
+ * `ctx` carries the plugin context with `adapter` and `inputNode` guaranteed present,
67
+ * plus `ctx.options` with the per-node resolved options (after exclude/include/override).
68
+ */
69
+ operation?: (node: OperationNode, ctx: GeneratorContext<TOptions>) => PossiblePromise<TElement | Array<FileNode> | void>
70
+ /**
71
+ * Called once after all operations have been walked.
72
+ * `ctx` carries the plugin context with `adapter` and `inputNode` guaranteed present,
73
+ * plus `ctx.options` with the plugin-level options for the batch call.
74
+ */
75
+ operations?: (nodes: Array<OperationNode>, ctx: GeneratorContext<TOptions>) => PossiblePromise<TElement | Array<FileNode> | void>
76
+ }
77
+
78
+ /**
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.
82
+ */
83
+ export function defineGenerator<TOptions extends PluginFactoryOptions = PluginFactoryOptions, TElement = unknown>(
84
+ generator: Generator<TOptions, TElement>,
85
+ ): Generator<TOptions, TElement> {
86
+ return generator
87
+ }
@@ -0,0 +1,19 @@
1
+ import type { Logger, LoggerOptions, UserLogger } from './types.ts'
2
+
3
+ /**
4
+ * Wraps a logger definition into a typed {@link Logger}.
5
+ *
6
+ * @example
7
+ * ```ts
8
+ * export const myLogger = defineLogger({
9
+ * 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))
13
+ * },
14
+ * })
15
+ * ```
16
+ */
17
+ export function defineLogger<Options extends LoggerOptions = LoggerOptions>(logger: UserLogger<Options>): Logger<Options> {
18
+ return logger
19
+ }
@@ -0,0 +1,62 @@
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
+ * 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.
29
+ *
30
+ * @note The factory can accept typed options. See examples for using options and per-build state patterns.
31
+ *
32
+ * @example
33
+ * ```ts
34
+ * import { defineMiddleware } from '@kubb/core'
35
+ *
36
+ * // Stateless middleware
37
+ * export const logMiddleware = defineMiddleware(() => ({
38
+ * name: 'log-middleware',
39
+ * hooks: {
40
+ * 'kubb:build:end'({ files }) {
41
+ * console.log(`Build complete with ${files.length} files`)
42
+ * },
43
+ * },
44
+ * }))
45
+ *
46
+ * // Middleware with options and per-build state
47
+ * export const prefixMiddleware = defineMiddleware((options: { prefix: string } = { prefix: '' }) => {
48
+ * const seen = new Set<string>()
49
+ * return {
50
+ * name: 'prefix-middleware',
51
+ * hooks: {
52
+ * 'kubb:plugin:end'({ plugin }) {
53
+ * seen.add(`${options.prefix}${plugin.name}`)
54
+ * },
55
+ * },
56
+ * }
57
+ * })
58
+ * ```
59
+ */
60
+ export function defineMiddleware<TOptions extends object = object>(factory: (options: TOptions) => Middleware): (options?: TOptions) => Middleware {
61
+ return (options) => factory(options ?? ({} as TOptions))
62
+ }
@@ -0,0 +1,44 @@
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. 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.
27
+ *
28
+ * @example
29
+ * ```ts
30
+ * import { defineParser } from '@kubb/core'
31
+ *
32
+ * export const jsonParser = defineParser({
33
+ * name: 'json',
34
+ * extNames: ['.json'],
35
+ * parse(file) {
36
+ * const { extractStringsFromNodes } = await import('@kubb/ast')
37
+ * return file.sources.map((s) => extractStringsFromNodes(s.nodes ?? [])).join('\n')
38
+ * },
39
+ * })
40
+ * ```
41
+ */
42
+ export function defineParser<TMeta extends object = any>(parser: Parser<TMeta>): Parser<TMeta> {
43
+ return parser
44
+ }
@@ -0,0 +1,83 @@
1
+ import type { KubbHooks } from './Kubb.ts'
2
+ import type { KubbPluginSetupContext, PluginFactoryOptions } from './types.ts'
3
+
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
+ }
55
+
56
+ /**
57
+ * Wraps a factory function and returns a typed `Plugin` with lifecycle handlers grouped under `hooks`.
58
+ *
59
+ * Handlers live in a single `hooks` object (inspired by Astro integrations).
60
+ * All lifecycle events from `KubbHooks` are available for subscription.
61
+ *
62
+ * @note For real plugins, use a `PluginFactoryOptions` type parameter to get type-safe context in `kubb:plugin:setup`.
63
+ * Plugin names should follow the convention `plugin-<feature>` (e.g., `plugin-react-query`, `plugin-zod`).
64
+ *
65
+ * @example
66
+ * ```ts
67
+ * import { definePlugin } from '@kubb/core'
68
+ *
69
+ * export const pluginTs = definePlugin((options: { prefix?: string } = {}) => ({
70
+ * name: 'plugin-ts',
71
+ * hooks: {
72
+ * 'kubb:plugin:setup'(ctx) {
73
+ * ctx.setResolver(resolverTs)
74
+ * },
75
+ * },
76
+ * }))
77
+ * ```
78
+ */
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']))
83
+ }