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

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 (62) hide show
  1. package/README.md +13 -40
  2. package/dist/PluginDriver-Cu1Kj9S-.cjs +1075 -0
  3. package/dist/PluginDriver-Cu1Kj9S-.cjs.map +1 -0
  4. package/dist/PluginDriver-D8Z0Htid.js +978 -0
  5. package/dist/PluginDriver-D8Z0Htid.js.map +1 -0
  6. package/dist/createKubb-ALdb8lmq.d.ts +2082 -0
  7. package/dist/index.cjs +747 -1667
  8. package/dist/index.cjs.map +1 -1
  9. package/dist/index.d.ts +175 -269
  10. package/dist/index.js +734 -1638
  11. package/dist/index.js.map +1 -1
  12. package/dist/mocks.cjs +145 -0
  13. package/dist/mocks.cjs.map +1 -0
  14. package/dist/mocks.d.ts +80 -0
  15. package/dist/mocks.js +140 -0
  16. package/dist/mocks.js.map +1 -0
  17. package/package.json +47 -60
  18. package/src/FileManager.ts +115 -0
  19. package/src/FileProcessor.ts +86 -0
  20. package/src/PluginDriver.ts +355 -561
  21. package/src/constants.ts +21 -48
  22. package/src/createAdapter.ts +88 -5
  23. package/src/createKubb.ts +1266 -0
  24. package/src/createRenderer.ts +57 -0
  25. package/src/createStorage.ts +13 -1
  26. package/src/defineGenerator.ts +160 -119
  27. package/src/defineLogger.ts +46 -5
  28. package/src/defineMiddleware.ts +62 -0
  29. package/src/defineParser.ts +44 -0
  30. package/src/definePlugin.ts +379 -0
  31. package/src/defineResolver.ts +548 -25
  32. package/src/devtools.ts +22 -15
  33. package/src/index.ts +13 -15
  34. package/src/mocks.ts +177 -0
  35. package/src/storages/fsStorage.ts +13 -8
  36. package/src/storages/memoryStorage.ts +4 -2
  37. package/src/types.ts +40 -547
  38. package/dist/PluginDriver-BkFepPdm.d.ts +0 -1054
  39. package/dist/chunk-ByKO4r7w.cjs +0 -38
  40. package/dist/hooks.cjs +0 -103
  41. package/dist/hooks.cjs.map +0 -1
  42. package/dist/hooks.d.ts +0 -77
  43. package/dist/hooks.js +0 -98
  44. package/dist/hooks.js.map +0 -1
  45. package/src/Kubb.ts +0 -224
  46. package/src/build.ts +0 -418
  47. package/src/config.ts +0 -56
  48. package/src/createPlugin.ts +0 -28
  49. package/src/hooks/index.ts +0 -4
  50. package/src/hooks/useKubb.ts +0 -143
  51. package/src/hooks/useMode.ts +0 -11
  52. package/src/hooks/usePlugin.ts +0 -11
  53. package/src/hooks/usePluginDriver.ts +0 -11
  54. package/src/utils/FunctionParams.ts +0 -155
  55. package/src/utils/TreeNode.ts +0 -215
  56. package/src/utils/diagnostics.ts +0 -15
  57. package/src/utils/executeStrategies.ts +0 -81
  58. package/src/utils/formatters.ts +0 -56
  59. package/src/utils/getBarrelFiles.ts +0 -141
  60. package/src/utils/getConfigs.ts +0 -12
  61. package/src/utils/linters.ts +0 -25
  62. package/src/utils/packageJSON.ts +0 -61
