@kubb/core 5.0.0-alpha.9 → 5.0.0-beta.75
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/README.md +23 -20
- package/dist/PluginDriver-BXibeQk-.cjs +1036 -0
- package/dist/PluginDriver-BXibeQk-.cjs.map +1 -0
- package/dist/PluginDriver-DV3p2Hky.js +945 -0
- package/dist/PluginDriver-DV3p2Hky.js.map +1 -0
- package/dist/index.cjs +729 -1641
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +271 -225
- package/dist/index.js +713 -1609
- package/dist/index.js.map +1 -1
- package/dist/mocks.cjs +145 -0
- package/dist/mocks.cjs.map +1 -0
- package/dist/mocks.d.ts +80 -0
- package/dist/mocks.js +140 -0
- package/dist/mocks.js.map +1 -0
- package/dist/types-CuNocrbJ.d.ts +2148 -0
- package/package.json +51 -57
- package/src/FileManager.ts +115 -0
- package/src/FileProcessor.ts +86 -0
- package/src/Kubb.ts +207 -131
- package/src/PluginDriver.ts +325 -564
- package/src/constants.ts +20 -47
- package/src/createAdapter.ts +13 -6
- package/src/createKubb.ts +548 -0
- package/src/createRenderer.ts +57 -0
- package/src/createStorage.ts +13 -1
- package/src/defineGenerator.ts +77 -124
- package/src/defineLogger.ts +4 -2
- package/src/defineMiddleware.ts +62 -0
- package/src/defineParser.ts +44 -0
- package/src/definePlugin.ts +83 -0
- package/src/defineResolver.ts +418 -28
- package/src/devtools.ts +14 -14
- package/src/index.ts +13 -15
- package/src/mocks.ts +178 -0
- package/src/renderNode.ts +35 -0
- package/src/storages/fsStorage.ts +41 -11
- package/src/storages/memoryStorage.ts +4 -2
- package/src/types.ts +1031 -283
- package/src/utils/diagnostics.ts +4 -1
- package/src/utils/isInputPath.ts +10 -0
- package/src/utils/packageJSON.ts +50 -12
- package/dist/PluginDriver-BkFepPdm.d.ts +0 -1054
- package/dist/chunk-ByKO4r7w.cjs +0 -38
- package/dist/hooks.cjs +0 -103
- package/dist/hooks.cjs.map +0 -1
- package/dist/hooks.d.ts +0 -77
- package/dist/hooks.js +0 -98
- package/dist/hooks.js.map +0 -1
- package/src/build.ts +0 -418
- package/src/config.ts +0 -56
- package/src/createPlugin.ts +0 -28
- package/src/hooks/index.ts +0 -4
- package/src/hooks/useKubb.ts +0 -143
- package/src/hooks/useMode.ts +0 -11
- package/src/hooks/usePlugin.ts +0 -11
- package/src/hooks/usePluginDriver.ts +0 -11
- package/src/utils/FunctionParams.ts +0 -155
- package/src/utils/TreeNode.ts +0 -215
- package/src/utils/executeStrategies.ts +0 -81
- package/src/utils/formatters.ts +0 -56
- package/src/utils/getBarrelFiles.ts +0 -141
- package/src/utils/getConfigs.ts +0 -12
- package/src/utils/linters.ts +0 -25
package/src/types.ts
CHANGED
|
@@ -1,241 +1,420 @@
|
|
|
1
1
|
import type { AsyncEventEmitter, PossiblePromise } from '@internals/utils'
|
|
2
|
-
import type {
|
|
3
|
-
import type { Fabric as FabricType, KubbFile } from '@kubb/fabric-core/types'
|
|
2
|
+
import type { FileNode, HttpMethod, ImportNode, InputNode, Node, SchemaNode, UserFileNode, Visitor } from '@kubb/ast'
|
|
4
3
|
import type { DEFAULT_STUDIO_URL, logLevel } from './constants.ts'
|
|
4
|
+
import type { RendererFactory } from './createRenderer.ts'
|
|
5
5
|
import type { Storage } from './createStorage.ts'
|
|
6
|
-
import type {
|
|
6
|
+
import type { Generator } from './defineGenerator.ts'
|
|
7
|
+
import type { Middleware } from './defineMiddleware.ts'
|
|
8
|
+
import type { Parser } from './defineParser.ts'
|
|
9
|
+
import type { Plugin } from './definePlugin.ts'
|
|
10
|
+
import type { KubbHooks } from './Kubb.ts'
|
|
7
11
|
import type { PluginDriver } from './PluginDriver.ts'
|
|
8
12
|
|
|
9
|
-
export type {
|
|
10
|
-
|
|
11
|
-
declare global {
|
|
12
|
-
namespace Kubb {
|
|
13
|
-
interface PluginContext {}
|
|
14
|
-
}
|
|
15
|
-
}
|
|
13
|
+
export type { Renderer, RendererFactory } from './createRenderer.ts'
|
|
16
14
|
|
|
17
15
|
/**
|
|
18
|
-
*
|
|
16
|
+
* Safely extracts a type from a registry, returning `{}` if the key doesn't exist.
|
|
17
|
+
* Enables optional interface augmentation for `Kubb.ConfigOptionsRegistry` and `Kubb.PluginOptionsRegistry`
|
|
18
|
+
* without requiring changes to core.
|
|
19
19
|
*
|
|
20
|
-
* @
|
|
21
|
-
* import { defineConfig } from '@kubb/core'
|
|
22
|
-
* export default defineConfig({
|
|
23
|
-
* ...
|
|
24
|
-
* })
|
|
20
|
+
* @internal
|
|
25
21
|
*/
|
|
26
|
-
|
|
27
|
-
/**
|
|
28
|
-
* The project root directory, which can be either an absolute path or a path relative to the location of your `kubb.config.ts` file.
|
|
29
|
-
* @default process.cwd()
|
|
30
|
-
*/
|
|
31
|
-
root?: string
|
|
32
|
-
/**
|
|
33
|
-
* 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.
|
|
34
|
-
*/
|
|
35
|
-
// inject needs to be omitted because else we have a clash with the PluginDriver instance
|
|
36
|
-
plugins?: Array<Omit<UnknownUserPlugin, 'inject'>>
|
|
37
|
-
}
|
|
22
|
+
type ExtractRegistryKey<T, K extends PropertyKey> = K extends keyof T ? T[K] : {}
|
|
38
23
|
|
|
24
|
+
/**
|
|
25
|
+
* Reference to an input file to generate code from.
|
|
26
|
+
*
|
|
27
|
+
* Specify an absolute path or a path relative to the config file location.
|
|
28
|
+
* The adapter will parse this file (e.g., OpenAPI YAML or JSON) into the universal AST.
|
|
29
|
+
*/
|
|
39
30
|
export type InputPath = {
|
|
40
31
|
/**
|
|
41
|
-
*
|
|
32
|
+
* Path to your Swagger/OpenAPI file, absolute or relative to the config file location.
|
|
33
|
+
*
|
|
34
|
+
* @example
|
|
35
|
+
* ```ts
|
|
36
|
+
* { path: './petstore.yaml' }
|
|
37
|
+
* { path: '/absolute/path/to/openapi.json' }
|
|
38
|
+
* ```
|
|
42
39
|
*/
|
|
43
40
|
path: string
|
|
44
41
|
}
|
|
45
42
|
|
|
43
|
+
/**
|
|
44
|
+
* Inline input data to generate code from.
|
|
45
|
+
*
|
|
46
|
+
* Useful when you want to pass the specification directly instead of from a file.
|
|
47
|
+
* Can be a string (YAML/JSON) or a parsed object.
|
|
48
|
+
*/
|
|
46
49
|
export type InputData = {
|
|
47
50
|
/**
|
|
48
|
-
*
|
|
51
|
+
* Swagger/OpenAPI data as a string (YAML/JSON) or a parsed object.
|
|
52
|
+
*
|
|
53
|
+
* @example
|
|
54
|
+
* ```ts
|
|
55
|
+
* { data: fs.readFileSync('./openapi.yaml', 'utf8') }
|
|
56
|
+
* { data: { openapi: '3.1.0', info: { ... } } }
|
|
57
|
+
* ```
|
|
49
58
|
*/
|
|
50
59
|
data: string | unknown
|
|
51
60
|
}
|
|
52
61
|
|
|
53
|
-
type Input = InputPath | InputData
|
|
62
|
+
type Input = InputPath | InputData
|
|
54
63
|
|
|
55
64
|
/**
|
|
56
|
-
*
|
|
57
|
-
* Mirrors the
|
|
65
|
+
* Source data passed to an adapter's `parse` function.
|
|
66
|
+
* Mirrors the config input shape with paths resolved to absolute.
|
|
58
67
|
*/
|
|
59
68
|
export type AdapterSource = { type: 'path'; path: string } | { type: 'data'; data: string | unknown } | { type: 'paths'; paths: Array<string> }
|
|
60
69
|
|
|
61
70
|
/**
|
|
62
|
-
*
|
|
71
|
+
* Generic type parameters for an adapter definition.
|
|
63
72
|
*
|
|
64
|
-
*
|
|
65
|
-
* - `
|
|
66
|
-
* - `
|
|
67
|
-
* - `
|
|
73
|
+
* - `TName` — unique identifier (e.g. `'oas'`, `'asyncapi'`)
|
|
74
|
+
* - `TOptions` — user-facing options passed to the adapter factory
|
|
75
|
+
* - `TResolvedOptions` — options after defaults applied
|
|
76
|
+
* - `TDocument` — type of the parsed source document
|
|
68
77
|
*/
|
|
69
|
-
export type AdapterFactoryOptions<
|
|
78
|
+
export type AdapterFactoryOptions<
|
|
79
|
+
TName extends string = string,
|
|
80
|
+
TOptions extends object = object,
|
|
81
|
+
TResolvedOptions extends object = TOptions,
|
|
82
|
+
TDocument = unknown,
|
|
83
|
+
> = {
|
|
70
84
|
name: TName
|
|
71
85
|
options: TOptions
|
|
72
86
|
resolvedOptions: TResolvedOptions
|
|
87
|
+
document: TDocument
|
|
73
88
|
}
|
|
74
89
|
|
|
75
90
|
/**
|
|
76
|
-
*
|
|
91
|
+
* Adapter that converts input files or data into an `InputNode`.
|
|
77
92
|
*
|
|
78
|
-
* Adapters
|
|
79
|
-
*
|
|
80
|
-
* that all Kubb plugins consume.
|
|
93
|
+
* Adapters parse different schema formats (OpenAPI, AsyncAPI, Drizzle, etc.) into Kubb's
|
|
94
|
+
* universal intermediate representation that all plugins consume.
|
|
81
95
|
*
|
|
82
96
|
* @example
|
|
83
97
|
* ```ts
|
|
84
|
-
* import {
|
|
98
|
+
* import { adapterOas } from '@kubb/adapter-oas'
|
|
85
99
|
*
|
|
86
100
|
* export default defineConfig({
|
|
87
|
-
* adapter: adapterOas(),
|
|
88
|
-
* input:
|
|
101
|
+
* adapter: adapterOas(),
|
|
102
|
+
* input: { path: './openapi.yaml' },
|
|
89
103
|
* plugins: [pluginTs(), pluginZod()],
|
|
90
104
|
* })
|
|
91
105
|
* ```
|
|
92
106
|
*/
|
|
93
107
|
export type Adapter<TOptions extends AdapterFactoryOptions = AdapterFactoryOptions> = {
|
|
94
|
-
/**
|
|
108
|
+
/**
|
|
109
|
+
* Human-readable adapter identifier (e.g. `'oas'`, `'asyncapi'`).
|
|
110
|
+
*/
|
|
95
111
|
name: TOptions['name']
|
|
96
|
-
/**
|
|
112
|
+
/**
|
|
113
|
+
* Resolved adapter options after defaults have been applied.
|
|
114
|
+
*/
|
|
97
115
|
options: TOptions['resolvedOptions']
|
|
98
|
-
/** Convert the raw source into a universal `RootNode`. */
|
|
99
|
-
parse: (source: AdapterSource) => PossiblePromise<RootNode>
|
|
100
116
|
/**
|
|
101
|
-
*
|
|
102
|
-
|
|
117
|
+
* Parsed source document after the first `parse()` call. `null` before parsing.
|
|
118
|
+
*/
|
|
119
|
+
document: TOptions['document'] | null
|
|
120
|
+
inputNode: InputNode | null
|
|
121
|
+
/**
|
|
122
|
+
* Parse the source into a universal `InputNode`.
|
|
123
|
+
*/
|
|
124
|
+
parse: (source: AdapterSource) => PossiblePromise<InputNode>
|
|
125
|
+
/**
|
|
126
|
+
* Extract `ImportNode` entries for a schema tree.
|
|
127
|
+
* Returns an empty array before the first `parse()` call.
|
|
103
128
|
*
|
|
104
129
|
* The `resolve` callback receives the collision-corrected schema name and must
|
|
105
|
-
* return
|
|
130
|
+
* return `{ name, path }` for the import, or `undefined` to skip it.
|
|
106
131
|
*/
|
|
107
|
-
getImports: (node: SchemaNode, resolve: (schemaName: string) => { name: string; path: string }) => Array<
|
|
132
|
+
getImports: (node: SchemaNode, resolve: (schemaName: string) => { name: string; path: string }) => Array<ImportNode>
|
|
108
133
|
}
|
|
109
134
|
|
|
110
|
-
export type BarrelType = 'all' | 'named' | 'propagate'
|
|
111
|
-
|
|
112
135
|
export type DevtoolsOptions = {
|
|
113
136
|
/**
|
|
114
|
-
* Open the AST inspector
|
|
115
|
-
* When `false`, opens the main Studio page instead.
|
|
137
|
+
* Open the AST inspector in Kubb Studio (`/ast`). Defaults to the main Studio page.
|
|
116
138
|
* @default false
|
|
117
139
|
*/
|
|
118
140
|
ast?: boolean
|
|
119
141
|
}
|
|
120
142
|
|
|
121
143
|
/**
|
|
144
|
+
* Build configuration for Kubb code generation.
|
|
145
|
+
*
|
|
146
|
+
* The Config is the main entry point for customizing how Kubb generates code. It specifies:
|
|
147
|
+
* - What to generate from (adapter + input)
|
|
148
|
+
* - Where to output generated code (output)
|
|
149
|
+
* - How to generate (plugins + middleware)
|
|
150
|
+
* - Runtime details (parsers, storage, renderer)
|
|
151
|
+
*
|
|
152
|
+
* See `UserConfig` for a relaxed version with sensible defaults.
|
|
153
|
+
*
|
|
122
154
|
* @private
|
|
123
155
|
*/
|
|
124
156
|
export type Config<TInput = Input> = {
|
|
125
157
|
/**
|
|
126
|
-
*
|
|
158
|
+
* Display name for this configuration in CLI output and logs.
|
|
159
|
+
* Useful when running multiple builds with `defineConfig` arrays.
|
|
160
|
+
*
|
|
161
|
+
* @example
|
|
162
|
+
* ```ts
|
|
163
|
+
* name: 'api-client'
|
|
164
|
+
* ```
|
|
127
165
|
*/
|
|
128
166
|
name?: string
|
|
129
167
|
/**
|
|
130
|
-
*
|
|
168
|
+
* Project root directory, absolute or relative to the config file.
|
|
131
169
|
* @default process.cwd()
|
|
132
170
|
*/
|
|
133
171
|
root: string
|
|
134
172
|
/**
|
|
135
|
-
*
|
|
136
|
-
*
|
|
173
|
+
* Parsers that convert generated files to strings.
|
|
174
|
+
* Each parser handles specific extensions (e.g. `.ts`, `.tsx`).
|
|
175
|
+
* A fallback parser is appended for unhandled extensions.
|
|
176
|
+
* When omitted, defaults to `parserTs` from `@kubb/parser-ts`.
|
|
137
177
|
*
|
|
138
|
-
*
|
|
139
|
-
*
|
|
140
|
-
*
|
|
178
|
+
* @default [parserTs] from `@kubb/parser-ts`
|
|
179
|
+
* @example
|
|
180
|
+
* ```ts
|
|
181
|
+
* import { parserTs, tsxParser } from '@kubb/parser-ts'
|
|
182
|
+
* export default defineConfig({
|
|
183
|
+
* parsers: [parserTs, tsxParser],
|
|
184
|
+
* })
|
|
185
|
+
* ```
|
|
186
|
+
*/
|
|
187
|
+
parsers: Array<Parser>
|
|
188
|
+
/**
|
|
189
|
+
* Adapter that parses input files into the universal `InputNode` representation.
|
|
190
|
+
* Use `@kubb/adapter-oas` for OpenAPI/Swagger or `@kubb/adapter-asyncapi` for other formats.
|
|
141
191
|
*
|
|
142
192
|
* @example
|
|
143
193
|
* ```ts
|
|
144
|
-
* import {
|
|
194
|
+
* import { adapterOas } from '@kubb/adapter-oas'
|
|
145
195
|
* export default defineConfig({
|
|
146
|
-
* adapter:
|
|
147
|
-
* input: { path: './
|
|
196
|
+
* adapter: adapterOas(),
|
|
197
|
+
* input: { path: './petstore.yaml' },
|
|
148
198
|
* })
|
|
149
199
|
* ```
|
|
150
200
|
*/
|
|
151
|
-
adapter
|
|
201
|
+
adapter: Adapter
|
|
152
202
|
/**
|
|
153
|
-
*
|
|
203
|
+
* Source file or data to generate code from.
|
|
204
|
+
* Use `input.path` for a file path or `input.data` for inline data.
|
|
154
205
|
*/
|
|
155
206
|
input: TInput
|
|
156
207
|
output: {
|
|
157
208
|
/**
|
|
158
|
-
*
|
|
159
|
-
*
|
|
209
|
+
* Output directory for generated files, absolute or relative to `root`.
|
|
210
|
+
*
|
|
211
|
+
* All generated files will be written under this directory. Subdirectories can be created
|
|
212
|
+
* by plugins based on grouping strategy (by tag, path, etc.).
|
|
213
|
+
*
|
|
214
|
+
* @example
|
|
215
|
+
* ```ts
|
|
216
|
+
* output: {
|
|
217
|
+
* path: './src/gen', // generates ./src/gen/api.ts, ./src/gen/types.ts, etc.
|
|
218
|
+
* }
|
|
219
|
+
* ```
|
|
160
220
|
*/
|
|
161
221
|
path: string
|
|
162
222
|
/**
|
|
163
|
-
*
|
|
223
|
+
* Remove all files from the output directory before starting the build.
|
|
224
|
+
*
|
|
225
|
+
* Useful to ensure old generated files aren't mixed with new ones.
|
|
226
|
+
* Set to `true` for fresh builds, `false` to preserve manual edits in output dir.
|
|
227
|
+
*
|
|
228
|
+
* @default false
|
|
229
|
+
* @example
|
|
230
|
+
* ```ts
|
|
231
|
+
* clean: true // wipes ./src/gen/* before generating
|
|
232
|
+
* ```
|
|
164
233
|
*/
|
|
165
234
|
clean?: boolean
|
|
166
235
|
/**
|
|
167
|
-
*
|
|
236
|
+
* Persists generated files to the file system.
|
|
237
|
+
*
|
|
168
238
|
* @default true
|
|
169
|
-
* @deprecated Use `storage` to control where files are written.
|
|
239
|
+
* @deprecated Use `storage` option to control where files are written instead.
|
|
170
240
|
*/
|
|
171
241
|
write?: boolean
|
|
172
242
|
/**
|
|
173
|
-
*
|
|
174
|
-
*
|
|
175
|
-
*
|
|
176
|
-
*
|
|
177
|
-
*
|
|
243
|
+
* Auto-format generated files after code generation completes.
|
|
244
|
+
*
|
|
245
|
+
* Applies a code formatter to all generated files. Use `'auto'` to detect which formatter
|
|
246
|
+
* is available on your system. Pass `false` to skip formatting (useful for CI or specific workflows).
|
|
247
|
+
*
|
|
248
|
+
* @default false
|
|
178
249
|
* @example
|
|
179
250
|
* ```ts
|
|
180
|
-
*
|
|
181
|
-
*
|
|
251
|
+
* format: 'auto' // auto-detect prettier, biome, or oxfmt
|
|
252
|
+
* format: 'prettier' // force prettier
|
|
253
|
+
* format: false // skip formatting
|
|
182
254
|
* ```
|
|
183
255
|
*/
|
|
184
|
-
storage?: Storage
|
|
185
|
-
/**
|
|
186
|
-
* Specifies the formatting tool to be used.
|
|
187
|
-
* - 'auto' automatically detects and uses biome or prettier (in that order of preference).
|
|
188
|
-
* - 'prettier' uses Prettier for code formatting.
|
|
189
|
-
* - 'biome' uses Biome for code formatting.
|
|
190
|
-
* - 'oxfmt' uses Oxfmt for code formatting.
|
|
191
|
-
* - false disables code formatting.
|
|
192
|
-
* @default 'prettier'
|
|
193
|
-
*/
|
|
194
256
|
format?: 'auto' | 'prettier' | 'biome' | 'oxfmt' | false
|
|
195
257
|
/**
|
|
196
|
-
*
|
|
197
|
-
*
|
|
198
|
-
*
|
|
199
|
-
*
|
|
200
|
-
*
|
|
201
|
-
*
|
|
202
|
-
* @
|
|
258
|
+
* Auto-lint generated files after code generation completes.
|
|
259
|
+
*
|
|
260
|
+
* Analyzes all generated files for style/correctness issues. Use `'auto'` to detect which linter
|
|
261
|
+
* is available on your system. Pass `false` to skip linting.
|
|
262
|
+
*
|
|
263
|
+
* @default false
|
|
264
|
+
* @example
|
|
265
|
+
* ```ts
|
|
266
|
+
* lint: 'auto' // auto-detect oxlint, biome, or eslint
|
|
267
|
+
* lint: 'eslint' // force eslint
|
|
268
|
+
* lint: false // skip linting
|
|
269
|
+
* ```
|
|
203
270
|
*/
|
|
204
271
|
lint?: 'auto' | 'eslint' | 'biome' | 'oxlint' | false
|
|
205
272
|
/**
|
|
206
|
-
*
|
|
207
|
-
*
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
*
|
|
212
|
-
* @
|
|
273
|
+
* Map file extensions to different output extensions.
|
|
274
|
+
*
|
|
275
|
+
* Useful when you want generated `.ts` imports to reference `.js` files or vice versa (e.g., for ESM dual packages).
|
|
276
|
+
* Keys are the original extension, values are the output extension. Use empty string `''` to omit extension.
|
|
277
|
+
*
|
|
278
|
+
* @default { '.ts': '.ts' }
|
|
279
|
+
* @example
|
|
280
|
+
* ```ts
|
|
281
|
+
* extension: { '.ts': '.js' } // generates import './api.js' instead of './api.ts'
|
|
282
|
+
* extension: { '.ts': '', '.tsx': '.jsx' }
|
|
283
|
+
* ```
|
|
213
284
|
*/
|
|
214
|
-
|
|
285
|
+
extension?: Record<FileNode['extname'], FileNode['extname'] | ''>
|
|
215
286
|
/**
|
|
216
|
-
*
|
|
217
|
-
*
|
|
218
|
-
* -
|
|
219
|
-
*
|
|
287
|
+
* Banner text prepended to every generated file.
|
|
288
|
+
*
|
|
289
|
+
* Useful for auto-generation notices or license headers. Choose a preset or write custom text.
|
|
290
|
+
* Use `'simple'` for a basic Kubb banner, `'full'` for detailed metadata, or `false` to omit.
|
|
291
|
+
*
|
|
220
292
|
* @default 'simple'
|
|
293
|
+
* @example
|
|
294
|
+
* ```ts
|
|
295
|
+
* defaultBanner: 'simple' // "This file was autogenerated by Kubb"
|
|
296
|
+
* defaultBanner: 'full' // adds source, title, description, API version
|
|
297
|
+
* defaultBanner: false // no banner
|
|
298
|
+
* ```
|
|
221
299
|
*/
|
|
222
300
|
defaultBanner?: 'simple' | 'full' | false
|
|
223
301
|
/**
|
|
224
|
-
*
|
|
225
|
-
*
|
|
226
|
-
*
|
|
302
|
+
* When `true`, overwrites existing files. When `false`, skips generated files that already exist.
|
|
303
|
+
*
|
|
304
|
+
* Individual plugins can override this setting. This is useful for preventing accidental data loss
|
|
305
|
+
* when re-generating while you have local edits in the output folder.
|
|
306
|
+
*
|
|
227
307
|
* @default false
|
|
308
|
+
* @example
|
|
309
|
+
* ```ts
|
|
310
|
+
* override: true // regenerate everything, even existing files
|
|
311
|
+
* override: false // skip files that already exist
|
|
312
|
+
* ```
|
|
228
313
|
*/
|
|
229
314
|
override?: boolean
|
|
230
|
-
}
|
|
315
|
+
} & ExtractRegistryKey<Kubb.ConfigOptionsRegistry, 'output'>
|
|
231
316
|
/**
|
|
232
|
-
*
|
|
233
|
-
*
|
|
234
|
-
*
|
|
317
|
+
* Storage backend that controls where and how generated files are persisted.
|
|
318
|
+
*
|
|
319
|
+
* Defaults to `fsStorage()` which writes to the file system. Pass `memoryStorage()` to keep files in RAM,
|
|
320
|
+
* or implement a custom `Storage` interface to write to cloud storage, databases, or other backends.
|
|
321
|
+
*
|
|
322
|
+
* @default fsStorage()
|
|
323
|
+
* @example
|
|
324
|
+
* ```ts
|
|
325
|
+
* import { memoryStorage } from '@kubb/core'
|
|
326
|
+
*
|
|
327
|
+
* // Keep generated files in memory (useful for testing, CI pipelines)
|
|
328
|
+
* storage: memoryStorage()
|
|
329
|
+
*
|
|
330
|
+
* // Use custom S3 storage
|
|
331
|
+
* storage: myS3Storage()
|
|
332
|
+
* ```
|
|
333
|
+
*
|
|
334
|
+
* @see {@link Storage} interface for implementing custom backends.
|
|
235
335
|
*/
|
|
236
|
-
|
|
336
|
+
storage?: Storage
|
|
337
|
+
/**
|
|
338
|
+
* Plugins that execute during the build to generate code and transform the AST.
|
|
339
|
+
*
|
|
340
|
+
* Each plugin processes the AST produced by the adapter and can emit files for different
|
|
341
|
+
* programming languages or formats (TypeScript, Zod schemas, Faker data, etc.).
|
|
342
|
+
* Dependencies are enforced — an error is thrown if a plugin requires another plugin that isn't registered.
|
|
343
|
+
*
|
|
344
|
+
* Plugins can declare their own options via `PluginFactoryOptions`. See plugin documentation for details.
|
|
345
|
+
*
|
|
346
|
+
* @example
|
|
347
|
+
* ```ts
|
|
348
|
+
* import { pluginTs } from '@kubb/plugin-ts'
|
|
349
|
+
* import { pluginZod } from '@kubb/plugin-zod'
|
|
350
|
+
*
|
|
351
|
+
* plugins: [
|
|
352
|
+
* pluginTs({ output: { path: './src/gen' } }),
|
|
353
|
+
* pluginZod({ output: { path: './src/gen' } }),
|
|
354
|
+
* ]
|
|
355
|
+
* ```
|
|
356
|
+
*/
|
|
357
|
+
plugins: Array<Plugin>
|
|
358
|
+
/**
|
|
359
|
+
* Middleware instances that observe build events and post-process generated code.
|
|
360
|
+
*
|
|
361
|
+
* Middleware fires AFTER all plugins for each event. Perfect for tasks like:
|
|
362
|
+
* - Auditing what was generated
|
|
363
|
+
* - Adding barrel/index files
|
|
364
|
+
* - Validating output
|
|
365
|
+
* - Running custom transformations
|
|
366
|
+
*
|
|
367
|
+
* @example
|
|
368
|
+
* ```ts
|
|
369
|
+
* import { middlewareBarrel } from '@kubb/middleware-barrel'
|
|
370
|
+
*
|
|
371
|
+
* middleware: [middlewareBarrel()]
|
|
372
|
+
* ```
|
|
373
|
+
*
|
|
374
|
+
* @see {@link defineMiddleware} to create custom middleware.
|
|
375
|
+
*/
|
|
376
|
+
middleware?: Array<Middleware>
|
|
377
|
+
/**
|
|
378
|
+
* Default renderer factory used by all plugins and generators.
|
|
379
|
+
* Resolution chain: `generator.renderer` → `plugin.renderer` → `config.renderer` → `undefined` (raw FileNode[] mode).
|
|
380
|
+
*
|
|
381
|
+
* @example
|
|
382
|
+
* ```ts
|
|
383
|
+
* import { jsxRenderer } from '@kubb/renderer-jsx'
|
|
384
|
+
* export default defineConfig({
|
|
385
|
+
* renderer: jsxRenderer,
|
|
386
|
+
* plugins: [pluginTs(), pluginZod()],
|
|
387
|
+
* })
|
|
388
|
+
* ```
|
|
389
|
+
*/
|
|
390
|
+
/**
|
|
391
|
+
* Renderer that converts generated AST nodes to code strings.
|
|
392
|
+
*
|
|
393
|
+
* By default, Kubb uses the JSX renderer (`rendererJsx`). Pass a custom renderer to support
|
|
394
|
+
* different output formats (template engines, code generation DSLs, etc.).
|
|
395
|
+
*
|
|
396
|
+
* @default rendererJsx() // from @kubb/renderer-jsx
|
|
397
|
+
* @example
|
|
398
|
+
* ```ts
|
|
399
|
+
* import { rendererJsx } from '@kubb/renderer-jsx'
|
|
400
|
+
* renderer: rendererJsx()
|
|
401
|
+
* ```
|
|
402
|
+
*
|
|
403
|
+
* @see {@link Renderer} to implement a custom renderer.
|
|
404
|
+
*/
|
|
405
|
+
renderer?: RendererFactory
|
|
237
406
|
/**
|
|
238
|
-
*
|
|
407
|
+
* Kubb Studio cloud integration settings.
|
|
408
|
+
*
|
|
409
|
+
* Kubb Studio (https://studio.kubb.dev) is a web-based IDE for managing API specs and generated code.
|
|
410
|
+
* Set to `true` to enable with default settings, or pass an object to customize the Studio URL.
|
|
411
|
+
*
|
|
412
|
+
* @default false // disabled by default
|
|
413
|
+
* @example
|
|
414
|
+
* ```ts
|
|
415
|
+
* devtools: true // use default Kubb Studio
|
|
416
|
+
* devtools: { studioUrl: 'https://my-studio.dev' } // custom Studio instance
|
|
417
|
+
* ```
|
|
239
418
|
*/
|
|
240
419
|
devtools?:
|
|
241
420
|
| true
|
|
@@ -247,12 +426,33 @@ export type Config<TInput = Input> = {
|
|
|
247
426
|
studioUrl?: typeof DEFAULT_STUDIO_URL | (string & {})
|
|
248
427
|
}
|
|
249
428
|
/**
|
|
250
|
-
*
|
|
429
|
+
* Lifecycle hooks that execute during or after the build process.
|
|
430
|
+
*
|
|
431
|
+
* Hooks allow you to run external tools (prettier, eslint, custom scripts) based on build events.
|
|
432
|
+
* Currently supports the `done` hook which fires after all plugins and middleware complete.
|
|
433
|
+
*
|
|
434
|
+
* @example
|
|
435
|
+
* ```ts
|
|
436
|
+
* hooks: {
|
|
437
|
+
* done: 'prettier --write "./src/gen"', // auto-format generated files
|
|
438
|
+
* // or multiple commands:
|
|
439
|
+
* done: ['prettier --write "./src/gen"', 'eslint --fix "./src/gen"']
|
|
440
|
+
* }
|
|
441
|
+
* ```
|
|
251
442
|
*/
|
|
252
443
|
hooks?: {
|
|
253
444
|
/**
|
|
254
|
-
*
|
|
255
|
-
*
|
|
445
|
+
* Command(s) to run after all plugins and middleware complete generation.
|
|
446
|
+
*
|
|
447
|
+
* Useful for post-processing: formatting, linting, copying files, or custom validation.
|
|
448
|
+
* Pass a single command string or array of command strings to run sequentially.
|
|
449
|
+
* Commands are executed relative to the `root` directory.
|
|
450
|
+
*
|
|
451
|
+
* @example
|
|
452
|
+
* ```ts
|
|
453
|
+
* done: 'prettier --write "./src/gen"'
|
|
454
|
+
* done: ['prettier --write "./src/gen"', 'eslint --fix "./src/gen"']
|
|
455
|
+
* ```
|
|
256
456
|
*/
|
|
257
457
|
done?: string | Array<string>
|
|
258
458
|
}
|
|
@@ -260,15 +460,26 @@ export type Config<TInput = Input> = {
|
|
|
260
460
|
|
|
261
461
|
// plugin
|
|
262
462
|
|
|
463
|
+
/**
|
|
464
|
+
* Type/string pattern filter for include/exclude/override matching.
|
|
465
|
+
*/
|
|
263
466
|
type PatternFilter = {
|
|
264
467
|
type: string
|
|
265
468
|
pattern: string | RegExp
|
|
266
469
|
}
|
|
267
470
|
|
|
471
|
+
/**
|
|
472
|
+
* Pattern filter with partial option overrides applied when the pattern matches.
|
|
473
|
+
*/
|
|
268
474
|
type PatternOverride<TOptions> = PatternFilter & {
|
|
269
475
|
options: Omit<Partial<TOptions>, 'override'>
|
|
270
476
|
}
|
|
271
477
|
|
|
478
|
+
/**
|
|
479
|
+
* Context for resolving filtered options for a given operation or schema node.
|
|
480
|
+
*
|
|
481
|
+
* @internal
|
|
482
|
+
*/
|
|
272
483
|
export type ResolveOptionsContext<TOptions> = {
|
|
273
484
|
options: TOptions
|
|
274
485
|
exclude?: Array<PatternFilter>
|
|
@@ -279,270 +490,807 @@ export type ResolveOptionsContext<TOptions> = {
|
|
|
279
490
|
/**
|
|
280
491
|
* Base constraint for all plugin resolver objects.
|
|
281
492
|
*
|
|
282
|
-
* `default
|
|
283
|
-
*
|
|
284
|
-
*
|
|
493
|
+
* `default`, `resolveOptions`, `resolvePath`, `resolveFile`, `resolveBanner`, and `resolveFooter`
|
|
494
|
+
* are injected automatically by `defineResolver` — extend this type to add custom resolution methods.
|
|
495
|
+
*
|
|
496
|
+
* @example
|
|
497
|
+
* ```ts
|
|
498
|
+
* type MyResolver = Resolver & {
|
|
499
|
+
* resolveName(node: SchemaNode): string
|
|
500
|
+
* resolveTypedName(node: SchemaNode): string
|
|
501
|
+
* }
|
|
502
|
+
* ```
|
|
285
503
|
*/
|
|
286
504
|
export type Resolver = {
|
|
287
|
-
|
|
505
|
+
name: string
|
|
506
|
+
pluginName: Plugin['name']
|
|
507
|
+
default(name: string, type?: 'file' | 'function' | 'type' | 'const'): string
|
|
288
508
|
resolveOptions<TOptions>(node: Node, context: ResolveOptionsContext<TOptions>): TOptions | null
|
|
509
|
+
resolvePath(params: ResolverPathParams, context: ResolverContext): string
|
|
510
|
+
resolveFile(params: ResolverFileParams, context: ResolverContext): FileNode
|
|
511
|
+
resolveBanner(node: InputNode | null, context: ResolveBannerContext): string | undefined
|
|
512
|
+
resolveFooter(node: InputNode | null, context: ResolveBannerContext): string | undefined
|
|
289
513
|
}
|
|
290
514
|
|
|
291
|
-
/**
|
|
292
|
-
* The user-facing subset of a `Resolver` — everything except the methods injected by
|
|
293
|
-
* `defineResolver` (`default` and `resolveOptions`).
|
|
294
|
-
*
|
|
295
|
-
* When you pass a `UserResolver` to `defineResolver`, the standard `default` and
|
|
296
|
-
* `resolveOptions` implementations are injected automatically so plugin authors never
|
|
297
|
-
* need to define them by hand. Both can still be overridden by providing them explicitly.
|
|
298
|
-
*/
|
|
299
|
-
export type UserResolver = Omit<Resolver, 'default' | 'resolveOptions'>
|
|
300
|
-
|
|
301
515
|
export type PluginFactoryOptions<
|
|
302
516
|
/**
|
|
303
|
-
*
|
|
517
|
+
* Unique plugin name.
|
|
304
518
|
*/
|
|
305
519
|
TName extends string = string,
|
|
306
520
|
/**
|
|
307
|
-
*
|
|
521
|
+
* User-facing plugin options.
|
|
308
522
|
*/
|
|
309
523
|
TOptions extends object = object,
|
|
310
524
|
/**
|
|
311
|
-
*
|
|
525
|
+
* Plugin options after defaults are applied.
|
|
312
526
|
*/
|
|
313
527
|
TResolvedOptions extends object = TOptions,
|
|
314
528
|
/**
|
|
315
|
-
*
|
|
316
|
-
|
|
317
|
-
TContext = unknown,
|
|
318
|
-
/**
|
|
319
|
-
* When calling `resolvePath` you can specify better types.
|
|
320
|
-
*/
|
|
321
|
-
TResolvePathOptions extends object = object,
|
|
322
|
-
/**
|
|
323
|
-
* Resolver object that encapsulates the naming and path-resolution helpers used by this plugin.
|
|
324
|
-
* Use `defineResolver` to define the resolver object and export it alongside the plugin.
|
|
529
|
+
* Resolver that encapsulates naming and path-resolution helpers.
|
|
530
|
+
* Define with `defineResolver` and export alongside the plugin.
|
|
325
531
|
*/
|
|
326
532
|
TResolver extends Resolver = Resolver,
|
|
327
533
|
> = {
|
|
328
534
|
name: TName
|
|
329
535
|
options: TOptions
|
|
330
536
|
resolvedOptions: TResolvedOptions
|
|
331
|
-
context: TContext
|
|
332
|
-
resolvePathOptions: TResolvePathOptions
|
|
333
537
|
resolver: TResolver
|
|
334
538
|
}
|
|
335
539
|
|
|
336
|
-
|
|
540
|
+
/**
|
|
541
|
+
* Normalized plugin after setup, with runtime fields populated.
|
|
542
|
+
* For internal use only — plugins use the public `Plugin` type externally.
|
|
543
|
+
*
|
|
544
|
+
* @internal
|
|
545
|
+
*/
|
|
546
|
+
export type NormalizedPlugin<TOptions extends PluginFactoryOptions = PluginFactoryOptions> = Plugin<TOptions> & {
|
|
547
|
+
options: TOptions['resolvedOptions'] & {
|
|
548
|
+
output: Output
|
|
549
|
+
include?: Array<Include>
|
|
550
|
+
exclude: Array<Exclude>
|
|
551
|
+
override: Array<Override<TOptions['resolvedOptions']>>
|
|
552
|
+
}
|
|
553
|
+
resolver: TOptions['resolver']
|
|
554
|
+
transformer?: Visitor
|
|
555
|
+
renderer?: RendererFactory
|
|
556
|
+
generators?: Array<Generator>
|
|
557
|
+
apply?: (config: Config) => boolean
|
|
558
|
+
version?: string
|
|
559
|
+
}
|
|
560
|
+
|
|
561
|
+
/**
|
|
562
|
+
* Partial `Config` for user-facing entry points with sensible defaults.
|
|
563
|
+
*
|
|
564
|
+
* `UserConfig` is what you pass to `defineConfig()`. It has optional `root`, `plugins`, `parsers`, and `adapter`
|
|
565
|
+
* fields (which fall back to sensible defaults). All other Config options are available, including `output`, `input`,
|
|
566
|
+
* `storage`, `middleware`, `renderer`, `devtools`, and `hooks`.
|
|
567
|
+
*
|
|
568
|
+
* @example
|
|
569
|
+
* ```ts
|
|
570
|
+
* export default defineConfig({
|
|
571
|
+
* input: { path: './petstore.yaml' },
|
|
572
|
+
* output: { path: './src/gen' },
|
|
573
|
+
* plugins: [pluginTs(), pluginZod()],
|
|
574
|
+
* })
|
|
575
|
+
* ```
|
|
576
|
+
*/
|
|
577
|
+
export type UserConfig<TInput = Input> = Omit<Config<TInput>, 'root' | 'plugins' | 'parsers' | 'adapter'> & {
|
|
337
578
|
/**
|
|
338
|
-
*
|
|
339
|
-
*
|
|
340
|
-
*
|
|
579
|
+
* Project root directory, absolute or relative to the config file location.
|
|
580
|
+
*
|
|
581
|
+
* Used as the base path for `root`-relative paths (e.g., `output.path`, file paths in hooks).
|
|
582
|
+
*
|
|
583
|
+
* @default process.cwd()
|
|
584
|
+
* @example
|
|
585
|
+
* ```ts
|
|
586
|
+
* root: '/home/user/my-project'
|
|
587
|
+
* root: './my-project' // relative to config file
|
|
588
|
+
* ```
|
|
341
589
|
*/
|
|
342
|
-
|
|
590
|
+
root?: string
|
|
591
|
+
/**
|
|
592
|
+
* Custom parsers that convert generated AST nodes to strings (TypeScript, JSON, markdown, etc.).
|
|
593
|
+
*
|
|
594
|
+
* Each parser handles a specific file type. By default, Kubb uses `parserTs` from `@kubb/parser-ts` for TypeScript files.
|
|
595
|
+
* Pass custom parsers to support additional languages or custom formats.
|
|
596
|
+
*
|
|
597
|
+
* @default [parserTs] // from @kubb/parser-ts
|
|
598
|
+
* @example
|
|
599
|
+
* ```ts
|
|
600
|
+
* import { parserTs } from '@kubb/parser-ts'
|
|
601
|
+
* import { parserJsonSchema } from '@kubb/parser-json-schema'
|
|
602
|
+
*
|
|
603
|
+
* parsers: [parserTs(), parserJsonSchema()]
|
|
604
|
+
* ```
|
|
605
|
+
*
|
|
606
|
+
* @see {@link Parser} to implement a custom parser.
|
|
607
|
+
*/
|
|
608
|
+
parsers?: Array<Parser>
|
|
609
|
+
/**
|
|
610
|
+
* Adapter that parses your API specification (OpenAPI, GraphQL, AsyncAPI, etc.) into Kubb's universal AST.
|
|
611
|
+
*
|
|
612
|
+
* The adapter bridge between your input format and Kubb's internal representation. By default, uses the OAS adapter.
|
|
613
|
+
* Pass an alternative adapter (or multiple configs with different adapters) to support different spec formats.
|
|
614
|
+
*
|
|
615
|
+
* @default new OasAdapter() // from @kubb/adapter-oas
|
|
616
|
+
* @example
|
|
617
|
+
* ```ts
|
|
618
|
+
* import { Oas } from '@kubb/adapter-oas'
|
|
619
|
+
*
|
|
620
|
+
* adapter: new Oas({ apiVersion: '3.0.0' })
|
|
621
|
+
* ```
|
|
622
|
+
*
|
|
623
|
+
* @see {@link Adapter} to implement a custom adapter for GraphQL or other formats.
|
|
624
|
+
*/
|
|
625
|
+
adapter?: Adapter
|
|
626
|
+
/**
|
|
627
|
+
* Plugins that execute during the build to generate code and transform the AST.
|
|
628
|
+
*
|
|
629
|
+
* Each plugin processes the AST produced by the adapter and can emit files for different
|
|
630
|
+
* programming languages or formats (TypeScript, Zod schemas, Faker data, etc.).
|
|
631
|
+
*
|
|
632
|
+
* @default [] // no plugins (useful for setup/testing)
|
|
633
|
+
* @example
|
|
634
|
+
* ```ts
|
|
635
|
+
* plugins: [
|
|
636
|
+
* pluginTs({ output: { path: './src/gen' } }),
|
|
637
|
+
* pluginZod({ output: { path: './src/gen' } }),
|
|
638
|
+
* ]
|
|
639
|
+
* ```
|
|
640
|
+
*
|
|
641
|
+
* @see {@link definePlugin} to create a custom plugin.
|
|
642
|
+
*/
|
|
643
|
+
plugins?: Array<Plugin>
|
|
644
|
+
}
|
|
645
|
+
|
|
646
|
+
export type ResolveNameParams = {
|
|
647
|
+
name: string
|
|
648
|
+
pluginName?: string
|
|
649
|
+
/**
|
|
650
|
+
* Entity type being named.
|
|
651
|
+
* - `'file'` — file name (camelCase)
|
|
652
|
+
* - `'function'` — exported function name (camelCase)
|
|
653
|
+
* - `'type'` — TypeScript type name (PascalCase)
|
|
654
|
+
* - `'const'` — variable name (camelCase)
|
|
655
|
+
*/
|
|
656
|
+
type?: 'file' | 'function' | 'type' | 'const'
|
|
657
|
+
}
|
|
658
|
+
/**
|
|
659
|
+
* Context object passed to generator `schema`, `operation`, and `operations` methods.
|
|
660
|
+
*
|
|
661
|
+
* The adapter is always defined (guaranteed by `runPluginAstHooks`) so no runtime checks
|
|
662
|
+
* are needed. `ctx.options` carries resolved per-node options after exclude/include/override
|
|
663
|
+
* filtering for individual schema/operation calls, or plugin-level options for operations.
|
|
664
|
+
*/
|
|
665
|
+
export type GeneratorContext<TOptions extends PluginFactoryOptions = PluginFactoryOptions> = {
|
|
666
|
+
config: Config
|
|
667
|
+
/**
|
|
668
|
+
* Absolute path to the current plugin's output directory.
|
|
669
|
+
*/
|
|
670
|
+
root: string
|
|
671
|
+
/**
|
|
672
|
+
* Determine output mode based on the output config.
|
|
673
|
+
* Returns `'single'` when `output.path` is a file, `'split'` for a directory.
|
|
674
|
+
*/
|
|
675
|
+
getMode: (output: { path: string }) => 'single' | 'split'
|
|
676
|
+
driver: PluginDriver
|
|
677
|
+
/**
|
|
678
|
+
* Get a plugin by name, typed via `Kubb.PluginRegistry` when registered.
|
|
679
|
+
*/
|
|
680
|
+
getPlugin<TName extends keyof Kubb.PluginRegistry>(name: TName): Plugin<Kubb.PluginRegistry[TName]> | undefined
|
|
681
|
+
getPlugin(name: string): Plugin | undefined
|
|
682
|
+
/**
|
|
683
|
+
* Get a plugin by name, throws an error if not found.
|
|
684
|
+
*/
|
|
685
|
+
requirePlugin<TName extends keyof Kubb.PluginRegistry>(name: TName): Plugin<Kubb.PluginRegistry[TName]>
|
|
686
|
+
requirePlugin(name: string): Plugin
|
|
687
|
+
/**
|
|
688
|
+
* Get a resolver by plugin name, typed via `Kubb.PluginRegistry` when registered.
|
|
689
|
+
*/
|
|
690
|
+
getResolver<TName extends keyof Kubb.PluginRegistry>(name: TName): Kubb.PluginRegistry[TName]['resolver']
|
|
691
|
+
getResolver(name: string): Resolver
|
|
692
|
+
/**
|
|
693
|
+
* Add files only if they don't exist.
|
|
694
|
+
*/
|
|
695
|
+
addFile: (...file: Array<FileNode>) => Promise<void>
|
|
696
|
+
/**
|
|
697
|
+
* Merge sources into the same output file.
|
|
698
|
+
*/
|
|
699
|
+
upsertFile: (...file: Array<FileNode>) => Promise<void>
|
|
700
|
+
hooks: AsyncEventEmitter<KubbHooks>
|
|
701
|
+
/**
|
|
702
|
+
* The current plugin instance.
|
|
703
|
+
*/
|
|
704
|
+
plugin: Plugin<TOptions>
|
|
705
|
+
/**
|
|
706
|
+
* The current plugin's resolver.
|
|
707
|
+
*/
|
|
708
|
+
resolver: TOptions['resolver']
|
|
709
|
+
/**
|
|
710
|
+
* The current plugin's transformer.
|
|
711
|
+
*/
|
|
712
|
+
transformer: Visitor | undefined
|
|
713
|
+
/**
|
|
714
|
+
* Emit a warning.
|
|
715
|
+
*/
|
|
716
|
+
warn: (message: string) => void
|
|
717
|
+
/**
|
|
718
|
+
* Emit an error.
|
|
719
|
+
*/
|
|
720
|
+
error: (error: string | Error) => void
|
|
721
|
+
/**
|
|
722
|
+
* Emit an info message.
|
|
723
|
+
*/
|
|
724
|
+
info: (message: string) => void
|
|
725
|
+
/**
|
|
726
|
+
* Open the current input node in Kubb Studio.
|
|
727
|
+
*/
|
|
728
|
+
openInStudio: (options?: DevtoolsOptions) => Promise<void>
|
|
729
|
+
/**
|
|
730
|
+
* The configured adapter instance.
|
|
731
|
+
*/
|
|
732
|
+
adapter: Adapter
|
|
343
733
|
/**
|
|
344
|
-
*
|
|
734
|
+
* The universal `InputNode` produced by the adapter.
|
|
735
|
+
*/
|
|
736
|
+
inputNode: InputNode
|
|
737
|
+
/**
|
|
738
|
+
* Resolved options after exclude/include/override filtering.
|
|
345
739
|
*/
|
|
346
740
|
options: TOptions['resolvedOptions']
|
|
741
|
+
}
|
|
742
|
+
/**
|
|
743
|
+
* Output configuration for generated files.
|
|
744
|
+
*/
|
|
745
|
+
export type Output<_TOptions = unknown> = {
|
|
746
|
+
/**
|
|
747
|
+
* Output folder or file path for generated code.
|
|
748
|
+
*/
|
|
749
|
+
path: string
|
|
347
750
|
/**
|
|
348
|
-
*
|
|
349
|
-
*
|
|
751
|
+
* Text or function prepended to every generated file.
|
|
752
|
+
* When a function, receives the current `InputNode` and returns a string.
|
|
350
753
|
*/
|
|
351
|
-
|
|
754
|
+
banner?: string | ((node?: InputNode) => string)
|
|
352
755
|
/**
|
|
353
|
-
*
|
|
756
|
+
* Text or function appended to every generated file.
|
|
757
|
+
* When a function, receives the current `InputNode` and returns a string.
|
|
758
|
+
*/
|
|
759
|
+
footer?: string | ((node?: InputNode) => string)
|
|
760
|
+
/**
|
|
761
|
+
* Whether to override existing external files if they already exist.
|
|
762
|
+
* @default false
|
|
354
763
|
*/
|
|
355
|
-
|
|
356
|
-
|
|
764
|
+
override?: boolean
|
|
765
|
+
} & ExtractRegistryKey<Kubb.PluginOptionsRegistry, 'output'>
|
|
766
|
+
|
|
767
|
+
export type Group = {
|
|
768
|
+
/**
|
|
769
|
+
* How to group files into subdirectories.
|
|
770
|
+
* - `'tag'` — group by OpenAPI tags
|
|
771
|
+
* - `'path'` — group by OpenAPI paths
|
|
772
|
+
*/
|
|
773
|
+
type: 'tag' | 'path'
|
|
774
|
+
/**
|
|
775
|
+
* Function that returns the subdirectory name for a group value.
|
|
776
|
+
* Defaults to `${camelCase(group)}Controller` for tags, first path segment for paths.
|
|
777
|
+
*/
|
|
778
|
+
name?: (context: { group: string }) => string
|
|
779
|
+
}
|
|
780
|
+
|
|
781
|
+
export type LoggerOptions = {
|
|
782
|
+
/**
|
|
783
|
+
* Log level for output verbosity.
|
|
784
|
+
* @default 3
|
|
785
|
+
*/
|
|
786
|
+
logLevel: (typeof logLevel)[keyof typeof logLevel]
|
|
787
|
+
}
|
|
788
|
+
|
|
789
|
+
/**
|
|
790
|
+
* Shared context passed to plugins, parsers, and other internals.
|
|
791
|
+
*/
|
|
792
|
+
export type LoggerContext = AsyncEventEmitter<KubbHooks>
|
|
793
|
+
|
|
794
|
+
export type Logger<TOptions extends LoggerOptions = LoggerOptions> = {
|
|
795
|
+
name: string
|
|
796
|
+
install: (context: LoggerContext, options?: TOptions) => void | Promise<void>
|
|
357
797
|
}
|
|
358
798
|
|
|
359
|
-
export type
|
|
799
|
+
export type UserLogger<TOptions extends LoggerOptions = LoggerOptions> = Logger<TOptions>
|
|
360
800
|
|
|
361
|
-
type
|
|
801
|
+
export type { Storage } from './createStorage.ts'
|
|
802
|
+
export type { Generator } from './defineGenerator.ts'
|
|
803
|
+
export type { Middleware } from './defineMiddleware.ts'
|
|
804
|
+
export type { Plugin } from './definePlugin.ts'
|
|
805
|
+
export type { Kubb, KubbHooks } from './Kubb.ts'
|
|
362
806
|
|
|
363
|
-
|
|
807
|
+
/**
|
|
808
|
+
* Context for hook-style plugin `kubb:plugin:setup` handler.
|
|
809
|
+
* Provides methods to register generators, configure resolvers, transformers, and renderers.
|
|
810
|
+
*/
|
|
811
|
+
export type KubbPluginSetupContext<TFactory extends PluginFactoryOptions = PluginFactoryOptions> = {
|
|
364
812
|
/**
|
|
365
|
-
*
|
|
366
|
-
*
|
|
813
|
+
* Register a generator dynamically. Generators fire during the AST walk (schema/operation/operations)
|
|
814
|
+
* just like generators declared statically on `createPlugin`.
|
|
367
815
|
*/
|
|
368
|
-
|
|
816
|
+
addGenerator<TElement = unknown>(generator: Generator<TFactory, TElement>): void
|
|
369
817
|
/**
|
|
370
|
-
*
|
|
371
|
-
*
|
|
818
|
+
* Set or override the resolver for this plugin.
|
|
819
|
+
* The resolver controls file naming and path resolution.
|
|
372
820
|
*/
|
|
373
|
-
|
|
821
|
+
setResolver(resolver: Partial<TFactory['resolver']>): void
|
|
374
822
|
/**
|
|
375
|
-
*
|
|
823
|
+
* Set the AST transformer to pre-process nodes before they reach generators.
|
|
376
824
|
*/
|
|
377
|
-
|
|
825
|
+
setTransformer(visitor: Visitor): void
|
|
378
826
|
/**
|
|
379
|
-
*
|
|
827
|
+
* Set the renderer factory to process JSX elements from generators.
|
|
380
828
|
*/
|
|
381
|
-
|
|
829
|
+
setRenderer(renderer: RendererFactory): void
|
|
830
|
+
/**
|
|
831
|
+
* Set resolved options merged into the normalized plugin's `options`.
|
|
832
|
+
* Call this in `kubb:plugin:setup` to provide options generators need.
|
|
833
|
+
*/
|
|
834
|
+
setOptions(options: TFactory['resolvedOptions']): void
|
|
835
|
+
/**
|
|
836
|
+
* Inject a raw file into the build output, bypassing the generation pipeline.
|
|
837
|
+
*/
|
|
838
|
+
injectFile(userFileNode: UserFileNode): void
|
|
839
|
+
/**
|
|
840
|
+
* Merge a partial config update into the current build configuration.
|
|
841
|
+
*/
|
|
842
|
+
updateConfig(config: Partial<Config>): void
|
|
843
|
+
/**
|
|
844
|
+
* The resolved build configuration at setup time.
|
|
845
|
+
*/
|
|
846
|
+
config: Config
|
|
847
|
+
/**
|
|
848
|
+
* The plugin's user-provided options.
|
|
849
|
+
*/
|
|
850
|
+
options: TFactory['options']
|
|
851
|
+
}
|
|
382
852
|
|
|
383
|
-
|
|
853
|
+
/**
|
|
854
|
+
* Context for hook-style plugin `kubb:build:start` handler.
|
|
855
|
+
* Fires immediately before the plugin execution loop begins.
|
|
856
|
+
*/
|
|
857
|
+
export type KubbBuildStartContext = {
|
|
858
|
+
config: Config
|
|
859
|
+
adapter: Adapter
|
|
860
|
+
inputNode: InputNode
|
|
384
861
|
/**
|
|
385
|
-
*
|
|
862
|
+
* Get a plugin by name, typed via `Kubb.PluginRegistry` when registered.
|
|
386
863
|
*/
|
|
387
|
-
|
|
864
|
+
getPlugin<TName extends keyof Kubb.PluginRegistry>(name: TName): Plugin<Kubb.PluginRegistry[TName]> | undefined
|
|
865
|
+
getPlugin(name: string): Plugin | undefined
|
|
866
|
+
/**
|
|
867
|
+
* Get all files currently in the file manager.
|
|
868
|
+
* Call this lazily (e.g. in `kubb:plugin:end`) to see files added by prior plugins.
|
|
869
|
+
*/
|
|
870
|
+
readonly files: ReadonlyArray<FileNode>
|
|
871
|
+
/**
|
|
872
|
+
* Upsert one or more files into the file manager.
|
|
873
|
+
* Files with the same path are merged; new files are appended.
|
|
874
|
+
* Safe to call at any point during the plugin lifecycle, including inside `kubb:plugin:end`.
|
|
875
|
+
*/
|
|
876
|
+
upsertFile: (...files: Array<FileNode>) => void
|
|
388
877
|
}
|
|
389
878
|
|
|
390
|
-
|
|
879
|
+
/**
|
|
880
|
+
* Context for `kubb:plugins:end` handlers.
|
|
881
|
+
* Fires after plugins run and per-plugin barrels are written, before final write to disk.
|
|
882
|
+
* Middleware that needs final files (e.g. root barrel) use this event.
|
|
883
|
+
*/
|
|
884
|
+
export type KubbPluginsEndContext = {
|
|
885
|
+
config: Config
|
|
886
|
+
/**
|
|
887
|
+
* All files currently in the file manager (lazy snapshot).
|
|
888
|
+
*/
|
|
889
|
+
readonly files: ReadonlyArray<FileNode>
|
|
890
|
+
/**
|
|
891
|
+
* Upsert files into the file manager.
|
|
892
|
+
* Files added here are included in the write pass.
|
|
893
|
+
*/
|
|
894
|
+
upsertFile: (...files: Array<FileNode>) => void
|
|
895
|
+
}
|
|
391
896
|
|
|
392
|
-
|
|
897
|
+
/**
|
|
898
|
+
* Context for hook-style plugin `kubb:build:end` handler.
|
|
899
|
+
* Fires after all files have been written to disk.
|
|
900
|
+
*/
|
|
901
|
+
export type KubbBuildEndContext = {
|
|
902
|
+
files: Array<FileNode>
|
|
903
|
+
config: Config
|
|
904
|
+
outputDir: string
|
|
905
|
+
}
|
|
906
|
+
|
|
907
|
+
export type KubbLifecycleStartContext = {
|
|
908
|
+
version: string
|
|
909
|
+
}
|
|
910
|
+
|
|
911
|
+
export type KubbConfigEndContext = {
|
|
912
|
+
configs: Array<Config>
|
|
913
|
+
}
|
|
914
|
+
|
|
915
|
+
export type KubbGenerationStartContext = {
|
|
916
|
+
config: Config
|
|
917
|
+
}
|
|
918
|
+
|
|
919
|
+
export type KubbGenerationEndContext = {
|
|
920
|
+
config: Config
|
|
921
|
+
files: Array<FileNode>
|
|
922
|
+
sources: Map<string, string>
|
|
923
|
+
}
|
|
924
|
+
|
|
925
|
+
export type KubbGenerationSummaryContext = {
|
|
926
|
+
config: Config
|
|
927
|
+
failedPlugins: Set<{ plugin: Plugin; error: Error }>
|
|
928
|
+
status: 'success' | 'failed'
|
|
929
|
+
hrStart: [number, number]
|
|
930
|
+
filesCreated: number
|
|
931
|
+
pluginTimings?: Map<Plugin['name'], number>
|
|
932
|
+
}
|
|
933
|
+
|
|
934
|
+
export type KubbVersionNewContext = {
|
|
935
|
+
currentVersion: string
|
|
936
|
+
latestVersion: string
|
|
937
|
+
}
|
|
938
|
+
|
|
939
|
+
export type KubbInfoContext = {
|
|
940
|
+
message: string
|
|
941
|
+
info?: string
|
|
942
|
+
}
|
|
943
|
+
|
|
944
|
+
export type KubbErrorContext = {
|
|
945
|
+
error: Error
|
|
946
|
+
meta?: Record<string, unknown>
|
|
947
|
+
}
|
|
948
|
+
|
|
949
|
+
export type KubbSuccessContext = {
|
|
950
|
+
message: string
|
|
951
|
+
info?: string
|
|
952
|
+
}
|
|
953
|
+
|
|
954
|
+
export type KubbWarnContext = {
|
|
955
|
+
message: string
|
|
956
|
+
info?: string
|
|
957
|
+
}
|
|
958
|
+
|
|
959
|
+
export type KubbDebugContext = {
|
|
960
|
+
date: Date
|
|
961
|
+
logs: Array<string>
|
|
962
|
+
fileName?: string
|
|
963
|
+
}
|
|
964
|
+
|
|
965
|
+
export type KubbFilesProcessingStartContext = {
|
|
966
|
+
files: Array<FileNode>
|
|
967
|
+
}
|
|
968
|
+
|
|
969
|
+
export type KubbFileProcessingUpdateContext = {
|
|
970
|
+
/**
|
|
971
|
+
* Number of files processed.
|
|
972
|
+
*/
|
|
973
|
+
processed: number
|
|
393
974
|
/**
|
|
394
|
-
*
|
|
395
|
-
* @type hookParallel
|
|
975
|
+
* Total files to process.
|
|
396
976
|
*/
|
|
397
|
-
|
|
977
|
+
total: number
|
|
398
978
|
/**
|
|
399
|
-
*
|
|
400
|
-
* Options can als be included.
|
|
401
|
-
* @type hookFirst
|
|
402
|
-
* @example ('./Pet.ts', './src/gen/') => '/src/gen/Pet.ts'
|
|
979
|
+
* Processing percentage (0–100).
|
|
403
980
|
*/
|
|
404
|
-
|
|
981
|
+
percentage: number
|
|
405
982
|
/**
|
|
406
|
-
*
|
|
407
|
-
* Useful when converting to PascalCase or camelCase.
|
|
408
|
-
* @type hookFirst
|
|
409
|
-
* @example ('pet') => 'Pet'
|
|
983
|
+
* Optional source identifier.
|
|
410
984
|
*/
|
|
411
|
-
|
|
985
|
+
source?: string
|
|
986
|
+
/**
|
|
987
|
+
* The file being processed.
|
|
988
|
+
*/
|
|
989
|
+
file: FileNode
|
|
990
|
+
/**
|
|
991
|
+
* The current build configuration.
|
|
992
|
+
*/
|
|
993
|
+
config: Config
|
|
412
994
|
}
|
|
413
995
|
|
|
414
|
-
export type
|
|
996
|
+
export type KubbFilesProcessingEndContext = {
|
|
997
|
+
files: Array<FileNode>
|
|
998
|
+
}
|
|
415
999
|
|
|
416
|
-
export type
|
|
1000
|
+
export type KubbPluginStartContext = {
|
|
1001
|
+
plugin: NormalizedPlugin
|
|
1002
|
+
}
|
|
417
1003
|
|
|
418
|
-
export type
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
1004
|
+
export type KubbPluginEndContext = {
|
|
1005
|
+
plugin: NormalizedPlugin
|
|
1006
|
+
duration: number
|
|
1007
|
+
success: boolean
|
|
1008
|
+
error?: Error
|
|
1009
|
+
config: Config
|
|
1010
|
+
/**
|
|
1011
|
+
* Returns all files currently in the file manager (lazy snapshot).
|
|
1012
|
+
* Includes files added by plugins that have already run.
|
|
1013
|
+
*/
|
|
1014
|
+
readonly files: ReadonlyArray<FileNode>
|
|
422
1015
|
/**
|
|
423
|
-
*
|
|
1016
|
+
* Upsert one or more files into the file manager.
|
|
424
1017
|
*/
|
|
425
|
-
|
|
1018
|
+
upsertFile: (...files: Array<FileNode>) => void
|
|
426
1019
|
}
|
|
427
1020
|
|
|
428
|
-
export type
|
|
429
|
-
|
|
430
|
-
|
|
1021
|
+
export type KubbHookStartContext = {
|
|
1022
|
+
id?: string
|
|
1023
|
+
command: string
|
|
1024
|
+
args?: readonly string[]
|
|
1025
|
+
}
|
|
1026
|
+
|
|
1027
|
+
export type KubbHookEndContext = {
|
|
1028
|
+
id?: string
|
|
1029
|
+
command: string
|
|
1030
|
+
args?: readonly string[]
|
|
1031
|
+
success: boolean
|
|
1032
|
+
error: Error | null
|
|
1033
|
+
}
|
|
1034
|
+
|
|
1035
|
+
type ByTag = {
|
|
431
1036
|
/**
|
|
432
|
-
*
|
|
433
|
-
* - 'file' customizes the name of the created file (uses camelCase).
|
|
434
|
-
* - 'function' customizes the exported function names (uses camelCase).
|
|
435
|
-
* - 'type' customizes TypeScript types (uses PascalCase).
|
|
436
|
-
* - 'const' customizes variable names (uses camelCase).
|
|
437
|
-
* @default undefined
|
|
1037
|
+
* Filter by OpenAPI `tags` field. Matches one or more tags assigned to operations.
|
|
438
1038
|
*/
|
|
439
|
-
type
|
|
1039
|
+
type: 'tag'
|
|
1040
|
+
/**
|
|
1041
|
+
* Tag name to match (case-sensitive). Can be a literal string or regex pattern.
|
|
1042
|
+
*/
|
|
1043
|
+
pattern: string | RegExp
|
|
440
1044
|
}
|
|
441
1045
|
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
1046
|
+
type ByOperationId = {
|
|
1047
|
+
/**
|
|
1048
|
+
* Filter by OpenAPI `operationId` field. Each operation (GET, POST, etc.) has a unique identifier.
|
|
1049
|
+
*/
|
|
1050
|
+
type: 'operationId'
|
|
446
1051
|
/**
|
|
447
|
-
*
|
|
1052
|
+
* Operation ID to match (case-sensitive). Can be a literal string or regex pattern.
|
|
448
1053
|
*/
|
|
449
|
-
|
|
1054
|
+
pattern: string | RegExp
|
|
1055
|
+
}
|
|
1056
|
+
|
|
1057
|
+
type ByPath = {
|
|
450
1058
|
/**
|
|
451
|
-
*
|
|
1059
|
+
* Filter by OpenAPI `path` (URL endpoint). Useful to group or filter by service segments like `/pets`, `/users`, etc.
|
|
452
1060
|
*/
|
|
453
|
-
|
|
454
|
-
events: AsyncEventEmitter<KubbEvents>
|
|
455
|
-
mode: KubbFile.Mode
|
|
1061
|
+
type: 'path'
|
|
456
1062
|
/**
|
|
457
|
-
*
|
|
1063
|
+
* URL path to match (case-sensitive). Can be a literal string or regex pattern. Matches against the full path.
|
|
458
1064
|
*/
|
|
459
|
-
|
|
1065
|
+
pattern: string | RegExp
|
|
1066
|
+
}
|
|
460
1067
|
|
|
1068
|
+
type ByMethod = {
|
|
461
1069
|
/**
|
|
462
|
-
*
|
|
463
|
-
* Falls back to printing the URL if the browser cannot be launched.
|
|
464
|
-
* No-ops silently when no adapter has set a `rootNode`.
|
|
1070
|
+
* Filter by HTTP method: `'get'`, `'post'`, `'put'`, `'delete'`, `'patch'`, `'head'`, `'options'`.
|
|
465
1071
|
*/
|
|
466
|
-
|
|
467
|
-
} & (
|
|
468
|
-
| {
|
|
469
|
-
/**
|
|
470
|
-
* Returns the universal `@kubb/ast` `RootNode` produced by the configured adapter.
|
|
471
|
-
* Returns `undefined` when no adapter was set (legacy OAS-only usage).
|
|
472
|
-
*/
|
|
473
|
-
rootNode: RootNode
|
|
474
|
-
/**
|
|
475
|
-
* Return the adapter from `@kubb/ast`
|
|
476
|
-
*/
|
|
477
|
-
adapter: Adapter
|
|
478
|
-
}
|
|
479
|
-
| {
|
|
480
|
-
rootNode?: never
|
|
481
|
-
adapter?: never
|
|
482
|
-
}
|
|
483
|
-
) &
|
|
484
|
-
Kubb.PluginContext
|
|
485
|
-
/**
|
|
486
|
-
* Specify the export location for the files and define the behavior of the output
|
|
487
|
-
*/
|
|
488
|
-
export type Output<TOptions> = {
|
|
1072
|
+
type: 'method'
|
|
489
1073
|
/**
|
|
490
|
-
*
|
|
1074
|
+
* HTTP method to match (case-insensitive when using string, or regex for dynamic matching).
|
|
491
1075
|
*/
|
|
492
|
-
|
|
1076
|
+
pattern: HttpMethod | RegExp
|
|
1077
|
+
}
|
|
1078
|
+
// TODO implement as alternative for ByMethod
|
|
1079
|
+
// type ByMethods = {
|
|
1080
|
+
// type: 'methods'
|
|
1081
|
+
// pattern: Array<HttpMethod>
|
|
1082
|
+
// }
|
|
1083
|
+
|
|
1084
|
+
type BySchemaName = {
|
|
493
1085
|
/**
|
|
494
|
-
*
|
|
495
|
-
* @default 'named'
|
|
1086
|
+
* Filter by schema component name (TypeScript or JSON schema). Matches schemas in `#/components/schemas`.
|
|
496
1087
|
*/
|
|
497
|
-
|
|
1088
|
+
type: 'schemaName'
|
|
498
1089
|
/**
|
|
499
|
-
*
|
|
1090
|
+
* Schema name to match (case-sensitive). Can be a literal string or regex pattern.
|
|
500
1091
|
*/
|
|
501
|
-
|
|
1092
|
+
pattern: string | RegExp
|
|
1093
|
+
}
|
|
1094
|
+
|
|
1095
|
+
type ByContentType = {
|
|
502
1096
|
/**
|
|
503
|
-
*
|
|
1097
|
+
* Filter by response or request content type: `'application/json'`, `'application/xml'`, etc.
|
|
504
1098
|
*/
|
|
505
|
-
|
|
1099
|
+
type: 'contentType'
|
|
506
1100
|
/**
|
|
507
|
-
*
|
|
508
|
-
* @default false
|
|
1101
|
+
* Content type to match (case-sensitive). Can be a literal string or regex pattern.
|
|
509
1102
|
*/
|
|
510
|
-
|
|
1103
|
+
pattern: string | RegExp
|
|
511
1104
|
}
|
|
512
1105
|
|
|
513
|
-
|
|
1106
|
+
/**
|
|
1107
|
+
* A pattern filter that prevents matching nodes from being generated.
|
|
1108
|
+
*
|
|
1109
|
+
* Use to skip code generation for specific operations or schemas. For example, exclude deprecated endpoints
|
|
1110
|
+
* or internal-only schemas. Can filter by tag, operationId, path, HTTP method, content type, or schema name.
|
|
1111
|
+
*
|
|
1112
|
+
* @example
|
|
1113
|
+
* ```ts
|
|
1114
|
+
* exclude: [
|
|
1115
|
+
* { type: 'tag', pattern: 'internal' }, // skip "internal" tag
|
|
1116
|
+
* { type: 'path', pattern: /^\/admin/ }, // skip all /admin endpoints
|
|
1117
|
+
* { type: 'operationId', pattern: 'deprecated_*' } // skip operationIds matching pattern
|
|
1118
|
+
* ]
|
|
1119
|
+
* ```
|
|
1120
|
+
*/
|
|
1121
|
+
export type Exclude = ByTag | ByOperationId | ByPath | ByMethod | ByContentType | BySchemaName
|
|
1122
|
+
|
|
1123
|
+
/**
|
|
1124
|
+
* A pattern filter that restricts generation to only matching nodes.
|
|
1125
|
+
*
|
|
1126
|
+
* Use to generate code for a subset of operations or schemas. For example, only generate for a specific service
|
|
1127
|
+
* tag or only for "production" endpoints. Can filter by tag, operationId, path, HTTP method, content type, or schema name.
|
|
1128
|
+
*
|
|
1129
|
+
* @example
|
|
1130
|
+
* ```ts
|
|
1131
|
+
* include: [
|
|
1132
|
+
* { type: 'tag', pattern: 'public' }, // generate only "public" tag
|
|
1133
|
+
* { type: 'path', pattern: /^\/api\/v1/ }, // generate only v1 endpoints
|
|
1134
|
+
* ]
|
|
1135
|
+
* ```
|
|
1136
|
+
*/
|
|
1137
|
+
export type Include = ByTag | ByOperationId | ByPath | ByMethod | ByContentType | BySchemaName
|
|
1138
|
+
|
|
1139
|
+
/**
|
|
1140
|
+
* A pattern filter paired with partial option overrides applied when the pattern matches.
|
|
1141
|
+
*
|
|
1142
|
+
* Use to customize generation for specific operations or schemas. For example, apply different output paths
|
|
1143
|
+
* for different tags, or use custom resolver functions per operation. Can filter by tag, operationId, path,
|
|
1144
|
+
* HTTP method, schema name, or content type.
|
|
1145
|
+
*
|
|
1146
|
+
* @example
|
|
1147
|
+
* ```ts
|
|
1148
|
+
* override: [
|
|
1149
|
+
* {
|
|
1150
|
+
* type: 'tag',
|
|
1151
|
+
* pattern: 'admin',
|
|
1152
|
+
* options: { output: { path: './src/gen/admin' } } // admin APIs go to separate folder
|
|
1153
|
+
* },
|
|
1154
|
+
* {
|
|
1155
|
+
* type: 'operationId',
|
|
1156
|
+
* pattern: 'listPets',
|
|
1157
|
+
* options: { exclude: true } // skip this specific operation
|
|
1158
|
+
* }
|
|
1159
|
+
* ]
|
|
1160
|
+
* ```
|
|
1161
|
+
*/
|
|
1162
|
+
export type Override<TOptions> = (ByTag | ByOperationId | ByPath | ByMethod | BySchemaName | ByContentType) & {
|
|
1163
|
+
//TODO should be options: Omit<Partial<TOptions>, 'override'>
|
|
1164
|
+
options: Partial<TOptions>
|
|
1165
|
+
}
|
|
1166
|
+
|
|
1167
|
+
/**
|
|
1168
|
+
* File-specific parameters for `Resolver.resolvePath`.
|
|
1169
|
+
*
|
|
1170
|
+
* Pass alongside a `ResolverContext` to identify which file to resolve.
|
|
1171
|
+
* Provide `tag` for tag-based grouping or `path` for path-based grouping.
|
|
1172
|
+
*
|
|
1173
|
+
* @example
|
|
1174
|
+
* ```ts
|
|
1175
|
+
* resolver.resolvePath(
|
|
1176
|
+
* { baseName: 'petTypes.ts', tag: 'pets' },
|
|
1177
|
+
* { root: '/src', output: { path: 'types' }, group: { type: 'tag' } },
|
|
1178
|
+
* )
|
|
1179
|
+
* // → '/src/types/petsController/petTypes.ts'
|
|
1180
|
+
* ```
|
|
1181
|
+
*/
|
|
1182
|
+
export type ResolverPathParams = {
|
|
1183
|
+
baseName: FileNode['baseName']
|
|
1184
|
+
pathMode?: 'single' | 'split'
|
|
514
1185
|
/**
|
|
515
|
-
*
|
|
516
|
-
* - 'tag' groups files by OpenAPI tags.
|
|
517
|
-
* - 'path' groups files by OpenAPI paths.
|
|
518
|
-
* @default undefined
|
|
1186
|
+
* Tag value used when `group.type === 'tag'`.
|
|
519
1187
|
*/
|
|
520
|
-
|
|
1188
|
+
tag?: string
|
|
521
1189
|
/**
|
|
522
|
-
*
|
|
1190
|
+
* Path value used when `group.type === 'path'`.
|
|
523
1191
|
*/
|
|
524
|
-
|
|
1192
|
+
path?: string
|
|
525
1193
|
}
|
|
526
1194
|
|
|
527
|
-
|
|
1195
|
+
/**
|
|
1196
|
+
* Shared context passed as the second argument to `Resolver.resolvePath` and `Resolver.resolveFile`.
|
|
1197
|
+
*
|
|
1198
|
+
* Describes where on disk output is rooted, which output config is active, and the optional
|
|
1199
|
+
* grouping strategy that controls subdirectory layout.
|
|
1200
|
+
*
|
|
1201
|
+
* @example
|
|
1202
|
+
* ```ts
|
|
1203
|
+
* const context: ResolverContext = {
|
|
1204
|
+
* root: config.root,
|
|
1205
|
+
* output,
|
|
1206
|
+
* group,
|
|
1207
|
+
* }
|
|
1208
|
+
* ```
|
|
1209
|
+
*/
|
|
1210
|
+
export type ResolverContext = {
|
|
1211
|
+
root: string
|
|
1212
|
+
output: Output
|
|
1213
|
+
group?: Group
|
|
528
1214
|
/**
|
|
529
|
-
*
|
|
1215
|
+
* Plugin name used to populate `meta.pluginName` on the resolved file.
|
|
530
1216
|
*/
|
|
531
|
-
|
|
1217
|
+
pluginName?: string
|
|
532
1218
|
}
|
|
533
1219
|
|
|
534
1220
|
/**
|
|
535
|
-
*
|
|
1221
|
+
* File-specific parameters for `Resolver.resolveFile`.
|
|
1222
|
+
*
|
|
1223
|
+
* Pass alongside a `ResolverContext` to fully describe the file to resolve.
|
|
1224
|
+
* `tag` and `path` are used only when a matching `group` is present in the context.
|
|
1225
|
+
*
|
|
1226
|
+
* @example
|
|
1227
|
+
* ```ts
|
|
1228
|
+
* resolver.resolveFile(
|
|
1229
|
+
* { name: 'listPets', extname: '.ts', tag: 'pets' },
|
|
1230
|
+
* { root: '/src', output: { path: 'types' }, group: { type: 'tag' } },
|
|
1231
|
+
* )
|
|
1232
|
+
* // → { baseName: 'listPets.ts', path: '/src/types/petsController/listPets.ts', ... }
|
|
1233
|
+
* ```
|
|
536
1234
|
*/
|
|
537
|
-
export type
|
|
538
|
-
|
|
539
|
-
export type Logger<TOptions extends LoggerOptions = LoggerOptions> = {
|
|
1235
|
+
export type ResolverFileParams = {
|
|
540
1236
|
name: string
|
|
541
|
-
|
|
1237
|
+
extname: FileNode['extname']
|
|
1238
|
+
/**
|
|
1239
|
+
* Tag value used when `group.type === 'tag'`.
|
|
1240
|
+
*/
|
|
1241
|
+
tag?: string
|
|
1242
|
+
/**
|
|
1243
|
+
* Path value used when `group.type === 'path'`.
|
|
1244
|
+
*/
|
|
1245
|
+
path?: string
|
|
542
1246
|
}
|
|
543
1247
|
|
|
544
|
-
|
|
1248
|
+
/**
|
|
1249
|
+
* Context passed to `Resolver.resolveBanner` and `Resolver.resolveFooter`.
|
|
1250
|
+
*
|
|
1251
|
+
* `output` is optional — not every plugin configures a banner/footer.
|
|
1252
|
+
* `config` carries the global Kubb config, used to derive the default Kubb banner.
|
|
1253
|
+
*
|
|
1254
|
+
* @example
|
|
1255
|
+
* ```ts
|
|
1256
|
+
* resolver.resolveBanner(inputNode, { output: { banner: '// generated' }, config })
|
|
1257
|
+
* // → '// generated'
|
|
1258
|
+
* ```
|
|
1259
|
+
*/
|
|
1260
|
+
export type ResolveBannerContext = {
|
|
1261
|
+
output?: Pick<Output, 'banner' | 'footer'>
|
|
1262
|
+
config: Config
|
|
1263
|
+
}
|
|
545
1264
|
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
1265
|
+
/**
|
|
1266
|
+
* CLI options derived from command-line flags.
|
|
1267
|
+
*/
|
|
1268
|
+
export type CLIOptions = {
|
|
1269
|
+
/**
|
|
1270
|
+
* Path to `kubb.config.js`.
|
|
1271
|
+
*/
|
|
1272
|
+
config?: string
|
|
1273
|
+
/**
|
|
1274
|
+
* Enable watch mode for input files.
|
|
1275
|
+
*/
|
|
1276
|
+
watch?: boolean
|
|
1277
|
+
/**
|
|
1278
|
+
* Logging verbosity for CLI usage.
|
|
1279
|
+
* @default 'silent'
|
|
1280
|
+
*/
|
|
1281
|
+
logLevel?: 'silent' | 'info' | 'debug'
|
|
1282
|
+
}
|
|
1283
|
+
|
|
1284
|
+
/**
|
|
1285
|
+
* All accepted forms of a Kubb configuration.
|
|
1286
|
+
*
|
|
1287
|
+
* Config is always `@kubb/core` {@link Config}.
|
|
1288
|
+
* - `PossibleConfig` accepts `Config`/`Config[]`/promise or a no-arg config factory.
|
|
1289
|
+
* - `PossibleConfig<TCliOptions>` accepts the same config forms or a config factory receiving `TCliOptions`.
|
|
1290
|
+
*/
|
|
1291
|
+
export type PossibleConfig<TCliOptions = undefined> =
|
|
1292
|
+
| PossiblePromise<Config | Config[]>
|
|
1293
|
+
| ((...args: [TCliOptions] extends [undefined] ? [] : [TCliOptions]) => PossiblePromise<Config | Config[]>)
|
|
1294
|
+
|
|
1295
|
+
export type { BuildOutput } from './createKubb.ts'
|
|
1296
|
+
export type { Parser } from './defineParser.ts'
|