@kubb/core 5.0.0-alpha.72 → 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 +49 -28
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +14 -7
- package/dist/index.js +49 -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-CNhttEMO.d.ts → types-CuNocrbJ.d.ts} +559 -326
- package/package.json +5 -5
- package/src/Kubb.ts +44 -71
- package/src/createAdapter.ts +13 -6
- package/src/createKubb.ts +2 -0
- 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 +474 -216
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kubb/core",
|
|
3
|
-
"version": "5.0.0-alpha.
|
|
3
|
+
"version": "5.0.0-alpha.74",
|
|
4
4
|
"description": "Core functionality for Kubb's plugin-based code generation system, providing the foundation for transforming OpenAPI specifications.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"ast",
|
|
@@ -64,16 +64,16 @@
|
|
|
64
64
|
},
|
|
65
65
|
"dependencies": {
|
|
66
66
|
"fflate": "^0.8.2",
|
|
67
|
-
"tinyexec": "^1.1.
|
|
68
|
-
"@kubb/ast": "5.0.0-alpha.
|
|
67
|
+
"tinyexec": "^1.1.2",
|
|
68
|
+
"@kubb/ast": "5.0.0-alpha.74"
|
|
69
69
|
},
|
|
70
70
|
"devDependencies": {
|
|
71
71
|
"p-limit": "^7.3.0",
|
|
72
72
|
"@internals/utils": "0.0.0",
|
|
73
|
-
"@kubb/renderer-jsx": "5.0.0-alpha.
|
|
73
|
+
"@kubb/renderer-jsx": "5.0.0-alpha.74"
|
|
74
74
|
},
|
|
75
75
|
"peerDependencies": {
|
|
76
|
-
"@kubb/renderer-jsx": "5.0.0-alpha.
|
|
76
|
+
"@kubb/renderer-jsx": "5.0.0-alpha.74"
|
|
77
77
|
},
|
|
78
78
|
"size-limit": [
|
|
79
79
|
{
|
package/src/Kubb.ts
CHANGED
|
@@ -30,49 +30,45 @@ import type {
|
|
|
30
30
|
} from './types'
|
|
31
31
|
|
|
32
32
|
/**
|
|
33
|
-
*
|
|
33
|
+
* Kubb code generation instance returned by {@link createKubb}.
|
|
34
|
+
*
|
|
35
|
+
* Use this when orchestrating multiple builds, inspecting plugin timings, or integrating Kubb into a larger toolchain.
|
|
36
|
+
* For a single one-off build, chain directly: `await createKubb(config).build()`.
|
|
34
37
|
*/
|
|
35
38
|
export type Kubb = {
|
|
36
39
|
/**
|
|
37
|
-
*
|
|
40
|
+
* Shared event emitter for lifecycle and status events. Attach listeners before calling `setup()` or `build()`.
|
|
38
41
|
*/
|
|
39
42
|
readonly hooks: AsyncEventEmitter<KubbHooks>
|
|
40
43
|
/**
|
|
41
|
-
*
|
|
42
|
-
* Populated after a successful `build()` or `safeBuild()` call.
|
|
44
|
+
* Generated source code keyed by absolute file path. Available after `build()` or `safeBuild()` completes.
|
|
43
45
|
*/
|
|
44
46
|
readonly sources: Map<string, string>
|
|
45
47
|
/**
|
|
46
|
-
*
|
|
48
|
+
* Plugin driver managing all plugins. Available after `setup()` completes.
|
|
47
49
|
*/
|
|
48
50
|
readonly driver: PluginDriver | undefined
|
|
49
51
|
/**
|
|
50
|
-
*
|
|
52
|
+
* Resolved configuration with defaults applied. Available after `setup()` completes.
|
|
51
53
|
*/
|
|
52
54
|
readonly config: Config | undefined
|
|
53
55
|
/**
|
|
54
|
-
*
|
|
55
|
-
* runs the adapter, and creates the PluginDriver.
|
|
56
|
-
*
|
|
57
|
-
* Calling `build()` or `safeBuild()` without calling `setup()` first will
|
|
58
|
-
* automatically invoke `setup()` before proceeding.
|
|
56
|
+
* Resolves config and initializes the driver. `build()` calls this automatically.
|
|
59
57
|
*/
|
|
60
58
|
setup(): Promise<void>
|
|
61
59
|
/**
|
|
62
|
-
* Runs
|
|
63
|
-
* Automatically calls `setup()` if it has not been called yet.
|
|
60
|
+
* Runs the full pipeline and throws on any plugin error. Automatically calls `setup()` if needed.
|
|
64
61
|
*/
|
|
65
62
|
build(): Promise<BuildOutput>
|
|
66
63
|
/**
|
|
67
|
-
* Runs
|
|
68
|
-
* Automatically calls `setup()` if it has not been called yet.
|
|
64
|
+
* Runs the full pipeline and captures errors in `BuildOutput` instead of throwing. Automatically calls `setup()` if needed.
|
|
69
65
|
*/
|
|
70
66
|
safeBuild(): Promise<BuildOutput>
|
|
71
67
|
}
|
|
72
68
|
|
|
73
69
|
/**
|
|
74
|
-
*
|
|
75
|
-
*
|
|
70
|
+
* Lifecycle events emitted during Kubb code generation.
|
|
71
|
+
* Use these for logging, progress tracking, and custom integrations.
|
|
76
72
|
*
|
|
77
73
|
* @example
|
|
78
74
|
* ```typescript
|
|
@@ -92,76 +88,74 @@ export type Kubb = {
|
|
|
92
88
|
*/
|
|
93
89
|
export interface KubbHooks {
|
|
94
90
|
/**
|
|
95
|
-
*
|
|
91
|
+
* Fires at the start of the Kubb lifecycle, before code generation begins.
|
|
96
92
|
*/
|
|
97
93
|
'kubb:lifecycle:start': [ctx: KubbLifecycleStartContext]
|
|
98
94
|
/**
|
|
99
|
-
*
|
|
95
|
+
* Fires at the end of the Kubb lifecycle, after all code generation completes.
|
|
100
96
|
*/
|
|
101
97
|
'kubb:lifecycle:end': []
|
|
102
98
|
|
|
103
99
|
/**
|
|
104
|
-
*
|
|
100
|
+
* Fires when configuration loading starts.
|
|
105
101
|
*/
|
|
106
102
|
'kubb:config:start': []
|
|
107
103
|
/**
|
|
108
|
-
*
|
|
104
|
+
* Fires when configuration loading completes.
|
|
109
105
|
*/
|
|
110
106
|
'kubb:config:end': [ctx: KubbConfigEndContext]
|
|
111
107
|
|
|
112
108
|
/**
|
|
113
|
-
*
|
|
109
|
+
* Fires when code generation starts.
|
|
114
110
|
*/
|
|
115
111
|
'kubb:generation:start': [ctx: KubbGenerationStartContext]
|
|
116
112
|
/**
|
|
117
|
-
*
|
|
113
|
+
* Fires when code generation completes.
|
|
118
114
|
*/
|
|
119
115
|
'kubb:generation:end': [ctx: KubbGenerationEndContext]
|
|
120
116
|
/**
|
|
121
|
-
*
|
|
122
|
-
* Contains summary lines, title, and success status.
|
|
117
|
+
* Fires with a generation summary including summary lines, title, and success status.
|
|
123
118
|
*/
|
|
124
119
|
'kubb:generation:summary': [ctx: KubbGenerationSummaryContext]
|
|
125
120
|
|
|
126
121
|
/**
|
|
127
|
-
*
|
|
122
|
+
* Fires when code formatting starts (e.g., Biome or Prettier).
|
|
128
123
|
*/
|
|
129
124
|
'kubb:format:start': []
|
|
130
125
|
/**
|
|
131
|
-
*
|
|
126
|
+
* Fires when code formatting completes.
|
|
132
127
|
*/
|
|
133
128
|
'kubb:format:end': []
|
|
134
129
|
|
|
135
130
|
/**
|
|
136
|
-
*
|
|
131
|
+
* Fires when linting starts.
|
|
137
132
|
*/
|
|
138
133
|
'kubb:lint:start': []
|
|
139
134
|
/**
|
|
140
|
-
*
|
|
135
|
+
* Fires when linting completes.
|
|
141
136
|
*/
|
|
142
137
|
'kubb:lint:end': []
|
|
143
138
|
|
|
144
139
|
/**
|
|
145
|
-
*
|
|
140
|
+
* Fires when plugin hooks execution starts.
|
|
146
141
|
*/
|
|
147
142
|
'kubb:hooks:start': []
|
|
148
143
|
/**
|
|
149
|
-
*
|
|
144
|
+
* Fires when plugin hooks execution completes.
|
|
150
145
|
*/
|
|
151
146
|
'kubb:hooks:end': []
|
|
152
147
|
|
|
153
148
|
/**
|
|
154
|
-
*
|
|
155
|
-
* The callback should be invoked when the command completes.
|
|
149
|
+
* Fires when a single hook executes (e.g., format or lint). The callback is invoked when the command finishes.
|
|
156
150
|
*/
|
|
157
151
|
'kubb:hook:start': [ctx: KubbHookStartContext]
|
|
158
152
|
/**
|
|
159
|
-
*
|
|
153
|
+
* Fires when a single hook execution completes.
|
|
160
154
|
*/
|
|
161
155
|
'kubb:hook:end': [ctx: KubbHookEndContext]
|
|
162
156
|
|
|
163
157
|
/**
|
|
164
|
-
*
|
|
158
|
+
* Fires when a new Kubb version is available.
|
|
165
159
|
*/
|
|
166
160
|
'kubb:version:new': [ctx: KubbVersionNewContext]
|
|
167
161
|
|
|
@@ -170,7 +164,7 @@ export interface KubbHooks {
|
|
|
170
164
|
*/
|
|
171
165
|
'kubb:info': [ctx: KubbInfoContext]
|
|
172
166
|
/**
|
|
173
|
-
* Error event
|
|
167
|
+
* Error event, fired when an error occurs during generation.
|
|
174
168
|
*/
|
|
175
169
|
'kubb:error': [ctx: KubbErrorContext]
|
|
176
170
|
/**
|
|
@@ -182,81 +176,60 @@ export interface KubbHooks {
|
|
|
182
176
|
*/
|
|
183
177
|
'kubb:warn': [ctx: KubbWarnContext]
|
|
184
178
|
/**
|
|
185
|
-
* Debug event for detailed logging.
|
|
186
|
-
* Contains timestamp, log messages, and optional filename.
|
|
179
|
+
* Debug event for detailed logging with timestamp and optional filename.
|
|
187
180
|
*/
|
|
188
181
|
'kubb:debug': [ctx: KubbDebugContext]
|
|
189
182
|
|
|
190
183
|
/**
|
|
191
|
-
*
|
|
192
|
-
* Contains the list of files to be processed.
|
|
184
|
+
* Fires when file processing starts with the list of files to process.
|
|
193
185
|
*/
|
|
194
186
|
'kubb:files:processing:start': [ctx: KubbFilesProcessingStartContext]
|
|
195
187
|
/**
|
|
196
|
-
*
|
|
197
|
-
* Contains processed count, total count, percentage, and file details.
|
|
188
|
+
* Fires for each file with progress updates: processed count, total, percentage, and file details.
|
|
198
189
|
*/
|
|
199
190
|
'kubb:file:processing:update': [ctx: KubbFileProcessingUpdateContext]
|
|
200
191
|
/**
|
|
201
|
-
*
|
|
202
|
-
* Contains the list of processed files.
|
|
192
|
+
* Fires when file processing completes with the list of processed files.
|
|
203
193
|
*/
|
|
204
194
|
'kubb:files:processing:end': [ctx: KubbFilesProcessingEndContext]
|
|
205
195
|
|
|
206
196
|
/**
|
|
207
|
-
*
|
|
197
|
+
* Fires when a plugin starts execution.
|
|
208
198
|
*/
|
|
209
199
|
'kubb:plugin:start': [ctx: KubbPluginStartContext]
|
|
210
200
|
/**
|
|
211
|
-
*
|
|
212
|
-
* Duration in ms.
|
|
201
|
+
* Fires when a plugin completes execution. Duration measured in milliseconds.
|
|
213
202
|
*/
|
|
214
203
|
'kubb:plugin:end': [ctx: KubbPluginEndContext]
|
|
215
204
|
|
|
216
205
|
/**
|
|
217
|
-
*
|
|
218
|
-
* can register generators, configure resolvers/transformers/renderers, or inject
|
|
219
|
-
* extra files. All `kubb:plugin:setup` handlers registered via `definePlugin` receive
|
|
220
|
-
* a plugin-specific context (with the correct `addGenerator` closure).
|
|
221
|
-
* External tooling can observe this event via `hooks.on('kubb:plugin:setup', …)`.
|
|
206
|
+
* Fires once before plugins execute — allowing plugins to register generators, configure resolvers/transformers/renderers, or inject files.
|
|
222
207
|
*/
|
|
223
208
|
'kubb:plugin:setup': [ctx: KubbPluginSetupContext]
|
|
224
209
|
/**
|
|
225
|
-
*
|
|
226
|
-
* The adapter has already parsed the source and `inputNode` is available.
|
|
210
|
+
* Fires before the plugin execution loop begins. The adapter has already parsed the source and `inputNode` is available.
|
|
227
211
|
*/
|
|
228
212
|
'kubb:build:start': [ctx: KubbBuildStartContext]
|
|
229
213
|
/**
|
|
230
|
-
*
|
|
231
|
-
*
|
|
232
|
-
* Use this event to inject final files (e.g. a root barrel) that must be persisted
|
|
233
|
-
* in the same write pass as the plugin output.
|
|
214
|
+
* Fires after all plugins run and per-plugin barrels generate, but before files write to disk.
|
|
215
|
+
* Use this to inject final files that must persist in the same write pass as plugin output.
|
|
234
216
|
*/
|
|
235
217
|
'kubb:plugins:end': [ctx: KubbPluginsEndContext]
|
|
236
218
|
/**
|
|
237
|
-
*
|
|
219
|
+
* Fires after all files write to disk.
|
|
238
220
|
*/
|
|
239
221
|
'kubb:build:end': [ctx: KubbBuildEndContext]
|
|
240
222
|
|
|
241
223
|
/**
|
|
242
|
-
*
|
|
243
|
-
* Generator listeners registered via `addGenerator()` in `kubb:plugin:setup` respond to this event.
|
|
244
|
-
* The `ctx.plugin.name` identifies which plugin is driving the current walk.
|
|
245
|
-
* `ctx.options` carries the per-node resolved options (after exclude/include/override).
|
|
224
|
+
* Fires for each schema node during AST traversal. Generator listeners respond to this.
|
|
246
225
|
*/
|
|
247
226
|
'kubb:generate:schema': [node: SchemaNode, ctx: GeneratorContext]
|
|
248
227
|
/**
|
|
249
|
-
*
|
|
250
|
-
* Generator listeners registered via `addGenerator()` in `kubb:plugin:setup` respond to this event.
|
|
251
|
-
* The `ctx.plugin.name` identifies which plugin is driving the current walk.
|
|
252
|
-
* `ctx.options` carries the per-node resolved options (after exclude/include/override).
|
|
228
|
+
* Fires for each operation node during AST traversal. Generator listeners respond to this.
|
|
253
229
|
*/
|
|
254
230
|
'kubb:generate:operation': [node: OperationNode, ctx: GeneratorContext]
|
|
255
231
|
/**
|
|
256
|
-
*
|
|
257
|
-
* Generator listeners with an `operations()` method respond to this event.
|
|
258
|
-
* The `ctx.plugin.name` identifies which plugin is driving the current walk.
|
|
259
|
-
* `ctx.options` carries the plugin-level resolved options for the batch call.
|
|
232
|
+
* Fires once after all operations traverse with the full collected array. Batch generator listeners respond to this.
|
|
260
233
|
*/
|
|
261
234
|
'kubb:generate:operations': [nodes: Array<OperationNode>, ctx: GeneratorContext]
|
|
262
235
|
}
|
package/src/createAdapter.ts
CHANGED
|
@@ -1,24 +1,31 @@
|
|
|
1
1
|
import type { Adapter, AdapterFactoryOptions } from './types.ts'
|
|
2
2
|
|
|
3
|
-
/**
|
|
4
|
-
* Builder type for an {@link Adapter} — takes options and returns the adapter instance.
|
|
5
|
-
*/
|
|
6
3
|
type AdapterBuilder<T extends AdapterFactoryOptions> = (options: T['options']) => Adapter<T>
|
|
7
4
|
|
|
8
5
|
/**
|
|
9
|
-
*
|
|
6
|
+
* Factory for implementing custom adapters that translate non-OpenAPI specs into Kubb's AST.
|
|
7
|
+
*
|
|
8
|
+
* Use this to support GraphQL schemas, gRPC definitions, AsyncAPI, or custom domain-specific languages.
|
|
9
|
+
* Built-in adapters include `@kubb/adapter-oas` for OpenAPI and Swagger documents.
|
|
10
|
+
*
|
|
11
|
+
* @note Adapters must parse their input format to Kubb's `InputNode` structure.
|
|
10
12
|
*
|
|
11
13
|
* @example
|
|
14
|
+
* ```ts
|
|
12
15
|
* export const myAdapter = createAdapter<MyAdapter>((options) => {
|
|
13
16
|
* return {
|
|
14
17
|
* name: 'my-adapter',
|
|
15
18
|
* options,
|
|
16
|
-
* async parse(source) {
|
|
19
|
+
* async parse(source) {
|
|
20
|
+
* // Transform source format to InputNode
|
|
21
|
+
* return { ... }
|
|
22
|
+
* },
|
|
17
23
|
* }
|
|
18
24
|
* })
|
|
19
25
|
*
|
|
20
|
-
* //
|
|
26
|
+
* // Instantiate:
|
|
21
27
|
* const adapter = myAdapter({ validate: true })
|
|
28
|
+
* ```
|
|
22
29
|
*/
|
|
23
30
|
export function createAdapter<T extends AdapterFactoryOptions = AdapterFactoryOptions>(build: AdapterBuilder<T>): (options?: T['options']) => Adapter<T> {
|
|
24
31
|
return (options) => build(options ?? ({} as T['options']))
|
package/src/createKubb.ts
CHANGED
|
@@ -110,6 +110,8 @@ async function setup(userConfig: UserConfig, options: SetupOptions = {}): Promis
|
|
|
110
110
|
parsers: userConfig.parsers ?? [],
|
|
111
111
|
adapter: userConfig.adapter,
|
|
112
112
|
output: {
|
|
113
|
+
format: false,
|
|
114
|
+
lint: false,
|
|
113
115
|
write: true,
|
|
114
116
|
extension: DEFAULT_EXTENSION,
|
|
115
117
|
defaultBanner: DEFAULT_BANNER,
|
package/src/createStorage.ts
CHANGED
|
@@ -34,9 +34,17 @@ export type Storage = {
|
|
|
34
34
|
}
|
|
35
35
|
|
|
36
36
|
/**
|
|
37
|
-
*
|
|
37
|
+
* Factory for implementing custom storage backends that control where generated files are written.
|
|
38
|
+
*
|
|
39
|
+
* Takes a builder function `(options: TOptions) => Storage` and returns a factory `(options?: TOptions) => Storage`.
|
|
40
|
+
* Kubb provides filesystem and in-memory implementations out of the box.
|
|
41
|
+
*
|
|
42
|
+
* @note Call the returned factory with optional options to instantiate the storage adapter.
|
|
38
43
|
*
|
|
39
44
|
* @example
|
|
45
|
+
* ```ts
|
|
46
|
+
* import { createStorage } from '@kubb/core'
|
|
47
|
+
*
|
|
40
48
|
* export const memoryStorage = createStorage(() => {
|
|
41
49
|
* const store = new Map<string, string>()
|
|
42
50
|
* return {
|
|
@@ -52,6 +60,10 @@ export type Storage = {
|
|
|
52
60
|
* async clear(base) { if (!base) store.clear() },
|
|
53
61
|
* }
|
|
54
62
|
* })
|
|
63
|
+
*
|
|
64
|
+
* // Instantiate:
|
|
65
|
+
* const storage = memoryStorage()
|
|
66
|
+
* ```
|
|
55
67
|
*/
|
|
56
68
|
export function createStorage<TOptions = Record<string, never>>(build: (options: TOptions) => Storage): (options?: TOptions) => Storage {
|
|
57
69
|
return (options) => build(options ?? ({} as TOptions))
|
package/src/defineGenerator.ts
CHANGED
|
@@ -6,33 +6,26 @@ import type { GeneratorContext, PluginFactoryOptions } from './types.ts'
|
|
|
6
6
|
export type { GeneratorContext } from './types.ts'
|
|
7
7
|
|
|
8
8
|
/**
|
|
9
|
-
*
|
|
10
|
-
* methods. Each method receives the AST node as the first argument and a typed
|
|
11
|
-
* `ctx` object as the second, giving access to `ctx.config`, `ctx.resolver`,
|
|
12
|
-
* `ctx.adapter`, `ctx.options`, `ctx.upsertFile`, etc.
|
|
9
|
+
* Declares a named generator unit that walks the AST and emits files.
|
|
13
10
|
*
|
|
14
|
-
*
|
|
15
|
-
*
|
|
16
|
-
*
|
|
11
|
+
* Each method (`schema`, `operation`, `operations`) is called for the matching node type.
|
|
12
|
+
* Each method returns `TElement | Array<FileNode> | void`. JSX-based generators require a `renderer` factory.
|
|
13
|
+
* Return `Array<FileNode>` directly or call `ctx.upsertFile()` manually and return `void` to bypass rendering.
|
|
17
14
|
*
|
|
18
|
-
*
|
|
19
|
-
* writing manually via `ctx.upsertFile`.
|
|
15
|
+
* @note Generators are consumed by plugins and registered via `ctx.addGenerator()` in `kubb:plugin:setup`.
|
|
20
16
|
*
|
|
21
17
|
* @example
|
|
22
18
|
* ```ts
|
|
19
|
+
* import { defineGenerator } from '@kubb/core'
|
|
23
20
|
* import { jsxRenderer } from '@kubb/renderer-jsx'
|
|
24
21
|
*
|
|
25
|
-
* export const typeGenerator = defineGenerator
|
|
22
|
+
* export const typeGenerator = defineGenerator({
|
|
26
23
|
* name: 'typescript',
|
|
27
24
|
* renderer: jsxRenderer,
|
|
28
25
|
* schema(node, ctx) {
|
|
29
26
|
* const { adapter, resolver, root, options } = ctx
|
|
30
27
|
* return <File ...><Type node={node} resolver={resolver} /></File>
|
|
31
28
|
* },
|
|
32
|
-
* operation(node, ctx) {
|
|
33
|
-
* const { options } = ctx
|
|
34
|
-
* return <File ...><OperationType node={node} /></File>
|
|
35
|
-
* },
|
|
36
29
|
* })
|
|
37
30
|
* ```
|
|
38
31
|
*/
|
package/src/defineMiddleware.ts
CHANGED
|
@@ -22,14 +22,17 @@ export type Middleware = {
|
|
|
22
22
|
}
|
|
23
23
|
|
|
24
24
|
/**
|
|
25
|
-
* Creates a middleware factory using the hook-style
|
|
25
|
+
* Creates a middleware factory using the hook-style `hooks` API.
|
|
26
26
|
*
|
|
27
|
-
*
|
|
28
|
-
*
|
|
29
|
-
*
|
|
27
|
+
* Middleware handlers fire after all plugin handlers for any given event, making them ideal for post-processing, logging, and auditing.
|
|
28
|
+
* Per-build state (such as accumulators) belongs inside the factory closure so each `createKubb` invocation gets its own isolated instance.
|
|
29
|
+
*
|
|
30
|
+
* @note The factory can accept typed options. See examples for using options and per-build state patterns.
|
|
30
31
|
*
|
|
31
32
|
* @example
|
|
32
33
|
* ```ts
|
|
34
|
+
* import { defineMiddleware } from '@kubb/core'
|
|
35
|
+
*
|
|
33
36
|
* // Stateless middleware
|
|
34
37
|
* export const logMiddleware = defineMiddleware(() => ({
|
|
35
38
|
* name: 'log-middleware',
|
|
@@ -41,10 +44,10 @@ export type Middleware = {
|
|
|
41
44
|
* }))
|
|
42
45
|
*
|
|
43
46
|
* // Middleware with options and per-build state
|
|
44
|
-
* export const
|
|
47
|
+
* export const prefixMiddleware = defineMiddleware((options: { prefix: string } = { prefix: '' }) => {
|
|
45
48
|
* const seen = new Set<string>()
|
|
46
49
|
* return {
|
|
47
|
-
* name: '
|
|
50
|
+
* name: 'prefix-middleware',
|
|
48
51
|
* hooks: {
|
|
49
52
|
* 'kubb:plugin:end'({ plugin }) {
|
|
50
53
|
* seen.add(`${options.prefix}${plugin.name}`)
|
|
@@ -52,11 +55,6 @@ export type Middleware = {
|
|
|
52
55
|
* },
|
|
53
56
|
* }
|
|
54
57
|
* })
|
|
55
|
-
*
|
|
56
|
-
* // Usage in kubb.config.ts:
|
|
57
|
-
* export default defineConfig({
|
|
58
|
-
* middleware: [logMiddleware(), myMiddleware({ prefix: 'pfx:' })],
|
|
59
|
-
* })
|
|
60
58
|
* ```
|
|
61
59
|
*/
|
|
62
60
|
export function defineMiddleware<TOptions extends object = object>(factory: (options: TOptions) => Middleware): (options?: TOptions) => Middleware {
|
package/src/defineParser.ts
CHANGED
|
@@ -21,10 +21,9 @@ export type Parser<TMeta extends object = any> = {
|
|
|
21
21
|
}
|
|
22
22
|
|
|
23
23
|
/**
|
|
24
|
-
* Defines a parser with type safety.
|
|
24
|
+
* Defines a parser with type safety. Creates parsers that transform generated files to strings based on their extension.
|
|
25
25
|
*
|
|
26
|
-
*
|
|
27
|
-
* based on their extension.
|
|
26
|
+
* @note Call the returned factory with optional options to instantiate the parser.
|
|
28
27
|
*
|
|
29
28
|
* @example
|
|
30
29
|
* ```ts
|
package/src/definePlugin.ts
CHANGED
|
@@ -54,23 +54,23 @@ export function isPlugin(plugin: unknown): plugin is Plugin {
|
|
|
54
54
|
}
|
|
55
55
|
|
|
56
56
|
/**
|
|
57
|
-
*
|
|
57
|
+
* Wraps a factory function and returns a typed `Plugin` with lifecycle handlers grouped under `hooks`.
|
|
58
58
|
*
|
|
59
|
-
*
|
|
60
|
-
*
|
|
61
|
-
* `kubb.config.ts`.
|
|
59
|
+
* Handlers live in a single `hooks` object (inspired by Astro integrations).
|
|
60
|
+
* All lifecycle events from `KubbHooks` are available for subscription.
|
|
62
61
|
*
|
|
63
|
-
*
|
|
64
|
-
*
|
|
62
|
+
* @note For real plugins, use a `PluginFactoryOptions` type parameter to get type-safe context in `kubb:plugin:setup`.
|
|
63
|
+
* Plugin names should follow the convention `plugin-<feature>` (e.g., `plugin-react-query`, `plugin-zod`).
|
|
65
64
|
*
|
|
66
65
|
* @example
|
|
67
66
|
* ```ts
|
|
68
|
-
*
|
|
69
|
-
*
|
|
67
|
+
* import { definePlugin } from '@kubb/core'
|
|
68
|
+
*
|
|
69
|
+
* export const pluginTs = definePlugin((options: { prefix?: string } = {}) => ({
|
|
70
70
|
* name: 'plugin-ts',
|
|
71
71
|
* hooks: {
|
|
72
72
|
* 'kubb:plugin:setup'(ctx) {
|
|
73
|
-
* ctx.setResolver(resolverTs)
|
|
73
|
+
* ctx.setResolver(resolverTs)
|
|
74
74
|
* },
|
|
75
75
|
* },
|
|
76
76
|
* }))
|
package/src/mocks.ts
CHANGED
|
@@ -7,7 +7,8 @@ import { applyHookResult } from './renderNode.ts'
|
|
|
7
7
|
import type { Adapter, AdapterFactoryOptions, Config, Generator, GeneratorContext, NormalizedPlugin, PluginFactoryOptions } from './types.ts'
|
|
8
8
|
|
|
9
9
|
/**
|
|
10
|
-
|
|
10
|
+
|
|
11
|
+
* Creates a minimal `PluginDriver` mock for unit tests.
|
|
11
12
|
*/
|
|
12
13
|
export function createMockedPluginDriver(options: { name?: string; plugin?: NormalizedPlugin; config?: Config } = {}): PluginDriver {
|
|
13
14
|
return {
|
|
@@ -26,10 +27,9 @@ export function createMockedPluginDriver(options: { name?: string; plugin?: Norm
|
|
|
26
27
|
}
|
|
27
28
|
|
|
28
29
|
/**
|
|
29
|
-
* Creates a minimal `Adapter` mock
|
|
30
|
-
*
|
|
31
|
-
*
|
|
32
|
-
* - `getImports` returns `[]` by default (single-file mode, no cross-file imports).
|
|
30
|
+
* Creates a minimal `Adapter` mock for unit tests.
|
|
31
|
+
* `parse` returns an empty `InputNode` by default; override via `options.parse`.
|
|
32
|
+
* `getImports` returns `[]` by default.
|
|
33
33
|
*/
|
|
34
34
|
export function createMockedAdapter<TOptions extends AdapterFactoryOptions = AdapterFactoryOptions>(
|
|
35
35
|
options: {
|
|
@@ -51,7 +51,7 @@ export function createMockedAdapter<TOptions extends AdapterFactoryOptions = Ada
|
|
|
51
51
|
}
|
|
52
52
|
|
|
53
53
|
/**
|
|
54
|
-
* Creates a minimal plugin mock
|
|
54
|
+
* Creates a minimal plugin mock for unit tests.
|
|
55
55
|
*
|
|
56
56
|
* @example
|
|
57
57
|
* const plugin = createMockedPlugin<PluginTs>({ name: '@kubb/plugin-ts', options })
|