@@ -1,15 +1,193 @@
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 type { PluginFactoryOptions } from './definePlugin.ts'
6
+ import { getMode } from './definePlugin.ts'
7
+ import type { Config, Group, Output } from './types.ts'
8
+
9
+ /**
10
+ * Type/string pattern filter for include/exclude/override matching.
11
+ */
12
+ type PatternFilter = {
13
+ type: string
14
+ pattern: string | RegExp
15
+ }
16
+
17
+ /**
18
+ * Pattern filter with partial option overrides applied when the pattern matches.
19
+ */
20
+ type PatternOverride<TOptions> = PatternFilter & {
21
+ options: Omit<Partial<TOptions>, 'override'>
22
+ }
23
+
24
+ /**
25
+ * Context for resolving filtered options for a given operation or schema node.
26
+ *
27
+ * @internal
28
+ */
29
+ export type ResolveOptionsContext<TOptions> = {
30
+ options: TOptions
31
+ exclude?: Array<PatternFilter>
32
+ include?: Array<PatternFilter>
33
+ override?: Array<PatternOverride<TOptions>>
34
+ }
35
+
36
+ /**
37
+ * Base constraint for all plugin resolver objects.
38
+ *
39
+ * `default`, `resolveOptions`, `resolvePath`, `resolveFile`, `resolveBanner`, and `resolveFooter`
40
+ * are injected automatically by `defineResolver` — extend this type to add custom resolution methods.
41
+ *
42
+ * @example
43
+ * ```ts
44
+ * type MyResolver = Resolver & {
45
+ * resolveName(node: SchemaNode): string
46
+ * resolveTypedName(node: SchemaNode): string
47
+ * }
48
+ * ```
49
+ */
50
+ export type Resolver = {
51
+ name: string
52
+ pluginName: string
53
+ default(name: string, type?: 'file' | 'function' | 'type' | 'const'): string
54
+ resolveOptions<TOptions>(node: Node, context: ResolveOptionsContext<TOptions>): TOptions | null
55
+ resolvePath(params: ResolverPathParams, context: ResolverContext): string
56
+ resolveFile(params: ResolverFileParams, context: ResolverContext): FileNode
57
+ resolveBanner(node: InputNode | null, context: ResolveBannerContext): string | undefined
58
+ resolveFooter(node: InputNode | null, context: ResolveBannerContext): string | undefined
59
+ }
60
+
61
+ /**
62
+ * File-specific parameters for `Resolver.resolvePath`.
63
+ *
64
+ * Pass alongside a `ResolverContext` to identify which file to resolve.
65
+ * Provide `tag` for tag-based grouping or `path` for path-based grouping.
66
+ *
67
+ * @example
68
+ * ```ts
69
+ * resolver.resolvePath(
70
+ * { baseName: 'petTypes.ts', tag: 'pets' },
71
+ * { root: '/src', output: { path: 'types' }, group: { type: 'tag' } },
72
+ * )
73
+ * // → '/src/types/petsController/petTypes.ts'
74
+ * ```
75
+ */
76
+ export type ResolverPathParams = {
77
+ baseName: FileNode['baseName']
78
+ pathMode?: 'single' | 'split'
79
+ /**
80
+ * Tag value used when `group.type === 'tag'`.
81
+ */
82
+ tag?: string
83
+ /**
84
+ * Path value used when `group.type === 'path'`.
85
+ */
86
+ path?: string
87
+ }
88
+
89
+ /**
90
+ * Shared context passed as the second argument to `Resolver.resolvePath` and `Resolver.resolveFile`.
91
+ *
92
+ * Describes where on disk output is rooted, which output config is active, and the optional
93
+ * grouping strategy that controls subdirectory layout.
94
+ *
95
+ * @example
96
+ * ```ts
97
+ * const context: ResolverContext = {
98
+ * root: config.root,
99
+ * output,
100
+ * group,
101
+ * }
102
+ * ```
103
+ */
104
+ export type ResolverContext = {
105
+ root: string
106
+ output: Output
107
+ group?: Group
108
+ /**
109
+ * Plugin name used to populate `meta.pluginName` on the resolved file.
110
+ */
111
+ pluginName?: string
112
+ }
113
+
114
+ /**
115
+ * File-specific parameters for `Resolver.resolveFile`.
116
+ *
117
+ * Pass alongside a `ResolverContext` to fully describe the file to resolve.
118
+ * `tag` and `path` are used only when a matching `group` is present in the context.
119
+ *
120
+ * @example
121
+ * ```ts
122
+ * resolver.resolveFile(
123
+ * { name: 'listPets', extname: '.ts', tag: 'pets' },
124
+ * { root: '/src', output: { path: 'types' }, group: { type: 'tag' } },
125
+ * )
126
+ * // → { baseName: 'listPets.ts', path: '/src/types/petsController/listPets.ts', ... }
127
+ * ```
128
+ */
129
+ export type ResolverFileParams = {
130
+ name: string
131
+ extname: FileNode['extname']
132
+ /**
133
+ * Tag value used when `group.type === 'tag'`.
134
+ */
135
+ tag?: string
136
+ /**
137
+ * Path value used when `group.type === 'path'`.
138
+ */
139
+ path?: string
140
+ }
141
+
142
+ /**
143
+ * Context passed to `Resolver.resolveBanner` and `Resolver.resolveFooter`.
144
+ *
145
+ * `output` is optional — not every plugin configures a banner/footer.
146
+ * `config` carries the global Kubb config, used to derive the default Kubb banner.
147
+ *
148
+ * @example
149
+ * ```ts
150
+ * resolver.resolveBanner(inputNode, { output: { banner: '// generated' }, config })
151
+ * // → '// generated'
152
+ * ```
153
+ */
154
+ export type ResolveBannerContext = {
155
+ output?: Pick<Output, 'banner' | 'footer'>
156
+ config: Config
157
+ }
5
158
 
