@kubb/core 5.0.0-beta.7 → 5.0.0-beta.70

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.
Files changed (49) hide show
  1. package/LICENSE +17 -10
  2. package/README.md +20 -123
  3. package/dist/diagnostics-CJtO1uSM.d.ts +2892 -0
  4. package/dist/index.cjs +2340 -1129
  5. package/dist/index.cjs.map +1 -1
  6. package/dist/index.d.ts +80 -289
  7. package/dist/index.js +2330 -1124
  8. package/dist/index.js.map +1 -1
  9. package/dist/memoryStorage-B4VTTIpQ.js +905 -0
  10. package/dist/memoryStorage-B4VTTIpQ.js.map +1 -0
  11. package/dist/memoryStorage-CfycFGzX.cjs +1043 -0
  12. package/dist/memoryStorage-CfycFGzX.cjs.map +1 -0
  13. package/dist/mocks.cjs +84 -24
  14. package/dist/mocks.cjs.map +1 -1
  15. package/dist/mocks.d.ts +37 -11
  16. package/dist/mocks.js +86 -28
  17. package/dist/mocks.js.map +1 -1
  18. package/package.json +9 -23
  19. package/dist/PluginDriver-BkTRD2H2.js +0 -946
  20. package/dist/PluginDriver-BkTRD2H2.js.map +0 -1
  21. package/dist/PluginDriver-Cadu4ORh.cjs +0 -1037
  22. package/dist/PluginDriver-Cadu4ORh.cjs.map +0 -1
  23. package/dist/types-ChyWgIgi.d.ts +0 -2159
  24. package/src/FileManager.ts +0 -115
  25. package/src/FileProcessor.ts +0 -86
  26. package/src/Kubb.ts +0 -300
  27. package/src/PluginDriver.ts +0 -426
  28. package/src/constants.ts +0 -35
  29. package/src/createAdapter.ts +0 -32
  30. package/src/createKubb.ts +0 -573
  31. package/src/createRenderer.ts +0 -57
  32. package/src/createStorage.ts +0 -70
  33. package/src/defineGenerator.ts +0 -87
  34. package/src/defineLogger.ts +0 -36
  35. package/src/defineMiddleware.ts +0 -62
  36. package/src/defineParser.ts +0 -44
  37. package/src/definePlugin.ts +0 -83
  38. package/src/defineResolver.ts +0 -521
  39. package/src/devtools.ts +0 -59
  40. package/src/index.ts +0 -20
  41. package/src/mocks.ts +0 -178
  42. package/src/renderNode.ts +0 -35
  43. package/src/storages/fsStorage.ts +0 -114
  44. package/src/storages/memoryStorage.ts +0 -55
  45. package/src/types.ts +0 -1305
  46. package/src/utils/diagnostics.ts +0 -18
  47. package/src/utils/isInputPath.ts +0 -10
  48. package/src/utils/packageJSON.ts +0 -99
  49. /package/dist/{chunk--u3MIqq1.js → rolldown-runtime-C0LytTxp.js} +0 -0
