@kubb/core 5.0.0-alpha.3 → 5.0.0-alpha.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 (60) hide show
  1. package/dist/PluginDriver-D0dY_hpJ.d.ts +1986 -0
  2. package/dist/{chunk-ByKO4r7w.cjs → chunk-MlS0t1Af.cjs} +15 -0
  3. package/dist/chunk-O_arW02_.js +17 -0
  4. package/dist/hooks.cjs +13 -28
  5. package/dist/hooks.cjs.map +1 -1
  6. package/dist/hooks.d.ts +11 -37
  7. package/dist/hooks.js +14 -28
  8. package/dist/hooks.js.map +1 -1
  9. package/dist/index.cjs +1469 -831
  10. package/dist/index.cjs.map +1 -1
  11. package/dist/index.d.ts +572 -191
  12. package/dist/index.js +1443 -826
  13. package/dist/index.js.map +1 -1
  14. package/package.json +7 -7
  15. package/src/Kubb.ts +38 -56
  16. package/src/KubbFile.ts +143 -0
  17. package/src/{PluginManager.ts → PluginDriver.ts} +159 -170
  18. package/src/build.ts +213 -65
  19. package/src/constants.ts +39 -6
  20. package/src/createAdapter.ts +25 -0
  21. package/src/createPlugin.ts +30 -0
  22. package/src/createStorage.ts +58 -0
  23. package/src/{config.ts → defineConfig.ts} +11 -16
  24. package/src/defineGenerator.ts +126 -0
  25. package/src/defineLogger.ts +13 -3
  26. package/src/defineParser.ts +57 -0
  27. package/src/definePresets.ts +16 -0
  28. package/src/defineResolver.ts +454 -0
  29. package/src/hooks/index.ts +1 -6
  30. package/src/hooks/useDriver.ts +11 -0
  31. package/src/hooks/useMode.ts +4 -4
  32. package/src/hooks/usePlugin.ts +3 -3
  33. package/src/index.ts +22 -10
  34. package/src/renderNode.tsx +25 -0
  35. package/src/storages/fsStorage.ts +2 -2
  36. package/src/storages/memoryStorage.ts +2 -2
  37. package/src/types.ts +639 -52
  38. package/src/utils/FunctionParams.ts +2 -2
  39. package/src/utils/TreeNode.ts +40 -2
  40. package/src/utils/diagnostics.ts +4 -1
  41. package/src/utils/executeStrategies.ts +29 -10
  42. package/src/utils/formatters.ts +10 -21
  43. package/src/utils/getBarrelFiles.ts +80 -10
  44. package/src/utils/getConfigs.ts +9 -23
  45. package/src/utils/getPreset.ts +78 -0
  46. package/src/utils/isInputPath.ts +8 -0
  47. package/src/utils/linters.ts +23 -3
  48. package/src/utils/packageJSON.ts +76 -0
  49. package/dist/chunk--u3MIqq1.js +0 -8
  50. package/dist/types-CiPWLv-5.d.ts +0 -1001
  51. package/src/BarrelManager.ts +0 -74
  52. package/src/PackageManager.ts +0 -180
  53. package/src/PromiseManager.ts +0 -40
  54. package/src/defineAdapter.ts +0 -22
  55. package/src/definePlugin.ts +0 -12
  56. package/src/defineStorage.ts +0 -56
  57. package/src/errors.ts +0 -1
  58. package/src/hooks/useKubb.ts +0 -22
  59. package/src/hooks/usePluginManager.ts +0 -11
  60. package/src/utils/getPlugins.ts +0 -23
@@ -1,16 +1,18 @@
1
1
  import type { PossiblePromise } from '@internals/utils'
2
- import type { InputPath, UserConfig } from './types.ts'
2
+ import type { UserConfig } from './types.ts'
3
3
 
4
4
  /**
5
5
  * CLI options derived from command-line flags.
6
6
  */
7
7
  export type CLIOptions = {
8
- /** Path to `kubb.config.js` */
8
+ /**
9
+ * Path to `kubb.config.js`.
10
+ */
9
11
  config?: string
10
-
11
- /** Enable watch mode for input files */
12
+ /**
13
+ * Enable watch mode for input files.
14
+ */
12
15
  watch?: boolean
13
-
14
16
  /**
15
17
  * Logging verbosity for CLI usage.
16
18
  *
@@ -20,12 +22,11 @@ export type CLIOptions = {
20
22
  * @default 'silent'
21
23
  */
22
24
  logLevel?: 'silent' | 'info' | 'debug'
23
-
24
- /** Run Kubb with Bun */
25
- bun?: boolean
26
25
  }
27
26
 
28
- /** All accepted forms of a Kubb configuration. */
27
+ /**
28
+ * All accepted forms of a Kubb configuration.
29
+ */
29
30
  export type ConfigInput = PossiblePromise<UserConfig | UserConfig[]> | ((cli: CLIOptions) => PossiblePromise<UserConfig | UserConfig[]>)
