@kubb/core 5.0.0-alpha.9 → 5.0.0-beta.1

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 (64) hide show
  1. package/README.md +23 -20
  2. package/dist/PluginDriver-BXibeQk-.cjs +1036 -0
  3. package/dist/PluginDriver-BXibeQk-.cjs.map +1 -0
  4. package/dist/PluginDriver-DV3p2Hky.js +945 -0
  5. package/dist/PluginDriver-DV3p2Hky.js.map +1 -0
  6. package/dist/index.cjs +729 -1641
  7. package/dist/index.cjs.map +1 -1
  8. package/dist/index.d.ts +271 -225
  9. package/dist/index.js +713 -1609
  10. package/dist/index.js.map +1 -1
  11. package/dist/mocks.cjs +145 -0
  12. package/dist/mocks.cjs.map +1 -0
  13. package/dist/mocks.d.ts +80 -0
  14. package/dist/mocks.js +140 -0
  15. package/dist/mocks.js.map +1 -0
  16. package/dist/types-CuNocrbJ.d.ts +2148 -0
  17. package/package.json +51 -57
  18. package/src/FileManager.ts +115 -0
  19. package/src/FileProcessor.ts +86 -0
  20. package/src/Kubb.ts +207 -131
  21. package/src/PluginDriver.ts +325 -564
  22. package/src/constants.ts +20 -47
  23. package/src/createAdapter.ts +13 -6
  24. package/src/createKubb.ts +548 -0
  25. package/src/createRenderer.ts +57 -0
  26. package/src/createStorage.ts +13 -1
  27. package/src/defineGenerator.ts +77 -124
  28. package/src/defineLogger.ts +4 -2
  29. package/src/defineMiddleware.ts +62 -0
  30. package/src/defineParser.ts +44 -0
  31. package/src/definePlugin.ts +83 -0
  32. package/src/defineResolver.ts +418 -28
  33. package/src/devtools.ts +14 -14
  34. package/src/index.ts +13 -15
  35. package/src/mocks.ts +178 -0
  36. package/src/renderNode.ts +35 -0
  37. package/src/storages/fsStorage.ts +41 -11
  38. package/src/storages/memoryStorage.ts +4 -2
  39. package/src/types.ts +1031 -283
  40. package/src/utils/diagnostics.ts +4 -1
  41. package/src/utils/isInputPath.ts +10 -0
  42. package/src/utils/packageJSON.ts +50 -12
  43. package/dist/PluginDriver-BkFepPdm.d.ts +0 -1054
  44. package/dist/chunk-ByKO4r7w.cjs +0 -38
  45. package/dist/hooks.cjs +0 -103
  46. package/dist/hooks.cjs.map +0 -1
  47. package/dist/hooks.d.ts +0 -77
  48. package/dist/hooks.js +0 -98
  49. package/dist/hooks.js.map +0 -1
  50. package/src/build.ts +0 -418
  51. package/src/config.ts +0 -56
  52. package/src/createPlugin.ts +0 -28
  53. package/src/hooks/index.ts +0 -4
  54. package/src/hooks/useKubb.ts +0 -143
  55. package/src/hooks/useMode.ts +0 -11
  56. package/src/hooks/usePlugin.ts +0 -11
  57. package/src/hooks/usePluginDriver.ts +0 -11
  58. package/src/utils/FunctionParams.ts +0 -155
  59. package/src/utils/TreeNode.ts +0 -215
  60. package/src/utils/executeStrategies.ts +0 -81
  61. package/src/utils/formatters.ts +0 -56
  62. package/src/utils/getBarrelFiles.ts +0 -141
  63. package/src/utils/getConfigs.ts +0 -12
  64. package/src/utils/linters.ts +0 -25
@@ -1,15 +1,54 @@
1
+ import path from 'node:path'
1
2
  import { camelCase, pascalCase } from '@internals/utils'
