@kubb/core 5.0.0-beta.62 → 5.0.0-beta.64

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 (43) hide show
  1. package/dist/{diagnostics-D0G07LHG.d.ts → diagnostics-BqiNAWVS.d.ts} +47 -40
  2. package/dist/index.cjs +47 -52
  3. package/dist/index.cjs.map +1 -1
  4. package/dist/index.d.ts +7 -8
  5. package/dist/index.js +49 -54
  6. package/dist/index.js.map +1 -1
  7. package/dist/{memoryStorage-CWFzAz4o.js → memoryStorage-DWnhqUf2.js} +3 -3
  8. package/dist/memoryStorage-DWnhqUf2.js.map +1 -0
  9. package/dist/{memoryStorage-CUj1hrxa.cjs → memoryStorage-mojU6pbA.cjs} +2 -2
  10. package/dist/memoryStorage-mojU6pbA.cjs.map +1 -0
  11. package/dist/mocks.cjs +2 -2
  12. package/dist/mocks.cjs.map +1 -1
  13. package/dist/mocks.d.ts +3 -3
  14. package/dist/mocks.js +3 -3
  15. package/dist/mocks.js.map +1 -1
  16. package/package.json +4 -5
  17. package/dist/memoryStorage-CUj1hrxa.cjs.map +0 -1
  18. package/dist/memoryStorage-CWFzAz4o.js.map +0 -1
  19. package/src/FileManager.ts +0 -137
  20. package/src/FileProcessor.ts +0 -212
  21. package/src/KubbDriver.ts +0 -893
  22. package/src/Transform.ts +0 -105
  23. package/src/constants.ts +0 -126
  24. package/src/createAdapter.ts +0 -127
  25. package/src/createKubb.ts +0 -196
  26. package/src/createRenderer.ts +0 -72
  27. package/src/createReporter.ts +0 -134
  28. package/src/createStorage.ts +0 -83
  29. package/src/defineGenerator.ts +0 -210
  30. package/src/defineParser.ts +0 -62
  31. package/src/definePlugin.ts +0 -437
  32. package/src/defineResolver.ts +0 -711
  33. package/src/diagnostics.ts +0 -662
  34. package/src/index.ts +0 -20
  35. package/src/mocks.ts +0 -249
  36. package/src/reporters/cliReporter.ts +0 -89
  37. package/src/reporters/fileReporter.ts +0 -103
  38. package/src/reporters/jsonReporter.ts +0 -20
  39. package/src/reporters/report.ts +0 -85
  40. package/src/storages/fsStorage.ts +0 -82
  41. package/src/storages/memoryStorage.ts +0 -55
  42. package/src/types.ts +0 -829
  43. /package/dist/{chunk-C0LytTxp.js → rolldown-runtime-C0LytTxp.js} +0 -0
