@kubb/core 5.0.0-beta.7 → 5.0.0-beta.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 (49) hide show
  1. package/LICENSE +17 -10
  2. package/README.md +20 -123
  3. package/dist/diagnostics-CJtO1uSM.d.ts +2892 -0
  4. package/dist/index.cjs +2340 -1129
  5. package/dist/index.cjs.map +1 -1
  6. package/dist/index.d.ts +80 -289
  7. package/dist/index.js +2330 -1124
  8. package/dist/index.js.map +1 -1
  9. package/dist/memoryStorage-B4VTTIpQ.js +905 -0
  10. package/dist/memoryStorage-B4VTTIpQ.js.map +1 -0
  11. package/dist/memoryStorage-CfycFGzX.cjs +1043 -0
  12. package/dist/memoryStorage-CfycFGzX.cjs.map +1 -0
  13. package/dist/mocks.cjs +84 -24
  14. package/dist/mocks.cjs.map +1 -1
  15. package/dist/mocks.d.ts +37 -11
  16. package/dist/mocks.js +86 -28
  17. package/dist/mocks.js.map +1 -1
  18. package/package.json +9 -23
  19. package/dist/PluginDriver-BkTRD2H2.js +0 -946
  20. package/dist/PluginDriver-BkTRD2H2.js.map +0 -1
  21. package/dist/PluginDriver-Cadu4ORh.cjs +0 -1037
  22. package/dist/PluginDriver-Cadu4ORh.cjs.map +0 -1
  23. package/dist/types-ChyWgIgi.d.ts +0 -2159
  24. package/src/FileManager.ts +0 -115
  25. package/src/FileProcessor.ts +0 -86
  26. package/src/Kubb.ts +0 -300
  27. package/src/PluginDriver.ts +0 -426
  28. package/src/constants.ts +0 -35
  29. package/src/createAdapter.ts +0 -32
  30. package/src/createKubb.ts +0 -573
  31. package/src/createRenderer.ts +0 -57
  32. package/src/createStorage.ts +0 -70
  33. package/src/defineGenerator.ts +0 -87
  34. package/src/defineLogger.ts +0 -36
  35. package/src/defineMiddleware.ts +0 -62
  36. package/src/defineParser.ts +0 -44
  37. package/src/definePlugin.ts +0 -83
  38. package/src/defineResolver.ts +0 -521
  39. package/src/devtools.ts +0 -59
  40. package/src/index.ts +0 -20
  41. package/src/mocks.ts +0 -178
  42. package/src/renderNode.ts +0 -35
  43. package/src/storages/fsStorage.ts +0 -114
  44. package/src/storages/memoryStorage.ts +0 -55
  45. package/src/types.ts +0 -1305
  46. package/src/utils/diagnostics.ts +0 -18
  47. package/src/utils/isInputPath.ts +0 -10
  48. package/src/utils/packageJSON.ts +0 -99
  49. /package/dist/{chunk--u3MIqq1.js → rolldown-runtime-C0LytTxp.js} +0 -0
@@ -1,87 +0,0 @@
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
- }
@@ -1,36 +0,0 @@
1
- import type { Logger, LoggerOptions, UserLogger } from './types.ts'
2
-
3
- /**
4
- * Wraps a logger definition into a typed {@link Logger}.
5
- *
6
- * The optional second type parameter `TInstallReturn` allows loggers to return
7
- * a value from `install` — for example, a sink factory that the caller can
8
- * forward to hook execution.
9
- *
10
- * @example Basic logger
11
- * ```ts
12
- * export const myLogger = defineLogger({
13
- * name: 'my-logger',
14
- * install(context, options) {
15
- * context.on('kubb:info', (message) => console.log('ℹ', message))
16
- * context.on('kubb:error', (error) => console.error('✗', error.message))
17
- * },
18
- * })
19
- * ```
20
- *
21
- * @example Logger that returns a hook sink factory
22
- * ```ts
23
- * export const myLogger = defineLogger<LoggerOptions, HookSinkFactory>({
24
- * name: 'my-logger',
25
- * install(context, options) {
26
- * // … register event handlers …
27
- * return (commandWithArgs) => ({ onStdout: console.log })
28
- * },
29
- * })
30
- * ```
31
- */
32
- export function defineLogger<Options extends LoggerOptions = LoggerOptions, TInstallReturn = void>(
33
- logger: UserLogger<Options, TInstallReturn>,
34
- ): Logger<Options, TInstallReturn> {
35
- return logger
36
- }
@@ -1,62 +0,0 @@
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
- }
@@ -1,44 +0,0 @@
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
- }
@@ -1,83 +0,0 @@
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
- }