@kubb/core 5.0.0-alpha.3 → 5.0.0-alpha.30
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-D110FoJ-.d.ts +1632 -0
- package/dist/hooks.cjs +12 -27
- package/dist/hooks.cjs.map +1 -1
- package/dist/hooks.d.ts +11 -36
- package/dist/hooks.js +13 -27
- package/dist/hooks.js.map +1 -1
- package/dist/index.cjs +1410 -823
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +597 -95
- package/dist/index.js +1391 -818
- package/dist/index.js.map +1 -1
- package/package.json +7 -7
- package/src/Kubb.ts +40 -58
- package/src/{PluginManager.ts → PluginDriver.ts} +165 -177
- package/src/build.ts +167 -44
- package/src/config.ts +9 -8
- package/src/constants.ts +40 -7
- package/src/createAdapter.ts +25 -0
- package/src/createPlugin.ts +30 -0
- package/src/createStorage.ts +58 -0
- package/src/defineGenerator.ts +126 -0
- package/src/defineLogger.ts +13 -3
- package/src/definePresets.ts +16 -0
- package/src/defineResolver.ts +457 -0
- package/src/hooks/index.ts +1 -6
- package/src/hooks/useDriver.ts +11 -0
- package/src/hooks/useMode.ts +5 -5
- package/src/hooks/usePlugin.ts +3 -3
- package/src/index.ts +18 -7
- package/src/renderNode.tsx +25 -0
- package/src/storages/fsStorage.ts +2 -2
- package/src/storages/memoryStorage.ts +2 -2
- package/src/types.ts +589 -52
- package/src/utils/FunctionParams.ts +2 -2
- package/src/utils/TreeNode.ts +45 -7
- package/src/utils/diagnostics.ts +4 -1
- package/src/utils/executeStrategies.ts +29 -10
- package/src/utils/formatters.ts +10 -21
- package/src/utils/getBarrelFiles.ts +83 -10
- package/src/utils/getConfigs.ts +8 -22
- package/src/utils/getPreset.ts +78 -0
- package/src/utils/linters.ts +23 -3
- package/src/utils/packageJSON.ts +76 -0
- package/dist/types-CiPWLv-5.d.ts +0 -1001
- package/src/BarrelManager.ts +0 -74
- package/src/PackageManager.ts +0 -180
- package/src/PromiseManager.ts +0 -40
- package/src/defineAdapter.ts +0 -22
- package/src/definePlugin.ts +0 -12
- package/src/defineStorage.ts +0 -56
- package/src/errors.ts +0 -1
- package/src/hooks/useKubb.ts +0 -22
- package/src/hooks/usePluginManager.ts +0 -11
- package/src/utils/getPlugins.ts +0 -23
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
import type { PossiblePromise } from '@internals/utils'
|
|
2
|
+
import type { OperationNode, SchemaNode } from '@kubb/ast/types'
|
|
3
|
+
import type { FabricFile } from '@kubb/fabric-core/types'
|
|
4
|
+
import type { FabricReactNode } from '@kubb/react-fabric/types'
|
|
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 `FabricFile.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<FabricFile.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<FabricFile.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<FabricFile.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
|
+
}
|
package/src/defineLogger.ts
CHANGED
|
@@ -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,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
|
+
}
|
|
@@ -0,0 +1,457 @@
|
|
|
1
|
+
import path from 'node:path'
|
|
2
|
+
import { camelCase, pascalCase } from '@internals/utils'
|
|
3
|
+
import { isOperationNode, isSchemaNode } from '@kubb/ast'
|
|
4
|
+
import type { Node, OperationNode, RootNode, SchemaNode } from '@kubb/ast/types'
|
|
5
|
+
import type { FabricFile } from '@kubb/fabric-core/types'
|
|
6
|
+
import { getMode } from './PluginDriver.ts'
|
|
7
|
+
import type {
|
|
8
|
+
Config,
|
|
9
|
+
PluginFactoryOptions,
|
|
10
|
+
ResolveBannerContext,
|
|
11
|
+
ResolveNameParams,
|
|
12
|
+
ResolveOptionsContext,
|
|
13
|
+
Resolver,
|
|
14
|
+
ResolverContext,
|
|
15
|
+
ResolverFileParams,
|
|
16
|
+
ResolverPathParams,
|
|
17
|
+
} from './types.ts'
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Builder type for the plugin-specific resolver fields.
|
|
21
|
+
*
|
|
22
|
+
* `default`, `resolveOptions`, `resolvePath`, `resolveFile`, `resolveBanner`, and `resolveFooter`
|
|
23
|
+
* are optional — built-in fallbacks are injected when omitted.
|
|
24
|
+
*/
|
|
25
|
+
type ResolverBuilder<T extends PluginFactoryOptions> = () => Omit<
|
|
26
|
+
T['resolver'],
|
|
27
|
+
'default' | 'resolveOptions' | 'resolvePath' | 'resolveFile' | 'resolveBanner' | 'resolveFooter' | 'name' | 'pluginName'
|
|
28
|
+
> &
|
|
29
|
+
Partial<Pick<T['resolver'], 'default' | 'resolveOptions' | 'resolvePath' | 'resolveFile' | 'resolveBanner' | 'resolveFooter'>> & {
|
|
30
|
+
name: string
|
|
31
|
+
pluginName: T['name']
|
|
32
|
+
} & ThisType<T['resolver']>
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Checks if an operation matches a pattern for a given filter type (`tag`, `operationId`, `path`, `method`).
|
|
36
|
+
*/
|
|
37
|
+
function matchesOperationPattern(node: OperationNode, type: string, pattern: string | RegExp): boolean {
|
|
38
|
+
switch (type) {
|
|
39
|
+
case 'tag':
|
|
40
|
+
return node.tags.some((tag) => !!tag.match(pattern))
|
|
41
|
+
case 'operationId':
|
|
42
|
+
return !!node.operationId.match(pattern)
|
|
43
|
+
case 'path':
|
|
44
|
+
return !!node.path.match(pattern)
|
|
45
|
+
case 'method':
|
|
46
|
+
return !!(node.method.toLowerCase() as string).match(pattern)
|
|
47
|
+
case 'contentType':
|
|
48
|
+
return !!node.requestBody?.contentType?.match(pattern)
|
|
49
|
+
default:
|
|
50
|
+
return false
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Checks if a schema matches a pattern for a given filter type (`schemaName`).
|
|
56
|
+
*
|
|
57
|
+
* Returns `null` when the filter type doesn't apply to schemas.
|
|
58
|
+
*/
|
|
59
|
+
function matchesSchemaPattern(node: SchemaNode, type: string, pattern: string | RegExp): boolean | null {
|
|
60
|
+
switch (type) {
|
|
61
|
+
case 'schemaName':
|
|
62
|
+
return node.name ? !!node.name.match(pattern) : false
|
|
63
|
+
default:
|
|
64
|
+
return null
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Default name resolver used by `defineResolver`.
|
|
70
|
+
*
|
|
71
|
+
* - `camelCase` for `function` and `file` types.
|
|
72
|
+
* - `PascalCase` for `type`.
|
|
73
|
+
* - `camelCase` for everything else.
|
|
74
|
+
*/
|
|
75
|
+
function defaultResolver(name: ResolveNameParams['name'], type: ResolveNameParams['type']): string {
|
|
76
|
+
let resolvedName = camelCase(name)
|
|
77
|
+
|
|
78
|
+
if (type === 'file' || type === 'function') {
|
|
79
|
+
resolvedName = camelCase(name, {
|
|
80
|
+
isFile: type === 'file',
|
|
81
|
+
})
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
if (type === 'type') {
|
|
85
|
+
resolvedName = pascalCase(name)
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
return resolvedName
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Default option resolver — applies include/exclude filters and merges matching override options.
|
|
93
|
+
*
|
|
94
|
+
* Returns `null` when the node is filtered out by an `exclude` rule or not matched by any `include` rule.
|
|
95
|
+
*
|
|
96
|
+
* @example Include/exclude filtering
|
|
97
|
+
* ```ts
|
|
98
|
+
* const options = defaultResolveOptions(operationNode, {
|
|
99
|
+
* options: { output: 'types' },
|
|
100
|
+
* exclude: [{ type: 'tag', pattern: 'internal' }],
|
|
101
|
+
* })
|
|
102
|
+
* // → null when node has tag 'internal'
|
|
103
|
+
* ```
|
|
104
|
+
*
|
|
105
|
+
* @example Override merging
|
|
106
|
+
* ```ts
|
|
107
|
+
* const options = defaultResolveOptions(operationNode, {
|
|
108
|
+
* options: { enumType: 'asConst' },
|
|
109
|
+
* override: [{ type: 'operationId', pattern: 'listPets', options: { enumType: 'enum' } }],
|
|
110
|
+
* })
|
|
111
|
+
* // → { enumType: 'enum' } when operationId matches
|
|
112
|
+
* ```
|
|
113
|
+
*/
|
|
114
|
+
export function defaultResolveOptions<TOptions>(
|
|
115
|
+
node: Node,
|
|
116
|
+
{ options, exclude = [], include, override = [] }: ResolveOptionsContext<TOptions>,
|
|
117
|
+
): TOptions | null {
|
|
118
|
+
if (isOperationNode(node)) {
|
|
119
|
+
const isExcluded = exclude.some(({ type, pattern }) => matchesOperationPattern(node, type, pattern))
|
|
120
|
+
if (isExcluded) {
|
|
121
|
+
return null
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
if (include && !include.some(({ type, pattern }) => matchesOperationPattern(node, type, pattern))) {
|
|
125
|
+
return null
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
const overrideOptions = override.find(({ type, pattern }) => matchesOperationPattern(node, type, pattern))?.options
|
|
129
|
+
|
|
130
|
+
return { ...options, ...overrideOptions }
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
if (isSchemaNode(node)) {
|
|
134
|
+
if (exclude.some(({ type, pattern }) => matchesSchemaPattern(node, type, pattern) === true)) {
|
|
135
|
+
return null
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
if (include) {
|
|
139
|
+
const results = include.map(({ type, pattern }) => matchesSchemaPattern(node, type, pattern))
|
|
140
|
+
const applicable = results.filter((r) => r !== null)
|
|
141
|
+
if (applicable.length > 0 && !applicable.includes(true)) {
|
|
142
|
+
return null
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
const overrideOptions = override.find(({ type, pattern }) => matchesSchemaPattern(node, type, pattern) === true)?.options
|
|
147
|
+
|
|
148
|
+
return { ...options, ...overrideOptions }
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
return options
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* Default path resolver used by `defineResolver`.
|
|
156
|
+
*
|
|
157
|
+
* - Returns the output directory in `single` mode.
|
|
158
|
+
* - Resolves into a tag- or path-based subdirectory when `group` and a `tag`/`path` value are provided.
|
|
159
|
+
* - Falls back to a flat `output/baseName` path otherwise.
|
|
160
|
+
*
|
|
161
|
+
* A custom `group.name` function overrides the default subdirectory naming.
|
|
162
|
+
* For `tag` groups the default is `${camelCase(tag)}Controller`.
|
|
163
|
+
* For `path` groups the default is the first path segment after `/`.
|
|
164
|
+
*
|
|
165
|
+
* @example Flat output
|
|
166
|
+
* ```ts
|
|
167
|
+
* defaultResolvePath({ baseName: 'petTypes.ts' }, { root: '/src', output: { path: 'types' } })
|
|
168
|
+
* // → '/src/types/petTypes.ts'
|
|
169
|
+
* ```
|
|
170
|
+
*
|
|
171
|
+
* @example Tag-based grouping
|
|
172
|
+
* ```ts
|
|
173
|
+
* defaultResolvePath(
|
|
174
|
+
* { baseName: 'petTypes.ts', tag: 'pets' },
|
|
175
|
+
* { root: '/src', output: { path: 'types' }, group: { type: 'tag' } },
|
|
176
|
+
* )
|
|
177
|
+
* // → '/src/types/petsController/petTypes.ts'
|
|
178
|
+
* ```
|
|
179
|
+
*
|
|
180
|
+
* @example Path-based grouping
|
|
181
|
+
* ```ts
|
|
182
|
+
* defaultResolvePath(
|
|
183
|
+
* { baseName: 'petTypes.ts', path: '/pets/list' },
|
|
184
|
+
* { root: '/src', output: { path: 'types' }, group: { type: 'path' } },
|
|
185
|
+
* )
|
|
186
|
+
* // → '/src/types/pets/petTypes.ts'
|
|
187
|
+
* ```
|
|
188
|
+
*
|
|
189
|
+
* @example Single-file mode
|
|
190
|
+
* ```ts
|
|
191
|
+
* defaultResolvePath(
|
|
192
|
+
* { baseName: 'petTypes.ts', pathMode: 'single' },
|
|
193
|
+
* { root: '/src', output: { path: 'types' } },
|
|
194
|
+
* )
|
|
195
|
+
* // → '/src/types'
|
|
196
|
+
* ```
|
|
197
|
+
*/
|
|
198
|
+
export function defaultResolvePath(
|
|
199
|
+
{ baseName, pathMode, tag, path: groupPath }: ResolverPathParams,
|
|
200
|
+
{ root, output, group }: ResolverContext,
|
|
201
|
+
): FabricFile.Path {
|
|
202
|
+
const mode = pathMode ?? getMode(path.resolve(root, output.path))
|
|
203
|
+
|
|
204
|
+
if (mode === 'single') {
|
|
205
|
+
return path.resolve(root, output.path) as FabricFile.Path
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
if (group && (groupPath || tag)) {
|
|
209
|
+
return path.resolve(root, output.path, group.name({ group: group.type === 'path' ? groupPath! : tag! }), baseName) as FabricFile.Path
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
return path.resolve(root, output.path, baseName) as FabricFile.Path
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
/**
|
|
216
|
+
* Default file resolver used by `defineResolver`.
|
|
217
|
+
*
|
|
218
|
+
* Resolves a `FabricFile.File` by combining name resolution (`resolver.default`) with
|
|
219
|
+
* path resolution (`resolver.resolvePath`). The resolved file always has empty
|
|
220
|
+
* `sources`, `imports`, and `exports` arrays — consumers populate those separately.
|
|
221
|
+
*
|
|
222
|
+
* In `single` mode the name is omitted and the file sits directly in the output directory.
|
|
223
|
+
*
|
|
224
|
+
* @example Resolve a schema file
|
|
225
|
+
* ```ts
|
|
226
|
+
* const file = defaultResolveFile.call(resolver,
|
|
227
|
+
* { name: 'pet', extname: '.ts' },
|
|
228
|
+
* { root: '/src', output: { path: 'types' } },
|
|
229
|
+
* )
|
|
230
|
+
* // → { baseName: 'pet.ts', path: '/src/types/pet.ts', sources: [], ... }
|
|
231
|
+
* ```
|
|
232
|
+
*
|
|
233
|
+
* @example Resolve an operation file with tag grouping
|
|
234
|
+
* ```ts
|
|
235
|
+
* const file = defaultResolveFile.call(resolver,
|
|
236
|
+
* { name: 'listPets', extname: '.ts', tag: 'pets' },
|
|
237
|
+
* { root: '/src', output: { path: 'types' }, group: { type: 'tag' } },
|
|
238
|
+
* )
|
|
239
|
+
* // → { baseName: 'listPets.ts', path: '/src/types/petsController/listPets.ts', ... }
|
|
240
|
+
* ```
|
|
241
|
+
*/
|
|
242
|
+
export function defaultResolveFile(this: Resolver, { name, extname, tag, path: groupPath }: ResolverFileParams, context: ResolverContext): FabricFile.File {
|
|
243
|
+
const pathMode = getMode(path.resolve(context.root, context.output.path))
|
|
244
|
+
const resolvedName = pathMode === 'single' ? '' : this.default(name, 'file')
|
|
245
|
+
const baseName = `${resolvedName}${extname}` as FabricFile.BaseName
|
|
246
|
+
const filePath = this.resolvePath({ baseName, pathMode, tag, path: groupPath }, context)
|
|
247
|
+
|
|
248
|
+
return {
|
|
249
|
+
path: filePath,
|
|
250
|
+
baseName: path.basename(filePath) as FabricFile.BaseName,
|
|
251
|
+
meta: {
|
|
252
|
+
pluginName: this.pluginName,
|
|
253
|
+
},
|
|
254
|
+
sources: [],
|
|
255
|
+
imports: [],
|
|
256
|
+
exports: [],
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
/**
|
|
261
|
+
* Generates the default "Generated by Kubb" banner from config and optional node metadata.
|
|
262
|
+
*/
|
|
263
|
+
export function buildDefaultBanner({
|
|
264
|
+
title,
|
|
265
|
+
description,
|
|
266
|
+
version,
|
|
267
|
+
config,
|
|
268
|
+
}: {
|
|
269
|
+
title?: string
|
|
270
|
+
description?: string
|
|
271
|
+
version?: string
|
|
272
|
+
config: Config
|
|
273
|
+
}): string {
|
|
274
|
+
try {
|
|
275
|
+
let source = ''
|
|
276
|
+
if (Array.isArray(config.input)) {
|
|
277
|
+
const first = config.input[0]
|
|
278
|
+
if (first && 'path' in first) {
|
|
279
|
+
source = path.basename(first.path)
|
|
280
|
+
}
|
|
281
|
+
} else if ('path' in config.input) {
|
|
282
|
+
source = path.basename(config.input.path)
|
|
283
|
+
} else if ('data' in config.input) {
|
|
284
|
+
source = 'text content'
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
let banner = '/**\n* Generated by Kubb (https://kubb.dev/).\n* Do not edit manually.\n'
|
|
288
|
+
|
|
289
|
+
if (config.output.defaultBanner === 'simple') {
|
|
290
|
+
banner += '*/\n'
|
|
291
|
+
return banner
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
if (source) {
|
|
295
|
+
banner += `* Source: ${source}\n`
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
if (title) {
|
|
299
|
+
banner += `* Title: ${title}\n`
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
if (description) {
|
|
303
|
+
const formattedDescription = description.replace(/\n/gm, '\n* ')
|
|
304
|
+
banner += `* Description: ${formattedDescription}\n`
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
if (version) {
|
|
308
|
+
banner += `* OpenAPI spec version: ${version}\n`
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
banner += '*/\n'
|
|
312
|
+
return banner
|
|
313
|
+
} catch (_error) {
|
|
314
|
+
return '/**\n* Generated by Kubb (https://kubb.dev/).\n* Do not edit manually.\n*/'
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
/**
|
|
319
|
+
* Default banner resolver — returns the banner string for a generated file.
|
|
320
|
+
*
|
|
321
|
+
* A user-supplied `output.banner` overrides the default Kubb "Generated by Kubb" notice.
|
|
322
|
+
* When no `output.banner` is set, the Kubb notice is used (including `title` and `version`
|
|
323
|
+
* from the OAS spec when a `node` is provided).
|
|
324
|
+
*
|
|
325
|
+
* - When `output.banner` is a function and `node` is provided, returns `output.banner(node)`.
|
|
326
|
+
* - When `output.banner` is a function and `node` is absent, falls back to the Kubb notice.
|
|
327
|
+
* - When `output.banner` is a string, returns it directly.
|
|
328
|
+
* - When `config.output.defaultBanner` is `false`, returns `undefined`.
|
|
329
|
+
* - Otherwise returns the Kubb "Generated by Kubb" notice.
|
|
330
|
+
*
|
|
331
|
+
* @example String banner overrides default
|
|
332
|
+
* ```ts
|
|
333
|
+
* defaultResolveBanner(undefined, { output: { banner: '// my banner' }, config })
|
|
334
|
+
* // → '// my banner'
|
|
335
|
+
* ```
|
|
336
|
+
*
|
|
337
|
+
* @example Function banner with node
|
|
338
|
+
* ```ts
|
|
339
|
+
* defaultResolveBanner(rootNode, { output: { banner: (node) => `// v${node.version}` }, config })
|
|
340
|
+
* // → '// v3.0.0'
|
|
341
|
+
* ```
|
|
342
|
+
*
|
|
343
|
+
* @example No user banner — Kubb notice with OAS metadata
|
|
344
|
+
* ```ts
|
|
345
|
+
* defaultResolveBanner(rootNode, { config })
|
|
346
|
+
* // → '/** Generated by Kubb ... Title: Pet Store ... *\/'
|
|
347
|
+
* ```
|
|
348
|
+
*
|
|
349
|
+
* @example Disabled default banner
|
|
350
|
+
* ```ts
|
|
351
|
+
* defaultResolveBanner(undefined, { config: { output: { defaultBanner: false }, ...config } })
|
|
352
|
+
* // → undefined
|
|
353
|
+
* ```
|
|
354
|
+
*/
|
|
355
|
+
export function defaultResolveBanner(node: RootNode | undefined, { output, config }: ResolveBannerContext): string | undefined {
|
|
356
|
+
if (typeof output?.banner === 'function') {
|
|
357
|
+
return output.banner(node)
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
if (typeof output?.banner === 'string') {
|
|
361
|
+
return output.banner
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
if (config.output.defaultBanner === false) {
|
|
365
|
+
return undefined
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
return buildDefaultBanner({ title: node?.meta?.title, version: node?.meta?.version, config })
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
/**
|
|
372
|
+
* Default footer resolver — returns the footer string for a generated file.
|
|
373
|
+
*
|
|
374
|
+
* - When `output.footer` is a function and `node` is provided, calls it with the node.
|
|
375
|
+
* - When `output.footer` is a function and `node` is absent, returns `undefined`.
|
|
376
|
+
* - When `output.footer` is a string, returns it directly.
|
|
377
|
+
* - Otherwise returns `undefined`.
|
|
378
|
+
*
|
|
379
|
+
* @example String footer
|
|
380
|
+
* ```ts
|
|
381
|
+
* defaultResolveFooter(undefined, { output: { footer: '// end of file' }, config })
|
|
382
|
+
* // → '// end of file'
|
|
383
|
+
* ```
|
|
384
|
+
*
|
|
385
|
+
* @example Function footer with node
|
|
386
|
+
* ```ts
|
|
387
|
+
* defaultResolveFooter(rootNode, { output: { footer: (node) => `// ${node.title}` }, config })
|
|
388
|
+
* // → '// Pet Store'
|
|
389
|
+
* ```
|
|
390
|
+
*/
|
|
391
|
+
export function defaultResolveFooter(node: RootNode | undefined, { output }: ResolveBannerContext): string | undefined {
|
|
392
|
+
if (typeof output?.footer === 'function') {
|
|
393
|
+
return node ? output.footer(node) : undefined
|
|
394
|
+
}
|
|
395
|
+
if (typeof output?.footer === 'string') {
|
|
396
|
+
return output.footer
|
|
397
|
+
}
|
|
398
|
+
return undefined
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
/**
|
|
402
|
+
* Defines a resolver for a plugin, injecting built-in defaults for name casing,
|
|
403
|
+
* include/exclude/override filtering, path resolution, and file construction.
|
|
404
|
+
*
|
|
405
|
+
* All four defaults can be overridden by providing them in the builder function:
|
|
406
|
+
* - `default` — name casing strategy (camelCase / PascalCase)
|
|
407
|
+
* - `resolveOptions` — include/exclude/override filtering
|
|
408
|
+
* - `resolvePath` — output path computation
|
|
409
|
+
* - `resolveFile` — full `FabricFile.File` construction
|
|
410
|
+
*
|
|
411
|
+
* Methods in the builder have access to `this` (the full resolver object), so they
|
|
412
|
+
* can call other resolver methods without circular imports.
|
|
413
|
+
*
|
|
414
|
+
* @example Basic resolver with naming helpers
|
|
415
|
+
* ```ts
|
|
416
|
+
* export const resolver = defineResolver<PluginTs>(() => ({
|
|
417
|
+
* name: 'default',
|
|
418
|
+
* resolveName(node) {
|
|
419
|
+
* return this.default(node.name, 'function')
|
|
420
|
+
* },
|
|
421
|
+
* resolveTypedName(node) {
|
|
422
|
+
* return this.default(node.name, 'type')
|
|
423
|
+
* },
|
|
424
|
+
* }))
|
|
425
|
+
* ```
|
|
426
|
+
*
|
|
427
|
+
* @example Override resolvePath for a custom output structure
|
|
428
|
+
* ```ts
|
|
429
|
+
* export const resolver = defineResolver<PluginTs>(() => ({
|
|
430
|
+
* name: 'custom',
|
|
431
|
+
* resolvePath({ baseName }, { root, output }) {
|
|
432
|
+
* return path.resolve(root, output.path, 'generated', baseName)
|
|
433
|
+
* },
|
|
434
|
+
* }))
|
|
435
|
+
* ```
|
|
436
|
+
*
|
|
437
|
+
* @example Use this.default inside a helper
|
|
438
|
+
* ```ts
|
|
439
|
+
* export const resolver = defineResolver<PluginTs>(() => ({
|
|
440
|
+
* name: 'default',
|
|
441
|
+
* resolveParamName(node, param) {
|
|
442
|
+
* return this.default(`${node.operationId} ${param.in} ${param.name}`, 'type')
|
|
443
|
+
* },
|
|
444
|
+
* }))
|
|
445
|
+
* ```
|
|
446
|
+
*/
|
|
447
|
+
export function defineResolver<T extends PluginFactoryOptions>(build: ResolverBuilder<T>): T['resolver'] {
|
|
448
|
+
return {
|
|
449
|
+
default: defaultResolver,
|
|
450
|
+
resolveOptions: defaultResolveOptions,
|
|
451
|
+
resolvePath: defaultResolvePath,
|
|
452
|
+
resolveFile: defaultResolveFile,
|
|
453
|
+
resolveBanner: defaultResolveBanner,
|
|
454
|
+
resolveFooter: defaultResolveFooter,
|
|
455
|
+
...build(),
|
|
456
|
+
} as T['resolver']
|
|
457
|
+
}
|
package/src/hooks/index.ts
CHANGED
|
@@ -1,8 +1,3 @@
|
|
|
1
|
-
export {
|
|
2
|
-
|
|
3
|
-
/** @deprecated Use `useKubb` from `@kubb/core/hooks` instead */
|
|
1
|
+
export { useDriver } from './useDriver.ts'
|
|
4
2
|
export { useMode } from './useMode.ts'
|
|
5
|
-
/** @deprecated Use `useKubb` from `@kubb/core/hooks` instead */
|
|
6
3
|
export { usePlugin } from './usePlugin.ts'
|
|
7
|
-
/** @deprecated Use `useKubb` from `@kubb/core/hooks` instead */
|
|
8
|
-
export { usePluginManager } from './usePluginManager.ts'
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { useFabric } from '@kubb/react-fabric'
|
|
2
|
+
import type { PluginDriver } from '../PluginDriver.ts'
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* @deprecated use `driver` from the generator component props instead
|
|
6
|
+
*/
|
|
7
|
+
export function useDriver(): PluginDriver {
|
|
8
|
+
const { meta } = useFabric<{ driver: PluginDriver }>()
|
|
9
|
+
|
|
10
|
+
return meta.driver
|
|
11
|
+
}
|
package/src/hooks/useMode.ts
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
import {
|
|
1
|
+
import type { FabricFile } from '@kubb/fabric-core/types'
|
|
2
|
+
import { useFabric } from '@kubb/react-fabric'
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
|
-
* @deprecated use `
|
|
5
|
+
* @deprecated use `mode` from the generator component props instead
|
|
6
6
|
*/
|
|
7
|
-
export function useMode():
|
|
8
|
-
const { meta } =
|
|
7
|
+
export function useMode(): FabricFile.Mode {
|
|
8
|
+
const { meta } = useFabric<{ mode: FabricFile.Mode }>()
|
|
9
9
|
|
|
10
10
|
return meta.mode
|
|
11
11
|
}
|
package/src/hooks/usePlugin.ts
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { useFabric } from '@kubb/react-fabric'
|
|
2
2
|
import type { Plugin, PluginFactoryOptions } from '../types.ts'
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
|
-
* @deprecated use
|
|
5
|
+
* @deprecated use `plugin` from the generator component props instead
|
|
6
6
|
*/
|
|
7
7
|
export function usePlugin<TOptions extends PluginFactoryOptions = PluginFactoryOptions>(): Plugin<TOptions> {
|
|
8
|
-
const { meta } =
|
|
8
|
+
const { meta } = useFabric<{ plugin: Plugin<TOptions> }>()
|
|
9
9
|
|
|
10
10
|
return meta.plugin
|
|
11
11
|
}
|