@kubb/core 5.0.0-alpha.34 → 5.0.0-alpha.36

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 (45) hide show
  1. package/dist/PluginDriver-B_65W4fv.js +1677 -0
  2. package/dist/PluginDriver-B_65W4fv.js.map +1 -0
  3. package/dist/{PluginDriver-BBi_41VF.d.ts → PluginDriver-C9iBgYbk.d.ts} +743 -376
  4. package/dist/PluginDriver-CCdkwR14.cjs +1806 -0
  5. package/dist/PluginDriver-CCdkwR14.cjs.map +1 -0
  6. package/dist/hooks.d.ts +1 -1
  7. package/dist/index.cjs +272 -1666
  8. package/dist/index.cjs.map +1 -1
  9. package/dist/index.d.ts +62 -141
  10. package/dist/index.js +231 -1623
  11. package/dist/index.js.map +1 -1
  12. package/dist/mocks.cjs +165 -0
  13. package/dist/mocks.cjs.map +1 -0
  14. package/dist/mocks.d.ts +74 -0
  15. package/dist/mocks.js +159 -0
  16. package/dist/mocks.js.map +1 -0
  17. package/package.json +11 -5
  18. package/src/FileManager.ts +1 -1
  19. package/src/FileProcessor.ts +1 -1
  20. package/src/Kubb.ts +145 -38
  21. package/src/PluginDriver.ts +318 -40
  22. package/src/constants.ts +1 -1
  23. package/src/{build.ts → createKubb.ts} +180 -122
  24. package/src/createPlugin.ts +1 -0
  25. package/src/createRenderer.ts +57 -0
  26. package/src/defineGenerator.ts +57 -84
  27. package/src/defineLogger.ts +2 -2
  28. package/src/defineParser.ts +3 -2
  29. package/src/definePlugin.ts +95 -0
  30. package/src/defineResolver.ts +1 -1
  31. package/src/devtools.ts +1 -1
  32. package/src/index.ts +7 -6
  33. package/src/mocks.ts +234 -0
  34. package/src/renderNode.ts +35 -0
  35. package/src/types.ts +275 -210
  36. package/src/utils/TreeNode.ts +1 -1
  37. package/src/utils/getBarrelFiles.ts +3 -3
  38. package/src/utils/getFunctionParams.ts +14 -7
  39. package/src/utils/isInputPath.ts +2 -2
  40. package/src/utils/packageJSON.ts +2 -3
  41. package/src/defineConfig.ts +0 -51
  42. package/src/definePresets.ts +0 -16
  43. package/src/renderNode.tsx +0 -28
  44. package/src/utils/getConfigs.ts +0 -16
  45. package/src/utils/getPreset.ts +0 -78
@@ -1,5 +1,5 @@
1
1
  import path from 'node:path'
2
- import type { FileNode } from '@kubb/ast/types'
2
+ import type { FileNode } from '@kubb/ast'
3
3
  import { getMode } from '../PluginDriver.ts'
4
4
 