2
- import { isOperationNode, isSchemaNode } from '@kubb/ast'
3
- import type { Node, OperationNode, SchemaNode } from '@kubb/ast/types'
4
- import type { PluginFactoryOptions, ResolveNameParams, ResolveOptionsContext } from './types.ts'
3
+ import type { FileNode, InputNode, Node, OperationNode, SchemaNode } from '@kubb/ast'
4
+ import { createFile, isOperationNode, isSchemaNode } from '@kubb/ast'
5
+ import { PluginDriver } from './PluginDriver.ts'
6
+ import type {
7
+ Config,
8
+ PluginFactoryOptions,
9
+ ResolveBannerContext,
10
+ ResolveOptionsContext,
11
+ Resolver,
12
+ ResolverContext,
13
+ ResolverFileParams,
14
+ ResolverPathParams,
15
+ } from './types.ts'
5
16
 
6
17
  /**
7
18
  * Builder type for the plugin-specific resolver fields.
8
- * `default` and `resolveOptions` are optional — built-in fallbacks are used when omitted.
19
+ *
20
+ * `default`, `resolveOptions`, `resolvePath`, `resolveFile`, `resolveBanner`, and `resolveFooter`
21
+ * are optional — built-in fallbacks are injected when omitted.
22
+ *
23
+ * The builder receives `ctx` — a reference to the fully assembled resolver — so methods can
24
+ * call sibling resolver methods without using `this`. Because `ctx` is captured by the closure
25
+ * and the resolver is populated after the builder runs, `ctx` correctly reflects any overrides
26
+ * that were applied by the builder itself.
9
27
  */
10
- type ResolverBuilder<T extends PluginFactoryOptions> = () => Omit<T['resolver'], 'default' | 'resolveOptions'> &
11
- Partial<Pick<T['resolver'], 'default' | 'resolveOptions'>> &
12
- ThisType<T['resolver']>
28
+ type ResolverBuilder<T extends PluginFactoryOptions> = (ctx: T['resolver']) => Omit<
29
+ T['resolver'],
30
+ 'default' | 'resolveOptions' | 'resolvePath' | 'resolveFile' | 'resolveBanner' | 'resolveFooter' | 'name' | 'pluginName'
31
+ > &
32
+ Partial<Pick<T['resolver'], 'default' | 'resolveOptions' | 'resolvePath' | 'resolveFile' | 'resolveBanner' | 'resolveFooter'>> & {
33
+ name: string
34
+ pluginName: T['name']
35
+ }
36
+
37
+ // String patterns are compiled lazily and cached — the same filter is reused for every node.
38
+ const stringPatternCache = new Map<string, RegExp>()
39
+
40
+ function testPattern(value: string, pattern: string | RegExp): boolean {
41
+ if (typeof pattern === 'string') {
42
+ let regex = stringPatternCache.get(pattern)
43
+ if (!regex) {
44
+ regex = new RegExp(pattern)
45
+ stringPatternCache.set(pattern, regex)
46
+ }
47
+ return regex.test(value)
48
+ }
49
+ // Use .match() for user-supplied RegExp to preserve semantics regardless of `g`/`y` flags.
50
+ return value.match(pattern) !== null
51
+ }
13
52
 
14
53
  /**
15
54
  * Checks if an operation matches a pattern for a given filter type (`tag`, `operationId`, `path`, `method`).
@@ -17,13 +56,15 @@ type ResolverBuilder<T extends PluginFactoryOptions> = () => Omit<T['resolver'],
17
56
  function matchesOperationPattern(node: OperationNode, type: string, pattern: string | RegExp): boolean {
18
57
  switch (type) {
19
58
  case 'tag':
20
- return node.tags.some((tag) => !!tag.match(pattern))
59
+ return node.tags.some((tag) => testPattern(tag, pattern))
21
60
  case 'operationId':
22
- return !!node.operationId.match(pattern)
61
+ return testPattern(node.operationId, pattern)
23
62
  case 'path':
24
- return !!node.path.match(pattern)
63
+ return testPattern(node.path, pattern)
25
64
  case 'method':
26
- return !!(node.method.toLowerCase() as string).match(pattern)
65
+ return testPattern(node.method.toLowerCase(), pattern)
66
+ case 'contentType':
67
+ return node.requestBody?.content?.some((c) => testPattern(c.contentType, pattern)) ?? false
27
68
  default:
28
69
  return false
29
70
  }
@@ -31,21 +72,26 @@ function matchesOperationPattern(node: OperationNode, type: string, pattern: str
31
72
 
32
73
  /**
33
74
  * Checks if a schema matches a pattern for a given filter type (`schemaName`).
75
+ *
34
76
  * Returns `null` when the filter type doesn't apply to schemas.
35
77
  */
