@kubb/core 5.0.0-alpha.30 → 5.0.0-alpha.32
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 → PluginDriver-nm7tvGs9.d.ts} +336 -73
- package/dist/hooks.cjs.map +1 -1
- package/dist/hooks.d.ts +2 -3
- package/dist/hooks.js.map +1 -1
- package/dist/index.cjs +374 -181
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +26 -149
- package/dist/index.js +377 -185
- package/dist/index.js.map +1 -1
- package/package.json +2 -4
- package/src/FileManager.ts +131 -0
- package/src/FileProcessor.ts +83 -0
- package/src/Kubb.ts +5 -5
- package/src/PluginDriver.ts +29 -23
- package/src/build.ts +118 -111
- package/src/constants.ts +7 -2
- package/src/{config.ts → defineConfig.ts} +2 -8
- package/src/defineGenerator.ts +12 -13
- package/src/defineParser.ts +57 -0
- package/src/defineResolver.ts +18 -22
- package/src/devtools.ts +14 -14
- package/src/hooks/useMode.ts +2 -3
- package/src/index.ts +3 -3
- package/src/renderNode.tsx +8 -7
- package/src/types.ts +103 -56
- package/src/utils/TreeNode.ts +7 -7
- package/src/utils/getBarrelFiles.ts +30 -28
- package/src/utils/getConfigs.ts +1 -1
- package/src/utils/isInputPath.ts +8 -0
package/src/defineResolver.ts
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
import path from 'node:path'
|
|
2
2
|
import { camelCase, pascalCase } from '@internals/utils'
|
|
3
|
-
import { isOperationNode, isSchemaNode } from '@kubb/ast'
|
|
4
|
-
import type { Node, OperationNode,
|
|
5
|
-
import type { FabricFile } from '@kubb/fabric-core/types'
|
|
3
|
+
import { createFile, isOperationNode, isSchemaNode } from '@kubb/ast'
|
|
4
|
+
import type { FileNode, InputNode, Node, OperationNode, SchemaNode } from '@kubb/ast/types'
|
|
6
5
|
import { getMode } from './PluginDriver.ts'
|
|
7
6
|
import type {
|
|
8
7
|
Config,
|
|
@@ -195,27 +194,24 @@ export function defaultResolveOptions<TOptions>(
|
|
|
195
194
|
* // → '/src/types'
|
|
196
195
|
* ```
|
|
197
196
|
*/
|
|
198
|
-
export function defaultResolvePath(
|
|
199
|
-
{ baseName, pathMode, tag, path: groupPath }: ResolverPathParams,
|
|
200
|
-
{ root, output, group }: ResolverContext,
|
|
201
|
-
): FabricFile.Path {
|
|
197
|
+
export function defaultResolvePath({ baseName, pathMode, tag, path: groupPath }: ResolverPathParams, { root, output, group }: ResolverContext): string {
|
|
202
198
|
const mode = pathMode ?? getMode(path.resolve(root, output.path))
|
|
203
199
|
|
|
204
200
|
if (mode === 'single') {
|
|
205
|
-
return path.resolve(root, output.path)
|
|
201
|
+
return path.resolve(root, output.path)
|
|
206
202
|
}
|
|
207
203
|
|
|
208
204
|
if (group && (groupPath || tag)) {
|
|
209
|
-
return path.resolve(root, output.path, group.name({ group: group.type === 'path' ? groupPath! : tag! }), baseName)
|
|
205
|
+
return path.resolve(root, output.path, group.name({ group: group.type === 'path' ? groupPath! : tag! }), baseName)
|
|
210
206
|
}
|
|
211
207
|
|
|
212
|
-
return path.resolve(root, output.path, baseName)
|
|
208
|
+
return path.resolve(root, output.path, baseName)
|
|
213
209
|
}
|
|
214
210
|
|
|
215
211
|
/**
|
|
216
212
|
* Default file resolver used by `defineResolver`.
|
|
217
213
|
*
|
|
218
|
-
* Resolves a `
|
|
214
|
+
* Resolves a `FileNode` by combining name resolution (`resolver.default`) with
|
|
219
215
|
* path resolution (`resolver.resolvePath`). The resolved file always has empty
|
|
220
216
|
* `sources`, `imports`, and `exports` arrays — consumers populate those separately.
|
|
221
217
|
*
|
|
@@ -239,22 +235,22 @@ export function defaultResolvePath(
|
|
|
239
235
|
* // → { baseName: 'listPets.ts', path: '/src/types/petsController/listPets.ts', ... }
|
|
240
236
|
* ```
|
|
241
237
|
*/
|
|
242
|
-
export function defaultResolveFile(this: Resolver, { name, extname, tag, path: groupPath }: ResolverFileParams, context: ResolverContext):
|
|
238
|
+
export function defaultResolveFile(this: Resolver, { name, extname, tag, path: groupPath }: ResolverFileParams, context: ResolverContext): FileNode {
|
|
243
239
|
const pathMode = getMode(path.resolve(context.root, context.output.path))
|
|
244
240
|
const resolvedName = pathMode === 'single' ? '' : this.default(name, 'file')
|
|
245
|
-
const baseName = `${resolvedName}${extname}` as
|
|
241
|
+
const baseName = `${resolvedName}${extname}` as FileNode['baseName']
|
|
246
242
|
const filePath = this.resolvePath({ baseName, pathMode, tag, path: groupPath }, context)
|
|
247
243
|
|
|
248
|
-
return {
|
|
244
|
+
return createFile({
|
|
249
245
|
path: filePath,
|
|
250
|
-
baseName: path.basename(filePath) as
|
|
246
|
+
baseName: path.basename(filePath) as `${string}.${string}`,
|
|
251
247
|
meta: {
|
|
252
248
|
pluginName: this.pluginName,
|
|
253
249
|
},
|
|
254
250
|
sources: [],
|
|
255
251
|
imports: [],
|
|
256
252
|
exports: [],
|
|
257
|
-
}
|
|
253
|
+
})
|
|
258
254
|
}
|
|
259
255
|
|
|
260
256
|
/**
|
|
@@ -336,13 +332,13 @@ export function buildDefaultBanner({
|
|
|
336
332
|
*
|
|
337
333
|
* @example Function banner with node
|
|
338
334
|
* ```ts
|
|
339
|
-
* defaultResolveBanner(
|
|
335
|
+
* defaultResolveBanner(inputNode, { output: { banner: (node) => `// v${node.version}` }, config })
|
|
340
336
|
* // → '// v3.0.0'
|
|
341
337
|
* ```
|
|
342
338
|
*
|
|
343
339
|
* @example No user banner — Kubb notice with OAS metadata
|
|
344
340
|
* ```ts
|
|
345
|
-
* defaultResolveBanner(
|
|
341
|
+
* defaultResolveBanner(inputNode, { config })
|
|
346
342
|
* // → '/** Generated by Kubb ... Title: Pet Store ... *\/'
|
|
347
343
|
* ```
|
|
348
344
|
*
|
|
@@ -352,7 +348,7 @@ export function buildDefaultBanner({
|
|
|
352
348
|
* // → undefined
|
|
353
349
|
* ```
|
|
354
350
|
*/
|
|
355
|
-
export function defaultResolveBanner(node:
|
|
351
|
+
export function defaultResolveBanner(node: InputNode | undefined, { output, config }: ResolveBannerContext): string | undefined {
|
|
356
352
|
if (typeof output?.banner === 'function') {
|
|
357
353
|
return output.banner(node)
|
|
358
354
|
}
|
|
@@ -384,11 +380,11 @@ export function defaultResolveBanner(node: RootNode | undefined, { output, confi
|
|
|
384
380
|
*
|
|
385
381
|
* @example Function footer with node
|
|
386
382
|
* ```ts
|
|
387
|
-
* defaultResolveFooter(
|
|
383
|
+
* defaultResolveFooter(inputNode, { output: { footer: (node) => `// ${node.title}` }, config })
|
|
388
384
|
* // → '// Pet Store'
|
|
389
385
|
* ```
|
|
390
386
|
*/
|
|
391
|
-
export function defaultResolveFooter(node:
|
|
387
|
+
export function defaultResolveFooter(node: InputNode | undefined, { output }: ResolveBannerContext): string | undefined {
|
|
392
388
|
if (typeof output?.footer === 'function') {
|
|
393
389
|
return node ? output.footer(node) : undefined
|
|
394
390
|
}
|
|
@@ -406,7 +402,7 @@ export function defaultResolveFooter(node: RootNode | undefined, { output }: Res
|
|
|
406
402
|
* - `default` — name casing strategy (camelCase / PascalCase)
|
|
407
403
|
* - `resolveOptions` — include/exclude/override filtering
|
|
408
404
|
* - `resolvePath` — output path computation
|
|
409
|
-
* - `resolveFile` — full `
|
|
405
|
+
* - `resolveFile` — full `FileNode` construction
|
|
410
406
|
*
|
|
411
407
|
* Methods in the builder have access to `this` (the full resolver object), so they
|
|
412
408
|
* can call other resolver methods without circular imports.
|
package/src/devtools.ts
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { InputNode } from '@kubb/ast/types'
|
|
2
2
|
import { deflateSync, inflateSync } from 'fflate'
|
|
3
3
|
import { x } from 'tinyexec'
|
|
4
4
|
import type { DevtoolsOptions } from './types.ts'
|
|
5
5
|
|
|
6
6
|
/**
|
|
7
|
-
* Encodes
|
|
7
|
+
* Encodes an `InputNode` as a compressed, URL-safe string.
|
|
8
8
|
*
|
|
9
9
|
* The JSON representation is deflate-compressed with {@link deflateSync} before
|
|
10
10
|
* base64url encoding, which typically reduces payload size by 70–80 % and
|
|
@@ -12,41 +12,41 @@ import type { DevtoolsOptions } from './types.ts'
|
|
|
12
12
|
*
|
|
13
13
|
* Use {@link decodeAst} to reverse.
|
|
14
14
|
*/
|
|
15
|
-
export function encodeAst(
|
|
16
|
-
const compressed = deflateSync(new TextEncoder().encode(JSON.stringify(
|
|
15
|
+
export function encodeAst(input: InputNode): string {
|
|
16
|
+
const compressed = deflateSync(new TextEncoder().encode(JSON.stringify(input)))
|
|
17
17
|
return Buffer.from(compressed).toString('base64url')
|
|
18
18
|
}
|
|
19
19
|
|
|
20
20
|
/**
|
|
21
|
-
* Decodes
|
|
21
|
+
* Decodes an `InputNode` from a string produced by {@link encodeAst}.
|
|
22
22
|
*
|
|
23
23
|
* Works in both Node.js and the browser — no streaming APIs required.
|
|
24
24
|
*/
|
|
25
|
-
export function decodeAst(encoded: string):
|
|
25
|
+
export function decodeAst(encoded: string): InputNode {
|
|
26
26
|
const bytes = Buffer.from(encoded, 'base64url')
|
|
27
|
-
return JSON.parse(new TextDecoder().decode(inflateSync(bytes))) as
|
|
27
|
+
return JSON.parse(new TextDecoder().decode(inflateSync(bytes))) as InputNode
|
|
28
28
|
}
|
|
29
29
|
|
|
30
30
|
/**
|
|
31
|
-
* Constructs the Kubb Studio URL for the given `
|
|
31
|
+
* Constructs the Kubb Studio URL for the given `InputNode`.
|
|
32
32
|
* When `options.ast` is `true`, navigates to the AST inspector (`/ast`).
|
|
33
|
-
* The `
|
|
33
|
+
* The `input` is encoded and attached as the `?root=` query parameter so Studio
|
|
34
34
|
* can decode and render it without a round-trip to any server.
|
|
35
35
|
*/
|
|
36
|
-
export function getStudioUrl(
|
|
36
|
+
export function getStudioUrl(input: InputNode, studioUrl: string, options: DevtoolsOptions = {}): string {
|
|
37
37
|
const baseUrl = studioUrl.replace(/\/$/, '')
|
|
38
38
|
const path = options.ast ? '/ast' : ''
|
|
39
39
|
|
|
40
|
-
return `${baseUrl}${path}?root=${encodeAst(
|
|
40
|
+
return `${baseUrl}${path}?root=${encodeAst(input)}`
|
|
41
41
|
}
|
|
42
42
|
|
|
43
43
|
/**
|
|
44
|
-
* Opens the Kubb Studio URL for the given `
|
|
44
|
+
* Opens the Kubb Studio URL for the given `InputNode` in the default browser —
|
|
45
45
|
*
|
|
46
46
|
* Falls back to printing the URL if the browser cannot be launched.
|
|
47
47
|
*/
|
|
48
|
-
export async function openInStudio(
|
|
49
|
-
const url = getStudioUrl(
|
|
48
|
+
export async function openInStudio(input: InputNode, studioUrl: string, options: DevtoolsOptions = {}): Promise<void> {
|
|
49
|
+
const url = getStudioUrl(input, studioUrl, options)
|
|
50
50
|
|
|
51
51
|
const cmd = process.platform === 'win32' ? 'cmd' : process.platform === 'darwin' ? 'open' : 'xdg-open'
|
|
52
52
|
const args = process.platform === 'win32' ? ['/c', 'start', '', url] : [url]
|
package/src/hooks/useMode.ts
CHANGED
|
@@ -1,11 +1,10 @@
|
|
|
1
|
-
import type { FabricFile } from '@kubb/fabric-core/types'
|
|
2
1
|
import { useFabric } from '@kubb/react-fabric'
|
|
3
2
|
|
|
4
3
|
/**
|
|
5
4
|
* @deprecated use `mode` from the generator component props instead
|
|
6
5
|
*/
|
|
7
|
-
export function useMode():
|
|
8
|
-
const { meta } = useFabric<{ mode:
|
|
6
|
+
export function useMode(): 'single' | 'split' {
|
|
7
|
+
const { meta } = useFabric<{ mode: 'single' | 'split' }>()
|
|
9
8
|
|
|
10
9
|
return meta.mode
|
|
11
10
|
}
|
package/src/index.ts
CHANGED
|
@@ -1,13 +1,14 @@
|
|
|
1
1
|
export { AsyncEventEmitter, URLPath } from '@internals/utils'
|
|
2
2
|
export { composeTransformers, definePrinter } from '@kubb/ast'
|
|
3
3
|
export { build, build as default, safeBuild, setup } from './build.ts'
|
|
4
|
-
export { type CLIOptions, type ConfigInput, defineConfig, isInputPath } from './config.ts'
|
|
5
4
|
export { formatters, linters, logLevel } from './constants.ts'
|
|
6
5
|
export { createAdapter } from './createAdapter.ts'
|
|
7
6
|
export { createPlugin } from './createPlugin.ts'
|
|
8
7
|
export { createStorage } from './createStorage.ts'
|
|
8
|
+
export { defineConfig } from './defineConfig.ts'
|
|
9
9
|
export { defineGenerator, mergeGenerators } from './defineGenerator.ts'
|
|
10
10
|
export { defineLogger } from './defineLogger.ts'
|
|
11
|
+
export { defineParser } from './defineParser.ts'
|
|
11
12
|
export { definePresets } from './definePresets.ts'
|
|
12
13
|
export {
|
|
13
14
|
buildDefaultBanner,
|
|
@@ -22,12 +23,11 @@ export { getMode, PluginDriver } from './PluginDriver.ts'
|
|
|
22
23
|
export { fsStorage } from './storages/fsStorage.ts'
|
|
23
24
|
export { memoryStorage } from './storages/memoryStorage.ts'
|
|
24
25
|
export * from './types.ts'
|
|
25
|
-
export type { FunctionParamsAST } from './utils/FunctionParams.ts'
|
|
26
26
|
export { FunctionParams } from './utils/FunctionParams.ts'
|
|
27
27
|
export { detectFormatter } from './utils/formatters.ts'
|
|
28
|
-
export type { FileMetaBase } from './utils/getBarrelFiles.ts'
|
|
29
28
|
export { getBarrelFiles } from './utils/getBarrelFiles.ts'
|
|
30
29
|
export { getConfigs } from './utils/getConfigs.ts'
|
|
31
30
|
export { getPreset } from './utils/getPreset.ts'
|
|
31
|
+
export { isInputPath } from './utils/isInputPath.ts'
|
|
32
32
|
export { detectLinter } from './utils/linters.ts'
|
|
33
33
|
export { satisfiesDependency } from './utils/packageJSON.ts'
|
package/src/renderNode.tsx
CHANGED
|
@@ -1,25 +1,26 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { FileNode } from '@kubb/ast/types'
|
|
2
2
|
import { createReactFabric, Fabric } from '@kubb/react-fabric'
|
|
3
|
-
import type { FabricReactNode
|
|
3
|
+
import type { FabricReactNode } from '@kubb/react-fabric/types'
|
|
4
|
+
import type { PluginDriver } from './PluginDriver.ts'
|
|
4
5
|
|
|
5
6
|
/**
|
|
6
7
|
* Handles the return value of a plugin AST hook or generator method.
|
|
7
8
|
*
|
|
8
|
-
* - React element → rendered via an isolated react-fabric context, files
|
|
9
|
-
* - `Array<
|
|
9
|
+
* - React element → rendered via an isolated react-fabric context, files stored in `driver.fileManager`
|
|
10
|
+
* - `Array<FileNode>` → upserted directly into `driver.fileManager`
|
|
10
11
|
* - `void` / `null` / `undefined` → no-op (plugin handled it via `this.upsertFile`)
|
|
11
12
|
*/
|
|
12
|
-
export async function applyHookResult(result: FabricReactNode | Array<
|
|
13
|
+
export async function applyHookResult(result: FabricReactNode | Array<FileNode> | void, driver: PluginDriver): Promise<void> {
|
|
13
14
|
if (!result) return
|
|
14
15
|
|
|
15
16
|
if (Array.isArray(result)) {
|
|
16
|
-
|
|
17
|
+
driver.fileManager.upsert(...(result as Array<FileNode>))
|
|
17
18
|
return
|
|
18
19
|
}
|
|
19
20
|
|
|
20
21
|
// Non-array truthy result is treated as a React element (FabricReactNode)
|
|
21
22
|
const fabricChild = createReactFabric()
|
|
22
23
|
await fabricChild.render(<Fabric>{result as FabricReactNode}</Fabric>)
|
|
23
|
-
|
|
24
|
+
driver.fileManager.upsert(...(fabricChild.files as unknown as Array<FileNode>))
|
|
24
25
|
fabricChild.unmount()
|
|
25
26
|
}
|
package/src/types.ts
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import type { AsyncEventEmitter, PossiblePromise } from '@internals/utils'
|
|
2
|
-
import type { Node, OperationNode, Printer,
|
|
3
|
-
import type { FabricFile, Fabric as FabricType } from '@kubb/fabric-core/types'
|
|
2
|
+
import type { FileNode, ImportNode, InputNode, Node, OperationNode, Printer, SchemaNode, Visitor } from '@kubb/ast/types'
|
|
4
3
|
import type { HttpMethod } from '@kubb/oas'
|
|
5
4
|
import type { FabricReactNode } from '@kubb/react-fabric/types'
|
|
6
5
|
import type { DEFAULT_STUDIO_URL, logLevel } from './constants.ts'
|
|
7
6
|
import type { Storage } from './createStorage.ts'
|
|
8
7
|
import type { Generator } from './defineGenerator.ts'
|
|
8
|
+
import type { Parser } from './defineParser.ts'
|
|
9
9
|
import type { KubbEvents } from './Kubb.ts'
|
|
10
10
|
import type { PluginDriver } from './PluginDriver.ts'
|
|
11
11
|
|
|
@@ -44,12 +44,43 @@ declare global {
|
|
|
44
44
|
* ...
|
|
45
45
|
* })
|
|
46
46
|
*/
|
|
47
|
-
export type UserConfig<TInput = Input> = Omit<Config<TInput>, 'root' | 'plugins'> & {
|
|
47
|
+
export type UserConfig<TInput = Input> = Omit<Config<TInput>, 'root' | 'plugins' | 'parsers' | 'adapter'> & {
|
|
48
48
|
/**
|
|
49
49
|
* The project root directory, which can be either an absolute path or a path relative to the location of your `kubb.config.ts` file.
|
|
50
50
|
* @default process.cwd()
|
|
51
51
|
*/
|
|
52
52
|
root?: string
|
|
53
|
+
/**
|
|
54
|
+
* An array of parsers used to convert generated files to strings.
|
|
55
|
+
* Each parser handles specific file extensions (e.g. `.ts`, `.tsx`).
|
|
56
|
+
*
|
|
57
|
+
* A catch-all fallback parser is always appended last for any unhandled extension.
|
|
58
|
+
*
|
|
59
|
+
* When omitted, `parserTs` from `@kubb/parser-ts` is used automatically as the
|
|
60
|
+
* default (requires `@kubb/parser-ts` to be installed as an optional dependency).
|
|
61
|
+
* @default [parserTs] — from `@kubb/parser-ts`
|
|
62
|
+
* @example
|
|
63
|
+
* ```ts
|
|
64
|
+
* import { parserTs, tsxParser } from '@kubb/parser-ts'
|
|
65
|
+
* export default defineConfig({
|
|
66
|
+
* parsers: [parserTs, tsxParser],
|
|
67
|
+
* })
|
|
68
|
+
* ```
|
|
69
|
+
*/
|
|
70
|
+
parsers?: Array<Parser>
|
|
71
|
+
/**
|
|
72
|
+
* Adapter that converts the input file into a `@kubb/ast` `InputNode` — the universal
|
|
73
|
+
* intermediate representation consumed by all Kubb plugins.
|
|
74
|
+
*
|
|
75
|
+
* When omitted, `adapterOas()` from `@kubb/adapter-oas` is used automatically as the
|
|
76
|
+
* default (requires `@kubb/adapter-oas` to be installed as an optional dependency).
|
|
77
|
+
*
|
|
78
|
+
* - Use `@kubb/adapter-oas` for OpenAPI / Swagger (default).
|
|
79
|
+
* - Use `@kubb/adapter-drizzle` or `@kubb/adapter-asyncapi` for other formats.
|
|
80
|
+
*
|
|
81
|
+
* @default adapterOas() — from `@kubb/adapter-oas`
|
|
82
|
+
*/
|
|
83
|
+
adapter?: Adapter
|
|
53
84
|
/**
|
|
54
85
|
* An array of Kubb plugins used for generation. Each plugin may have additional configurable options (defined within the plugin itself). If a plugin relies on another plugin, an error will occur if the required dependency is missing. Refer to “pre” for more details.
|
|
55
86
|
*/
|
|
@@ -71,7 +102,7 @@ export type InputData = {
|
|
|
71
102
|
data: string | unknown
|
|
72
103
|
}
|
|
73
104
|
|
|
74
|
-
type Input = InputPath | InputData
|
|
105
|
+
type Input = InputPath | InputData
|
|
75
106
|
|
|
76
107
|
/**
|
|
77
108
|
* The raw source passed to an adapter's `parse` function.
|
|
@@ -101,10 +132,10 @@ export type AdapterFactoryOptions<
|
|
|
101
132
|
}
|
|
102
133
|
|
|
103
134
|
/**
|
|
104
|
-
* An adapter converts a source file or data into a `@kubb/ast` `
|
|
135
|
+
* An adapter converts a source file or data into a `@kubb/ast` `InputNode`.
|
|
105
136
|
*
|
|
106
137
|
* Adapters are the single entry-point for different schema formats
|
|
107
|
-
* (OpenAPI, AsyncAPI, Drizzle, …) and produce the universal `
|
|
138
|
+
* (OpenAPI, AsyncAPI, Drizzle, …) and produce the universal `InputNode`
|
|
108
139
|
* that all Kubb plugins consume.
|
|
109
140
|
*
|
|
110
141
|
* @example
|
|
@@ -132,19 +163,19 @@ export type Adapter<TOptions extends AdapterFactoryOptions = AdapterFactoryOptio
|
|
|
132
163
|
* `undefined` before parsing; typed by the adapter's `TDocument` generic.
|
|
133
164
|
*/
|
|
134
165
|
document: TOptions['document'] | null
|
|
135
|
-
|
|
166
|
+
inputNode: InputNode | null
|
|
136
167
|
/**
|
|
137
|
-
* Convert the raw source into a universal `
|
|
168
|
+
* Convert the raw source into a universal `InputNode`.
|
|
138
169
|
*/
|
|
139
|
-
parse: (source: AdapterSource) => PossiblePromise<
|
|
170
|
+
parse: (source: AdapterSource) => PossiblePromise<InputNode>
|
|
140
171
|
/**
|
|
141
|
-
* Extracts `
|
|
172
|
+
* Extracts `ImportNode` entries needed by a `SchemaNode` tree.
|
|
142
173
|
* Populated after the first `parse()` call. Returns an empty array before that.
|
|
143
174
|
*
|
|
144
175
|
* The `resolve` callback receives the collision-corrected schema name and must
|
|
145
176
|
* return the `{ name, path }` pair for the import, or `undefined` to skip it.
|
|
146
177
|
*/
|
|
147
|
-
getImports: (node: SchemaNode, resolve: (schemaName: string) => { name: string; path: string }) => Array<
|
|
178
|
+
getImports: (node: SchemaNode, resolve: (schemaName: string) => { name: string; path: string }) => Array<ImportNode>
|
|
148
179
|
}
|
|
149
180
|
|
|
150
181
|
export type BarrelType = 'all' | 'named' | 'propagate'
|
|
@@ -172,23 +203,40 @@ export type Config<TInput = Input> = {
|
|
|
172
203
|
*/
|
|
173
204
|
root: string
|
|
174
205
|
/**
|
|
175
|
-
*
|
|
206
|
+
* An array of parsers used to convert generated files to strings.
|
|
207
|
+
* Each parser handles specific file extensions (e.g. `.ts`, `.tsx`).
|
|
208
|
+
*
|
|
209
|
+
* A catch-all fallback parser is always appended last for any unhandled extension.
|
|
210
|
+
*
|
|
211
|
+
* When omitted, `parserTs` from `@kubb/parser-ts` is used automatically as the
|
|
212
|
+
* default (requires `@kubb/parser-ts` to be installed as an optional dependency).
|
|
213
|
+
* @default [parserTs] — from `@kubb/parser-ts`
|
|
214
|
+
* @example
|
|
215
|
+
* ```ts
|
|
216
|
+
* import { parserTs, tsxParser } from '@kubb/parser-ts'
|
|
217
|
+
* export default defineConfig({
|
|
218
|
+
* parsers: [parserTs, tsxParser],
|
|
219
|
+
* })
|
|
220
|
+
* ```
|
|
221
|
+
*/
|
|
222
|
+
parsers: Array<Parser>
|
|
223
|
+
/**
|
|
224
|
+
* Adapter that converts the input file into a `@kubb/ast` `InputNode` — the universal
|
|
176
225
|
* intermediate representation consumed by all Kubb plugins.
|
|
177
226
|
*
|
|
178
|
-
* -
|
|
179
|
-
* - Use `@kubb/adapter-oas` for explicit OpenAPI configuration (validate, contentType, …).
|
|
227
|
+
* - Use `@kubb/adapter-oas` for OpenAPI / Swagger.
|
|
180
228
|
* - Use `@kubb/adapter-drizzle` or `@kubb/adapter-asyncapi` for other formats.
|
|
181
229
|
*
|
|
182
230
|
* @example
|
|
183
231
|
* ```ts
|
|
184
|
-
* import {
|
|
232
|
+
* import { adapterOas } from '@kubb/adapter-oas'
|
|
185
233
|
* export default defineConfig({
|
|
186
|
-
* adapter:
|
|
187
|
-
* input: { path: './
|
|
234
|
+
* adapter: adapterOas(),
|
|
235
|
+
* input: { path: './petStore.yaml' },
|
|
188
236
|
* })
|
|
189
237
|
* ```
|
|
190
238
|
*/
|
|
191
|
-
adapter
|
|
239
|
+
adapter: Adapter
|
|
192
240
|
/**
|
|
193
241
|
* You can use either `input.path` or `input.data`, depending on your specific needs.
|
|
194
242
|
*/
|
|
@@ -246,7 +294,7 @@ export type Config<TInput = Input> = {
|
|
|
246
294
|
* Overrides the extension for generated imports and exports. By default, each plugin adds an extension.
|
|
247
295
|
* @default { '.ts': '.ts'}
|
|
248
296
|
*/
|
|
249
|
-
extension?: Record<
|
|
297
|
+
extension?: Record<FileNode['extname'], FileNode['extname'] | ''>
|
|
250
298
|
/**
|
|
251
299
|
* Configures how `index.ts` files are created, including disabling barrel file generation. Each plugin has its own `barrelType` option; this setting controls the root barrel file (e.g., `src/gen/index.ts`).
|
|
252
300
|
* @default 'named'
|
|
@@ -346,10 +394,10 @@ export type Resolver = {
|
|
|
346
394
|
pluginName: Plugin['name']
|
|
347
395
|
default(name: ResolveNameParams['name'], type?: ResolveNameParams['type']): string
|
|
348
396
|
resolveOptions<TOptions>(node: Node, context: ResolveOptionsContext<TOptions>): TOptions | null
|
|
349
|
-
resolvePath(params: ResolverPathParams, context: ResolverContext):
|
|
350
|
-
resolveFile(params: ResolverFileParams, context: ResolverContext):
|
|
351
|
-
resolveBanner(node:
|
|
352
|
-
resolveFooter(node:
|
|
397
|
+
resolvePath(params: ResolverPathParams, context: ResolverContext): string
|
|
398
|
+
resolveFile(params: ResolverFileParams, context: ResolverContext): FileNode
|
|
399
|
+
resolveBanner(node: InputNode | null, context: ResolveBannerContext): string | undefined
|
|
400
|
+
resolveFooter(node: InputNode | null, context: ResolveBannerContext): string | undefined
|
|
353
401
|
}
|
|
354
402
|
|
|
355
403
|
/**
|
|
@@ -477,7 +525,7 @@ export type SchemaHook<TOptions extends PluginFactoryOptions = PluginFactoryOpti
|
|
|
477
525
|
this: GeneratorContext<TOptions>,
|
|
478
526
|
node: SchemaNode,
|
|
479
527
|
options: TOptions['resolvedOptions'],
|
|
480
|
-
) => PossiblePromise<FabricReactNode | Array<
|
|
528
|
+
) => PossiblePromise<FabricReactNode | Array<FileNode> | void>
|
|
481
529
|
|
|
482
530
|
/**
|
|
483
531
|
* Handler for a single operation node. Used by the `operation` hook on a plugin.
|
|
@@ -486,7 +534,7 @@ export type OperationHook<TOptions extends PluginFactoryOptions = PluginFactoryO
|
|
|
486
534
|
this: GeneratorContext<TOptions>,
|
|
487
535
|
node: OperationNode,
|
|
488
536
|
options: TOptions['resolvedOptions'],
|
|
489
|
-
) => PossiblePromise<FabricReactNode | Array<
|
|
537
|
+
) => PossiblePromise<FabricReactNode | Array<FileNode> | void>
|
|
490
538
|
|
|
491
539
|
/**
|
|
492
540
|
* Handler for all collected operation nodes. Used by the `operations` hook on a plugin.
|
|
@@ -495,7 +543,7 @@ export type OperationsHook<TOptions extends PluginFactoryOptions = PluginFactory
|
|
|
495
543
|
this: GeneratorContext<TOptions>,
|
|
496
544
|
nodes: Array<OperationNode>,
|
|
497
545
|
options: TOptions['resolvedOptions'],
|
|
498
|
-
) => PossiblePromise<FabricReactNode | Array<
|
|
546
|
+
) => PossiblePromise<FabricReactNode | Array<FileNode> | void>
|
|
499
547
|
|
|
500
548
|
export type Plugin<TOptions extends PluginFactoryOptions = PluginFactoryOptions> = {
|
|
501
549
|
/**
|
|
@@ -552,7 +600,7 @@ export type Plugin<TOptions extends PluginFactoryOptions = PluginFactoryOptions>
|
|
|
552
600
|
buildEnd: (this: PluginContext<TOptions>) => PossiblePromise<void>
|
|
553
601
|
/**
|
|
554
602
|
* Called for each schema node during the AST walk.
|
|
555
|
-
* Return a React element, an array of `
|
|
603
|
+
* Return a React element, an array of `FileNode`, or `void` for manual handling.
|
|
556
604
|
* Nodes matching `exclude`/`include` filters are skipped automatically.
|
|
557
605
|
*
|
|
558
606
|
* For multiple generators, use `composeGenerators` inside the plugin factory.
|
|
@@ -560,7 +608,7 @@ export type Plugin<TOptions extends PluginFactoryOptions = PluginFactoryOptions>
|
|
|
560
608
|
schema?: SchemaHook<TOptions>
|
|
561
609
|
/**
|
|
562
610
|
* Called for each operation node during the AST walk.
|
|
563
|
-
* Return a React element, an array of `
|
|
611
|
+
* Return a React element, an array of `FileNode`, or `void` for manual handling.
|
|
564
612
|
*
|
|
565
613
|
* For multiple generators, use `composeGenerators` inside the plugin factory.
|
|
566
614
|
*/
|
|
@@ -595,7 +643,7 @@ export type PluginLifecycle<TOptions extends PluginFactoryOptions = PluginFactor
|
|
|
595
643
|
buildEnd?: (this: PluginContext<TOptions>) => PossiblePromise<void>
|
|
596
644
|
/**
|
|
597
645
|
* Called for each schema node during the AST walk.
|
|
598
|
-
* Return a React element (`<File>...</File>`), an array of `
|
|
646
|
+
* Return a React element (`<File>...</File>`), an array of `FileNode` objects,
|
|
599
647
|
* or `void` to handle file writing manually via `this.upsertFile`.
|
|
600
648
|
* Nodes matching `exclude` / `include` filters are skipped automatically.
|
|
601
649
|
*
|
|
@@ -604,7 +652,7 @@ export type PluginLifecycle<TOptions extends PluginFactoryOptions = PluginFactor
|
|
|
604
652
|
schema?: SchemaHook<TOptions>
|
|
605
653
|
/**
|
|
606
654
|
* Called for each operation node during the AST walk.
|
|
607
|
-
* Return a React element (`<File>...</File>`), an array of `
|
|
655
|
+
* Return a React element (`<File>...</File>`), an array of `FileNode` objects,
|
|
608
656
|
* or `void` to handle file writing manually via `this.upsertFile`.
|
|
609
657
|
*
|
|
610
658
|
* For multiple generators, use `composeGenerators` inside the plugin factory.
|
|
@@ -624,12 +672,7 @@ export type PluginLifecycle<TOptions extends PluginFactoryOptions = PluginFactor
|
|
|
624
672
|
* @example ('./Pet.ts', './src/gen/') => '/src/gen/Pet.ts'
|
|
625
673
|
* @deprecated this will be replaced by resolvers
|
|
626
674
|
*/
|
|
627
|
-
resolvePath?: (
|
|
628
|
-
this: PluginContext<TOptions>,
|
|
629
|
-
baseName: FabricFile.BaseName,
|
|
630
|
-
mode?: FabricFile.Mode,
|
|
631
|
-
options?: TOptions['resolvePathOptions'],
|
|
632
|
-
) => FabricFile.Path
|
|
675
|
+
resolvePath?: (this: PluginContext<TOptions>, baseName: FileNode['baseName'], mode?: 'single' | 'split', options?: TOptions['resolvePathOptions']) => string
|
|
633
676
|
/**
|
|
634
677
|
* Resolve to a name based on a string.
|
|
635
678
|
* Useful when converting to PascalCase or camelCase.
|
|
@@ -646,8 +689,8 @@ export type PluginParameter<H extends PluginLifecycleHooks> = Parameters<Require
|
|
|
646
689
|
|
|
647
690
|
export type ResolvePathParams<TOptions = object> = {
|
|
648
691
|
pluginName?: string
|
|
649
|
-
baseName:
|
|
650
|
-
mode?:
|
|
692
|
+
baseName: FileNode['baseName']
|
|
693
|
+
mode?: 'single' | 'split'
|
|
651
694
|
/**
|
|
652
695
|
* Options to be passed to 'resolvePath' 3th parameter
|
|
653
696
|
*/
|
|
@@ -669,7 +712,6 @@ export type ResolveNameParams = {
|
|
|
669
712
|
}
|
|
670
713
|
|
|
671
714
|
export type PluginContext<TOptions extends PluginFactoryOptions = PluginFactoryOptions> = {
|
|
672
|
-
fabric: FabricType
|
|
673
715
|
config: Config
|
|
674
716
|
/**
|
|
675
717
|
* Absolute path to the output directory for the current plugin.
|
|
@@ -681,7 +723,7 @@ export type PluginContext<TOptions extends PluginFactoryOptions = PluginFactoryO
|
|
|
681
723
|
* Returns `'single'` when `output.path` has a file extension, `'split'` otherwise.
|
|
682
724
|
* Shorthand for `getMode(path.resolve(this.root, output.path))`.
|
|
683
725
|
*/
|
|
684
|
-
getMode: (output: { path: string }) =>
|
|
726
|
+
getMode: (output: { path: string }) => 'single' | 'split'
|
|
685
727
|
driver: PluginDriver
|
|
686
728
|
/**
|
|
687
729
|
* Get a plugin by name. Returns the plugin typed via `Kubb.PluginRegistry` when
|
|
@@ -698,11 +740,11 @@ export type PluginContext<TOptions extends PluginFactoryOptions = PluginFactoryO
|
|
|
698
740
|
/**
|
|
699
741
|
* Only add when the file does not exist yet
|
|
700
742
|
*/
|
|
701
|
-
addFile: (...file: Array<
|
|
743
|
+
addFile: (...file: Array<FileNode>) => Promise<void>
|
|
702
744
|
/**
|
|
703
745
|
* merging multiple sources into the same output file
|
|
704
746
|
*/
|
|
705
|
-
upsertFile: (...file: Array<
|
|
747
|
+
upsertFile: (...file: Array<FileNode>) => Promise<void>
|
|
706
748
|
/**
|
|
707
749
|
* @deprecated use this.warn, this.error, this.info instead
|
|
708
750
|
*/
|
|
@@ -737,25 +779,25 @@ export type PluginContext<TOptions extends PluginFactoryOptions = PluginFactoryO
|
|
|
737
779
|
*/
|
|
738
780
|
info: (message: string) => void
|
|
739
781
|
/**
|
|
740
|
-
* Opens the Kubb Studio URL for the current `
|
|
782
|
+
* Opens the Kubb Studio URL for the current `inputNode` in the default browser.
|
|
741
783
|
* Falls back to printing the URL if the browser cannot be launched.
|
|
742
|
-
* No-ops silently when no adapter has set
|
|
784
|
+
* No-ops silently when no adapter has set an `inputNode`.
|
|
743
785
|
*/
|
|
744
786
|
openInStudio: (options?: DevtoolsOptions) => Promise<void>
|
|
745
787
|
} & (
|
|
746
788
|
| {
|
|
747
789
|
/**
|
|
748
|
-
* Returns the universal `@kubb/ast` `
|
|
790
|
+
* Returns the universal `@kubb/ast` `InputNode` produced by the configured adapter.
|
|
749
791
|
* Returns `undefined` when no adapter was set (legacy OAS-only usage).
|
|
750
792
|
*/
|
|
751
|
-
|
|
793
|
+
inputNode: InputNode
|
|
752
794
|
/**
|
|
753
795
|
* Return the adapter from `@kubb/ast`
|
|
754
796
|
*/
|
|
755
797
|
adapter: Adapter
|
|
756
798
|
}
|
|
757
799
|
| {
|
|
758
|
-
|
|
800
|
+
inputNode?: never
|
|
759
801
|
adapter?: never
|
|
760
802
|
}
|
|
761
803
|
) &
|
|
@@ -766,12 +808,12 @@ export type PluginContext<TOptions extends PluginFactoryOptions = PluginFactoryO
|
|
|
766
808
|
*
|
|
767
809
|
* Generators and the `schema`/`operation`/`operations` plugin hooks are only invoked from
|
|
768
810
|
* `runPluginAstHooks`, which already guards against a missing adapter. This type reflects
|
|
769
|
-
* that guarantee — `this.adapter` and `this.
|
|
811
|
+
* that guarantee — `this.adapter` and `this.inputNode` are always defined, so no runtime
|
|
770
812
|
* checks or casts are needed inside the method bodies.
|
|
771
813
|
*/
|
|
772
|
-
export type GeneratorContext<TOptions extends PluginFactoryOptions = PluginFactoryOptions> = Omit<PluginContext<TOptions>, 'adapter' | '
|
|
814
|
+
export type GeneratorContext<TOptions extends PluginFactoryOptions = PluginFactoryOptions> = Omit<PluginContext<TOptions>, 'adapter' | 'inputNode'> & {
|
|
773
815
|
adapter: Adapter
|
|
774
|
-
|
|
816
|
+
inputNode: InputNode
|
|
775
817
|
}
|
|
776
818
|
/**
|
|
777
819
|
* Specify the export location for the files and define the behavior of the output
|
|
@@ -789,11 +831,11 @@ export type Output<_TOptions = unknown> = {
|
|
|
789
831
|
/**
|
|
790
832
|
* Add a banner text in the beginning of every file
|
|
791
833
|
*/
|
|
792
|
-
banner?: string | ((node?:
|
|
834
|
+
banner?: string | ((node?: InputNode) => string)
|
|
793
835
|
/**
|
|
794
836
|
* Add a footer text in the beginning of every file
|
|
795
837
|
*/
|
|
796
|
-
footer?: string | ((node?:
|
|
838
|
+
footer?: string | ((node?: InputNode) => string)
|
|
797
839
|
/**
|
|
798
840
|
* Whether to override existing external files if they already exist.
|
|
799
841
|
* @default false
|
|
@@ -966,8 +1008,8 @@ export type ResolvePathOptions = {
|
|
|
966
1008
|
* ```
|
|
967
1009
|
*/
|
|
968
1010
|
export type ResolverPathParams = {
|
|
969
|
-
baseName:
|
|
970
|
-
pathMode?:
|
|
1011
|
+
baseName: FileNode['baseName']
|
|
1012
|
+
pathMode?: 'single' | 'split'
|
|
971
1013
|
/**
|
|
972
1014
|
* Tag value used when `group.type === 'tag'`.
|
|
973
1015
|
*/
|
|
@@ -1020,7 +1062,7 @@ export type ResolverContext = {
|
|
|
1020
1062
|
*/
|
|
1021
1063
|
export type ResolverFileParams = {
|
|
1022
1064
|
name: string
|
|
1023
|
-
extname:
|
|
1065
|
+
extname: FileNode['extname']
|
|
1024
1066
|
/**
|
|
1025
1067
|
* Tag value used when `group.type === 'tag'`.
|
|
1026
1068
|
*/
|
|
@@ -1039,7 +1081,7 @@ export type ResolverFileParams = {
|
|
|
1039
1081
|
*
|
|
1040
1082
|
* @example
|
|
1041
1083
|
* ```ts
|
|
1042
|
-
* resolver.resolveBanner(
|
|
1084
|
+
* resolver.resolveBanner(inputNode, { output: { banner: '// generated' }, config })
|
|
1043
1085
|
* // → '// generated'
|
|
1044
1086
|
* ```
|
|
1045
1087
|
*/
|
|
@@ -1047,3 +1089,8 @@ export type ResolveBannerContext = {
|
|
|
1047
1089
|
output?: Pick<Output, 'banner' | 'footer'>
|
|
1048
1090
|
config: Config
|
|
1049
1091
|
}
|
|
1092
|
+
|
|
1093
|
+
export type { CLIOptions, ConfigInput } from './defineConfig.ts'
|
|
1094
|
+
export type { Parser, UserParser } from './defineParser.ts'
|
|
1095
|
+
export type { FunctionParamsAST } from './utils/FunctionParams.ts'
|
|
1096
|
+
export type { FileMetaBase } from './utils/getBarrelFiles.ts'
|