30
31
 
31
32
  /**
@@ -41,16 +42,10 @@ export type ConfigInput = PossiblePromise<UserConfig | UserConfig[]> | ((cli: CL
41
42
  * root: 'src',
42
43
  * plugins: [myPlugin()],
43
44
  * }))
45
+ * @deprecated as of Kubb v5, @kubb/core will not expose `defineConfig` anymore. use the `kubb` package instead
44
46
  */
45
47
  export function defineConfig(config: (cli: CLIOptions) => PossiblePromise<UserConfig | UserConfig[]>): typeof config
46
48
  export function defineConfig(config: PossiblePromise<UserConfig | UserConfig[]>): typeof config
47
49
  export function defineConfig(config: ConfigInput): ConfigInput {
48
50
  return config
49
51
  }
50
-
51
- /**
52
- * Type guard to check if a given config has an `input.path`.
53
- */
54
- export function isInputPath(config: UserConfig | undefined): config is UserConfig<InputPath> {
55
- return typeof config?.input === 'object' && config.input !== null && 'path' in config.input
56
- }
@@ -0,0 +1,126 @@
1
+ import type { PossiblePromise } from '@internals/utils'
2
+ import type { OperationNode, SchemaNode } from '@kubb/ast/types'
3
+ import type { FabricReactNode } from '@kubb/react-fabric/types'
4
+ import type * as KubbFile from './KubbFile.ts'
5
+ import { applyHookResult } from './renderNode.tsx'
6
+ import type { GeneratorContext, PluginFactoryOptions } from './types.ts'
7
+
8
+ export type { GeneratorContext } from './types.ts'
9
+
10
+ /**
11
+ * A generator is a named object with optional `schema`, `operation`, and `operations`
12
+ * methods. Each method is called with `this = PluginContext` of the parent plugin,
13
+ * giving full access to `this.config`, `this.resolver`, `this.adapter`, `this.fabric`,
14
+ * `this.driver`, etc.
15
+ *
16
+ * Return a React element, an array of `KubbFile.File`, or `void` to handle file
17
+ * writing manually via `this.upsertFile`. Both React and core (non-React) generators
18
+ * use the same method signatures — the return type determines how output is handled.
19
+ *
20
+ * @example
21
+ * ```ts
22
+ * export const typeGenerator = defineGenerator<PluginTs>({
23
+ * name: 'typescript',
24
+ * schema(node, options) {
25
+ * const { adapter, resolver, root } = this
26
+ * return <File ...><Type node={node} resolver={resolver} /></File>
27
+ * },
28
+ * operation(node, options) {
29
+ * return <File ...><OperationType node={node} /></File>
30
+ * },
31
+ * })
32
+ * ```
33
+ */
34
+ export type Generator<TOptions extends PluginFactoryOptions = PluginFactoryOptions> = {
35
+ /** Used in diagnostic messages and debug output. */
36
+ name: string
37
+ /**
38
+ * Called for each schema node in the AST walk.
39
+ * `this` is the parent plugin's context with `adapter` and `rootNode` guaranteed present.
40
+ * `options` contains the per-node resolved options (after exclude/include/override).
41
+ */
42
+ schema?: (
43
+ this: GeneratorContext<TOptions>,
44
+ node: SchemaNode,
45
+ options: TOptions['resolvedOptions'],
46
+ ) => PossiblePromise<FabricReactNode | Array<KubbFile.File> | void>
47
+ /**
48
+ * Called for each operation node in the AST walk.
49
+ * `this` is the parent plugin's context with `adapter` and `rootNode` guaranteed present.
50
+ */
51
+ operation?: (
52
+ this: GeneratorContext<TOptions>,
53
+ node: OperationNode,
54
+ options: TOptions['resolvedOptions'],
55
+ ) => PossiblePromise<FabricReactNode | Array<KubbFile.File> | void>
56
+ /**
57
+ * Called once after all operations have been walked.
58
+ * `this` is the parent plugin's context with `adapter` and `rootNode` guaranteed present.
59
+ */
60
+ operations?: (
61
+ this: GeneratorContext<TOptions>,
62
+ nodes: Array<OperationNode>,
63
+ options: TOptions['resolvedOptions'],
64
+ ) => PossiblePromise<FabricReactNode | Array<KubbFile.File> | void>
65
+ }
66
+
67
+ /**
68
+ * Defines a generator. Returns the object as-is with correct `this` typings.
69
+ * No type discrimination (`type: 'react' | 'core'`) needed — `applyHookResult`
70
+ * handles React elements and `File[]` uniformly.
71
+ */
72
+ export function defineGenerator<TOptions extends PluginFactoryOptions = PluginFactoryOptions>(generator: Generator<TOptions>): Generator<TOptions> {
73
+ return generator
74
+ }
75
+
76
+ /**
77
+ * Merges an array of generators into a single generator.
78
+ *
79
+ * The merged generator's `schema`, `operation`, and `operations` methods run
80
+ * the corresponding method from each input generator in sequence, applying each
81
+ * result via `applyHookResult`. This eliminates the need to write the loop
82
+ * manually in each plugin.
83
+ *
84
+ * @param generators - Array of generators to merge into a single generator.
85
+ *
86
+ * @example
87
+ * ```ts
88
+ * const merged = mergeGenerators(generators)
89
+ *
90
+ * return {
91
+ * name: pluginName,
92
+ * schema: merged.schema,
93
+ * operation: merged.operation,
94
+ * operations: merged.operations,
95
+ * }
96
+ * ```
97
+ */
98
+ export function mergeGenerators<TOptions extends PluginFactoryOptions = PluginFactoryOptions>(generators: Array<Generator<TOptions>>): Generator<TOptions> {
99
+ return {
100
+ name: generators.length > 0 ? generators.map((g) => g.name).join('+') : 'merged',
101
+ async schema(node, options) {
102
+ for (const gen of generators) {
103
+ if (!gen.schema) continue
104
+ const result = await gen.schema.call(this, node, options)
105
+
106
+ await applyHookResult(result, this.fabric)
107
+ }
108
+ },
109
+ async operation(node, options) {
110
+ for (const gen of generators) {
111
+ if (!gen.operation) continue
112
+ const result = await gen.operation.call(this, node, options)
113
+
114
+ await applyHookResult(result, this.fabric)
115
+ }
116
+ },
117
+ async operations(nodes, options) {
118
+ for (const gen of generators) {
119
+ if (!gen.operations) continue
120
+ const result = await gen.operations.call(this, nodes, options)
121
+
122
+ await applyHookResult(result, this.fabric)
123
+ }
124
+ },
125
+ }
126
+ }
@@ -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('info', (message) => console.log('ℹ', message))
11
+ * context.on('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,57 @@
1
+ import type * as KubbFile from './KubbFile.ts'
2
+
3
+ type PrintOptions = {
4
+ extname?: KubbFile.Extname
5
+ }
6
+
7
+ export type Parser<TMeta extends object = any> = {
8
+ name: string
9
+ type: 'parser'
10
+ /**
11
+ * File extensions this parser handles.
12
+ * Use `undefined` to create a catch-all fallback parser.
13
+ *
14
+ * @example ['.ts', '.js']
15
+ */
16
+ extNames: Array<KubbFile.Extname> | undefined
17
+ /**
18
+ * @deprecated Will be removed once Fabric no longer requires it.
19
+ * @default () => {}
20
+ */
21
+ install(...args: unknown[]): void | Promise<void>
22
+ /**
23
+ * Convert a resolved file to a string.
24
+ */
25
+ parse(file: KubbFile.ResolvedFile<TMeta>, options?: PrintOptions): Promise<string> | string
26
+ }
27
+
28
+ export type UserParser<TMeta extends object = any> = Omit<Parser<TMeta>, 'type' | 'install'> & {
29
+ install?(...args: unknown[]): void | Promise<void>
30
+ }
31
+
32
+ /**
33
+ * Defines a parser with type safety.
34
+ *
35
+ * Use this function to create parsers that transform generated files to strings
36
+ * based on their extension.
37
+ *
38
+ * @example
39
+ * ```ts
40
+ * import { defineParser } from '@kubb/core'
41
+ *
42
+ * export const jsonParser = defineParser({
43
+ * name: 'json',
44
+ * extNames: ['.json'],
45
+ * parse(file) {
46
+ * return file.sources.map((s) => s.value).join('\n')
47
+ * },
48
+ * })
49
+ * ```
50
+ */
51
+ export function defineParser<TMeta extends object = any>(parser: UserParser<TMeta>): Parser<TMeta> {
52
+ return {
53
+ install() {},
54
+ type: 'parser',
55
+ ...parser,
56
+ }
57
+ }
@@ -0,0 +1,16 @@
1
+ import type { Preset, Presets, Resolver } from './types.ts'
2
+
3
+ /**
4
+ * Creates a typed presets registry object — a named collection of {@link Preset} entries.
5
+ *
6
+ * @example
7
+ * import { definePreset, definePresets } from '@kubb/core'
8
+ * import { resolverTsLegacy } from '@kubb/plugin-ts'
9
+ *
10
+ * export const myPresets = definePresets({
11
+ * kubbV4: definePreset('kubbV4', { resolvers: [resolverTsLegacy] }),
12
+ * })
13
+ */
14
+ export function definePresets<TResolver extends Resolver = Resolver>(presets: Presets<TResolver>): Presets<TResolver> {
15
+ return presets
16
+ }