36
78
  function matchesSchemaPattern(node: SchemaNode, type: string, pattern: string | RegExp): boolean | null {
37
79
  switch (type) {
38
80
  case 'schemaName':
39
- return node.name ? !!node.name.match(pattern) : false
81
+ return node.name ? testPattern(node.name, pattern) : false
40
82
  default:
41
83
  return null
42
84
  }
43
85
  }
44
86
 
45
87
  /**
46
- * Default name resolver `camelCase` for most types, `PascalCase` for `type`.
88
+ * Default name resolver used by `defineResolver`.
89
+ *
90
+ * - `camelCase` for `function` and `file` types.
91
+ * - `PascalCase` for `type`.
92
+ * - `camelCase` for everything else.
47
93
  */
48
- function defaultResolver(name: ResolveNameParams['name'], type: ResolveNameParams['type']): string {
94
+ function defaultResolver(name: string, type?: 'file' | 'function' | 'type' | 'const'): string {
49
95
  let resolvedName = camelCase(name)
50
96
 
51
97
  if (type === 'file' || type === 'function') {
@@ -62,8 +108,27 @@ function defaultResolver(name: ResolveNameParams['name'], type: ResolveNameParam
62
108
  }
63
109
 
64
110
  /**
65
- * Default option resolver — applies include/exclude filters and merges any matching override options.
66
- * Returns `null` when the node is filtered out.
111
+ * Default option resolver — applies include/exclude filters and merges matching override options.
112
+ *
113
+ * Returns `null` when the node is filtered out by an `exclude` rule or not matched by any `include` rule.
114
+ *
115
+ * @example Include/exclude filtering
116
+ * ```ts
117
+ * const options = defaultResolveOptions(operationNode, {
118
+ * options: { output: 'types' },
119
+ * exclude: [{ type: 'tag', pattern: 'internal' }],
120
+ * })
121
+ * // → null when node has tag 'internal'
122
+ * ```
123
+ *
124
+ * @example Override merging
125
+ * ```ts
126
+ * const options = defaultResolveOptions(operationNode, {
127
+ * options: { enumType: 'asConst' },
128
+ * override: [{ type: 'operationId', pattern: 'listPets', options: { enumType: 'enum' } }],
129
+ * })
130
+ * // → { enumType: 'enum' } when operationId matches
131
+ * ```
67
132
  */
68
133
  export function defaultResolveOptions<TOptions>(
69
134
  node: Node,
@@ -106,26 +171,351 @@ export function defaultResolveOptions<TOptions>(
106
171
  }
107
172
 
108
173
  /**
109
- * Defines a resolver for a plugin, with built-in defaults for name casing and include/exclude/override filtering.
110
- * Override `default` or `resolveOptions` in the builder to customize the behavior.
174
+ * Default path resolver used by `defineResolver`.
175
+ *
176
+ * - Returns the output directory in `single` mode.
177
+ * - Resolves into a tag- or path-based subdirectory when `group` and a `tag`/`path` value are provided.
178
+ * - Falls back to a flat `output/baseName` path otherwise.
179
+ *
180
+ * A custom `group.name` function overrides the default subdirectory naming.
181
+ * For `tag` groups the default is `${camelCase(tag)}Controller`.
182
+ * For `path` groups the default is the first path segment after `/`.
183
+ *
184
+ * @example Flat output
185
+ * ```ts
186
+ * defaultResolvePath({ baseName: 'petTypes.ts' }, { root: '/src', output: { path: 'types' } })
187
+ * // → '/src/types/petTypes.ts'
188
+ * ```
189
+ *
190
+ * @example Tag-based grouping
191
+ * ```ts
192
+ * defaultResolvePath(
193
+ * { baseName: 'petTypes.ts', tag: 'pets' },
194
+ * { root: '/src', output: { path: 'types' }, group: { type: 'tag' } },
195
+ * )
196
+ * // → '/src/types/petsController/petTypes.ts'
197
+ * ```
198
+ *
199
+ * @example Path-based grouping
200
+ * ```ts
201
+ * defaultResolvePath(
202
+ * { baseName: 'petTypes.ts', path: '/pets/list' },
203
+ * { root: '/src', output: { path: 'types' }, group: { type: 'path' } },
204
+ * )
205
+ * // → '/src/types/pets/petTypes.ts'
206
+ * ```
111
207
  *
112
- * @example
113
- * export const resolver = defineResolver<PluginTs>(() => ({
114
- * resolveName(name) {
115
- * return this.default(name, 'function')
208
+ * @example Single-file mode
209
+ * ```ts
210
+ * defaultResolvePath(
211
+ * { baseName: 'petTypes.ts', pathMode: 'single' },
212
+ * { root: '/src', output: { path: 'types' } },
213
+ * )
214
+ * // → '/src/types'
215
+ * ```
216
+ */
217
+ export function defaultResolvePath({ baseName, pathMode, tag, path: groupPath }: ResolverPathParams, { root, output, group }: ResolverContext): string {
218
+ const mode = pathMode ?? PluginDriver.getMode(path.resolve(root, output.path))
219
+
220
+ if (mode === 'single') {
221
+ return path.resolve(root, output.path)
222
+ }
223
+
224
+ let result: string
225
+
226
+ if (group && (groupPath || tag)) {
227
+ const groupValue = group.type === 'path' ? groupPath! : tag!
228
+ const defaultName =
229
+ group.type === 'tag'
230
+ ? ({ group: g }: { group: string }) => `${camelCase(g)}Controller`
231
+ : ({ group: g }: { group: string }) => {
232
+ // Strip traversal components (empty, '.', '..') before taking the first meaningful segment.
233
+ // When every segment is a traversal component (e.g. '../../') we fall back to '' so the
234
+ // file is placed directly in the output root — the boundary check below ensures safety.
235
+ const segment = g.split('/').filter((s) => s !== '' && s !== '.' && s !== '..')[0]
236
+ return segment ? camelCase(segment) : ''
237
+ }
238
+ const resolveName = group.name ?? defaultName
239
+ result = path.resolve(root, output.path, resolveName({ group: groupValue }), baseName)
240
+ } else {
241
+ result = path.resolve(root, output.path, baseName)
242
+ }
243
+
244
+ // Ensure the resolved path stays within the configured output directory.
245
+ // This prevents path traversal from malicious OpenAPI specs or custom group.name functions.
246
+ // `result === outputDir` is intentionally permitted: it matches single-file mode paths and
247
+ // edge cases where baseName resolves to the output directory itself.
248
+ const outputDir = path.resolve(root, output.path)
249
+ const outputDirWithSep = outputDir.endsWith(path.sep) ? outputDir : `${outputDir}${path.sep}`
250
+ if (result !== outputDir && !result.startsWith(outputDirWithSep)) {
251
+ throw new Error(
252
+ `[Kubb] Resolved path "${result}" is outside the output directory "${outputDir}". ` +
253
+ 'This may indicate a path traversal attempt in the OpenAPI specification or a misconfigured group.name function.',
254
+ )
255
+ }
256
+
257
+ return result
258
+ }
259
+
260
+ /**
261
+ * Default file resolver used by `defineResolver`.
262
+ *
263
+ * Resolves a `FileNode` by combining name resolution (`resolver.default`) with
264
+ * path resolution (`resolver.resolvePath`). The resolved file always has empty
265
+ * `sources`, `imports`, and `exports` arrays — consumers populate those separately.
266
+ *
267
+ * In `single` mode the name is omitted and the file sits directly in the output directory.
268
+ *
269
+ * @example Resolve a schema file
270
+ * ```ts
271
+ * const file = defaultResolveFile(
272
+ * { name: 'pet', extname: '.ts' },
273
+ * { root: '/src', output: { path: 'types' } },
274
+ * resolver,
275
+ * )
276
+ * // → { baseName: 'pet.ts', path: '/src/types/pet.ts', sources: [], ... }
277
+ * ```
278
+ *
279
+ * @example Resolve an operation file with tag grouping
280
+ * ```ts
281
+ * const file = defaultResolveFile(
282
+ * { name: 'listPets', extname: '.ts', tag: 'pets' },
283
+ * { root: '/src', output: { path: 'types' }, group: { type: 'tag' } },
284
+ * resolver,
285
+ * )
286
+ * // → { baseName: 'listPets.ts', path: '/src/types/petsController/listPets.ts', ... }
287
+ * ```
288
+ */
289
+ export function defaultResolveFile({ name, extname, tag, path: groupPath }: ResolverFileParams, context: ResolverContext, ctx: Resolver): FileNode {
290
+ const pathMode = PluginDriver.getMode(path.resolve(context.root, context.output.path))
291
+ const resolvedName = pathMode === 'single' ? '' : ctx.default(name, 'file')
292
+ const baseName = `${resolvedName}${extname}` as FileNode['baseName']
293
+ const filePath = ctx.resolvePath({ baseName, pathMode, tag, path: groupPath }, context)
294
+
295
+ return createFile({
296
+ path: filePath,
297
+ baseName: path.basename(filePath) as `${string}.${string}`,
298
+ meta: {
299
+ pluginName: ctx.pluginName,
300
+ },
301
+ sources: [],
302
+ imports: [],
303
+ exports: [],
304
+ })
305
+ }
306
+
307
+ /**
308
+ * Generates the default "Generated by Kubb" banner from config and optional node metadata.
309
+ */
310
+ export function buildDefaultBanner({
311
+ title,
312
+ description,
313
+ version,
314
+ config,
315
+ }: {
316
+ title?: string
317
+ description?: string
318
+ version?: string
319
+ config: Config
320
+ }): string {
321
+ try {
322
+ let source = ''
323
+ if (Array.isArray(config.input)) {
324
+ const first = config.input[0]
325
+ if (first && 'path' in first) {
326
+ source = path.basename(first.path)
327
+ }
328
+ } else if ('path' in config.input) {
329
+ source = path.basename(config.input.path)
330
+ } else if ('data' in config.input) {
331
+ source = 'text content'
332
+ }
333
+
334
+ let banner = '/**\n* Generated by Kubb (https://kubb.dev/).\n* Do not edit manually.\n'
335
+
336
+ if (config.output.defaultBanner === 'simple') {
337
+ banner += '*/\n'
338
+ return banner
339
+ }
340
+
341
+ if (source) {
342
+ banner += `* Source: ${source}\n`
343
+ }
344
+
345
+ if (title) {
346
+ banner += `* Title: ${title}\n`
347
+ }
348
+
349
+ if (description) {
350
+ const formattedDescription = description.replace(/\n/gm, '\n* ')
351
+ banner += `* Description: ${formattedDescription}\n`
352
+ }
353
+
354
+ if (version) {
355
+ banner += `* OpenAPI spec version: ${version}\n`
356
+ }
357
+
358
+ banner += '*/\n'
359
+ return banner
360
+ } catch (_error) {
361
+ return '/**\n* Generated by Kubb (https://kubb.dev/).\n* Do not edit manually.\n*/'
362
+ }
363
+ }
364
+
365
+ /**
366
+ * Default banner resolver — returns the banner string for a generated file.
367
+ *
368
+ * A user-supplied `output.banner` overrides the default Kubb "Generated by Kubb" notice.
369
+ * When no `output.banner` is set, the Kubb notice is used (including `title` and `version`
370
+ * from the OAS spec when a `node` is provided).
371
+ *
372
+ * - When `output.banner` is a function and `node` is provided, returns `output.banner(node)`.
373
+ * - When `output.banner` is a function and `node` is absent, falls back to the Kubb notice.
374
+ * - When `output.banner` is a string, returns it directly.
375
+ * - When `config.output.defaultBanner` is `false`, returns `undefined`.
376
+ * - Otherwise returns the Kubb "Generated by Kubb" notice.
377
+ *
378
+ * @example String banner overrides default
379
+ * ```ts
380
+ * defaultResolveBanner(undefined, { output: { banner: '// my banner' }, config })
381
+ * // → '// my banner'
382
+ * ```
383
+ *
384
+ * @example Function banner with node
385
+ * ```ts
386
+ * defaultResolveBanner(inputNode, { output: { banner: (node) => `// v${node.version}` }, config })
387
+ * // → '// v3.0.0'
388
+ * ```
389
+ *
390
+ * @example No user banner — Kubb notice with OAS metadata
391
+ * ```ts
392
+ * defaultResolveBanner(inputNode, { config })
393
+ * // → '/** Generated by Kubb ... Title: Pet Store ... *\/'
394
+ * ```
395
+ *
396
+ * @example Disabled default banner
397
+ * ```ts
398
+ * defaultResolveBanner(undefined, { config: { output: { defaultBanner: false }, ...config } })
399
+ * // → undefined
400
+ * ```
401
+ */
402
+ export function defaultResolveBanner(node: InputNode | undefined, { output, config }: ResolveBannerContext): string | undefined {
403
+ if (typeof output?.banner === 'function') {
404
+ return output.banner(node)
405
+ }
406
+
407
+ if (typeof output?.banner === 'string') {
408
+ return output.banner
409
+ }
410
+
411
+ if (config.output.defaultBanner === false) {
412
+ return undefined
413
+ }
414
+
415
+ return buildDefaultBanner({
416
+ title: node?.meta?.title,
417
+ version: node?.meta?.version,
418
+ config,
419
+ })
420
+ }
421
+
422
+ /**
423
+ * Default footer resolver — returns the footer string for a generated file.
424
+ *
425
+ * - When `output.footer` is a function and `node` is provided, calls it with the node.
426
+ * - When `output.footer` is a function and `node` is absent, returns `undefined`.
427
+ * - When `output.footer` is a string, returns it directly.
428
+ * - Otherwise returns `undefined`.
429
+ *
430
+ * @example String footer
431
+ * ```ts
432
+ * defaultResolveFooter(undefined, { output: { footer: '// end of file' }, config })
433
+ * // → '// end of file'
434
+ * ```
435
+ *
436
+ * @example Function footer with node
437
+ * ```ts
438
+ * defaultResolveFooter(inputNode, { output: { footer: (node) => `// ${node.title}` }, config })
439
+ * // → '// Pet Store'
440
+ * ```
441
+ */
442
+ export function defaultResolveFooter(node: InputNode | undefined, { output }: ResolveBannerContext): string | undefined {
443
+ if (typeof output?.footer === 'function') {
444
+ return node ? output.footer(node) : undefined
445
+ }
446
+ if (typeof output?.footer === 'string') {
447
+ return output.footer
448
+ }
449
+ return undefined
450
+ }
451
+
452
+ /**
453
+ * Defines a resolver for a plugin, injecting built-in defaults for name casing,
454
+ * include/exclude/override filtering, path resolution, and file construction.
455
+ *
456
+ * All four defaults can be overridden by providing them in the builder function:
457
+ * - `default` — name casing strategy (camelCase / PascalCase)
458
+ * - `resolveOptions` — include/exclude/override filtering
459
+ * - `resolvePath` — output path computation
460
+ * - `resolveFile` — full `FileNode` construction
461
+ *
462
+ * The builder receives `ctx` — a reference to the assembled resolver — so methods can
463
+ * call sibling resolver methods using `ctx` instead of `this`.
464
+ *
465
+ * @example Basic resolver with naming helpers
466
+ * ```ts
467
+ * export const resolver = defineResolver<PluginTs>((ctx) => ({
468
+ * name: 'default',
469
+ * resolveName(node) {
470
+ * return ctx.default(node.name, 'function')
116
471
  * },
117
- * resolveTypedName(name) {
118
- * return this.default(name, 'type')
472
+ * resolveTypedName(node) {
473
+ * return ctx.default(node.name, 'type')
119
474
  * },
475
+ * }))
476
+ * ```
477
+ *
478
+ * @example Override resolvePath for a custom output structure
479
+ * ```ts
480
+ * export const resolver = defineResolver<PluginTs>((_ctx) => ({
481
+ * name: 'custom',
482
+ * resolvePath({ baseName }, { root, output }) {
483
+ * return path.resolve(root, output.path, 'generated', baseName)
484
+ * },
485
+ * }))
486
+ * ```
487
+ *
488
+ * @example Use ctx.default inside a helper
489
+ * ```ts
490
+ * export const resolver = defineResolver<PluginTs>((ctx) => ({
491
+ * name: 'default',
120
492
  * resolveParamName(node, param) {
121
- * return this.resolveName(`${node.operationId} ${param.in} ${param.name}`)
493
+ * return ctx.default(`${node.operationId} ${param.in} ${param.name}`, 'type')
122
494
  * },
123
495
  * }))
496
+ * ```
124
497
  */
125
498
  export function defineResolver<T extends PluginFactoryOptions>(build: ResolverBuilder<T>): T['resolver'] {
126
- return {
499
+ // Create the resolver shell first. When `build(resolver)` executes below, `resolver` is
500
+ // still empty, but methods returned by the builder capture it by reference. By the time
501
+ // those methods are actually called, `Object.assign` will have already populated all
502
+ // properties (including any overrides from the builder itself).
503
+ const resolver = {} as T['resolver']
504
+
505
+ Object.assign(resolver, {
127
506
  default: defaultResolver,
128
507
  resolveOptions: defaultResolveOptions,
129
- ...build(),
130
- } as T['resolver']
508
+ resolvePath: defaultResolvePath,
509
+ // Wire the default resolveFile implementation with a wrapper that passes resolver as ctx.
510
+ // Unlike other defaults which can be assigned directly, defaultResolveFile requires the
511
+ // resolver as its third parameter.
512
+ resolveFile: (params: ResolverFileParams, context: ResolverContext) => defaultResolveFile(params, context, resolver as Resolver),
513
+ resolveBanner: defaultResolveBanner,
514
+ resolveFooter: defaultResolveFooter,
515
+ // Builder overrides are applied last. Any method in the builder can call
516
+ // ctx.xxx() and will see the fully merged resolver (including its own overrides).
517
+ ...build(resolver),
518
+ })
519
+
520
+ return resolver
131
521
  }
package/src/devtools.ts CHANGED
@@ -1,10 +1,10 @@
1
- import type { RootNode } from '@kubb/ast/types'
1
+ import type { InputNode } from '@kubb/ast'
2
2
  import { deflateSync, inflateSync } from 'fflate'
3
3
  import { x } from 'tinyexec'
4
4
  import type { DevtoolsOptions } from './types.ts'
5
5
 
6
6
  /**
7
- * Encodes a `RootNode` as a compressed, URL-safe string.
7
+ * Encodes an `InputNode` as a compressed, URL-safe string.
8
8
  *
9
9
  * The JSON representation is deflate-compressed with {@link deflateSync} before
10
10
  * base64url encoding, which typically reduces payload size by 70–80 % and
@@ -12,41 +12,41 @@ import type { DevtoolsOptions } from './types.ts'
12
12
  *
13
13
  * Use {@link decodeAst} to reverse.
14
14
  */
15
- export function encodeAst(root: RootNode): string {
16
- const compressed = deflateSync(new TextEncoder().encode(JSON.stringify(root)))
15
+ export function encodeAst(input: InputNode): string {
16
+ const compressed = deflateSync(new TextEncoder().encode(JSON.stringify(input)))
17
17
  return Buffer.from(compressed).toString('base64url')
18
18
  }
19
19
 
20
20
  /**
21
- * Decodes a `RootNode` from a string produced by {@link encodeAst}.
21
+ * Decodes an `InputNode` from a string produced by {@link encodeAst}.
22
22
  *
23
23
  * Works in both Node.js and the browser — no streaming APIs required.
24
24
  */
25
- export function decodeAst(encoded: string): RootNode {
25
+ export function decodeAst(encoded: string): InputNode {
26
26
  const bytes = Buffer.from(encoded, 'base64url')
27
- return JSON.parse(new TextDecoder().decode(inflateSync(bytes))) as RootNode
27
+ return JSON.parse(new TextDecoder().decode(inflateSync(bytes))) as InputNode
28
28
  }
29
29
 
30
30
  /**
31
- * Constructs the Kubb Studio URL for the given `RootNode`.
31
+ * Constructs the Kubb Studio URL for the given `InputNode`.
32
32
  * When `options.ast` is `true`, navigates to the AST inspector (`/ast`).
33
- * The `root` is encoded and attached as the `?root=` query parameter so Studio
33
+ * The `input` is encoded and attached as the `?root=` query parameter so Studio
34
34
  * can decode and render it without a round-trip to any server.
35
35
  */
36
- export function getStudioUrl(root: RootNode, studioUrl: string, options: DevtoolsOptions = {}): string {
36
+ export function getStudioUrl(input: InputNode, studioUrl: string, options: DevtoolsOptions = {}): string {
37
37
  const baseUrl = studioUrl.replace(/\/$/, '')
38
38
  const path = options.ast ? '/ast' : ''
39
39
 
40
- return `${baseUrl}${path}?root=${encodeAst(root)}`
40
+ return `${baseUrl}${path}?root=${encodeAst(input)}`
41
41
  }
42
42
 
43
43
  /**
44
- * Opens the Kubb Studio URL for the given `RootNode` in the default browser —
44
+ * Opens the Kubb Studio URL for the given `InputNode` in the default browser —
45
45
  *
46
46
  * Falls back to printing the URL if the browser cannot be launched.
47
47
  */
48
- export async function openInStudio(root: RootNode, studioUrl: string, options: DevtoolsOptions = {}): Promise<void> {
49
- const url = getStudioUrl(root, studioUrl, options)
48
+ export async function openInStudio(input: InputNode, studioUrl: string, options: DevtoolsOptions = {}): Promise<void> {
49
+ const url = getStudioUrl(input, studioUrl, options)
50
50
 
51
51
  const cmd = process.platform === 'win32' ? 'cmd' : process.platform === 'darwin' ? 'open' : 'xdg-open'
52
52
  const args = process.platform === 'win32' ? ['/c', 'start', '', url] : [url]
package/src/index.ts CHANGED
@@ -1,22 +1,20 @@
1
- export { definePrinter } from '@kubb/ast'
2
- export { build, build as default, safeBuild, setup } from './build.ts'
3
- export { type CLIOptions, type ConfigInput, defineConfig, isInputPath } from './config.ts'
4
- export { formatters, linters, logLevel } from './constants.ts'
1
+ export { AsyncEventEmitter, URLPath } from '@internals/utils'
2
+ export * as ast from '@kubb/ast'
3
+ export { logLevel } from './constants.ts'
5
4
  export { createAdapter } from './createAdapter.ts'
6
- export { createPlugin } from './createPlugin.ts'
5
+ export { createKubb } from './createKubb.ts'
6
+ export { createRenderer } from './createRenderer.ts'
7
7
  export { createStorage } from './createStorage.ts'
8
8
  export { defineGenerator } from './defineGenerator.ts'
9
9
  export { defineLogger } from './defineLogger.ts'
10
- export { defaultResolveOptions, defineResolver } from './defineResolver.ts'
11
- export { getMode, PluginDriver } from './PluginDriver.ts'
10
+ export { defineMiddleware } from './defineMiddleware.ts'
11
+ export { defineParser } from './defineParser.ts'
12
+ export { definePlugin } from './definePlugin.ts'
13
+ export { defineResolver } from './defineResolver.ts'
14
+ export { FileManager } from './FileManager.ts'
15
+ export { FileProcessor } from './FileProcessor.ts'
16
+ export { PluginDriver } from './PluginDriver.ts'
12
17
  export { fsStorage } from './storages/fsStorage.ts'
13
18
  export { memoryStorage } from './storages/memoryStorage.ts'
14
19
  export * from './types.ts'
15
- export type { FunctionParamsAST } from './utils/FunctionParams.ts'
16
- export { FunctionParams } from './utils/FunctionParams.ts'
17
- export { detectFormatter } from './utils/formatters.ts'
18
- export type { FileMetaBase } from './utils/getBarrelFiles.ts'
19
- export { getBarrelFiles } from './utils/getBarrelFiles.ts'
20
- export { getConfigs } from './utils/getConfigs.ts'
21
- export { detectLinter } from './utils/linters.ts'
22
- export { satisfiesDependency } from './utils/packageJSON.ts'
20
+ export { isInputPath } from './utils/isInputPath.ts'