6
159
  /**
7
160
  * Builder type for the plugin-specific resolver fields.
8
- * `default` and `resolveOptions` are optional — built-in fallbacks are used when omitted.
161
+ *
162
+ * `default`, `resolveOptions`, `resolvePath`, `resolveFile`, `resolveBanner`, and `resolveFooter`
163
+ * are optional — built-in fallbacks are injected when omitted.
164
+ *
165
+ * Methods in the returned object can call sibling resolver methods via `this`.
9
166
  */
10
- type ResolverBuilder<T extends PluginFactoryOptions> = () => Omit<T['resolver'], 'default' | 'resolveOptions'> &
11
- Partial<Pick<T['resolver'], 'default' | 'resolveOptions'>> &
12
- ThisType<T['resolver']>
167
+ type ResolverBuilder<T extends PluginFactoryOptions> = () => Omit<
168
+ T['resolver'],
169
+ 'default' | 'resolveOptions' | 'resolvePath' | 'resolveFile' | 'resolveBanner' | 'resolveFooter' | 'name' | 'pluginName'
170
+ > &
171
+ Partial<Pick<T['resolver'], 'default' | 'resolveOptions' | 'resolvePath' | 'resolveFile' | 'resolveBanner' | 'resolveFooter'>> & {
172
+ name: string
173
+ pluginName: T['name']
174
+ } & ThisType<T['resolver']>
175
+
176
+ // String patterns are compiled lazily and cached — the same filter is reused for every node.
177
+ const stringPatternCache = new Map<string, RegExp>()
178
+
179
+ function testPattern(value: string, pattern: string | RegExp): boolean {
180
+ if (typeof pattern === 'string') {
181
+ let regex = stringPatternCache.get(pattern)
182
+ if (!regex) {
183
+ regex = new RegExp(pattern)
184
+ stringPatternCache.set(pattern, regex)
185
+ }
186
+ return regex.test(value)
187
+ }
188
+ // Use .match() for user-supplied RegExp to preserve semantics regardless of `g`/`y` flags.
189
+ return value.match(pattern) !== null
190
+ }
13
191
 
14
192
  /**
15
193
  * Checks if an operation matches a pattern for a given filter type (`tag`, `operationId`, `path`, `method`).
@@ -17,13 +195,15 @@ type ResolverBuilder<T extends PluginFactoryOptions> = () => Omit<T['resolver'],
17
195
  function matchesOperationPattern(node: OperationNode, type: string, pattern: string | RegExp): boolean {
18
196
  switch (type) {
19
197
  case 'tag':
20
- return node.tags.some((tag) => !!tag.match(pattern))
198
+ return node.tags.some((tag) => testPattern(tag, pattern))
21
199
  case 'operationId':
22
- return !!node.operationId.match(pattern)
200
+ return testPattern(node.operationId, pattern)
23
201
  case 'path':
24
- return !!node.path.match(pattern)
202
+ return testPattern(node.path, pattern)
25
203
  case 'method':
26
- return !!(node.method.toLowerCase() as string).match(pattern)
204
+ return testPattern(node.method.toLowerCase(), pattern)
205
+ case 'contentType':
206
+ return node.requestBody?.content?.some((c) => testPattern(c.contentType, pattern)) ?? false
27
207
  default:
28
208
  return false
29
209
  }
@@ -31,21 +211,26 @@ function matchesOperationPattern(node: OperationNode, type: string, pattern: str
31
211
 
32
212
  /**
33
213
  * Checks if a schema matches a pattern for a given filter type (`schemaName`).
214
+ *
34
215
  * Returns `null` when the filter type doesn't apply to schemas.
35
216
  */