5
5
  type BarrelData = {
@@ -1,8 +1,8 @@
1
1
  /** biome-ignore-all lint/suspicious/useIterableCallbackReturn: not needed */
2
2
  import { join } from 'node:path'
3
3
  import { getRelativePath } from '@internals/utils'
4
+ import type { FileNode } from '@kubb/ast'
4
5
  import { createExport, createFile, createSource } from '@kubb/ast'
5
- import type { FileNode } from '@kubb/ast/types'
6
6
  import type { BarrelType } from '../types.ts'
7
7
  import { TreeNode } from './TreeNode.ts'
8
8
 
@@ -13,11 +13,11 @@ export type FileMetaBase = {
13
13
  type AddIndexesProps = {
14
14
  type: BarrelType | false | undefined
15
15
  /**
16
- * Root based on root and output.path specified in the config
16
+ * Absolute output root derived from config `root` and `output.path`.
17
17
  */
18
18
  root: string
19
19
  /**
20
- * Output for plugin
20
+ * Output settings for the plugin.
21
21
  */
22
22
  output: {
23
23
  path: string
@@ -2,22 +2,29 @@ import { sortBy } from 'remeda'
2
2
 
3
3
  export type Param = {
4
4
  /**
5
- * `object` will return the pathParams as an object.
5
+ * Controls how path parameters are emitted in the function signature.
6
+ * - `'object'` groups them as a single destructured parameter.
7
+ * - `'inline'` spreads them as individual comma-separated parameters.
8
+ * - `'inlineSpread'` emits a single rest parameter.
6
9
  *
7
- * `inline` will return the pathParams as comma separated params.
8
- * @default `'inline'`
9
- * @private
10
+ * @default 'inline'
11
+ * @internal
10
12
  */
11
13
  mode?: 'object' | 'inline' | 'inlineSpread'
12
14
  type?: 'string' | 'number' | (string & {})
13
15
  optional?: boolean
14
16
  /**
15
- * @example test = "default"
17
+ * Default value expression for the parameter.
18
+ *
19
+ * @example Assignment syntax
20
+ * `test = "default"`
16
21
  */
17
22
  default?: string
18
23
  /**
19
- * Used for no TypeScript(with mode object)
20
- * @example test: "default"
24
+ * Used for no TypeScript (with mode object).
25
+ *
26
+ * @example Value syntax
27
+ * `test: "default"`
21
28
  */
22
29
  value?: string
23
30
  children?: Params
@@ -1,8 +1,8 @@
1
- import type { InputPath, UserConfig } from '../types'
1
+ import type { Config, InputPath } from '../types'
2
2
 
3
3
  /**
4
4
  * Type guard to check if a given config has an `input.path`.
5
5
  */
6
- export function isInputPath(config: UserConfig | undefined): config is UserConfig<InputPath> {
6
+ export function isInputPath(config: Config | undefined): config is Config<InputPath> {
7
7
  return typeof config?.input === 'object' && config.input !== null && 'path' in config.input
8
8
  }
@@ -1,5 +1,4 @@
1
- import { readSync } from '@internals/utils'
2
- import * as pkg from 'empathic/package'
1
+ import { findPackageJSON, readSync } from '@internals/utils'
3
2
  import { coerce, satisfies } from 'semver'
4
3
 
5
4
  type PackageJSON = {
@@ -11,7 +10,7 @@ type DependencyName = string
11
10
  type DependencyVersion = string
12
11
 
13
12
  function getPackageJSONSync(cwd?: string): PackageJSON | null {
14
- const pkgPath = pkg.up({ cwd })
13
+ const pkgPath = findPackageJSON(cwd)
15
14
  if (!pkgPath) {
16
15
  return null
17
16
  }
@@ -1,51 +0,0 @@
1
- import type { PossiblePromise } from '@internals/utils'
2
- import type { UserConfig } from './types.ts'
3
-
4
- /**
5
- * CLI options derived from command-line flags.
6
- */
7
- export type CLIOptions = {
8
- /**
9
- * Path to `kubb.config.js`.
10
- */
11
- config?: string
12
- /**
13
- * Enable watch mode for input files.
14
- */
15
- watch?: boolean
16
- /**
17
- * Logging verbosity for CLI usage.
18
- *
19
- * - `silent`: hide non-essential logs
20
- * - `info`: show general logs (non-plugin-related)
21
- * - `debug`: include detailed plugin lifecycle logs
22
- * @default 'silent'
23
- */
24
- logLevel?: 'silent' | 'info' | 'debug'
25
- }
26
-
27
- /**
28
- * All accepted forms of a Kubb configuration.
29
- */
30
- export type ConfigInput = PossiblePromise<UserConfig | UserConfig[]> | ((cli: CLIOptions) => PossiblePromise<UserConfig | UserConfig[]>)
31
-
32
- /**
33
- * Helper for defining a Kubb configuration.
34
- *
35
- * Accepts either:
36
- * - A config object or array of configs
37
- * - A function returning the config(s), optionally async,
38
- * receiving the CLI options as argument
39
- *
40
- * @example
41
- * export default defineConfig(({ logLevel }) => ({
42
- * root: 'src',
43
- * plugins: [myPlugin()],
44
- * }))
45
- * @deprecated as of Kubb v5, @kubb/core will not expose `defineConfig` anymore. use the `kubb` package instead
46
- */
47
- export function defineConfig(config: (cli: CLIOptions) => PossiblePromise<UserConfig | UserConfig[]>): typeof config
48
- export function defineConfig(config: PossiblePromise<UserConfig | UserConfig[]>): typeof config
49
- export function defineConfig(config: ConfigInput): ConfigInput {
50
- return config
51
- }
@@ -1,16 +0,0 @@
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
- }
@@ -1,28 +0,0 @@
1
- import type { FileNode } from '@kubb/ast/types'
2
- import { createRenderer } from '@kubb/renderer-jsx'
3
- import type { KubbReactNode } from '@kubb/renderer-jsx/types'
4
- import type { PluginDriver } from './PluginDriver.ts'
5
-
6
- /**
7
- * Handles the return value of a plugin AST hook or generator method.
8
- *
9
- * - React element → rendered via renderer-jsx, files stored in `driver.fileManager`
10
- * - `Array<FileNode>` → upserted directly into `driver.fileManager`
11
- * - `void` / `null` / `undefined` → no-op (plugin handled it via `this.upsertFile`)
12
- */
13
- export async function applyHookResult(result: KubbReactNode | Array<FileNode> | void, driver: PluginDriver): Promise<void> {
14
- if (!result) return
15
-
16
- if (Array.isArray(result)) {
17
- driver.fileManager.upsert(...(result as Array<FileNode>))
18
- return
19
- }
20
-
21
- const renderer = createRenderer()
22
-
23
- // biome-ignore lint/complexity/noUselessFragments: not needed
24
- await renderer.render(<>{result as KubbReactNode}</>)
25
-
26
- driver.fileManager.upsert(...renderer.files)
27
- renderer.unmount()
28
- }
@@ -1,16 +0,0 @@
1
- import type { CLIOptions, ConfigInput } from '../defineConfig.ts'
2
- import type { Config, UserConfig } from '../types.ts'
3
-
4
- /**
5
- * Resolves a {@link ConfigInput} into a normalized array of {@link Config} objects.
6
- *
7
- * - Awaits the config when it is a `Promise`.
8
- * - Calls the factory function with `args` when the config is a function.
9
- * - Wraps a single config object in an array for uniform downstream handling.
10
- */
11
- export async function getConfigs(config: ConfigInput | UserConfig, args: CLIOptions): Promise<Array<Config>> {
12
- const resolved = await (typeof config === 'function' ? config(args as CLIOptions) : config)
13
- const userConfigs = Array.isArray(resolved) ? resolved : [resolved]
14
-
15
- return userConfigs.map((item) => ({ plugins: [], ...item }) as Config)
16
- }
@@ -1,78 +0,0 @@
1
- import { composeTransformers } from '@kubb/ast'
2
- import type { Visitor } from '@kubb/ast/types'
3
- import type { CompatibilityPreset, Generator, Preset, Presets, Resolver } from '../types.ts'
4
-
5
- /**
6
- * Returns a copy of `defaults` where each function in `userOverrides` is wrapped
7
- * so a `null`/`undefined` return falls back to the original. Non-function values
8
- * are assigned directly. All calls use the merged object as `this`.
9
- */
10
- function withFallback<T extends object>(defaults: T, userOverrides: Partial<T>): T {
11
- const merged = { ...defaults } as T
12
-
13
- for (const key of Object.keys(userOverrides) as Array<keyof T>) {
14
- const userVal = userOverrides[key]
15
- const defaultVal = defaults[key]
16
-
17
- if (typeof userVal === 'function' && typeof defaultVal === 'function') {
18
- ;(merged as any)[key] = (...args: any[]) => (userVal as Function).apply(merged, args) ?? (defaultVal as Function).apply(merged, args)
19
- } else if (userVal !== undefined) {
20
- merged[key] = userVal as T[typeof key]
21
- }
22
- }
23
-
24
- return merged
25
- }
26
-
27
- type GetPresetParams<TResolver extends Resolver> = {
28
- preset: CompatibilityPreset
29
- presets: Presets<TResolver>
30
- /**
31
- * Optional single resolver whose methods override the preset resolver.
32
- * When a method returns `null` or `undefined` the preset resolver's method is used instead.
33
- */
34
- resolver?: Partial<TResolver> & ThisType<TResolver>
35
- /**
36
- * User-supplied generators to append after the preset's generators.
37
- */
38
- generators?: Array<Generator<any>>
39
- /**
40
- * Optional single transformer visitor whose methods override the preset transformer.
41
- * When a method returns `null` or `undefined` the preset transformer's method is used instead.
42
- */
43
- transformer?: Visitor
44
- }
45
-
46
- type GetPresetResult<TResolver extends Resolver> = {
47
- resolver: TResolver
48
- transformer: Visitor | undefined
49
- generators: Array<Generator<any>>
50
- preset: Preset<TResolver> | undefined
51
- }
52
-
53
- /**
54
- * Resolves a named preset into a resolver, transformer, and generators.
55
- *
56
- * - Selects the preset resolver; wraps it with user overrides using null/undefined fallback.
57
- * - Composes the preset's transformers into a single visitor; wraps it with the user transformer using null/undefined fallback.
58
- * - Combines preset generators with user-supplied generators; falls back to the `default` preset's generators when neither provides any.
59
- */
60
- export function getPreset<TResolver extends Resolver = Resolver>(params: GetPresetParams<TResolver>): GetPresetResult<TResolver> {
61
- const { preset: presetName, presets, resolver: userResolver, transformer: userTransformer, generators: userGenerators = [] } = params
62
- const preset = presets[presetName]
63
-
64
- const presetResolver = preset?.resolver ?? presets['default']!.resolver
65
- const resolver = userResolver ? withFallback(presetResolver, userResolver) : presetResolver
66
-
67
- const presetTransformers = preset?.transformers ?? []
68
- const presetTransformer = presetTransformers.length > 0 ? composeTransformers(...presetTransformers) : undefined
69
- const transformer = presetTransformer && userTransformer ? withFallback(presetTransformer, userTransformer) : (userTransformer ?? presetTransformer)
70
-
71
- const presetGenerators = preset?.generators ?? []
72
- const defaultGenerators = presets['default']?.generators ?? []
73
- const generators = (presetGenerators.length > 0 || userGenerators.length > 0 ? [...presetGenerators, ...userGenerators] : defaultGenerators) as Array<
74
- Generator<any>
75
- >
76
-
77
- return { resolver, transformer, generators, preset }
78
- }