@@ -1,426 +0,0 @@
1
- import { extname, resolve } from 'node:path'
2
- import type { AsyncEventEmitter } from '@internals/utils'
3
- import type { FileNode, InputNode, OperationNode, SchemaNode } from '@kubb/ast'
4
- import { createFile } from '@kubb/ast'
5
- import { DEFAULT_STUDIO_URL } from './constants.ts'
6
- import type { Generator } from './defineGenerator.ts'
7
- import type { Plugin } from './definePlugin.ts'
8
- import { defineResolver } from './defineResolver.ts'
9
- import { openInStudio as openInStudioFn } from './devtools.ts'
10
- import { FileManager } from './FileManager.ts'
11
- import { applyHookResult } from './renderNode.ts'
12
-
13
- import type {
14
- Adapter,
15
- Config,
16
- DevtoolsOptions,
17
- GeneratorContext,
18
- KubbHooks,
19
- KubbPluginSetupContext,
20
- NormalizedPlugin,
21
- PluginFactoryOptions,
22
- Resolver,
23
- } from './types.ts'
24
-
25
- // inspired by: https://github.com/rollup/rollup/blob/master/src/utils/PluginDriver.ts#
26
-
27
- type Options = {
28
- hooks: AsyncEventEmitter<KubbHooks>
29
- }
30
-
31
- function enforceOrder(enforce: 'pre' | 'post' | undefined): number {
32
- return enforce === 'pre' ? -1 : enforce === 'post' ? 1 : 0
33
- }
34
-
35
- export class PluginDriver {
36
- readonly config: Config
37
- readonly options: Options
38
-
39
- /**
40
- * Returns `'single'` when `fileOrFolder` has a file extension, `'split'` otherwise.
41
- *
42
- * @example
43
- * ```ts
44
- * PluginDriver.getMode('src/gen/types.ts') // 'single'
45
- * PluginDriver.getMode('src/gen/types') // 'split'
46
- * ```
47
- */
48
- static getMode(fileOrFolder: string | undefined | null): 'single' | 'split' {
49
- if (!fileOrFolder) {
50
- return 'split'
51
- }
52
- return extname(fileOrFolder) ? 'single' : 'split'
53
- }
54
-
55
- /**
56
- * The universal `@kubb/ast` `InputNode` produced by the adapter, set by
57
- * the build pipeline after the adapter's `parse()` resolves.
58
- */
59
- inputNode: InputNode | undefined = undefined
60
- adapter: Adapter | undefined = undefined
61
- #studioIsOpen = false
62
-
63
- /**
64
- * Central file store for all generated files.
65
- * Plugins should use `this.addFile()` / `this.upsertFile()` (via their context) to
66
- * add files; this property gives direct read/write access when needed.
67
- */
68
- readonly fileManager = new FileManager()
69
-
70
- readonly plugins = new Map<string, NormalizedPlugin>()
71
-
72
- /**
73
- * Tracks which plugins have generators registered via `addGenerator()` (event-based path).
74
- * Used by the build loop to decide whether to emit generator events for a given plugin.
75
- */
76
- readonly #pluginsWithEventGenerators = new Set<string>()
77
- readonly #resolvers = new Map<string, Resolver>()
78
- readonly #defaultResolvers = new Map<string, Resolver>()
79
- readonly #hookListeners = new Map<keyof KubbHooks, Set<(...args: never[]) => void | Promise<void>>>()
80
-
81
- constructor(config: Config, options: Options) {
82
- this.config = config
83
- this.options = options
84
- config.plugins
85
- .map((rawPlugin) => this.#normalizePlugin(rawPlugin as Plugin))
86
- .filter((plugin) => {
87
- if (typeof plugin.apply === 'function') {
88
- return plugin.apply(config)
89
- }
90
- return true
91
- })
92
- .sort((a, b) => {
93
- if (b.dependencies?.includes(a.name)) return -1
94
- if (a.dependencies?.includes(b.name)) return 1
95
- // enforce: 'pre' plugins run first, 'post' plugins run last
96
- return enforceOrder(a.enforce) - enforceOrder(b.enforce)
97
- })
98
- .forEach((plugin) => {
99
- this.plugins.set(plugin.name, plugin)
100
- })
101
- }
102
-
103
- get hooks() {
104
- return this.options.hooks
105
- }
106
-
107
- /**
108
- * Creates an `NormalizedPlugin` from a hook-style plugin and registers
109
- * its lifecycle handlers on the `AsyncEventEmitter`.
110
- */
111
- #normalizePlugin(hookPlugin: Plugin): NormalizedPlugin {
112
- const normalizedPlugin = {
113
- name: hookPlugin.name,
114
- dependencies: hookPlugin.dependencies,
115
- enforce: hookPlugin.enforce,
116
- options: { output: { path: '.' }, exclude: [], override: [] },
117
- } as unknown as NormalizedPlugin
118
-
119
- this.registerPluginHooks(hookPlugin, normalizedPlugin)
120
- return normalizedPlugin
121
- }
122
-
123
- /**
124
- * Registers a hook-style plugin's lifecycle handlers on the shared `AsyncEventEmitter`.
125
- *
126
- * For `kubb:plugin:setup`, the registered listener wraps the globally emitted context with a
127
- * plugin-specific one so that `addGenerator`, `setResolver`, `setTransformer`, and
128
- * `setRenderer` all target the correct `normalizedPlugin` entry in the plugins map.
129
- *
130
- * All other hooks are iterated and registered directly as pass-through listeners.
131
- * Any event key present in the global `KubbHooks` interface can be subscribed to.
132
- *
133
- * External tooling can subscribe to any of these events via `hooks.on(...)` to observe
134
- * the plugin lifecycle without modifying plugin behavior.
135
- *
136
- * @internal
137
- */
138
- registerPluginHooks(hookPlugin: Plugin, normalizedPlugin: NormalizedPlugin): void {
139
- const { hooks } = hookPlugin
140
-
141
- if (!hooks) return
142
-
143
- // kubb:plugin:setup gets special treatment: the globally emitted context is wrapped with
144
- // plugin-specific implementations so that addGenerator / setResolver / etc. target
145
- // this plugin's normalizedPlugin entry rather than being no-ops.
146
- if (hooks['kubb:plugin:setup']) {
147
- const setupHandler = (globalCtx: KubbPluginSetupContext) => {
148
- const pluginCtx: KubbPluginSetupContext = {
149
- ...globalCtx,
150
- options: hookPlugin.options ?? {},
151
- addGenerator: (gen) => {
152
- this.registerGenerator(normalizedPlugin.name, gen)
153
- },
154
- setResolver: (resolver) => {
155
- this.setPluginResolver(normalizedPlugin.name, resolver)
156
- },
157
- setTransformer: (visitor) => {
158
- normalizedPlugin.transformer = visitor
159
- },
160
- setRenderer: (renderer) => {
161
- normalizedPlugin.renderer = renderer
162
- },
163
- setOptions: (opts) => {
164
- normalizedPlugin.options = { ...normalizedPlugin.options, ...opts }
165
- },
166
- injectFile: (userFileNode) => {
167
- this.fileManager.add(createFile(userFileNode))
168
- },
169
- }
170
- return hooks['kubb:plugin:setup']!(pluginCtx)
171
- }
172
-
173
- this.hooks.on('kubb:plugin:setup', setupHandler)
174
- this.#trackHookListener('kubb:plugin:setup', setupHandler as (...args: never[]) => void | Promise<void>)
175
- }
176
-
177
- // All other hooks are registered as direct pass-through listeners on the shared emitter.
178
- for (const [event, handler] of Object.entries(hooks) as Array<[keyof KubbHooks, ((...args: never[]) => void | Promise<void>) | undefined]>) {
179
- if (event === 'kubb:plugin:setup' || !handler) continue
180
-
181
- this.hooks.on(event, handler as never)
182
- this.#trackHookListener(event, handler as (...args: never[]) => void | Promise<void>)
183
- }
184
- }
185
-
186
- /**
187
- * Emits the `kubb:plugin:setup` event so that all registered hook-style plugin listeners
188
- * can configure generators, resolvers, transformers and renderers before `buildStart` runs.
189
- *
190
- * Call this once from `safeBuild` before the plugin execution loop begins.
191
- */
192
- async emitSetupHooks(): Promise<void> {
193
- const noop = () => {}
194
- await this.hooks.emit('kubb:plugin:setup', {
195
- config: this.config,
196
- options: {},
197
- addGenerator: noop,
198
- setResolver: noop,
199
- setTransformer: noop,
200
- setRenderer: noop,
201
- setOptions: noop,
202
- injectFile: noop,
203
- updateConfig: noop,
204
- })
205
- }
206
-
207
- /**
208
- * Registers a generator for the given plugin on the shared event emitter.
209
- *
210
- * The generator's `schema`, `operation`, and `operations` methods are registered as
211
- * listeners on `kubb:generate:schema`, `kubb:generate:operation`, and `kubb:generate:operations`
212
- * respectively. Each listener is scoped to the owning plugin via a `ctx.plugin.name` check
213
- * so that generators from different plugins do not cross-fire.
214
- *
215
- * The renderer resolution chain is: `generator.renderer → plugin.renderer → config.renderer`.
216
- * Set `generator.renderer = null` to explicitly opt out of rendering even when the plugin
217
- * declares a renderer.
218
- *
219
- * Call this method inside `addGenerator()` (in `kubb:plugin:setup`) to wire up a generator.
220
- */
221
- registerGenerator(pluginName: string, gen: Generator): void {
222
- const resolveRenderer = () => {
223
- const plugin = this.plugins.get(pluginName)
224
- return gen.renderer === null ? undefined : (gen.renderer ?? plugin?.renderer ?? this.config.renderer)
225
- }
226
-
227
- if (gen.schema) {
228
- const schemaHandler = async (node: SchemaNode, ctx: GeneratorContext) => {
229
- if (ctx.plugin.name !== pluginName) return
230
- const result = await gen.schema!(node, ctx)
231
- await applyHookResult(result, this, resolveRenderer())
232
- }
233
-
234
- this.hooks.on('kubb:generate:schema', schemaHandler)
235
- this.#trackHookListener('kubb:generate:schema', schemaHandler as (...args: never[]) => void | Promise<void>)
236
- }
237
-
238
- if (gen.operation) {
239
- const operationHandler = async (node: OperationNode, ctx: GeneratorContext) => {
240
- if (ctx.plugin.name !== pluginName) return
241
- const result = await gen.operation!(node, ctx)
242
- await applyHookResult(result, this, resolveRenderer())
243
- }
244
-
245
- this.hooks.on('kubb:generate:operation', operationHandler)
246
- this.#trackHookListener('kubb:generate:operation', operationHandler as (...args: never[]) => void | Promise<void>)
247
- }
248
-
249
- if (gen.operations) {
250
- const operationsHandler = async (nodes: Array<OperationNode>, ctx: GeneratorContext) => {
251
- if (ctx.plugin.name !== pluginName) return
252
- const result = await gen.operations!(nodes, ctx)
253
- await applyHookResult(result, this, resolveRenderer())
254
- }
255
-
256
- this.hooks.on('kubb:generate:operations', operationsHandler)
257
- this.#trackHookListener('kubb:generate:operations', operationsHandler as (...args: never[]) => void | Promise<void>)
258
- }
259
-
260
- this.#pluginsWithEventGenerators.add(pluginName)
261
- }
262
-
263
- /**
264
- * Returns `true` when at least one generator was registered for the given plugin
265
- * via `addGenerator()` in `kubb:plugin:setup` (event-based path).
266
- *
267
- * Used by the build loop to decide whether to walk the AST and emit generator events
268
- * for a plugin that has no static `plugin.generators`.
269
- */
270
- hasRegisteredGenerators(pluginName: string): boolean {
271
- return this.#pluginsWithEventGenerators.has(pluginName)
272
- }
273
-
274
- /**
275
- * Unregisters all plugin lifecycle listeners from the shared event emitter.
276
- * Called at the end of a build to prevent listener leaks across repeated builds.
277
- *
278
- * @internal
279
- */
280
- dispose(): void {
281
- for (const [event, handlers] of this.#hookListeners) {
282
- for (const handler of handlers) {
283
- this.hooks.off(event, handler as never)
284
- }
285
- }
286
- this.#hookListeners.clear()
287
- this.#pluginsWithEventGenerators.clear()
288
- }
289
-
290
- #trackHookListener(event: keyof KubbHooks, handler: (...args: never[]) => void | Promise<void>): void {
291
- let handlers = this.#hookListeners.get(event)
292
- if (!handlers) {
293
- handlers = new Set()
294
- this.#hookListeners.set(event, handlers)
295
- }
296
- handlers.add(handler)
297
- }
298
-
299
- #createDefaultResolver(pluginName: string): Resolver {
300
- const existingResolver = this.#defaultResolvers.get(pluginName)
301
- if (existingResolver) {
302
- return existingResolver
303
- }
304
-
305
- const resolver = defineResolver<PluginFactoryOptions>((_ctx) => ({
306
- name: 'default',
307
- pluginName,
308
- }))
309
- this.#defaultResolvers.set(pluginName, resolver)
310
- return resolver
311
- }
312
-
313
- /**
314
- * Merges `partial` with the plugin's default resolver and stores the result.
315
- * Also mirrors it onto `plugin.resolver` so callers using `getPlugin(name).resolver`
316
- * get the up-to-date resolver without going through `getResolver()`.
317
- */
318
- setPluginResolver(pluginName: string, partial: Partial<Resolver>): void {
319
- const defaultResolver = this.#createDefaultResolver(pluginName)
320
- const merged = { ...defaultResolver, ...partial }
321
- this.#resolvers.set(pluginName, merged)
322
- const plugin = this.plugins.get(pluginName)
323
- if (plugin) {
324
- plugin.resolver = merged
325
- }
326
- }
327
-
328
- /**
329
- * Returns the resolver for the given plugin.
330
- *
331
- * Resolution order: dynamic resolver set via `setPluginResolver` → static resolver on the
332
- * plugin → lazily created default resolver (identity name, no path transforms).
333
- */
334
- getResolver<TName extends keyof Kubb.PluginRegistry>(pluginName: TName): Kubb.PluginRegistry[TName]['resolver']
335
- getResolver<TResolver extends Resolver = Resolver>(pluginName: string): TResolver
336
- getResolver(pluginName: string): Resolver {
337
- return this.#resolvers.get(pluginName) ?? this.plugins.get(pluginName)?.resolver ?? this.#createDefaultResolver(pluginName)
338
- }
339
-
340
- getContext<TOptions extends PluginFactoryOptions>(plugin: NormalizedPlugin<TOptions>): GeneratorContext<TOptions> & Record<string, unknown> {
341
- const driver = this
342
-
343
- const baseContext = {
344
- config: driver.config,
345
- get root(): string {
346
- return resolve(driver.config.root, driver.config.output.path)
347
- },
348
- getMode(output: { path: string }): 'single' | 'split' {
349
- return PluginDriver.getMode(resolve(driver.config.root, driver.config.output.path, output.path))
350
- },
351
- hooks: driver.hooks,
352
- plugin,
353
- getPlugin: driver.getPlugin.bind(driver),
354
- requirePlugin: driver.requirePlugin.bind(driver),
355
- getResolver: driver.getResolver.bind(driver),
356
- driver,
357
- addFile: async (...files: Array<FileNode>) => {
358
- driver.fileManager.add(...files)
359
- },
360
- upsertFile: async (...files: Array<FileNode>) => {
361
- driver.fileManager.upsert(...files)
362
- },
363
- get inputNode(): InputNode | undefined {
364
- return driver.inputNode
365
- },
366
- get adapter(): Adapter | undefined {
367
- return driver.adapter
368
- },
369
- get resolver() {
370
- return driver.getResolver(plugin.name)
371
- },
372
- get transformer() {
373
- return plugin.transformer
374
- },
375
- warn(message: string) {
376
- driver.hooks.emit('kubb:warn', { message })
377
- },
378
- error(error: string | Error) {
379
- driver.hooks.emit('kubb:error', { error: typeof error === 'string' ? new Error(error) : error })
380
- },
381
- info(message: string) {
382
- driver.hooks.emit('kubb:info', { message })
383
- },
384
- openInStudio(options?: DevtoolsOptions) {
385
- if (!driver.config.devtools || driver.#studioIsOpen) {
386
- return
387
- }
388
-
389
- if (typeof driver.config.devtools !== 'object') {
390
- throw new Error('Devtools must be an object')
391
- }
392
-
393
- if (!driver.inputNode || !driver.adapter) {
394
- throw new Error('adapter is not defined, make sure you have set the parser in kubb.config.ts')
395
- }
396
-
397
- driver.#studioIsOpen = true
398
-
399
- const studioUrl = driver.config.devtools?.studioUrl ?? DEFAULT_STUDIO_URL
400
-
401
- return openInStudioFn(driver.inputNode, studioUrl, options)
402
- },
403
- } as unknown as GeneratorContext<TOptions>
404
-
405
- return baseContext
406
- }
407
-
408
- getPlugin<TName extends keyof Kubb.PluginRegistry>(pluginName: TName): Plugin<Kubb.PluginRegistry[TName]> | undefined
409
- getPlugin<TOptions extends PluginFactoryOptions = PluginFactoryOptions>(pluginName: string): Plugin<TOptions> | undefined
410
- getPlugin(pluginName: string): Plugin | undefined {
411
- return this.plugins.get(pluginName)
412
- }
413
-
414
- /**
415
- * Like `getPlugin` but throws a descriptive error when the plugin is not found.
416
- */
417
- requirePlugin<TName extends keyof Kubb.PluginRegistry>(pluginName: TName): Plugin<Kubb.PluginRegistry[TName]>
418
- requirePlugin<TOptions extends PluginFactoryOptions = PluginFactoryOptions>(pluginName: string): Plugin<TOptions>
419
- requirePlugin(pluginName: string): Plugin {
420
- const plugin = this.plugins.get(pluginName)
421
- if (!plugin) {
422
- throw new Error(`[kubb] Plugin "${pluginName}" is required but not found. Make sure it is included in your Kubb config.`)
423
- }
424
- return plugin
425
- }
426
- }
package/src/constants.ts DELETED
@@ -1,35 +0,0 @@
1
- import type { FileNode } from '@kubb/ast'
2
-
3
- /**
4
- * Base URL for the Kubb Studio web app.
5
- */
6
- export const DEFAULT_STUDIO_URL = 'https://kubb.studio' as const
7
-
8
- /**
9
- * Maximum number of files processed in parallel by FileProcessor.
10
- */
11
- export const PARALLEL_CONCURRENCY_LIMIT = 100
12
-
13
- /**
14
- * Default banner style written at the top of every generated file.
15
- */
16
- export const DEFAULT_BANNER = 'simple' as const
17
-
18
- /**
19
- * Default file-extension mapping used when no explicit mapping is configured.
20
- */
21
- export const DEFAULT_EXTENSION: Record<FileNode['extname'], FileNode['extname'] | ''> = { '.ts': '.ts' }
22
-
23
- /**
24
- * Numeric log-level thresholds used internally to compare verbosity.
25
- *
26
- * Higher numbers are more verbose.
27
- */
28
- export const logLevel = {
29
- silent: Number.NEGATIVE_INFINITY,
30
- error: 0,
31
- warn: 1,
32
- info: 3,
33
- verbose: 4,
34
- debug: 5,
35
- } as const
@@ -1,32 +0,0 @@
1
- import type { Adapter, AdapterFactoryOptions } from './types.ts'
2
-
3
- type AdapterBuilder<T extends AdapterFactoryOptions> = (options: T['options']) => Adapter<T>
4
-
5
- /**
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.
12
- *
13
- * @example
14
- * ```ts
15
- * export const myAdapter = createAdapter<MyAdapter>((options) => {
16
- * return {
17
- * name: 'my-adapter',
18
- * options,
19
- * async parse(source) {
20
- * // Transform source format to InputNode
21
- * return { ... }
22
- * },
23
- * }
24
- * })
25
- *
26
- * // Instantiate:
27
- * const adapter = myAdapter({ validate: true })
28
- * ```
29
- */
30
- export function createAdapter<T extends AdapterFactoryOptions = AdapterFactoryOptions>(build: AdapterBuilder<T>): (options?: T['options']) => Adapter<T> {
31
- return (options) => build(options ?? ({} as T['options']))
32
- }