36
217
  function matchesSchemaPattern(node: SchemaNode, type: string, pattern: string | RegExp): boolean | null {
37
218
  switch (type) {
38
219
  case 'schemaName':
39
- return node.name ? !!node.name.match(pattern) : false
220
+ return node.name ? testPattern(node.name, pattern) : false
40
221
  default:
41
222
  return null
42
223
  }
43
224
  }
44
225
 
45
226
  /**
46
- * Default name resolver `camelCase` for most types, `PascalCase` for `type`.
227
+ * Default name resolver used by `defineResolver`.
228
+ *
229
+ * - `camelCase` for `function` and `file` types.
230
+ * - `PascalCase` for `type`.
231
+ * - `camelCase` for everything else.
47
232
  */
48
- function defaultResolver(name: ResolveNameParams['name'], type: ResolveNameParams['type']): string {
233
+ function defaultResolver(name: string, type?: 'file' | 'function' | 'type' | 'const'): string {
49
234
  let resolvedName = camelCase(name)
50
235
 
51
236
  if (type === 'file' || type === 'function') {
@@ -62,8 +247,27 @@ function defaultResolver(name: ResolveNameParams['name'], type: ResolveNameParam
62
247
  }
63
248
 
64
249
  /**
65
- * Default option resolver — applies include/exclude filters and merges any matching override options.
66
- * Returns `null` when the node is filtered out.
250
+ * Default option resolver — applies include/exclude filters and merges matching override options.
251
+ *
252
+ * Returns `null` when the node is filtered out by an `exclude` rule or not matched by any `include` rule.
253
+ *
254
+ * @example Include/exclude filtering
255
+ * ```ts
256
+ * const options = defaultResolveOptions(operationNode, {
257
+ * options: { output: 'types' },
258
+ * exclude: [{ type: 'tag', pattern: 'internal' }],
259
+ * })
260
+ * // → null when node has tag 'internal'
261
+ * ```
262
+ *
263
+ * @example Override merging
264
+ * ```ts
265
+ * const options = defaultResolveOptions(operationNode, {
266
+ * options: { enumType: 'asConst' },
267
+ * override: [{ type: 'operationId', pattern: 'listPets', options: { enumType: 'enum' } }],
268
+ * })
269
+ * // → { enumType: 'enum' } when operationId matches
270
+ * ```
67
271
  */
68
272
  export function defaultResolveOptions<TOptions>(
69
273
  node: Node,
@@ -106,26 +310,345 @@ export function defaultResolveOptions<TOptions>(
106
310
  }
107
311
 
108
312
  /**
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.
313
+ * Default path resolver used by `defineResolver`.
111
314
  *
112
- * @example
315
+ * - Returns the output directory in `single` mode.
316
+ * - Resolves into a tag- or path-based subdirectory when `group` and a `tag`/`path` value are provided.
317
+ * - Falls back to a flat `output/baseName` path otherwise.
318
+ *
319
+ * A custom `group.name` function overrides the default subdirectory naming.
320
+ * For `tag` groups the default is `${camelCase(tag)}Controller`.
321
+ * For `path` groups the default is the first path segment after `/`.
322
+ *
323
+ * @example Flat output
324
+ * ```ts
325
+ * defaultResolvePath({ baseName: 'petTypes.ts' }, { root: '/src', output: { path: 'types' } })
326
+ * // → '/src/types/petTypes.ts'
327
+ * ```
328
+ *
329
+ * @example Tag-based grouping
330
+ * ```ts
331
+ * defaultResolvePath(
332
+ * { baseName: 'petTypes.ts', tag: 'pets' },
333
+ * { root: '/src', output: { path: 'types' }, group: { type: 'tag' } },
334
+ * )
335
+ * // → '/src/types/petsController/petTypes.ts'
336
+ * ```
337
+ *
338
+ * @example Path-based grouping
339
+ * ```ts
340
+ * defaultResolvePath(
341
+ * { baseName: 'petTypes.ts', path: '/pets/list' },
342
+ * { root: '/src', output: { path: 'types' }, group: { type: 'path' } },
343
+ * )
344
+ * // → '/src/types/pets/petTypes.ts'
345
+ * ```
346
+ *
347
+ * @example Single-file mode
348
+ * ```ts
349
+ * defaultResolvePath(
350
+ * { baseName: 'petTypes.ts', pathMode: 'single' },
351
+ * { root: '/src', output: { path: 'types' } },
352
+ * )
353
+ * // → '/src/types'
354
+ * ```
355
+ */
356
+ export function defaultResolvePath({ baseName, pathMode, tag, path: groupPath }: ResolverPathParams, { root, output, group }: ResolverContext): string {
357
+ const mode = pathMode ?? getMode(path.resolve(root, output.path))
358
+
359
+ if (mode === 'single') {
360
+ return path.resolve(root, output.path)
361
+ }
362
+
363
+ let result: string
364
+
365
+ if (group && (groupPath || tag)) {
366
+ const groupValue = group.type === 'path' ? groupPath! : tag!
367
+ const defaultName =
368
+ group.type === 'tag'
369
+ ? ({ group: g }: { group: string }) => `${camelCase(g)}Controller`
370
+ : ({ group: g }: { group: string }) => {
371
+ // Strip traversal components (empty, '.', '..') before taking the first meaningful segment.
372
+ // When every segment is a traversal component (e.g. '../../') we fall back to '' so the
373
+ // file is placed directly in the output root — the boundary check below ensures safety.
374
+ const segment = g.split('/').filter((s) => s !== '' && s !== '.' && s !== '..')[0]
375
+ return segment ? camelCase(segment) : ''
376
+ }
377
+ const resolveName = group.name ?? defaultName
378
+ result = path.resolve(root, output.path, resolveName({ group: groupValue }), baseName)
379
+ } else {
380
+ result = path.resolve(root, output.path, baseName)
381
+ }
382
+
383
+ // Ensure the resolved path stays within the configured output directory.
384
+ // This prevents path traversal from malicious OpenAPI specs or custom group.name functions.
385
+ // `result === outputDir` is intentionally permitted: it matches single-file mode paths and
386
+ // edge cases where baseName resolves to the output directory itself.
387
+ const outputDir = path.resolve(root, output.path)
388
+ const outputDirWithSep = outputDir.endsWith(path.sep) ? outputDir : `${outputDir}${path.sep}`
389
+ if (result !== outputDir && !result.startsWith(outputDirWithSep)) {
390
+ throw new Error(
391
+ `[Kubb] Resolved path "${result}" is outside the output directory "${outputDir}". ` +
392
+ 'This may indicate a path traversal attempt in the OpenAPI specification or a misconfigured group.name function.',
393
+ )
394
+ }
395
+
396
+ return result
397
+ }
398
+
399
+ /**
400
+ * Default file resolver used by `defineResolver`.
401
+ *
402
+ * Resolves a `FileNode` by combining name resolution (`resolver.default`) with
403
+ * path resolution (`resolver.resolvePath`). The resolved file always has empty
404
+ * `sources`, `imports`, and `exports` arrays — consumers populate those separately.
405
+ *
406
+ * In `single` mode the name is omitted and the file sits directly in the output directory.
407
+ *
408
+ * @example Resolve a schema file
409
+ * ```ts
410
+ * const file = defaultResolveFile.call(
411
+ * resolver,
412
+ * { name: 'pet', extname: '.ts' },
413
+ * { root: '/src', output: { path: 'types' } },
414
+ * )
415
+ * // → { baseName: 'pet.ts', path: '/src/types/pet.ts', sources: [], ... }
416
+ * ```
417
+ *
418
+ * @example Resolve an operation file with tag grouping
419
+ * ```ts
420
+ * const file = defaultResolveFile.call(
421
+ * resolver,
422
+ * { name: 'listPets', extname: '.ts', tag: 'pets' },
423
+ * { root: '/src', output: { path: 'types' }, group: { type: 'tag' } },
424
+ * )
425
+ * // → { baseName: 'listPets.ts', path: '/src/types/petsController/listPets.ts', ... }
426
+ * ```
427
+ */
428
+ export function defaultResolveFile(this: Resolver, { name, extname, tag, path: groupPath }: ResolverFileParams, context: ResolverContext): FileNode {
429
+ const pathMode = getMode(path.resolve(context.root, context.output.path))
430
+ const resolvedName = pathMode === 'single' ? '' : this.default(name, 'file')
431
+ const baseName = `${resolvedName}${extname}` as FileNode['baseName']
432
+ const filePath = this.resolvePath({ baseName, pathMode, tag, path: groupPath }, context)
433
+
434
+ return createFile({
435
+ path: filePath,
436
+ baseName: path.basename(filePath) as `${string}.${string}`,
437
+ meta: {
438
+ pluginName: this.pluginName,
439
+ },
440
+ sources: [],
441
+ imports: [],
442
+ exports: [],
443
+ })
444
+ }
445
+
446
+ /**
447
+ * Generates the default "Generated by Kubb" banner from config and optional node metadata.
448
+ */
449
+ export function buildDefaultBanner({
450
+ title,
451
+ description,
452
+ version,
453
+ config,
454
+ }: {
455
+ title?: string
456
+ description?: string
457
+ version?: string
458
+ config: Config
459
+ }): string {
460
+ try {
461
+ let source = ''
462
+ if (Array.isArray(config.input)) {
463
+ const first = config.input[0]
464
+ if (first && 'path' in first) {
465
+ source = path.basename(first.path)
466
+ }
467
+ } else if (config.input && 'path' in config.input) {
468
+ source = path.basename(config.input.path)
469
+ } else if (config.input && 'data' in config.input) {
470
+ source = 'text content'
471
+ }
472
+
473
+ let banner = '/**\n* Generated by Kubb (https://kubb.dev/).\n* Do not edit manually.\n'
474
+
475
+ if (config.output.defaultBanner === 'simple') {
476
+ banner += '*/\n'
477
+ return banner
478
+ }
479
+
480
+ if (source) {
481
+ banner += `* Source: ${source}\n`
482
+ }
483
+
484
+ if (title) {
485
+ banner += `* Title: ${title}\n`
486
+ }
487
+
488
+ if (description) {
489
+ const formattedDescription = description.replace(/\n/gm, '\n* ')
490
+ banner += `* Description: ${formattedDescription}\n`
491
+ }
492
+
493
+ if (version) {
494
+ banner += `* OpenAPI spec version: ${version}\n`
495
+ }
496
+
497
+ banner += '*/\n'
498
+ return banner
499
+ } catch (_error) {
500
+ return '/**\n* Generated by Kubb (https://kubb.dev/).\n* Do not edit manually.\n*/'
501
+ }
502
+ }
503
+
504
+ /**
505
+ * Default banner resolver — returns the banner string for a generated file.
506
+ *
507
+ * A user-supplied `output.banner` overrides the default Kubb "Generated by Kubb" notice.
508
+ * When no `output.banner` is set, the Kubb notice is used (including `title` and `version`
509
+ * from the OAS spec when a `node` is provided).
510
+ *
511
+ * - When `output.banner` is a function and `node` is provided, returns `output.banner(node)`.
512
+ * - When `output.banner` is a function and `node` is absent, falls back to the Kubb notice.
513
+ * - When `output.banner` is a string, returns it directly.
514
+ * - When `config.output.defaultBanner` is `false`, returns `undefined`.
515
+ * - Otherwise returns the Kubb "Generated by Kubb" notice.
516
+ *
517
+ * @example String banner overrides default
518
+ * ```ts
519
+ * defaultResolveBanner(undefined, { output: { banner: '// my banner' }, config })
520
+ * // → '// my banner'
521
+ * ```
522
+ *
523
+ * @example Function banner with node
524
+ * ```ts
525
+ * defaultResolveBanner(inputNode, { output: { banner: (node) => `// v${node.version}` }, config })
526
+ * // → '// v3.0.0'
527
+ * ```
528
+ *
529
+ * @example No user banner — Kubb notice with OAS metadata
530
+ * ```ts
531
+ * defaultResolveBanner(inputNode, { config })
532
+ * // → '/** Generated by Kubb ... Title: Pet Store ... *\/'
533
+ * ```
534
+ *
535
+ * @example Disabled default banner
536
+ * ```ts
537
+ * defaultResolveBanner(undefined, { config: { output: { defaultBanner: false }, ...config } })
538
+ * // → undefined
539
+ * ```
540
+ */
541
+ export function defaultResolveBanner(node: InputNode | undefined, { output, config }: ResolveBannerContext): string | undefined {
542
+ if (typeof output?.banner === 'function') {
543
+ return output.banner(node)
544
+ }
545
+
546
+ if (typeof output?.banner === 'string') {
547
+ return output.banner
548
+ }
549
+
550
+ if (config.output.defaultBanner === false) {
551
+ return undefined
552
+ }
553
+
554
+ return buildDefaultBanner({
555
+ title: node?.meta?.title,
556
+ version: node?.meta?.version,
557
+ config,
558
+ })
559
+ }
560
+
561
+ /**
562
+ * Default footer resolver — returns the footer string for a generated file.
563
+ *
564
+ * - When `output.footer` is a function and `node` is provided, calls it with the node.
565
+ * - When `output.footer` is a function and `node` is absent, returns `undefined`.
566
+ * - When `output.footer` is a string, returns it directly.
567
+ * - Otherwise returns `undefined`.
568
+ *
569
+ * @example String footer
570
+ * ```ts
571
+ * defaultResolveFooter(undefined, { output: { footer: '// end of file' }, config })
572
+ * // → '// end of file'
573
+ * ```
574
+ *
575
+ * @example Function footer with node
576
+ * ```ts
577
+ * defaultResolveFooter(inputNode, { output: { footer: (node) => `// ${node.title}` }, config })
578
+ * // → '// Pet Store'
579
+ * ```
580
+ */
581
+ export function defaultResolveFooter(node: InputNode | undefined, { output }: ResolveBannerContext): string | undefined {
582
+ if (typeof output?.footer === 'function') {
583
+ return node ? output.footer(node) : undefined
584
+ }
585
+ if (typeof output?.footer === 'string') {
586
+ return output.footer
587
+ }
588
+ return undefined
589
+ }
590
+
591
+ /**
592
+ * Defines a resolver for a plugin, injecting built-in defaults for name casing,
593
+ * include/exclude/override filtering, path resolution, and file construction.
594
+ *
595
+ * All four defaults can be overridden by providing them in the builder function:
596
+ * - `default` — name casing strategy (camelCase / PascalCase)
597
+ * - `resolveOptions` — include/exclude/override filtering
598
+ * - `resolvePath` — output path computation
599
+ * - `resolveFile` — full `FileNode` construction
600
+ *
601
+ * Methods in the returned object can call sibling resolver methods via `this`.
602
+ *
603
+ * @example Basic resolver with naming helpers
604
+ * ```ts
113
605
  * export const resolver = defineResolver<PluginTs>(() => ({
114
- * resolveName(name) {
115
- * return this.default(name, 'function')
606
+ * name: 'default',
607
+ * resolveName(node) {
608
+ * return this.default(node.name, 'function')
609
+ * },
610
+ * resolveTypedName(node) {
611
+ * return this.default(node.name, 'type')
116
612
  * },
117
- * resolveTypedName(name) {
118
- * return this.default(name, 'type')
613
+ * }))
614
+ * ```
615
+ *
616
+ * @example Override resolvePath for a custom output structure
617
+ * ```ts
618
+ * export const resolver = defineResolver<PluginTs>(() => ({
619
+ * name: 'custom',
620
+ * resolvePath({ baseName }, { root, output }) {
621
+ * return path.resolve(root, output.path, 'generated', baseName)
119
622
  * },
623
+ * }))
624
+ * ```
625
+ *
626
+ * @example Use this.default inside a helper
627
+ * ```ts
628
+ * export const resolver = defineResolver<PluginTs>(() => ({
629
+ * name: 'default',
120
630
  * resolveParamName(node, param) {
121
- * return this.resolveName(`${node.operationId} ${param.in} ${param.name}`)
631
+ * return this.default(`${node.operationId} ${param.in} ${param.name}`, 'type')
122
632
  * },
123
633
  * }))
634
+ * ```
124
635
  */
125
636
  export function defineResolver<T extends PluginFactoryOptions>(build: ResolverBuilder<T>): T['resolver'] {
126
- return {
637
+ // `resolver` is kept so the default `resolveFile` wrapper can reference the fully assembled
638
+ // object via `.call(resolver, ...)` at call-time, after the result is assigned below.
639
+ let resolver: T['resolver']
640
+
641
+ const result = {
127
642
  default: defaultResolver,
128
643
  resolveOptions: defaultResolveOptions,
644
+ resolvePath: defaultResolvePath,
645
+ resolveFile: (params: ResolverFileParams, context: ResolverContext) => defaultResolveFile.call(resolver as Resolver, params, context),
646
+ resolveBanner: defaultResolveBanner,
647
+ resolveFooter: defaultResolveFooter,
129
648
  ...build(),
130
649
  } as T['resolver']
650
+
651
+ resolver = result
652
+
653
+ return resolver
131
654
  }