@kubb/core 5.0.0-beta.63 → 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 (39) hide show
  1. package/dist/{diagnostics-IjkPEgAO.d.ts → diagnostics-BqiNAWVS.d.ts} +9 -10
  2. package/dist/index.cjs +24 -34
  3. package/dist/index.cjs.map +1 -1
  4. package/dist/index.d.ts +3 -3
  5. package/dist/index.js +25 -35
  6. package/dist/index.js.map +1 -1
  7. package/dist/{memoryStorage-CWFzAz4o.js → memoryStorage-DWnhqUf2.js} +3 -3
  8. package/dist/{memoryStorage-CWFzAz4o.js.map → memoryStorage-DWnhqUf2.js.map} +1 -1
  9. package/dist/{memoryStorage-CUj1hrxa.cjs → memoryStorage-mojU6pbA.cjs} +2 -2
  10. package/dist/{memoryStorage-CUj1hrxa.cjs.map → memoryStorage-mojU6pbA.cjs.map} +1 -1
  11. package/dist/mocks.cjs +1 -1
  12. package/dist/mocks.d.ts +2 -2
  13. package/dist/mocks.js +2 -2
  14. package/package.json +4 -5
  15. package/src/FileManager.ts +0 -137
  16. package/src/FileProcessor.ts +0 -212
  17. package/src/KubbDriver.ts +0 -900
  18. package/src/Transform.ts +0 -105
  19. package/src/constants.ts +0 -126
  20. package/src/createAdapter.ts +0 -127
  21. package/src/createKubb.ts +0 -195
  22. package/src/createRenderer.ts +0 -72
  23. package/src/createReporter.ts +0 -134
  24. package/src/createStorage.ts +0 -83
  25. package/src/defineGenerator.ts +0 -211
  26. package/src/defineParser.ts +0 -63
  27. package/src/definePlugin.ts +0 -438
  28. package/src/defineResolver.ts +0 -711
  29. package/src/diagnostics.ts +0 -662
  30. package/src/index.ts +0 -20
  31. package/src/mocks.ts +0 -249
  32. package/src/reporters/cliReporter.ts +0 -89
  33. package/src/reporters/fileReporter.ts +0 -102
  34. package/src/reporters/jsonReporter.ts +0 -20
  35. package/src/reporters/report.ts +0 -85
  36. package/src/storages/fsStorage.ts +0 -82
  37. package/src/storages/memoryStorage.ts +0 -55
  38. package/src/types.ts +0 -829
  39. /package/dist/{chunk-C0LytTxp.js → rolldown-runtime-C0LytTxp.js} +0 -0
