@kubb/core 5.0.0-alpha.73 → 5.0.0-alpha.74
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 +20 -18
- package/dist/index.cjs +47 -28
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +14 -7
- package/dist/index.js +47 -28
- package/dist/index.js.map +1 -1
- package/dist/mocks.cjs +6 -6
- package/dist/mocks.cjs.map +1 -1
- package/dist/mocks.d.ts +7 -7
- package/dist/mocks.js +6 -6
- package/dist/mocks.js.map +1 -1
- package/dist/{types-bVvdPbki.d.ts → types-CuNocrbJ.d.ts} +557 -324
- package/package.json +5 -5
- package/src/Kubb.ts +44 -71
- package/src/createAdapter.ts +13 -6
- package/src/createStorage.ts +13 -1
- package/src/defineGenerator.ts +7 -14
- package/src/defineMiddleware.ts +9 -11
- package/src/defineParser.ts +2 -3
- package/src/definePlugin.ts +9 -9
- package/src/mocks.ts +6 -6
- package/src/types.ts +472 -214
package/src/types.ts
CHANGED
|
@@ -13,25 +13,48 @@ import type { PluginDriver } from './PluginDriver.ts'
|
|
|
13
13
|
export type { Renderer, RendererFactory } from './createRenderer.ts'
|
|
14
14
|
|
|
15
15
|
/**
|
|
16
|
-
* Safely extracts
|
|
17
|
-
*
|
|
18
|
-
*
|
|
19
|
-
* without requiring modifications to core.
|
|
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.
|
|
20
19
|
*
|
|
21
20
|
* @internal
|
|
22
21
|
*/
|
|
23
22
|
type ExtractRegistryKey<T, K extends PropertyKey> = K extends keyof T ? T[K] : {}
|
|
24
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
|
+
*/
|
|
25
30
|
export type InputPath = {
|
|
26
31
|
/**
|
|
27
|
-
*
|
|
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
|
+
* ```
|
|
28
39
|
*/
|
|
29
40
|
path: string
|
|
30
41
|
}
|
|
31
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
|
+
*/
|
|
32
49
|
export type InputData = {
|
|
33
50
|
/**
|
|
34
|
-
*
|
|
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
|
+
* ```
|
|
35
58
|
*/
|
|
36
59
|
data: string | unknown
|
|
37
60
|
}
|
|
@@ -39,19 +62,18 @@ export type InputData = {
|
|
|
39
62
|
type Input = InputPath | InputData
|
|
40
63
|
|
|
41
64
|
/**
|
|
42
|
-
*
|
|
43
|
-
* Mirrors the
|
|
65
|
+
* Source data passed to an adapter's `parse` function.
|
|
66
|
+
* Mirrors the config input shape with paths resolved to absolute.
|
|
44
67
|
*/
|
|
45
68
|
export type AdapterSource = { type: 'path'; path: string } | { type: 'data'; data: string | unknown } | { type: 'paths'; paths: Array<string> }
|
|
46
69
|
|
|
47
70
|
/**
|
|
48
|
-
*
|
|
71
|
+
* Generic type parameters for an adapter definition.
|
|
49
72
|
*
|
|
50
|
-
*
|
|
51
|
-
* - `
|
|
52
|
-
* - `
|
|
53
|
-
* - `
|
|
54
|
-
* - `TDocument` — type of the raw source document exposed by the adapter after `parse()`
|
|
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
|
|
55
77
|
*/
|
|
56
78
|
export type AdapterFactoryOptions<
|
|
57
79
|
TName extends string = string,
|
|
@@ -66,83 +88,94 @@ export type AdapterFactoryOptions<
|
|
|
66
88
|
}
|
|
67
89
|
|
|
68
90
|
/**
|
|
69
|
-
*
|
|
91
|
+
* Adapter that converts input files or data into an `InputNode`.
|
|
70
92
|
*
|
|
71
|
-
* Adapters
|
|
72
|
-
*
|
|
73
|
-
* 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.
|
|
74
95
|
*
|
|
75
96
|
* @example
|
|
76
97
|
* ```ts
|
|
77
|
-
* import {
|
|
98
|
+
* import { adapterOas } from '@kubb/adapter-oas'
|
|
78
99
|
*
|
|
79
100
|
* export default defineConfig({
|
|
80
|
-
* adapter: adapterOas(),
|
|
81
|
-
* input:
|
|
101
|
+
* adapter: adapterOas(),
|
|
102
|
+
* input: { path: './openapi.yaml' },
|
|
82
103
|
* plugins: [pluginTs(), pluginZod()],
|
|
83
104
|
* })
|
|
84
105
|
* ```
|
|
85
106
|
*/
|
|
86
107
|
export type Adapter<TOptions extends AdapterFactoryOptions = AdapterFactoryOptions> = {
|
|
87
108
|
/**
|
|
88
|
-
* Human-readable identifier
|
|
109
|
+
* Human-readable adapter identifier (e.g. `'oas'`, `'asyncapi'`).
|
|
89
110
|
*/
|
|
90
111
|
name: TOptions['name']
|
|
91
112
|
/**
|
|
92
|
-
* Resolved options
|
|
113
|
+
* Resolved adapter options after defaults have been applied.
|
|
93
114
|
*/
|
|
94
115
|
options: TOptions['resolvedOptions']
|
|
95
116
|
/**
|
|
96
|
-
*
|
|
97
|
-
* `undefined` before parsing; typed by the adapter's `TDocument` generic.
|
|
117
|
+
* Parsed source document after the first `parse()` call. `null` before parsing.
|
|
98
118
|
*/
|
|
99
119
|
document: TOptions['document'] | null
|
|
100
120
|
inputNode: InputNode | null
|
|
101
121
|
/**
|
|
102
|
-
*
|
|
122
|
+
* Parse the source into a universal `InputNode`.
|
|
103
123
|
*/
|
|
104
124
|
parse: (source: AdapterSource) => PossiblePromise<InputNode>
|
|
105
125
|
/**
|
|
106
|
-
*
|
|
107
|
-
*
|
|
126
|
+
* Extract `ImportNode` entries for a schema tree.
|
|
127
|
+
* Returns an empty array before the first `parse()` call.
|
|
108
128
|
*
|
|
109
129
|
* The `resolve` callback receives the collision-corrected schema name and must
|
|
110
|
-
* return
|
|
130
|
+
* return `{ name, path }` for the import, or `undefined` to skip it.
|
|
111
131
|
*/
|
|
112
132
|
getImports: (node: SchemaNode, resolve: (schemaName: string) => { name: string; path: string }) => Array<ImportNode>
|
|
113
133
|
}
|
|
114
134
|
|
|
115
135
|
export type DevtoolsOptions = {
|
|
116
136
|
/**
|
|
117
|
-
* Open the AST inspector
|
|
118
|
-
* When `false`, opens the main Studio page instead.
|
|
137
|
+
* Open the AST inspector in Kubb Studio (`/ast`). Defaults to the main Studio page.
|
|
119
138
|
* @default false
|
|
120
139
|
*/
|
|
121
140
|
ast?: boolean
|
|
122
141
|
}
|
|
123
142
|
|
|
124
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
|
+
*
|
|
125
154
|
* @private
|
|
126
155
|
*/
|
|
127
156
|
export type Config<TInput = Input> = {
|
|
128
157
|
/**
|
|
129
|
-
*
|
|
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
|
+
* ```
|
|
130
165
|
*/
|
|
131
166
|
name?: string
|
|
132
167
|
/**
|
|
133
|
-
*
|
|
168
|
+
* Project root directory, absolute or relative to the config file.
|
|
134
169
|
* @default process.cwd()
|
|
135
170
|
*/
|
|
136
171
|
root: string
|
|
137
172
|
/**
|
|
138
|
-
*
|
|
139
|
-
* Each parser handles specific
|
|
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`.
|
|
140
177
|
*
|
|
141
|
-
*
|
|
142
|
-
*
|
|
143
|
-
* When omitted, `parserTs` from `@kubb/parser-ts` is used automatically as the
|
|
144
|
-
* default (requires `@kubb/parser-ts` to be installed as an optional dependency).
|
|
145
|
-
* @default [parserTs] — from `@kubb/parser-ts`
|
|
178
|
+
* @default [parserTs] from `@kubb/parser-ts`
|
|
146
179
|
* @example
|
|
147
180
|
* ```ts
|
|
148
181
|
* import { parserTs, tsxParser } from '@kubb/parser-ts'
|
|
@@ -153,125 +186,197 @@ export type Config<TInput = Input> = {
|
|
|
153
186
|
*/
|
|
154
187
|
parsers: Array<Parser>
|
|
155
188
|
/**
|
|
156
|
-
* Adapter that
|
|
157
|
-
*
|
|
158
|
-
*
|
|
159
|
-
* - Use `@kubb/adapter-oas` for OpenAPI / Swagger.
|
|
160
|
-
* - Use `@kubb/adapter-drizzle` or `@kubb/adapter-asyncapi` for other formats.
|
|
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.
|
|
161
191
|
*
|
|
162
192
|
* @example
|
|
163
193
|
* ```ts
|
|
164
194
|
* import { adapterOas } from '@kubb/adapter-oas'
|
|
165
195
|
* export default defineConfig({
|
|
166
196
|
* adapter: adapterOas(),
|
|
167
|
-
* input: { path: './
|
|
197
|
+
* input: { path: './petstore.yaml' },
|
|
168
198
|
* })
|
|
169
199
|
* ```
|
|
170
200
|
*/
|
|
171
201
|
adapter: Adapter
|
|
172
202
|
/**
|
|
173
203
|
* Source file or data to generate code from.
|
|
174
|
-
* Use `input.path` for a file
|
|
204
|
+
* Use `input.path` for a file path or `input.data` for inline data.
|
|
175
205
|
*/
|
|
176
206
|
input: TInput
|
|
177
207
|
output: {
|
|
178
208
|
/**
|
|
179
|
-
* Output directory for generated files
|
|
180
|
-
*
|
|
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
|
+
* ```
|
|
181
220
|
*/
|
|
182
221
|
path: string
|
|
183
222
|
/**
|
|
184
|
-
*
|
|
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
|
+
* ```
|
|
185
233
|
*/
|
|
186
234
|
clean?: boolean
|
|
187
235
|
/**
|
|
188
|
-
*
|
|
236
|
+
* Persists generated files to the file system.
|
|
237
|
+
*
|
|
189
238
|
* @default true
|
|
190
|
-
* @deprecated Use `storage` to control where files are written.
|
|
239
|
+
* @deprecated Use `storage` option to control where files are written instead.
|
|
191
240
|
*/
|
|
192
241
|
write?: boolean
|
|
193
242
|
/**
|
|
194
|
-
*
|
|
195
|
-
*
|
|
196
|
-
*
|
|
197
|
-
*
|
|
198
|
-
*
|
|
199
|
-
* - false disables code formatting.
|
|
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
|
+
*
|
|
200
248
|
* @default false
|
|
249
|
+
* @example
|
|
250
|
+
* ```ts
|
|
251
|
+
* format: 'auto' // auto-detect prettier, biome, or oxfmt
|
|
252
|
+
* format: 'prettier' // force prettier
|
|
253
|
+
* format: false // skip formatting
|
|
254
|
+
* ```
|
|
201
255
|
*/
|
|
202
256
|
format?: 'auto' | 'prettier' | 'biome' | 'oxfmt' | false
|
|
203
257
|
/**
|
|
204
|
-
*
|
|
205
|
-
*
|
|
206
|
-
*
|
|
207
|
-
*
|
|
208
|
-
*
|
|
209
|
-
* - false disables linting.
|
|
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
|
+
*
|
|
210
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
|
+
* ```
|
|
211
270
|
*/
|
|
212
271
|
lint?: 'auto' | 'eslint' | 'biome' | 'oxlint' | false
|
|
213
272
|
/**
|
|
214
|
-
*
|
|
215
|
-
*
|
|
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
|
+
* ```
|
|
216
284
|
*/
|
|
217
285
|
extension?: Record<FileNode['extname'], FileNode['extname'] | ''>
|
|
218
286
|
/**
|
|
219
|
-
*
|
|
220
|
-
*
|
|
221
|
-
* -
|
|
222
|
-
*
|
|
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
|
+
*
|
|
223
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
|
+
* ```
|
|
224
299
|
*/
|
|
225
300
|
defaultBanner?: 'simple' | 'full' | false
|
|
226
301
|
/**
|
|
227
|
-
*
|
|
228
|
-
*
|
|
229
|
-
*
|
|
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
|
+
*
|
|
230
307
|
* @default false
|
|
308
|
+
* @example
|
|
309
|
+
* ```ts
|
|
310
|
+
* override: true // regenerate everything, even existing files
|
|
311
|
+
* override: false // skip files that already exist
|
|
312
|
+
* ```
|
|
231
313
|
*/
|
|
232
314
|
override?: boolean
|
|
233
315
|
} & ExtractRegistryKey<Kubb.ConfigOptionsRegistry, 'output'>
|
|
234
316
|
/**
|
|
235
|
-
* Storage backend
|
|
236
|
-
*
|
|
237
|
-
*
|
|
238
|
-
*
|
|
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
|
+
*
|
|
239
322
|
* @default fsStorage()
|
|
240
323
|
* @example
|
|
241
324
|
* ```ts
|
|
242
325
|
* import { memoryStorage } from '@kubb/core'
|
|
326
|
+
*
|
|
327
|
+
* // Keep generated files in memory (useful for testing, CI pipelines)
|
|
243
328
|
* storage: memoryStorage()
|
|
329
|
+
*
|
|
330
|
+
* // Use custom S3 storage
|
|
331
|
+
* storage: myS3Storage()
|
|
244
332
|
* ```
|
|
333
|
+
*
|
|
334
|
+
* @see {@link Storage} interface for implementing custom backends.
|
|
245
335
|
*/
|
|
246
336
|
storage?: Storage
|
|
247
337
|
/**
|
|
248
|
-
*
|
|
249
|
-
*
|
|
250
|
-
*
|
|
251
|
-
*
|
|
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
|
+
* ```
|
|
252
356
|
*/
|
|
253
357
|
plugins: Array<Plugin>
|
|
254
358
|
/**
|
|
255
|
-
* Middleware instances that observe and post-process
|
|
256
|
-
*
|
|
257
|
-
* Middleware
|
|
258
|
-
*
|
|
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
|
|
259
366
|
*
|
|
260
367
|
* @example
|
|
261
368
|
* ```ts
|
|
262
369
|
* import { middlewareBarrel } from '@kubb/middleware-barrel'
|
|
263
|
-
*
|
|
264
|
-
*
|
|
265
|
-
* plugins: [pluginTs(), pluginZod()],
|
|
266
|
-
* })
|
|
370
|
+
*
|
|
371
|
+
* middleware: [middlewareBarrel()]
|
|
267
372
|
* ```
|
|
373
|
+
*
|
|
374
|
+
* @see {@link defineMiddleware} to create custom middleware.
|
|
268
375
|
*/
|
|
269
376
|
middleware?: Array<Middleware>
|
|
270
377
|
/**
|
|
271
|
-
*
|
|
272
|
-
* `renderer`
|
|
273
|
-
*
|
|
274
|
-
* The resolution chain is: `generator.renderer` → `plugin.renderer` → `config.renderer` → `undefined` (raw `FileNode[]` mode).
|
|
378
|
+
* Default renderer factory used by all plugins and generators.
|
|
379
|
+
* Resolution chain: `generator.renderer` → `plugin.renderer` → `config.renderer` → `undefined` (raw FileNode[] mode).
|
|
275
380
|
*
|
|
276
381
|
* @example
|
|
277
382
|
* ```ts
|
|
@@ -282,9 +387,34 @@ export type Config<TInput = Input> = {
|
|
|
282
387
|
* })
|
|
283
388
|
* ```
|
|
284
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
|
+
*/
|
|
285
405
|
renderer?: RendererFactory
|
|
286
406
|
/**
|
|
287
|
-
*
|
|
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
|
+
* ```
|
|
288
418
|
*/
|
|
289
419
|
devtools?:
|
|
290
420
|
| true
|
|
@@ -296,12 +426,33 @@ export type Config<TInput = Input> = {
|
|
|
296
426
|
studioUrl?: typeof DEFAULT_STUDIO_URL | (string & {})
|
|
297
427
|
}
|
|
298
428
|
/**
|
|
299
|
-
*
|
|
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
|
+
* ```
|
|
300
442
|
*/
|
|
301
443
|
hooks?: {
|
|
302
444
|
/**
|
|
303
|
-
*
|
|
304
|
-
*
|
|
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
|
+
* ```
|
|
305
456
|
*/
|
|
306
457
|
done?: string | Array<string>
|
|
307
458
|
}
|
|
@@ -310,7 +461,7 @@ export type Config<TInput = Input> = {
|
|
|
310
461
|
// plugin
|
|
311
462
|
|
|
312
463
|
/**
|
|
313
|
-
*
|
|
464
|
+
* Type/string pattern filter for include/exclude/override matching.
|
|
314
465
|
*/
|
|
315
466
|
type PatternFilter = {
|
|
316
467
|
type: string
|
|
@@ -318,18 +469,14 @@ type PatternFilter = {
|
|
|
318
469
|
}
|
|
319
470
|
|
|
320
471
|
/**
|
|
321
|
-
*
|
|
472
|
+
* Pattern filter with partial option overrides applied when the pattern matches.
|
|
322
473
|
*/
|
|
323
474
|
type PatternOverride<TOptions> = PatternFilter & {
|
|
324
475
|
options: Omit<Partial<TOptions>, 'override'>
|
|
325
476
|
}
|
|
326
477
|
|
|
327
478
|
/**
|
|
328
|
-
* Context
|
|
329
|
-
* for a given operation or schema node.
|
|
330
|
-
*/
|
|
331
|
-
/**
|
|
332
|
-
* Resolves filtered options for a given operation or schema node.
|
|
479
|
+
* Context for resolving filtered options for a given operation or schema node.
|
|
333
480
|
*
|
|
334
481
|
* @internal
|
|
335
482
|
*/
|
|
@@ -343,9 +490,8 @@ export type ResolveOptionsContext<TOptions> = {
|
|
|
343
490
|
/**
|
|
344
491
|
* Base constraint for all plugin resolver objects.
|
|
345
492
|
*
|
|
346
|
-
* `default`, `resolveOptions`, `resolvePath`,
|
|
347
|
-
* by `defineResolver` —
|
|
348
|
-
* from scratch.
|
|
493
|
+
* `default`, `resolveOptions`, `resolvePath`, `resolveFile`, `resolveBanner`, and `resolveFooter`
|
|
494
|
+
* are injected automatically by `defineResolver` — extend this type to add custom resolution methods.
|
|
349
495
|
*
|
|
350
496
|
* @example
|
|
351
497
|
* ```ts
|
|
@@ -368,20 +514,20 @@ export type Resolver = {
|
|
|
368
514
|
|
|
369
515
|
export type PluginFactoryOptions<
|
|
370
516
|
/**
|
|
371
|
-
*
|
|
517
|
+
* Unique plugin name.
|
|
372
518
|
*/
|
|
373
519
|
TName extends string = string,
|
|
374
520
|
/**
|
|
375
|
-
*
|
|
521
|
+
* User-facing plugin options.
|
|
376
522
|
*/
|
|
377
523
|
TOptions extends object = object,
|
|
378
524
|
/**
|
|
379
|
-
*
|
|
525
|
+
* Plugin options after defaults are applied.
|
|
380
526
|
*/
|
|
381
527
|
TResolvedOptions extends object = TOptions,
|
|
382
528
|
/**
|
|
383
|
-
* Resolver
|
|
384
|
-
*
|
|
529
|
+
* Resolver that encapsulates naming and path-resolution helpers.
|
|
530
|
+
* Define with `defineResolver` and export alongside the plugin.
|
|
385
531
|
*/
|
|
386
532
|
TResolver extends Resolver = Resolver,
|
|
387
533
|
> = {
|
|
@@ -392,9 +538,9 @@ export type PluginFactoryOptions<
|
|
|
392
538
|
}
|
|
393
539
|
|
|
394
540
|
/**
|
|
395
|
-
*
|
|
396
|
-
*
|
|
397
|
-
*
|
|
541
|
+
* Normalized plugin after setup, with runtime fields populated.
|
|
542
|
+
* For internal use only — plugins use the public `Plugin` type externally.
|
|
543
|
+
*
|
|
398
544
|
* @internal
|
|
399
545
|
*/
|
|
400
546
|
export type NormalizedPlugin<TOptions extends PluginFactoryOptions = PluginFactoryOptions> = Plugin<TOptions> & {
|
|
@@ -413,30 +559,86 @@ export type NormalizedPlugin<TOptions extends PluginFactoryOptions = PluginFacto
|
|
|
413
559
|
}
|
|
414
560
|
|
|
415
561
|
/**
|
|
416
|
-
* Partial
|
|
562
|
+
* Partial `Config` for user-facing entry points with sensible defaults.
|
|
417
563
|
*
|
|
418
|
-
*
|
|
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
|
+
* ```
|
|
419
576
|
*/
|
|
420
577
|
export type UserConfig<TInput = Input> = Omit<Config<TInput>, 'root' | 'plugins' | 'parsers' | 'adapter'> & {
|
|
421
578
|
/**
|
|
422
|
-
*
|
|
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
|
+
*
|
|
423
583
|
* @default process.cwd()
|
|
584
|
+
* @example
|
|
585
|
+
* ```ts
|
|
586
|
+
* root: '/home/user/my-project'
|
|
587
|
+
* root: './my-project' // relative to config file
|
|
588
|
+
* ```
|
|
424
589
|
*/
|
|
425
590
|
root?: string
|
|
426
591
|
/**
|
|
427
|
-
*
|
|
428
|
-
*
|
|
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
|
+
* ```
|
|
429
605
|
*
|
|
430
|
-
*
|
|
606
|
+
* @see {@link Parser} to implement a custom parser.
|
|
431
607
|
*/
|
|
432
608
|
parsers?: Array<Parser>
|
|
433
609
|
/**
|
|
434
|
-
* Adapter that
|
|
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.
|
|
435
624
|
*/
|
|
436
625
|
adapter?: Adapter
|
|
437
626
|
/**
|
|
438
|
-
*
|
|
439
|
-
*
|
|
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.
|
|
440
642
|
*/
|
|
441
643
|
plugins?: Array<Plugin>
|
|
442
644
|
}
|
|
@@ -445,121 +647,114 @@ export type ResolveNameParams = {
|
|
|
445
647
|
name: string
|
|
446
648
|
pluginName?: string
|
|
447
649
|
/**
|
|
448
|
-
*
|
|
449
|
-
* - `'file'` —
|
|
450
|
-
* - `'function'` —
|
|
451
|
-
* - `'type'` —
|
|
452
|
-
* - `'const'` —
|
|
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)
|
|
453
655
|
*/
|
|
454
656
|
type?: 'file' | 'function' | 'type' | 'const'
|
|
455
657
|
}
|
|
456
658
|
/**
|
|
457
|
-
* Context object passed
|
|
458
|
-
* `operations` methods.
|
|
659
|
+
* Context object passed to generator `schema`, `operation`, and `operations` methods.
|
|
459
660
|
*
|
|
460
|
-
*
|
|
461
|
-
*
|
|
462
|
-
*
|
|
463
|
-
*
|
|
464
|
-
* `ctx.options` carries the per-node resolved options for `schema`/`operation` calls
|
|
465
|
-
* (after exclude/include/override filtering) and the plugin-level options for `operations`.
|
|
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.
|
|
466
664
|
*/
|
|
467
665
|
export type GeneratorContext<TOptions extends PluginFactoryOptions = PluginFactoryOptions> = {
|
|
468
666
|
config: Config
|
|
469
667
|
/**
|
|
470
|
-
* Absolute path to the output directory
|
|
471
|
-
* Shorthand for `path.resolve(config.root, config.output.path)`.
|
|
668
|
+
* Absolute path to the current plugin's output directory.
|
|
472
669
|
*/
|
|
473
670
|
root: string
|
|
474
671
|
/**
|
|
475
|
-
*
|
|
476
|
-
* Returns `'single'` when `output.path`
|
|
672
|
+
* Determine output mode based on the output config.
|
|
673
|
+
* Returns `'single'` when `output.path` is a file, `'split'` for a directory.
|
|
477
674
|
*/
|
|
478
675
|
getMode: (output: { path: string }) => 'single' | 'split'
|
|
479
676
|
driver: PluginDriver
|
|
480
677
|
/**
|
|
481
|
-
* Get a plugin by name
|
|
482
|
-
* the name is a registered key, otherwise returns the generic `Plugin`.
|
|
678
|
+
* Get a plugin by name, typed via `Kubb.PluginRegistry` when registered.
|
|
483
679
|
*/
|
|
484
680
|
getPlugin<TName extends keyof Kubb.PluginRegistry>(name: TName): Plugin<Kubb.PluginRegistry[TName]> | undefined
|
|
485
681
|
getPlugin(name: string): Plugin | undefined
|
|
486
682
|
/**
|
|
487
|
-
*
|
|
683
|
+
* Get a plugin by name, throws an error if not found.
|
|
488
684
|
*/
|
|
489
685
|
requirePlugin<TName extends keyof Kubb.PluginRegistry>(name: TName): Plugin<Kubb.PluginRegistry[TName]>
|
|
490
686
|
requirePlugin(name: string): Plugin
|
|
491
687
|
/**
|
|
492
|
-
* Get a resolver by plugin name
|
|
493
|
-
* the name is a registered key, otherwise returns the generic `Resolver`.
|
|
688
|
+
* Get a resolver by plugin name, typed via `Kubb.PluginRegistry` when registered.
|
|
494
689
|
*/
|
|
495
690
|
getResolver<TName extends keyof Kubb.PluginRegistry>(name: TName): Kubb.PluginRegistry[TName]['resolver']
|
|
496
691
|
getResolver(name: string): Resolver
|
|
497
692
|
/**
|
|
498
|
-
* Add files only
|
|
693
|
+
* Add files only if they don't exist.
|
|
499
694
|
*/
|
|
500
695
|
addFile: (...file: Array<FileNode>) => Promise<void>
|
|
501
696
|
/**
|
|
502
|
-
* Merge
|
|
697
|
+
* Merge sources into the same output file.
|
|
503
698
|
*/
|
|
504
699
|
upsertFile: (...file: Array<FileNode>) => Promise<void>
|
|
505
700
|
hooks: AsyncEventEmitter<KubbHooks>
|
|
506
701
|
/**
|
|
507
|
-
* The current plugin.
|
|
702
|
+
* The current plugin instance.
|
|
508
703
|
*/
|
|
509
704
|
plugin: Plugin<TOptions>
|
|
510
705
|
/**
|
|
511
|
-
*
|
|
706
|
+
* The current plugin's resolver.
|
|
512
707
|
*/
|
|
513
708
|
resolver: TOptions['resolver']
|
|
514
709
|
/**
|
|
515
|
-
*
|
|
710
|
+
* The current plugin's transformer.
|
|
516
711
|
*/
|
|
517
712
|
transformer: Visitor | undefined
|
|
518
713
|
/**
|
|
519
|
-
* Emit a warning
|
|
714
|
+
* Emit a warning.
|
|
520
715
|
*/
|
|
521
716
|
warn: (message: string) => void
|
|
522
717
|
/**
|
|
523
|
-
* Emit an error
|
|
718
|
+
* Emit an error.
|
|
524
719
|
*/
|
|
525
720
|
error: (error: string | Error) => void
|
|
526
721
|
/**
|
|
527
|
-
* Emit an info message
|
|
722
|
+
* Emit an info message.
|
|
528
723
|
*/
|
|
529
724
|
info: (message: string) => void
|
|
530
725
|
/**
|
|
531
|
-
*
|
|
726
|
+
* Open the current input node in Kubb Studio.
|
|
532
727
|
*/
|
|
533
728
|
openInStudio: (options?: DevtoolsOptions) => Promise<void>
|
|
534
729
|
/**
|
|
535
|
-
* The adapter
|
|
730
|
+
* The configured adapter instance.
|
|
536
731
|
*/
|
|
537
732
|
adapter: Adapter
|
|
538
733
|
/**
|
|
539
|
-
* The universal
|
|
734
|
+
* The universal `InputNode` produced by the adapter.
|
|
540
735
|
*/
|
|
541
736
|
inputNode: InputNode
|
|
542
737
|
/**
|
|
543
|
-
*
|
|
738
|
+
* Resolved options after exclude/include/override filtering.
|
|
544
739
|
*/
|
|
545
740
|
options: TOptions['resolvedOptions']
|
|
546
741
|
}
|
|
547
742
|
/**
|
|
548
|
-
*
|
|
743
|
+
* Output configuration for generated files.
|
|
549
744
|
*/
|
|
550
745
|
export type Output<_TOptions = unknown> = {
|
|
551
746
|
/**
|
|
552
|
-
*
|
|
747
|
+
* Output folder or file path for generated code.
|
|
553
748
|
*/
|
|
554
749
|
path: string
|
|
555
750
|
/**
|
|
556
|
-
* Text or function
|
|
557
|
-
* When a function, receives the current `InputNode` and
|
|
751
|
+
* Text or function prepended to every generated file.
|
|
752
|
+
* When a function, receives the current `InputNode` and returns a string.
|
|
558
753
|
*/
|
|
559
754
|
banner?: string | ((node?: InputNode) => string)
|
|
560
755
|
/**
|
|
561
|
-
* Text or function appended
|
|
562
|
-
* When a function, receives the current `InputNode` and
|
|
756
|
+
* Text or function appended to every generated file.
|
|
757
|
+
* When a function, receives the current `InputNode` and returns a string.
|
|
563
758
|
*/
|
|
564
759
|
footer?: string | ((node?: InputNode) => string)
|
|
565
760
|
/**
|
|
@@ -571,27 +766,28 @@ export type Output<_TOptions = unknown> = {
|
|
|
571
766
|
|
|
572
767
|
export type Group = {
|
|
573
768
|
/**
|
|
574
|
-
*
|
|
575
|
-
* - `'tag'`
|
|
576
|
-
* - `'path'`
|
|
769
|
+
* How to group files into subdirectories.
|
|
770
|
+
* - `'tag'` — group by OpenAPI tags
|
|
771
|
+
* - `'path'` — group by OpenAPI paths
|
|
577
772
|
*/
|
|
578
773
|
type: 'tag' | 'path'
|
|
579
774
|
/**
|
|
580
|
-
*
|
|
581
|
-
* Defaults to `${camelCase(group)}Controller` for tags
|
|
775
|
+
* Function that returns the subdirectory name for a group value.
|
|
776
|
+
* Defaults to `${camelCase(group)}Controller` for tags, first path segment for paths.
|
|
582
777
|
*/
|
|
583
778
|
name?: (context: { group: string }) => string
|
|
584
779
|
}
|
|
585
780
|
|
|
586
781
|
export type LoggerOptions = {
|
|
587
782
|
/**
|
|
783
|
+
* Log level for output verbosity.
|
|
588
784
|
* @default 3
|
|
589
785
|
*/
|
|
590
786
|
logLevel: (typeof logLevel)[keyof typeof logLevel]
|
|
591
787
|
}
|
|
592
788
|
|
|
593
789
|
/**
|
|
594
|
-
* Shared context passed to
|
|
790
|
+
* Shared context passed to plugins, parsers, and other internals.
|
|
595
791
|
*/
|
|
596
792
|
export type LoggerContext = AsyncEventEmitter<KubbHooks>
|
|
597
793
|
|
|
@@ -609,44 +805,35 @@ export type { Plugin } from './definePlugin.ts'
|
|
|
609
805
|
export type { Kubb, KubbHooks } from './Kubb.ts'
|
|
610
806
|
|
|
611
807
|
/**
|
|
612
|
-
* Context
|
|
613
|
-
* Provides methods to register generators, configure
|
|
614
|
-
* and renderer, as well as access to the current build configuration.
|
|
808
|
+
* Context for hook-style plugin `kubb:plugin:setup` handler.
|
|
809
|
+
* Provides methods to register generators, configure resolvers, transformers, and renderers.
|
|
615
810
|
*/
|
|
616
811
|
export type KubbPluginSetupContext<TFactory extends PluginFactoryOptions = PluginFactoryOptions> = {
|
|
617
812
|
/**
|
|
618
|
-
* Register a generator
|
|
619
|
-
*
|
|
813
|
+
* Register a generator dynamically. Generators fire during the AST walk (schema/operation/operations)
|
|
814
|
+
* just like generators declared statically on `createPlugin`.
|
|
620
815
|
*/
|
|
621
816
|
addGenerator<TElement = unknown>(generator: Generator<TFactory, TElement>): void
|
|
622
817
|
/**
|
|
623
|
-
* Set or
|
|
624
|
-
* The resolver controls file naming and path resolution
|
|
625
|
-
*
|
|
626
|
-
* When `TFactory` is a concrete `PluginFactoryOptions` (e.g. `PluginClient`),
|
|
627
|
-
* the resolver parameter is typed to the plugin's own resolver type (e.g. `ResolverClient`).
|
|
818
|
+
* Set or override the resolver for this plugin.
|
|
819
|
+
* The resolver controls file naming and path resolution.
|
|
628
820
|
*/
|
|
629
821
|
setResolver(resolver: Partial<TFactory['resolver']>): void
|
|
630
822
|
/**
|
|
631
|
-
* Set the AST transformer
|
|
632
|
-
* The transformer pre-processes nodes before they reach the generators.
|
|
823
|
+
* Set the AST transformer to pre-process nodes before they reach generators.
|
|
633
824
|
*/
|
|
634
825
|
setTransformer(visitor: Visitor): void
|
|
635
826
|
/**
|
|
636
|
-
* Set the renderer factory
|
|
637
|
-
* Used to process JSX elements returned by generators.
|
|
827
|
+
* Set the renderer factory to process JSX elements from generators.
|
|
638
828
|
*/
|
|
639
829
|
setRenderer(renderer: RendererFactory): void
|
|
640
830
|
/**
|
|
641
|
-
* Set
|
|
642
|
-
*
|
|
643
|
-
*
|
|
644
|
-
* Call this in `kubb:plugin:setup` to provide the resolved options that generators
|
|
645
|
-
* and the build loop need (e.g., `enumType`, `optionalType`, `group`).
|
|
831
|
+
* Set resolved options merged into the normalized plugin's `options`.
|
|
832
|
+
* Call this in `kubb:plugin:setup` to provide options generators need.
|
|
646
833
|
*/
|
|
647
834
|
setOptions(options: TFactory['resolvedOptions']): void
|
|
648
835
|
/**
|
|
649
|
-
* Inject a raw file into the build output, bypassing the
|
|
836
|
+
* Inject a raw file into the build output, bypassing the generation pipeline.
|
|
650
837
|
*/
|
|
651
838
|
injectFile(userFileNode: UserFileNode): void
|
|
652
839
|
/**
|
|
@@ -654,17 +841,17 @@ export type KubbPluginSetupContext<TFactory extends PluginFactoryOptions = Plugi
|
|
|
654
841
|
*/
|
|
655
842
|
updateConfig(config: Partial<Config>): void
|
|
656
843
|
/**
|
|
657
|
-
* The resolved build configuration at
|
|
844
|
+
* The resolved build configuration at setup time.
|
|
658
845
|
*/
|
|
659
846
|
config: Config
|
|
660
847
|
/**
|
|
661
|
-
* The plugin's
|
|
848
|
+
* The plugin's user-provided options.
|
|
662
849
|
*/
|
|
663
850
|
options: TFactory['options']
|
|
664
851
|
}
|
|
665
852
|
|
|
666
853
|
/**
|
|
667
|
-
* Context
|
|
854
|
+
* Context for hook-style plugin `kubb:build:start` handler.
|
|
668
855
|
* Fires immediately before the plugin execution loop begins.
|
|
669
856
|
*/
|
|
670
857
|
export type KubbBuildStartContext = {
|
|
@@ -672,15 +859,13 @@ export type KubbBuildStartContext = {
|
|
|
672
859
|
adapter: Adapter
|
|
673
860
|
inputNode: InputNode
|
|
674
861
|
/**
|
|
675
|
-
* Get a plugin by name
|
|
676
|
-
* the name is a registered key, otherwise returns the generic `Plugin`.
|
|
862
|
+
* Get a plugin by name, typed via `Kubb.PluginRegistry` when registered.
|
|
677
863
|
*/
|
|
678
864
|
getPlugin<TName extends keyof Kubb.PluginRegistry>(name: TName): Plugin<Kubb.PluginRegistry[TName]> | undefined
|
|
679
865
|
getPlugin(name: string): Plugin | undefined
|
|
680
866
|
/**
|
|
681
|
-
*
|
|
682
|
-
* Call this lazily (e.g.
|
|
683
|
-
* that have already run.
|
|
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.
|
|
684
869
|
*/
|
|
685
870
|
readonly files: ReadonlyArray<FileNode>
|
|
686
871
|
/**
|
|
@@ -692,27 +877,25 @@ export type KubbBuildStartContext = {
|
|
|
692
877
|
}
|
|
693
878
|
|
|
694
879
|
/**
|
|
695
|
-
* Context
|
|
696
|
-
* Fires after
|
|
697
|
-
*
|
|
698
|
-
* Middleware that needs to inject final files (e.g. a root barrel) should use this event.
|
|
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.
|
|
699
883
|
*/
|
|
700
884
|
export type KubbPluginsEndContext = {
|
|
701
885
|
config: Config
|
|
702
886
|
/**
|
|
703
|
-
*
|
|
704
|
-
* Includes files added by plugins and per-plugin barrel middleware.
|
|
887
|
+
* All files currently in the file manager (lazy snapshot).
|
|
705
888
|
*/
|
|
706
889
|
readonly files: ReadonlyArray<FileNode>
|
|
707
890
|
/**
|
|
708
|
-
* Upsert
|
|
709
|
-
* Files added here
|
|
891
|
+
* Upsert files into the file manager.
|
|
892
|
+
* Files added here are included in the write pass.
|
|
710
893
|
*/
|
|
711
894
|
upsertFile: (...files: Array<FileNode>) => void
|
|
712
895
|
}
|
|
713
896
|
|
|
714
897
|
/**
|
|
715
|
-
* Context
|
|
898
|
+
* Context for hook-style plugin `kubb:build:end` handler.
|
|
716
899
|
* Fires after all files have been written to disk.
|
|
717
900
|
*/
|
|
718
901
|
export type KubbBuildEndContext = {
|
|
@@ -785,11 +968,11 @@ export type KubbFilesProcessingStartContext = {
|
|
|
785
968
|
|
|
786
969
|
export type KubbFileProcessingUpdateContext = {
|
|
787
970
|
/**
|
|
788
|
-
* Number of files processed
|
|
971
|
+
* Number of files processed.
|
|
789
972
|
*/
|
|
790
973
|
processed: number
|
|
791
974
|
/**
|
|
792
|
-
* Total
|
|
975
|
+
* Total files to process.
|
|
793
976
|
*/
|
|
794
977
|
total: number
|
|
795
978
|
/**
|
|
@@ -805,8 +988,7 @@ export type KubbFileProcessingUpdateContext = {
|
|
|
805
988
|
*/
|
|
806
989
|
file: FileNode
|
|
807
990
|
/**
|
|
808
|
-
*
|
|
809
|
-
* Provides access to the current config during file processing.
|
|
991
|
+
* The current build configuration.
|
|
810
992
|
*/
|
|
811
993
|
config: Config
|
|
812
994
|
}
|
|
@@ -851,22 +1033,46 @@ export type KubbHookEndContext = {
|
|
|
851
1033
|
}
|
|
852
1034
|
|
|
853
1035
|
type ByTag = {
|
|
1036
|
+
/**
|
|
1037
|
+
* Filter by OpenAPI `tags` field. Matches one or more tags assigned to operations.
|
|
1038
|
+
*/
|
|
854
1039
|
type: 'tag'
|
|
1040
|
+
/**
|
|
1041
|
+
* Tag name to match (case-sensitive). Can be a literal string or regex pattern.
|
|
1042
|
+
*/
|
|
855
1043
|
pattern: string | RegExp
|
|
856
1044
|
}
|
|
857
1045
|
|
|
858
1046
|
type ByOperationId = {
|
|
1047
|
+
/**
|
|
1048
|
+
* Filter by OpenAPI `operationId` field. Each operation (GET, POST, etc.) has a unique identifier.
|
|
1049
|
+
*/
|
|
859
1050
|
type: 'operationId'
|
|
1051
|
+
/**
|
|
1052
|
+
* Operation ID to match (case-sensitive). Can be a literal string or regex pattern.
|
|
1053
|
+
*/
|
|
860
1054
|
pattern: string | RegExp
|
|
861
1055
|
}
|
|
862
1056
|
|
|
863
1057
|
type ByPath = {
|
|
1058
|
+
/**
|
|
1059
|
+
* Filter by OpenAPI `path` (URL endpoint). Useful to group or filter by service segments like `/pets`, `/users`, etc.
|
|
1060
|
+
*/
|
|
864
1061
|
type: 'path'
|
|
1062
|
+
/**
|
|
1063
|
+
* URL path to match (case-sensitive). Can be a literal string or regex pattern. Matches against the full path.
|
|
1064
|
+
*/
|
|
865
1065
|
pattern: string | RegExp
|
|
866
1066
|
}
|
|
867
1067
|
|
|
868
1068
|
type ByMethod = {
|
|
1069
|
+
/**
|
|
1070
|
+
* Filter by HTTP method: `'get'`, `'post'`, `'put'`, `'delete'`, `'patch'`, `'head'`, `'options'`.
|
|
1071
|
+
*/
|
|
869
1072
|
type: 'method'
|
|
1073
|
+
/**
|
|
1074
|
+
* HTTP method to match (case-insensitive when using string, or regex for dynamic matching).
|
|
1075
|
+
*/
|
|
870
1076
|
pattern: HttpMethod | RegExp
|
|
871
1077
|
}
|
|
872
1078
|
// TODO implement as alternative for ByMethod
|
|
@@ -876,30 +1082,82 @@ type ByMethod = {
|
|
|
876
1082
|
// }
|
|
877
1083
|
|
|
878
1084
|
type BySchemaName = {
|
|
1085
|
+
/**
|
|
1086
|
+
* Filter by schema component name (TypeScript or JSON schema). Matches schemas in `#/components/schemas`.
|
|
1087
|
+
*/
|
|
879
1088
|
type: 'schemaName'
|
|
1089
|
+
/**
|
|
1090
|
+
* Schema name to match (case-sensitive). Can be a literal string or regex pattern.
|
|
1091
|
+
*/
|
|
880
1092
|
pattern: string | RegExp
|
|
881
1093
|
}
|
|
882
1094
|
|
|
883
1095
|
type ByContentType = {
|
|
1096
|
+
/**
|
|
1097
|
+
* Filter by response or request content type: `'application/json'`, `'application/xml'`, etc.
|
|
1098
|
+
*/
|
|
884
1099
|
type: 'contentType'
|
|
1100
|
+
/**
|
|
1101
|
+
* Content type to match (case-sensitive). Can be a literal string or regex pattern.
|
|
1102
|
+
*/
|
|
885
1103
|
pattern: string | RegExp
|
|
886
1104
|
}
|
|
887
1105
|
|
|
888
1106
|
/**
|
|
889
1107
|
* A pattern filter that prevents matching nodes from being generated.
|
|
890
|
-
*
|
|
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
|
+
* ```
|
|
891
1120
|
*/
|
|
892
1121
|
export type Exclude = ByTag | ByOperationId | ByPath | ByMethod | ByContentType | BySchemaName
|
|
893
1122
|
|
|
894
1123
|
/**
|
|
895
1124
|
* A pattern filter that restricts generation to only matching nodes.
|
|
896
|
-
*
|
|
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
|
+
* ```
|
|
897
1136
|
*/
|
|
898
1137
|
export type Include = ByTag | ByOperationId | ByPath | ByMethod | ByContentType | BySchemaName
|
|
899
1138
|
|
|
900
1139
|
/**
|
|
901
1140
|
* A pattern filter paired with partial option overrides applied when the pattern matches.
|
|
902
|
-
*
|
|
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
|
+
* ```
|
|
903
1161
|
*/
|
|
904
1162
|
export type Override<TOptions> = (ByTag | ByOperationId | ByPath | ByMethod | BySchemaName | ByContentType) & {
|
|
905
1163
|
//TODO should be options: Omit<Partial<TOptions>, 'override'>
|