@@ -1,711 +0,0 @@
1
- import path from 'node:path'
2
- import { camelCase, pascalCase, toFilePath } from '@internals/utils'
3
- import type { FileNode, InputMeta, Node, OperationNode, SchemaNode } from '@kubb/ast'
4
- import { operationDef, schemaDef } from '@kubb/ast'
5
- import * as factory from '@kubb/ast/factory'
6
- import { Diagnostics } from './diagnostics.ts'
7
- import type { PluginFactoryOptions } from './definePlugin.ts'
8
- import type { Config, Group, Output } from './types.ts'
9
-
10
- /**
11
- * Type/string pattern filter for include/exclude/override matching.
12
- */
13
- type PatternFilter = {
14
- type: string
15
- pattern: string | RegExp
16
- }
17
-
18
- /**
19
- * Pattern filter with partial option overrides applied when the pattern matches.
20
- */
21
- type PatternOverride<TOptions> = PatternFilter & {
22
- options: Omit<Partial<TOptions>, 'override'>
23
- }
24
-
25
- /**
26
- * Context for resolving filtered options for a given operation or schema node.
27
- *
28
- * @internal
29
- */
30
- export type ResolveOptionsContext<TOptions> = {
31
- options: TOptions
32
- exclude?: Array<PatternFilter>
33
- include?: Array<PatternFilter>
34
- override?: Array<PatternOverride<TOptions>>
35
- }
36
-
37
- /**
38
- * Base constraint for all plugin resolver objects.
39
- *
40
- * `default`, `resolveOptions`, `resolvePath`, `resolveFile`, `resolveBanner`, and `resolveFooter`
41
- * are injected automatically by `defineResolver`. Extend this type to add custom resolution methods.
42
- *
43
- * @example
44
- * ```ts
45
- * type MyResolver = Resolver & {
46
- * resolveName(node: SchemaNode): string
47
- * resolveTypedName(node: SchemaNode): string
48
- * }
49
- * ```
50
- */
51
- export type Resolver = {
52
- name: string
53
- pluginName: string
54
- default(name: string, type?: 'file' | 'function' | 'type' | 'const'): string
55
- resolveOptions<TOptions>(node: Node, context: ResolveOptionsContext<TOptions>): TOptions | null
56
- resolvePath(params: ResolverPathParams, context: ResolverContext): string
57
- resolveFile(params: ResolverFileParams, context: ResolverContext): FileNode
58
- resolveBanner(meta: InputMeta | undefined, context: ResolveBannerContext): string | null
59
- resolveFooter(meta: InputMeta | undefined, context: ResolveBannerContext): string | null
60
- }
61
-
62
- /**
63
- * File-specific parameters for `Resolver.resolvePath`.
64
- *
65
- * Pass alongside a `ResolverContext` to identify which file to resolve.
66
- * Provide `tag` for tag-based grouping or `path` for path-based grouping.
67
- *
68
- * @example
69
- * ```ts
70
- * resolver.resolvePath(
71
- * { baseName: 'petTypes.ts', tag: 'pets' },
72
- * { root: '/src', output: { path: 'types' }, group: { type: 'tag' } },
73
- * )
74
- * // → '/src/types/pets/petTypes.ts'
75
- * ```
76
- */
77
- export type ResolverPathParams = {
78
- baseName: FileNode['baseName']
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/pets/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
- * Per-file context describing the file a banner/footer is being resolved for.
144
- *
145
- * Supplied by the generator (or the barrel plugin) at resolve-time and merged
146
- * into `BannerMeta` so a `banner`/`footer` function can branch on the file kind,
147
- * e.g. omit a `'use server'` directive on re-export files.
148
- */
149
- export type ResolveBannerFile = {
150
- /**
151
- * Full output path of the file being generated.
152
- */
153
- path: string
154
- /**
155
- * File name only, e.g. `'stocks.ts'`.
156
- */
157
- baseName: string
158
- /**
159
- * `true` for `index.ts` re-export barrels.
160
- */
161
- isBarrel?: boolean
162
- /**
163
- * `true` for group `[dir]/[dir].ts` aggregation files.
164
- */
165
- isAggregation?: boolean
166
- }
167
-
168
- /**
169
- * Document metadata extended with per-file context, passed to a `banner`/`footer` function.
170
- *
171
- * Carries everything in {@link InputMeta} plus the file the banner is rendered into, so a
172
- * single function can decide per file (e.g. skip a directive on barrel/aggregation files).
173
- *
174
- * @example Skip a directive on re-export files
175
- * `banner: (meta) => (meta.isBarrel || meta.isAggregation) ? '' : "'use server'"`
176
- */
177
- export type BannerMeta = InputMeta & {
178
- /**
179
- * Full output path of the file being generated.
180
- */
181
- filePath: string
182
- /**
183
- * File name only, e.g. `'stocks.ts'`.
184
- */
185
- baseName: string
186
- /**
187
- * `true` for `index.ts` re-export barrels.
188
- */
189
- isBarrel: boolean
190
- /**
191
- * `true` for group `[dir]/[dir].ts` aggregation files.
192
- */
193
- isAggregation: boolean
194
- }
195
-
196
- /**
197
- * Context passed to `Resolver.resolveBanner` and `Resolver.resolveFooter`.
198
- *
199
- * `output` is optional, since not every plugin configures a banner/footer.
200
- * `config` carries the global Kubb config, used to derive the default Kubb banner.
201
- * `file` carries per-file context forwarded to a `banner`/`footer` function.
202
- *
203
- * @example
204
- * ```ts
205
- * resolver.resolveBanner(meta, { output: { banner: '// generated' }, config })
206
- * // → '// generated'
207
- * ```
208
- */
209
- export type ResolveBannerContext = {
210
- output?: Pick<Output, 'banner' | 'footer'>
211
- config: Config
212
- file?: ResolveBannerFile
213
- }
214
-
215
- /**
216
- * Merges document `meta` with per-file `file` context into the `BannerMeta` passed to a
217
- * `banner`/`footer` function. Missing fields default to empty/`false` so the object shape
218
- * is stable even when a caller (e.g. the barrel plugin) has no document metadata.
219
- */
220
- function buildBannerMeta({ meta, file }: { meta: InputMeta | undefined; file: ResolveBannerFile | undefined }): BannerMeta {
221
- return {
222
- title: meta?.title,
223
- description: meta?.description,
224
- version: meta?.version,
225
- baseURL: meta?.baseURL,
226
- circularNames: meta?.circularNames ?? [],
227
- enumNames: meta?.enumNames ?? [],
228
- filePath: file?.path ?? '',
229
- baseName: file?.baseName ?? '',
230
- isBarrel: file?.isBarrel ?? false,
231
- isAggregation: file?.isAggregation ?? false,
232
- }
233
- }
234
-
235
- /**
236
- * Builder type for the plugin-specific resolver fields.
237
- *
238
- * `default`, `resolveOptions`, `resolvePath`, `resolveFile`, `resolveBanner`, and `resolveFooter`
239
- * are optional, with built-in fallbacks injected when omitted.
240
- *
241
- * Methods in the returned object can call sibling resolver methods via `this`.
242
- */
243
- type ResolverBuilder<T extends PluginFactoryOptions> = () => Omit<
244
- T['resolver'],
245
- 'default' | 'resolveOptions' | 'resolvePath' | 'resolveFile' | 'resolveBanner' | 'resolveFooter' | 'name' | 'pluginName'
246
- > &
247
- Partial<Pick<T['resolver'], 'default' | 'resolveOptions' | 'resolvePath' | 'resolveFile' | 'resolveBanner' | 'resolveFooter'>> & {
248
- name: string
249
- pluginName: T['name']
250
- } & ThisType<T['resolver']>
251
-
252
- // String patterns are compiled lazily and cached, so the same filter is reused for every node.
253
- const stringPatternCache = new Map<string, RegExp>()
254
-
255
- function testPattern(value: string, pattern: string | RegExp): boolean {
256
- if (typeof pattern === 'string') {
257
- let regex = stringPatternCache.get(pattern)
258
- if (!regex) {
259
- regex = new RegExp(pattern)
260
- stringPatternCache.set(pattern, regex)
261
- }
262
- return regex.test(value)
263
- }
264
- // Use .match() for user-supplied RegExp to preserve semantics regardless of `g`/`y` flags.
265
- return value.match(pattern) !== null
266
- }
267
-
268
- /**
269
- * Checks if an operation matches a pattern for a given filter type (`tag`, `operationId`, `path`, `method`).
270
- */
271
- function matchesOperationPattern(node: OperationNode, type: string, pattern: string | RegExp): boolean {
272
- if (type === 'tag') return node.tags.some((tag) => testPattern(tag, pattern))
273
- if (type === 'operationId') return testPattern(node.operationId, pattern)
274
- if (type === 'path') return node.path !== undefined && testPattern(node.path, pattern)
275
- if (type === 'method') return node.method !== undefined && testPattern(node.method.toLowerCase(), pattern)
276
- if (type === 'contentType') return node.requestBody?.content?.some((c) => testPattern(c.contentType, pattern)) ?? false
277
- return false
278
- }
279
-
280
- /**
281
- * Checks if a schema matches a pattern for a given filter type (`schemaName`).
282
- *
283
- * Returns `null` when the filter type doesn't apply to schemas.
284
- */
285
- function matchesSchemaPattern(node: SchemaNode, type: string, pattern: string | RegExp): boolean | null {
286
- if (type === 'schemaName') return node.name ? testPattern(node.name, pattern) : false
287
- return null
288
- }
289
-
290
- /**
291
- * Default name resolver used by `defineResolver`.
292
- *
293
- * - `camelCase` for `file`, with dotted names split into `/`-joined nested paths.
294
- * - `PascalCase` for `type`.
295
- * - `camelCase` for `function` and everything else.
296
- */
297
- function defaultResolver(name: string, type?: 'file' | 'function' | 'type' | 'const'): string {
298
- if (type === 'file') return toFilePath(name)
299
- if (type === 'type') return pascalCase(name)
300
- return camelCase(name)
301
- }
302
-
303
- /**
304
- * Default option resolver. Applies include/exclude filters and merges matching override options.
305
- *
306
- * Returns `null` when the node is filtered out by an `exclude` rule or not matched by any `include` rule.
307
- *
308
- * @example Include/exclude filtering
309
- * ```ts
310
- * const options = defaultResolveOptions(operationNode, {
311
- * options: { output: 'types' },
312
- * exclude: [{ type: 'tag', pattern: 'internal' }],
313
- * })
314
- * // → null when node has tag 'internal'
315
- * ```
316
- *
317
- * @example Override merging
318
- * ```ts
319
- * const options = defaultResolveOptions(operationNode, {
320
- * options: { enumType: 'asConst' },
321
- * override: [{ type: 'operationId', pattern: 'listPets', options: { enumType: 'enum' } }],
322
- * })
323
- * // → { enumType: 'enum' } when operationId matches
324
- * ```
325
- */
326
- const resolveOptionsCache = new WeakMap<object, WeakMap<Node, { value: unknown }>>()
327
-
328
- function computeOptions<TOptions>(
329
- node: Node,
330
- options: TOptions,
331
- exclude: Array<PatternFilter>,
332
- include: Array<PatternFilter> | undefined,
333
- override: Array<PatternOverride<TOptions>>,
334
- ): TOptions | null {
335
- if (operationDef.is(node)) {
336
- if (exclude.some(({ type, pattern }) => matchesOperationPattern(node, type, pattern))) return null
337
- if (include && !include.some(({ type, pattern }) => matchesOperationPattern(node, type, pattern))) return null
338
-
339
- const overrideOptions = override.find(({ type, pattern }) => matchesOperationPattern(node, type, pattern))?.options
340
-
341
- return { ...options, ...overrideOptions }
342
- }
343
-
344
- if (schemaDef.is(node)) {
345
- if (exclude.some(({ type, pattern }) => matchesSchemaPattern(node, type, pattern) === true)) return null
346
- if (include) {
347
- const results = include.map(({ type, pattern }) => matchesSchemaPattern(node, type, pattern))
348
- const applicable = results.filter((result) => result !== null)
349
-
350
- if (applicable.length > 0 && !applicable.includes(true)) return null
351
- }
352
- const overrideOptions = override.find(({ type, pattern }) => matchesSchemaPattern(node, type, pattern) === true)?.options
353
-
354
- return { ...options, ...overrideOptions }
355
- }
356
-
357
- return options
358
- }
359
-
360
- function defaultResolveOptions<TOptions>(node: Node, { options, exclude = [], include, override = [] }: ResolveOptionsContext<TOptions>): TOptions | null {
361
- const optionsKey = options as object
362
- let byOptions = resolveOptionsCache.get(optionsKey)
363
- if (!byOptions) {
364
- byOptions = new WeakMap()
365
- resolveOptionsCache.set(optionsKey, byOptions)
366
- }
367
- const cached = byOptions.get(node)
368
- if (cached !== undefined) return cached.value as TOptions | null
369
-
370
- const result = computeOptions(node, options, exclude, include, override)
371
-
372
- byOptions.set(node, { value: result })
373
-
374
- return result
375
- }
376
-
377
- /**
378
- * Default path resolver used by `defineResolver`.
379
- *
380
- * - `mode: 'file'` resolves directly to `output.path` (the full file path, extension included).
381
- * - `mode: 'directory'` (default) resolves to `output.path/{baseName}`, or into a
382
- * subdirectory when `group` and a `tag`/`path` value are provided.
383
- *
384
- * A custom `group.name` function overrides the default subdirectory naming.
385
- * For `tag` groups the default is the camelCased tag.
386
- * For `path` groups the default is the first path segment after `/`.
387
- *
388
- * @example Flat output
389
- * ```ts
390
- * defaultResolvePath({ baseName: 'petTypes.ts' }, { root: '/src', output: { path: 'types' } })
391
- * // → '/src/types/petTypes.ts'
392
- * ```
393
- *
394
- * @example Tag-based grouping
395
- * ```ts
396
- * defaultResolvePath(
397
- * { baseName: 'petTypes.ts', tag: 'pets' },
398
- * { root: '/src', output: { path: 'types' }, group: { type: 'tag' } },
399
- * )
400
- * // → '/src/types/pets/petTypes.ts'
401
- * ```
402
- *
403
- * @example Path-based grouping
404
- * ```ts
405
- * defaultResolvePath(
406
- * { baseName: 'petTypes.ts', path: '/pets/list' },
407
- * { root: '/src', output: { path: 'types' }, group: { type: 'path' } },
408
- * )
409
- * // → '/src/types/pets/petTypes.ts'
410
- * ```
411
- *
412
- * @example Single file (`mode: 'file'`)
413
- * ```ts
414
- * defaultResolvePath(
415
- * { baseName: 'petTypes.ts' },
416
- * { root: '/src', output: { path: 'types.ts', mode: 'file' } },
417
- * )
418
- * // → '/src/types.ts'
419
- * ```
420
- */
421
- export function defaultResolvePath({ baseName, tag, path: groupPath }: ResolverPathParams, { root, output, group }: ResolverContext): string {
422
- const mode = output.mode ?? 'directory'
423
-
424
- if (mode === 'file') {
425
- return path.resolve(root, output.path)
426
- }
427
-
428
- const result: string = (() => {
429
- if (group && (groupPath || tag)) {
430
- const groupValue = group.type === 'path' ? groupPath! : tag!
431
- const defaultName =
432
- group.type === 'tag'
433
- ? ({ group: groupName }: { group: string }) => camelCase(groupName)
434
- : ({ group: groupName }: { group: string }) => {
435
- // Strip traversal components (empty, '.', '..') before taking the first meaningful segment.
436
- // When every segment is a traversal component (e.g. '../../') we fall back to '' so the
437
- // file is placed directly in the output root, and the boundary check below ensures safety.
438
- const segment = groupName.split('/').filter((part) => part !== '' && part !== '.' && part !== '..')[0]
439
- return segment ? camelCase(segment) : ''
440
- }
441
- const resolveName = group.name ?? defaultName
442
- const groupName = resolveName({ group: groupValue })
443
-
444
- return path.resolve(root, output.path, groupName, baseName)
445
- }
446
- return path.resolve(root, output.path, baseName)
447
- })()
448
-
449
- // Ensure the resolved path stays within the configured output directory.
450
- // This prevents path traversal from malicious OpenAPI specs or custom group.name functions.
451
- // `result === outputDir` is intentionally permitted: it matches edge cases where baseName
452
- // resolves to the output directory itself.
453
- const outputDir = path.resolve(root, output.path)
454
- const outputDirWithSep = outputDir.endsWith(path.sep) ? outputDir : `${outputDir}${path.sep}`
455
- if (result !== outputDir && !result.startsWith(outputDirWithSep)) {
456
- throw new Diagnostics.Error({
457
- code: Diagnostics.code.pathTraversal,
458
- severity: 'error',
459
- message: `Resolved path "${result}" is outside the output directory "${outputDir}".`,
460
- help: 'This can stem from a path traversal in the OpenAPI specification or a misconfigured `group.name` function. Keep generated paths within the output directory.',
461
- location: { kind: 'config' },
462
- })
463
- }
464
-
465
- return result
466
- }
467
-
468
- /**
469
- * Default file resolver used by `defineResolver`.
470
- *
471
- * Resolves a `FileNode` by combining name resolution (`resolver.default`) with
472
- * path resolution (`resolver.resolvePath`). The resolved file always has empty
473
- * `sources`, `imports`, and `exports` arrays, which consumers populate separately.
474
- *
475
- * In `mode: 'file'` the name is omitted and the file sits directly at the output path.
476
- *
477
- * @example Resolve a schema file
478
- * ```ts
479
- * const file = defaultResolveFile.call(
480
- * resolver,
481
- * { name: 'pet', extname: '.ts' },
482
- * { root: '/src', output: { path: 'types' } },
483
- * )
484
- * // → { baseName: 'pet.ts', path: '/src/types/pet.ts', sources: [], ... }
485
- * ```
486
- *
487
- * @example Resolve an operation file with tag grouping
488
- * ```ts
489
- * const file = defaultResolveFile.call(
490
- * resolver,
491
- * { name: 'listPets', extname: '.ts', tag: 'pets' },
492
- * { root: '/src', output: { path: 'types' }, group: { type: 'tag' } },
493
- * )
494
- * // → { baseName: 'listPets.ts', path: '/src/types/pets/listPets.ts', ... }
495
- * ```
496
- */
497
- export function defaultResolveFile(this: Resolver, { name, extname, tag, path: groupPath }: ResolverFileParams, context: ResolverContext): FileNode {
498
- const mode = context.output.mode ?? 'directory'
499
- const resolvedName = mode === 'file' ? '' : this.default(name, 'file')
500
- const baseName = `${resolvedName}${extname}` as FileNode['baseName']
501
- const filePath = this.resolvePath({ baseName, tag, path: groupPath }, context)
502
-
503
- return factory.createFile({
504
- path: filePath,
505
- baseName: path.basename(filePath) as `${string}.${string}`,
506
- meta: {
507
- pluginName: this.pluginName,
508
- },
509
- sources: [],
510
- imports: [],
511
- exports: [],
512
- })
513
- }
514
-
515
- /**
516
- * Generates the default "Generated by Kubb" banner from config and optional node metadata.
517
- */
518
- function buildDefaultBanner({ title, description, version, config }: { title?: string; description?: string; version?: string; config: Config }): string {
519
- try {
520
- const source = (() => {
521
- if (Array.isArray(config.input)) {
522
- const first = config.input[0]
523
- if (first && 'path' in first) return path.basename(first.path)
524
- return ''
525
- }
526
- if (config.input && 'path' in config.input) return path.basename(config.input.path)
527
- if (config.input && 'data' in config.input) return 'text content'
528
- return ''
529
- })()
530
-
531
- let banner = '/**\n* Generated by Kubb (https://kubb.dev/).\n* Do not edit manually.\n'
532
-
533
- if (config.output.defaultBanner === 'simple') {
534
- banner += '*/\n'
535
- return banner
536
- }
537
-
538
- if (source) {
539
- banner += `* Source: ${source}\n`
540
- }
541
-
542
- if (title) {
543
- banner += `* Title: ${title}\n`
544
- }
545
-
546
- if (description) {
547
- const formattedDescription = description.replace(/\n/gm, '\n* ')
548
- banner += `* Description: ${formattedDescription}\n`
549
- }
550
-
551
- if (version) {
552
- banner += `* OpenAPI spec version: ${version}\n`
553
- }
554
-
555
- banner += '*/\n'
556
- return banner
557
- } catch (_error) {
558
- return '/**\n* Generated by Kubb (https://kubb.dev/).\n* Do not edit manually.\n*/'
559
- }
560
- }
561
-
562
- /**
563
- * Default banner resolver. Returns the banner string for a generated file.
564
- *
565
- * A user-supplied `output.banner` overrides the default Kubb "Generated by Kubb" notice.
566
- * When no `output.banner` is set, the Kubb notice is used (including `title` and `version`
567
- * from the document metadata when `meta` is provided).
568
- *
569
- * - When `output.banner` is a function, calls it with the file's `BannerMeta` and returns the result.
570
- * - When `output.banner` is a string, returns it directly.
571
- * - When `config.output.defaultBanner` is `false`, returns `undefined`.
572
- * - Otherwise returns the Kubb "Generated by Kubb" notice.
573
- *
574
- * @example String banner overrides default
575
- * ```ts
576
- * defaultResolveBanner(undefined, { output: { banner: '// my banner' }, config })
577
- * // → '// my banner'
578
- * ```
579
- *
580
- * @example Function banner with metadata
581
- * ```ts
582
- * defaultResolveBanner(meta, { output: { banner: (m) => `// v${m.version}` }, config })
583
- * // → '// v3.0.0'
584
- * ```
585
- *
586
- * @example Function banner skips re-export files
587
- * ```ts
588
- * defaultResolveBanner(meta, { output: { banner: (m) => (m.isBarrel ? '' : "'use server'") }, config, file: { path, baseName, isBarrel: true } })
589
- * // → ''
590
- * ```
591
- *
592
- * @example No user banner, Kubb notice with OAS metadata
593
- * ```ts
594
- * defaultResolveBanner(meta, { config })
595
- * // → '/** Generated by Kubb ... Title: Pet Store ... *\/'
596
- * ```
597
- *
598
- * @example Disabled default banner
599
- * ```ts
600
- * defaultResolveBanner(undefined, { config: { output: { defaultBanner: false }, ...config } })
601
- * // → null
602
- * ```
603
- */
604
- export function defaultResolveBanner(meta: InputMeta | undefined, { output, config, file }: ResolveBannerContext): string | null {
605
- if (typeof output?.banner === 'function') {
606
- return output.banner(buildBannerMeta({ meta, file }))
607
- }
608
-
609
- if (typeof output?.banner === 'string') {
610
- return output.banner
611
- }
612
-
613
- if (config.output.defaultBanner === false) {
614
- return null
615
- }
616
-
617
- return buildDefaultBanner({
618
- title: meta?.title,
619
- version: meta?.version,
620
- config,
621
- })
622
- }
623
-
624
- /**
625
- * Default footer resolver. Returns the footer string for a generated file.
626
- *
627
- * - When `output.footer` is a function, calls it with the file's `BannerMeta` and returns the result.
628
- * - When `output.footer` is a string, returns it directly.
629
- * - Otherwise returns `undefined`.
630
- *
631
- * @example String footer
632
- * ```ts
633
- * defaultResolveFooter(undefined, { output: { footer: '// end of file' }, config })
634
- * // → '// end of file'
635
- * ```
636
- *
637
- * @example Function footer with metadata
638
- * ```ts
639
- * defaultResolveFooter(meta, { output: { footer: (m) => `// ${m.title}` }, config })
640
- * // → '// Pet Store'
641
- * ```
642
- */
643
- export function defaultResolveFooter(meta: InputMeta | undefined, { output, file }: ResolveBannerContext): string | null {
644
- if (typeof output?.footer === 'function') {
645
- return output.footer(buildBannerMeta({ meta, file }))
646
- }
647
- if (typeof output?.footer === 'string') {
648
- return output.footer
649
- }
650
- return null
651
- }
652
-
653
- /**
654
- * Defines a plugin resolver. The resolver is the object that decides what
655
- * every generated symbol and file path is called. Built-in defaults handle
656
- * name casing, include/exclude/override filtering, output path computation,
657
- * and file construction. Supply your own to override any of them:
658
- *
659
- * - `default` sets the name casing strategy (camelCase or PascalCase).
660
- * - `resolveOptions` does include/exclude/override filtering.
661
- * - `resolvePath` computes the output path.
662
- * - `resolveFile` builds the full `FileNode`.
663
- * - `resolveBanner` and `resolveFooter` produce the top and bottom of file text.
664
- *
665
- * Methods in the returned object can call sibling resolver methods via `this`.
666
- * A custom rule can delegate to a default, for example `this.default(name, 'type')`.
667
- *
668
- * @example Basic resolver with naming helpers
669
- * ```ts
670
- * export const resolverTs = defineResolver<PluginTs>(() => ({
671
- * name: 'default',
672
- * resolveName(name) {
673
- * return this.default(name, 'function')
674
- * },
675
- * resolveTypeName(name) {
676
- * return this.default(name, 'type')
677
- * },
678
- * }))
679
- * ```
680
- *
681
- * @example Custom output path
682
- * ```ts
683
- * import path from 'node:path'
684
- *
685
- * export const resolverTs = defineResolver<PluginTs>(() => ({
686
- * name: 'custom',
687
- * resolvePath({ baseName }, { root, output }) {
688
- * return path.resolve(root, output.path, 'generated', baseName)
689
- * },
690
- * }))
691
- * ```
692
- */
693
- export function defineResolver<T extends PluginFactoryOptions>(build: ResolverBuilder<T>): T['resolver'] {
694
- // `resolver` is kept so the default `resolveFile` wrapper can reference the fully assembled
695
- // object via `.call(resolver, ...)` at call-time, after the result is assigned below.
696
- let resolver: T['resolver']
697
-
698
- const result = {
699
- default: defaultResolver,
700
- resolveOptions: defaultResolveOptions,
701
- resolvePath: defaultResolvePath,
702
- resolveFile: (params: ResolverFileParams, context: ResolverContext) => defaultResolveFile.call(resolver as Resolver, params, context),
703
- resolveBanner: defaultResolveBanner,
704
- resolveFooter: defaultResolveFooter,
705
- ...build(),
706
- } as T['resolver']
707
-
708
- resolver = result
709
-
710
- return resolver
711
- }