@@ -1,438 +0,0 @@
1
- import type { Enforce, FileNode, HttpMethod, Macro, UserFileNode } from '@kubb/ast'
2
- import { diagnosticCode } from './constants.ts'
3
- import type { Generator } from './defineGenerator.ts'
4
- import type { BannerMeta, Resolver } from './defineResolver.ts'
5
- import { Diagnostics } from './diagnostics.ts'
6
- import type { Config, KubbHooks } from './types.ts'
7
-
8
- /**
9
- * Reads a type from a registry, falling back to `{}` when the key is absent. Lets
10
- * `Kubb.ConfigOptionsRegistry` and `Kubb.PluginOptionsRegistry` be augmented without
11
- * touching core.
12
- *
13
- * @internal
14
- */
15
- type ExtractRegistryKey<T, K extends PropertyKey> = K extends keyof T ? T[K] : {}
16
-
17
- /**
18
- * How a plugin consolidates its generated code into files.
19
- * - `'directory'` writes one file per operation or schema under `path`.
20
- * - `'file'` writes everything into a single file.
21
- */
22
- export type OutputMode = 'directory' | 'file'
23
-
24
- /**
25
- * Output configuration shared by every plugin. Each plugin extends this with
26
- * its own keys via the `Kubb.PluginOptionsRegistry.output` interface merge.
27
- */
28
- export type Output<_TOptions = unknown> = {
29
- /**
30
- * Directory where the plugin writes its generated code, resolved against the global
31
- * `output.path` set on `defineConfig`. With `mode: 'file'`, this is the full output file
32
- * path and must include the extension (e.g. `'types.ts'`, `'models.py'`).
33
- */
34
- path: string
35
- /**
36
- * How generated code is consolidated into files.
37
- * - `'directory'` writes one file per operation or schema under `path`.
38
- * - `'file'` writes everything into a single file. The `path` must include the file extension.
39
- *
40
- * @default 'directory'
41
- */
42
- mode?: OutputMode
43
- /**
44
- * Text prepended to every generated file. Useful for license headers,
45
- * lint disables, or `@ts-nocheck` directives.
46
- *
47
- * A string is applied to every file (including barrel and aggregation re-export files).
48
- * Pass a function to compute the banner from the file's `BannerMeta` document metadata
49
- * plus per-file context (`isBarrel`, `isAggregation`, `filePath`, `baseName`), so you can
50
- * skip the banner on specific files.
51
- *
52
- * @example Add a directive to source files but not re-export files
53
- * `banner: (meta) => (meta.isBarrel || meta.isAggregation) ? '' : "'use server'"`
54
- */
55
- banner?: string | ((meta: BannerMeta) => string)
56
- /**
57
- * Text appended at the end of every generated file. Mirror of `banner`.
58
- * Pass a function to compute the footer from the file's `BannerMeta`.
59
- */
60
- footer?: string | ((meta: BannerMeta) => string)
61
- /**
62
- * Allows the plugin to overwrite hand-written files at the same path.
63
- * Defaults to `false` to protect manual edits.
64
- *
65
- * @default false
66
- */
67
- override?: boolean
68
- } & ExtractRegistryKey<Kubb.PluginOptionsRegistry, 'output'>
69
-
70
- /**
71
- * Groups generated files into subdirectories based on an OpenAPI tag or path
72
- * segment.
73
- */
74
- export type Group = {
75
- /**
76
- * Property used to assign each operation to a group.
77
- * - `'tag'` uses the first tag (`operation.getTags().at(0)?.name`).
78
- * - `'path'` uses the first segment of the operation's URL.
79
- */
80
- type: 'tag' | 'path'
81
- /**
82
- * Returns the subdirectory name from the group key. Defaults to the camelCased tag for
83
- * `tag` groups, or the camelCased first path segment for `path` groups.
84
- */
85
- name?: (context: { group: string }) => string
86
- }
87
-
88
- /**
89
- * Couples `output.mode` with the plugin's `group` option at the type level.
90
- * - `mode: 'file'` forbids `group` (a single file has nothing to group).
91
- * - `mode: 'directory'` (or no mode) allows an optional `group` to organize
92
- * files into per-group subdirectories.
93
- *
94
- * Intersect into a plugin's `Options` type instead of declaring `output` and
95
- * `group` directly, since `mode` lives inside `output` while `group` is its sibling.
96
- * The generic keeps a plugin's extended `Output` shape intact.
97
- *
98
- * @example
99
- * ```ts
100
- * export type Options = OutputOptions & {
101
- * exclude?: Array<Exclude>
102
- * }
103
- * ```
104
- */
105
- export type OutputOptions<TOutput extends Output = Output> =
106
- | {
107
- output?: TOutput & { mode?: 'directory' }
108
- group?: Group
109
- }
110
- | {
111
- output: TOutput & { mode: 'file' }
112
- group?: never
113
- }
114
-
115
- /**
116
- * Merges the `output.mode` default into the output config and validates the combination.
117
- * Throws `KUBB_INVALID_PLUGIN_OPTIONS` when `mode: 'file'` is paired with a `group` option,
118
- * since a single-file output has nothing to group.
119
- */
120
- export function normalizeOutput({ output, group, pluginName }: { output: Output; group?: Group | null; pluginName: string }): Output {
121
- const mode = output.mode ?? 'directory'
122
-
123
- if (mode === 'file' && group) {
124
- throw new Diagnostics.Error({
125
- code: diagnosticCode.invalidPluginOptions,
126
- severity: 'error',
127
- message: `Plugin "${pluginName}" sets \`output.mode: 'file'\` but also configures a \`group\` option.`,
128
- help: "A single-file output has nothing to group. Remove the `group` option, or use `output.mode: 'directory'` to organize files into subdirectories.",
129
- location: { kind: 'config' },
130
- plugin: pluginName,
131
- })
132
- }
133
-
134
- return { ...output, mode }
135
- }
136
-
137
- type ByTag = {
138
- /**
139
- * Filter by OpenAPI `tags` field. Matches one or more tags assigned to operations.
140
- */
141
- type: 'tag'
142
- /**
143
- * Tag name to match (case-sensitive). Can be a literal string or regex pattern.
144
- */
145
- pattern: string | RegExp
146
- }
147
-
148
- type ByOperationId = {
149
- /**
150
- * Filter by OpenAPI `operationId` field. Each operation (GET, POST, etc.) has a unique identifier.
151
- */
152
- type: 'operationId'
153
- /**
154
- * Operation ID to match (case-sensitive). Can be a literal string or regex pattern.
155
- */
156
- pattern: string | RegExp
157
- }
158
-
159
- type ByPath = {
160
- /**
161
- * Filter by OpenAPI `path` (URL endpoint). Useful to group or filter by service segments like `/pets`, `/users`, etc.
162
- */
163
- type: 'path'
164
- /**
165
- * URL path to match (case-sensitive). Can be a literal string or regex pattern. Matches against the full path.
166
- */
167
- pattern: string | RegExp
168
- }
169
-
170
- type ByMethod = {
171
- /**
172
- * Filter by HTTP method: `'GET'`, `'POST'`, `'PUT'`, `'PATCH'`, `'DELETE'`, `'HEAD'`, `'OPTIONS'`, `'TRACE'`.
173
- */
174
- type: 'method'
175
- /**
176
- * HTTP method to match, as one of the `HttpMethod` values (`'GET'`, `'POST'`, `'PUT'`,
177
- * `'PATCH'`, `'DELETE'`, `'HEAD'`, `'OPTIONS'`, `'TRACE'`) or a regex.
178
- */
179
- pattern: HttpMethod | RegExp
180
- }
181
-
182
- type BySchemaName = {
183
- /**
184
- * Filter by schema component name (TypeScript or JSON schema). Matches schemas in `#/components/schemas`.
185
- */
186
- type: 'schemaName'
187
- /**
188
- * Schema name to match (case-sensitive). Can be a literal string or regex pattern.
189
- */
190
- pattern: string | RegExp
191
- }
192
-
193
- type ByContentType = {
194
- /**
195
- * Filter by response or request content type: `'application/json'`, `'application/xml'`, etc.
196
- */
197
- type: 'contentType'
198
- /**
199
- * Content type to match (case-sensitive). Can be a literal string or regex pattern.
200
- */
201
- pattern: string | RegExp
202
- }
203
-
204
- /**
205
- * Filter that skips matching operations or schemas during generation, for example
206
- * deprecated endpoints or internal-only schemas.
207
- *
208
- * @example
209
- * ```ts
210
- * exclude: [
211
- * { type: 'tag', pattern: 'internal' },
212
- * { type: 'path', pattern: /^\/admin/ },
213
- * { type: 'operationId', pattern: /^deprecated_/ },
214
- * ]
215
- * ```
216
- */
217
- export type Exclude = ByTag | ByOperationId | ByPath | ByMethod | ByContentType | BySchemaName
218
-
219
- /**
220
- * Filter that restricts generation to operations or schemas matching at least
221
- * one entry. Useful for partial builds (one tag, one API version).
222
- *
223
- * @example
224
- * ```ts
225
- * include: [
226
- * { type: 'tag', pattern: 'public' },
227
- * { type: 'path', pattern: /^\/api\/v1/ },
228
- * ]
229
- * ```
230
- */
231
- export type Include = ByTag | ByOperationId | ByPath | ByMethod | ByContentType | BySchemaName
232
-
233
- /**
234
- * Filter paired with a partial options object. When the filter matches, the
235
- * options are merged on top of the plugin defaults for that operation only.
236
- * Useful for "this one tag goes to a different folder" rules.
237
- *
238
- * Entries are evaluated top to bottom. The first matching entry wins.
239
- *
240
- * @example
241
- * ```ts
242
- * override: [
243
- * {
244
- * type: 'tag',
245
- * pattern: 'admin',
246
- * options: { output: { path: './src/gen/admin' } },
247
- * },
248
- * {
249
- * type: 'operationId',
250
- * pattern: 'listPets',
251
- * options: { enumType: 'literal' },
252
- * },
253
- * ]
254
- * ```
255
- */
256
- export type Override<TOptions> = (ByTag | ByOperationId | ByPath | ByMethod | BySchemaName | ByContentType) & {
257
- options: Omit<Partial<TOptions>, 'override'>
258
- }
259
-
260
- export type PluginFactoryOptions<
261
- /**
262
- * Unique plugin name.
263
- */
264
- TName extends string = string,
265
- /**
266
- * User-facing plugin options.
267
- */
268
- TOptions extends object = object,
269
- /**
270
- * Plugin options after defaults are applied.
271
- */
272
- TResolvedOptions extends object = TOptions,
273
- /**
274
- * Resolver that encapsulates naming and path-resolution helpers.
275
- * Define with `defineResolver` and export alongside the plugin.
276
- */
277
- TResolver extends Resolver = Resolver,
278
- > = {
279
- name: TName
280
- options: TOptions
281
- resolvedOptions: TResolvedOptions
282
- resolver: TResolver
283
- }
284
-
285
- /**
286
- * Context passed to a plugin's `kubb:plugin:setup` handler, where it registers generators and
287
- * sets its resolver, transformer, and options.
288
- */
289
- export type KubbPluginSetupContext<TFactory extends PluginFactoryOptions = PluginFactoryOptions> = {
290
- /**
291
- * Register a generator dynamically. Generators fire during the AST walk (schema/operation/operations)
292
- * just like generators declared statically on `createPlugin`.
293
- */
294
- addGenerator<TElement = unknown>(generator: Generator<TFactory, TElement>): void
295
- /**
296
- * Set or override the resolver for this plugin.
297
- * The resolver controls file naming and path resolution.
298
- */
299
- setResolver(resolver: Partial<TFactory['resolver']>): void
300
- /**
301
- * Add a macro that rewrites AST nodes before they reach generators. Macros run in the order they
302
- * are added, after any macros from earlier `addMacro` calls.
303
- */
304
- addMacro(macro: Macro): void
305
- /**
306
- * Replace this plugin's macros with `macros`.
307
- */
308
- setMacros(macros: ReadonlyArray<Macro>): void
309
- /**
310
- * Set resolved options merged into the normalized plugin's `options`.
311
- * Call this in `kubb:plugin:setup` to provide options generators need.
312
- */
313
- setOptions(options: TFactory['resolvedOptions']): void
314
- /**
315
- * Inject a raw file into the build output, bypassing the generation pipeline.
316
- */
317
- injectFile(userFileNode: UserFileNode): void
318
- /**
319
- * Merge a partial config update into the current build configuration.
320
- */
321
- updateConfig(config: Partial<Config>): void
322
- /**
323
- * The resolved build configuration at setup time.
324
- */
325
- config: Config
326
- /**
327
- * The plugin's user-provided options.
328
- */
329
- options: TFactory['options']
330
- }
331
-
332
- /**
333
- * A plugin object produced by `definePlugin`. Its lifecycle handlers live under a single
334
- * `hooks` property rather than flat methods.
335
- */
336
- export type Plugin<TFactory extends PluginFactoryOptions = PluginFactoryOptions> = {
337
- /**
338
- * Unique name for the plugin, following the same naming convention as `createPlugin`.
339
- */
340
- name: string
341
- /**
342
- * Plugins that must be registered before this plugin executes.
343
- * An error is thrown at startup when any listed dependency is missing.
344
- */
345
- dependencies?: Array<string>
346
- /**
347
- * Controls the execution order of this plugin relative to others.
348
- *
349
- * - `'pre'` runs before all normal plugins.
350
- * - `'post'` runs after all normal plugins.
351
- * - `undefined` (default) runs in declaration order among normal plugins.
352
- *
353
- * Dependency constraints always take precedence over `enforce`.
354
- */
355
- enforce?: Enforce
356
- /**
357
- * The options passed by the user when calling the plugin factory.
358
- */
359
- options?: TFactory['options']
360
- /**
361
- * Lifecycle event handlers for this plugin.
362
- * Any event from the global `KubbHooks` map can be subscribed to here.
363
- */
364
- hooks: {
365
- [K in keyof KubbHooks as K extends 'kubb:plugin:setup' ? never : K]?: (...args: KubbHooks[K]) => void | Promise<void>
366
- } & {
367
- 'kubb:plugin:setup'?(ctx: KubbPluginSetupContext<TFactory>): void | Promise<void>
368
- }
369
- }
370
-
371
- /**
372
- * Normalized plugin after setup, with runtime fields populated. Internal only. Plugins use the
373
- * public `Plugin` type.
374
- *
375
- * @internal
376
- */
377
- export type NormalizedPlugin<TOptions extends PluginFactoryOptions = PluginFactoryOptions> = Plugin<TOptions> & {
378
- options: TOptions['resolvedOptions'] & {
379
- output: Output
380
- include?: Array<Include>
381
- exclude: Array<Exclude>
382
- override: Array<Override<TOptions['resolvedOptions']>>
383
- }
384
- resolver: TOptions['resolver']
385
- macros?: Array<Macro>
386
- generators?: Array<Generator>
387
- apply?: (config: Config) => boolean
388
- version?: string
389
- }
390
-
391
- export type KubbPluginStartContext = {
392
- plugin: NormalizedPlugin
393
- }
394
-
395
- export type KubbPluginEndContext = {
396
- plugin: NormalizedPlugin
397
- duration: number
398
- success: boolean
399
- error?: Error
400
- config: Config
401
- /**
402
- * Returns all files currently in the file manager (lazy snapshot).
403
- * Includes files added by plugins that have already run.
404
- */
405
- readonly files: ReadonlyArray<FileNode>
406
- /**
407
- * Upsert one or more files into the file manager.
408
- */
409
- upsertFile: (...files: Array<FileNode>) => void
410
- }
411
-
412
- /**
413
- * Wraps a plugin factory and returns a function that accepts user options and
414
- * yields a typed `Plugin`. Lifecycle handlers go inside a single `hooks` object.
415
- *
416
- * Pass a `PluginFactoryOptions` type parameter to get a typed `ctx` inside
417
- * `kubb:plugin:setup`. Plugin names should follow the `plugin-<feature>`
418
- * convention (`plugin-react-query`, `plugin-zod`, ...).
419
- *
420
- * @example
421
- * ```ts
422
- * import { definePlugin } from '@kubb/core'
423
- *
424
- * export const pluginTs = definePlugin((options: { prefix?: string } = {}) => ({
425
- * name: 'plugin-ts',
426
- * hooks: {
427
- * 'kubb:plugin:setup'(ctx) {
428
- * ctx.setResolver(resolverTs)
429
- * },
430
- * },
431
- * }))
432
- * ```
433
- */
434
- export function definePlugin<TFactory extends PluginFactoryOptions = PluginFactoryOptions>(
435
- factory: (options: TFactory['options']) => Plugin<TFactory>,
436
- ): (options?: TFactory['options']) => Plugin<TFactory> {
437
- return (options) => factory(options ?? ({} as TFactory['options']))
438
- }