@kubb/core 5.0.0-alpha.32 → 5.0.0-alpha.34
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.
- package/dist/{PluginDriver-nm7tvGs9.d.ts → PluginDriver-BBi_41VF.d.ts} +16 -36
- package/dist/hooks.cjs +4 -7
- package/dist/hooks.cjs.map +1 -1
- package/dist/hooks.d.ts +1 -1
- package/dist/hooks.js +4 -7
- package/dist/hooks.js.map +1 -1
- package/dist/index.cjs +132 -93
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +56 -5
- package/dist/index.js +132 -95
- package/dist/index.js.map +1 -1
- package/package.json +5 -5
- package/src/FileProcessor.ts +4 -3
- package/src/Kubb.ts +1 -1
- package/src/build.ts +0 -1
- package/src/defineGenerator.ts +4 -8
- package/src/defineParser.ts +4 -17
- package/src/hooks/useDriver.ts +2 -4
- package/src/hooks/useMode.ts +2 -4
- package/src/hooks/usePlugin.ts +2 -4
- package/src/index.ts +3 -1
- package/src/renderNode.tsx +11 -9
- package/src/types.ts +8 -8
- package/src/utils/getBarrelFiles.ts +0 -2
- package/src/utils/getFunctionParams.ts +247 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kubb/core",
|
|
3
|
-
"version": "5.0.0-alpha.
|
|
3
|
+
"version": "5.0.0-alpha.34",
|
|
4
4
|
"description": "Core functionality for Kubb's plugin-based code generation system, providing the foundation for transforming OpenAPI specifications.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"typescript",
|
|
@@ -64,13 +64,13 @@
|
|
|
64
64
|
}
|
|
65
65
|
],
|
|
66
66
|
"dependencies": {
|
|
67
|
-
"@kubb/react-fabric": "0.15.1",
|
|
68
67
|
"empathic": "^2.0.0",
|
|
69
68
|
"fflate": "^0.8.2",
|
|
70
69
|
"remeda": "^2.33.7",
|
|
71
70
|
"semver": "^7.7.4",
|
|
72
|
-
"tinyexec": "^1.
|
|
73
|
-
"@kubb/ast": "5.0.0-alpha.
|
|
71
|
+
"tinyexec": "^1.1.1",
|
|
72
|
+
"@kubb/ast": "5.0.0-alpha.34",
|
|
73
|
+
"@kubb/renderer-jsx": "5.0.0-alpha.34"
|
|
74
74
|
},
|
|
75
75
|
"devDependencies": {
|
|
76
76
|
"@types/semver": "^7.7.1",
|
|
@@ -78,7 +78,7 @@
|
|
|
78
78
|
"@internals/utils": "0.0.0"
|
|
79
79
|
},
|
|
80
80
|
"peerDependencies": {
|
|
81
|
-
"@kubb/
|
|
81
|
+
"@kubb/renderer-jsx": "5.0.0-alpha.34"
|
|
82
82
|
},
|
|
83
83
|
"engines": {
|
|
84
84
|
"node": ">=22"
|
package/src/FileProcessor.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { extractStringsFromNodes } from '@kubb/ast'
|
|
2
|
+
import type { CodeNode, FileNode } from '@kubb/ast/types'
|
|
2
3
|
import pLimit from 'p-limit'
|
|
3
4
|
import { PARALLEL_CONCURRENCY_LIMIT } from './constants.ts'
|
|
4
5
|
import type { Parser } from './defineParser.ts'
|
|
@@ -20,8 +21,8 @@ type RunOptions = ParseOptions & {
|
|
|
20
21
|
|
|
21
22
|
function joinSources(file: FileNode): string {
|
|
22
23
|
return file.sources
|
|
23
|
-
.map((item) => item.
|
|
24
|
-
.filter(
|
|
24
|
+
.map((item) => extractStringsFromNodes(item.nodes as Array<CodeNode>))
|
|
25
|
+
.filter(Boolean)
|
|
25
26
|
.join('\n\n')
|
|
26
27
|
}
|
|
27
28
|
|
package/src/Kubb.ts
CHANGED
package/src/build.ts
CHANGED
|
@@ -56,7 +56,6 @@ type SetupResult = {
|
|
|
56
56
|
*
|
|
57
57
|
* - Validates the input path (when applicable).
|
|
58
58
|
* - Applies config defaults (`root`, `output.*`, `devtools`).
|
|
59
|
-
* - Creates the Fabric instance and wires storage, format, and lint hooks.
|
|
60
59
|
* - Runs the adapter (if configured) to produce the universal `InputNode`.
|
|
61
60
|
* When no adapter is supplied and `@kubb/adapter-oas` is installed as an
|
|
62
61
|
*
|
package/src/defineGenerator.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { PossiblePromise } from '@internals/utils'
|
|
2
2
|
import type { FileNode, OperationNode, SchemaNode } from '@kubb/ast/types'
|
|
3
|
-
import type {
|
|
3
|
+
import type { KubbReactNode } from '@kubb/renderer-jsx/types'
|
|
4
4
|
import { applyHookResult } from './renderNode.tsx'
|
|
5
5
|
import type { GeneratorContext, PluginFactoryOptions } from './types.ts'
|
|
6
6
|
|
|
@@ -38,11 +38,7 @@ export type Generator<TOptions extends PluginFactoryOptions = PluginFactoryOptio
|
|
|
38
38
|
* `this` is the parent plugin's context with `adapter` and `inputNode` guaranteed present.
|
|
39
39
|
* `options` contains the per-node resolved options (after exclude/include/override).
|
|
40
40
|
*/
|
|
41
|
-
schema?: (
|
|
42
|
-
this: GeneratorContext<TOptions>,
|
|
43
|
-
node: SchemaNode,
|
|
44
|
-
options: TOptions['resolvedOptions'],
|
|
45
|
-
) => PossiblePromise<FabricReactNode | Array<FileNode> | void>
|
|
41
|
+
schema?: (this: GeneratorContext<TOptions>, node: SchemaNode, options: TOptions['resolvedOptions']) => PossiblePromise<KubbReactNode | Array<FileNode> | void>
|
|
46
42
|
/**
|
|
47
43
|
* Called for each operation node in the AST walk.
|
|
48
44
|
* `this` is the parent plugin's context with `adapter` and `inputNode` guaranteed present.
|
|
@@ -51,7 +47,7 @@ export type Generator<TOptions extends PluginFactoryOptions = PluginFactoryOptio
|
|
|
51
47
|
this: GeneratorContext<TOptions>,
|
|
52
48
|
node: OperationNode,
|
|
53
49
|
options: TOptions['resolvedOptions'],
|
|
54
|
-
) => PossiblePromise<
|
|
50
|
+
) => PossiblePromise<KubbReactNode | Array<FileNode> | void>
|
|
55
51
|
/**
|
|
56
52
|
* Called once after all operations have been walked.
|
|
57
53
|
* `this` is the parent plugin's context with `adapter` and `inputNode` guaranteed present.
|
|
@@ -60,7 +56,7 @@ export type Generator<TOptions extends PluginFactoryOptions = PluginFactoryOptio
|
|
|
60
56
|
this: GeneratorContext<TOptions>,
|
|
61
57
|
nodes: Array<OperationNode>,
|
|
62
58
|
options: TOptions['resolvedOptions'],
|
|
63
|
-
) => PossiblePromise<
|
|
59
|
+
) => PossiblePromise<KubbReactNode | Array<FileNode> | void>
|
|
64
60
|
}
|
|
65
61
|
|
|
66
62
|
/**
|
package/src/defineParser.ts
CHANGED
|
@@ -6,7 +6,6 @@ type PrintOptions = {
|
|
|
6
6
|
|
|
7
7
|
export type Parser<TMeta extends object = any> = {
|
|
8
8
|
name: string
|
|
9
|
-
type: 'parser'
|
|
10
9
|
/**
|
|
11
10
|
* File extensions this parser handles.
|
|
12
11
|
* Use `undefined` to create a catch-all fallback parser.
|
|
@@ -14,21 +13,12 @@ export type Parser<TMeta extends object = any> = {
|
|
|
14
13
|
* @example ['.ts', '.js']
|
|
15
14
|
*/
|
|
16
15
|
extNames: Array<FileNode['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
16
|
/**
|
|
23
17
|
* Convert a resolved file to a string.
|
|
24
18
|
*/
|
|
25
19
|
parse(file: FileNode<TMeta>, options?: PrintOptions): Promise<string> | string
|
|
26
20
|
}
|
|
27
21
|
|
|
28
|
-
export type UserParser<TMeta extends object = any> = Omit<Parser<TMeta>, 'type' | 'install'> & {
|
|
29
|
-
install?(...args: unknown[]): void | Promise<void>
|
|
30
|
-
}
|
|
31
|
-
|
|
32
22
|
/**
|
|
33
23
|
* Defines a parser with type safety.
|
|
34
24
|
*
|
|
@@ -43,15 +33,12 @@ export type UserParser<TMeta extends object = any> = Omit<Parser<TMeta>, 'type'
|
|
|
43
33
|
* name: 'json',
|
|
44
34
|
* extNames: ['.json'],
|
|
45
35
|
* parse(file) {
|
|
46
|
-
*
|
|
36
|
+
* const { extractStringsFromNodes } = await import('@kubb/ast')
|
|
37
|
+
* return file.sources.map((s) => extractStringsFromNodes(s.nodes ?? [])).join('\n')
|
|
47
38
|
* },
|
|
48
39
|
* })
|
|
49
40
|
* ```
|
|
50
41
|
*/
|
|
51
|
-
export function defineParser<TMeta extends object = any>(parser:
|
|
52
|
-
return
|
|
53
|
-
install() {},
|
|
54
|
-
type: 'parser',
|
|
55
|
-
...parser,
|
|
56
|
-
}
|
|
42
|
+
export function defineParser<TMeta extends object = any>(parser: Parser<TMeta>): Parser<TMeta> {
|
|
43
|
+
return parser
|
|
57
44
|
}
|
package/src/hooks/useDriver.ts
CHANGED
|
@@ -1,11 +1,9 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { inject, KubbContext } from '@kubb/renderer-jsx'
|
|
2
2
|
import type { PluginDriver } from '../PluginDriver.ts'
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
5
|
* @deprecated use `driver` from the generator component props instead
|
|
6
6
|
*/
|
|
7
7
|
export function useDriver(): PluginDriver {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
return meta.driver
|
|
8
|
+
return inject(KubbContext)!.driver as PluginDriver
|
|
11
9
|
}
|
package/src/hooks/useMode.ts
CHANGED
|
@@ -1,10 +1,8 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { inject, KubbContext } from '@kubb/renderer-jsx'
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* @deprecated use `mode` from the generator component props instead
|
|
5
5
|
*/
|
|
6
6
|
export function useMode(): 'single' | 'split' {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
return meta.mode
|
|
7
|
+
return inject(KubbContext)!.mode
|
|
10
8
|
}
|
package/src/hooks/usePlugin.ts
CHANGED
|
@@ -1,11 +1,9 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { inject, KubbContext } from '@kubb/renderer-jsx'
|
|
2
2
|
import type { Plugin, PluginFactoryOptions } from '../types.ts'
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
5
|
* @deprecated use `plugin` from the generator component props instead
|
|
6
6
|
*/
|
|
7
7
|
export function usePlugin<TOptions extends PluginFactoryOptions = PluginFactoryOptions>(): Plugin<TOptions> {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
return meta.plugin
|
|
8
|
+
return inject(KubbContext)!.plugin as Plugin<TOptions>
|
|
11
9
|
}
|
package/src/index.ts
CHANGED
|
@@ -23,10 +23,12 @@ export { getMode, PluginDriver } from './PluginDriver.ts'
|
|
|
23
23
|
export { fsStorage } from './storages/fsStorage.ts'
|
|
24
24
|
export { memoryStorage } from './storages/memoryStorage.ts'
|
|
25
25
|
export * from './types.ts'
|
|
26
|
-
export {
|
|
26
|
+
export type { FunctionParamsAST } from './utils/FunctionParams.ts'
|
|
27
27
|
export { detectFormatter } from './utils/formatters.ts'
|
|
28
28
|
export { getBarrelFiles } from './utils/getBarrelFiles.ts'
|
|
29
29
|
export { getConfigs } from './utils/getConfigs.ts'
|
|
30
|
+
export type { Param, Params } from './utils/getFunctionParams.ts'
|
|
31
|
+
export { createFunctionParams, FunctionParams, getFunctionParams } from './utils/getFunctionParams.ts'
|
|
30
32
|
export { getPreset } from './utils/getPreset.ts'
|
|
31
33
|
export { isInputPath } from './utils/isInputPath.ts'
|
|
32
34
|
export { detectLinter } from './utils/linters.ts'
|
package/src/renderNode.tsx
CHANGED
|
@@ -1,16 +1,16 @@
|
|
|
1
1
|
import type { FileNode } from '@kubb/ast/types'
|
|
2
|
-
import {
|
|
3
|
-
import type {
|
|
2
|
+
import { createRenderer } from '@kubb/renderer-jsx'
|
|
3
|
+
import type { KubbReactNode } from '@kubb/renderer-jsx/types'
|
|
4
4
|
import type { PluginDriver } from './PluginDriver.ts'
|
|
5
5
|
|
|
6
6
|
/**
|
|
7
7
|
* Handles the return value of a plugin AST hook or generator method.
|
|
8
8
|
*
|
|
9
|
-
* - React element → rendered via
|
|
9
|
+
* - React element → rendered via renderer-jsx, files stored in `driver.fileManager`
|
|
10
10
|
* - `Array<FileNode>` → upserted directly into `driver.fileManager`
|
|
11
11
|
* - `void` / `null` / `undefined` → no-op (plugin handled it via `this.upsertFile`)
|
|
12
12
|
*/
|
|
13
|
-
export async function applyHookResult(result:
|
|
13
|
+
export async function applyHookResult(result: KubbReactNode | Array<FileNode> | void, driver: PluginDriver): Promise<void> {
|
|
14
14
|
if (!result) return
|
|
15
15
|
|
|
16
16
|
if (Array.isArray(result)) {
|
|
@@ -18,9 +18,11 @@ export async function applyHookResult(result: FabricReactNode | Array<FileNode>
|
|
|
18
18
|
return
|
|
19
19
|
}
|
|
20
20
|
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
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()
|
|
26
28
|
}
|
package/src/types.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { AsyncEventEmitter, PossiblePromise } from '@internals/utils'
|
|
2
2
|
import type { FileNode, ImportNode, InputNode, Node, OperationNode, Printer, SchemaNode, Visitor } from '@kubb/ast/types'
|
|
3
3
|
import type { HttpMethod } from '@kubb/oas'
|
|
4
|
-
import type {
|
|
4
|
+
import type { KubbReactNode } from '@kubb/renderer-jsx/types'
|
|
5
5
|
import type { DEFAULT_STUDIO_URL, logLevel } from './constants.ts'
|
|
6
6
|
import type { Storage } from './createStorage.ts'
|
|
7
7
|
import type { Generator } from './defineGenerator.ts'
|
|
@@ -56,9 +56,9 @@ export type UserConfig<TInput = Input> = Omit<Config<TInput>, 'root' | 'plugins'
|
|
|
56
56
|
*
|
|
57
57
|
* A catch-all fallback parser is always appended last for any unhandled extension.
|
|
58
58
|
*
|
|
59
|
-
* When omitted, `
|
|
59
|
+
* When omitted, `parserTsx` from `@kubb/parser-ts` is used automatically as the
|
|
60
60
|
* default (requires `@kubb/parser-ts` to be installed as an optional dependency).
|
|
61
|
-
* @default [
|
|
61
|
+
* @default [parserTsx] — from `@kubb/parser-ts`
|
|
62
62
|
* @example
|
|
63
63
|
* ```ts
|
|
64
64
|
* import { parserTs, tsxParser } from '@kubb/parser-ts'
|
|
@@ -525,7 +525,7 @@ export type SchemaHook<TOptions extends PluginFactoryOptions = PluginFactoryOpti
|
|
|
525
525
|
this: GeneratorContext<TOptions>,
|
|
526
526
|
node: SchemaNode,
|
|
527
527
|
options: TOptions['resolvedOptions'],
|
|
528
|
-
) => PossiblePromise<
|
|
528
|
+
) => PossiblePromise<KubbReactNode | Array<FileNode> | void>
|
|
529
529
|
|
|
530
530
|
/**
|
|
531
531
|
* Handler for a single operation node. Used by the `operation` hook on a plugin.
|
|
@@ -534,7 +534,7 @@ export type OperationHook<TOptions extends PluginFactoryOptions = PluginFactoryO
|
|
|
534
534
|
this: GeneratorContext<TOptions>,
|
|
535
535
|
node: OperationNode,
|
|
536
536
|
options: TOptions['resolvedOptions'],
|
|
537
|
-
) => PossiblePromise<
|
|
537
|
+
) => PossiblePromise<KubbReactNode | Array<FileNode> | void>
|
|
538
538
|
|
|
539
539
|
/**
|
|
540
540
|
* Handler for all collected operation nodes. Used by the `operations` hook on a plugin.
|
|
@@ -543,7 +543,7 @@ export type OperationsHook<TOptions extends PluginFactoryOptions = PluginFactory
|
|
|
543
543
|
this: GeneratorContext<TOptions>,
|
|
544
544
|
nodes: Array<OperationNode>,
|
|
545
545
|
options: TOptions['resolvedOptions'],
|
|
546
|
-
) => PossiblePromise<
|
|
546
|
+
) => PossiblePromise<KubbReactNode | Array<FileNode> | void>
|
|
547
547
|
|
|
548
548
|
export type Plugin<TOptions extends PluginFactoryOptions = PluginFactoryOptions> = {
|
|
549
549
|
/**
|
|
@@ -879,7 +879,7 @@ export type LoggerOptions = {
|
|
|
879
879
|
}
|
|
880
880
|
|
|
881
881
|
/**
|
|
882
|
-
* Shared context passed to all plugins, parsers, and
|
|
882
|
+
* Shared context passed to all plugins, parsers, and other internals.
|
|
883
883
|
*/
|
|
884
884
|
export type LoggerContext = AsyncEventEmitter<KubbEvents>
|
|
885
885
|
|
|
@@ -1091,6 +1091,6 @@ export type ResolveBannerContext = {
|
|
|
1091
1091
|
}
|
|
1092
1092
|
|
|
1093
1093
|
export type { CLIOptions, ConfigInput } from './defineConfig.ts'
|
|
1094
|
-
export type { Parser
|
|
1094
|
+
export type { Parser } from './defineParser.ts'
|
|
1095
1095
|
export type { FunctionParamsAST } from './utils/FunctionParams.ts'
|
|
1096
1096
|
export type { FileMetaBase } from './utils/getBarrelFiles.ts'
|
|
@@ -80,8 +80,6 @@ function getBarrelFilesByRoot(root: string | undefined, files: Array<FileNode>):
|
|
|
80
80
|
createSource({
|
|
81
81
|
name: source.name,
|
|
82
82
|
isTypeOnly: source.isTypeOnly,
|
|
83
|
-
//TODO use parser to generate import
|
|
84
|
-
value: '',
|
|
85
83
|
isExportable: false,
|
|
86
84
|
isIndexable: false,
|
|
87
85
|
}),
|
|
@@ -0,0 +1,247 @@
|
|
|
1
|
+
import { sortBy } from 'remeda'
|
|
2
|
+
|
|
3
|
+
export type Param = {
|
|
4
|
+
/**
|
|
5
|
+
* `object` will return the pathParams as an object.
|
|
6
|
+
*
|
|
7
|
+
* `inline` will return the pathParams as comma separated params.
|
|
8
|
+
* @default `'inline'`
|
|
9
|
+
* @private
|
|
10
|
+
*/
|
|
11
|
+
mode?: 'object' | 'inline' | 'inlineSpread'
|
|
12
|
+
type?: 'string' | 'number' | (string & {})
|
|
13
|
+
optional?: boolean
|
|
14
|
+
/**
|
|
15
|
+
* @example test = "default"
|
|
16
|
+
*/
|
|
17
|
+
default?: string
|
|
18
|
+
/**
|
|
19
|
+
* Used for no TypeScript(with mode object)
|
|
20
|
+
* @example test: "default"
|
|
21
|
+
*/
|
|
22
|
+
value?: string
|
|
23
|
+
children?: Params
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
type ParamItem =
|
|
27
|
+
| (Pick<Param, 'mode' | 'type' | 'value'> & {
|
|
28
|
+
optional?: true
|
|
29
|
+
default?: never
|
|
30
|
+
children?: Params
|
|
31
|
+
})
|
|
32
|
+
| (Pick<Param, 'mode' | 'type' | 'value'> & {
|
|
33
|
+
optional?: false
|
|
34
|
+
default?: string
|
|
35
|
+
children?: Params
|
|
36
|
+
})
|
|
37
|
+
|
|
38
|
+
export type Params = Record<string, Param | undefined>
|
|
39
|
+
|
|
40
|
+
type Options = {
|
|
41
|
+
type: 'constructor' | 'call' | 'object' | 'objectValue'
|
|
42
|
+
transformName?: (name: string) => string
|
|
43
|
+
transformType?: (type: string) => string
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function order(items: Array<[key: string, item?: ParamItem]>) {
|
|
47
|
+
return sortBy(items.filter(Boolean) as Array<[key: string, item?: ParamItem]>, ([_key, item]) => {
|
|
48
|
+
if (item?.children) {
|
|
49
|
+
return 0 // Treat items with children as required (they'll get = {} if all children are optional)
|
|
50
|
+
}
|
|
51
|
+
// Priority order: required (0) → optional (1) → default-only (2)
|
|
52
|
+
if (item?.optional) {
|
|
53
|
+
return 1 // Optional parameters (with or without default)
|
|
54
|
+
}
|
|
55
|
+
if (item?.default) {
|
|
56
|
+
// Parameters with default only (not marked as optional)
|
|
57
|
+
// Note: While the ParamItem type suggests optional and default are mutually exclusive,
|
|
58
|
+
// this handles the case where a parameter has a default value but isn't explicitly marked as optional
|
|
59
|
+
return 2
|
|
60
|
+
}
|
|
61
|
+
return 0 // Required parameters
|
|
62
|
+
})
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
function parseChild(key: string, item: ParamItem, options: Options): string | null {
|
|
66
|
+
// @ts-expect-error
|
|
67
|
+
const entries = order(Object.entries(item.children))
|
|
68
|
+
|
|
69
|
+
const types: string[] = []
|
|
70
|
+
const names: string[] = []
|
|
71
|
+
|
|
72
|
+
const optional = entries.every(([_key, item]) => item?.optional || !!item?.default)
|
|
73
|
+
|
|
74
|
+
entries.forEach(([key, entryItem]) => {
|
|
75
|
+
if (entryItem) {
|
|
76
|
+
const name = parseItem(key, { ...entryItem, type: undefined }, options)
|
|
77
|
+
if (entryItem.children) {
|
|
78
|
+
const subTypes = Object.entries(entryItem.children)
|
|
79
|
+
.map(([key]) => {
|
|
80
|
+
return key
|
|
81
|
+
})
|
|
82
|
+
.join(', ')
|
|
83
|
+
|
|
84
|
+
if (subTypes) {
|
|
85
|
+
names.push(`${name}: { ${subTypes} }`)
|
|
86
|
+
} else {
|
|
87
|
+
names.push(name)
|
|
88
|
+
}
|
|
89
|
+
} else {
|
|
90
|
+
if (options.type === 'call' && options.transformName) {
|
|
91
|
+
names.push(`${key}: ${name}`)
|
|
92
|
+
} else {
|
|
93
|
+
names.push(name)
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
if (entries.some(([_key, item]) => item?.type)) {
|
|
98
|
+
types.push(parseItem(key, { ...entryItem, default: undefined }, options))
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
})
|
|
102
|
+
|
|
103
|
+
const name = item.mode === 'inline' ? key : names.length ? `{ ${names.join(', ')} }` : undefined
|
|
104
|
+
const type = item.type ? item.type : types.length ? `{ ${types.join('; ')} }` : undefined
|
|
105
|
+
|
|
106
|
+
if (!name) {
|
|
107
|
+
return null
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
return parseItem(
|
|
111
|
+
name,
|
|
112
|
+
{
|
|
113
|
+
type,
|
|
114
|
+
default: item.default,
|
|
115
|
+
optional: !item.default ? optional : undefined,
|
|
116
|
+
} as ParamItem,
|
|
117
|
+
options,
|
|
118
|
+
)
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
function parseItem(name: string, item: ParamItem, options: Options): string {
|
|
122
|
+
const acc: string[] = []
|
|
123
|
+
const transformedName = options.transformName ? options.transformName(name) : name
|
|
124
|
+
const transformedType = options.transformType && item.type ? options.transformType(item.type) : item.type
|
|
125
|
+
|
|
126
|
+
if (options.type === 'object') {
|
|
127
|
+
return transformedName
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
if (options.type === 'objectValue') {
|
|
131
|
+
return item.value ? `${transformedName}: ${item.value}` : transformedName
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
//LEGACY
|
|
135
|
+
if (item.type && options.type === 'constructor') {
|
|
136
|
+
if (item.optional) {
|
|
137
|
+
// Check if this is a destructured parameter (object mode)
|
|
138
|
+
const isDestructured = transformedName.startsWith('{')
|
|
139
|
+
if (isDestructured) {
|
|
140
|
+
// For destructured parameters, use ": type = {}" syntax to make it optional
|
|
141
|
+
acc.push(`${transformedName}: ${transformedType} = {}`)
|
|
142
|
+
} else {
|
|
143
|
+
// For inline parameters, use "?: type" syntax
|
|
144
|
+
acc.push(`${transformedName}?: ${transformedType}`)
|
|
145
|
+
}
|
|
146
|
+
} else {
|
|
147
|
+
acc.push(`${transformedName}: ${transformedType}${item.default ? ` = ${item.default}` : ''}`)
|
|
148
|
+
}
|
|
149
|
+
} else if (item.default && options.type === 'constructor') {
|
|
150
|
+
acc.push(`${transformedName} = ${item.default}`)
|
|
151
|
+
} else if (item.value) {
|
|
152
|
+
acc.push(`${transformedName} : ${item.value}`)
|
|
153
|
+
} else if (item.mode === 'inlineSpread') {
|
|
154
|
+
acc.push(`... ${transformedName}`)
|
|
155
|
+
} else {
|
|
156
|
+
acc.push(transformedName)
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
return acc[0] as string
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
export function getFunctionParams(params: Params, options: Options): string {
|
|
163
|
+
const entries = order(Object.entries(params as Record<string, ParamItem | undefined>))
|
|
164
|
+
|
|
165
|
+
return entries
|
|
166
|
+
.reduce((acc, [key, item]) => {
|
|
167
|
+
if (!item) {
|
|
168
|
+
return acc
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
if (item.children) {
|
|
172
|
+
if (Object.keys(item.children).length === 0) {
|
|
173
|
+
return acc
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
if (item.mode === 'inlineSpread') {
|
|
177
|
+
return [...acc, getFunctionParams(item.children, options)]
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
const parsedItem = parseChild(key, item, options)
|
|
181
|
+
if (!parsedItem) {
|
|
182
|
+
return acc
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
return [...acc, parsedItem]
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
const parsedItem = parseItem(key, item, options)
|
|
189
|
+
|
|
190
|
+
return [...acc, parsedItem]
|
|
191
|
+
}, [] as string[])
|
|
192
|
+
.join(', ')
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
/**
|
|
196
|
+
* @deprecated use @kubb/ast
|
|
197
|
+
*/
|
|
198
|
+
export function createFunctionParams(params: Params): Params {
|
|
199
|
+
return params
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
/**
|
|
203
|
+
* @deprecated use @kubb/ast
|
|
204
|
+
*/
|
|
205
|
+
export class FunctionParams {
|
|
206
|
+
#params: Params
|
|
207
|
+
|
|
208
|
+
static factory(params: Params) {
|
|
209
|
+
return new FunctionParams(params)
|
|
210
|
+
}
|
|
211
|
+
constructor(params: Params) {
|
|
212
|
+
this.#params = params
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
get params(): Params {
|
|
216
|
+
return this.#params
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
get flatParams(): Params {
|
|
220
|
+
const flatter = (acc: Params, [key, item]: [key: string, item?: Param]): Params => {
|
|
221
|
+
if (item?.children) {
|
|
222
|
+
return Object.entries(item.children).reduce(flatter, acc)
|
|
223
|
+
}
|
|
224
|
+
if (item) {
|
|
225
|
+
acc[key] = item
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
return acc
|
|
229
|
+
}
|
|
230
|
+
return Object.entries(this.#params).reduce(flatter, {} as Params)
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
toCall({ transformName, transformType }: Pick<Options, 'transformName' | 'transformType'> = {}): string {
|
|
234
|
+
return getFunctionParams(this.#params, { type: 'call', transformName, transformType })
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
toObject(): string {
|
|
238
|
+
return getFunctionParams(this.#params, { type: 'object' })
|
|
239
|
+
}
|
|
240
|
+
toObjectValue(): string {
|
|
241
|
+
return getFunctionParams(this.#params, { type: 'objectValue' })
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
toConstructor(): string {
|
|
245
|
+
return getFunctionParams(this.#params, { type: 'constructor' })
|
|
246
|
+
}
|
|
247
|
+
}
|